forked from wormhole-foundation/wormhole-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
473 lines (419 loc) · 15.1 KB
/
main.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// TODO: this currently leverages `p2p.Run` for gathering messages,
// but this abtracts away some critical metrics that would be advantageous to track, for example
// - getting raw counts for heartbeats (only guardian heartbeats are counted)
// - getting the sender's p2p key for VAAs (these are not currently attributed)
// - attributing messages to p2p key instead of guardian address (the guardian address field is currently unverified)
// - mapping p2p key to guardian address (is is possible for the same guardian key to have multiple p2p keys, such as testnet)
// manually connecting to the gossip network here would allow for the flexibility to do the above
// at cost of the added complexity of verifying guardian heartbeats to determine their legitimate p2p key(s)
package main
import (
"context"
"encoding/hex"
"flag"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
node_common "github.com/certusone/wormhole/node/pkg/common"
"github.com/certusone/wormhole/node/pkg/p2p"
gossipv1 "github.com/certusone/wormhole/node/pkg/proto/gossip/v1"
"github.com/certusone/wormhole/node/pkg/supervisor"
ipfslog "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/wormhole-foundation/wormhole-monitor/fly/common"
"github.com/wormhole-foundation/wormhole-monitor/fly/utils"
"github.com/wormhole-foundation/wormhole/sdk"
"github.com/wormhole-foundation/wormhole/sdk/vaa"
"go.uber.org/zap"
)
var (
envStr = flag.String("env", "mainnet", `environment (may be "mainnet", "testnet" or "devnet", required)`)
logLevel = flag.String("logLevel", "warn", "Logging level (debug, info, warn, error, dpanic, panic, fatal)")
p2pNetworkID = flag.String("network", "", "P2P network identifier (optional, overrides default, required for devnet)")
p2pPort = flag.Uint("port", 8999, "P2P UDP listener port")
p2pBootstrap = flag.String("bootstrap", "", "P2P bootstrap peers (optional, overrides default)")
nodeKeyPath = flag.String("nodeKey", "/tmp/node.key", "Path to node key (will be generated if it doesn't exist)")
ethRPC = flag.String("ethRPC", "", "Ethereum RPC for fetching current guardian set (default is based on env)")
ethContract = flag.String("ethContract", "", "Ethereum core bridge address for fetching current guardian set (default is based on env)")
)
var (
rootCtx context.Context
rootCtxCancel context.CancelFunc
rpcUrl string
coreBridgeAddr string
numGuardians int
// Guardian address to index map
guardianIndexMap = map[string]int{}
// Guardian index to address map
guardianIndexToNameMap = map[int]string{}
// The known token bridge emitters
knownEmitters = map[string]bool{}
)
var (
gossipByType = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "gossip_by_type_total",
Help: "The total number of gossip messages by type",
}, []string{"type"})
uniqueObservationsCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "gossip_observations_unique_total",
Help: "The unique number of observations received over gossip",
})
observationsByGuardianPerChain = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "gossip_observations_by_guardian_per_chain_total",
Help: "The number of observations received over gossip by guardian, per chain",
}, []string{"guardian_name", "chain_name"})
tbObservationsByGuardianPerChain = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "gossip_token_bridge_observations_by_guardian_per_chain_total",
Help: "The number of token bridge observations received over gossip by guardian, per chain",
}, []string{"guardian_name", "chain_name"})
observationRequestsPerChain = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "gossip_observation_requests_per_chain_total",
Help: "The number of observation requests received over gossip per chain",
}, []string{"chain_name"})
uniqueVAAsCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "gossip_vaas_unique_total",
Help: "The unique number of vaas received over gossip",
})
uniqueVAAsByGuardianPerChain = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "gossip_vaas_by_guardian_per_chain_total",
Help: "The number of unique VAAs received over gossip, grouped by guardian and chain",
}, []string{"guardian_name", "chain_name"})
heartbeatsByGuardian = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "gossip_heartbeats_by_guardian_total",
Help: "The number of heartbeats received over gossip by guardian",
}, []string{"guardian_name"})
govConfigByGuardian = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "gossip_gov_config_by_guardian_total",
Help: "The number of heartbeats received over gossip by guardian",
}, []string{"guardian_name"})
govStatusByGuardian = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "gossip_gov_status_by_guardian_total",
Help: "The number of heartbeats received over gossip by guardian",
}, []string{"guardian_name"})
)
func main() {
flag.Parse()
// Set up the logger.
lvl, err := ipfslog.LevelFromString(*logLevel)
if err != nil {
fmt.Println("Invalid log level")
os.Exit(1)
}
logger := ipfslog.Logger("wormhole-fly").Desugar()
ipfslog.SetAllLoggers(lvl)
if *envStr == "" {
logger.Fatal("--env is required")
}
env, err := node_common.ParseEnvironment(*envStr)
if err != nil || (env != node_common.UnsafeDevNet && env != node_common.TestNet && env != node_common.MainNet) {
if *envStr == "" {
logger.Fatal("Please specify --env")
}
logger.Fatal("Invalid value for --env, should be devnet, testnet or mainnet", zap.String("val", *envStr))
}
// Build the set of guardians based on our environment, where the default is mainnet.
var guardians []common.GuardianEntry
var knownEmitter []sdk.EmitterInfo
if env == node_common.MainNet {
guardians = common.MainnetGuardians
knownEmitter = sdk.KnownEmitters
rpcUrl = "https://ethereum-rpc.publicnode.com"
coreBridgeAddr = "0x98f3c9e6E3fAce36bAAd05FE09d375Ef1464288B"
} else if env == node_common.TestNet {
guardians = common.TestnetGuardians
knownEmitter = sdk.KnownTestnetEmitters
rpcUrl = "https://ethereum-holesky-rpc.publicnode.com"
coreBridgeAddr = "0xa10f2eF61dE1f19f586ab8B6F2EbA89bACE63F7a"
} else if env == node_common.UnsafeDevNet {
guardians = common.DevnetGuardians
knownEmitter = sdk.KnownDevnetEmitters
rpcUrl = "http://localhost:8545"
coreBridgeAddr = "0xC89Ce4735882C9F0f0FE26686c53074E09B0D550"
}
for _, gse := range guardians {
guardianIndexToNameMap[gse.Index] = gse.Name
guardianIndexMap[strings.ToLower(gse.Address)] = gse.Index
}
// Fill in the known emitters
for _, knownEmitter := range knownEmitter {
knownEmitters[strings.ToLower(knownEmitter.Emitter)] = true
}
numGuardians = len(guardianIndexToNameMap)
// Set up P2P.
if *p2pNetworkID == "" {
*p2pNetworkID = p2p.GetNetworkId(env)
}
if *p2pBootstrap == "" {
*p2pBootstrap, err = p2p.GetBootstrapPeers(env)
if err != nil {
logger.Fatal("failed to determine the bootstrap peers from the environment", zap.String("env", string(env)), zap.Error(err))
}
}
// If they specified the RPC or contract address, override the defaults.
if *ethRPC != "" {
rpcUrl = *ethRPC
}
if *ethContract != "" {
coreBridgeAddr = *ethContract
}
// Node's main lifecycle context.
rootCtx, rootCtxCancel = context.WithCancel(context.Background())
defer rootCtxCancel()
// Inbound observations
batchObsvC := make(chan *node_common.MsgWithTimeStamp[gossipv1.SignedObservationBatch], 20000)
// Inbound observation requests
obsvReqC := make(chan *gossipv1.ObservationRequest, 20000)
// Inbound signed VAAs
signedInC := make(chan *gossipv1.SignedVAAWithQuorum, 20000)
// Heartbeat updates
heartbeatC := make(chan *gossipv1.Heartbeat, 20000)
// Guardian set state managed by processor
gst := node_common.NewGuardianSetState(heartbeatC)
// Governor cfg
govConfigC := make(chan *gossipv1.SignedChainGovernorConfig, 20000)
// Governor status
govStatusC := make(chan *gossipv1.SignedChainGovernorStatus, 20000)
// Bootstrap guardian set, otherwise heartbeats would be skipped
idx, sgs, err := utils.FetchCurrentGuardianSet(rpcUrl, coreBridgeAddr)
if err != nil {
logger.Fatal("Failed to fetch guardian set", zap.Error(err))
}
logger.Info("guardian set", zap.Uint32("index", idx), zap.Any("gs", sgs))
gs := node_common.GuardianSet{
Keys: sgs.Keys,
Index: idx,
}
gst.Set(&gs)
if len(gs.Keys) != numGuardians {
logger.Error("Invalid number of guardians.", zap.Int("found", len(gs.Keys)), zap.Int("expected", numGuardians))
return
}
// Count observations
go func() {
// TODO: move this to a function / struct with a mutex so that the cleanup can be run independently from the message handling, so as to not back up the channel
uniqueObs := make(map[string]time.Time)
timeout := time.Hour
delay := time.Minute * 10
timer := time.NewTimer(delay)
defer timer.Stop()
for {
select {
case <-rootCtx.Done():
return
case <-timer.C:
beforeCount := len(uniqueObs)
now := time.Now()
for hash, t := range uniqueObs {
if now.After(t.Add(timeout)) {
delete(uniqueObs, hash)
}
}
afterCount := len(uniqueObs)
logger.Info("Cleaned up unique observations cache", zap.Int("beforeCount", beforeCount), zap.Int("afterCount", afterCount), zap.Int("cleanedUpCount", beforeCount-afterCount))
timer.Reset(delay)
case batch := <-batchObsvC:
gossipByType.WithLabelValues("batch_observation").Inc()
addr := "0x" + string(hex.EncodeToString(batch.Msg.Addr))
name := addr
idx, found := guardianIndexMap[strings.ToLower(addr)]
if found {
name = guardianIndexToNameMap[idx]
}
for _, o := range batch.Msg.Observations {
spl := strings.Split(o.MessageId, "/")
chain, err := parseChainID(spl[0])
if err != nil {
chain = vaa.ChainIDUnset
}
emitter := strings.ToLower(spl[1])
observationsByGuardianPerChain.WithLabelValues(name, chain.String()).Inc()
if knownEmitters[emitter] {
tbObservationsByGuardianPerChain.WithLabelValues(name, chain.String()).Inc()
}
hash := hex.EncodeToString(o.Hash)
if _, exists := uniqueObs[hash]; !exists {
uniqueObservationsCounter.Inc()
}
uniqueObs[hash] = time.Now()
}
}
}
}()
// Count observation requests
go func() {
for {
select {
case <-rootCtx.Done():
return
case or := <-obsvReqC:
// There is no guardian address in the observation request
gossipByType.WithLabelValues("observation_request").Inc()
chain := vaa.ChainID(or.ChainId)
observationRequestsPerChain.WithLabelValues(chain.String()).Inc()
}
}
}()
// Count signed VAAs
go func() {
uniqueVAAs := make(map[string]time.Time)
timeout := time.Hour
delay := time.Minute * 10
timer := time.NewTimer(delay)
for {
select {
case <-rootCtx.Done():
return
case <-timer.C:
beforeCount := len(uniqueVAAs)
now := time.Now()
for hash, t := range uniqueVAAs {
if now.After(t.Add(timeout)) {
delete(uniqueVAAs, hash)
}
}
afterCount := len(uniqueVAAs)
logger.Info("Cleaned up unique VAAs cache", zap.Int("beforeCount", beforeCount), zap.Int("afterCount", afterCount), zap.Int("cleanedUpCount", beforeCount-afterCount))
timer.Reset(delay)
case m := <-signedInC:
gossipByType.WithLabelValues("vaa").Inc()
v, err := vaa.Unmarshal(m.Vaa)
if err != nil {
logger.Warn("received invalid VAA in SignedVAAWithQuorum message", zap.Error(err), zap.Any("message", m))
continue
}
digest := v.HexDigest()
chain := v.EmitterChain.String() // Extract chain name
// Extract guardian name using signature index
guardianName := "unknown"
for _, sig := range v.Signatures {
guardianIndex := int(sig.Index) // Extract the guardian index
if name, found := guardianIndexToNameMap[guardianIndex]; found {
guardianName = name
break // Take the first matched guardian
}
}
if _, exists := uniqueVAAs[digest]; !exists {
// Increment the original gossip_vaas_unique_total metric
uniqueVAAsCounter.Inc()
// Increment the new metric with guardian and chain labels
uniqueVAAsByGuardianPerChain.WithLabelValues(guardianName, chain).Inc()
}
uniqueVAAs[digest] = time.Now()
}
}
}()
// Handle heartbeats
go func() {
for {
select {
case <-rootCtx.Done():
return
case hb := <-heartbeatC:
gossipByType.WithLabelValues("heartbeat").Inc()
idx := guardianIndexMap[strings.ToLower(hb.GuardianAddr)]
name := guardianIndexToNameMap[idx]
heartbeatsByGuardian.WithLabelValues(name).Inc()
}
}
}()
// Count govConfigs
go func() {
for {
select {
case <-rootCtx.Done():
return
case g := <-govConfigC:
gossipByType.WithLabelValues("gov_config").Inc()
addr := "0x" + string(hex.EncodeToString(g.GuardianAddr))
name := addr
idx, found := guardianIndexMap[strings.ToLower(addr)]
if found {
name = guardianIndexToNameMap[idx]
}
govConfigByGuardian.WithLabelValues(name).Inc()
}
}
}()
// Count govStatus
go func() {
for {
select {
case <-rootCtx.Done():
return
case g := <-govStatusC:
gossipByType.WithLabelValues("gov_status").Inc()
addr := "0x" + string(hex.EncodeToString(g.GuardianAddr))
name := addr
idx, found := guardianIndexMap[strings.ToLower(addr)]
if found {
name = guardianIndexToNameMap[idx]
}
govStatusByGuardian.WithLabelValues(name).Inc()
}
}
}()
// Start prometheus server
go func() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":2112", nil)
}()
// Load p2p private key
var priv crypto.PrivKey
priv, err = node_common.GetOrCreateNodeKey(logger, *nodeKeyPath)
if err != nil {
logger.Fatal("Failed to load node key", zap.Error(err))
}
// Run supervisor.
components := p2p.DefaultComponents()
components.Port = *p2pPort
params, err := p2p.NewRunParams(
*p2pBootstrap,
*p2pNetworkID,
priv,
gst,
rootCtxCancel,
p2p.WithComponents(components),
p2p.WithSignedObservationBatchListener(batchObsvC),
p2p.WithSignedVAAListener(signedInC),
p2p.WithObservationRequestListener(obsvReqC),
p2p.WithChainGovernorConfigListener(govConfigC),
p2p.WithChainGovernorStatusListener(govStatusC),
)
if err != nil {
logger.Fatal("Failed to create RunParams", zap.Error(err))
}
supervisor.New(rootCtx, logger, func(ctx context.Context) error {
if err := supervisor.Run(ctx,
"p2p",
p2p.Run(params)); err != nil {
return err
}
logger.Info("Started internal services")
<-ctx.Done()
return nil
},
// It's safer to crash and restart the process in case we encounter a panic,
// rather than attempting to reschedule the runnable.
supervisor.WithPropagatePanic)
<-rootCtx.Done()
logger.Info("root context cancelled, exiting...")
}
// parseChainID parses a human-readable chain name or a chain ID.
func parseChainID(name string) (vaa.ChainID, error) {
s, err := vaa.ChainIDFromString(name)
if err == nil {
return s, nil
}
// parse as uint16
i, err := strconv.ParseUint(name, 10, 16)
if err != nil {
return 0, fmt.Errorf("failed to parse as name or uint16: %v", err)
}
return vaa.ChainID(i), nil
}