-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathbfgd.go
More file actions
150 lines (132 loc) · 3.75 KB
/
bfgd.go
File metadata and controls
150 lines (132 loc) · 3.75 KB
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
// 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;bfg=INFO"
)
var (
log = loggo.GetLogger(daemonName)
welcome string
cfg = bfg.NewDefaultConfig()
cm = config.CfgMap{
"BFG_BITCOIN_SOURCE": config.Config{
Value: &cfg.BitcoinSource,
DefaultValue: "blockstream",
Help: "which bitcoin source of truth is used (blockstream and tbc)",
Print: config.PrintAll,
},
"BFG_BITCOIN_URL": config.Config{
Value: &cfg.BitcoinURL,
DefaultValue: "",
Help: "conection url for bitcoin source if needed e.g. ws://localhost:8082/v1/ws",
Print: config.PrintAll,
},
"BFG_LOG_LEVEL": config.Config{
Value: &cfg.LogLevel,
DefaultValue: defaultLogLevel,
Help: "loglevel for various packages; INFO, DEBUG and TRACE",
Print: config.PrintAll,
},
"BFG_LISTEN_ADDRESS": config.Config{
Value: &cfg.ListenAddress,
DefaultValue: bfgapi.DefaultListenAddress,
Help: "address and port bfgd listens on for http connections",
Print: config.PrintAll,
},
"BFG_NETWORK": config.Config{
Value: &cfg.Network,
DefaultValue: "mainnet",
Help: "network bfg is working on (mainnet/testnet3)",
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_PROMETHEUS_ADDRESS": config.Config{
Value: &cfg.PrometheusListenAddress,
DefaultValue: "",
Help: "address and port bfgd prometheus listens on",
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(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)
}
}