export NodeIDHash

This commit is contained in:
Jan Winkelmann 2015-03-21 14:02:18 +01:00
parent 5524c4656a
commit 41dea2adb3
4 changed files with 11 additions and 11 deletions

View file

@ -22,13 +22,13 @@ import (
const nodeIDBits = 512 const nodeIDBits = 512
// nodeIDHash is the hash of a NodeID // nodeIDHash is the hash of a NodeID
type nodeIDHash []byte type NodeIDHash []byte
// hashNodeID generates a nodeIDHash from a NodeID by hashing it using sha3-256 // hashNodeID generates a nodeIDHash from a NodeID by hashing it using sha3-256
func hashNodeID(id NodeID) nodeIDHash { func hashNodeID(id NodeID) NodeIDHash {
h := sha3.NewKeccak256() h := sha3.NewKeccak256()
h.Write(id[:]) h.Write(id[:])
return nodeIDHash(h.Sum(nil)) return NodeIDHash(h.Sum(nil))
} }
// Node represents a host on the network. // Node represents a host on the network.
@ -219,7 +219,7 @@ func recoverNodeID(hash, sig []byte) (id NodeID, err error) {
// distcmp compares the distances a->target and b->target. // distcmp compares the distances a->target and b->target.
// Returns -1 if a is closer to target, 1 if b is closer to target // Returns -1 if a is closer to target, 1 if b is closer to target
// and 0 if they are equal. // and 0 if they are equal.
func distcmp(target, a, b nodeIDHash) int { func distcmp(target, a, b NodeIDHash) int {
for i := range target { for i := range target {
da := a[i] ^ target[i] da := a[i] ^ target[i]
db := b[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). // logdist returns the logarithmic distance between a and b, log2(a ^ b).
func logdist(a, b nodeIDHash) int { func logdist(a, b NodeIDHash) int {
lz := 0 lz := 0
for i := range a { for i := range a {

View file

@ -161,7 +161,7 @@ func TestNodeID_distcmpEqual(t *testing.T) {
} }
func TestNodeID_logdist(t *testing.T) { func TestNodeID_logdist(t *testing.T) {
logdistBig := func(a, b nodeIDHash) int { logdistBig := func(a, b NodeIDHash) int {
abig, bbig := new(big.Int).SetBytes(a[:]), new(big.Int).SetBytes(b[:]) abig, bbig := new(big.Int).SetBytes(a[:]), new(big.Int).SetBytes(b[:])
return new(big.Int).Xor(abig, bbig).BitLen() return new(big.Int).Xor(abig, bbig).BitLen()
} }

View file

@ -259,7 +259,7 @@ func (b *bucket) bump(id NodeID) *Node {
// distance to target. // distance to target.
type nodesByDistance struct { type nodesByDistance struct {
entries []*Node entries []*Node
target nodeIDHash target NodeIDHash
} }
// push adds the given node to the list, keeping the total size below maxElems. // push adds the given node to the list, keeping the total size below maxElems.

View file

@ -184,8 +184,8 @@ type closeTest struct {
Self NodeID Self NodeID
Target NodeID Target NodeID
SelfHash nodeIDHash SelfHash NodeIDHash
TargetHash nodeIDHash TargetHash NodeIDHash
All []*Node All []*Node
N int N int
@ -281,8 +281,8 @@ func hasDuplicates(slice []*Node) bool {
return false return false
} }
func sortedByDistanceTo(distbase nodeIDHash, slice []*Node) bool { func sortedByDistanceTo(distbase NodeIDHash, slice []*Node) bool {
var last nodeIDHash var last NodeIDHash
for i, e := range slice { for i, e := range slice {
if i > 0 && distcmp(distbase, e.idHash, last) < 0 { if i > 0 && distcmp(distbase, e.idHash, last) < 0 {
return false return false