2019-07-22 12:17:27 +03:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
2015-07-22 18:48:40 +02:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 02:54:22 +02:00
|
|
|
//
|
2015-07-23 18:35:11 +02:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 02:54:22 +02:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 18:48:40 +02:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 02:54:22 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 18:48:40 +02:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 02:54:22 +02:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 18:48:40 +02:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 02:54:22 +02:00
|
|
|
|
2015-01-27 14:33:26 +01:00
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-08-08 02:49:28 +02:00
|
|
|
"container/list"
|
2019-10-29 16:08:57 +01:00
|
|
|
"context"
|
2015-01-27 14:33:26 +01:00
|
|
|
"crypto/ecdsa"
|
2019-04-30 13:13:22 +02:00
|
|
|
crand "crypto/rand"
|
2015-01-27 14:33:26 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-04-30 13:13:22 +02:00
|
|
|
"io"
|
2024-05-29 15:02:26 +02:00
|
|
|
"net/netip"
|
2018-10-12 11:47:24 +02:00
|
|
|
"sync"
|
2015-01-27 14:33:26 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2017-02-22 14:10:07 +02:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2020-06-02 13:20:19 +02:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover/v4wire"
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 00:59:00 +02:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2016-11-21 18:11:54 +01:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/netutil"
|
2015-01-27 14:33:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Errors
|
|
|
|
var (
|
2015-03-25 16:45:53 +01:00
|
|
|
errExpired = errors.New("expired")
|
|
|
|
errUnsolicitedReply = errors.New("unsolicited reply")
|
|
|
|
errUnknownNode = errors.New("unknown node")
|
|
|
|
errTimeout = errors.New("RPC timeout")
|
2015-08-08 02:49:28 +02:00
|
|
|
errClockWarp = errors.New("reply deadline too far in the future")
|
2015-03-25 16:45:53 +01:00
|
|
|
errClosed = errors.New("socket closed")
|
2020-04-08 09:57:23 +02:00
|
|
|
errLowPort = errors.New("low port")
|
2024-05-29 15:02:26 +02:00
|
|
|
errNoUDPEndpoint = errors.New("node has no UDP endpoint")
|
2015-01-27 14:33:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 00:59:00 +02:00
|
|
|
respTimeout = 500 * time.Millisecond
|
|
|
|
expiration = 20 * time.Second
|
|
|
|
bondExpiration = 24 * time.Hour
|
2016-02-15 20:31:58 +02:00
|
|
|
|
2019-05-15 06:47:45 +02:00
|
|
|
maxFindnodeFailures = 5 // nodes exceeding this limit are dropped
|
2016-02-19 16:18:55 +02:00
|
|
|
ntpFailureThreshold = 32 // Continuous timeouts after which to check NTP
|
|
|
|
ntpWarningCooldown = 10 * time.Minute // Minimum amount of time to pass before repeating NTP warning
|
|
|
|
driftThreshold = 10 * time.Second // Allowed clock drift before warning user
|
2019-02-19 03:27:29 -08:00
|
|
|
|
|
|
|
// Discovery packets are defined to be no larger than 1280 bytes.
|
|
|
|
// Packets larger than this size will be cut at the end and treated
|
|
|
|
// as invalid because their hash won't match.
|
|
|
|
maxPacketSize = 1280
|
2015-01-27 14:33:26 +01:00
|
|
|
)
|
|
|
|
|
2019-04-30 13:13:22 +02:00
|
|
|
// UDPv4 implements the v4 wire protocol.
|
|
|
|
type UDPv4 struct {
|
|
|
|
conn UDPConn
|
|
|
|
log log.Logger
|
2016-11-22 20:51:59 +01:00
|
|
|
netrestrict *netutil.Netlist
|
2015-04-18 01:50:31 +02:00
|
|
|
priv *ecdsa.PrivateKey
|
2018-10-12 11:47:24 +02:00
|
|
|
localNode *enode.LocalNode
|
|
|
|
db *enode.DB
|
|
|
|
tab *Table
|
2019-04-30 13:13:22 +02:00
|
|
|
closeOnce sync.Once
|
2018-10-12 11:47:24 +02:00
|
|
|
wg sync.WaitGroup
|
2015-03-25 16:45:53 +01:00
|
|
|
|
2019-01-29 17:39:20 +01:00
|
|
|
addReplyMatcher chan *replyMatcher
|
|
|
|
gotreply chan reply
|
2019-10-29 16:08:57 +01:00
|
|
|
closeCtx context.Context
|
2020-04-08 09:57:23 +02:00
|
|
|
cancelCloseCtx context.CancelFunc
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
|
|
|
|
2019-05-15 06:47:45 +02:00
|
|
|
// replyMatcher represents a pending reply.
|
2015-01-27 14:33:26 +01:00
|
|
|
//
|
2019-01-29 17:39:20 +01:00
|
|
|
// Some implementations of the protocol wish to send more than one
|
|
|
|
// reply packet to findnode. In general, any neighbors packet cannot
|
2015-01-27 14:33:26 +01:00
|
|
|
// be matched up with a specific findnode packet.
|
|
|
|
//
|
2019-01-29 17:39:20 +01:00
|
|
|
// Our implementation handles this by storing a callback function for
|
|
|
|
// each pending reply. Incoming packets from a node are dispatched
|
|
|
|
// to all callback functions for that node.
|
|
|
|
type replyMatcher struct {
|
2015-01-27 14:33:26 +01:00
|
|
|
// these fields must match in the reply.
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 00:59:00 +02:00
|
|
|
from enode.ID
|
2024-05-29 15:02:26 +02:00
|
|
|
ip netip.Addr
|
2015-01-27 14:33:26 +01:00
|
|
|
ptype byte
|
|
|
|
|
|
|
|
// time when the request must complete
|
|
|
|
deadline time.Time
|
|
|
|
|
2019-01-29 17:39:20 +01:00
|
|
|
// callback is called when a matching reply arrives. If it returns matched == true, the
|
|
|
|
// reply was acceptable. The second return value indicates whether the callback should
|
|
|
|
// be removed from the pending reply queue. If it returns false, the reply is considered
|
|
|
|
// incomplete and the callback will be invoked again for the next matching reply.
|
|
|
|
callback replyMatchFunc
|
2015-01-27 14:33:26 +01:00
|
|
|
|
|
|
|
// errc receives nil when the callback indicates completion or an
|
|
|
|
// error if no further reply is received within the timeout.
|
2019-05-15 06:47:45 +02:00
|
|
|
errc chan error
|
|
|
|
|
|
|
|
// reply contains the most recent reply. This field is safe for reading after errc has
|
|
|
|
// received a value.
|
2020-06-02 13:20:19 +02:00
|
|
|
reply v4wire.Packet
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 13:20:19 +02:00
|
|
|
type replyMatchFunc func(v4wire.Packet) (matched bool, requestDone bool)
|
2019-01-29 17:39:20 +01:00
|
|
|
|
2019-05-15 06:47:45 +02:00
|
|
|
// reply is a reply packet from a certain node.
|
2015-01-27 14:33:26 +01:00
|
|
|
type reply struct {
|
2019-05-15 06:47:45 +02:00
|
|
|
from enode.ID
|
2024-05-29 15:02:26 +02:00
|
|
|
ip netip.Addr
|
2020-06-02 13:20:19 +02:00
|
|
|
data v4wire.Packet
|
2015-03-25 16:45:53 +01:00
|
|
|
// loop indicates whether there was
|
|
|
|
// a matching request by sending on this channel.
|
|
|
|
matched chan<- bool
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
|
|
|
|
2019-04-30 13:13:22 +02:00
|
|
|
func ListenV4(c UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv4, error) {
|
2020-04-08 09:57:23 +02:00
|
|
|
cfg = cfg.withDefaults()
|
2019-10-29 16:08:57 +01:00
|
|
|
closeCtx, cancel := context.WithCancel(context.Background())
|
2019-04-30 13:13:22 +02:00
|
|
|
t := &UDPv4{
|
2023-04-25 12:12:34 +02:00
|
|
|
conn: newMeteredConn(c),
|
2019-01-29 17:39:20 +01:00
|
|
|
priv: cfg.PrivateKey,
|
|
|
|
netrestrict: cfg.NetRestrict,
|
|
|
|
localNode: ln,
|
|
|
|
db: ln.Database(),
|
|
|
|
gotreply: make(chan reply),
|
|
|
|
addReplyMatcher: make(chan *replyMatcher),
|
2019-10-29 16:08:57 +01:00
|
|
|
closeCtx: closeCtx,
|
|
|
|
cancelCloseCtx: cancel,
|
2019-04-30 13:13:22 +02:00
|
|
|
log: cfg.Log,
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
2019-10-29 16:08:57 +01:00
|
|
|
|
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 14:26:09 +02:00
|
|
|
tab, err := newTable(t, ln.Database(), cfg)
|
2015-11-05 23:57:57 +02:00
|
|
|
if err != nil {
|
2019-04-30 13:13:22 +02:00
|
|
|
return nil, err
|
2015-11-05 23:57:57 +02:00
|
|
|
}
|
2019-04-30 13:13:22 +02:00
|
|
|
t.tab = tab
|
2019-05-13 11:25:54 +02:00
|
|
|
go tab.loop()
|
2015-11-05 23:57:57 +02:00
|
|
|
|
2019-04-30 13:13:22 +02:00
|
|
|
t.wg.Add(2)
|
|
|
|
go t.loop()
|
|
|
|
go t.readLoop(cfg.Unhandled)
|
|
|
|
return t, nil
|
2018-10-12 11:47:24 +02:00
|
|
|
}
|
|
|
|
|
2019-04-30 13:13:22 +02:00
|
|
|
// Self returns the local node.
|
|
|
|
func (t *UDPv4) Self() *enode.Node {
|
2018-10-12 11:47:24 +02:00
|
|
|
return t.localNode.Node()
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
|
|
|
|
2019-04-30 13:13:22 +02:00
|
|
|
// Close shuts down the socket and aborts any running queries.
|
|
|
|
func (t *UDPv4) Close() {
|
|
|
|
t.closeOnce.Do(func() {
|
2019-10-29 16:08:57 +01:00
|
|
|
t.cancelCloseCtx()
|
2019-04-30 13:13:22 +02:00
|
|
|
t.conn.Close()
|
|
|
|
t.wg.Wait()
|
|
|
|
t.tab.close()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-15 06:47:45 +02:00
|
|
|
// Resolve searches for a specific node with the given ID and tries to get the most recent
|
|
|
|
// version of the node record for it. It returns n if the node could not be resolved.
|
2019-04-30 13:13:22 +02:00
|
|
|
func (t *UDPv4) Resolve(n *enode.Node) *enode.Node {
|
2019-05-15 06:47:45 +02:00
|
|
|
// Try asking directly. This works if the node is still responding on the endpoint we have.
|
2019-06-07 15:29:16 +02:00
|
|
|
if rn, err := t.RequestENR(n); err == nil {
|
2019-05-15 06:47:45 +02:00
|
|
|
return rn
|
|
|
|
}
|
|
|
|
// Check table for the ID, we might have a newer version there.
|
|
|
|
if intable := t.tab.getNode(n.ID()); intable != nil && intable.Seq() > n.Seq() {
|
|
|
|
n = intable
|
2019-06-07 15:29:16 +02:00
|
|
|
if rn, err := t.RequestENR(n); err == nil {
|
2019-05-15 06:47:45 +02:00
|
|
|
return rn
|
2019-04-30 13:13:22 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-15 06:47:45 +02:00
|
|
|
// Otherwise perform a network lookup.
|
2019-05-15 11:11:17 -04:00
|
|
|
var key enode.Secp256k1
|
|
|
|
if n.Load(&key) != nil {
|
2019-05-15 06:47:45 +02:00
|
|
|
return n // no secp256k1 key
|
|
|
|
}
|
2019-05-15 11:11:17 -04:00
|
|
|
result := t.LookupPubkey((*ecdsa.PublicKey)(&key))
|
2019-05-15 06:47:45 +02:00
|
|
|
for _, rn := range result {
|
|
|
|
if rn.ID() == n.ID() {
|
2019-06-07 15:29:16 +02:00
|
|
|
if rn, err := t.RequestENR(rn); err == nil {
|
2019-05-15 06:47:45 +02:00
|
|
|
return rn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n
|
2018-10-12 11:47:24 +02:00
|
|
|
}
|
|
|
|
|
2020-06-02 13:20:19 +02:00
|
|
|
func (t *UDPv4) ourEndpoint() v4wire.Endpoint {
|
2024-05-29 15:02:26 +02:00
|
|
|
node := t.Self()
|
|
|
|
addr, ok := node.UDPEndpoint()
|
|
|
|
if !ok {
|
|
|
|
return v4wire.Endpoint{}
|
|
|
|
}
|
|
|
|
return v4wire.NewEndpoint(addr, uint16(node.TCP()))
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
|
|
|
|
2019-06-07 15:29:16 +02:00
|
|
|
// Ping sends a ping message to the given node.
|
|
|
|
func (t *UDPv4) Ping(n *enode.Node) error {
|
|
|
|
_, err := t.ping(n)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-27 14:33:26 +01:00
|
|
|
// ping sends a ping message to the given node and waits for a reply.
|
2019-05-15 06:47:45 +02:00
|
|
|
func (t *UDPv4) ping(n *enode.Node) (seq uint64, err error) {
|
2024-05-29 15:02:26 +02:00
|
|
|
addr, ok := n.UDPEndpoint()
|
|
|
|
if !ok {
|
|
|
|
return 0, errNoUDPEndpoint
|
|
|
|
}
|
|
|
|
rm := t.sendPing(n.ID(), addr, nil)
|
2019-05-15 06:47:45 +02:00
|
|
|
if err = <-rm.errc; err == nil {
|
2021-05-18 11:48:41 +02:00
|
|
|
seq = rm.reply.(*v4wire.Pong).ENRSeq
|
2019-05-15 06:47:45 +02:00
|
|
|
}
|
|
|
|
return seq, err
|
2018-07-03 15:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// sendPing sends a ping message to the given node and invokes the callback
|
|
|
|
// when the reply arrives.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) sendPing(toid enode.ID, toaddr netip.AddrPort, callback func()) *replyMatcher {
|
2019-05-15 06:47:45 +02:00
|
|
|
req := t.makePing(toaddr)
|
2020-06-02 13:20:19 +02:00
|
|
|
packet, hash, err := v4wire.Encode(t.priv, req)
|
2018-02-12 13:36:09 +01:00
|
|
|
if err != nil {
|
2018-07-03 15:24:12 +02:00
|
|
|
errc := make(chan error, 1)
|
|
|
|
errc <- err
|
2019-05-15 06:47:45 +02:00
|
|
|
return &replyMatcher{errc: errc}
|
2018-02-12 13:36:09 +01:00
|
|
|
}
|
2019-01-29 17:39:20 +01:00
|
|
|
// Add a matcher for the reply to the pending reply queue. Pongs are matched if they
|
|
|
|
// reference the ping we're about to send.
|
2024-05-29 15:02:26 +02:00
|
|
|
rm := t.pending(toid, toaddr.Addr(), v4wire.PongPacket, func(p v4wire.Packet) (matched bool, requestDone bool) {
|
2020-06-02 13:20:19 +02:00
|
|
|
matched = bytes.Equal(p.(*v4wire.Pong).ReplyTok, hash)
|
2019-01-29 17:39:20 +01:00
|
|
|
if matched && callback != nil {
|
2018-07-03 15:24:12 +02:00
|
|
|
callback()
|
|
|
|
}
|
2019-01-29 17:39:20 +01:00
|
|
|
return matched, matched
|
2015-01-27 14:33:26 +01:00
|
|
|
})
|
2019-01-29 17:39:20 +01:00
|
|
|
// Send the packet.
|
2024-06-05 19:31:04 +02:00
|
|
|
t.localNode.UDPContact(toaddr)
|
2020-06-02 13:20:19 +02:00
|
|
|
t.write(toaddr, toid, req.Name(), packet)
|
2019-05-15 06:47:45 +02:00
|
|
|
return rm
|
|
|
|
}
|
|
|
|
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) makePing(toaddr netip.AddrPort) *v4wire.Ping {
|
2020-06-02 13:20:19 +02:00
|
|
|
return &v4wire.Ping{
|
2019-05-15 06:47:45 +02:00
|
|
|
Version: 4,
|
|
|
|
From: t.ourEndpoint(),
|
2020-06-02 13:20:19 +02:00
|
|
|
To: v4wire.NewEndpoint(toaddr, 0),
|
2019-05-15 06:47:45 +02:00
|
|
|
Expiration: uint64(time.Now().Add(expiration).Unix()),
|
2021-05-18 11:48:41 +02:00
|
|
|
ENRSeq: t.localNode.Node().Seq(),
|
2019-05-15 06:47:45 +02:00
|
|
|
}
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 16:08:57 +01:00
|
|
|
// LookupPubkey finds the closest nodes to the given public key.
|
|
|
|
func (t *UDPv4) LookupPubkey(key *ecdsa.PublicKey) []*enode.Node {
|
|
|
|
if t.tab.len() == 0 {
|
|
|
|
// All nodes were dropped, refresh. The very first query will hit this
|
|
|
|
// case and run the bootstrapping logic.
|
|
|
|
<-t.tab.refresh()
|
|
|
|
}
|
2024-07-18 11:09:02 +02:00
|
|
|
return t.newLookup(t.closeCtx, v4wire.EncodePubkey(key)).run()
|
2019-10-29 16:08:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RandomNodes is an iterator yielding nodes from a random walk of the DHT.
|
|
|
|
func (t *UDPv4) RandomNodes() enode.Iterator {
|
|
|
|
return newLookupIterator(t.closeCtx, t.newRandomLookup)
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookupRandom implements transport.
|
|
|
|
func (t *UDPv4) lookupRandom() []*enode.Node {
|
|
|
|
return t.newRandomLookup(t.closeCtx).run()
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookupSelf implements transport.
|
|
|
|
func (t *UDPv4) lookupSelf() []*enode.Node {
|
2024-07-18 11:09:02 +02:00
|
|
|
pubkey := v4wire.EncodePubkey(&t.priv.PublicKey)
|
|
|
|
return t.newLookup(t.closeCtx, pubkey).run()
|
2019-10-29 16:08:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *UDPv4) newRandomLookup(ctx context.Context) *lookup {
|
2024-07-18 11:09:02 +02:00
|
|
|
var target v4wire.Pubkey
|
2019-10-29 16:08:57 +01:00
|
|
|
crand.Read(target[:])
|
|
|
|
return t.newLookup(ctx, target)
|
|
|
|
}
|
|
|
|
|
2024-07-18 11:09:02 +02:00
|
|
|
func (t *UDPv4) newLookup(ctx context.Context, targetKey v4wire.Pubkey) *lookup {
|
2019-10-29 16:08:57 +01:00
|
|
|
target := enode.ID(crypto.Keccak256Hash(targetKey[:]))
|
2024-05-29 15:02:26 +02:00
|
|
|
it := newLookup(ctx, t.tab, target, func(n *enode.Node) ([]*enode.Node, error) {
|
|
|
|
addr, ok := n.UDPEndpoint()
|
|
|
|
if !ok {
|
|
|
|
return nil, errNoUDPEndpoint
|
|
|
|
}
|
2024-07-18 11:09:02 +02:00
|
|
|
return t.findnode(n.ID(), addr, targetKey)
|
2019-10-29 16:08:57 +01:00
|
|
|
})
|
|
|
|
return it
|
|
|
|
}
|
|
|
|
|
2015-01-27 14:33:26 +01:00
|
|
|
// findnode sends a findnode request to the given node and waits until
|
|
|
|
// the node has sent up to k neighbors.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) findnode(toid enode.ID, toAddrPort netip.AddrPort, target v4wire.Pubkey) ([]*enode.Node, error) {
|
|
|
|
t.ensureBond(toid, toAddrPort)
|
2018-07-03 15:24:12 +02:00
|
|
|
|
2019-01-29 17:39:20 +01:00
|
|
|
// Add a matcher for 'neighbours' replies to the pending reply queue. The matcher is
|
|
|
|
// active until enough nodes have been received.
|
2024-05-29 15:02:26 +02:00
|
|
|
nodes := make([]*enode.Node, 0, bucketSize)
|
2015-01-27 14:33:26 +01:00
|
|
|
nreceived := 0
|
2024-05-29 15:02:26 +02:00
|
|
|
rm := t.pending(toid, toAddrPort.Addr(), v4wire.NeighborsPacket, func(r v4wire.Packet) (matched bool, requestDone bool) {
|
2020-06-02 13:20:19 +02:00
|
|
|
reply := r.(*v4wire.Neighbors)
|
2015-04-23 12:11:21 +02:00
|
|
|
for _, rn := range reply.Nodes {
|
2015-01-27 14:33:26 +01:00
|
|
|
nreceived++
|
2024-05-29 15:02:26 +02:00
|
|
|
n, err := t.nodeFromRPC(toAddrPort, rn)
|
2016-11-21 18:11:54 +01:00
|
|
|
if err != nil {
|
2024-05-29 15:02:26 +02:00
|
|
|
t.log.Trace("Invalid neighbor node received", "ip", rn.IP, "addr", toAddrPort, "err", err)
|
2016-11-21 18:11:54 +01:00
|
|
|
continue
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
2016-11-21 18:11:54 +01:00
|
|
|
nodes = append(nodes, n)
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
2019-01-29 17:39:20 +01:00
|
|
|
return true, nreceived >= bucketSize
|
2015-01-27 14:33:26 +01:00
|
|
|
})
|
2024-05-29 15:02:26 +02:00
|
|
|
t.send(toAddrPort, toid, &v4wire.Findnode{
|
2015-01-27 14:33:26 +01:00
|
|
|
Target: target,
|
|
|
|
Expiration: uint64(time.Now().Add(expiration).Unix()),
|
|
|
|
})
|
2020-08-24 14:42:39 +02:00
|
|
|
// Ensure that callers don't see a timeout if the node actually responded. Since
|
|
|
|
// findnode can receive more than one neighbors response, the reply matcher will be
|
|
|
|
// active until the remote node sends enough nodes. If the remote end doesn't have
|
|
|
|
// enough nodes the reply matcher will time out waiting for the second reply, but
|
|
|
|
// there's no need for an error in that case.
|
|
|
|
err := <-rm.errc
|
2022-06-07 17:27:21 +02:00
|
|
|
if errors.Is(err, errTimeout) && rm.reply != nil {
|
2020-08-24 14:42:39 +02:00
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return nodes, err
|
2019-05-15 06:47:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-10 17:15:54 +09:00
|
|
|
// RequestENR sends ENRRequest to the given node and waits for a response.
|
2019-06-07 15:29:16 +02:00
|
|
|
func (t *UDPv4) RequestENR(n *enode.Node) (*enode.Node, error) {
|
2024-05-29 15:02:26 +02:00
|
|
|
addr, _ := n.UDPEndpoint()
|
2019-05-15 06:47:45 +02:00
|
|
|
t.ensureBond(n.ID(), addr)
|
|
|
|
|
2020-06-02 13:20:19 +02:00
|
|
|
req := &v4wire.ENRRequest{
|
2019-05-15 06:47:45 +02:00
|
|
|
Expiration: uint64(time.Now().Add(expiration).Unix()),
|
|
|
|
}
|
2020-06-02 13:20:19 +02:00
|
|
|
packet, hash, err := v4wire.Encode(t.priv, req)
|
2019-05-15 06:47:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-02 13:20:19 +02:00
|
|
|
|
2019-05-15 06:47:45 +02:00
|
|
|
// Add a matcher for the reply to the pending reply queue. Responses are matched if
|
|
|
|
// they reference the request we're about to send.
|
2024-05-29 15:02:26 +02:00
|
|
|
rm := t.pending(n.ID(), addr.Addr(), v4wire.ENRResponsePacket, func(r v4wire.Packet) (matched bool, requestDone bool) {
|
2020-06-02 13:20:19 +02:00
|
|
|
matched = bytes.Equal(r.(*v4wire.ENRResponse).ReplyTok, hash)
|
2019-05-15 06:47:45 +02:00
|
|
|
return matched, matched
|
|
|
|
})
|
|
|
|
// Send the packet and wait for the reply.
|
2020-06-02 13:20:19 +02:00
|
|
|
t.write(addr, n.ID(), req.Name(), packet)
|
2019-05-15 06:47:45 +02:00
|
|
|
if err := <-rm.errc; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Verify the response record.
|
2020-06-02 13:20:19 +02:00
|
|
|
respN, err := enode.New(enode.ValidSchemes, &rm.reply.(*v4wire.ENRResponse).Record)
|
2019-05-15 06:47:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if respN.ID() != n.ID() {
|
2024-02-29 17:56:46 +08:00
|
|
|
return nil, errors.New("invalid ID in response record")
|
2019-05-15 06:47:45 +02:00
|
|
|
}
|
|
|
|
if respN.Seq() < n.Seq() {
|
|
|
|
return n, nil // response record is older
|
|
|
|
}
|
2024-06-05 19:31:04 +02:00
|
|
|
if err := netutil.CheckRelayAddr(addr.Addr(), respN.IPAddr()); err != nil {
|
2019-05-15 06:47:45 +02:00
|
|
|
return nil, fmt.Errorf("invalid IP in response record: %v", err)
|
|
|
|
}
|
|
|
|
return respN, nil
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
|
|
|
|
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 14:26:09 +02:00
|
|
|
func (t *UDPv4) TableBuckets() [][]BucketNode {
|
|
|
|
return t.tab.Nodes()
|
|
|
|
}
|
|
|
|
|
2019-01-29 17:39:20 +01:00
|
|
|
// pending adds a reply matcher to the pending reply queue.
|
|
|
|
// see the documentation of type replyMatcher for a detailed explanation.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) pending(id enode.ID, ip netip.Addr, ptype byte, callback replyMatchFunc) *replyMatcher {
|
2015-01-27 14:33:26 +01:00
|
|
|
ch := make(chan error, 1)
|
2019-01-29 17:39:20 +01:00
|
|
|
p := &replyMatcher{from: id, ip: ip, ptype: ptype, callback: callback, errc: ch}
|
2015-01-27 14:33:26 +01:00
|
|
|
select {
|
2019-01-29 17:39:20 +01:00
|
|
|
case t.addReplyMatcher <- p:
|
2015-01-27 14:33:26 +01:00
|
|
|
// loop will handle it
|
2019-10-29 16:08:57 +01:00
|
|
|
case <-t.closeCtx.Done():
|
2015-01-27 14:33:26 +01:00
|
|
|
ch <- errClosed
|
|
|
|
}
|
2019-05-15 06:47:45 +02:00
|
|
|
return p
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
|
|
|
|
2019-01-29 17:39:20 +01:00
|
|
|
// handleReply dispatches a reply packet, invoking reply matchers. It returns
|
|
|
|
// whether any matcher considered the packet acceptable.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) handleReply(from enode.ID, fromIP netip.Addr, req v4wire.Packet) bool {
|
2015-08-08 02:49:28 +02:00
|
|
|
matched := make(chan bool, 1)
|
2015-03-25 16:45:53 +01:00
|
|
|
select {
|
2019-05-15 06:47:45 +02:00
|
|
|
case t.gotreply <- reply{from, fromIP, req, matched}:
|
2015-03-25 16:45:53 +01:00
|
|
|
// loop will handle it
|
|
|
|
return <-matched
|
2019-10-29 16:08:57 +01:00
|
|
|
case <-t.closeCtx.Done():
|
2015-03-25 16:45:53 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 20:31:58 +02:00
|
|
|
// loop runs in its own goroutine. it keeps track of
|
2015-01-27 14:33:26 +01:00
|
|
|
// the refresh timer and the pending reply queue.
|
2019-04-30 13:13:22 +02:00
|
|
|
func (t *UDPv4) loop() {
|
2018-10-12 11:47:24 +02:00
|
|
|
defer t.wg.Done()
|
|
|
|
|
2015-01-27 14:33:26 +01:00
|
|
|
var (
|
2016-02-15 20:31:58 +02:00
|
|
|
plist = list.New()
|
|
|
|
timeout = time.NewTimer(0)
|
2019-01-29 17:39:20 +01:00
|
|
|
nextTimeout *replyMatcher // head of plist when timeout was last reset
|
|
|
|
contTimeouts = 0 // number of continuous timeouts to do NTP checks
|
2016-02-19 16:18:55 +02:00
|
|
|
ntpWarnTime = time.Unix(0, 0)
|
2015-01-27 14:33:26 +01:00
|
|
|
)
|
|
|
|
<-timeout.C // ignore first timeout
|
|
|
|
defer timeout.Stop()
|
|
|
|
|
2015-08-08 02:49:28 +02:00
|
|
|
resetTimeout := func() {
|
|
|
|
if plist.Front() == nil || nextTimeout == plist.Front().Value {
|
2015-01-27 14:33:26 +01:00
|
|
|
return
|
|
|
|
}
|
2015-08-08 02:49:28 +02:00
|
|
|
// Start the timer so it fires when the next pending reply has expired.
|
|
|
|
now := time.Now()
|
|
|
|
for el := plist.Front(); el != nil; el = el.Next() {
|
2019-01-29 17:39:20 +01:00
|
|
|
nextTimeout = el.Value.(*replyMatcher)
|
2015-08-08 02:49:28 +02:00
|
|
|
if dist := nextTimeout.deadline.Sub(now); dist < 2*respTimeout {
|
|
|
|
timeout.Reset(dist)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Remove pending replies whose deadline is too far in the
|
|
|
|
// future. These can occur if the system clock jumped
|
|
|
|
// backwards after the deadline was assigned.
|
|
|
|
nextTimeout.errc <- errClockWarp
|
|
|
|
plist.Remove(el)
|
|
|
|
}
|
|
|
|
nextTimeout = nil
|
|
|
|
timeout.Stop()
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2015-08-08 02:49:28 +02:00
|
|
|
resetTimeout()
|
|
|
|
|
2015-01-27 14:33:26 +01:00
|
|
|
select {
|
2019-10-29 16:08:57 +01:00
|
|
|
case <-t.closeCtx.Done():
|
2015-08-08 02:49:28 +02:00
|
|
|
for el := plist.Front(); el != nil; el = el.Next() {
|
2019-01-29 17:39:20 +01:00
|
|
|
el.Value.(*replyMatcher).errc <- errClosed
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
|
2019-01-29 17:39:20 +01:00
|
|
|
case p := <-t.addReplyMatcher:
|
2015-01-27 14:33:26 +01:00
|
|
|
p.deadline = time.Now().Add(respTimeout)
|
2015-08-08 02:49:28 +02:00
|
|
|
plist.PushBack(p)
|
2015-01-27 14:33:26 +01:00
|
|
|
|
2015-03-25 16:45:53 +01:00
|
|
|
case r := <-t.gotreply:
|
2019-01-29 17:39:20 +01:00
|
|
|
var matched bool // whether any replyMatcher considered the reply acceptable.
|
2015-08-08 02:49:28 +02:00
|
|
|
for el := plist.Front(); el != nil; el = el.Next() {
|
2019-01-29 17:39:20 +01:00
|
|
|
p := el.Value.(*replyMatcher)
|
2024-05-29 15:02:26 +02:00
|
|
|
if p.from == r.from && p.ptype == r.data.Kind() && p.ip == r.ip {
|
2019-01-29 17:39:20 +01:00
|
|
|
ok, requestDone := p.callback(r.data)
|
|
|
|
matched = matched || ok
|
2020-08-24 14:42:39 +02:00
|
|
|
p.reply = r.data
|
2019-01-29 17:39:20 +01:00
|
|
|
// Remove the matcher if callback indicates that all replies have been received.
|
|
|
|
if requestDone {
|
2015-03-25 16:45:53 +01:00
|
|
|
p.errc <- nil
|
2015-08-08 02:49:28 +02:00
|
|
|
plist.Remove(el)
|
2015-03-25 16:45:53 +01:00
|
|
|
}
|
2016-02-15 20:31:58 +02:00
|
|
|
// Reset the continuous timeout counter (time drift detection)
|
|
|
|
contTimeouts = 0
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
|
|
|
}
|
2015-03-25 16:45:53 +01:00
|
|
|
r.matched <- matched
|
2015-01-27 14:33:26 +01:00
|
|
|
|
|
|
|
case now := <-timeout.C:
|
2015-08-08 02:49:28 +02:00
|
|
|
nextTimeout = nil
|
2016-02-15 20:31:58 +02:00
|
|
|
|
2015-08-08 02:49:28 +02:00
|
|
|
// Notify and remove callbacks whose deadline is in the past.
|
|
|
|
for el := plist.Front(); el != nil; el = el.Next() {
|
2019-01-29 17:39:20 +01:00
|
|
|
p := el.Value.(*replyMatcher)
|
2015-08-08 02:49:28 +02:00
|
|
|
if now.After(p.deadline) || now.Equal(p.deadline) {
|
|
|
|
p.errc <- errTimeout
|
|
|
|
plist.Remove(el)
|
2016-02-15 20:31:58 +02:00
|
|
|
contTimeouts++
|
2015-08-08 02:49:28 +02:00
|
|
|
}
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
2016-02-15 20:31:58 +02:00
|
|
|
// If we've accumulated too many timeouts, do an NTP time sync check
|
2016-02-19 16:18:55 +02:00
|
|
|
if contTimeouts > ntpFailureThreshold {
|
|
|
|
if time.Since(ntpWarnTime) >= ntpWarningCooldown {
|
|
|
|
ntpWarnTime = time.Now()
|
|
|
|
go checkClockDrift()
|
|
|
|
}
|
2016-02-15 20:31:58 +02:00
|
|
|
contTimeouts = 0
|
|
|
|
}
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) send(toaddr netip.AddrPort, toid enode.ID, req v4wire.Packet) ([]byte, error) {
|
2020-06-02 13:20:19 +02:00
|
|
|
packet, hash, err := v4wire.Encode(t.priv, req)
|
2015-03-25 16:45:53 +01:00
|
|
|
if err != nil {
|
2018-02-12 13:36:09 +01:00
|
|
|
return hash, err
|
2015-03-25 16:45:53 +01:00
|
|
|
}
|
2020-06-02 13:20:19 +02:00
|
|
|
return hash, t.write(toaddr, toid, req.Name(), packet)
|
2018-02-12 13:36:09 +01:00
|
|
|
}
|
|
|
|
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) write(toaddr netip.AddrPort, toid enode.ID, what string, packet []byte) error {
|
|
|
|
_, err := t.conn.WriteToUDPAddrPort(packet, toaddr)
|
2019-04-30 13:13:22 +02:00
|
|
|
t.log.Trace(">> "+what, "id", toid, "addr", toaddr, "err", err)
|
2015-03-25 16:45:53 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-27 14:33:26 +01:00
|
|
|
// readLoop runs in its own goroutine. it handles incoming UDP packets.
|
2019-04-30 13:13:22 +02:00
|
|
|
func (t *UDPv4) readLoop(unhandled chan<- ReadPacket) {
|
2018-10-12 11:47:24 +02:00
|
|
|
defer t.wg.Done()
|
2018-01-22 13:38:34 +01:00
|
|
|
if unhandled != nil {
|
|
|
|
defer close(unhandled)
|
|
|
|
}
|
2018-10-12 11:47:24 +02:00
|
|
|
|
2019-02-19 03:27:29 -08:00
|
|
|
buf := make([]byte, maxPacketSize)
|
2015-01-27 14:33:26 +01:00
|
|
|
for {
|
2024-05-29 15:02:26 +02:00
|
|
|
nbytes, from, err := t.conn.ReadFromUDPAddrPort(buf)
|
2016-11-21 18:39:36 +01:00
|
|
|
if netutil.IsTemporaryError(err) {
|
2015-08-19 14:11:12 +02:00
|
|
|
// Ignore temporary read errors.
|
2019-04-30 13:13:22 +02:00
|
|
|
t.log.Debug("Temporary UDP read error", "err", err)
|
2015-08-19 14:11:12 +02:00
|
|
|
continue
|
|
|
|
} else if err != nil {
|
2022-08-19 01:00:21 -05:00
|
|
|
// Shut down the loop for permanent errors.
|
2022-06-07 17:27:21 +02:00
|
|
|
if !errors.Is(err, io.EOF) {
|
2019-04-30 13:13:22 +02:00
|
|
|
t.log.Debug("UDP read error", "err", err)
|
|
|
|
}
|
2015-01-27 14:33:26 +01:00
|
|
|
return
|
|
|
|
}
|
2024-08-20 08:02:54 -06:00
|
|
|
if err := t.handlePacket(from, buf[:nbytes]); err != nil && unhandled == nil {
|
|
|
|
t.log.Debug("Bad discv4 packet", "addr", from, "err", err)
|
|
|
|
} else if err != nil && unhandled != nil {
|
2018-01-22 13:38:34 +01:00
|
|
|
select {
|
|
|
|
case unhandled <- ReadPacket{buf[:nbytes], from}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2015-04-18 01:50:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) handlePacket(from netip.AddrPort, buf []byte) error {
|
2024-06-06 15:15:22 +02:00
|
|
|
// Unwrap IPv4-in-6 source address.
|
|
|
|
if from.Addr().Is4In6() {
|
|
|
|
from = netip.AddrPortFrom(netip.AddrFrom4(from.Addr().As4()), from.Port())
|
|
|
|
}
|
|
|
|
|
2020-06-02 13:20:19 +02:00
|
|
|
rawpacket, fromKey, hash, err := v4wire.Decode(buf)
|
2015-04-18 01:50:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
2020-06-02 13:20:19 +02:00
|
|
|
packet := t.wrapPacket(rawpacket)
|
|
|
|
fromID := fromKey.ID()
|
2024-04-25 14:40:29 +08:00
|
|
|
if packet.preverify != nil {
|
2020-06-02 13:20:19 +02:00
|
|
|
err = packet.preverify(packet, from, fromID, fromKey)
|
2019-01-29 17:39:20 +01:00
|
|
|
}
|
2020-06-02 13:20:19 +02:00
|
|
|
t.log.Trace("<< "+packet.Name(), "id", fromID, "addr", from, "err", err)
|
|
|
|
if err == nil && packet.handle != nil {
|
|
|
|
packet.handle(packet, from, fromID, hash)
|
2019-01-29 17:39:20 +01:00
|
|
|
}
|
2015-04-18 01:50:31 +02:00
|
|
|
return err
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
|
|
|
|
2019-05-15 06:47:45 +02:00
|
|
|
// checkBond checks if the given node has a recent enough endpoint proof.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) checkBond(id enode.ID, ip netip.AddrPort) bool {
|
2024-06-05 19:31:04 +02:00
|
|
|
return time.Since(t.db.LastPongReceived(id, ip.Addr())) < bondExpiration
|
2019-05-15 06:47:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ensureBond solicits a ping from a node if we haven't seen a ping from it for a while.
|
|
|
|
// This ensures there is a valid endpoint proof on the remote end.
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) ensureBond(toid enode.ID, toaddr netip.AddrPort) {
|
2024-06-05 19:31:04 +02:00
|
|
|
tooOld := time.Since(t.db.LastPingReceived(toid, toaddr.Addr())) > bondExpiration
|
|
|
|
if tooOld || t.db.FindFails(toid, toaddr.Addr()) > maxFindnodeFailures {
|
2019-05-15 06:47:45 +02:00
|
|
|
rm := t.sendPing(toid, toaddr, nil)
|
|
|
|
<-rm.errc
|
|
|
|
// Wait for them to ping back and process our pong.
|
|
|
|
time.Sleep(respTimeout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) nodeFromRPC(sender netip.AddrPort, rn v4wire.Node) (*enode.Node, error) {
|
2020-06-02 13:20:19 +02:00
|
|
|
if rn.UDP <= 1024 {
|
|
|
|
return nil, errLowPort
|
|
|
|
}
|
2024-05-29 15:02:26 +02:00
|
|
|
if err := netutil.CheckRelayIP(sender.Addr().AsSlice(), rn.IP); err != nil {
|
2020-06-02 13:20:19 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if t.netrestrict != nil && !t.netrestrict.Contains(rn.IP) {
|
2021-07-29 17:50:18 +02:00
|
|
|
return nil, errors.New("not contained in netrestrict list")
|
2020-06-02 13:20:19 +02:00
|
|
|
}
|
|
|
|
key, err := v4wire.DecodePubkey(crypto.S256(), rn.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-29 15:02:26 +02:00
|
|
|
n := enode.NewV4(key, rn.IP, int(rn.TCP), int(rn.UDP))
|
2020-06-02 13:20:19 +02:00
|
|
|
err = n.ValidateComplete()
|
|
|
|
return n, err
|
2019-05-15 06:47:45 +02:00
|
|
|
}
|
|
|
|
|
2024-05-29 15:02:26 +02:00
|
|
|
func nodeToRPC(n *enode.Node) v4wire.Node {
|
2020-06-02 13:20:19 +02:00
|
|
|
var key ecdsa.PublicKey
|
|
|
|
var ekey v4wire.Pubkey
|
|
|
|
if err := n.Load((*enode.Secp256k1)(&key)); err == nil {
|
|
|
|
ekey = v4wire.EncodePubkey(&key)
|
|
|
|
}
|
|
|
|
return v4wire.Node{ID: ekey, IP: n.IP(), UDP: uint16(n.UDP()), TCP: uint16(n.TCP())}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrapPacket returns the handler functions applicable to a packet.
|
|
|
|
func (t *UDPv4) wrapPacket(p v4wire.Packet) *packetHandlerV4 {
|
|
|
|
var h packetHandlerV4
|
|
|
|
h.Packet = p
|
|
|
|
switch p.(type) {
|
|
|
|
case *v4wire.Ping:
|
|
|
|
h.preverify = t.verifyPing
|
|
|
|
h.handle = t.handlePing
|
|
|
|
case *v4wire.Pong:
|
|
|
|
h.preverify = t.verifyPong
|
|
|
|
case *v4wire.Findnode:
|
|
|
|
h.preverify = t.verifyFindnode
|
|
|
|
h.handle = t.handleFindnode
|
|
|
|
case *v4wire.Neighbors:
|
|
|
|
h.preverify = t.verifyNeighbors
|
|
|
|
case *v4wire.ENRRequest:
|
|
|
|
h.preverify = t.verifyENRRequest
|
|
|
|
h.handle = t.handleENRRequest
|
|
|
|
case *v4wire.ENRResponse:
|
|
|
|
h.preverify = t.verifyENRResponse
|
|
|
|
}
|
|
|
|
return &h
|
|
|
|
}
|
|
|
|
|
|
|
|
// packetHandlerV4 wraps a packet with handler functions.
|
|
|
|
type packetHandlerV4 struct {
|
|
|
|
v4wire.Packet
|
|
|
|
senderKey *ecdsa.PublicKey // used for ping
|
|
|
|
|
|
|
|
// preverify checks whether the packet is valid and should be handled at all.
|
2024-05-29 15:02:26 +02:00
|
|
|
preverify func(p *packetHandlerV4, from netip.AddrPort, fromID enode.ID, fromKey v4wire.Pubkey) error
|
2020-06-02 13:20:19 +02:00
|
|
|
// handle handles the packet.
|
2024-05-29 15:02:26 +02:00
|
|
|
handle func(req *packetHandlerV4, from netip.AddrPort, fromID enode.ID, mac []byte)
|
2019-05-15 06:47:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PING/v4
|
|
|
|
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) verifyPing(h *packetHandlerV4, from netip.AddrPort, fromID enode.ID, fromKey v4wire.Pubkey) error {
|
2020-06-02 13:20:19 +02:00
|
|
|
req := h.Packet.(*v4wire.Ping)
|
2019-01-29 17:39:20 +01:00
|
|
|
|
2023-06-22 10:06:14 +02:00
|
|
|
if v4wire.Expired(req.Expiration) {
|
|
|
|
return errExpired
|
|
|
|
}
|
2020-06-02 13:20:19 +02:00
|
|
|
senderKey, err := v4wire.DecodePubkey(crypto.S256(), fromKey)
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 00:59:00 +02:00
|
|
|
if err != nil {
|
2020-06-02 13:20:19 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
h.senderKey = senderKey
|
2019-01-29 17:39:20 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) handlePing(h *packetHandlerV4, from netip.AddrPort, fromID enode.ID, mac []byte) {
|
2020-06-02 13:20:19 +02:00
|
|
|
req := h.Packet.(*v4wire.Ping)
|
|
|
|
|
2019-01-29 17:39:20 +01:00
|
|
|
// Reply.
|
2020-06-02 13:20:19 +02:00
|
|
|
t.send(from, fromID, &v4wire.Pong{
|
|
|
|
To: v4wire.NewEndpoint(from, req.From.TCP),
|
2015-01-27 14:33:26 +01:00
|
|
|
ReplyTok: mac,
|
|
|
|
Expiration: uint64(time.Now().Add(expiration).Unix()),
|
2021-05-18 11:48:41 +02:00
|
|
|
ENRSeq: t.localNode.Node().Seq(),
|
2015-01-27 14:33:26 +01:00
|
|
|
})
|
2019-01-29 17:39:20 +01:00
|
|
|
|
|
|
|
// Ping back if our last pong on file is too far in the past.
|
2024-05-29 15:02:26 +02:00
|
|
|
fromIP := from.Addr().AsSlice()
|
|
|
|
n := enode.NewV4(h.senderKey, fromIP, int(req.From.TCP), int(from.Port()))
|
2024-06-05 19:31:04 +02:00
|
|
|
if time.Since(t.db.LastPongReceived(n.ID(), from.Addr())) > bondExpiration {
|
2019-01-29 17:39:20 +01:00
|
|
|
t.sendPing(fromID, from, func() {
|
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 14:26:09 +02:00
|
|
|
t.tab.addInboundNode(n)
|
2019-01-29 17:39:20 +01:00
|
|
|
})
|
2018-07-03 15:24:12 +02:00
|
|
|
} else {
|
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 14:26:09 +02:00
|
|
|
t.tab.addInboundNode(n)
|
2015-03-25 16:45:53 +01:00
|
|
|
}
|
2019-01-29 17:39:20 +01:00
|
|
|
|
|
|
|
// Update node database and endpoint predictor.
|
2024-06-05 19:31:04 +02:00
|
|
|
t.db.UpdateLastPingReceived(n.ID(), from.Addr(), time.Now())
|
|
|
|
toaddr := netip.AddrPortFrom(netutil.IPToAddr(req.To.IP), req.To.UDP)
|
|
|
|
t.localNode.UDPEndpointStatement(from, toaddr)
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
|
|
|
|
2019-05-15 06:47:45 +02:00
|
|
|
// PONG/v4
|
|
|
|
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) verifyPong(h *packetHandlerV4, from netip.AddrPort, fromID enode.ID, fromKey v4wire.Pubkey) error {
|
2020-06-02 13:20:19 +02:00
|
|
|
req := h.Packet.(*v4wire.Pong)
|
2017-02-24 09:58:04 +01:00
|
|
|
|
2020-06-02 13:20:19 +02:00
|
|
|
if v4wire.Expired(req.Expiration) {
|
2015-01-27 14:33:26 +01:00
|
|
|
return errExpired
|
|
|
|
}
|
2024-05-29 15:02:26 +02:00
|
|
|
if !t.handleReply(fromID, from.Addr(), req) {
|
2015-03-25 16:45:53 +01:00
|
|
|
return errUnsolicitedReply
|
|
|
|
}
|
2024-06-05 19:31:04 +02:00
|
|
|
toaddr := netip.AddrPortFrom(netutil.IPToAddr(req.To.IP), req.To.UDP)
|
|
|
|
t.localNode.UDPEndpointStatement(from, toaddr)
|
|
|
|
t.db.UpdateLastPongReceived(fromID, from.Addr(), time.Now())
|
2020-06-02 13:20:19 +02:00
|
|
|
return nil
|
2019-01-29 17:39:20 +01:00
|
|
|
}
|
|
|
|
|
2019-05-15 06:47:45 +02:00
|
|
|
// FINDNODE/v4
|
|
|
|
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) verifyFindnode(h *packetHandlerV4, from netip.AddrPort, fromID enode.ID, fromKey v4wire.Pubkey) error {
|
2020-06-02 13:20:19 +02:00
|
|
|
req := h.Packet.(*v4wire.Findnode)
|
2017-02-24 09:58:04 +01:00
|
|
|
|
2020-06-02 13:20:19 +02:00
|
|
|
if v4wire.Expired(req.Expiration) {
|
2015-01-27 14:33:26 +01:00
|
|
|
return errExpired
|
|
|
|
}
|
2024-05-29 15:02:26 +02:00
|
|
|
if !t.checkBond(fromID, from) {
|
2018-07-03 15:24:12 +02:00
|
|
|
// No endpoint proof pong exists, we don't process the packet. This prevents an
|
|
|
|
// attack vector where the discovery protocol could be used to amplify traffic in a
|
|
|
|
// DDOS attack. A malicious actor would send a findnode request with the IP address
|
|
|
|
// and UDP port of the target as the source address. The recipient of the findnode
|
|
|
|
// packet would then send a neighbors packet (which is a much bigger packet than
|
|
|
|
// findnode) to the victim.
|
2015-03-25 16:45:53 +01:00
|
|
|
return errUnknownNode
|
|
|
|
}
|
2019-01-29 17:39:20 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) handleFindnode(h *packetHandlerV4, from netip.AddrPort, fromID enode.ID, mac []byte) {
|
2020-06-02 13:20:19 +02:00
|
|
|
req := h.Packet.(*v4wire.Findnode)
|
|
|
|
|
2019-01-29 17:39:20 +01:00
|
|
|
// Determine closest nodes.
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 00:59:00 +02:00
|
|
|
target := enode.ID(crypto.Keccak256Hash(req.Target[:]))
|
2024-09-30 10:56:14 +02:00
|
|
|
preferLive := !t.tab.cfg.NoFindnodeLivenessCheck
|
|
|
|
closest := t.tab.findnodeByID(target, bucketSize, preferLive).entries
|
2015-01-27 14:33:26 +01:00
|
|
|
|
2015-05-13 21:29:32 +02:00
|
|
|
// Send neighbors in chunks with at most maxNeighbors per packet
|
2019-02-19 03:27:29 -08:00
|
|
|
// to stay below the packet size limit.
|
2020-06-02 13:20:19 +02:00
|
|
|
p := v4wire.Neighbors{Expiration: uint64(time.Now().Add(expiration).Unix())}
|
2019-01-29 17:39:20 +01:00
|
|
|
var sent bool
|
2018-02-12 13:36:09 +01:00
|
|
|
for _, n := range closest {
|
2024-06-05 19:31:04 +02:00
|
|
|
if netutil.CheckRelayAddr(from.Addr(), n.IPAddr()) == nil {
|
2018-02-12 13:36:09 +01:00
|
|
|
p.Nodes = append(p.Nodes, nodeToRPC(n))
|
2016-11-21 18:11:54 +01:00
|
|
|
}
|
2020-06-02 13:20:19 +02:00
|
|
|
if len(p.Nodes) == v4wire.MaxNeighbors {
|
2019-05-15 06:47:45 +02:00
|
|
|
t.send(from, fromID, &p)
|
2015-05-13 21:29:32 +02:00
|
|
|
p.Nodes = p.Nodes[:0]
|
2018-02-12 13:36:09 +01:00
|
|
|
sent = true
|
2015-05-13 21:29:32 +02:00
|
|
|
}
|
2015-05-13 20:03:17 +02:00
|
|
|
}
|
2018-02-12 13:36:09 +01:00
|
|
|
if len(p.Nodes) > 0 || !sent {
|
2019-05-15 06:47:45 +02:00
|
|
|
t.send(from, fromID, &p)
|
2018-02-12 13:36:09 +01:00
|
|
|
}
|
2015-01-27 14:33:26 +01:00
|
|
|
}
|
|
|
|
|
2019-05-15 06:47:45 +02:00
|
|
|
// NEIGHBORS/v4
|
|
|
|
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) verifyNeighbors(h *packetHandlerV4, from netip.AddrPort, fromID enode.ID, fromKey v4wire.Pubkey) error {
|
2020-06-02 13:20:19 +02:00
|
|
|
req := h.Packet.(*v4wire.Neighbors)
|
2017-02-24 09:58:04 +01:00
|
|
|
|
2020-06-02 13:20:19 +02:00
|
|
|
if v4wire.Expired(req.Expiration) {
|
2015-01-27 14:33:26 +01:00
|
|
|
return errExpired
|
|
|
|
}
|
2024-05-29 15:02:26 +02:00
|
|
|
if !t.handleReply(fromID, from.Addr(), h.Packet) {
|
2015-03-25 16:45:53 +01:00
|
|
|
return errUnsolicitedReply
|
|
|
|
}
|
2015-01-27 14:33:26 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-15 06:47:45 +02:00
|
|
|
// ENRREQUEST/v4
|
2017-02-24 09:58:04 +01:00
|
|
|
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) verifyENRRequest(h *packetHandlerV4, from netip.AddrPort, fromID enode.ID, fromKey v4wire.Pubkey) error {
|
2020-06-02 13:20:19 +02:00
|
|
|
req := h.Packet.(*v4wire.ENRRequest)
|
2019-05-15 06:47:45 +02:00
|
|
|
|
2020-06-02 13:20:19 +02:00
|
|
|
if v4wire.Expired(req.Expiration) {
|
2019-05-15 06:47:45 +02:00
|
|
|
return errExpired
|
|
|
|
}
|
2024-05-29 15:02:26 +02:00
|
|
|
if !t.checkBond(fromID, from) {
|
2019-05-15 06:47:45 +02:00
|
|
|
return errUnknownNode
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) handleENRRequest(h *packetHandlerV4, from netip.AddrPort, fromID enode.ID, mac []byte) {
|
2020-06-02 13:20:19 +02:00
|
|
|
t.send(from, fromID, &v4wire.ENRResponse{
|
2019-05-15 06:47:45 +02:00
|
|
|
ReplyTok: mac,
|
|
|
|
Record: *t.localNode.Node().Record(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ENRRESPONSE/v4
|
|
|
|
|
2024-05-29 15:02:26 +02:00
|
|
|
func (t *UDPv4) verifyENRResponse(h *packetHandlerV4, from netip.AddrPort, fromID enode.ID, fromKey v4wire.Pubkey) error {
|
|
|
|
if !t.handleReply(fromID, from.Addr(), h.Packet) {
|
2019-05-15 06:47:45 +02:00
|
|
|
return errUnsolicitedReply
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|