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
dms3
go-dms3
Commits
550971fb
Commit
550971fb
authored
Jul 29, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
equalize sizes
parent
171f96b7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
36 deletions
+50
-36
routing/dht/table.go
routing/dht/table.go
+50
-36
No files found.
routing/dht/table.go
View file @
550971fb
package
dht
import
(
"container/list"
"bytes"
"container/list"
)
// ID for IpfsDHT should be a byte slice, to allow for simpler operations
// (xor). DHT ids are based on the peer.IDs.
//
// NOTE: peer.IDs are biased because they are (a) multihashes (first bytes
// biased), and (b) first bits are zeroes when using the S/Kademlia PoW.
// Thus, may need to re-hash keys (uniform dist). TODO(jbenet)
// NOTE: peer.IDs are biased because they are multihashes (first bytes
// biased). Thus, may need to re-hash keys (uniform dist). TODO(jbenet)
type
ID
[]
byte
// Bucket holds a list of peers.
type
Bucket
[]
*
list
.
List
// RoutingTable defines the routing table.
type
RoutingTable
struct
{
// kBuckets define all the fingers to other nodes.
Buckets
[]
Bucket
// kBuckets define all the fingers to other nodes.
Buckets
[]
Bucket
}
func
(
id
ID
)
Equal
(
other
ID
)
bool
{
return
bytes
.
Equal
(
id
,
other
)
}
func
(
id
ID
)
Less
(
other
interface
{})
bool
{
a
,
b
:=
equalizeSizes
(
id
,
other
.
(
ID
))
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
;
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
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
}
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
}
// ids may actually be of different sizes.
var
ba
ID
var
bb
ID
if
len
(
a
)
>=
len
(
b
)
{
ba
=
a
bb
=
b
}
else
{
ba
=
b
bb
=
a
}
c
:=
make
(
ID
,
len
(
ba
))
for
i
:=
0
;
i
<
len
(
ba
);
i
++
{
if
len
(
bb
)
>
i
{
c
[
i
]
=
ba
[
i
]
^
bb
[
i
]
}
else
{
c
[
i
]
=
ba
[
i
]
^
0
}
}
return
c
return
a
,
b
}
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