Skip to content

Commit

Permalink
Fix connected weight calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkaplan13 committed Sep 11, 2024
1 parent 3c2ae78 commit 8e50532
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions peers/app_request_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package peers

import (
"context"
"encoding/hex"
"os"
"sync"
"time"
Expand Down Expand Up @@ -230,30 +231,37 @@ func (n *appRequestNetwork) ConnectToCanonicalValidators(subnetID ids.ID) (*Conn
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)
nodeIDs := set.NewSet[ids.NodeID](len(nodeValidatorIndexMap))
for i, vdr := range validatorSet {
for _, node := range vdr.NodeIDs {
nodeValidatorIndexMap[node] = i
nodeIDs.Add(node)
}
}

// 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
// Calculate the total weight of connected validators.
connectedBLSPubKeys := set.NewSet[string](len(validatorSet))
connectedWeight := uint64(0)
for node := range connectedNodes {
connectedWeight += validatorSet[nodeValidatorIndexMap[node]].Weight
vdr := validatorSet[nodeValidatorIndexMap[node]]
blsPubKey := hex.EncodeToString(vdr.PublicKeyBytes)
if connectedBLSPubKeys.Contains(blsPubKey) {
continue
}
connectedWeight += vdr.Weight
connectedBLSPubKeys.Add(blsPubKey)
}

return &ConnectedCanonicalValidators{
ConnectedWeight: connectedWeight,
TotalValidatorWeight: totalValidatorWeight,
Expand Down

0 comments on commit 8e50532

Please sign in to comment.