Unverified Commit 671b1a00 authored by Aarsh Shah's avatar Aarsh Shah Committed by GitHub

Cache the XOR DHT Id for a peerID (#67)

* cache the XOR DHT ID for a peerId
parent caabf645
......@@ -9,12 +9,15 @@ import (
"github.com/libp2p/go-libp2p-core/peer"
)
// PeerInfo holds all related information for a peer in the K-Bucket.
type PeerInfo struct {
// peerInfo holds all related information for a peer in the K-Bucket.
type peerInfo struct {
Id peer.ID
// lastSuccessfulOutboundQuery is the time instant when we last made a successful
// outbound query to this peer
lastSuccessfulOutboundQuery time.Time
// Id of the peer in the DHT XOR keyspace
dhtId ID
}
// bucket holds a list of peers.
......@@ -34,10 +37,10 @@ func newBucket() *bucket {
// returns all peers in the bucket
// it is safe for the caller to modify the returned objects as it is a defensive copy
func (b *bucket) peers() []PeerInfo {
var ps []PeerInfo
func (b *bucket) peers() []peerInfo {
var ps []peerInfo
for e := b.list.Front(); e != nil; e = e.Next() {
p := e.Value.(*PeerInfo)
p := e.Value.(*peerInfo)
ps = append(ps, *p)
}
return ps
......@@ -47,7 +50,7 @@ func (b *bucket) peers() []PeerInfo {
func (b *bucket) peerIds() []peer.ID {
ps := make([]peer.ID, 0, b.list.Len())
for e := b.list.Front(); e != nil; e = e.Next() {
p := e.Value.(*PeerInfo)
p := e.Value.(*peerInfo)
ps = append(ps, p.Id)
}
return ps
......@@ -55,10 +58,10 @@ func (b *bucket) peerIds() []peer.ID {
// returns the peer with the given Id if it exists
// returns nil if the peerId does not exist
func (b *bucket) getPeer(p peer.ID) *PeerInfo {
func (b *bucket) getPeer(p peer.ID) *peerInfo {
for e := b.list.Front(); e != nil; e = e.Next() {
if e.Value.(*PeerInfo).Id == p {
return e.Value.(*PeerInfo)
if e.Value.(*peerInfo).Id == p {
return e.Value.(*peerInfo)
}
}
return nil
......@@ -68,7 +71,7 @@ func (b *bucket) getPeer(p peer.ID) *PeerInfo {
// returns true if successful, false otherwise.
func (b *bucket) remove(id peer.ID) bool {
for e := b.list.Front(); e != nil; e = e.Next() {
if e.Value.(*PeerInfo).Id == id {
if e.Value.(*peerInfo).Id == id {
b.list.Remove(e)
return true
}
......@@ -79,13 +82,13 @@ func (b *bucket) remove(id peer.ID) bool {
func (b *bucket) moveToFront(id peer.ID) {
for e := b.list.Front(); e != nil; e = e.Next() {
if e.Value.(*PeerInfo).Id == id {
if e.Value.(*peerInfo).Id == id {
b.list.MoveToFront(e)
}
}
}
func (b *bucket) pushFront(p *PeerInfo) {
func (b *bucket) pushFront(p *peerInfo) {
b.list.PushFront(p)
}
......@@ -102,8 +105,8 @@ func (b *bucket) split(cpl int, target ID) *bucket {
newbuck.list = out
e := b.list.Front()
for e != nil {
peerID := ConvertPeerID(e.Value.(*PeerInfo).Id)
peerCPL := CommonPrefixLen(peerID, target)
pDhtId := e.Value.(*peerInfo).dhtId
peerCPL := CommonPrefixLen(pDhtId, target)
if peerCPL > cpl {
cur := e
out.PushBack(e.Value)
......
......@@ -28,17 +28,17 @@ func (pds *peerDistanceSorter) Less(a, b int) bool {
}
// Append the peer.ID to the sorter's slice. It may no longer be sorted.
func (pds *peerDistanceSorter) appendPeer(p peer.ID) {
func (pds *peerDistanceSorter) appendPeer(p peer.ID, pDhtId ID) {
pds.peers = append(pds.peers, peerDistance{
p: p,
distance: xor(pds.target, ConvertPeerID(p)),
distance: xor(pds.target, pDhtId),
})
}
// Append the peer.ID values in the list to the sorter's slice. It may no longer be sorted.
func (pds *peerDistanceSorter) appendPeersFromList(l *list.List) {
for e := l.Front(); e != nil; e = e.Next() {
pds.appendPeer(e.Value.(*PeerInfo).Id)
pds.appendPeer(e.Value.(*peerInfo).Id, e.Value.(*peerInfo).dhtId)
}
}
......@@ -53,7 +53,7 @@ func SortClosestPeers(peers []peer.ID, target ID) []peer.ID {
target: target,
}
for _, p := range peers {
sorter.appendPeer(p)
sorter.appendPeer(p, ConvertPeerID(p))
}
sorter.sort()
out := make([]peer.ID, 0, sorter.Len())
......
......@@ -129,7 +129,7 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) {
// We have enough space in the bucket (whether spawned or grouped).
if bucket.len() < rt.bucketsize {
bucket.pushFront(&PeerInfo{p, lastSuccessfulOutboundQuery})
bucket.pushFront(&peerInfo{p, lastSuccessfulOutboundQuery, ConvertPeerID(p)})
rt.PeerAdded(p)
return true, nil
}
......@@ -143,7 +143,7 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) {
// push the peer only if the bucket isn't overflowing after slitting
if bucket.len() < rt.bucketsize {
bucket.pushFront(&PeerInfo{p, lastSuccessfulOutboundQuery})
bucket.pushFront(&peerInfo{p, lastSuccessfulOutboundQuery, ConvertPeerID(p)})
rt.PeerAdded(p)
return true, nil
}
......@@ -156,7 +156,7 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) {
if float64(time.Since(pc.lastSuccessfulOutboundQuery)) > rt.maxLastSuccessfulOutboundThreshold {
// let's evict it and add the new peer
if bucket.remove(pc.Id) {
bucket.pushFront(&PeerInfo{p, lastSuccessfulOutboundQuery})
bucket.pushFront(&peerInfo{p, lastSuccessfulOutboundQuery, ConvertPeerID(p)})
rt.PeerAdded(p)
return true, nil
}
......@@ -334,7 +334,7 @@ func (rt *RoutingTable) Print() {
fmt.Printf("\tbucket: %d\n", i)
for e := b.list.Front(); e != nil; e = e.Next() {
p := e.Value.(*PeerInfo).Id
p := e.Value.(*peerInfo).Id
fmt.Printf("\t\t- %s %s\n", p.Pretty(), rt.metrics.LatencyEWMA(p).String())
}
}
......
......@@ -33,7 +33,7 @@ func TestBucket(t *testing.T) {
peers := make([]peer.ID, 100)
for i := 0; i < 100; i++ {
peers[i] = test.RandPeerIDFatal(t)
b.pushFront(&PeerInfo{peers[i], testTime1})
b.pushFront(&peerInfo{peers[i], testTime1, ConvertPeerID(peers[i])})
}
local := test.RandPeerIDFatal(t)
......@@ -46,6 +46,7 @@ func TestBucket(t *testing.T) {
p := b.getPeer(peers[i])
require.NotNil(t, p)
require.Equal(t, peers[i], p.Id)
require.Equal(t, ConvertPeerID(peers[i]), p.dhtId)
require.EqualValues(t, testTime1, p.lastSuccessfulOutboundQuery)
// mark as missing
......@@ -58,7 +59,7 @@ func TestBucket(t *testing.T) {
spl := b.split(0, ConvertPeerID(local))
llist := b.list
for e := llist.Front(); e != nil; e = e.Next() {
p := ConvertPeerID(e.Value.(*PeerInfo).Id)
p := ConvertPeerID(e.Value.(*peerInfo).Id)
cpl := CommonPrefixLen(p, localID)
if cpl > 0 {
t.Fatalf("split failed. found id with cpl > 0 in 0 bucket")
......@@ -67,7 +68,7 @@ func TestBucket(t *testing.T) {
rlist := spl.list
for e := rlist.Front(); e != nil; e = e.Next() {
p := ConvertPeerID(e.Value.(*PeerInfo).Id)
p := ConvertPeerID(e.Value.(*peerInfo).Id)
cpl := CommonPrefixLen(p, localID)
if cpl == 0 {
t.Fatalf("split failed. found id with cpl == 0 in non 0 bucket")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment