Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mybee
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
mybee
Commits
87491926
Unverified
Commit
87491926
authored
Mar 31, 2021
by
Esad Akar
Committed by
GitHub
Mar 31, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
kademlia: EachNeighbor funcs (#1510)
parent
3462ee91
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
114 additions
and
2 deletions
+114
-2
kademlia.go
pkg/kademlia/kademlia.go
+31
-2
kademlia_test.go
pkg/kademlia/kademlia_test.go
+83
-0
No files found.
pkg/kademlia/kademlia.go
View file @
87491926
...
...
@@ -888,12 +888,41 @@ func (k *Kad) ClosestPeer(addr swarm.Address, skipPeers ...swarm.Address) (swarm
return
closest
,
nil
}
// EachPeer iterates from closest bin to farthest
// IsWithinDepth returns if an address is within the neighborhood depth of a node.
func
(
k
*
Kad
)
IsWithinDepth
(
adr
swarm
.
Address
)
bool
{
return
swarm
.
Proximity
(
k
.
base
.
Bytes
(),
adr
.
Bytes
())
>=
k
.
NeighborhoodDepth
()
}
// // EachNeighbor iterates from closest bin to farthest of the neighborhood peers.
func
(
k
*
Kad
)
EachNeighbor
(
f
topology
.
EachPeerFunc
)
error
{
depth
:=
k
.
NeighborhoodDepth
()
fn
:=
func
(
a
swarm
.
Address
,
po
uint8
)
(
bool
,
bool
,
error
)
{
if
po
<
depth
{
return
true
,
false
,
nil
}
return
f
(
a
,
po
)
}
return
k
.
connectedPeers
.
EachBin
(
fn
)
}
// EachNeighborRev iterates from farthest bin to closest of the neighborhood peers.
func
(
k
*
Kad
)
EachNeighborRev
(
f
topology
.
EachPeerFunc
)
error
{
depth
:=
k
.
NeighborhoodDepth
()
fn
:=
func
(
a
swarm
.
Address
,
po
uint8
)
(
bool
,
bool
,
error
)
{
if
po
<
depth
{
return
false
,
true
,
nil
}
return
f
(
a
,
po
)
}
return
k
.
connectedPeers
.
EachBinRev
(
fn
)
}
// EachPeer iterates from closest bin to farthest.
func
(
k
*
Kad
)
EachPeer
(
f
topology
.
EachPeerFunc
)
error
{
return
k
.
connectedPeers
.
EachBin
(
f
)
}
// EachPeerRev iterates from farthest bin to closest
// EachPeerRev iterates from farthest bin to closest
.
func
(
k
*
Kad
)
EachPeerRev
(
f
topology
.
EachPeerFunc
)
error
{
return
k
.
connectedPeers
.
EachBinRev
(
f
)
}
...
...
pkg/kademlia/kademlia_test.go
View file @
87491926
...
...
@@ -146,6 +146,89 @@ func TestNeighborhoodDepth(t *testing.T) {
kDepth
(
t
,
kad
,
1
)
}
func
TestIsWithinDepth
(
t
*
testing
.
T
)
{
var
(
conns
int32
// how many connect calls were made to the p2p mock
base
,
kad
,
ab
,
_
,
signer
=
newTestKademlia
(
&
conns
,
nil
,
kademlia
.
Options
{})
peers
[]
swarm
.
Address
)
if
err
:=
kad
.
Start
(
context
.
Background
());
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
kad
.
Close
()
for
i
:=
0
;
i
<
15
;
i
++
{
addr
:=
test
.
RandomAddressAt
(
base
,
i
)
peers
=
append
(
peers
,
addr
)
}
add
(
t
,
signer
,
kad
,
ab
,
peers
,
0
,
15
)
waitCounter
(
t
,
&
conns
,
15
)
if
kad
.
IsWithinDepth
(
peers
[
kad
.
NeighborhoodDepth
()
-
1
])
{
t
.
Fatalf
(
"peer should NOT be in neignborhood"
)
}
if
!
kad
.
IsWithinDepth
(
peers
[
kad
.
NeighborhoodDepth
()])
{
t
.
Fatalf
(
"peer should be in neignborhood"
)
}
}
func
TestEachNeighbor
(
t
*
testing
.
T
)
{
var
(
conns
int32
// how many connect calls were made to the p2p mock
base
,
kad
,
ab
,
_
,
signer
=
newTestKademlia
(
&
conns
,
nil
,
kademlia
.
Options
{})
peers
[]
swarm
.
Address
)
if
err
:=
kad
.
Start
(
context
.
Background
());
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
kad
.
Close
()
for
i
:=
0
;
i
<
15
;
i
++
{
addr
:=
test
.
RandomAddressAt
(
base
,
i
)
peers
=
append
(
peers
,
addr
)
}
add
(
t
,
signer
,
kad
,
ab
,
peers
,
0
,
15
)
waitCounter
(
t
,
&
conns
,
15
)
var
depth
uint8
=
15
err
:=
kad
.
EachNeighbor
(
func
(
adr
swarm
.
Address
,
po
uint8
)
(
stop
,
jumpToNext
bool
,
err
error
)
{
if
po
<
depth
{
depth
=
po
}
return
false
,
false
,
nil
})
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
depth
<
kad
.
NeighborhoodDepth
()
{
t
.
Fatalf
(
"incorrect depth argument pass to iterator function: expected >= %d (neighbourhood depth), got %d"
,
kad
.
NeighborhoodDepth
(),
depth
)
}
depth
=
15
err
=
kad
.
EachNeighborRev
(
func
(
adr
swarm
.
Address
,
po
uint8
)
(
stop
,
jumpToNext
bool
,
err
error
)
{
if
po
<
depth
{
depth
=
po
}
return
false
,
false
,
nil
})
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
depth
<
kad
.
NeighborhoodDepth
()
{
t
.
Fatalf
(
"incorrect depth argument pass to iterator function: expected >= %d (neighbourhood depth), got %d"
,
kad
.
NeighborhoodDepth
(),
depth
)
}
}
// TestManage explicitly tests that new connections are made according to
// the addition or subtraction of peers to the knownPeers and connectedPeers
// data structures. It tests that kademlia will try to initiate (emphesis on _initiate_,
...
...
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