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
7dd9040c
Commit
7dd9040c
authored
Feb 27, 2020
by
Aarsh Shah
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bucket size fncs
parent
552cb4a1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
0 deletions
+64
-0
table.go
table.go
+28
-0
table_test.go
table_test.go
+36
-0
No files found.
table.go
View file @
7dd9040c
...
...
@@ -196,6 +196,34 @@ func (rt *RoutingTable) peersToValidate() []PeerInfo {
return
rt
.
peersForValidationFnc
(
peers
)
}
// NPeersForCPL returns the number of peers we have for a given Cpl
func
(
rt
*
RoutingTable
)
NPeersForCpl
(
cpl
uint
)
int
{
rt
.
tabLock
.
RLock
()
defer
rt
.
tabLock
.
RUnlock
()
// it's in the last bucket
if
int
(
cpl
)
>=
len
(
rt
.
buckets
)
-
1
{
count
:=
0
b
:=
rt
.
buckets
[
len
(
rt
.
buckets
)
-
1
]
for
_
,
p
:=
range
b
.
peerIds
()
{
if
CommonPrefixLen
(
rt
.
local
,
ConvertPeerID
(
p
))
==
int
(
cpl
)
{
count
++
}
}
return
count
}
else
{
return
rt
.
buckets
[
cpl
]
.
len
()
}
}
// IsBucketFull returns true if the Logical bucket for a given Cpl is full
func
(
rt
*
RoutingTable
)
IsBucketFull
(
cpl
uint
)
bool
{
rt
.
tabLock
.
RLock
()
defer
rt
.
tabLock
.
RUnlock
()
return
rt
.
NPeersForCpl
(
cpl
)
==
rt
.
bucketsize
}
// GetTrackedCplsForRefresh returns the Cpl's we are tracking for refresh.
// Caller is free to modify the returned slice as it is a defensive copy.
func
(
rt
*
RoutingTable
)
GetTrackedCplsForRefresh
()
[]
CplRefresh
{
...
...
table_test.go
View file @
7dd9040c
...
...
@@ -90,6 +90,42 @@ func TestGenRandPeerID(t *testing.T) {
}
}
func
TestNPeersForCpl
(
t
*
testing
.
T
)
{
t
.
Parallel
()
local
:=
test
.
RandPeerIDFatal
(
t
)
m
:=
pstore
.
NewMetrics
()
rt
,
err
:=
NewRoutingTable
(
2
,
ConvertPeerID
(
local
),
time
.
Hour
,
m
,
PeerValidationFnc
(
PeerAlwaysValidFnc
))
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
0
,
rt
.
NPeersForCpl
(
0
))
require
.
Equal
(
t
,
0
,
rt
.
NPeersForCpl
(
1
))
// one peer with cpl 1
p
,
_
:=
rt
.
GenRandPeerID
(
1
)
rt
.
HandlePeerAlive
(
p
)
require
.
Equal
(
t
,
0
,
rt
.
NPeersForCpl
(
0
))
require
.
Equal
(
t
,
1
,
rt
.
NPeersForCpl
(
1
))
require
.
Equal
(
t
,
0
,
rt
.
NPeersForCpl
(
2
))
// one peer with cpl 0
p
,
_
=
rt
.
GenRandPeerID
(
0
)
rt
.
HandlePeerAlive
(
p
)
require
.
Equal
(
t
,
1
,
rt
.
NPeersForCpl
(
0
))
require
.
Equal
(
t
,
1
,
rt
.
NPeersForCpl
(
1
))
require
.
Equal
(
t
,
0
,
rt
.
NPeersForCpl
(
2
))
// split the bucket with a peer with cpl 1
p
,
_
=
rt
.
GenRandPeerID
(
1
)
rt
.
HandlePeerAlive
(
p
)
require
.
Equal
(
t
,
1
,
rt
.
NPeersForCpl
(
0
))
require
.
Equal
(
t
,
2
,
rt
.
NPeersForCpl
(
1
))
require
.
Equal
(
t
,
0
,
rt
.
NPeersForCpl
(
2
))
p
,
_
=
rt
.
GenRandPeerID
(
0
)
rt
.
HandlePeerAlive
(
p
)
require
.
Equal
(
t
,
2
,
rt
.
NPeersForCpl
(
0
))
}
func
TestRefreshAndGetTrackedCpls
(
t
*
testing
.
T
)
{
t
.
Parallel
()
local
:=
test
.
RandPeerIDFatal
(
t
)
...
...
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