-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathbfgd.go
206 lines (188 loc) · 5.71 KB
/
bfgd.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
// Copyright (c) 2024-2025 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/bfgapi"
"github.com/hemilabs/heminetwork/config"
"github.com/hemilabs/heminetwork/service/bfg"
"github.com/hemilabs/heminetwork/version"
)
const (
daemonName = "bfgd"
defaultLogLevel = daemonName + "=INFO;postgres=INFO;bfgpostgres=INFO;bfg=INFO"
)
var (
log = loggo.GetLogger(daemonName)
welcome string
cfg = bfg.NewDefaultConfig()
cm = config.CfgMap{
"BFG_EXBTC_ADDRESS": config.Config{
Value: &cfg.EXBTCAddress,
DefaultValue: "localhost:18001",
Help: "electrs endpoint",
Print: config.PrintAll,
},
"BFG_EXBTC_INITIAL_CONNECTIONS": config.Config{
Value: &cfg.EXBTCInitialConns,
DefaultValue: 5,
Help: "electrs initial connections",
Print: config.PrintAll,
},
"BFG_EXBTC_MAX_CONNECTIONS": config.Config{
Value: &cfg.EXBTCMaxConns,
DefaultValue: 100,
Help: "electrs max connections",
Print: config.PrintAll,
},
"BFG_BTC_START_HEIGHT": config.Config{
Value: &cfg.BTCStartHeight,
DefaultValue: uint64(0),
Help: "bitcoin start height that serves as genesis",
Print: config.PrintAll,
Required: true,
},
"BFG_LOG_LEVEL": config.Config{
Value: &cfg.LogLevel,
DefaultValue: defaultLogLevel,
Help: "loglevel for various packages; INFO, DEBUG and TRACE",
Print: config.PrintAll,
},
"BFG_POSTGRES_URI": config.Config{
Value: &cfg.PgURI,
DefaultValue: "",
Help: "postgres connection URI",
Print: config.PrintSecret,
Required: true,
},
"BFG_PUBLIC_ADDRESS": config.Config{
Value: &cfg.PublicListenAddress,
DefaultValue: bfgapi.DefaultPublicListen,
Help: "address and port bfgd listens on for public, authenticated, websocket connections",
Print: config.PrintAll,
},
"BFG_PRIVATE_ADDRESS": config.Config{
Value: &cfg.PrivateListenAddress,
DefaultValue: bfgapi.DefaultPrivateListen,
Help: "address and port bfgd listens on for private, unauthenticated, websocket connections",
Print: config.PrintAll,
},
"BFG_PROMETHEUS_ADDRESS": config.Config{
Value: &cfg.PrometheusListenAddress,
DefaultValue: "",
Help: "address and port bfgd prometheus listens on",
Print: config.PrintAll,
},
"BFG_PPROF_ADDRESS": config.Config{
Value: &cfg.PprofListenAddress,
DefaultValue: "",
Help: "address and port bfgd pprof listens on (open <address>/debug/pprof to see available profiles)",
Print: config.PrintAll,
},
"BFG_REQUEST_LIMIT": config.Config{
Value: &cfg.RequestLimit,
DefaultValue: bfgapi.DefaultRequestLimit,
Help: "maximum request queue depth",
Print: config.PrintAll,
},
"BFG_REQUEST_TIMEOUT": config.Config{
Value: &cfg.RequestTimeout,
DefaultValue: bfgapi.DefaultRequestTimeout,
Help: "request timeout in seconds",
Print: config.PrintAll,
},
"BFG_TRUSTED_PROXIES": config.Config{
Value: &cfg.TrustedProxies,
DefaultValue: []string{},
Help: "trusted proxies IP addresses or CIDRs",
Print: config.PrintAll,
},
"BFG_REMOTE_IP_HEADERS": config.Config{
Value: &cfg.RemoteIPHeaders,
DefaultValue: []string{},
Help: "list of headers used to obtain the client IP address (requires trusted proxies)",
Print: config.PrintAll,
},
"BFG_BFG_URL": config.Config{
Value: &cfg.BFGURL,
DefaultValue: "",
Help: "public websocket address of another BFG you'd like to receive L2Keystones from",
Print: config.PrintAll,
},
"BFG_BTC_PRIVKEY": config.Config{
Value: &cfg.BTCPrivateKey,
DefaultValue: "",
Help: "a btc private key, this is only needed when connecting to another BFG",
Print: config.PrintSecret,
},
}
)
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(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("bfg service received signal: %s", s)
})
server, err := bfg.NewServer(cfg)
if err != nil {
return fmt.Errorf("create BFG server: %w", err)
}
if err = server.Run(ctx); !errors.Is(err, context.Canceled) {
return fmt.Errorf("bfg server terminated: %w", err)
}
return nil
}
func init() {
version.Component = "bfgd"
welcome = "Hemi Bitcoin Finality Governor " + 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 {
log.Errorf("%v", err)
os.Exit(1)
}
}