p2p/discover: move node list helpers to node.go

This commit is contained in:
Felix Lange 2024-05-28 22:00:48 +02:00
parent 0a0d38f3ed
commit 38fbf4cc7f
2 changed files with 47 additions and 45 deletions

View file

@ -22,6 +22,8 @@ import (
"errors" "errors"
"math/big" "math/big"
"net" "net"
"slices"
"sort"
"time" "time"
"github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/common/math"
@ -89,3 +91,48 @@ func (n *tableNode) addr() *net.UDPAddr {
func (n *tableNode) String() string { func (n *tableNode) String() string {
return n.Node.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
})
}

View file

@ -27,7 +27,6 @@ import (
"fmt" "fmt"
"net" "net"
"slices" "slices"
"sort"
"sync" "sync"
"time" "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. // pushNode adds n to the front of list, keeping at most max items.
func pushNode(list []*tableNode, n *tableNode, max int) ([]*tableNode, *tableNode) { func pushNode(list []*tableNode, n *tableNode, max int) ([]*tableNode, *tableNode) {
if len(list) < max { if len(list) < max {
@ -706,34 +692,3 @@ func pushNode(list []*tableNode, n *tableNode, max int) ([]*tableNode, *tableNod
list[0] = n list[0] = n
return list, removed 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
}
}