Skip to content

Commit

Permalink
Merge pull request #117 from JamesHertz/master
Browse files Browse the repository at this point in the history
Fixed peer replacement with bucket size of 1.
  • Loading branch information
guillaumemichel authored May 16, 2023
2 parents 182a8e0 + 00f987b commit 06cb923
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
31 changes: 18 additions & 13 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,24 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool, isReplaceable bool) (
})

if replaceablePeer != nil && replaceablePeer.replaceable {
// let's evict it and add the new peer
if rt.removePeer(replaceablePeer.Id) {
bucket.pushFront(&PeerInfo{
Id: p,
LastUsefulAt: lastUsefulAt,
LastSuccessfulOutboundQueryAt: now,
AddedAt: now,
dhtId: ConvertPeerID(p),
replaceable: isReplaceable,
})
rt.PeerAdded(p)
return true, nil
}
// we found a replaceable peer, let's replace it with the new peer.

// add new peer to the bucket. needs to happen before we remove the replaceable peer
// as if the bucket size is 1, we will end up removing the only peer, and deleting
// the bucket.
bucket.pushFront(&PeerInfo{
Id: p,
LastUsefulAt: lastUsefulAt,
LastSuccessfulOutboundQueryAt: now,
AddedAt: now,
dhtId: ConvertPeerID(p),
replaceable: isReplaceable,
})
rt.PeerAdded(p)

// remove the replaceable peer
rt.removePeer(replaceablePeer.Id)
return true, nil
}

// we weren't able to find place for the peer, remove it from the filter state.
Expand Down
18 changes: 18 additions & 0 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,24 @@ func TestTryAddPeer(t *testing.T) {

}

func TestReplacePeerWithBucketSize1(t *testing.T) {
localID := test.RandPeerIDFatal(t)
rt, err := NewRoutingTable(1, ConvertPeerID(localID), time.Hour, pstore.NewMetrics(), NoOpThreshold, nil)
require.NoError(t, err)
p1, _ := rt.GenRandPeerID(1) // for any targetCpl > 0
p2, _ := rt.GenRandPeerID(1)

rt.TryAddPeer(p1, true, true)
success, err := rt.TryAddPeer(p2, true, true)

require.NoError(t, err)
require.True(t, success)

require.Equal(t, peer.ID(""), rt.Find(p1))
require.Equal(t, p2, rt.Find(p2))
require.Equal(t, rt.Size(), 1)
}

func TestMarkAllPeersIrreplaceable(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 06cb923

Please sign in to comment.