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

bugfix(op-node): syncClient incorrectly removes peer issue #50

Merged
merged 6 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion op-node/p2p/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ func (n *NodeP2P) init(resourcesCtx context.Context, rollupCfg *rollup.Config, l
n.syncCl.AddPeer(conn.RemotePeer())
},
DisconnectedF: func(nw network.Network, conn network.Conn) {
n.syncCl.RemovePeer(conn.RemotePeer())
// only when no connection is available, we can remove the peer
if nw.Connectedness(conn.RemotePeer()) == network.NotConnected {
n.syncCl.RemovePeer(conn.RemotePeer())
}
},
})
n.syncCl.Start()
Expand Down
54 changes: 54 additions & 0 deletions op-node/p2p/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,57 @@ func TestMultiPeerSync(t *testing.T) {
require.Equal(t, exp.BlockHash, p.BlockHash, "expecting the correct payload")
}
}

func TestNetworkNotifyAddPeerAndRemovePeer(t *testing.T) {
t.Parallel()
log := testlog.Logger(t, log.LvlDebug)

cfg, _ := setupSyncTestData(25)

confA := TestingConfig(t)
confB := TestingConfig(t)
hostA, err := confA.Host(log.New("host", "A"), nil, metrics.NoopMetrics)
require.NoError(t, err, "failed to launch host A")
defer hostA.Close()
hostB, err := confB.Host(log.New("host", "B"), nil, metrics.NoopMetrics)
require.NoError(t, err, "failed to launch host B")
defer hostB.Close()

syncCl := NewSyncClient(log, cfg, hostA.NewStream, func(ctx context.Context, from peer.ID, payload *eth.ExecutionPayload) error {
return nil
}, metrics.NoopMetrics)

waitChan := make(chan struct{}, 1)
hostA.Network().Notify(&network.NotifyBundle{
ConnectedF: func(nw network.Network, conn network.Conn) {
syncCl.AddPeer(conn.RemotePeer())
waitChan <- struct{}{}
},
DisconnectedF: func(nw network.Network, conn network.Conn) {
// only when no connection is available, we can remove the peer
if nw.Connectedness(conn.RemotePeer()) == network.NotConnected {
syncCl.RemovePeer(conn.RemotePeer())
}
waitChan <- struct{}{}
},
})
syncCl.Start()

err = hostA.Connect(context.Background(), peer.AddrInfo{ID: hostB.ID(), Addrs: hostB.Addrs()})
require.NoError(t, err, "failed to connect to peer B from peer A")
require.Equal(t, hostA.Network().Connectedness(hostB.ID()), network.Connected)

//wait for async add process done
<-waitChan
_, ok := syncCl.peers[hostB.ID()]
require.True(t, ok, "peerB should exist in syncClient")

err = hostA.Network().ClosePeer(hostB.ID())
require.NoError(t, err, "close peer fail")

//wait for async removing process done
<-waitChan
_, peerBExist3 := syncCl.peers[hostB.ID()]
require.True(t, !peerBExist3, "peerB should not exist in syncClient")

}
Loading