Skip to content

Commit

Permalink
fix sending cancels when excluding peer
Browse files Browse the repository at this point in the history
When sending cancels for want-block and want-have requests, a peer may be sxcluded because it was the sender of a block and that peer will cancel the want itself without having to receive a cancel message. The would-be sender of tha cancel message still needs to do everything, such as adjusting want counts, as if the cancel were being sent and then not send out the cancel message.
  • Loading branch information
gammazero committed Jan 22, 2025
1 parent 9d04741 commit 2d4df29
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
10 changes: 5 additions & 5 deletions bitswap/client/internal/peermanager/peermanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func TestSendCancelsExclude(t *testing.T) {
// Clear messages
collectMessages(msgs, 2*time.Millisecond)

// Send cancels for 1 want-block and 1 want-have
// Send cancels for 1 want-block and 1 want-have, excluding peer1.
peerManager.SendCancels(ctx, []cid.Cid{cids[0], cids[2]}, peer1)
collected := collectMessages(msgs, 2*time.Millisecond)

Expand All @@ -292,15 +292,15 @@ func TestSendCancelsExclude(t *testing.T) {
t.Fatal("Expected no cancels to be sent to excluded peer")
}

// Send cancels for all cids
// Send cancels for all cids. Expect cancels for the 1 remaining sid that
// was not previously canceled.
peerManager.SendCancels(ctx, cids, "")
collected = collectMessages(msgs, 2*time.Millisecond)

if _, ok := collected[peer2]; ok {
t.Fatal("Expected no cancels to be sent to peer that was not sent messages")
}
if len(collected[peer1].cancels) != 3 {
t.Fatal("Expected cancel to be sent for want-blocks")
if len(collected[peer1].cancels) != 1 {
t.Fatalf("Expected cancel to be sent for 1 want-blocks, got %d", len(collected[peer1].cancels))
}
}

Expand Down
18 changes: 12 additions & 6 deletions bitswap/client/internal/peermanager/peerwantmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,15 @@ func (pwm *peerWantManager) sendCancels(cancelKs []cid.Cid, excludePeer peer.ID)

// Send cancels to a particular peer
send := func(p peer.ID, pws *peerWant) {
// Start from the broadcast cancels
toCancel := broadcastCancels
noSend := p == excludePeer

var toCancel []cid.Cid

// If peer is not excluded, then send broadcast cancels to this peer.
if !noSend {
// Start from the broadcast cancels
toCancel = broadcastCancels
}

// For each key to be cancelled
for _, c := range cancelKs {
Expand All @@ -271,9 +278,9 @@ func (pwm *peerWantManager) sendCancels(cancelKs []cid.Cid, excludePeer peer.ID)
pws.wantBlocks.Remove(c)
pws.wantHaves.Remove(c)

// If it's a broadcast want, we've already added it to
// the peer cancels.
if !pwm.broadcastWants.Has(c) {
// If peer is not excluded and this a broadcast want is not already
// added it to the peer cancels, then add the cancel.
if !noSend && !pwm.broadcastWants.Has(c) {
toCancel = append(toCancel, c)
}
}
Expand All @@ -298,7 +305,6 @@ func (pwm *peerWantManager) sendCancels(cancelKs []cid.Cid, excludePeer peer.ID)
cancelPeers[p] = struct{}{}
}
}
delete(cancelPeers, excludePeer)
for p := range cancelPeers {
pws, ok := pwm.peerWants[p]
if !ok {
Expand Down

0 comments on commit 2d4df29

Please sign in to comment.