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

fixed kbucket bug :) #117

Merged
merged 2 commits into from
May 16, 2023
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
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