Skip to content

Commit 86120e2

Browse files
feat(session): do not record erroneous session want sends (#452)
* feat(session): do not record erroneous session want sends Co-authored-by: gammazero <[email protected]>
1 parent 5965637 commit 86120e2

File tree

7 files changed

+35
-17
lines changed

7 files changed

+35
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ The following emojis are used to highlight certain changes:
8080

8181
### Fixed
8282

83+
- Do not erroneously update the state of sent wants when a send a peer disconnected and the send did not happen. [#452](https://github.com/ipfs/boxo/pull/452)
84+
8385
### Security
8486

8587
## [v0.24.3]

bitswap/client/internal/peermanager/peermanager.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,15 @@ func (pm *PeerManager) BroadcastWantHaves(ctx context.Context, wantHaves []cid.C
143143

144144
// SendWants sends the given want-blocks and want-haves to the given peer.
145145
// It filters out wants that have previously been sent to the peer.
146-
func (pm *PeerManager) SendWants(ctx context.Context, p peer.ID, wantBlocks []cid.Cid, wantHaves []cid.Cid) {
146+
func (pm *PeerManager) SendWants(ctx context.Context, p peer.ID, wantBlocks []cid.Cid, wantHaves []cid.Cid) bool {
147147
pm.pqLk.Lock()
148148
defer pm.pqLk.Unlock()
149149

150-
if _, ok := pm.peerQueues[p]; ok {
151-
pm.pwm.sendWants(p, wantBlocks, wantHaves)
150+
if _, ok := pm.peerQueues[p]; !ok {
151+
return false
152152
}
153+
pm.pwm.sendWants(p, wantBlocks, wantHaves)
154+
return true
153155
}
154156

155157
// SendCancels sends cancels for the given keys to all peers who had previously

bitswap/client/internal/session/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type PeerManager interface {
3838
// interested in a peer's connection state
3939
UnregisterSession(uint64)
4040
// SendWants tells the PeerManager to send wants to the given peer
41-
SendWants(ctx context.Context, peerId peer.ID, wantBlocks []cid.Cid, wantHaves []cid.Cid)
41+
SendWants(ctx context.Context, peerId peer.ID, wantBlocks []cid.Cid, wantHaves []cid.Cid) bool
4242
// BroadcastWantHaves sends want-haves to all connected peers (used for
4343
// session discovery)
4444
BroadcastWantHaves(context.Context, []cid.Cid)

bitswap/client/internal/session/session_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,11 @@ func newFakePeerManager() *fakePeerManager {
141141
}
142142
}
143143

144-
func (pm *fakePeerManager) RegisterSession(peer.ID, bspm.Session) {}
145-
func (pm *fakePeerManager) UnregisterSession(uint64) {}
146-
func (pm *fakePeerManager) SendWants(context.Context, peer.ID, []cid.Cid, []cid.Cid) {}
144+
func (pm *fakePeerManager) RegisterSession(peer.ID, bspm.Session) {}
145+
func (pm *fakePeerManager) UnregisterSession(uint64) {}
146+
func (pm *fakePeerManager) SendWants(context.Context, peer.ID, []cid.Cid, []cid.Cid) bool {
147+
return true
148+
}
147149
func (pm *fakePeerManager) BroadcastWantHaves(ctx context.Context, cids []cid.Cid) {
148150
select {
149151
case pm.wantReqs <- wantReq{cids}:

bitswap/client/internal/session/sessionwantsender.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ func (sws *sessionWantSender) processExhaustedWants(exhausted []cid.Cid) {
513513
type wantSets struct {
514514
wantBlocks *cid.Set
515515
wantHaves *cid.Set
516+
sent bool
516517
}
517518

518519
type allWants map[peer.ID]*wantSets
@@ -551,9 +552,6 @@ func (sws *sessionWantSender) sendNextWants(newlyAvailable []peer.ID) {
551552
continue
552553
}
553554

554-
// Record that we are sending a want-block for this want to the peer
555-
sws.setWantSentTo(c, wi.bestPeer)
556-
557555
// Send a want-block to the chosen peer
558556
toSend.forPeer(wi.bestPeer).wantBlocks.Add(c)
559557

@@ -567,6 +565,16 @@ func (sws *sessionWantSender) sendNextWants(newlyAvailable []peer.ID) {
567565

568566
// Send any wants we've collected
569567
sws.sendWants(toSend)
568+
569+
for c, wi := range sws.wants {
570+
if wi.bestPeer != "" && wi.sentTo == "" {
571+
// check if a want block was successfully sent to the best peer
572+
if toSend.forPeer(wi.bestPeer).sent {
573+
// Record that we are sending a want-block for this want to the peer
574+
sws.setWantSentTo(c, wi.bestPeer)
575+
}
576+
}
577+
}
570578
}
571579

572580
// sendWants sends want-have and want-blocks to the appropriate peers
@@ -584,8 +592,11 @@ func (sws *sessionWantSender) sendWants(sends allWants) {
584592
// precedence over want-haves.
585593
wblks := snd.wantBlocks.Keys()
586594
whaves := snd.wantHaves.Keys()
587-
sws.pm.SendWants(sws.ctx, p, wblks, whaves)
588-
595+
snd.sent = sws.pm.SendWants(sws.ctx, p, wblks, whaves)
596+
if !snd.sent {
597+
// Do not update state if the wants not sent.
598+
continue
599+
}
589600
// Inform the session that we've sent the wants
590601
sws.onSend(p, wblks, whaves)
591602

bitswap/client/internal/session/sessionwantsender_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (*mockPeerManager) UnregisterSession(uint64) {}
8282
func (*mockPeerManager) BroadcastWantHaves(context.Context, []cid.Cid) {}
8383
func (*mockPeerManager) SendCancels(context.Context, []cid.Cid) {}
8484

85-
func (pm *mockPeerManager) SendWants(ctx context.Context, p peer.ID, wantBlocks []cid.Cid, wantHaves []cid.Cid) {
85+
func (pm *mockPeerManager) SendWants(ctx context.Context, p peer.ID, wantBlocks []cid.Cid, wantHaves []cid.Cid) bool {
8686
pm.lk.Lock()
8787
defer pm.lk.Unlock()
8888

@@ -92,6 +92,7 @@ func (pm *mockPeerManager) SendWants(ctx context.Context, p peer.ID, wantBlocks
9292
pm.peerSends[p] = sw
9393
}
9494
sw.add(wantBlocks, wantHaves)
95+
return true
9596
}
9697

9798
func (pm *mockPeerManager) waitNextWants() map[peer.ID]*sentWants {

bitswap/client/internal/sessionmanager/sessionmanager_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ type fakePeerManager struct {
6666
cancels []cid.Cid
6767
}
6868

69-
func (*fakePeerManager) RegisterSession(peer.ID, bspm.Session) {}
70-
func (*fakePeerManager) UnregisterSession(uint64) {}
71-
func (*fakePeerManager) SendWants(context.Context, peer.ID, []cid.Cid, []cid.Cid) {}
72-
func (*fakePeerManager) BroadcastWantHaves(context.Context, []cid.Cid) {}
69+
func (*fakePeerManager) RegisterSession(peer.ID, bspm.Session) {}
70+
func (*fakePeerManager) UnregisterSession(uint64) {}
71+
func (*fakePeerManager) SendWants(context.Context, peer.ID, []cid.Cid, []cid.Cid) bool { return true }
72+
func (*fakePeerManager) BroadcastWantHaves(context.Context, []cid.Cid) {}
7373
func (fpm *fakePeerManager) SendCancels(ctx context.Context, cancels []cid.Cid) {
7474
fpm.lk.Lock()
7575
defer fpm.lk.Unlock()

0 commit comments

Comments
 (0)