Skip to content

Commit 08303d9

Browse files
committed
Major refactoring and libsecp256k1 bindings
We are now using the proper cmd, internal, pkg structure. The fucntions and information flow within the program will still need to be overhauled.
1 parent 15edd33 commit 08303d9

40 files changed

+335
-677
lines changed

NOTES.md

Lines changed: 0 additions & 84 deletions
This file was deleted.

blindbit.example.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# possible values: trace, debug, info, warn, error
2+
log_level = "debug"
3+
14
# 0.0.0.0:8000 to expose outside of localhost
25
# default: "127.0.0.1:8000"
36
host = "127.0.0.1:8000"

cmd/blindbit-oracle/main.go

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main
22

33
import (
4+
"errors"
45
"flag"
56
"fmt"
6-
"log"
77
"path"
88

99
"os"
@@ -27,27 +27,41 @@ var (
2727
)
2828

2929
func init() {
30-
flag.StringVar(&config.BaseDirectory, "datadir", config.DefaultBaseDirectory, "Set the base directory for blindbit oracle. Default directory is ~/.blindbit-oracle")
31-
flag.BoolVar(&displayVersion, "version", false, "show version of blindbit-oracle")
32-
flag.BoolVar(&pruneOnStart, "reprune", false, "set this flag if you want to prune on startup")
33-
flag.BoolVar(&exportData, "export-data", false, "export the databases")
30+
flag.StringVar(
31+
&config.BaseDirectory,
32+
"datadir",
33+
config.DefaultBaseDirectory,
34+
"Set the base directory for blindbit oracle. Default directory is ~/.blindbit-oracle",
35+
)
36+
flag.BoolVar(
37+
&displayVersion,
38+
"version",
39+
false,
40+
"show version of blindbit-oracle",
41+
)
42+
flag.BoolVar(
43+
&pruneOnStart,
44+
"reprune",
45+
false,
46+
"set this flag if you want to prune on startup",
47+
)
48+
flag.BoolVar(
49+
&exportData,
50+
"export-data",
51+
false,
52+
"export the databases",
53+
)
3454
flag.Parse()
3555

3656
if displayVersion {
3757
// we only need the version for this
3858
return
3959
}
60+
4061
config.SetDirectories() // todo a proper set settings function which does it all would be good to avoid several small function calls
4162
err := os.Mkdir(config.BaseDirectory, 0750)
42-
if err != nil && !strings.Contains(err.Error(), "file exists") {
43-
fmt.Println(err.Error())
44-
log.Fatal(err)
45-
}
46-
47-
err = os.Mkdir(config.LogsPath, 0750)
48-
if err != nil && !strings.Contains(err.Error(), "file exists") {
49-
fmt.Println(err.Error())
50-
log.Fatal(err)
63+
if err != nil && !errors.Is(err, os.ErrExist) {
64+
logging.L.Fatal().Err(err).Msg("error creating base directory")
5165
}
5266

5367
logging.L.Info().Msgf("base directory %s", config.BaseDirectory)
@@ -59,7 +73,7 @@ func init() {
5973
err = os.Mkdir(config.DBPath, 0750)
6074
if err != nil && !strings.Contains(err.Error(), "file exists") {
6175
logging.L.Err(err).Msg("error creating db path")
62-
panic(err)
76+
os.Exit(1)
6377
}
6478

6579
// open levelDB connections
@@ -68,23 +82,23 @@ func init() {
6882
if config.CookiePath != "" {
6983
data, err := os.ReadFile(config.CookiePath)
7084
if err != nil {
71-
panic(err)
85+
logging.L.Fatal().Err(err).Msg("error reading cookie file")
7286
}
7387

7488
credentials := strings.Split(string(data), ":")
7589
if len(credentials) != 2 {
76-
panic("cookie file is invalid")
90+
logging.L.Fatal().Msg("cookie file is invalid")
7791
}
7892
config.RpcUser = credentials[0]
7993
config.RpcPass = credentials[1]
8094
}
8195

8296
if config.RpcUser == "" {
83-
panic("rpc user not set") // todo use cookie file to circumvent this requirement
97+
logging.L.Fatal().Msg("rpc user not set") // todo use cookie file to circumvent this requirement
8498
}
8599

86100
if config.RpcPass == "" {
87-
panic("rpc pass not set") // todo use cookie file to circumvent this requirement
101+
logging.L.Fatal().Msg("rpc pass not set") // todo use cookie file to circumvent this requirement
88102
}
89103
}
90104

@@ -96,7 +110,6 @@ func main() {
96110
defer logging.L.Info().Msg("Program shut down")
97111
defer dblevel.CloseDBs()
98112

99-
//log.SetFlags(log.LstdFlags | log.Lshortfile | log.Lmicroseconds)
100113
interrupt := make(chan os.Signal, 1)
101114
signal.Notify(interrupt, os.Interrupt)
102115

@@ -108,7 +121,8 @@ func main() {
108121

109122
if exportData {
110123
logging.L.Info().Msg("Exporting data")
111-
dataexport.ExportUTXOs(fmt.Sprintf("%s/export/utxos.csv", config.BaseDirectory))
124+
dataexport.ExportAll()
125+
// dataexport.ExportUTXOs(fmt.Sprintf("%s/export/utxos.csv", config.BaseDirectory))
112126
return
113127
}
114128

@@ -141,10 +155,9 @@ func main() {
141155
// only call this if you need to reindex. It doesn't delete anything but takes a couple of minutes to finish
142156
//err := core.ReindexDustLimitsOnly()
143157
//if err != nil {
144-
// common.ErrorLogger.Fatalln(err)
158+
// logging.L.Err(err).Msg("error reindexing dust limits")
145159
// return
146160
//}
147-
148161
}()
149162

150163
for {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ require (
3333
github.com/go-playground/locales v0.14.1 // indirect
3434
github.com/go-playground/universal-translator v0.18.1 // indirect
3535
github.com/go-playground/validator/v10 v10.24.0 // indirect
36+
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1 // indirect
3637
github.com/goccy/go-json v0.10.4 // indirect
3738
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
3839
github.com/hashicorp/hcl v1.0.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
7070
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
7171
github.com/go-playground/validator/v10 v10.24.0 h1:KHQckvo8G6hlWnrPX4NJJ+aBfWNAE/HH+qdL2cBpCmg=
7272
github.com/go-playground/validator/v10 v10.24.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus=
73+
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1 h1:FWNFq4fM1wPfcK40yHE5UO3RUdSNPaBC+j3PokzA6OQ=
74+
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI=
7375
github.com/goccy/go-json v0.10.4 h1:JSwxQzIqKfmFX1swYPpUThQZp/Ka4wzJdK0LWVytLPM=
7476
github.com/goccy/go-json v0.10.4/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
7577
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=

internal/config/config.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"errors"
55

6+
"github.com/rs/zerolog"
67
"github.com/setavenger/blindbit-lib/logging"
78
"github.com/spf13/viper"
89
)
@@ -30,7 +31,7 @@ func LoadConfigs(pathToConfig string) {
3031
viper.SetDefault("tweaks_full_basic", true)
3132
viper.SetDefault("tweaks_full_with_dust_filter", false)
3233
viper.SetDefault("tweaks_cut_through_with_dust_filter", false)
33-
34+
viper.SetDefault("log_level", "info")
3435
// Bind viper keys to environment variables (optional, for backup)
3536
viper.AutomaticEnv()
3637
viper.BindEnv("host", "HOST")
@@ -46,12 +47,13 @@ func LoadConfigs(pathToConfig string) {
4647
viper.BindEnv("tweaks_full_basic", "TWEAKS_FULL_BASIC")
4748
viper.BindEnv("tweaks_full_with_dust_filter", "TWEAKS_FULL_WITH_DUST_FILTER")
4849
viper.BindEnv("tweaks_cut_through_with_dust_filter", "TWEAKS_CUT_THROUGH_WITH_DUST_FILTER")
50+
viper.BindEnv("log_level", "LOG_LEVEL")
4951

5052
/* read and set config variables */
5153
// General
5254
SyncStartHeight = viper.GetUint32("sync_start_height")
5355
Host = viper.GetString("host")
54-
56+
LogLevel = viper.GetString("log_level")
5557
// Performance
5658
MaxParallelRequests = viper.GetUint16("max_parallel_requests")
5759
MaxParallelTweakComputations = viper.GetInt("max_parallel_tweak_computations")
@@ -80,7 +82,21 @@ func LoadConfigs(pathToConfig string) {
8082
case "testnet":
8183
Chain = Testnet3
8284
default:
83-
panic("chain undefined")
85+
logging.L.Fatal().Msg("chain undefined")
86+
return
87+
}
88+
89+
switch LogLevel {
90+
case "trace":
91+
logging.SetLogLevel(zerolog.TraceLevel)
92+
case "info":
93+
logging.SetLogLevel(zerolog.InfoLevel)
94+
case "debug":
95+
logging.SetLogLevel(zerolog.DebugLevel)
96+
case "warn":
97+
logging.SetLogLevel(zerolog.WarnLevel)
98+
case "error":
99+
logging.SetLogLevel(zerolog.ErrorLevel)
84100
}
85101

86102
// todo print settings

internal/config/vars.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package config
22

3-
import "github.com/setavenger/blindbit-lib/utils"
3+
import (
4+
"github.com/setavenger/blindbit-lib/logging"
5+
"github.com/setavenger/blindbit-lib/utils"
6+
)
47

58
// TaprootActivation
69
// todo might be inapplicable due to transactions that have taproot prevouts from before the activation
710
//
811
// is relevant for the height-to-hash lookup in the db
912

13+
var (
14+
LogLevel = "info"
15+
)
16+
1017
const (
1118
TaprootActivation uint32 = 709632
1219
ConfigFileName string = "blindbit.toml"
@@ -63,7 +70,7 @@ var (
6370
PruneFrequency = 72
6471
)
6572

66-
// one has to call SetDirectories otherwise common.DBPath will be empty
73+
// one has to call SetDirectories otherwise config.DBPath will be empty
6774
var (
6875
DBPathHeaders string
6976
DBPathHeadersInv string // for height to blockHash mapping
@@ -99,6 +106,7 @@ func SetDirectories() {
99106
func HeaderMustSyncHeight() uint32 {
100107
switch Chain {
101108
case Mainnet:
109+
// height based on heuristic checks to see where no old taproot style coins were locked
102110
return 500_000
103111
case Signet:
104112
return 1
@@ -107,7 +115,8 @@ func HeaderMustSyncHeight() uint32 {
107115
case Testnet3:
108116
return 1
109117
case Unknown:
110-
panic("chain not defined")
118+
logging.L.Panic().Msg("chain not defined")
119+
return 0
111120
default:
112121
return 1
113122
}
@@ -124,7 +133,8 @@ func ChainToString(c chain) string {
124133
case Testnet3:
125134
return "testnet"
126135
default:
127-
panic("chain not defined")
136+
logging.L.Panic().Msg("chain not defined")
137+
return ""
128138
}
129139

130140
}

internal/core/block_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ func TestBlockAnalysis(t *testing.T) {
2323
}
2424

2525
for _, tweak := range tweaks {
26-
logging.L.Info().Bytes("tweak", tweak.TweakData).Str("txid", tweak.Txid).Msg("tweak")
26+
logging.L.Info().Hex("tweak", tweak.TweakData[:]).Str("txid", tweak.Txid).Msg("tweak")
2727
}
2828

2929
for _, tx := range block.Txs {
3030
for _, tweak := range tweaks {
3131
if tx.Txid == tweak.Txid {
32-
logging.L.Info().Hex("tweak", tweak.TweakData).Msg("tweak")
32+
logging.L.Info().Hex("tweak", tweak.TweakData[:]).Msg("tweak")
3333
}
3434
}
3535
}

internal/core/cleanup.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"errors"
55

66
"github.com/setavenger/blindbit-lib/logging"
7-
config "github.com/setavenger/blindbit-oracle/internal/conig"
7+
"github.com/setavenger/blindbit-oracle/internal/config"
88
"github.com/setavenger/blindbit-oracle/internal/dblevel"
99
"github.com/setavenger/blindbit-oracle/internal/types"
1010
)
1111

1212
func overwriteUTXOsWithLookUp(utxos []types.UTXO) error {
13-
logging.L.Info().Msg("overwriting utxos with lookup")
13+
logging.L.Trace().Msg("overwriting utxos with lookup")
1414
var utxosToOverwrite []*types.UTXO
1515

1616
for _, utxo := range utxos {
@@ -56,7 +56,7 @@ func overwriteUTXOsWithLookUp(utxos []types.UTXO) error {
5656

5757
// todo construct the subsequent deletion of all utxos per transaction once all per transaction are spent
5858
func markSpentUTXOsAndTweaks(utxos []types.UTXO) error {
59-
logging.L.Info().Msg("marking utxos")
59+
logging.L.Trace().Msg("marking utxos")
6060
if len(utxos) == 0 {
6161
if config.Chain == config.Mainnet {
6262
// no warnings on other chains as it is very likely to not have any taproot outputs for several blocks on end

0 commit comments

Comments
 (0)