-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathstart.go
More file actions
315 lines (272 loc) · 7.81 KB
/
start.go
File metadata and controls
315 lines (272 loc) · 7.81 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package cmd
import (
"context"
"fmt"
"time"
"github.com/keep-network/keep-core/pkg/tbtcpg"
"github.com/keep-network/keep-common/pkg/persistence"
"github.com/keep-network/keep-core/build"
"github.com/keep-network/keep-core/pkg/bitcoin/electrum"
"github.com/keep-network/keep-core/pkg/operator"
"github.com/keep-network/keep-core/pkg/storage"
"github.com/spf13/cobra"
"github.com/keep-network/keep-core/config"
"github.com/keep-network/keep-core/pkg/beacon"
"github.com/keep-network/keep-core/pkg/chain"
"github.com/keep-network/keep-core/pkg/chain/ethereum"
"github.com/keep-network/keep-core/pkg/clientinfo"
"github.com/keep-network/keep-core/pkg/firewall"
"github.com/keep-network/keep-core/pkg/generator"
"github.com/keep-network/keep-core/pkg/net"
"github.com/keep-network/keep-core/pkg/net/libp2p"
"github.com/keep-network/keep-core/pkg/net/retransmission"
"github.com/keep-network/keep-core/pkg/tbtc"
)
// StartCommand contains the definition of the start command-line subcommand.
var StartCommand = &cobra.Command{
Use: "start",
Short: "Starts the Keep Client",
Long: "Starts the Keep Client in the foreground",
PreRun: func(cmd *cobra.Command, args []string) {
if err := clientConfig.ReadConfig(configFilePath, cmd.Flags(), config.StartCmdCategories...); err != nil {
logger.Fatalf("error reading config: %v", err)
}
},
Run: func(cmd *cobra.Command, args []string) {
if err := start(cmd); err != nil {
logger.Fatal(err)
}
},
}
func init() {
initFlags(StartCommand, &configFilePath, clientConfig, config.StartCmdCategories...)
StartCommand.SetUsageTemplate(
fmt.Sprintf(`%s
Environment variables:
%s Password for Keep operator account keyfile decryption.
%s Space-delimited set of log level directives; set to "help" for help.
`,
StartCommand.UsageString(),
config.EthereumPasswordEnvVariable,
config.LogLevelEnvVariable,
),
)
}
// start starts a node
func start(cmd *cobra.Command) error {
ctx := context.Background()
beaconChain, tbtcChain, blockCounter, signing, operatorPrivateKey, err :=
ethereum.Connect(ctx, clientConfig.Ethereum)
if err != nil {
return fmt.Errorf("error connecting to Ethereum node: [%v]", err)
}
netProvider, err := initializeNetwork(
ctx,
[]firewall.Application{beaconChain, tbtcChain},
operatorPrivateKey,
blockCounter,
)
if err != nil {
return fmt.Errorf("cannot initialize network: [%v]", err)
}
clientInfoRegistry := initializeClientInfo(
ctx,
clientConfig,
netProvider,
signing,
blockCounter,
)
// Wire performance metrics into network provider if available
var perfMetrics *clientinfo.PerformanceMetrics
if clientInfoRegistry != nil {
perfMetrics = clientinfo.NewPerformanceMetrics(ctx, clientInfoRegistry)
// Type assert to libp2p provider to set metrics recorder
// The provider struct is not exported, so we use interface assertion
if setter, ok := netProvider.(interface {
SetMetricsRecorder(recorder interface {
IncrementCounter(name string, value float64)
SetGauge(name string, value float64)
RecordDuration(name string, duration time.Duration)
})
}); ok {
setter.SetMetricsRecorder(perfMetrics)
}
}
// Initialize beacon and tbtc only for non-bootstrap nodes.
// Skip initialization for bootstrap nodes as they are only used for network
// discovery.
if !isBootstrap() {
btcChain, err := electrum.Connect(ctx, clientConfig.Bitcoin.Electrum)
if err != nil {
return fmt.Errorf("could not connect to Electrum chain: [%v]", err)
}
beaconKeyStorePersistence,
tbtcKeyStorePersistence,
tbtcDataPersistence,
err := initializePersistence()
if err != nil {
return fmt.Errorf("cannot initialize persistence: [%w]", err)
}
scheduler := generator.StartScheduler()
if clientInfoRegistry != nil {
clientInfoRegistry.ObserveBtcConnectivity(
btcChain,
clientConfig.ClientInfo.BitcoinMetricsTick,
)
clientInfoRegistry.RegisterBtcChainInfoSource(btcChain)
rpcHealthChecker := clientinfo.NewRPCHealthChecker(
clientInfoRegistry,
blockCounter,
btcChain,
clientConfig.ClientInfo.RPCHealthCheckInterval,
)
rpcHealthChecker.Start(ctx)
}
err = beacon.Initialize(
ctx,
beaconChain,
netProvider,
beaconKeyStorePersistence,
scheduler,
)
if err != nil {
return fmt.Errorf("error initializing beacon: [%v]", err)
}
proposalGenerator := tbtcpg.NewProposalGenerator(
tbtcChain,
btcChain,
)
err = tbtc.Initialize(
ctx,
tbtcChain,
btcChain,
netProvider,
tbtcKeyStorePersistence,
tbtcDataPersistence,
scheduler,
proposalGenerator,
clientConfig.Tbtc,
clientInfoRegistry,
perfMetrics, // Pass the existing performance metrics instance to avoid duplicate registrations
)
if err != nil {
return fmt.Errorf("error initializing TBTC: [%v]", err)
}
}
nodeHeader(
netProvider.ConnectionManager().AddrStrings(),
beaconChain.Signing().Address().String(),
clientConfig.LibP2P.Port,
clientConfig.Ethereum,
)
<-ctx.Done()
return fmt.Errorf("shutting down the node because its context has ended")
}
func isBootstrap() bool {
if clientConfig.LibP2P.Bootstrap {
logger.Warnf("--network.bootstrap is deprecated and will be removed in a future release")
}
return clientConfig.LibP2P.Bootstrap
}
func initializeNetwork(
ctx context.Context,
applications []firewall.Application,
operatorPrivateKey *operator.PrivateKey,
blockCounter chain.BlockCounter,
) (net.Provider, error) {
firewall := firewall.AnyApplicationPolicy(
applications,
firewall.EmptyAllowList(),
)
netProvider, err := libp2p.Connect(
ctx,
clientConfig.LibP2P,
operatorPrivateKey,
firewall,
retransmission.NewTicker(blockCounter.WatchBlocks(ctx)),
)
if err != nil {
return nil, fmt.Errorf("failed while creating the network provider: [%v]", err)
}
return netProvider, nil
}
func initializeClientInfo(
ctx context.Context,
config *config.Config,
netProvider net.Provider,
signing chain.Signing,
blockCounter chain.BlockCounter,
) *clientinfo.Registry {
registry, isConfigured := clientinfo.Initialize(ctx, config.ClientInfo.Port)
if !isConfigured {
logger.Infof("client info endpoint not configured")
return nil
}
registry.ObserveConnectedPeersCount(
netProvider,
config.ClientInfo.NetworkMetricsTick,
)
registry.ObserveConnectedWellknownPeersCount(
netProvider,
config.LibP2P.Peers,
config.ClientInfo.NetworkMetricsTick,
)
registry.ObserveEthConnectivity(
blockCounter,
config.ClientInfo.EthereumMetricsTick,
)
registry.RegisterMetricClientInfo(build.Version)
registry.RegisterConnectedPeersSource(netProvider, signing)
registry.RegisterClientInfoSource(
netProvider,
signing,
build.Version,
build.Revision,
)
registry.RegisterEthChainInfoSource(blockCounter)
logger.Infof(
"enabled client info endpoint on port [%v]",
config.ClientInfo.Port,
)
return registry
}
func initializePersistence() (
beaconKeyStorePersistence persistence.ProtectedHandle,
tbtcKeyStorePersistence persistence.ProtectedHandle,
tbtcDataPersistence persistence.BasicHandle,
err error,
) {
storage, err := storage.Initialize(
clientConfig.Storage,
clientConfig.Ethereum.KeyFilePassword,
)
if err != nil {
return nil, nil, nil, fmt.Errorf("cannot initialize storage: [%w]", err)
}
beaconKeyStorePersistence, err = storage.InitializeKeyStorePersistence(
"beacon",
)
if err != nil {
return nil, nil, nil, fmt.Errorf(
"cannot initialize beacon keystore persistence: [%w]",
err,
)
}
tbtcKeyStorePersistence, err = storage.InitializeKeyStorePersistence(
"tbtc",
)
if err != nil {
return nil, nil, nil, fmt.Errorf(
"cannot initialize tbtc keystore persistence: [%w]",
err,
)
}
tbtcDataPersistence, err = storage.InitializeWorkPersistence("tbtc")
if err != nil {
return nil, nil, nil, fmt.Errorf(
"cannot initialize tbtc data persistence: [%w]",
err,
)
}
return
}