Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow non primary network validators to request all peers #3491

Merged
merged 9 commits into from
Nov 15, 2024
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 {
cam-schultz marked this conversation as resolved.
Show resolved Hide resolved
// 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) {
marun marked this conversation as resolved.
Show resolved Hide resolved
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()
}
Loading