2019-10-29 18:08:57 +03:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-06-07 18:27:21 +03:00
|
|
|
"errors"
|
2019-11-26 13:14:43 +03:00
|
|
|
"time"
|
2019-10-29 18:08:57 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
)
|
|
|
|
|
|
|
|
// lookup performs a network search for nodes close to the given target. It approaches the
|
|
|
|
// target by querying nodes that are closer to it on each iteration. The given target does
|
|
|
|
// not need to be an actual node identifier.
|
|
|
|
type lookup struct {
|
|
|
|
tab *Table
|
2024-05-29 16:02:26 +03:00
|
|
|
queryfunc queryFunc
|
|
|
|
replyCh chan []*enode.Node
|
2019-10-29 18:08:57 +03:00
|
|
|
cancelCh <-chan struct{}
|
|
|
|
asked, seen map[enode.ID]bool
|
|
|
|
result nodesByDistance
|
2024-05-29 16:02:26 +03:00
|
|
|
replyBuffer []*enode.Node
|
2019-10-29 18:08:57 +03:00
|
|
|
queries int
|
|
|
|
}
|
|
|
|
|
2024-05-29 16:02:26 +03:00
|
|
|
type queryFunc func(*enode.Node) ([]*enode.Node, error)
|
2019-10-29 18:08:57 +03:00
|
|
|
|
|
|
|
func newLookup(ctx context.Context, tab *Table, target enode.ID, q queryFunc) *lookup {
|
|
|
|
it := &lookup{
|
|
|
|
tab: tab,
|
|
|
|
queryfunc: q,
|
|
|
|
asked: make(map[enode.ID]bool),
|
|
|
|
seen: make(map[enode.ID]bool),
|
|
|
|
result: nodesByDistance{target: target},
|
2024-05-29 16:02:26 +03:00
|
|
|
replyCh: make(chan []*enode.Node, alpha),
|
2019-10-29 18:08:57 +03:00
|
|
|
cancelCh: ctx.Done(),
|
|
|
|
queries: -1,
|
|
|
|
}
|
|
|
|
// Don't query further if we hit ourself.
|
|
|
|
// Unlikely to happen often in practice.
|
|
|
|
it.asked[tab.self().ID()] = true
|
|
|
|
return it
|
|
|
|
}
|
|
|
|
|
|
|
|
// run runs the lookup to completion and returns the closest nodes found.
|
|
|
|
func (it *lookup) run() []*enode.Node {
|
|
|
|
for it.advance() {
|
|
|
|
}
|
2024-05-29 16:02:26 +03:00
|
|
|
return it.result.entries
|
2019-10-29 18:08:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// advance advances the lookup until any new nodes have been found.
|
|
|
|
// It returns false when the lookup has ended.
|
|
|
|
func (it *lookup) advance() bool {
|
|
|
|
for it.startQueries() {
|
|
|
|
select {
|
|
|
|
case nodes := <-it.replyCh:
|
|
|
|
it.replyBuffer = it.replyBuffer[:0]
|
|
|
|
for _, n := range nodes {
|
|
|
|
if n != nil && !it.seen[n.ID()] {
|
|
|
|
it.seen[n.ID()] = true
|
|
|
|
it.result.push(n, bucketSize)
|
|
|
|
it.replyBuffer = append(it.replyBuffer, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
it.queries--
|
|
|
|
if len(it.replyBuffer) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
case <-it.cancelCh:
|
|
|
|
it.shutdown()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *lookup) shutdown() {
|
|
|
|
for it.queries > 0 {
|
|
|
|
<-it.replyCh
|
|
|
|
it.queries--
|
|
|
|
}
|
|
|
|
it.queryfunc = nil
|
|
|
|
it.replyBuffer = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *lookup) startQueries() bool {
|
|
|
|
if it.queryfunc == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first query returns nodes from the local table.
|
|
|
|
if it.queries == -1 {
|
2020-08-24 15:42:39 +03:00
|
|
|
closest := it.tab.findnodeByID(it.result.target, bucketSize, false)
|
2019-11-26 13:14:43 +03:00
|
|
|
// Avoid finishing the lookup too quickly if table is empty. It'd be better to wait
|
|
|
|
// for the table to fill in this case, but there is no good mechanism for that
|
|
|
|
// yet.
|
|
|
|
if len(closest.entries) == 0 {
|
|
|
|
it.slowdown()
|
|
|
|
}
|
2019-10-29 18:08:57 +03:00
|
|
|
it.queries = 1
|
|
|
|
it.replyCh <- closest.entries
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask the closest nodes that we haven't asked yet.
|
|
|
|
for i := 0; i < len(it.result.entries) && it.queries < alpha; i++ {
|
|
|
|
n := it.result.entries[i]
|
|
|
|
if !it.asked[n.ID()] {
|
|
|
|
it.asked[n.ID()] = true
|
|
|
|
it.queries++
|
|
|
|
go it.query(n, it.replyCh)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The lookup ends when no more nodes can be asked.
|
|
|
|
return it.queries > 0
|
|
|
|
}
|
|
|
|
|
2019-11-26 13:14:43 +03:00
|
|
|
func (it *lookup) slowdown() {
|
|
|
|
sleep := time.NewTimer(1 * time.Second)
|
|
|
|
defer sleep.Stop()
|
|
|
|
select {
|
|
|
|
case <-sleep.C:
|
|
|
|
case <-it.tab.closeReq:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 16:02:26 +03:00
|
|
|
func (it *lookup) query(n *enode.Node, reply chan<- []*enode.Node) {
|
2019-10-29 18:08:57 +03:00
|
|
|
r, err := it.queryfunc(n)
|
p2p/discover: improved node revalidation (#29572)
Node discovery periodically revalidates the nodes in its table by sending PING, checking
if they are still alive. I recently noticed some issues with the implementation of this
process, which can cause strange results such as nodes dropping unexpectedly, certain
nodes not getting revalidated often enough, and bad results being returned to incoming
FINDNODE queries.
In this change, the revalidation process is improved with the following logic:
- We maintain two 'revalidation lists' containing the table nodes, named 'fast' and 'slow'.
- The process chooses random nodes from each list on a randomized interval, the interval being
faster for the 'fast' list, and performs revalidation for the chosen node.
- Whenever a node is newly inserted into the table, it goes into the 'fast' list.
Once validation passes, it transfers to the 'slow' list. If a request fails, or the
node changes endpoint, it transfers back into 'fast'.
- livenessChecks is incremented by one for successful checks. Unlike the old implementation,
we will not drop the node on the first failing check. We instead quickly decay the
livenessChecks give it another chance.
- Order of nodes in bucket doesn't matter anymore.
I am also adding a debug API endpoint to dump the node table content.
Co-authored-by: Martin HS <martin@swende.se>
2024-05-23 15:26:09 +03:00
|
|
|
if !errors.Is(err, errClosed) { // avoid recording failures on shutdown.
|
|
|
|
success := len(r) > 0
|
|
|
|
it.tab.trackRequest(n, success, r)
|
|
|
|
if err != nil {
|
|
|
|
it.tab.log.Trace("FINDNODE failed", "id", n.ID(), "err", err)
|
2019-10-29 18:08:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
reply <- r
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookupIterator performs lookup operations and iterates over all seen nodes.
|
|
|
|
// When a lookup finishes, a new one is created through nextLookup.
|
|
|
|
type lookupIterator struct {
|
2024-05-29 16:02:26 +03:00
|
|
|
buffer []*enode.Node
|
2019-10-29 18:08:57 +03:00
|
|
|
nextLookup lookupFunc
|
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
|
|
|
lookup *lookup
|
|
|
|
}
|
|
|
|
|
|
|
|
type lookupFunc func(ctx context.Context) *lookup
|
|
|
|
|
|
|
|
func newLookupIterator(ctx context.Context, next lookupFunc) *lookupIterator {
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
return &lookupIterator{ctx: ctx, cancel: cancel, nextLookup: next}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Node returns the current node.
|
|
|
|
func (it *lookupIterator) Node() *enode.Node {
|
|
|
|
if len(it.buffer) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-29 16:02:26 +03:00
|
|
|
return it.buffer[0]
|
2019-10-29 18:08:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next moves to the next node.
|
|
|
|
func (it *lookupIterator) Next() bool {
|
|
|
|
// Consume next node in buffer.
|
|
|
|
if len(it.buffer) > 0 {
|
|
|
|
it.buffer = it.buffer[1:]
|
|
|
|
}
|
|
|
|
// Advance the lookup to refill the buffer.
|
|
|
|
for len(it.buffer) == 0 {
|
|
|
|
if it.ctx.Err() != nil {
|
|
|
|
it.lookup = nil
|
|
|
|
it.buffer = nil
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if it.lookup == nil {
|
|
|
|
it.lookup = it.nextLookup(it.ctx)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !it.lookup.advance() {
|
|
|
|
it.lookup = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
it.buffer = it.lookup.replyBuffer
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close ends the iterator.
|
|
|
|
func (it *lookupIterator) Close() {
|
|
|
|
it.cancel()
|
|
|
|
}
|