Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
fix: block receive shouldn't affect DONT_HAVE count for other peers
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc authored and Stebalien committed Mar 6, 2020
1 parent 2112d90 commit 33443d7
Showing 1 changed file with 25 additions and 30 deletions.
55 changes: 25 additions & 30 deletions internal/session/sessionwantsender.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,15 @@ func (sws *sessionWantSender) processUpdates(updates []update) []cid.Cid {
prunePeers := make(map[peer.ID]struct{})
for _, upd := range updates {
for _, c := range upd.dontHaves {
// If we already received a block for the want, ignore any
// DONT_HAVE for the want
// Track the number of consecutive DONT_HAVEs each peer receives
if sws.peerConsecutiveDontHaves[upd.from] == peerDontHaveLimit {
prunePeers[upd.from] = struct{}{}
} else {
sws.peerConsecutiveDontHaves[upd.from]++
}

// If we already received a block for the want, there's no need to
// update block presence etc
if blkCids.Has(c) {
continue
}
Expand All @@ -341,28 +348,18 @@ func (sws *sessionWantSender) processUpdates(updates []update) []cid.Cid {
sws.setWantSentTo(c, "")
}
}

// Track the number of consecutive DONT_HAVEs each peer receives
if sws.peerConsecutiveDontHaves[upd.from] == peerDontHaveLimit {
prunePeers[upd.from] = struct{}{}
} else {
sws.peerConsecutiveDontHaves[upd.from]++
}
}
}

// Process received HAVEs
for _, upd := range updates {
for _, c := range upd.haves {
// If we already received a block for the want, ignore any HAVE for
// the want
if blkCids.Has(c) {
continue
// If we haven't already received a block for the want
if !blkCids.Has(c) {
// Update the block presence for the peer
sws.updateWantBlockPresence(c, upd.from)
}

// Update the block presence for the peer
sws.updateWantBlockPresence(c, upd.from)

// Clear the consecutive DONT_HAVE count for the peer
delete(sws.peerConsecutiveDontHaves, upd.from)
delete(prunePeers, upd.from)
Expand All @@ -372,23 +369,21 @@ func (sws *sessionWantSender) processUpdates(updates []update) []cid.Cid {
// If any peers have sent us too many consecutive DONT_HAVEs, remove them
// from the session
if len(prunePeers) > 0 {
for p := range prunePeers {
// Before removing the peer from the session, check if the peer
// sent us a HAVE for a block that we want
for c := range sws.wants {
if sws.bpm.PeerHasBlock(p, c) {
delete(prunePeers, p)
break
}
}
}
go func() {
for p := range prunePeers {
// Before removing the peer from the session, check if the peer
// sent us a HAVE for a block that we want
peerHasWantedBlock := false
for c := range sws.wants {
if sws.bpm.PeerHasBlock(p, c) {
peerHasWantedBlock = true
break
}
}

// Peer doesn't have anything we want, so remove it
if !peerHasWantedBlock {
log.Infof("peer %s sent too many dont haves", lu.P(p))
sws.SignalAvailability(p, false)
}
log.Infof("peer %s sent too many dont haves", lu.P(p))
sws.SignalAvailability(p, false)
}
}()
}
Expand Down

0 comments on commit 33443d7

Please sign in to comment.