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

fix sending cancels when excluding peer #805

Merged
merged 1 commit into from
Jan 22, 2025
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
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
Loading