-
Notifications
You must be signed in to change notification settings - Fork 55
/
tbcd.go
224 lines (203 loc) · 5.97 KB
/
tbcd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright (c) 2024 Hemi Labs, Inc.
// Use of this source code is governed by the MIT License,
// which can be found in the LICENSE file.
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/juju/loggo"
"github.com/hemilabs/heminetwork/api/tbcapi"
"github.com/hemilabs/heminetwork/config"
"github.com/hemilabs/heminetwork/service/tbc"
"github.com/hemilabs/heminetwork/version"
)
const (
daemonName = "tbcd"
defaultLogLevel = daemonName + "=INFO;tbc=INFO;level=INFO"
defaultNetwork = "testnet3" // XXX make this mainnet
defaultHome = "~/." + daemonName
bhsDefault = int(1e6) // enough for mainnet
)
var (
log = loggo.GetLogger(daemonName)
welcome string
cfg = tbc.NewDefaultConfig()
cm = config.CfgMap{
"TBC_ADDRESS": config.Config{
Value: &cfg.ListenAddress,
DefaultValue: tbcapi.DefaultListen,
Help: "address port to listen on",
Print: config.PrintAll,
},
"TBC_AUTO_INDEX": config.Config{
Value: &cfg.AutoIndex,
DefaultValue: true,
Help: "enable auto utxo and tx indexes",
Print: config.PrintAll,
},
"TBC_BLOCK_CACHE": config.Config{
Value: &cfg.BlockCache,
DefaultValue: 250,
Help: "number of cached blocks",
Print: config.PrintAll,
},
"TBC_BLOCKHEADER_CACHE": config.Config{
Value: &cfg.BlockheaderCache,
DefaultValue: bhsDefault,
Help: "number of cached blockheaders",
Print: config.PrintAll,
},
"TBC_BLOCK_SANITY": config.Config{
Value: &cfg.BlockSanity,
DefaultValue: false,
Help: "enable/disable block sanity checks before inserting",
Print: config.PrintAll,
},
"TBC_LEVELDB_HOME": config.Config{
Value: &cfg.LevelDBHome,
DefaultValue: defaultHome,
Help: "data directory for leveldb",
Print: config.PrintAll,
},
"TBC_LOG_LEVEL": config.Config{
Value: &cfg.LogLevel,
DefaultValue: defaultLogLevel,
Help: "loglevel for various packages; INFO, DEBUG and TRACE",
Print: config.PrintAll,
},
"TBC_MAX_CACHED_TXS": config.Config{
Value: &cfg.MaxCachedTxs,
DefaultValue: int(1e6),
Help: "maximum cached utxos and/or txs during indexing",
Print: config.PrintAll,
},
"TBC_MEMPOOL_ENABLED": config.Config{
Value: &cfg.MempoolEnabled,
DefaultValue: true,
Help: "bitcoin network mempool enable/disable switch",
Print: config.PrintAll,
},
"TBC_NETWORK": config.Config{
Value: &cfg.Network,
DefaultValue: defaultNetwork,
Help: "bitcoin network; mainnet or testnet3",
Print: config.PrintAll,
},
"TBC_PEERS_WANTED": config.Config{
Value: &cfg.PeersWanted,
DefaultValue: 64,
Help: "number of wanted p2p peers",
Print: config.PrintAll,
},
"TBC_PROMETHEUS_ADDRESS": config.Config{
Value: &cfg.PrometheusListenAddress,
DefaultValue: "",
Help: "address and port tbcd prometheus listens on",
Print: config.PrintAll,
},
"TBC_PPROF_ADDRESS": config.Config{
Value: &cfg.PprofListenAddress,
DefaultValue: "",
Help: "address and port tbcd pprof listens on (open <address>/debug/pprof to see available profiles)",
Print: config.PrintAll,
},
"TBC_SEEDS": config.Config{
Value: &cfg.Seeds,
DefaultValue: []string{},
Help: "list of seed domains for Bitcoin P2P, in the format '<host>:<port>' (for localnet, must be a single host:port)",
Print: config.PrintAll,
},
}
)
func HandleSignals(ctx context.Context, cancel context.CancelFunc, callback func(os.Signal)) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
defer func() {
signal.Stop(signalChan)
cancel()
}()
select {
case <-ctx.Done():
case s := <-signalChan: // First signal, cancel context.
if callback != nil {
callback(s) // Do whatever caller wants first.
cancel()
}
}
<-signalChan // Second signal, hard exit.
os.Exit(2)
}
func _main() error {
// Parse configuration from environment
if err := config.Parse(cm); err != nil {
return err
}
if err := loggo.ConfigureLoggers(cfg.LogLevel); err != nil {
return err
}
log.Infof("%v", welcome)
pc := config.PrintableConfig(cm)
for k := range pc {
log.Infof("%v", pc[k])
}
ctx, cancel := context.WithCancel(context.Background())
go HandleSignals(ctx, cancel, func(s os.Signal) {
log.Infof("tbc service received signal: %s", s)
})
server, err := tbc.NewServer(cfg)
if err != nil {
return fmt.Errorf("create tbc server: %w", err)
}
// XXX remove, this is an illustration of calling the direct API of server
// go func() {
// for {
// select {
// case <-ctx.Done():
// return
// case <-time.After(2 * time.Second):
// }
// log.Infof("synced: %v", spew.Sdump(server.Synced(ctx)))
// hashS := "000000001a4c2c64beded987790ab0c00675b4bc467cd3574ad455b1397c967c"
// ch, err := chainhash.NewHashFromStr(hashS)
// if err != nil {
// panic(err)
// }
// bh, height, err := server.BlockHeaderByHash(ctx, ch)
// if err != nil {
// panic(err)
// }
// log.Infof("height %v hash %v%v", height, bh.BlockHash(), spew.Sdump(bh))
// bhbh, err := server.BlockHeadersByHeight(ctx, height)
// if err != nil {
// panic(err)
// }
// log.Infof("height %v headers %v", height, spew.Sdump(bhbh))
// }
// }()
if err := server.Run(ctx); !errors.Is(err, context.Canceled) {
return fmt.Errorf("tbc server terminated: %w", err)
}
return nil
}
func init() {
version.Component = "tbcd"
welcome = "Hemi Tiny Bitcoin Daemon " + version.BuildInfo()
}
func main() {
if len(os.Args) != 1 {
fmt.Fprintf(os.Stderr, "%v\n", welcome)
fmt.Fprintf(os.Stderr, "Usage:\n")
fmt.Fprintf(os.Stderr, "\thelp (this help)\n")
fmt.Fprintf(os.Stderr, "Environment:\n")
config.Help(os.Stderr, cm)
os.Exit(1)
}
if err := _main(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}