Repository navigation
Expand file tree
/
Copy pathbfgd.go
More file actions
141 lines (124 loc) · 3.58 KB
/
Copy pathbfgd.go
File metadata and controls
141 lines (124 loc) · 3.58 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
// 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/v2"
"github.com/hemilabs/heminetwork/v2/api/bfgapi"
"github.com/hemilabs/heminetwork/v2/api/tbcapi"
"github.com/hemilabs/heminetwork/v2/config"
"github.com/hemilabs/heminetwork/v2/service/bfg"
"github.com/hemilabs/heminetwork/v2/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: "tbc",
Help: "bitcoin source of truth used (tbc only for now)",
Print: config.PrintAll,
},
"BFG_BITCOIN_URL": config.Config{
Value: &cfg.BitcoinURL,
DefaultValue: tbcapi.DefaultURL,
Help: "connection 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|testnet4)",
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,
},
"BFG_OPGETH_URL": config.Config{
Value: &cfg.OpgethURL,
DefaultValue: "",
Help: "connection url for opgeth e.g. http://127.0.0.1:9999/v1/ws",
Print: config.PrintAll,
},
}
)
func _main() error {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
// 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])
}
go func() {
// Stop receiving signals as soon as possible.
<-ctx.Done()
cancel()
}()
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)
}
}