mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 04:36:42 +00:00
export NodeIDHash
This commit is contained in:
parent
5524c4656a
commit
41dea2adb3
4 changed files with 11 additions and 11 deletions
|
|
@ -22,13 +22,13 @@ import (
|
|||
const nodeIDBits = 512
|
||||
|
||||
// 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
|
||||
func hashNodeID(id NodeID) nodeIDHash {
|
||||
func hashNodeID(id NodeID) NodeIDHash {
|
||||
h := sha3.NewKeccak256()
|
||||
h.Write(id[:])
|
||||
return nodeIDHash(h.Sum(nil))
|
||||
return NodeIDHash(h.Sum(nil))
|
||||
}
|
||||
|
||||
// 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.
|
||||
// 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 nodeIDHash) int {
|
||||
func distcmp(target, a, b NodeIDHash) 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 nodeIDHash) int {
|
||||
func logdist(a, b NodeIDHash) int {
|
||||
|
||||
lz := 0
|
||||
for i := range a {
|
||||
|
|
|
|||
|
|
@ -161,7 +161,7 @@ func TestNodeID_distcmpEqual(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[:])
|
||||
return new(big.Int).Xor(abig, bbig).BitLen()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -259,7 +259,7 @@ func (b *bucket) bump(id NodeID) *Node {
|
|||
// distance to target.
|
||||
type nodesByDistance struct {
|
||||
entries []*Node
|
||||
target nodeIDHash
|
||||
target NodeIDHash
|
||||
}
|
||||
|
||||
// push adds the given node to the list, keeping the total size below maxElems.
|
||||
|
|
|
|||
|
|
@ -184,8 +184,8 @@ type closeTest struct {
|
|||
Self NodeID
|
||||
Target NodeID
|
||||
|
||||
SelfHash nodeIDHash
|
||||
TargetHash nodeIDHash
|
||||
SelfHash NodeIDHash
|
||||
TargetHash NodeIDHash
|
||||
|
||||
All []*Node
|
||||
N int
|
||||
|
|
@ -281,8 +281,8 @@ func hasDuplicates(slice []*Node) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func sortedByDistanceTo(distbase nodeIDHash, slice []*Node) bool {
|
||||
var last nodeIDHash
|
||||
func sortedByDistanceTo(distbase NodeIDHash, slice []*Node) bool {
|
||||
var last NodeIDHash
|
||||
for i, e := range slice {
|
||||
if i > 0 && distcmp(distbase, e.idHash, last) < 0 {
|
||||
return false
|
||||
|
|
|
|||
Loading…
Reference in a new issue