Skip to content

Commit 302620b

Browse files
committed
unfinished refactoring, trying to get rid of common
1 parent 8886e2f commit 302620b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+917
-659
lines changed

cmd/blindbit-oracle/main.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"log"
7+
"path"
8+
9+
"os"
10+
"os/signal"
11+
"strings"
12+
"time"
13+
14+
"github.com/setavenger/blindbit-lib/logging"
15+
"github.com/setavenger/blindbit-oracle/internal/config"
16+
"github.com/setavenger/blindbit-oracle/internal/core"
17+
"github.com/setavenger/blindbit-oracle/internal/dataexport"
18+
"github.com/setavenger/blindbit-oracle/internal/dblevel"
19+
"github.com/setavenger/blindbit-oracle/internal/server"
20+
)
21+
22+
var (
23+
displayVersion bool
24+
pruneOnStart bool
25+
exportData bool
26+
Version = "0.0.0"
27+
)
28+
29+
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")
34+
flag.Parse()
35+
36+
if displayVersion {
37+
// we only need the version for this
38+
return
39+
}
40+
config.SetDirectories() // todo a proper set settings function which does it all would be good to avoid several small function calls
41+
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)
51+
}
52+
53+
logging.L.Info().Msgf("base directory %s", config.BaseDirectory)
54+
55+
// load after loggers are instantiated
56+
config.LoadConfigs(path.Join(config.BaseDirectory, config.ConfigFileName))
57+
58+
// create DB path
59+
err = os.Mkdir(config.DBPath, 0750)
60+
if err != nil && !strings.Contains(err.Error(), "file exists") {
61+
logging.L.Err(err).Msg("error creating db path")
62+
panic(err)
63+
}
64+
65+
// open levelDB connections
66+
openLevelDBConnections()
67+
68+
if config.CookiePath != "" {
69+
data, err := os.ReadFile(config.CookiePath)
70+
if err != nil {
71+
panic(err)
72+
}
73+
74+
credentials := strings.Split(string(data), ":")
75+
if len(credentials) != 2 {
76+
panic("cookie file is invalid")
77+
}
78+
config.RpcUser = credentials[0]
79+
config.RpcPass = credentials[1]
80+
}
81+
82+
if config.RpcUser == "" {
83+
panic("rpc user not set") // todo use cookie file to circumvent this requirement
84+
}
85+
86+
if config.RpcPass == "" {
87+
panic("rpc pass not set") // todo use cookie file to circumvent this requirement
88+
}
89+
}
90+
91+
func main() {
92+
if displayVersion {
93+
fmt.Println("blindbit-oracle version:", Version) // using fmt because loggers are not initialised
94+
os.Exit(0)
95+
}
96+
defer logging.L.Info().Msg("Program shut down")
97+
defer dblevel.CloseDBs()
98+
99+
//log.SetFlags(log.LstdFlags | log.Lshortfile | log.Lmicroseconds)
100+
interrupt := make(chan os.Signal, 1)
101+
signal.Notify(interrupt, os.Interrupt)
102+
103+
logging.L.Info().Msg("Program Started")
104+
105+
// make sure everything is ready before we receive data
106+
107+
//todo create proper handling for exporting data
108+
109+
if exportData {
110+
logging.L.Info().Msg("Exporting data")
111+
dataexport.ExportUTXOs(fmt.Sprintf("%s/export/utxos.csv", config.BaseDirectory))
112+
return
113+
}
114+
115+
//moved into go routine such that the interrupt signal will apply properly
116+
go func() {
117+
if pruneOnStart {
118+
startPrune := time.Now()
119+
core.PruneAllUTXOs()
120+
logging.L.Info().Msgf("Pruning took: %s", time.Since(startPrune).String())
121+
}
122+
startSync := time.Now()
123+
err := core.PreSyncHeaders()
124+
if err != nil {
125+
logging.L.Fatal().Err(err).Msg("error pre-syncing headers")
126+
return
127+
}
128+
129+
// so we can start fetching data while not fully synced. Requires headers to be synced to avoid grave errors.
130+
go server.RunServer(&server.ApiHandler{})
131+
132+
// todo buggy for sync catchup from 0, needs to be 1 or higher
133+
err = core.SyncChain()
134+
if err != nil {
135+
logging.L.Fatal().Err(err).Msg("error syncing chain")
136+
return
137+
}
138+
logging.L.Info().Msgf("Sync took: %s", time.Since(startSync).String())
139+
go core.CheckForNewBlockRoutine()
140+
141+
// only call this if you need to reindex. It doesn't delete anything but takes a couple of minutes to finish
142+
//err := core.ReindexDustLimitsOnly()
143+
//if err != nil {
144+
// common.ErrorLogger.Fatalln(err)
145+
// return
146+
//}
147+
148+
}()
149+
150+
for {
151+
<-interrupt
152+
logging.L.Info().Msg("Program interrupted")
153+
return
154+
}
155+
}
156+
157+
func openLevelDBConnections() {
158+
dblevel.HeadersDB = dblevel.OpenDBConnection(config.DBPathHeaders)
159+
dblevel.HeadersInvDB = dblevel.OpenDBConnection(config.DBPathHeadersInv)
160+
dblevel.NewUTXOsFiltersDB = dblevel.OpenDBConnection(config.DBPathFilters)
161+
dblevel.TweaksDB = dblevel.OpenDBConnection(config.DBPathTweaks)
162+
dblevel.TweakIndexDB = dblevel.OpenDBConnection(config.DBPathTweakIndex)
163+
dblevel.TweakIndexDustDB = dblevel.OpenDBConnection(config.DBPathTweakIndexDust)
164+
dblevel.UTXOsDB = dblevel.OpenDBConnection(config.DBPathUTXOs)
165+
dblevel.SpentOutpointsIndexDB = dblevel.OpenDBConnection(config.DBPathSpentOutpointsIndex)
166+
dblevel.SpentOutpointsFilterDB = dblevel.OpenDBConnection(config.DBPathSpentOutpointsFilter)
167+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module SilentPaymentAppBackend
1+
module github.com/setavenger/blindbit-oracle
22

33
go 1.20
44

src/common/config.go renamed to internal/config/config.go

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

33
import (
44
"errors"
5-
"os"
65

6+
"github.com/setavenger/blindbit-lib/logging"
77
"github.com/spf13/viper"
88
)
99

@@ -13,7 +13,7 @@ func LoadConfigs(pathToConfig string) {
1313

1414
// Handle errors reading the config file
1515
if err := viper.ReadInConfig(); err != nil {
16-
WarningLogger.Println("No config file detected", err.Error())
16+
logging.L.Warn().Err(err).Msg("No config file detected")
1717
}
1818

1919
/* set defaults */
@@ -84,19 +84,19 @@ func LoadConfigs(pathToConfig string) {
8484
}
8585

8686
// todo print settings
87-
InfoLogger.Printf("tweaks_only: %t\n", TweaksOnly)
88-
InfoLogger.Printf("tweaks_full_basic: %t\n", TweakIndexFullNoDust)
89-
InfoLogger.Printf("tweaks_full_with_dust_filter: %t\n", TweakIndexFullIncludingDust)
90-
InfoLogger.Printf("tweaks_cut_through_with_dust_filter: %t\n", TweaksCutThroughWithDust)
87+
logging.L.Info().Msgf("tweaks_only: %t", TweaksOnly)
88+
logging.L.Info().Msgf("tweaks_full_basic: %t", TweakIndexFullNoDust)
89+
logging.L.Info().Msgf("tweaks_full_with_dust_filter: %t", TweakIndexFullIncludingDust)
90+
logging.L.Info().Msgf("tweaks_cut_through_with_dust_filter: %t", TweaksCutThroughWithDust)
9191

9292
if !TweakIndexFullNoDust && !TweakIndexFullIncludingDust && !TweaksCutThroughWithDust {
93-
WarningLogger.Println("no tweaks are being collected, all tweak settings were set to 0")
94-
WarningLogger.Println("make sure your configuration loaded correctly, check example blindbit.toml for configuration")
93+
logging.L.Warn().Msg("no tweaks are being collected, all tweak settings were set to 0")
94+
logging.L.Warn().Msg("make sure your configuration loaded correctly, check example blindbit.toml for configuration")
9595
}
9696

9797
if TweaksCutThroughWithDust && TweaksOnly {
9898
err := errors.New("cut through requires tweaks_only to be set to 0")
99-
ErrorLogger.Println(err)
100-
os.Exit(1)
99+
logging.L.Fatal().Err(err).Msg("cut through requires tweaks_only to be set to 0")
100+
return
101101
}
102102
}

internal/config/endpoints.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package config
2+
3+
const MempoolEndpointMainnet = "http://localhost:80/api/tx/"
4+
const MempoolEndpointSignet = "https://mempool.space/signet/api/tx"
5+
const MempoolEndpointTestnet3 = "https://mempool.space/testnet/api/tx"
6+
const MempoolEndpointTestnet4 = "https://mempool.space/testnet4/api/tx"

src/common/vars.go renamed to internal/config/vars.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
package common
1+
package config
2+
3+
import "github.com/setavenger/blindbit-lib/utils"
24

35
// TaprootActivation
46
// todo might be inapplicable due to transactions that have taproot prevouts from before the activation
57
//
68
// is relevant for the height-to-hash lookup in the db
7-
const TaprootActivation uint32 = 709632
8-
const ConfigFileName string = "blindbit.toml"
9-
const DefaultBaseDirectory = "~/.blindbit-oracle"
109

11-
var TweaksOnly bool
12-
var TweakIndexFullNoDust bool
13-
var TweakIndexFullIncludingDust bool
14-
var TweaksCutThroughWithDust bool
10+
const (
11+
TaprootActivation uint32 = 709632
12+
ConfigFileName string = "blindbit.toml"
13+
DefaultBaseDirectory string = "~/.blindbit-oracle"
14+
)
15+
16+
var (
17+
TweaksOnly bool
18+
TweakIndexFullNoDust bool
19+
TweakIndexFullIncludingDust bool
20+
TweaksCutThroughWithDust bool
21+
)
1522

1623
var (
1724
RpcEndpoint = "http://127.0.0.1:8332" // default local node
@@ -73,7 +80,7 @@ var (
7380
var NumsH = []byte{80, 146, 155, 116, 193, 160, 73, 84, 183, 139, 75, 96, 53, 233, 122, 94, 7, 138, 90, 15, 40, 236, 150, 213, 71, 191, 238, 154, 206, 128, 58, 192}
7481

7582
func SetDirectories() {
76-
BaseDirectory = ResolvePath(BaseDirectory)
83+
BaseDirectory = utils.ResolvePath(BaseDirectory)
7784

7885
DBPath = BaseDirectory + "/data"
7986
LogsPath = BaseDirectory + "/logs"

internal/core/block_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package core
2+
3+
import (
4+
"testing"
5+
6+
"github.com/setavenger/blindbit-lib/logging"
7+
"github.com/setavenger/blindbit-oracle/internal/testhelpers"
8+
"github.com/setavenger/blindbit-oracle/internal/types"
9+
)
10+
11+
func TestBlockAnalysis(t *testing.T) {
12+
var block types.Block
13+
err := testhelpers.LoadBlockFromFile("/Users/setorblagogee/dev/sp-test-dir/block-716120.json", &block)
14+
if err != nil {
15+
logging.L.Fatal().Err(err).Msg("error loading block from file")
16+
t.FailNow()
17+
}
18+
19+
tweaks, err := ComputeTweaksForBlock(&block)
20+
if err != nil {
21+
logging.L.Fatal().Err(err).Msg("error computing tweaks for block")
22+
t.FailNow()
23+
}
24+
25+
for _, tweak := range tweaks {
26+
logging.L.Info().Bytes("tweak", tweak.TweakData).Str("txid", tweak.Txid).Msg("tweak")
27+
}
28+
29+
for _, tx := range block.Txs {
30+
for _, tweak := range tweaks {
31+
if tx.Txid == tweak.Txid {
32+
logging.L.Info().Hex("tweak", tweak.TweakData).Msg("tweak")
33+
}
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)