-
Notifications
You must be signed in to change notification settings - Fork 4
/
receive.go
203 lines (170 loc) · 5.76 KB
/
receive.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
package main
import (
"context"
"encoding/hex"
"strconv"
"strings"
"time"
"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"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/rs/zerolog/log"
"github.com/wormhole-foundation/wormhole/sdk/vaa"
"go.uber.org/zap"
)
func ReceiveMessages(channel chan *vaa.VAA, heartbeat *Heartbeat, wormholeEnv, networkID, bootstrapAddrs string, listenPort uint) {
common.SetRestrictiveUmask()
// Node's main lifecycle context.
rootCtx, rootCtxCancel := context.WithCancel(context.Background())
defer rootCtxCancel()
if wormholeEnv != "" {
// If env is set, networkId and bootstrapAddrs should not be set
if networkID != "" || bootstrapAddrs != "" {
log.Panic().Msg("Network ID and bootstrap addresses should not be set when wormhole env is set")
}
env, err := common.ParseEnvironment(wormholeEnv)
if err != nil || (env != common.MainNet && env != common.TestNet) {
log.Panic().Err(err).Msg("Invalid wormhole environment, should be 'mainnet' or 'testnet'")
}
networkID = p2p.GetNetworkId(env)
bootstrapAddrs, err = p2p.GetBootstrapPeers(env)
if err != nil {
log.Panic().Err(err).Msg("Failed to determine p2p bootstrap peers from env")
}
} else {
// If env is not set, networkId and bootstrapAddrs should be set
if networkID == "" || bootstrapAddrs == "" {
log.Panic().Msg("Network ID and bootstrap addresses should be set when wormhole env is not set")
}
}
// Inbound observations
obsvC := make(chan *common.MsgWithTimeStamp[gossipv1.SignedObservationBatch], 1024)
// Inbound observation requests
obsvReqC := make(chan *gossipv1.ObservationRequest, 1024)
// Inbound signed VAAs
signedInC := make(chan *gossipv1.SignedVAAWithQuorum, 1024)
// Guardian set state managed by processor. We do not have a way to fetch
// it; so we will use a nil guardian set state
gst := common.NewGuardianSetState(nil)
messagesMetric := promauto.NewCounter(prometheus.CounterOpts{
Name: "beacon_messages",
Help: "Count of messages received from the p2p network",
})
messageLatencyMetric := promauto.NewHistogram(prometheus.HistogramOpts{
Name: "beacon_message_latency",
Help: "Latency of messages received from the p2p network",
Buckets: []float64{0.3, 0.7, 1, 1.3, 1.7, 2, 2.3, 2.7, 3, 3.5, 4, 5, 10, 20},
})
observationsMetric := promauto.NewCounterVec(prometheus.CounterOpts{
Name: "beacon_observations",
Help: "Count of observations received from the p2p network",
}, []string{"guardian"})
// Update metrics for observation to track guardians behaviour
go func() {
for {
select {
case <-rootCtx.Done():
return
case batchObservation := <-obsvC:
guardian := hex.EncodeToString(batchObservation.Msg.Addr)
for _, observation := range batchObservation.Msg.Observations {
// A messageId has `chain/emittter/seq` format. We are only interested in the chainId.
// ChainId is a uint16 represented in base 10.
chainId, err := strconv.ParseUint(strings.Split(observation.MessageId, "/")[0], 10, 16)
if err != nil {
log.Error().Err(err).Msg("Failed to parse chainId")
continue
}
if vaa.ChainID(chainId) == vaa.ChainIDPythNet {
observationsMetric.WithLabelValues(guardian).Inc()
}
}
}
}
}()
// Ignore observation requests
// Note: without this, the whole program hangs on observation requests
go func() {
for {
select {
case <-rootCtx.Done():
return
case <-obsvReqC:
}
}
}()
// Log signed VAAs
go func() {
for {
select {
case <-rootCtx.Done():
return
case v := <-signedInC:
vaaBytes := v.GetVaa()
vaa, err := vaa.Unmarshal(vaaBytes)
if err != nil {
log.Error().Err(err).Msg("Failed to parse VAA")
continue
}
// Send message on channel, increment counter, and update heartbeat
channel <- vaa
messagesMetric.Inc()
if vaa.Timestamp.Unix() > heartbeat.Timestamp {
heartbeat.Timestamp = vaa.Timestamp.Unix()
// Only count the latency for the newer messages. This will
// give a better indication of the lag in the network.
//
// Also, we might receive a single message multiple times and doing the check
// here is that we only count the latency once.
messageLatencyMetric.Observe(time.Since(time.Unix(vaa.Timestamp.Unix(), 0)).Seconds())
}
log.Debug().Str("id", vaa.MessageID()).Msg("Received message")
}
}
}()
// Create a random private key
priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)
if err != nil {
log.Panic().Err(err).Msg("Failed to create private key")
}
// TODO: use zap logger everywhere
logger, err := zap.NewProductionConfig().Build()
if err != nil {
log.Panic().Err(err).Msg("Failed to create logger")
}
// Run supervisor.
supervisor.New(rootCtx, logger, func(ctx context.Context) error {
components := p2p.DefaultComponents()
components.Port = listenPort
params, err := p2p.NewRunParams(
bootstrapAddrs,
networkID,
priv,
gst,
rootCtxCancel,
p2p.WithSignedVAAListener(signedInC),
p2p.WithSignedObservationBatchListener(obsvC),
p2p.WithComponents(components),
)
if err != nil {
return err
}
if err := supervisor.Run(ctx,
"p2p",
p2p.Run(params)); err != nil {
return err
}
log.Info().Msg("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()
log.Info().Msg("root context cancelled, exiting...")
}