aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/discover/node.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2015-04-27 06:50:18 +0800
committerFelix Lange <fjl@twurst.com>2015-05-06 22:10:41 +0800
commit2adcc31bb48af0dee979f2b4ab255d9af21fd097 (patch)
treee13845f15c96a87ac0fc9345f3a0ee90cfd006da /p2p/discover/node.go
parentd457a1187dbbbf08bcce437789732dab02a73b0f (diff)
downloaddexon-2adcc31bb48af0dee979f2b4ab255d9af21fd097.tar.gz
dexon-2adcc31bb48af0dee979f2b4ab255d9af21fd097.tar.zst
dexon-2adcc31bb48af0dee979f2b4ab255d9af21fd097.zip
p2p/discover: new distance metric based on sha3(id)
The previous metric was pubkey1^pubkey2, as specified in the Kademlia paper. We missed that EC public keys are not uniformly distributed. Using the hash of the public keys addresses that. It also makes it a bit harder to generate node IDs that are close to a particular node.
Diffstat (limited to 'p2p/discover/node.go')
-rw-r--r--p2p/discover/node.go8
1 files changed, 4 insertions, 4 deletions
diff --git a/p2p/discover/node.go b/p2p/discover/node.go
index d922ed317..a365ade15 100644
--- a/p2p/discover/node.go
+++ b/p2p/discover/node.go
@@ -219,7 +219,7 @@ func recoverNodeID(hash, sig []byte) (id NodeID, err error) {
// distcmp compares the distances a->target and b->target.
// Returns -1 if a is closer to target, 1 if b is closer to target
// and 0 if they are equal.
-func distcmp(target, a, b NodeID) int {
+func distcmp(target, a, b common.Hash) int {
for i := range target {
da := a[i] ^ target[i]
db := b[i] ^ target[i]
@@ -269,7 +269,7 @@ var lzcount = [256]int{
}
// logdist returns the logarithmic distance between a and b, log2(a ^ b).
-func logdist(a, b NodeID) int {
+func logdist(a, b common.Hash) int {
lz := 0
for i := range a {
x := a[i] ^ b[i]
@@ -283,8 +283,8 @@ func logdist(a, b NodeID) int {
return len(a)*8 - lz
}
-// randomID returns a random NodeID such that logdist(a, b) == n
-func randomID(a NodeID, n int) (b NodeID) {
+// hashAtDistance returns a random hash such that logdist(a, b) == n
+func hashAtDistance(a common.Hash, n int) (b common.Hash) {
if n == 0 {
return a
}