mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
p2p/discover: rename node -> tableNode
This commit is contained in:
parent
974538eaf1
commit
2c5bc1d843
5 changed files with 36 additions and 36 deletions
|
|
@ -37,8 +37,8 @@ type BucketNode struct {
|
||||||
Live bool `json:"live"`
|
Live bool `json:"live"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// node is a node table entry.
|
// tableNode is an entry in Table.
|
||||||
type node struct {
|
type tableNode struct {
|
||||||
*enode.Node
|
*enode.Node
|
||||||
revalList *revalidationList
|
revalList *revalidationList
|
||||||
addedToTable time.Time // first time node was added to bucket or replacement list
|
addedToTable time.Time // first time node was added to bucket or replacement list
|
||||||
|
|
@ -74,7 +74,7 @@ func (e encPubkey) id() enode.ID {
|
||||||
return enode.ID(crypto.Keccak256Hash(e[:]))
|
return enode.ID(crypto.Keccak256Hash(e[:]))
|
||||||
}
|
}
|
||||||
|
|
||||||
func unwrapNodes(ns []*node) []*enode.Node {
|
func unwrapNodes(ns []*tableNode) []*enode.Node {
|
||||||
result := make([]*enode.Node, len(ns))
|
result := make([]*enode.Node, len(ns))
|
||||||
for i, n := range ns {
|
for i, n := range ns {
|
||||||
result[i] = n.Node
|
result[i] = n.Node
|
||||||
|
|
@ -82,10 +82,10 @@ func unwrapNodes(ns []*node) []*enode.Node {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *node) addr() *net.UDPAddr {
|
func (n *tableNode) addr() *net.UDPAddr {
|
||||||
return &net.UDPAddr{IP: n.IP(), Port: n.UDP()}
|
return &net.UDPAddr{IP: n.IP(), Port: n.UDP()}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *node) String() string {
|
func (n *tableNode) String() string {
|
||||||
return n.Node.String()
|
return n.Node.String()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -85,8 +85,8 @@ type Table struct {
|
||||||
closeReq chan struct{}
|
closeReq chan struct{}
|
||||||
closed chan struct{}
|
closed chan struct{}
|
||||||
|
|
||||||
nodeAddedHook func(*bucket, *node)
|
nodeAddedHook func(*bucket, *tableNode)
|
||||||
nodeRemovedHook func(*bucket, *node)
|
nodeRemovedHook func(*bucket, *tableNode)
|
||||||
}
|
}
|
||||||
|
|
||||||
// transport is implemented by the UDP transports.
|
// transport is implemented by the UDP transports.
|
||||||
|
|
@ -101,8 +101,8 @@ type transport interface {
|
||||||
// bucket contains nodes, ordered by their last activity. the entry
|
// bucket contains nodes, ordered by their last activity. the entry
|
||||||
// that was most recently active is the first element in entries.
|
// that was most recently active is the first element in entries.
|
||||||
type bucket struct {
|
type bucket struct {
|
||||||
entries []*node // live entries, sorted by time of last contact
|
entries []*tableNode // live entries, sorted by time of last contact
|
||||||
replacements []*node // recently seen nodes to be used if revalidation fails
|
replacements []*tableNode // recently seen nodes to be used if revalidation fails
|
||||||
ips netutil.DistinctNetSet
|
ips netutil.DistinctNetSet
|
||||||
index int
|
index int
|
||||||
}
|
}
|
||||||
|
|
@ -531,7 +531,7 @@ func (tab *Table) handleAddNode(req addNodeOp) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add to bucket.
|
// Add to bucket.
|
||||||
wn := &node{Node: req.node}
|
wn := &tableNode{Node: req.node}
|
||||||
if req.forceSetLive {
|
if req.forceSetLive {
|
||||||
wn.livenessChecks = 1
|
wn.livenessChecks = 1
|
||||||
wn.isValidatedLive = true
|
wn.isValidatedLive = true
|
||||||
|
|
@ -552,15 +552,15 @@ func (tab *Table) addReplacement(b *bucket, n *enode.Node) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
wn := &node{Node: n, addedToTable: time.Now()}
|
wn := &tableNode{Node: n, addedToTable: time.Now()}
|
||||||
var removed *node
|
var removed *tableNode
|
||||||
b.replacements, removed = pushNode(b.replacements, wn, maxReplacements)
|
b.replacements, removed = pushNode(b.replacements, wn, maxReplacements)
|
||||||
if removed != nil {
|
if removed != nil {
|
||||||
tab.removeIP(b, removed.IP())
|
tab.removeIP(b, removed.IP())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tab *Table) nodeAdded(b *bucket, n *node) {
|
func (tab *Table) nodeAdded(b *bucket, n *tableNode) {
|
||||||
if n.addedToTable == (time.Time{}) {
|
if n.addedToTable == (time.Time{}) {
|
||||||
n.addedToTable = time.Now()
|
n.addedToTable = time.Now()
|
||||||
}
|
}
|
||||||
|
|
@ -574,7 +574,7 @@ func (tab *Table) nodeAdded(b *bucket, n *node) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tab *Table) nodeRemoved(b *bucket, n *node) {
|
func (tab *Table) nodeRemoved(b *bucket, n *tableNode) {
|
||||||
tab.revalidation.nodeRemoved(n)
|
tab.revalidation.nodeRemoved(n)
|
||||||
if tab.nodeRemovedHook != nil {
|
if tab.nodeRemovedHook != nil {
|
||||||
tab.nodeRemovedHook(b, n)
|
tab.nodeRemovedHook(b, n)
|
||||||
|
|
@ -586,8 +586,8 @@ func (tab *Table) nodeRemoved(b *bucket, n *node) {
|
||||||
|
|
||||||
// deleteInBucket removes node n from the table.
|
// deleteInBucket removes node n from the table.
|
||||||
// If there are replacement nodes in the bucket, the node is replaced.
|
// If there are replacement nodes in the bucket, the node is replaced.
|
||||||
func (tab *Table) deleteInBucket(b *bucket, id enode.ID) *node {
|
func (tab *Table) deleteInBucket(b *bucket, id enode.ID) *tableNode {
|
||||||
index := slices.IndexFunc(b.entries, func(e *node) bool { return e.ID() == id })
|
index := slices.IndexFunc(b.entries, func(e *tableNode) bool { return e.ID() == id })
|
||||||
if index == -1 {
|
if index == -1 {
|
||||||
// Entry has been removed already.
|
// Entry has been removed already.
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -615,8 +615,8 @@ func (tab *Table) deleteInBucket(b *bucket, id enode.ID) *node {
|
||||||
|
|
||||||
// bumpInBucket updates a node record if it exists in the bucket.
|
// bumpInBucket updates a node record if it exists in the bucket.
|
||||||
// The second return value reports whether the node's endpoint (IP/port) was updated.
|
// The second return value reports whether the node's endpoint (IP/port) was updated.
|
||||||
func (tab *Table) bumpInBucket(b *bucket, newRecord *enode.Node, isInbound bool) (n *node, endpointChanged bool) {
|
func (tab *Table) bumpInBucket(b *bucket, newRecord *enode.Node, isInbound bool) (n *tableNode, endpointChanged bool) {
|
||||||
i := slices.IndexFunc(b.entries, func(elem *node) bool {
|
i := slices.IndexFunc(b.entries, func(elem *tableNode) bool {
|
||||||
return elem.ID() == newRecord.ID()
|
return elem.ID() == newRecord.ID()
|
||||||
})
|
})
|
||||||
if i == -1 {
|
if i == -1 {
|
||||||
|
|
@ -697,7 +697,7 @@ func containsID[N nodeType](ns []N, id enode.ID) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 []*node, n *node, max int) ([]*node, *node) {
|
func pushNode(list []*tableNode, n *tableNode, max int) ([]*tableNode, *tableNode) {
|
||||||
if len(list) < max {
|
if len(list) < max {
|
||||||
list = append(list, nil)
|
list = append(list, nil)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ type tableRevalidation struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type revalidationResponse struct {
|
type revalidationResponse struct {
|
||||||
n *node
|
n *tableNode
|
||||||
newRecord *enode.Node
|
newRecord *enode.Node
|
||||||
didRespond bool
|
didRespond bool
|
||||||
}
|
}
|
||||||
|
|
@ -55,12 +55,12 @@ func (tr *tableRevalidation) init(cfg *Config) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// nodeAdded is called when the table receives a new node.
|
// nodeAdded is called when the table receives a new node.
|
||||||
func (tr *tableRevalidation) nodeAdded(tab *Table, n *node) {
|
func (tr *tableRevalidation) nodeAdded(tab *Table, n *tableNode) {
|
||||||
tr.fast.push(n, tab.cfg.Clock.Now(), &tab.rand)
|
tr.fast.push(n, tab.cfg.Clock.Now(), &tab.rand)
|
||||||
}
|
}
|
||||||
|
|
||||||
// nodeRemoved is called when a node was removed from the table.
|
// nodeRemoved is called when a node was removed from the table.
|
||||||
func (tr *tableRevalidation) nodeRemoved(n *node) {
|
func (tr *tableRevalidation) nodeRemoved(n *tableNode) {
|
||||||
if n.revalList == nil {
|
if n.revalList == nil {
|
||||||
panic(fmt.Errorf("removed node %v has nil revalList", n.ID()))
|
panic(fmt.Errorf("removed node %v has nil revalList", n.ID()))
|
||||||
}
|
}
|
||||||
|
|
@ -68,7 +68,7 @@ func (tr *tableRevalidation) nodeRemoved(n *node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// nodeEndpointChanged is called when a change in IP or port is detected.
|
// nodeEndpointChanged is called when a change in IP or port is detected.
|
||||||
func (tr *tableRevalidation) nodeEndpointChanged(tab *Table, n *node) {
|
func (tr *tableRevalidation) nodeEndpointChanged(tab *Table, n *tableNode) {
|
||||||
n.isValidatedLive = false
|
n.isValidatedLive = false
|
||||||
tr.moveToList(&tr.fast, n, tab.cfg.Clock.Now(), &tab.rand)
|
tr.moveToList(&tr.fast, n, tab.cfg.Clock.Now(), &tab.rand)
|
||||||
}
|
}
|
||||||
|
|
@ -90,7 +90,7 @@ func (tr *tableRevalidation) run(tab *Table, now mclock.AbsTime) (nextTime mcloc
|
||||||
}
|
}
|
||||||
|
|
||||||
// startRequest spawns a revalidation request for node n.
|
// startRequest spawns a revalidation request for node n.
|
||||||
func (tr *tableRevalidation) startRequest(tab *Table, n *node) {
|
func (tr *tableRevalidation) startRequest(tab *Table, n *tableNode) {
|
||||||
if _, ok := tr.activeReq[n.ID()]; ok {
|
if _, ok := tr.activeReq[n.ID()]; ok {
|
||||||
panic(fmt.Errorf("duplicate startRequest (node %v)", n.ID()))
|
panic(fmt.Errorf("duplicate startRequest (node %v)", n.ID()))
|
||||||
}
|
}
|
||||||
|
|
@ -180,7 +180,7 @@ func (tr *tableRevalidation) handleResponse(tab *Table, resp revalidationRespons
|
||||||
}
|
}
|
||||||
|
|
||||||
// moveToList ensures n is in the 'dest' list.
|
// moveToList ensures n is in the 'dest' list.
|
||||||
func (tr *tableRevalidation) moveToList(dest *revalidationList, n *node, now mclock.AbsTime, rand randomSource) {
|
func (tr *tableRevalidation) moveToList(dest *revalidationList, n *tableNode, now mclock.AbsTime, rand randomSource) {
|
||||||
if n.revalList == dest {
|
if n.revalList == dest {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -192,14 +192,14 @@ func (tr *tableRevalidation) moveToList(dest *revalidationList, n *node, now mcl
|
||||||
|
|
||||||
// revalidationList holds a list nodes and the next revalidation time.
|
// revalidationList holds a list nodes and the next revalidation time.
|
||||||
type revalidationList struct {
|
type revalidationList struct {
|
||||||
nodes []*node
|
nodes []*tableNode
|
||||||
nextTime mclock.AbsTime
|
nextTime mclock.AbsTime
|
||||||
interval time.Duration
|
interval time.Duration
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
// get returns a random node from the queue. Nodes in the 'exclude' map are not returned.
|
// get returns a random node from the queue. Nodes in the 'exclude' map are not returned.
|
||||||
func (list *revalidationList) get(now mclock.AbsTime, rand randomSource, exclude map[enode.ID]struct{}) *node {
|
func (list *revalidationList) get(now mclock.AbsTime, rand randomSource, exclude map[enode.ID]struct{}) *tableNode {
|
||||||
if now < list.nextTime || len(list.nodes) == 0 {
|
if now < list.nextTime || len(list.nodes) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -217,7 +217,7 @@ func (list *revalidationList) schedule(now mclock.AbsTime, rand randomSource) {
|
||||||
list.nextTime = now.Add(time.Duration(rand.Int63n(int64(list.interval))))
|
list.nextTime = now.Add(time.Duration(rand.Int63n(int64(list.interval))))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (list *revalidationList) push(n *node, now mclock.AbsTime, rand randomSource) {
|
func (list *revalidationList) push(n *tableNode, now mclock.AbsTime, rand randomSource) {
|
||||||
list.nodes = append(list.nodes, n)
|
list.nodes = append(list.nodes, n)
|
||||||
if list.nextTime == never {
|
if list.nextTime == never {
|
||||||
list.schedule(now, rand)
|
list.schedule(now, rand)
|
||||||
|
|
@ -225,7 +225,7 @@ func (list *revalidationList) push(n *node, now mclock.AbsTime, rand randomSourc
|
||||||
n.revalList = list
|
n.revalList = list
|
||||||
}
|
}
|
||||||
|
|
||||||
func (list *revalidationList) remove(n *node) {
|
func (list *revalidationList) remove(n *tableNode) {
|
||||||
i := slices.Index(list.nodes, n)
|
i := slices.Index(list.nodes, n)
|
||||||
if i == -1 {
|
if i == -1 {
|
||||||
panic(fmt.Errorf("node %v not found in list", n.ID()))
|
panic(fmt.Errorf("node %v not found in list", n.ID()))
|
||||||
|
|
@ -238,7 +238,7 @@ func (list *revalidationList) remove(n *node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (list *revalidationList) contains(id enode.ID) bool {
|
func (list *revalidationList) contains(id enode.ID) bool {
|
||||||
return slices.ContainsFunc(list.nodes, func(n *node) bool {
|
return slices.ContainsFunc(list.nodes, func(n *tableNode) bool {
|
||||||
return n.ID() == id
|
return n.ID() == id
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -105,7 +105,7 @@ func intIP(i int) net.IP {
|
||||||
}
|
}
|
||||||
|
|
||||||
// fillBucket inserts nodes into the given bucket until it is full.
|
// fillBucket inserts nodes into the given bucket until it is full.
|
||||||
func fillBucket(tab *Table, id enode.ID) (last *node) {
|
func fillBucket(tab *Table, id enode.ID) (last *tableNode) {
|
||||||
ld := enode.LogDist(tab.self().ID(), id)
|
ld := enode.LogDist(tab.self().ID(), id)
|
||||||
b := tab.bucket(id)
|
b := tab.bucket(id)
|
||||||
for len(b.entries) < bucketSize {
|
for len(b.entries) < bucketSize {
|
||||||
|
|
@ -302,7 +302,7 @@ type nodeEventRecorder struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type recordedNodeEvent struct {
|
type recordedNodeEvent struct {
|
||||||
node *node
|
node *tableNode
|
||||||
added bool
|
added bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -312,7 +312,7 @@ func newNodeEventRecorder(buffer int) *nodeEventRecorder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (set *nodeEventRecorder) nodeAdded(b *bucket, n *node) {
|
func (set *nodeEventRecorder) nodeAdded(b *bucket, n *tableNode) {
|
||||||
select {
|
select {
|
||||||
case set.evc <- recordedNodeEvent{n, true}:
|
case set.evc <- recordedNodeEvent{n, true}:
|
||||||
default:
|
default:
|
||||||
|
|
@ -320,7 +320,7 @@ func (set *nodeEventRecorder) nodeAdded(b *bucket, n *node) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (set *nodeEventRecorder) nodeRemoved(b *bucket, n *node) {
|
func (set *nodeEventRecorder) nodeRemoved(b *bucket, n *tableNode) {
|
||||||
select {
|
select {
|
||||||
case set.evc <- recordedNodeEvent{n, false}:
|
case set.evc <- recordedNodeEvent{n, false}:
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
|
|
@ -393,8 +393,8 @@ func TestUDPv4_pingMatchIP(t *testing.T) {
|
||||||
|
|
||||||
func TestUDPv4_successfulPing(t *testing.T) {
|
func TestUDPv4_successfulPing(t *testing.T) {
|
||||||
test := newUDPTest(t)
|
test := newUDPTest(t)
|
||||||
added := make(chan *node, 1)
|
added := make(chan *tableNode, 1)
|
||||||
test.table.nodeAddedHook = func(b *bucket, n *node) { added <- n }
|
test.table.nodeAddedHook = func(b *bucket, n *tableNode) { added <- n }
|
||||||
defer test.close()
|
defer test.close()
|
||||||
|
|
||||||
// The remote side sends a ping packet to initiate the exchange.
|
// The remote side sends a ping packet to initiate the exchange.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue