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
51478358
Commit
51478358
authored
Apr 03, 2020
by
Aarsh Shah
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
get peer infos
parent
671b1a00
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
21 deletions
+66
-21
bucket.go
bucket.go
+13
-13
sorting.go
sorting.go
+1
-1
table.go
table.go
+18
-4
table_test.go
table_test.go
+34
-3
No files found.
bucket.go
View file @
51478358
...
...
@@ -9,8 +9,8 @@ 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
...
...
@@ -37,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
...
...
@@ -50,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
...
...
@@ -58,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
...
...
@@ -71,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
}
...
...
@@ -82,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
)
}
...
...
@@ -105,7 +105,7 @@ func (b *bucket) split(cpl int, target ID) *bucket {
newbuck
.
list
=
out
e
:=
b
.
list
.
Front
()
for
e
!=
nil
{
pDhtId
:=
e
.
Value
.
(
*
p
eerInfo
)
.
dhtId
pDhtId
:=
e
.
Value
.
(
*
P
eerInfo
)
.
dhtId
peerCPL
:=
CommonPrefixLen
(
pDhtId
,
target
)
if
peerCPL
>
cpl
{
cur
:=
e
...
...
sorting.go
View file @
51478358
...
...
@@ -38,7 +38,7 @@ func (pds *peerDistanceSorter) appendPeer(p peer.ID, pDhtId ID) {
// 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
,
e
.
Value
.
(
*
p
eerInfo
)
.
dhtId
)
pds
.
appendPeer
(
e
.
Value
.
(
*
P
eerInfo
)
.
Id
,
e
.
Value
.
(
*
P
eerInfo
)
.
dhtId
)
}
}
...
...
table.go
View file @
51478358
...
...
@@ -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
,
ConvertPeerID
(
p
)})
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
,
ConvertPeerID
(
p
)})
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
,
ConvertPeerID
(
p
)})
bucket
.
pushFront
(
&
P
eerInfo
{
p
,
lastSuccessfulOutboundQuery
,
ConvertPeerID
(
p
)})
rt
.
PeerAdded
(
p
)
return
true
,
nil
}
...
...
@@ -166,6 +166,20 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) {
return
false
,
ErrPeerRejectedNoCapacity
}
// GetPeerInfos returns the peer information that we've stored in the buckets
func
(
rt
*
RoutingTable
)
GetPeerInfos
()
[]
PeerInfo
{
rt
.
tabLock
.
RLock
()
defer
rt
.
tabLock
.
RUnlock
()
var
pis
[]
PeerInfo
for
_
,
b
:=
range
rt
.
buckets
{
for
_
,
p
:=
range
b
.
peers
()
{
pis
=
append
(
pis
,
p
)
}
}
return
pis
}
// UpdateLastSuccessfulOutboundQuery updates the lastSuccessfulOutboundQuery time of the peer
// Returns true if the update was successful, false otherwise.
func
(
rt
*
RoutingTable
)
UpdateLastSuccessfulOutboundQuery
(
p
peer
.
ID
,
t
time
.
Time
)
bool
{
...
...
@@ -334,7 +348,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 @
51478358
...
...
@@ -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
,
ConvertPeerID
(
peers
[
i
])})
b
.
pushFront
(
&
P
eerInfo
{
peers
[
i
],
testTime1
,
ConvertPeerID
(
peers
[
i
])})
}
local
:=
test
.
RandPeerIDFatal
(
t
)
...
...
@@ -59,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"
)
...
...
@@ -68,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"
)
...
...
@@ -480,6 +480,37 @@ func TestTableMultithreaded(t *testing.T) {
<-
done
}
func
TestGetPeerInfos
(
t
*
testing
.
T
)
{
local
:=
test
.
RandPeerIDFatal
(
t
)
m
:=
pstore
.
NewMetrics
()
rt
,
err
:=
NewRoutingTable
(
10
,
ConvertPeerID
(
local
),
time
.
Hour
,
m
,
NoOpThreshold
)
require
.
NoError
(
t
,
err
)
require
.
Empty
(
t
,
rt
.
GetPeerInfos
())
p1
:=
test
.
RandPeerIDFatal
(
t
)
p2
:=
test
.
RandPeerIDFatal
(
t
)
b
,
err
:=
rt
.
TryAddPeer
(
p1
,
false
)
require
.
True
(
t
,
b
)
require
.
NoError
(
t
,
err
)
b
,
err
=
rt
.
TryAddPeer
(
p2
,
true
)
require
.
True
(
t
,
b
)
require
.
NoError
(
t
,
err
)
ps
:=
rt
.
GetPeerInfos
()
require
.
Len
(
t
,
ps
,
2
)
ms
:=
make
(
map
[
peer
.
ID
]
PeerInfo
)
for
_
,
p
:=
range
ps
{
ms
[
p
.
Id
]
=
p
}
require
.
Equal
(
t
,
p1
,
ms
[
p1
]
.
Id
)
require
.
True
(
t
,
ms
[
p1
]
.
lastSuccessfulOutboundQuery
.
IsZero
())
require
.
Equal
(
t
,
p2
,
ms
[
p2
]
.
Id
)
require
.
False
(
t
,
ms
[
p2
]
.
lastSuccessfulOutboundQuery
.
IsZero
())
}
func
BenchmarkAddPeer
(
b
*
testing
.
B
)
{
b
.
StopTimer
()
local
:=
ConvertKey
(
"localKey"
)
...
...
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