2017-09-25 09:08:07 +01:00
|
|
|
// Copyright 2017 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 adapters
|
|
|
|
|
|
|
|
import (
|
2020-02-13 11:10:03 +01:00
|
|
|
"context"
|
2017-09-25 09:08:07 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-04-30 01:44:41 +08:00
|
|
|
"maps"
|
2017-09-25 09:08:07 +01:00
|
|
|
"math"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2017-12-01 11:49:04 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2017-09-25 09:08:07 +01:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
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"
|
2018-06-14 11:21:17 +02:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/simulations/pipes"
|
2017-09-25 09:08:07 +01:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2019-11-18 09:40:59 +01:00
|
|
|
"github.com/gorilla/websocket"
|
2017-09-25 09:08:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// SimAdapter is a NodeAdapter which creates in-memory simulation nodes and
|
2018-06-14 11:21:17 +02:00
|
|
|
// connects them using net.Pipe
|
2017-09-25 09:08:07 +01:00
|
|
|
type SimAdapter struct {
|
2020-08-03 19:40:46 +02:00
|
|
|
pipe func() (net.Conn, net.Conn, error)
|
|
|
|
mtx sync.RWMutex
|
|
|
|
nodes map[enode.ID]*SimNode
|
|
|
|
lifecycles LifecycleConstructors
|
2017-09-25 09:08:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSimAdapter creates a SimAdapter which is capable of running in-memory
|
|
|
|
// simulation nodes running any of the given services (the services to run on a
|
|
|
|
// particular node are passed to the NewNode function in the NodeConfig)
|
2018-06-14 11:21:17 +02:00
|
|
|
// the adapter uses a net.Pipe for in-memory simulated network connections
|
2020-08-03 19:40:46 +02:00
|
|
|
func NewSimAdapter(services LifecycleConstructors) *SimAdapter {
|
2017-09-25 09:08:07 +01:00
|
|
|
return &SimAdapter{
|
2020-08-03 19:40:46 +02:00
|
|
|
pipe: pipes.NetPipe,
|
|
|
|
nodes: make(map[enode.ID]*SimNode),
|
|
|
|
lifecycles: services,
|
2017-09-25 09:08:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the adapter for logging purposes
|
|
|
|
func (s *SimAdapter) Name() string {
|
|
|
|
return "sim-adapter"
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNode returns a new SimNode using the given config
|
|
|
|
func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error) {
|
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
|
|
|
|
|
|
|
id := config.ID
|
2019-03-15 11:27:17 +01:00
|
|
|
// verify that the node has a private key in the config
|
|
|
|
if config.PrivateKey == nil {
|
|
|
|
return nil, fmt.Errorf("node is missing private key: %s", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check a node with the ID doesn't already exist
|
2017-09-25 09:08:07 +01:00
|
|
|
if _, exists := s.nodes[id]; exists {
|
|
|
|
return nil, fmt.Errorf("node already exists: %s", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the services are valid
|
2020-08-03 19:40:46 +02:00
|
|
|
if len(config.Lifecycles) == 0 {
|
2017-09-25 09:08:07 +01:00
|
|
|
return nil, errors.New("node must have at least one service")
|
|
|
|
}
|
2020-08-03 19:40:46 +02:00
|
|
|
for _, service := range config.Lifecycles {
|
|
|
|
if _, exists := s.lifecycles[service]; !exists {
|
2017-09-25 09:08:07 +01:00
|
|
|
return nil, fmt.Errorf("unknown node service %q", service)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 05:55:47 +01:00
|
|
|
err := config.initDummyEnode()
|
2019-03-15 11:27:17 +01:00
|
|
|
if err != nil {
|
2019-03-22 05:55:47 +01:00
|
|
|
return nil, err
|
2019-03-15 11:27:17 +01:00
|
|
|
}
|
|
|
|
|
2017-09-25 09:08:07 +01:00
|
|
|
n, err := node.New(&node.Config{
|
|
|
|
P2P: p2p.Config{
|
|
|
|
PrivateKey: config.PrivateKey,
|
|
|
|
MaxPeers: math.MaxInt32,
|
|
|
|
NoDiscovery: true,
|
|
|
|
Dialer: s,
|
2018-06-14 11:21:17 +02:00
|
|
|
EnableMsgEvents: config.EnableMsgEvents,
|
2017-09-25 09:08:07 +01:00
|
|
|
},
|
les, p2p/simulations/adapters: fix issues found while simulating les (#21761)
This adds a few tiny fixes for les and the p2p simulation framework:
LES Parts
- Keep the LES-SERVER connection even it's non-synced
We had this idea to reject the connections in LES protocol if the les-server itself is
not synced. However, in LES protocol we will also receive the connection from another
les-server. In this case even the local node is not synced yet, we should keep the tcp
connection for other protocols(e.g. eth protocol).
- Don't count "invalid message" for non-existing GetBlockHeadersMsg request
In the eth syncing mechanism (full sync, fast sync, light sync), it will try to fetch
some non-existent blocks or headers(to ensure we indeed download all the missing chain).
In this case, it's possible that the les-server will receive the request for
non-existent headers. So don't count it as the "invalid message" for scheduling
dropping.
- Copy the announce object in the closure
Before the les-server pushes the latest headers to all connected clients, it will create
a closure and queue it in the underlying request scheduler. In some scenarios it's
problematic. E.g, in private networks, the block can be mined very fast. So before the
first closure is executed, we may already update the latest_announce object. So actually
the "announce" object we want to send is replaced.
The downsize is the client will receive two announces with the same td and then drop the
server.
P2P Simulation Framework
- Don't double register the protocol services in p2p-simulation "Start".
The protocols upon the devp2p are registered in the "New node stage". So don't reigster
them again when starting a node in the p2p simulation framework
- Add one more new config field "ExternalSigner", in order to use clef service in the
framework.
2020-10-31 01:04:38 +08:00
|
|
|
ExternalSigner: config.ExternalSigner,
|
|
|
|
Logger: log.New("node.id", id.String()),
|
2017-09-25 09:08:07 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
simNode := &SimNode{
|
|
|
|
ID: id,
|
|
|
|
config: config,
|
|
|
|
node: n,
|
|
|
|
adapter: s,
|
2020-08-03 19:40:46 +02:00
|
|
|
running: make(map[string]node.Lifecycle),
|
2017-09-25 09:08:07 +01:00
|
|
|
}
|
|
|
|
s.nodes[id] = simNode
|
|
|
|
return simNode, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dial implements the p2p.NodeDialer interface by connecting to the node using
|
2018-06-14 11:21:17 +02:00
|
|
|
// an in-memory net.Pipe
|
2020-02-13 11:10:03 +01:00
|
|
|
func (s *SimAdapter) Dial(ctx context.Context, dest *enode.Node) (conn net.Conn, err error) {
|
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
|
|
|
node, ok := s.GetNode(dest.ID())
|
2017-09-25 09:08:07 +01:00
|
|
|
if !ok {
|
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
|
|
|
return nil, fmt.Errorf("unknown node: %s", dest.ID())
|
2017-09-25 09:08:07 +01:00
|
|
|
}
|
|
|
|
srv := node.Server()
|
|
|
|
if srv == nil {
|
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
|
|
|
return nil, fmt.Errorf("node not running: %s", dest.ID())
|
2017-09-25 09:08:07 +01:00
|
|
|
}
|
2018-06-14 11:21:17 +02:00
|
|
|
// SimAdapter.pipe is net.Pipe (NewSimAdapter)
|
|
|
|
pipe1, pipe2, err := s.pipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// this is simulated 'listening'
|
2019-01-06 11:58:57 +01:00
|
|
|
// asynchronously call the dialed destination node's p2p server
|
2018-06-14 11:21:17 +02:00
|
|
|
// to set up connection on the 'listening' side
|
2017-09-25 09:08:07 +01:00
|
|
|
go srv.SetupConn(pipe1, 0, nil)
|
|
|
|
return pipe2, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialRPC implements the RPCDialer interface by creating an in-memory RPC
|
|
|
|
// client of the given node
|
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
|
|
|
func (s *SimAdapter) DialRPC(id enode.ID) (*rpc.Client, error) {
|
2017-09-25 09:08:07 +01:00
|
|
|
node, ok := s.GetNode(id)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unknown node: %s", id)
|
|
|
|
}
|
2023-06-14 21:24:47 +09:00
|
|
|
return node.node.Attach(), nil
|
2017-09-25 09:08:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetNode returns the node with the given ID if it exists
|
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
|
|
|
func (s *SimAdapter) GetNode(id enode.ID) (*SimNode, bool) {
|
2017-09-25 09:08:07 +01:00
|
|
|
s.mtx.RLock()
|
|
|
|
defer s.mtx.RUnlock()
|
|
|
|
node, ok := s.nodes[id]
|
|
|
|
return node, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// SimNode is an in-memory simulation node which connects to other nodes using
|
2018-06-14 11:21:17 +02:00
|
|
|
// net.Pipe (see SimAdapter.Dial), running devp2p protocols directly over that
|
|
|
|
// pipe
|
2017-09-25 09:08:07 +01:00
|
|
|
type SimNode struct {
|
|
|
|
lock sync.RWMutex
|
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
|
|
|
ID enode.ID
|
2017-09-25 09:08:07 +01:00
|
|
|
config *NodeConfig
|
|
|
|
adapter *SimAdapter
|
|
|
|
node *node.Node
|
2020-08-03 19:40:46 +02:00
|
|
|
running map[string]node.Lifecycle
|
2017-09-25 09:08:07 +01:00
|
|
|
client *rpc.Client
|
|
|
|
registerOnce sync.Once
|
|
|
|
}
|
|
|
|
|
2024-02-05 23:16:32 +02:00
|
|
|
// Close closes the underlying node.Node to release
|
2019-02-07 11:40:36 +01:00
|
|
|
// acquired resources.
|
|
|
|
func (sn *SimNode) Close() error {
|
|
|
|
return sn.node.Close()
|
|
|
|
}
|
|
|
|
|
2017-09-25 09:08:07 +01:00
|
|
|
// Addr returns the node's discovery address
|
2018-05-08 04:08:43 -07:00
|
|
|
func (sn *SimNode) Addr() []byte {
|
|
|
|
return []byte(sn.Node().String())
|
2017-09-25 09:08:07 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
// Node returns a node descriptor representing the SimNode
|
|
|
|
func (sn *SimNode) Node() *enode.Node {
|
|
|
|
return sn.config.Node()
|
2017-09-25 09:08:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client returns an rpc.Client which can be used to communicate with the
|
|
|
|
// underlying services (it is set once the node has started)
|
2018-05-08 04:08:43 -07:00
|
|
|
func (sn *SimNode) Client() (*rpc.Client, error) {
|
|
|
|
sn.lock.RLock()
|
|
|
|
defer sn.lock.RUnlock()
|
|
|
|
if sn.client == nil {
|
2017-09-25 09:08:07 +01:00
|
|
|
return nil, errors.New("node not started")
|
|
|
|
}
|
2018-05-08 04:08:43 -07:00
|
|
|
return sn.client, nil
|
2017-09-25 09:08:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServeRPC serves RPC requests over the given connection by creating an
|
2019-11-18 09:40:59 +01:00
|
|
|
// in-memory client to the node's RPC server.
|
|
|
|
func (sn *SimNode) ServeRPC(conn *websocket.Conn) error {
|
2018-05-08 04:08:43 -07:00
|
|
|
handler, err := sn.node.RPCHandler()
|
2017-09-25 09:08:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-07 14:02:14 +01:00
|
|
|
codec := rpc.NewFuncCodec(conn, func(v any, _ bool) error { return conn.WriteJSON(v) }, conn.ReadJSON)
|
2019-11-18 09:40:59 +01:00
|
|
|
handler.ServeCodec(codec, 0)
|
2017-09-25 09:08:07 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Snapshots creates snapshots of the services by calling the
|
|
|
|
// simulation_snapshot RPC method
|
2018-05-08 04:08:43 -07:00
|
|
|
func (sn *SimNode) Snapshots() (map[string][]byte, error) {
|
|
|
|
sn.lock.RLock()
|
2024-04-30 01:44:41 +08:00
|
|
|
services := maps.Clone(sn.running)
|
2018-05-08 04:08:43 -07:00
|
|
|
sn.lock.RUnlock()
|
2017-09-25 09:08:07 +01:00
|
|
|
if len(services) == 0 {
|
|
|
|
return nil, errors.New("no running services")
|
|
|
|
}
|
|
|
|
snapshots := make(map[string][]byte)
|
|
|
|
for name, service := range services {
|
|
|
|
if s, ok := service.(interface {
|
|
|
|
Snapshot() ([]byte, error)
|
|
|
|
}); ok {
|
|
|
|
snap, err := s.Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
snapshots[name] = snap
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return snapshots, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start registers the services and starts the underlying devp2p node
|
2018-05-08 04:08:43 -07:00
|
|
|
func (sn *SimNode) Start(snapshots map[string][]byte) error {
|
2020-08-03 19:40:46 +02:00
|
|
|
// ensure we only register the services once in the case of the node
|
|
|
|
// being stopped and then started again
|
|
|
|
var regErr error
|
|
|
|
sn.registerOnce.Do(func() {
|
|
|
|
for _, name := range sn.config.Lifecycles {
|
2017-09-25 09:08:07 +01:00
|
|
|
ctx := &ServiceContext{
|
2020-08-03 19:40:46 +02:00
|
|
|
RPCDialer: sn.adapter,
|
|
|
|
Config: sn.config,
|
2017-09-25 09:08:07 +01:00
|
|
|
}
|
|
|
|
if snapshots != nil {
|
|
|
|
ctx.Snapshot = snapshots[name]
|
|
|
|
}
|
2020-08-03 19:40:46 +02:00
|
|
|
serviceFunc := sn.adapter.lifecycles[name]
|
|
|
|
service, err := serviceFunc(ctx, sn.node)
|
2017-09-25 09:08:07 +01:00
|
|
|
if err != nil {
|
|
|
|
regErr = err
|
2018-06-14 11:21:17 +02:00
|
|
|
break
|
2017-09-25 09:08:07 +01:00
|
|
|
}
|
2020-08-03 19:40:46 +02:00
|
|
|
// if the service has already been registered, don't register it again.
|
|
|
|
if _, ok := sn.running[name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sn.running[name] = service
|
2017-09-25 09:08:07 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
if regErr != nil {
|
|
|
|
return regErr
|
|
|
|
}
|
|
|
|
|
2018-05-08 04:08:43 -07:00
|
|
|
if err := sn.node.Start(); err != nil {
|
2017-09-25 09:08:07 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create an in-process RPC client
|
2023-06-14 21:24:47 +09:00
|
|
|
client := sn.node.Attach()
|
2018-05-08 04:08:43 -07:00
|
|
|
sn.lock.Lock()
|
2020-08-03 19:40:46 +02:00
|
|
|
sn.client = client
|
2018-05-08 04:08:43 -07:00
|
|
|
sn.lock.Unlock()
|
2017-09-25 09:08:07 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop closes the RPC client and stops the underlying devp2p node
|
2018-05-08 04:08:43 -07:00
|
|
|
func (sn *SimNode) Stop() error {
|
|
|
|
sn.lock.Lock()
|
|
|
|
if sn.client != nil {
|
|
|
|
sn.client.Close()
|
|
|
|
sn.client = nil
|
2017-09-25 09:08:07 +01:00
|
|
|
}
|
2018-05-08 04:08:43 -07:00
|
|
|
sn.lock.Unlock()
|
2020-08-03 19:40:46 +02:00
|
|
|
return sn.node.Close()
|
2017-09-25 09:08:07 +01:00
|
|
|
}
|
|
|
|
|
2018-07-17 07:08:34 +02:00
|
|
|
// Service returns a running service by name
|
2020-08-03 19:40:46 +02:00
|
|
|
func (sn *SimNode) Service(name string) node.Lifecycle {
|
2018-07-17 07:08:34 +02:00
|
|
|
sn.lock.RLock()
|
|
|
|
defer sn.lock.RUnlock()
|
|
|
|
return sn.running[name]
|
|
|
|
}
|
|
|
|
|
2017-09-25 09:08:07 +01:00
|
|
|
// Services returns a copy of the underlying services
|
2020-08-03 19:40:46 +02:00
|
|
|
func (sn *SimNode) Services() []node.Lifecycle {
|
2018-05-08 04:08:43 -07:00
|
|
|
sn.lock.RLock()
|
|
|
|
defer sn.lock.RUnlock()
|
2020-08-03 19:40:46 +02:00
|
|
|
services := make([]node.Lifecycle, 0, len(sn.running))
|
2018-05-08 04:08:43 -07:00
|
|
|
for _, service := range sn.running {
|
2017-09-25 09:08:07 +01:00
|
|
|
services = append(services, service)
|
|
|
|
}
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2018-07-17 07:08:34 +02:00
|
|
|
// ServiceMap returns a map by names of the underlying services
|
2020-08-03 19:40:46 +02:00
|
|
|
func (sn *SimNode) ServiceMap() map[string]node.Lifecycle {
|
2018-07-17 07:08:34 +02:00
|
|
|
sn.lock.RLock()
|
|
|
|
defer sn.lock.RUnlock()
|
2024-04-30 01:44:41 +08:00
|
|
|
return maps.Clone(sn.running)
|
2018-07-17 07:08:34 +02:00
|
|
|
}
|
|
|
|
|
2017-09-25 09:08:07 +01:00
|
|
|
// Server returns the underlying p2p.Server
|
2018-05-08 04:08:43 -07:00
|
|
|
func (sn *SimNode) Server() *p2p.Server {
|
|
|
|
return sn.node.Server()
|
2017-09-25 09:08:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeEvents subscribes the given channel to peer events from the
|
|
|
|
// underlying p2p.Server
|
2018-05-08 04:08:43 -07:00
|
|
|
func (sn *SimNode) SubscribeEvents(ch chan *p2p.PeerEvent) event.Subscription {
|
|
|
|
srv := sn.Server()
|
2017-09-25 09:08:07 +01:00
|
|
|
if srv == nil {
|
|
|
|
panic("node not running")
|
|
|
|
}
|
|
|
|
return srv.SubscribeEvents(ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeInfo returns information about the node
|
2018-05-08 04:08:43 -07:00
|
|
|
func (sn *SimNode) NodeInfo() *p2p.NodeInfo {
|
|
|
|
server := sn.Server()
|
2017-09-25 09:08:07 +01:00
|
|
|
if server == nil {
|
|
|
|
return &p2p.NodeInfo{
|
2018-05-08 04:08:43 -07:00
|
|
|
ID: sn.ID.String(),
|
|
|
|
Enode: sn.Node().String(),
|
2017-09-25 09:08:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return server.NodeInfo()
|
|
|
|
}
|