Skip to content

Commit

Permalink
Revert networking AllowConnection change (#2219)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Oct 25, 2023
1 parent f020a05 commit 1f779af
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
6 changes: 5 additions & 1 deletion network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,11 @@ func (n *network) Connected(nodeID ids.NodeID) {
// of peers, then it should only connect if this node is a validator, or the
// peer is a validator/beacon.
func (n *network) AllowConnection(nodeID ids.NodeID) bool {
return !n.config.RequireValidatorToConnect || n.WantsConnection(nodeID)
if !n.config.RequireValidatorToConnect {
return true
}
_, isValidator := n.config.Validators.GetValidator(constants.PrimaryNetworkID, n.config.MyNodeID)
return isValidator || n.WantsConnection(nodeID)
}

func (n *network) Track(peerID ids.NodeID, claimedIPPorts []*ips.ClaimedIPPort) ([]*p2p.PeerAck, error) {
Expand Down
85 changes: 85 additions & 0 deletions network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,3 +683,88 @@ func TestDialContext(t *testing.T) {
network.StartClose()
wg.Wait()
}

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

dialer, listeners, nodeIDs, configs := newTestNetwork(t, 2)

networks := make([]Network, len(configs))
for i, config := range configs {
msgCreator := newMessageCreator(t)
registry := prometheus.NewRegistry()

g, err := peer.NewGossipTracker(registry, "foobar")
require.NoError(err)

log := logging.NoLog{}
gossipTrackerCallback := peer.GossipTrackerCallback{
Log: log,
GossipTracker: g,
}

beacons := validators.NewManager()
require.NoError(beacons.AddStaker(constants.PrimaryNetworkID, nodeIDs[0], nil, ids.GenerateTestID(), 1))

vdrs := validators.NewManager()
vdrs.RegisterCallbackListener(constants.PrimaryNetworkID, &gossipTrackerCallback)
require.NoError(vdrs.AddStaker(constants.PrimaryNetworkID, nodeIDs[0], nil, ids.GenerateTestID(), 1))

config := config

config.GossipTracker = g
config.Beacons = beacons
config.Validators = vdrs
config.RequireValidatorToConnect = true

net, err := NewNetwork(
config,
msgCreator,
registry,
log,
listeners[i],
dialer,
&testHandler{
InboundHandler: nil,
ConnectedF: nil,
DisconnectedF: nil,
},
)
require.NoError(err)
networks[i] = net
}

wg := sync.WaitGroup{}
wg.Add(len(networks))
for i, net := range networks {
if i != 0 {
config := configs[0]
net.ManuallyTrack(config.MyNodeID, config.MyIPPort.IPPort())
}

go func(net Network) {
defer wg.Done()

require.NoError(net.Dispatch())
}(net)
}

network := networks[1].(*network)
require.Eventually(
func() bool {
network.peersLock.RLock()
defer network.peersLock.RUnlock()

nodeID := nodeIDs[0]
_, contains := network.connectedPeers.GetByID(nodeID)
return contains
},
10*time.Second,
50*time.Millisecond,
)

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

0 comments on commit 1f779af

Please sign in to comment.