mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
p2p/discover: move node list helpers to node.go
This commit is contained in:
parent
0a0d38f3ed
commit
38fbf4cc7f
2 changed files with 47 additions and 45 deletions
|
|
@ -22,6 +22,8 @@ import (
|
|||
"errors"
|
||||
"math/big"
|
||||
"net"
|
||||
"slices"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/math"
|
||||
|
|
@ -89,3 +91,48 @@ func (n *tableNode) addr() *net.UDPAddr {
|
|||
func (n *tableNode) String() string {
|
||||
return n.Node.String()
|
||||
}
|
||||
|
||||
// nodesByDistance is a list of nodes, ordered by distance to target.
|
||||
type nodesByDistance struct {
|
||||
entries []*enode.Node
|
||||
target enode.ID
|
||||
}
|
||||
|
||||
// push adds the given node to the list, keeping the total size below maxElems.
|
||||
func (h *nodesByDistance) push(n *enode.Node, maxElems int) {
|
||||
ix := sort.Search(len(h.entries), func(i int) bool {
|
||||
return enode.DistCmp(h.target, h.entries[i].ID(), n.ID()) > 0
|
||||
})
|
||||
|
||||
end := len(h.entries)
|
||||
if len(h.entries) < maxElems {
|
||||
h.entries = append(h.entries, n)
|
||||
}
|
||||
if ix < end {
|
||||
// Slide existing entries down to make room.
|
||||
// This will overwrite the entry we just appended.
|
||||
copy(h.entries[ix+1:], h.entries[ix:])
|
||||
h.entries[ix] = n
|
||||
}
|
||||
}
|
||||
|
||||
type nodeType interface {
|
||||
ID() enode.ID
|
||||
}
|
||||
|
||||
// containsID reports whether ns contains a node with the given ID.
|
||||
func containsID[N nodeType](ns []N, id enode.ID) bool {
|
||||
for _, n := range ns {
|
||||
if n.ID() == id {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// deleteNode removes a node from the list.
|
||||
func deleteNode[N nodeType](list []N, id enode.ID) []N {
|
||||
return slices.DeleteFunc(list, func(n N) bool {
|
||||
return n.ID() == id
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,6 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
"slices"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
|
|
@ -683,19 +682,6 @@ func (tab *Table) handleTrackRequest(op trackRequestOp) {
|
|||
}
|
||||
}
|
||||
|
||||
type nodeType interface {
|
||||
ID() enode.ID
|
||||
}
|
||||
|
||||
func containsID[N nodeType](ns []N, id enode.ID) bool {
|
||||
for _, n := range ns {
|
||||
if n.ID() == id {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// pushNode adds n to the front of list, keeping at most max items.
|
||||
func pushNode(list []*tableNode, n *tableNode, max int) ([]*tableNode, *tableNode) {
|
||||
if len(list) < max {
|
||||
|
|
@ -706,34 +692,3 @@ func pushNode(list []*tableNode, n *tableNode, max int) ([]*tableNode, *tableNod
|
|||
list[0] = n
|
||||
return list, removed
|
||||
}
|
||||
|
||||
// deleteNode removes n from list.
|
||||
func deleteNode[N nodeType](list []N, id enode.ID) []N {
|
||||
return slices.DeleteFunc(list, func(n N) bool {
|
||||
return n.ID() == id
|
||||
})
|
||||
}
|
||||
|
||||
// nodesByDistance is a list of nodes, ordered by distance to target.
|
||||
type nodesByDistance struct {
|
||||
entries []*enode.Node
|
||||
target enode.ID
|
||||
}
|
||||
|
||||
// push adds the given node to the list, keeping the total size below maxElems.
|
||||
func (h *nodesByDistance) push(n *enode.Node, maxElems int) {
|
||||
ix := sort.Search(len(h.entries), func(i int) bool {
|
||||
return enode.DistCmp(h.target, h.entries[i].ID(), n.ID()) > 0
|
||||
})
|
||||
|
||||
end := len(h.entries)
|
||||
if len(h.entries) < maxElems {
|
||||
h.entries = append(h.entries, n)
|
||||
}
|
||||
if ix < end {
|
||||
// Slide existing entries down to make room.
|
||||
// This will overwrite the entry we just appended.
|
||||
copy(h.entries[ix+1:], h.entries[ix:])
|
||||
h.entries[ix] = n
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue