diff options
author | Felix Lange <fjl@twurst.com> | 2015-04-27 06:50:18 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2015-05-06 22:10:41 +0800 |
commit | 2adcc31bb48af0dee979f2b4ab255d9af21fd097 (patch) | |
tree | e13845f15c96a87ac0fc9345f3a0ee90cfd006da /p2p/discover/udp_test.go | |
parent | d457a1187dbbbf08bcce437789732dab02a73b0f (diff) | |
download | dexon-2adcc31bb48af0dee979f2b4ab255d9af21fd097.tar dexon-2adcc31bb48af0dee979f2b4ab255d9af21fd097.tar.gz dexon-2adcc31bb48af0dee979f2b4ab255d9af21fd097.tar.bz2 dexon-2adcc31bb48af0dee979f2b4ab255d9af21fd097.tar.lz dexon-2adcc31bb48af0dee979f2b4ab255d9af21fd097.tar.xz 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/udp_test.go')
-rw-r--r-- | p2p/discover/udp_test.go | 16 |
1 files changed, 6 insertions, 10 deletions
diff --git a/p2p/discover/udp_test.go b/p2p/discover/udp_test.go index 167a5439b..a2bb503ff 100644 --- a/p2p/discover/udp_test.go +++ b/p2p/discover/udp_test.go @@ -16,6 +16,7 @@ import ( "testing" "time" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/logger" ) @@ -26,7 +27,7 @@ func init() { // shared test variables var ( futureExp = uint64(time.Now().Add(10 * time.Hour).Unix()) - testTarget = MustHexID("01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101") + testTarget = NodeID{0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1} testRemote = rpcEndpoint{IP: net.ParseIP("1.1.1.1").To4(), UDP: 1, TCP: 2} testLocalAnnounced = rpcEndpoint{IP: net.ParseIP("2.2.2.2").To4(), UDP: 3, TCP: 4} testLocal = rpcEndpoint{IP: net.ParseIP("3.3.3.3").To4(), UDP: 5, TCP: 6} @@ -145,15 +146,10 @@ func TestUDP_findnode(t *testing.T) { // put a few nodes into the table. their exact // distribution shouldn't matter much, altough we need to // take care not to overflow any bucket. - target := testTarget - nodes := &nodesByDistance{target: target} + targetHash := crypto.Sha3Hash(testTarget[:]) + nodes := &nodesByDistance{target: targetHash} for i := 0; i < bucketSize; i++ { - nodes.push(&Node{ - IP: net.IP{1, 2, 3, byte(i)}, - UDP: uint16(i + 2), - TCP: uint16(i + 3), - ID: randomID(test.table.self.ID, i+2), - }, bucketSize) + nodes.push(nodeAtDistance(test.table.self.sha, i+2), bucketSize) } test.table.add(nodes.entries) @@ -168,7 +164,7 @@ func TestUDP_findnode(t *testing.T) { // check that closest neighbors are returned. test.packetIn(nil, findnodePacket, &findnode{Target: testTarget, Expiration: futureExp}) test.waitPacketOut(func(p *neighbors) { - expected := test.table.closest(testTarget, bucketSize) + expected := test.table.closest(targetHash, bucketSize) if len(p.Nodes) != bucketSize { t.Errorf("wrong number of results: got %d, want %d", len(p.Nodes), bucketSize) } |