-
Notifications
You must be signed in to change notification settings - Fork 19
/
app_request_network.go
347 lines (313 loc) · 11.6 KB
/
app_request_network.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
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package peers
import (
"context"
"math/big"
"os"
"sync"
"time"
"github.com/ava-labs/avalanchego/api/info"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/message"
"github.com/ava-labs/avalanchego/network"
snowVdrs "github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/ips"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/vms/platformvm"
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
"github.com/ava-labs/awm-relayer/config"
"github.com/ava-labs/awm-relayer/utils"
"github.com/ava-labs/awm-relayer/validators"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
)
const (
InboundMessageChannelSize = 1000
DefaultAppRequestTimeout = time.Second * 2
)
type AppRequestNetwork struct {
Network network.Network
Handler *RelayerExternalHandler
infoClient info.Client
logger logging.Logger
lock *sync.Mutex
validatorClient *validators.CanonicalValidatorClient
}
// NewNetwork connects to a peers at the app request level.
func NewNetwork(
logLevel logging.Level,
registerer prometheus.Registerer,
cfg *config.Config,
infoClient info.Client,
pChainClient platformvm.Client,
) (*AppRequestNetwork, map[ids.ID]chan message.InboundMessage, error) {
logger := logging.NewLogger(
"awm-relayer-p2p",
logging.NewWrappedCore(
logLevel,
os.Stdout,
logging.JSON.ConsoleEncoder(),
),
)
networkID, err := infoClient.GetNetworkID(context.Background())
if err != nil {
logger.Error(
"Failed to get network ID",
zap.Error(err),
)
return nil, nil, err
}
// Create the test network for AppRequests
var trackedSubnets set.Set[ids.ID]
for _, sourceBlockchain := range cfg.SourceBlockchains {
trackedSubnets.Add(sourceBlockchain.GetSubnetID())
}
// Construct a response chan for each chain. Inbound messages will be routed to the proper channel in the handler
responseChans := make(map[ids.ID]chan message.InboundMessage)
for _, sourceBlockchain := range cfg.SourceBlockchains {
responseChan := make(chan message.InboundMessage, InboundMessageChannelSize)
responseChans[sourceBlockchain.GetBlockchainID()] = responseChan
}
responseChansLock := new(sync.RWMutex)
handler, err := NewRelayerExternalHandler(logger, registerer, responseChans, responseChansLock)
if err != nil {
logger.Error(
"Failed to create p2p network handler",
zap.Error(err),
)
return nil, nil, err
}
testNetwork, err := network.NewTestNetwork(logger, networkID, snowVdrs.NewManager(), trackedSubnets, handler)
if err != nil {
logger.Error(
"Failed to create test network",
zap.Error(err),
)
return nil, nil, err
}
validatorClient := validators.NewCanonicalValidatorClient(logger, pChainClient)
arNetwork := &AppRequestNetwork{
Network: testNetwork,
Handler: handler,
infoClient: infoClient,
logger: logger,
lock: new(sync.Mutex),
validatorClient: validatorClient,
}
// Manually connect to the validators of each of the source subnets.
// We return an error if we are unable to connect to sufficient stake on any of the subnets.
// Sufficient stake is determined by the Warp quora of the configured supported destinations,
// or if the subnet supports all destinations, by the quora of all configured destinations.
for _, sourceBlockchain := range cfg.SourceBlockchains {
if sourceBlockchain.GetSubnetID() == constants.PrimaryNetworkID {
if err := arNetwork.connectToPrimaryNetworkPeers(cfg, sourceBlockchain); err != nil {
return nil, nil, err
}
} else {
if err := arNetwork.connectToNonPrimaryNetworkPeers(cfg, sourceBlockchain); err != nil {
return nil, nil, err
}
}
}
go logger.RecoverAndPanic(func() {
testNetwork.Dispatch()
})
return arNetwork, responseChans, nil
}
// ConnectPeers connects the network to peers with the given nodeIDs.
// Returns the set of nodeIDs that were successfully connected to.
func (n *AppRequestNetwork) ConnectPeers(nodeIDs set.Set[ids.NodeID]) set.Set[ids.NodeID] {
n.lock.Lock()
defer n.lock.Unlock()
// First, check if we are already connected to all the peers
connectedPeers := n.Network.PeerInfo(nodeIDs.List())
if len(connectedPeers) == nodeIDs.Len() {
return nodeIDs
}
// If we are not connected to all the peers already, then we have to iterate
// through the full list of peers obtained from the info API. Rather than iterating
// through connectedPeers for already tracked peers, just iterate through the full list,
// re-adding connections to already tracked peers.
// Get the list of peers
peers, err := n.infoClient.Peers(context.Background())
if err != nil {
n.logger.Error(
"Failed to get peers",
zap.Error(err),
)
return nil
}
// Attempt to connect to each peer
var trackedNodes set.Set[ids.NodeID]
for _, peer := range peers {
if nodeIDs.Contains(peer.ID) {
ipPort, err := ips.ToIPPort(peer.PublicIP)
if err != nil {
n.logger.Error(
"Failed to parse peer IP",
zap.String("beaconIP", peer.PublicIP),
zap.Error(err),
)
continue
}
trackedNodes.Add(peer.ID)
n.Network.ManuallyTrack(peer.ID, ipPort)
if len(trackedNodes) == nodeIDs.Len() {
return trackedNodes
}
}
}
// If the Info API node is in nodeIDs, it will not be reflected in the call to info.Peers.
// In this case, we need to manually track the API node.
if apiNodeID, _, err := n.infoClient.GetNodeID(context.Background()); err != nil {
n.logger.Error(
"Failed to get API Node ID",
zap.Error(err),
)
} else if nodeIDs.Contains(apiNodeID) {
if apiNodeIP, err := n.infoClient.GetNodeIP(context.Background()); err != nil {
n.logger.Error(
"Failed to get API Node IP",
zap.Error(err),
)
} else if ipPort, err := ips.ToIPPort(apiNodeIP); err != nil {
n.logger.Error(
"Failed to parse API Node IP",
zap.String("nodeIP", apiNodeIP),
zap.Error(err),
)
} else {
trackedNodes.Add(apiNodeID)
n.Network.ManuallyTrack(apiNodeID, ipPort)
}
}
return trackedNodes
}
// Helper struct to hold connected validator information
// Warp Validators sharing the same BLS key may consist of multiple nodes,
// so we need to track the node ID to validator index mapping
type ConnectedCanonicalValidators struct {
ConnectedWeight uint64
TotalValidatorWeight uint64
ValidatorSet []*warp.Validator
nodeValidatorIndexMap map[ids.NodeID]int
}
// Returns the Warp Validator and its index in the canonical Validator ordering for a given nodeID
func (c *ConnectedCanonicalValidators) GetValidator(nodeID ids.NodeID) (*warp.Validator, int) {
return c.ValidatorSet[c.nodeValidatorIndexMap[nodeID]], c.nodeValidatorIndexMap[nodeID]
}
// ConnectToCanonicalValidators connects to the canonical validators of the given subnet and returns the connected
// validator information
func (n *AppRequestNetwork) ConnectToCanonicalValidators(subnetID ids.ID) (*ConnectedCanonicalValidators, error) {
// Get the subnet's current canonical validator set
validatorSet, totalValidatorWeight, err := n.validatorClient.GetCurrentCanonicalValidatorSet(subnetID)
if err != nil {
return nil, err
}
// We make queries to node IDs, not unique validators as represented by a BLS pubkey, so we need this map to track
// responses from nodes and populate the signatureMap with the corresponding validator signature
// This maps node IDs to the index in the canonical validator set
nodeValidatorIndexMap := make(map[ids.NodeID]int)
for i, vdr := range validatorSet {
for _, node := range vdr.NodeIDs {
nodeValidatorIndexMap[node] = i
}
}
// Manually connect to all peers in the validator set
// If new peers are connected, AppRequests may fail while the handshake is in progress.
// In that case, AppRequests to those nodes will be retried in the next iteration of the retry loop.
nodeIDs := set.NewSet[ids.NodeID](len(nodeValidatorIndexMap))
for node := range nodeValidatorIndexMap {
nodeIDs.Add(node)
}
connectedNodes := n.ConnectPeers(nodeIDs)
// Check if we've connected to a stake threshold of nodes
connectedWeight := uint64(0)
for node := range connectedNodes {
connectedWeight += validatorSet[nodeValidatorIndexMap[node]].Weight
}
return &ConnectedCanonicalValidators{
ConnectedWeight: connectedWeight,
TotalValidatorWeight: totalValidatorWeight,
ValidatorSet: validatorSet,
nodeValidatorIndexMap: nodeValidatorIndexMap,
}, nil
}
// Private helpers
// Connect to the validators of the source blockchain. For each destination blockchain, verify that we have connected to a threshold of stake.
func (n *AppRequestNetwork) connectToNonPrimaryNetworkPeers(cfg *config.Config, sourceBlockchain *config.SourceBlockchain) error {
subnetID := sourceBlockchain.GetSubnetID()
connectedValidators, err := n.ConnectToCanonicalValidators(subnetID)
if err != nil {
n.logger.Error(
"Failed to connect to canonical validators",
zap.String("subnetID", subnetID.String()),
zap.Error(err),
)
return err
}
for _, destinationBlockchainID := range sourceBlockchain.GetSupportedDestinations().List() {
if ok, quorum, err := n.checkForSufficientConnectedStake(cfg, connectedValidators, destinationBlockchainID); !ok {
n.logger.Error(
"Failed to connect to a threshold of stake",
zap.String("destinationBlockchainID", destinationBlockchainID.String()),
zap.Uint64("connectedWeight", connectedValidators.ConnectedWeight),
zap.Uint64("totalValidatorWeight", connectedValidators.TotalValidatorWeight),
zap.Any("warpQuorum", quorum),
)
return err
}
}
return nil
}
// Connect to the validators of the destination blockchains. Verify that we have connected to a threshold of stake for each blockchain.
func (n *AppRequestNetwork) connectToPrimaryNetworkPeers(cfg *config.Config, sourceBlockchain *config.SourceBlockchain) error {
for _, destinationBlockchainID := range sourceBlockchain.GetSupportedDestinations().List() {
subnetID := cfg.GetSubnetID(destinationBlockchainID)
connectedValidators, err := n.ConnectToCanonicalValidators(subnetID)
if err != nil {
n.logger.Error(
"Failed to connect to canonical validators",
zap.String("subnetID", subnetID.String()),
zap.Error(err),
)
return err
}
if ok, quorum, err := n.checkForSufficientConnectedStake(cfg, connectedValidators, destinationBlockchainID); !ok {
n.logger.Error(
"Failed to connect to a threshold of stake",
zap.String("destinationBlockchainID", destinationBlockchainID.String()),
zap.Uint64("connectedWeight", connectedValidators.ConnectedWeight),
zap.Uint64("totalValidatorWeight", connectedValidators.TotalValidatorWeight),
zap.Any("warpQuorum", quorum),
)
return err
}
}
return nil
}
// Fetch the warp quorum from the config and check if the connected stake exceeds the threshold
func (n *AppRequestNetwork) checkForSufficientConnectedStake(
cfg *config.Config,
connectedValidators *ConnectedCanonicalValidators,
destinationBlockchainID ids.ID,
) (bool, *config.WarpQuorum, error) {
quorum, err := cfg.GetWarpQuorum(destinationBlockchainID)
if err != nil {
n.logger.Error(
"Failed to get warp quorum from config",
zap.String("destinationBlockchainID", destinationBlockchainID.String()),
zap.Error(err),
)
return false, nil, err
}
return utils.CheckStakeWeightExceedsThreshold(
big.NewInt(0).SetUint64(connectedValidators.ConnectedWeight),
connectedValidators.TotalValidatorWeight,
quorum.QuorumNumerator,
quorum.QuorumDenominator,
), &quorum, nil
}