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/node_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/node_test.go')
-rw-r--r-- | p2p/discover/node_test.go | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/p2p/discover/node_test.go b/p2p/discover/node_test.go index 4c95d316f..b1babd989 100644 --- a/p2p/discover/node_test.go +++ b/p2p/discover/node_test.go @@ -9,6 +9,7 @@ import ( "testing/quick" "time" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" ) @@ -169,7 +170,7 @@ func TestNodeID_pubkeyBad(t *testing.T) { } func TestNodeID_distcmp(t *testing.T) { - distcmpBig := func(target, a, b NodeID) int { + distcmpBig := func(target, a, b common.Hash) int { tbig := new(big.Int).SetBytes(target[:]) abig := new(big.Int).SetBytes(a[:]) bbig := new(big.Int).SetBytes(b[:]) @@ -182,15 +183,15 @@ func TestNodeID_distcmp(t *testing.T) { // the random tests is likely to miss the case where they're equal. func TestNodeID_distcmpEqual(t *testing.T) { - base := NodeID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} - x := NodeID{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0} + base := common.Hash{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} + x := common.Hash{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0} if distcmp(base, x, x) != 0 { t.Errorf("distcmp(base, x, x) != 0") } } func TestNodeID_logdist(t *testing.T) { - logdistBig := func(a, b NodeID) int { + logdistBig := func(a, b common.Hash) int { abig, bbig := new(big.Int).SetBytes(a[:]), new(big.Int).SetBytes(b[:]) return new(big.Int).Xor(abig, bbig).BitLen() } @@ -201,19 +202,19 @@ func TestNodeID_logdist(t *testing.T) { // the random tests is likely to miss the case where they're equal. func TestNodeID_logdistEqual(t *testing.T) { - x := NodeID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} + x := common.Hash{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} if logdist(x, x) != 0 { t.Errorf("logdist(x, x) != 0") } } -func TestNodeID_randomID(t *testing.T) { +func TestNodeID_hashAtDistance(t *testing.T) { // we don't use quick.Check here because its output isn't // very helpful when the test fails. for i := 0; i < quickcfg.MaxCount; i++ { - a := gen(NodeID{}, quickrand).(NodeID) - dist := quickrand.Intn(len(NodeID{}) * 8) - result := randomID(a, dist) + a := gen(common.Hash{}, quickrand).(common.Hash) + dist := quickrand.Intn(len(common.Hash{}) * 8) + result := hashAtDistance(a, dist) actualdist := logdist(result, a) if dist != actualdist { @@ -224,6 +225,9 @@ func TestNodeID_randomID(t *testing.T) { } } +// TODO: this can be dropped when we require Go >= 1.5 +// because testing/quick learned to generate arrays in 1.5. + func (NodeID) Generate(rand *rand.Rand, size int) reflect.Value { var id NodeID m := rand.Intn(len(id)) |