Skip to content

Commit

Permalink
added UsefulPeer function and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumemichel committed Feb 23, 2023
1 parent 2e31078 commit 43db441
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
43 changes: 43 additions & 0 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,49 @@ func (rt *RoutingTable) NPeersForCpl(cpl uint) int {
}
}

// UsefulPeer verifies whether the given peer.ID would be a good fit for the routing table
// It returns true if the bucket corresponding to peer.ID isn't full, if it contains
// replaceable peers or if it is the last bucket and adding a peer would unfold it.
func (rt *RoutingTable) UsefulPeer(p peer.ID) bool {
rt.tabLock.RLock()
defer rt.tabLock.RUnlock()

// bucket corresponding to p
bucketID := rt.bucketIdForPeer(p)
bucket := rt.buckets[bucketID]

// bucket isn't full
if bucket.len() < rt.bucketsize {
return true
}

// find and returns a replaceable peer (if any)
replaceablePeer := bucket.min(func(p1 *PeerInfo, p2 *PeerInfo) bool {
return p1.replaceable
})
if replaceablePeer != nil && replaceablePeer.replaceable {
// at least 1 peer is replaceable
return true
}

// the last bucket potentially contains peer ids with different CPL,
// and can be split in 2 buckets if needed
if bucketID == len(rt.buckets)-1 {
peers := bucket.peers()
cpl := CommonPrefixLen(rt.local, ConvertPeerID(p))
for _, peer := range peers {
// if at least 2 peers have a different CPL, the new peer is
// useful and will trigger a bucket split
if CommonPrefixLen(rt.local, peer.dhtId) != cpl {
return true
}
}
}

// the appropriate bucket is full of non replaceable peers
return false
}

// TryAddPeer tries to add a peer to the Routing table.
// If the peer ALREADY exists in the Routing Table and has been queried before, this call is a no-op.
// If the peer ALREADY exists in the Routing Table but hasn't been queried before, we set it's LastUsefulAt value to
Expand Down
65 changes: 65 additions & 0 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,71 @@ func TestNPeersForCpl(t *testing.T) {
require.Equal(t, 2, rt.NPeersForCpl(0))
}

func TestUsefulPeer(t *testing.T) {
t.Parallel()
local := test.RandPeerIDFatal(t)
m := pstore.NewMetrics()
rt, err := NewRoutingTable(2, ConvertPeerID(local), time.Hour, m, NoOpThreshold, nil)
require.NoError(t, err)

// add first peer to bucket 0
p, _ := rt.GenRandPeerID(0)
require.True(t, rt.UsefulPeer(p))
rt.TryAddPeer(p, true, false)

// add second peer to bucket 0
p, _ = rt.GenRandPeerID(0)
require.True(t, rt.UsefulPeer(p))
rt.TryAddPeer(p, true, false)

// bucket 0 (also last bucket) full with non replaceable peers
p, _ = rt.GenRandPeerID(0)
require.False(t, rt.UsefulPeer(p))

// bucket 0 is full, unfolding it
// add first peer to bucket 1
p, _ = rt.GenRandPeerID(1)
require.True(t, rt.UsefulPeer(p))
rt.TryAddPeer(p, true, false)

// add second peer to bucket 1
// cpl is 2, but bucket 1 is last bucket
p, _ = rt.GenRandPeerID(2)
require.True(t, rt.UsefulPeer(p))
rt.TryAddPeer(p, true, false)

// unfolding bucket 1
// adding second peer to bucket 2
p, _ = rt.GenRandPeerID(2)
require.True(t, rt.UsefulPeer(p))
rt.TryAddPeer(p, true, false)

// adding replaceable peer to bucket 1
// bucket 1 size: 1 -> 2
p, _ = rt.GenRandPeerID(1)
require.True(t, rt.UsefulPeer(p))
rt.TryAddPeer(p, true, true)

// adding replaceable peer to bucket 1
// bucket 1 size: 2 -> 2
p, _ = rt.GenRandPeerID(1)
require.True(t, rt.UsefulPeer(p))
rt.TryAddPeer(p, true, true)

// adding non replaceable peer to bucket 1
// bucket 1 size: 2 -> 2
p, _ = rt.GenRandPeerID(1)
require.True(t, rt.UsefulPeer(p))
rt.TryAddPeer(p, true, false)

// adding non replaceable peer to bucket 1
// bucket 1 size: 2 -> 2
p, _ = rt.GenRandPeerID(1)
require.False(t, rt.UsefulPeer(p))
rt.TryAddPeer(p, true, false)

}

func TestEmptyBucketCollapse(t *testing.T) {
t.Parallel()
local := test.RandPeerIDFatal(t)
Expand Down

0 comments on commit 43db441

Please sign in to comment.