mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 12:46:44 +00:00
also export hash's stuct field on Node
This commit is contained in:
parent
41dea2adb3
commit
d80f79349e
3 changed files with 13 additions and 13 deletions
|
|
@ -40,7 +40,7 @@ type Node struct {
|
||||||
TCPPort int // TCP listening port for RLPx
|
TCPPort int // TCP listening port for RLPx
|
||||||
|
|
||||||
active time.Time
|
active time.Time
|
||||||
idHash []byte
|
IDHash []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func newNode(id NodeID, addr *net.UDPAddr) *Node {
|
func newNode(id NodeID, addr *net.UDPAddr) *Node {
|
||||||
|
|
@ -50,7 +50,7 @@ func newNode(id NodeID, addr *net.UDPAddr) *Node {
|
||||||
DiscPort: addr.Port,
|
DiscPort: addr.Port,
|
||||||
TCPPort: addr.Port,
|
TCPPort: addr.Port,
|
||||||
active: time.Now(),
|
active: time.Now(),
|
||||||
idHash: hashNodeID(id),
|
IDHash: hashNodeID(id),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -96,7 +96,7 @@ func (tab *Table) Lookup(target NodeID) []*Node {
|
||||||
|
|
||||||
tab.mutex.Lock()
|
tab.mutex.Lock()
|
||||||
// update last lookup stamp (for refresh logic)
|
// update last lookup stamp (for refresh logic)
|
||||||
tab.buckets[logdist(tab.self.idHash, targetHash)].lastLookup = time.Now()
|
tab.buckets[logdist(tab.self.IDHash, targetHash)].lastLookup = time.Now()
|
||||||
// generate initial result set
|
// generate initial result set
|
||||||
result := tab.closest(target, bucketSize)
|
result := tab.closest(target, bucketSize)
|
||||||
tab.mutex.Unlock()
|
tab.mutex.Unlock()
|
||||||
|
|
@ -182,7 +182,7 @@ func (tab *Table) len() (n int) {
|
||||||
// attempts to insert the node into a bucket. The returned Node might
|
// attempts to insert the node into a bucket. The returned Node might
|
||||||
// not be part of the table. The caller must hold tab.mutex.
|
// not be part of the table. The caller must hold tab.mutex.
|
||||||
func (tab *Table) bumpOrAdd(node NodeID, from *net.UDPAddr) (n *Node) {
|
func (tab *Table) bumpOrAdd(node NodeID, from *net.UDPAddr) (n *Node) {
|
||||||
b := tab.buckets[logdist(tab.self.idHash, hashNodeID(node))]
|
b := tab.buckets[logdist(tab.self.IDHash, hashNodeID(node))]
|
||||||
if n = b.bump(node); n == nil {
|
if n = b.bump(node); n == nil {
|
||||||
n = newNode(node, from)
|
n = newNode(node, from)
|
||||||
if len(b.entries) == bucketSize {
|
if len(b.entries) == bucketSize {
|
||||||
|
|
@ -216,7 +216,7 @@ func (tab *Table) pingReplace(n *Node, b *bucket) {
|
||||||
// bump updates the activity timestamp for the given node.
|
// bump updates the activity timestamp for the given node.
|
||||||
// The caller must hold tab.mutex.
|
// The caller must hold tab.mutex.
|
||||||
func (tab *Table) bump(node NodeID) {
|
func (tab *Table) bump(node NodeID) {
|
||||||
tab.buckets[logdist(tab.self.idHash, hashNodeID(node))].bump(node)
|
tab.buckets[logdist(tab.self.IDHash, hashNodeID(node))].bump(node)
|
||||||
}
|
}
|
||||||
|
|
||||||
// add puts the entries into the table if their corresponding
|
// add puts the entries into the table if their corresponding
|
||||||
|
|
@ -229,7 +229,7 @@ outer:
|
||||||
// input lists.
|
// input lists.
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
bucket := tab.buckets[logdist(tab.self.idHash, n.idHash)]
|
bucket := tab.buckets[logdist(tab.self.IDHash, n.IDHash)]
|
||||||
for i := range bucket.entries {
|
for i := range bucket.entries {
|
||||||
if bucket.entries[i].ID == n.ID {
|
if bucket.entries[i].ID == n.ID {
|
||||||
// already in bucket
|
// already in bucket
|
||||||
|
|
@ -265,7 +265,7 @@ type nodesByDistance struct {
|
||||||
// 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.
|
||||||
func (h *nodesByDistance) push(n *Node, maxElems int) {
|
func (h *nodesByDistance) push(n *Node, maxElems int) {
|
||||||
ix := sort.Search(len(h.entries), func(i int) bool {
|
ix := sort.Search(len(h.entries), func(i int) bool {
|
||||||
return distcmp(h.target, h.entries[i].idHash, n.idHash) > 0
|
return distcmp(h.target, h.entries[i].IDHash, n.IDHash) > 0
|
||||||
})
|
})
|
||||||
if len(h.entries) < maxElems {
|
if len(h.entries) < maxElems {
|
||||||
h.entries = append(h.entries, n)
|
h.entries = append(h.entries, n)
|
||||||
|
|
|
||||||
|
|
@ -163,8 +163,8 @@ func TestTable_closest(t *testing.T) {
|
||||||
if contains(result, n.ID) {
|
if contains(result, n.ID) {
|
||||||
continue // don't run the check below for nodes in result
|
continue // don't run the check below for nodes in result
|
||||||
}
|
}
|
||||||
farthestResult := result[len(result)-1].idHash
|
farthestResult := result[len(result)-1].IDHash
|
||||||
if distcmp(test.TargetHash, n.idHash, farthestResult) < 0 {
|
if distcmp(test.TargetHash, n.IDHash, farthestResult) < 0 {
|
||||||
t.Errorf("table contains node that is closer to target but it's not in result")
|
t.Errorf("table contains node that is closer to target but it's not in result")
|
||||||
t.Logf(" Target: %v", test.Target)
|
t.Logf(" Target: %v", test.Target)
|
||||||
t.Logf(" Farthest Result: %v", farthestResult)
|
t.Logf(" Farthest Result: %v", farthestResult)
|
||||||
|
|
@ -202,7 +202,7 @@ func (*closeTest) Generate(rand *rand.Rand, size int) reflect.Value {
|
||||||
t.TargetHash = hashNodeID(t.Target)
|
t.TargetHash = hashNodeID(t.Target)
|
||||||
|
|
||||||
for _, id := range gen([]NodeID{}, rand).([]NodeID) {
|
for _, id := range gen([]NodeID{}, rand).([]NodeID) {
|
||||||
t.All = append(t.All, &Node{ID: id, idHash: hashNodeID(id)})
|
t.All = append(t.All, &Node{ID: id, IDHash: hashNodeID(id)})
|
||||||
}
|
}
|
||||||
return reflect.ValueOf(t)
|
return reflect.ValueOf(t)
|
||||||
}
|
}
|
||||||
|
|
@ -224,7 +224,7 @@ func TestTable_Lookup(t *testing.T) {
|
||||||
results := tab.Lookup(target)
|
results := tab.Lookup(target)
|
||||||
t.Logf("results:")
|
t.Logf("results:")
|
||||||
for _, e := range results {
|
for _, e := range results {
|
||||||
t.Logf(" ld=%d, %v", logdist(targetHash, e.idHash), e.ID)
|
t.Logf(" ld=%d, %v", logdist(targetHash, e.IDHash), e.ID)
|
||||||
}
|
}
|
||||||
if len(results) != bucketSize {
|
if len(results) != bucketSize {
|
||||||
t.Errorf("wrong number of results: got %d, want %d", len(results), bucketSize)
|
t.Errorf("wrong number of results: got %d, want %d", len(results), bucketSize)
|
||||||
|
|
@ -284,10 +284,10 @@ func hasDuplicates(slice []*Node) bool {
|
||||||
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
|
||||||
}
|
}
|
||||||
last = e.idHash
|
last = e.IDHash
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue