diff --git a/p2p/exchange.go b/p2p/exchange.go index c4dd8289..3b00a4e6 100644 --- a/p2p/exchange.go +++ b/p2p/exchange.go @@ -12,7 +12,6 @@ import ( "github.com/libp2p/go-libp2p/core/host" "github.com/libp2p/go-libp2p/core/peer" "github.com/libp2p/go-libp2p/core/protocol" - "github.com/libp2p/go-libp2p/p2p/net/conngater" "github.com/celestiaorg/go-header" p2p_pb "github.com/celestiaorg/go-header/p2p/pb" @@ -50,7 +49,7 @@ type Exchange[H header.Header[H]] struct { func NewExchange[H header.Header[H]]( host host.Host, peers peer.IDSlice, - gater *conngater.BasicConnectionGater, + blacklistPeer func(id peer.ID) error, opts ...Option[ClientParameters], ) (*Exchange[H], error) { params := DefaultClientParameters() @@ -66,7 +65,7 @@ func NewExchange[H header.Header[H]]( ex := &Exchange[H]{ host: host, protocolID: protocolID(params.networkID), - peerTracker: newPeerTracker(host, gater, params.pidstore), + peerTracker: newPeerTracker(host, blacklistPeer, params.pidstore), Params: params, } diff --git a/p2p/exchange_test.go b/p2p/exchange_test.go index 6b81f529..8bac8541 100644 --- a/p2p/exchange_test.go +++ b/p2p/exchange_test.go @@ -6,14 +6,11 @@ import ( "testing" "time" - "github.com/ipfs/go-datastore" - "github.com/ipfs/go-datastore/sync" libhost "github.com/libp2p/go-libp2p/core/host" "github.com/libp2p/go-libp2p/core/network" "github.com/libp2p/go-libp2p/core/peer" "github.com/libp2p/go-libp2p/core/peerstore" blankhost "github.com/libp2p/go-libp2p/p2p/host/blank" - "github.com/libp2p/go-libp2p/p2p/net/conngater" mocknet "github.com/libp2p/go-libp2p/p2p/net/mock" swarm "github.com/libp2p/go-libp2p/p2p/net/swarm/testing" "github.com/stretchr/testify/assert" @@ -148,6 +145,14 @@ func TestExchange_RequestVerifiedHeadersFails(t *testing.T) { hosts := createMocknet(t, 2) exchg, store := createP2PExAndServer(t, hosts[0], hosts[1]) store.Headers[2] = store.Headers[3] + + // replace blacklistPeer with a function that records the peer that was blacklisted + var blacklisted peer.ID + exchg.peerTracker.blacklistPeer = func(pID peer.ID) error { + blacklisted = pID + return nil + } + // perform expected request h := store.Headers[1] ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*500) @@ -156,9 +161,7 @@ func TestExchange_RequestVerifiedHeadersFails(t *testing.T) { assert.Error(t, err) // ensure that peer was added to the blacklist - peers := exchg.peerTracker.connGater.ListBlockedPeers() - require.Len(t, peers, 1) - require.True(t, hosts[1].ID() == peers[0]) + require.Equal(t, hosts[1].ID(), blacklisted) } // TestExchange_RequestFullRangeHeaders requests max amount of headers @@ -167,11 +170,9 @@ func TestExchange_RequestFullRangeHeaders(t *testing.T) { // create mocknet with 5 peers hosts := createMocknet(t, 5) store := headertest.NewStore[*headertest.DummyHeader](t, headertest.NewTestSuite(t), int(header.MaxRangeRequestSize)) - connGater, err := conngater.NewBasicConnectionGater(sync.MutexWrap(datastore.NewMapDatastore())) - require.NoError(t, err) // create new exchange - exchange, err := NewExchange[*headertest.DummyHeader](hosts[len(hosts)-1], []peer.ID{hosts[4].ID()}, connGater, + exchange, err := NewExchange[*headertest.DummyHeader](hosts[len(hosts)-1], []peer.ID{hosts[4].ID()}, nil, WithNetworkID[ClientParameters](networkID), WithChainID(networkID), ) @@ -509,10 +510,7 @@ func createP2PExAndServer( err = serverSideEx.Start(context.Background()) require.NoError(t, err) - connGater, err := conngater.NewBasicConnectionGater(sync.MutexWrap(datastore.NewMapDatastore())) - require.NoError(t, err) - - ex, err := NewExchange[*headertest.DummyHeader](host, []peer.ID{tpeer.ID()}, connGater, + ex, err := NewExchange[*headertest.DummyHeader](host, []peer.ID{tpeer.ID()}, nil, WithNetworkID[ClientParameters](networkID), WithChainID(networkID), ) diff --git a/p2p/peer_tracker.go b/p2p/peer_tracker.go index 8981fd7f..3b629fd6 100644 --- a/p2p/peer_tracker.go +++ b/p2p/peer_tracker.go @@ -9,7 +9,6 @@ import ( "github.com/libp2p/go-libp2p/core/host" "github.com/libp2p/go-libp2p/core/network" libpeer "github.com/libp2p/go-libp2p/core/peer" - "github.com/libp2p/go-libp2p/p2p/net/conngater" ) const ( @@ -28,8 +27,8 @@ var ( ) type peerTracker struct { - host host.Host - connGater *conngater.BasicConnectionGater + host host.Host + blacklistPeer func(id libpeer.ID) error peerLk sync.RWMutex // trackedPeers contains active peers that we can request to. @@ -53,13 +52,13 @@ type peerTracker struct { func newPeerTracker( h host.Host, - connGater *conngater.BasicConnectionGater, + blacklistPeer func(id libpeer.ID) error, pidstore PeerIDStore, ) *peerTracker { ctx, cancel := context.WithCancel(context.Background()) return &peerTracker{ host: h, - connGater: connGater, + blacklistPeer: blacklistPeer, trackedPeers: make(map[libpeer.ID]*peerStat), disconnectedPeers: make(map[libpeer.ID]*peerStat), pidstore: pidstore, @@ -301,7 +300,7 @@ func (p *peerTracker) stop(ctx context.Context) error { // blockPeer blocks a peer on the networking level and removes it from the local cache. func (p *peerTracker) blockPeer(pID libpeer.ID, reason error) { // add peer to the blacklist, so we can't connect to it in the future. - err := p.connGater.BlockPeer(pID) + err := p.blacklistPeer(pID) if err != nil { log.Errorw("header/p2p: blocking peer failed", "pID", pID, "err", err) } diff --git a/p2p/peer_tracker_test.go b/p2p/peer_tracker_test.go index ff5eaf31..7c070058 100644 --- a/p2p/peer_tracker_test.go +++ b/p2p/peer_tracker_test.go @@ -11,7 +11,6 @@ import ( "github.com/ipfs/go-datastore/sync" "github.com/libp2p/go-libp2p/core/peer" testpeer "github.com/libp2p/go-libp2p/core/test" - "github.com/libp2p/go-libp2p/p2p/net/conngater" mocknet "github.com/libp2p/go-libp2p/p2p/net/mock" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -25,11 +24,8 @@ func TestPeerTracker_GC(t *testing.T) { gcCycle = time.Millisecond * 200 - connGater, err := conngater.NewBasicConnectionGater(sync.MutexWrap(datastore.NewMapDatastore())) - require.NoError(t, err) - pidstore := newDummyPIDStore() - p := newPeerTracker(h[0], connGater, pidstore) + p := newPeerTracker(h[0], nil, pidstore) maxAwaitingTime = time.Millisecond @@ -51,7 +47,7 @@ func TestPeerTracker_GC(t *testing.T) { time.Sleep(time.Millisecond * 500) - err = p.stop(ctx) + err := p.stop(ctx) require.NoError(t, err) require.Nil(t, p.trackedPeers[pid1]) @@ -65,22 +61,22 @@ func TestPeerTracker_GC(t *testing.T) { func TestPeerTracker_BlockPeer(t *testing.T) { h := createMocknet(t, 2) - connGater, err := conngater.NewBasicConnectionGater(sync.MutexWrap(datastore.NewMapDatastore())) - require.NoError(t, err) - p := newPeerTracker(h[0], connGater, nil) - maxAwaitingTime = time.Millisecond + // create blacklistPeer function that records the peer that was blacklisted + var blacklisted peer.ID + blacklistPeer := func(pID peer.ID) error { + blacklisted = pID + return nil + } + p := newPeerTracker(h[0], blacklistPeer, nil) p.blockPeer(h[1].ID(), errors.New("test")) - require.Len(t, connGater.ListBlockedPeers(), 1) - require.True(t, connGater.ListBlockedPeers()[0] == h[1].ID()) + require.Equal(t, h[1].ID(), blacklisted) + require.Len(t, h[0].Network().Conns(), 0) } func TestPeerTracker_Bootstrap(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) - connGater, err := conngater.NewBasicConnectionGater(sync.MutexWrap(datastore.NewMapDatastore())) - require.NoError(t, err) - // mn := createMocknet(t, 10) mn, err := mocknet.FullMeshConnected(10) require.NoError(t, err) @@ -100,7 +96,7 @@ func TestPeerTracker_Bootstrap(t *testing.T) { err = pidstore.Put(ctx, prevSeen[2:]) require.NoError(t, err) - tracker := newPeerTracker(mn.Hosts()[0], connGater, pidstore) + tracker := newPeerTracker(mn.Hosts()[0], nil, pidstore) go tracker.track()