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
6e226d95
Commit
6e226d95
authored
Dec 16, 2019
by
Aarsh Shah
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
changes as per review
parent
a58d5968
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
15 deletions
+19
-15
table.go
table.go
+13
-9
table_test.go
table_test.go
+6
-6
No files found.
table.go
View file @
6e226d95
...
...
@@ -21,10 +21,12 @@ var log = logging.Logger("table")
var
ErrPeerRejectedHighLatency
=
errors
.
New
(
"peer rejected; latency too high"
)
var
ErrPeerRejectedNoCapacity
=
errors
.
New
(
"peer rejected; insufficient capacity"
)
//
M
axCplForRefresh is the maximum cpl we support for refresh.
// This limit exists because we can only generate '
M
axCplForRefresh' bit prefixes for now.
var
M
axCplForRefresh
uint
=
15
//
m
axCplForRefresh is the maximum cpl we support for refresh.
// This limit exists because we can only generate '
m
axCplForRefresh' bit prefixes for now.
const
m
axCplForRefresh
uint
=
15
// CplRefresh contains a CPL(common prefix length) with the host & the last time
// we refreshed that cpl/searched for an ID which has that cpl with the host.
type
CplRefresh
struct
{
Cpl
uint
LastRefreshAt
time
.
Time
...
...
@@ -74,14 +76,16 @@ func NewRoutingTable(bucketsize int, localID ID, latency time.Duration, m peerst
// 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
{
func
(
rt
*
RoutingTable
)
GetTrackedCplsForRefresh
()
[]
CplRefresh
{
rt
.
cplRefreshLk
.
RLock
()
defer
rt
.
cplRefreshLk
.
RUnlock
()
var
cpls
[]
*
CplRefresh
cpls
:=
make
(
[]
CplRefresh
,
len
(
rt
.
cplRefreshedAt
))
i
:=
0
for
c
,
t
:=
range
rt
.
cplRefreshedAt
{
cpls
=
append
(
cpls
,
&
CplRefresh
{
c
,
t
})
cpls
[
i
]
=
CplRefresh
{
c
,
t
}
i
++
}
return
cpls
...
...
@@ -89,8 +93,8 @@ func (rt *RoutingTable) GetTrackedCplsForRefresh() []*CplRefresh {
// GenRandPeerID generates a random peerID for a given Cpl
func
(
rt
*
RoutingTable
)
GenRandPeerID
(
targetCpl
uint
)
(
peer
.
ID
,
error
)
{
if
targetCpl
>
M
axCplForRefresh
{
return
""
,
fmt
.
Errorf
(
"cannot generate peer ID for Cpl greater than %d"
,
M
axCplForRefresh
)
if
targetCpl
>
m
axCplForRefresh
{
return
""
,
fmt
.
Errorf
(
"cannot generate peer ID for Cpl greater than %d"
,
m
axCplForRefresh
)
}
localPrefix
:=
binary
.
BigEndian
.
Uint16
(
rt
.
local
)
...
...
@@ -116,7 +120,7 @@ func (rt *RoutingTable) GenRandPeerID(targetCpl uint) (peer.ID, error) {
// ResetCplRefreshedAtForID resets the refresh time for the Cpl of the given ID.
func
(
rt
*
RoutingTable
)
ResetCplRefreshedAtForID
(
id
ID
,
newTime
time
.
Time
)
{
cpl
:=
CommonPrefixLen
(
id
,
rt
.
local
)
if
uint
(
cpl
)
>
M
axCplForRefresh
{
if
uint
(
cpl
)
>
m
axCplForRefresh
{
return
}
...
...
table_test.go
View file @
6e226d95
...
...
@@ -58,13 +58,13 @@ func TestGenRandPeerID(t *testing.T) {
m
:=
pstore
.
NewMetrics
()
rt
:=
NewRoutingTable
(
1
,
ConvertPeerID
(
local
),
time
.
Hour
,
m
)
// generate above
M
axCplForRefresh fails
p
,
err
:=
rt
.
GenRandPeerID
(
M
axCplForRefresh
+
1
)
// generate above
m
axCplForRefresh fails
p
,
err
:=
rt
.
GenRandPeerID
(
m
axCplForRefresh
+
1
)
require
.
Error
(
t
,
err
)
require
.
Empty
(
t
,
p
)
// test generate rand peer ID
for
cpl
:=
uint
(
0
);
cpl
<=
M
axCplForRefresh
;
cpl
++
{
for
cpl
:=
uint
(
0
);
cpl
<=
m
axCplForRefresh
;
cpl
++
{
peerID
,
err
:=
rt
.
GenRandPeerID
(
cpl
)
require
.
NoError
(
t
,
err
)
...
...
@@ -80,7 +80,7 @@ func TestRefreshAndGetTrackedCpls(t *testing.T) {
rt
:=
NewRoutingTable
(
1
,
ConvertPeerID
(
local
),
time
.
Hour
,
m
)
// add cpl's for tracking
for
cpl
:=
uint
(
0
);
cpl
<
M
axCplForRefresh
;
cpl
++
{
for
cpl
:=
uint
(
0
);
cpl
<
m
axCplForRefresh
;
cpl
++
{
peerID
,
err
:=
rt
.
GenRandPeerID
(
cpl
)
require
.
NoError
(
t
,
err
)
rt
.
ResetCplRefreshedAtForID
(
ConvertPeerID
(
peerID
),
time
.
Now
())
...
...
@@ -88,13 +88,13 @@ func TestRefreshAndGetTrackedCpls(t *testing.T) {
// fetch cpl's
trackedCpls
:=
rt
.
GetTrackedCplsForRefresh
()
require
.
Len
(
t
,
trackedCpls
,
int
(
M
axCplForRefresh
))
require
.
Len
(
t
,
trackedCpls
,
int
(
m
axCplForRefresh
))
actualCpls
:=
make
(
map
[
uint
]
struct
{})
for
i
:=
0
;
i
<
len
(
trackedCpls
);
i
++
{
actualCpls
[
trackedCpls
[
i
]
.
Cpl
]
=
struct
{}{}
}
for
i
:=
uint
(
0
);
i
<
M
axCplForRefresh
;
i
++
{
for
i
:=
uint
(
0
);
i
<
m
axCplForRefresh
;
i
++
{
_
,
ok
:=
actualCpls
[
i
]
require
.
True
(
t
,
ok
,
"tracked cpl's should have cpl %d"
,
i
)
}
...
...
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