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.
This commit is contained in:
Felix Lange
2015-04-27 00:50:18 +02:00
parent d457a1187d
commit 2adcc31bb4
6 changed files with 354 additions and 106 deletions

View File

@ -65,10 +65,9 @@ type (
Expiration uint64 // Absolute timestamp at which the packet becomes invalid.
}
// findnode is a query for nodes close to the given target.
findnode struct {
// Id to look up. The responding node will send back nodes
// closest to the target.
Target NodeID
Target NodeID // doesn't need to be an actual public key
Expiration uint64
}
@ -500,8 +499,9 @@ func (req *findnode) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte
// (which is a much bigger packet than findnode) to the victim.
return errUnknownNode
}
target := crypto.Sha3Hash(req.Target[:])
t.mutex.Lock()
closest := t.closest(req.Target, bucketSize).entries
closest := t.closest(target, bucketSize).entries
t.mutex.Unlock()
// TODO: this conversion could use a cached version of the slice