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
922f2ae4
Commit
922f2ae4
authored
Aug 20, 2019
by
Steven Allen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
simplify prefix lookup for rand peer ID generation
parent
33746c1a
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
15 additions
and
98 deletions
+15
-98
table.go
table.go
+15
-49
util.go
util.go
+0
-16
util_test.go
util_test.go
+0
-33
No files found.
table.go
View file @
922f2ae4
...
@@ -76,11 +76,11 @@ func (rt *RoutingTable) GenRandPeerID(bucketID int) (peer.ID, error) {
...
@@ -76,11 +76,11 @@ func (rt *RoutingTable) GenRandPeerID(bucketID int) (peer.ID, error) {
bucketLen
:=
len
(
rt
.
Buckets
)
bucketLen
:=
len
(
rt
.
Buckets
)
rt
.
tabLock
.
RUnlock
()
rt
.
tabLock
.
RUnlock
()
var
targetCpl
int
var
targetCpl
u
int
if
bucketID
>
(
bucketLen
-
1
)
{
if
bucketID
>
(
bucketLen
-
1
)
{
targetCpl
=
bucketLen
-
1
targetCpl
=
uint
(
bucketLen
)
-
1
}
else
{
}
else
{
targetCpl
=
bucketID
targetCpl
=
uint
(
bucketID
)
}
}
// We can only handle 16 bit prefixes.
// We can only handle 16 bit prefixes.
...
@@ -88,54 +88,20 @@ func (rt *RoutingTable) GenRandPeerID(bucketID int) (peer.ID, error) {
...
@@ -88,54 +88,20 @@ func (rt *RoutingTable) GenRandPeerID(bucketID int) (peer.ID, error) {
targetCpl
=
16
targetCpl
=
16
}
}
// generate random 16 bits
// Extract the local prefix and a random prefix.
r
:=
rand
.
New
(
rand
.
NewSource
(
time
.
Now
()
.
UnixNano
()))
localPrefix
:=
binary
.
BigEndian
.
Uint16
(
rt
.
local
)
buf
:=
make
([]
byte
,
2
)
randPrefix
:=
uint16
(
rand
.
Uint32
())
_
,
err
:=
r
.
Read
(
buf
)
if
err
!=
nil
{
return
""
,
err
}
// replace the first targetCPL bits with those from the hashed local peer ID & toggle the (targetCpl+1)th bit
// so that exactly targetCpl bits match
numBytes
:=
targetCpl
/
8
// number of bytes we need to replace
numBits
:=
targetCpl
%
8
// number of bits we need to replace after numBytes have been replaced
// replace the bytes
byteIndex
:=
0
for
;
byteIndex
<
numBytes
;
byteIndex
++
{
buf
[
byteIndex
]
=
rt
.
local
[
byteIndex
]
}
// replace the bits
// Combine the local prefix and the random bits at the correct offset
if
byteIndex
<
len
(
buf
)
{
// such that the first `bucketID` bits match the local ID.
dstByte
:=
buf
[
byteIndex
]
mask
:=
(
^
uint16
(
0
))
<<
targetCpl
srcByte
:=
rt
.
local
[
byteIndex
]
targetPrefix
:=
(
localPrefix
&
mask
)
|
(
randPrefix
&
^
mask
)
j
:=
uint
(
7
)
for
k
:=
1
;
k
<=
numBits
;
k
++
{
if
isSet
(
srcByte
,
j
)
{
dstByte
=
setBit
(
dstByte
,
j
)
}
else
{
dstByte
=
clearBit
(
dstByte
,
j
)
}
j
--
}
// toggle the next bit
if
isSet
(
srcByte
,
j
)
{
dstByte
=
clearBit
(
dstByte
,
j
)
}
else
{
dstByte
=
setBit
(
dstByte
,
j
)
}
buf
[
byteIndex
]
=
dstByte
}
// get the seed using buf & use it as the hash digest for a SHA2-256 Multihash to get the desired peer ID
// Convert to a known peer ID.
prefix
:=
binary
.
BigEndian
.
Uint16
(
buf
)
key
:=
keyPrefixMap
[
targetPrefix
]
key
:=
keyPrefixMap
[
prefix
]
id
:=
[
34
]
byte
{
mh
.
SHA2_256
,
32
}
h
:=
[
34
]
byte
{
mh
.
SHA2_256
,
32
}
binary
.
BigEndian
.
PutUint64
(
id
[
2
:
],
key
)
binary
.
BigEndian
.
PutUint64
(
h
[
2
:
],
key
)
return
peer
.
ID
(
id
[
:
]),
nil
return
peer
.
ID
(
h
[
:
]),
err
}
}
// Returns the bucket for a given peer
// Returns the bucket for a given peer
...
...
util.go
View file @
922f2ae4
...
@@ -35,22 +35,6 @@ func xor(a, b ID) ID {
...
@@ -35,22 +35,6 @@ func xor(a, b ID) ID {
return
ID
(
u
.
XOR
(
a
,
b
))
return
ID
(
u
.
XOR
(
a
,
b
))
}
}
func
setBit
(
n
byte
,
pos
uint
)
byte
{
n
|=
(
1
<<
pos
)
return
n
}
func
clearBit
(
n
byte
,
pos
uint
)
byte
{
mask
:=
byte
(
^
(
1
<<
pos
))
n
&=
mask
return
n
}
func
isSet
(
n
byte
,
pos
uint
)
bool
{
val
:=
n
&
(
1
<<
pos
)
return
(
val
>
0
)
}
func
CommonPrefixLen
(
a
,
b
ID
)
int
{
func
CommonPrefixLen
(
a
,
b
ID
)
int
{
return
ks
.
ZeroPrefixLen
(
u
.
XOR
(
a
,
b
))
return
ks
.
ZeroPrefixLen
(
u
.
XOR
(
a
,
b
))
}
}
...
...
util_test.go
deleted
100644 → 0
View file @
33746c1a
package
kbucket
import
(
"testing"
)
func
TestIsSet
(
t
*
testing
.
T
)
{
a
:=
byte
(
2
)
if
!
isSet
(
a
,
1
)
{
t
.
Fatal
(
"1st bit should be set"
)
}
if
isSet
(
a
,
0
)
{
t
.
Fatal
(
"0th bit should not be set"
)
}
}
func
TestSetBit
(
t
*
testing
.
T
)
{
a
:=
byte
(
1
)
if
setBit
(
a
,
1
)
!=
3
{
t
.
Fatal
(
"1st bit should have been set"
)
}
}
func
TestClearBit
(
t
*
testing
.
T
)
{
a
:=
byte
(
3
)
if
clearBit
(
a
,
0
)
!=
2
{
t
.
Fatal
(
"0th bit should have been cleared"
)
}
}
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