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
955487f2
Commit
955487f2
authored
Sep 17, 2014
by
Juan Batiz-Benet
Committed by
Brian Tiger Chow
Sep 22, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
kbucket use new keyspace
parent
6aca2ab2
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
13 additions
and
51 deletions
+13
-51
bucket.go
bucket.go
+1
-1
table.go
table.go
+2
-2
table_test.go
table_test.go
+2
-2
util.go
util.go
+8
-46
No files found.
bucket.go
View file @
955487f2
...
...
@@ -69,7 +69,7 @@ func (b *Bucket) Split(cpl int, target ID) *Bucket {
e
:=
b
.
list
.
Front
()
for
e
!=
nil
{
peerID
:=
ConvertPeerID
(
e
.
Value
.
(
*
peer
.
Peer
)
.
ID
)
peerCPL
:=
p
refLen
(
peerID
,
target
)
peerCPL
:=
commonP
ref
ix
Len
(
peerID
,
target
)
if
peerCPL
>
cpl
{
cur
:=
e
out
.
PushBack
(
e
.
Value
)
...
...
table.go
View file @
955487f2
...
...
@@ -44,7 +44,7 @@ func (rt *RoutingTable) Update(p *peer.Peer) *peer.Peer {
rt
.
tabLock
.
Lock
()
defer
rt
.
tabLock
.
Unlock
()
peerID
:=
ConvertPeerID
(
p
.
ID
)
cpl
:=
xor
(
peerID
,
rt
.
local
)
.
commonPrefixLen
()
cpl
:=
commonPrefixLen
(
peerID
,
rt
.
local
)
bucketID
:=
cpl
if
bucketID
>=
len
(
rt
.
Buckets
)
{
...
...
@@ -145,7 +145,7 @@ func (rt *RoutingTable) NearestPeer(id ID) *peer.Peer {
func
(
rt
*
RoutingTable
)
NearestPeers
(
id
ID
,
count
int
)
[]
*
peer
.
Peer
{
rt
.
tabLock
.
RLock
()
defer
rt
.
tabLock
.
RUnlock
()
cpl
:=
p
refLen
(
id
,
rt
.
local
)
cpl
:=
commonP
ref
ix
Len
(
id
,
rt
.
local
)
// Get bucket at cpl index or last bucket
var
bucket
*
Bucket
...
...
table_test.go
View file @
955487f2
...
...
@@ -48,7 +48,7 @@ func TestBucket(t *testing.T) {
llist
:=
b
.
list
for
e
:=
llist
.
Front
();
e
!=
nil
;
e
=
e
.
Next
()
{
p
:=
ConvertPeerID
(
e
.
Value
.
(
*
peer
.
Peer
)
.
ID
)
cpl
:=
xor
(
p
,
localID
)
.
commonPrefixLen
()
cpl
:=
commonPrefixLen
(
p
,
localID
)
if
cpl
>
0
{
t
.
Fatalf
(
"Split failed. found id with cpl > 0 in 0 bucket"
)
}
...
...
@@ -57,7 +57,7 @@ func TestBucket(t *testing.T) {
rlist
:=
spl
.
list
for
e
:=
rlist
.
Front
();
e
!=
nil
;
e
=
e
.
Next
()
{
p
:=
ConvertPeerID
(
e
.
Value
.
(
*
peer
.
Peer
)
.
ID
)
cpl
:=
xor
(
p
,
localID
)
.
commonPrefixLen
()
cpl
:=
commonPrefixLen
(
p
,
localID
)
if
cpl
==
0
{
t
.
Fatalf
(
"Split failed. found id with cpl == 0 in non 0 bucket"
)
}
...
...
util.go
View file @
955487f2
...
...
@@ -6,6 +6,7 @@ import (
"errors"
peer
"github.com/jbenet/go-ipfs/peer"
ks
"github.com/jbenet/go-ipfs/routing/keyspace"
u
"github.com/jbenet/go-ipfs/util"
)
...
...
@@ -13,8 +14,7 @@ import (
// behaviour
var
ErrLookupFailure
=
errors
.
New
(
"failed to find any peer in table"
)
// ID for IpfsDHT should be a byte slice, to allow for simpler operations
// (xor). DHT ids are based on the peer.IDs.
// ID for IpfsDHT is in the XORKeySpace
//
// The type dht.ID signifies that its contents have been hashed from either a
// peer.ID or a util.Key. This unifies the keyspace
...
...
@@ -25,55 +25,17 @@ func (id ID) equal(other ID) bool {
}
func
(
id
ID
)
less
(
other
ID
)
bool
{
a
,
b
:=
equalizeSizes
(
id
,
other
)
for
i
:=
0
;
i
<
len
(
a
);
i
++
{
if
a
[
i
]
!=
b
[
i
]
{
return
a
[
i
]
<
b
[
i
]
}
}
return
len
(
a
)
<
len
(
b
)
}
func
(
id
ID
)
commonPrefixLen
()
int
{
for
i
:=
0
;
i
<
len
(
id
);
i
++
{
for
j
:=
0
;
j
<
8
;
j
++
{
if
(
id
[
i
]
>>
uint8
(
7
-
j
))
&
0x1
!=
0
{
return
i
*
8
+
j
}
}
}
return
len
(
id
)
*
8
-
1
}
func
prefLen
(
a
,
b
ID
)
int
{
return
xor
(
a
,
b
)
.
commonPrefixLen
()
a
:=
ks
.
Key
{
Space
:
ks
.
XORKeySpace
,
Adjusted
:
id
}
b
:=
ks
.
Key
{
Space
:
ks
.
XORKeySpace
,
Adjusted
:
other
}
return
a
.
Less
(
b
)
}
func
xor
(
a
,
b
ID
)
ID
{
a
,
b
=
equalizeSizes
(
a
,
b
)
c
:=
make
(
ID
,
len
(
a
))
for
i
:=
0
;
i
<
len
(
a
);
i
++
{
c
[
i
]
=
a
[
i
]
^
b
[
i
]
}
return
c
return
ID
(
ks
.
XOR
(
a
,
b
))
}
func
equalizeSizes
(
a
,
b
ID
)
(
ID
,
ID
)
{
la
:=
len
(
a
)
lb
:=
len
(
b
)
if
la
<
lb
{
na
:=
make
([]
byte
,
lb
)
copy
(
na
,
a
)
a
=
na
}
else
if
lb
<
la
{
nb
:=
make
([]
byte
,
la
)
copy
(
nb
,
b
)
b
=
nb
}
return
a
,
b
func
commonPrefixLen
(
a
,
b
ID
)
int
{
return
ks
.
ZeroPrefixLen
(
ks
.
XOR
(
a
,
b
))
}
// ConvertPeerID creates a DHT ID by hashing a Peer ID (Multihash)
...
...
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