Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-kbucket
Commits
671b1a00
Unverified
Commit
671b1a00
authored
Apr 03, 2020
by
Aarsh Shah
Committed by
GitHub
Apr 03, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cache the XOR DHT Id for a peerID (#67)
* cache the XOR DHT ID for a peerId
parent
caabf645
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
29 additions
and
25 deletions
+29
-25
bucket.go
bucket.go
+17
-14
sorting.go
sorting.go
+4
-4
table.go
table.go
+4
-4
table_test.go
table_test.go
+4
-3
No files found.
bucket.go
View file @
671b1a00
...
...
@@ -9,12 +9,15 @@ import (
"github.com/libp2p/go-libp2p-core/peer"
)
//
P
eerInfo holds all related information for a peer in the K-Bucket.
type
P
eerInfo
struct
{
//
p
eerInfo holds all related information for a peer in the K-Bucket.
type
p
eerInfo
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
()
[]
P
eerInfo
{
var
ps
[]
P
eerInfo
func
(
b
*
bucket
)
peers
()
[]
p
eerInfo
{
var
ps
[]
p
eerInfo
for
e
:=
b
.
list
.
Front
();
e
!=
nil
;
e
=
e
.
Next
()
{
p
:=
e
.
Value
.
(
*
P
eerInfo
)
p
:=
e
.
Value
.
(
*
p
eerInfo
)
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
.
(
*
P
eerInfo
)
p
:=
e
.
Value
.
(
*
p
eerInfo
)
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
)
*
P
eerInfo
{
func
(
b
*
bucket
)
getPeer
(
p
peer
.
ID
)
*
p
eerInfo
{
for
e
:=
b
.
list
.
Front
();
e
!=
nil
;
e
=
e
.
Next
()
{
if
e
.
Value
.
(
*
P
eerInfo
)
.
Id
==
p
{
return
e
.
Value
.
(
*
P
eerInfo
)
if
e
.
Value
.
(
*
p
eerInfo
)
.
Id
==
p
{
return
e
.
Value
.
(
*
p
eerInfo
)
}
}
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
.
(
*
P
eerInfo
)
.
Id
==
id
{
if
e
.
Value
.
(
*
p
eerInfo
)
.
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
.
(
*
P
eerInfo
)
.
Id
==
id
{
if
e
.
Value
.
(
*
p
eerInfo
)
.
Id
==
id
{
b
.
list
.
MoveToFront
(
e
)
}
}
}
func
(
b
*
bucket
)
pushFront
(
p
*
P
eerInfo
)
{
func
(
b
*
bucket
)
pushFront
(
p
*
p
eerInfo
)
{
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
{
p
eerID
:=
ConvertPeerID
(
e
.
Value
.
(
*
P
eerInfo
)
.
Id
)
peerCPL
:=
CommonPrefixLen
(
p
eerID
,
target
)
p
DhtId
:=
e
.
Value
.
(
*
p
eerInfo
)
.
dht
Id
peerCPL
:=
CommonPrefixLen
(
p
DhtId
,
target
)
if
peerCPL
>
cpl
{
cur
:=
e
out
.
PushBack
(
e
.
Value
)
...
...
sorting.go
View file @
671b1a00
...
...
@@ -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
.
(
*
P
eerInfo
)
.
Id
)
pds
.
appendPeer
(
e
.
Value
.
(
*
peerInfo
)
.
Id
,
e
.
Value
.
(
*
p
eerInfo
)
.
dht
Id
)
}
}
...
...
@@ -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
())
...
...
table.go
View file @
671b1a00
...
...
@@ -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
(
&
P
eerInfo
{
p
,
lastSuccessfulOutboundQuery
})
bucket
.
pushFront
(
&
p
eerInfo
{
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
(
&
P
eerInfo
{
p
,
lastSuccessfulOutboundQuery
})
bucket
.
pushFront
(
&
p
eerInfo
{
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
(
&
P
eerInfo
{
p
,
lastSuccessfulOutboundQuery
})
bucket
.
pushFront
(
&
p
eerInfo
{
p
,
lastSuccessfulOutboundQuery
,
ConvertPeerID
(
p
)
})
rt
.
PeerAdded
(
p
)
return
true
,
nil
}
...
...
@@ -334,7 +334,7 @@ func (rt *RoutingTable) Print() {
fmt
.
Printf
(
"
\t
bucket: %d
\n
"
,
i
)
for
e
:=
b
.
list
.
Front
();
e
!=
nil
;
e
=
e
.
Next
()
{
p
:=
e
.
Value
.
(
*
P
eerInfo
)
.
Id
p
:=
e
.
Value
.
(
*
p
eerInfo
)
.
Id
fmt
.
Printf
(
"
\t\t
- %s %s
\n
"
,
p
.
Pretty
(),
rt
.
metrics
.
LatencyEWMA
(
p
)
.
String
())
}
}
...
...
table_test.go
View file @
671b1a00
...
...
@@ -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
(
&
P
eerInfo
{
peers
[
i
],
testTime1
})
b
.
pushFront
(
&
p
eerInfo
{
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
.
(
*
P
eerInfo
)
.
Id
)
p
:=
ConvertPeerID
(
e
.
Value
.
(
*
p
eerInfo
)
.
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
.
(
*
P
eerInfo
)
.
Id
)
p
:=
ConvertPeerID
(
e
.
Value
.
(
*
p
eerInfo
)
.
Id
)
cpl
:=
CommonPrefixLen
(
p
,
localID
)
if
cpl
==
0
{
t
.
Fatalf
(
"split failed. found id with cpl == 0 in non 0 bucket"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment