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-xor
Commits
03dc1869
Commit
03dc1869
authored
Mar 27, 2020
by
Petar Maymounkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix bug in routing table health calculation.
parent
64ea8239
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
13 deletions
+62
-13
kademlia/health.go
kademlia/health.go
+12
-3
kademlia/health_test.go
kademlia/health_test.go
+49
-0
trie/intersect_test.go
trie/intersect_test.go
+1
-10
No files found.
kademlia/health.go
View file @
03dc1869
...
...
@@ -81,6 +81,14 @@ func AllTablesHealth(tables []*Table) (report []*TableHealthReport) {
return
}
func
TableHealthFromSets
(
node
key
.
Key
,
nodeContacts
[]
key
.
Key
,
knownNodes
[]
key
.
Key
)
*
TableHealthReport
{
knownNodesTrie
:=
trie
.
New
()
for
_
,
k
:=
range
knownNodes
{
knownNodesTrie
.
Add
(
k
)
}
return
TableHealth
(
node
,
nodeContacts
,
knownNodesTrie
)
}
// TableHealth computes the health report for a node,
// given its routing contacts and a list of all known nodes in the network currently.
func
TableHealth
(
node
key
.
Key
,
nodeContacts
[]
key
.
Key
,
knownNodes
*
trie
.
Trie
)
*
TableHealthReport
{
...
...
@@ -114,6 +122,7 @@ func walkBucketHealth(depth int, node key.Key, nodeTable, knownNodes *trie.Trie)
}
else
{
dir
:=
node
.
BitAt
(
depth
)
switch
{
//
case
knownNodes
==
nil
||
knownNodes
.
IsEmptyLeaf
()
:
r
:=
walkBucketHealth
(
depth
+
1
,
node
,
nodeTable
.
Branch
[
dir
],
nil
)
return
append
(
r
,
...
...
@@ -135,12 +144,12 @@ func walkBucketHealth(depth int, node key.Key, nodeTable, knownNodes *trie.Trie)
})
}
else
{
r
:=
walkBucketHealth
(
depth
+
1
,
node
,
nodeTable
.
Branch
[
dir
],
nil
)
return
append
(
r
,
bucketReportFromTries
(
depth
,
nodeTable
.
Branch
[
1
-
dir
],
knownNodes
))
return
append
(
r
,
bucketReportFromTries
(
depth
+
1
,
nodeTable
.
Branch
[
1
-
dir
],
knownNodes
))
}
case
!
knownNodes
.
IsLeaf
()
:
r
:=
walkBucketHealth
(
depth
+
1
,
node
,
nodeTable
.
Branch
[
dir
],
knownNodes
.
Branch
[
dir
])
return
append
(
r
,
bucketReportFromTries
(
depth
,
nodeTable
.
Branch
[
1
-
dir
],
knownNodes
.
Branch
[
1
-
dir
]))
bucketReportFromTries
(
depth
+
1
,
nodeTable
.
Branch
[
1
-
dir
],
knownNodes
.
Branch
[
1
-
dir
]))
default
:
panic
(
"unreachable"
)
}
...
...
@@ -148,7 +157,7 @@ func walkBucketHealth(depth int, node key.Key, nodeTable, knownNodes *trie.Trie)
}
func
bucketReportFromTries
(
depth
int
,
actualBucket
,
maxBucket
*
trie
.
Trie
)
*
BucketHealthReport
{
actualKnown
:=
trie
.
Intersect
(
actualBucket
,
maxBucket
)
actualKnown
:=
trie
.
Intersect
AtDepth
(
depth
,
actualBucket
,
maxBucket
)
actualKnownSize
:=
actualKnown
.
Size
()
return
&
BucketHealthReport
{
Depth
:
depth
,
...
...
kademlia/health_test.go
0 → 100644
View file @
03dc1869
package
kademlia
import
(
"math/rand"
"testing"
"github.com/libp2p/go-libp2p-xor/key"
)
func
TestTableHealthFromSets
(
t
*
testing
.
T
)
{
for
i
:=
0
;
i
<
10
;
i
++
{
s
:=
randomTestTableHealthSubsetSamples
(
10
,
100
)
report
:=
TableHealthFromSets
(
s
.
Node
,
s
.
Contacts
,
s
.
Known
)
for
_
,
b
:=
range
report
.
Bucket
{
if
b
.
ActualUnknownContacts
!=
0
{
t
.
Errorf
(
"expecting no actual unknown contacts, got %d"
,
b
.
ActualUnknownContacts
)
}
}
}
}
func
randomTestTableHealthSubsetSamples
(
contactSize
,
knownSize
int
)
*
testTableHealthSample
{
known
:=
make
([]
key
.
Key
,
knownSize
)
for
i
:=
range
known
{
known
[
i
]
=
randomKey
(
16
)
}
contacts
:=
make
([]
key
.
Key
,
contactSize
)
perm
:=
rand
.
Perm
(
knownSize
)
for
i
:=
0
;
i
<
contactSize
;
i
++
{
contacts
[
i
]
=
known
[
perm
[
i
]]
}
return
&
testTableHealthSample
{
Node
:
randomKey
(
16
),
Contacts
:
contacts
,
Known
:
known
,
}
}
func
randomKey
(
size
int
)
key
.
Key
{
k
:=
make
([]
byte
,
size
)
rand
.
Read
(
k
)
return
key
.
Key
(
k
)
}
type
testTableHealthSample
struct
{
Node
key
.
Key
Contacts
[]
key
.
Key
Known
[]
key
.
Key
}
trie/intersect_test.go
View file @
03dc1869
...
...
@@ -188,13 +188,4 @@ func testIntersectTrieFromJSON(srcJSON string) *testIntersectTrie {
return
s
}
var
testIntersectJSONTries
=
[]
string
{
// `
// {
// "LeftTrie": [
// ],
// "RightTrie": [
// ]
// }
// `,
}
var
testIntersectJSONTries
=
[]
string
{}
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