Skip to content

Commit

Permalink
Allow non primary network validators to request all peers (#3491)
Browse files Browse the repository at this point in the history
  • Loading branch information
cam-schultz authored Nov 15, 2024
1 parent aaedc23 commit c0b85a8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
7 changes: 2 additions & 5 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,12 +525,10 @@ func (n *network) KnownPeers() ([]byte, []byte) {
//
// - Respond with all subnet IPs
// - The peer requests all peers
// - We believe the peer to be a primary network validator
// - We believe ourself to be a primary network validator
//
// - Respond with subnet IPs tracked by the peer
// - Either the peer does not request all peers or we don't consider them to
// be a primary network validator
// - The peer does not request all peers
// - We believe ourself to be a primary network validator
//
// The reason we allow the peer to request all peers is so that we can avoid
Expand All @@ -555,8 +553,7 @@ func (n *network) Peers(
}
}

_, areTheyAPrimaryNetworkValidator := n.config.Validators.GetValidator(constants.PrimaryNetworkID, peerID)
if areWeAPrimaryNetworkValidator && requestAllPeers && areTheyAPrimaryNetworkValidator {
if areWeAPrimaryNetworkValidator && requestAllPeers {
// Return IPs for all subnets.
return getGossipableIPs(
n.ipTracker,
Expand Down
75 changes: 75 additions & 0 deletions network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/ava-labs/avalanchego/subnets"
"github.com/ava-labs/avalanchego/upgrade"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/bloom"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/ips"
Expand Down Expand Up @@ -753,3 +754,77 @@ func TestAllowConnectionAsAValidator(t *testing.T) {
}
wg.Wait()
}

func TestGetAllPeers(t *testing.T) {
require := require.New(t)

// Create a non-validator peer
dialer, listeners, nonVdrNodeIDs, configs := newTestNetwork(t, 1)

configs[0].Beacons = validators.NewManager()
configs[0].Validators = validators.NewManager()
nonValidatorNetwork, err := NewNetwork(
configs[0],
upgrade.InitiallyActiveTime,
newMessageCreator(t),
prometheus.NewRegistry(),
logging.NoLog{},
listeners[0],
dialer,
&testHandler{
InboundHandler: nil,
ConnectedF: nil,
DisconnectedF: nil,
},
)
require.NoError(err)

// Create a network of validators
nodeIDs, networks, wg := newFullyConnectedTestNetwork(
t,
[]router.InboundHandler{
nil, nil, nil,
},
)

// Connect the non-validator peer to the validator network
wg.Add(1)
nonValidatorNetwork.ManuallyTrack(networks[0].config.MyNodeID, networks[0].config.MyIPPort.Get())
go func() {
defer wg.Done()

require.NoError(nonValidatorNetwork.Dispatch())
}()

{
// The non-validator peer should be able to get all the peers in the network
peersListFromNonVdr := networks[0].Peers(nonVdrNodeIDs[0], nil, true, bloom.EmptyFilter, []byte{})
require.Len(peersListFromNonVdr, len(nodeIDs)-1)
peerNodes := set.NewSet[ids.NodeID](len(peersListFromNonVdr))
for _, peer := range peersListFromNonVdr {
peerNodes.Add(peer.NodeID)
}
for _, nodeID := range nodeIDs[1:] {
require.True(peerNodes.Contains(nodeID))
}
}

{
// A validator peer should be able to get all the peers in the network
peersListFromVdr := networks[0].Peers(nodeIDs[1], nil, true, bloom.EmptyFilter, []byte{})
require.Len(peersListFromVdr, len(nodeIDs)-2) // GetPeerList doesn't return the peer that requested it
peerNodes := set.NewSet[ids.NodeID](len(peersListFromVdr))
for _, peer := range peersListFromVdr {
peerNodes.Add(peer.NodeID)
}
for _, nodeID := range nodeIDs[2:] {
require.True(peerNodes.Contains(nodeID))
}
}

nonValidatorNetwork.StartClose()
for _, net := range networks {
net.StartClose()
}
wg.Wait()
}

0 comments on commit c0b85a8

Please sign in to comment.