2017-09-25 03:08:07 -05: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 simulations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2018-12-06 23:51:40 -06:00
|
|
|
"errors"
|
2017-09-25 03:08:07 -05:00
|
|
|
"fmt"
|
2018-12-17 05:19:01 -06:00
|
|
|
"math/rand"
|
2017-09-25 03:08:07 -05:00
|
|
|
"sync"
|
2017-12-01 05:49:04 -06:00
|
|
|
"time"
|
2017-09-25 03:08:07 -05:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"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-24 17:59:00 -05:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2017-09-25 03:08:07 -05:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
|
|
|
|
)
|
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
var DialBanTimeout = 200 * time.Millisecond
|
2017-12-01 05:49:04 -06:00
|
|
|
|
2017-09-25 03:08:07 -05:00
|
|
|
// NetworkConfig defines configuration options for starting a Network
|
|
|
|
type NetworkConfig struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
DefaultService string `json:"default_service,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Network models a p2p simulation network which consists of a collection of
|
|
|
|
// simulated nodes and the connections which exist between them.
|
|
|
|
//
|
|
|
|
// The Network has a single NodeAdapter which is responsible for actually
|
|
|
|
// starting nodes and connecting them together.
|
|
|
|
//
|
|
|
|
// The Network emits events when nodes are started and stopped, when they are
|
|
|
|
// connected and disconnected, and also when messages are sent between nodes.
|
|
|
|
type Network struct {
|
|
|
|
NetworkConfig
|
|
|
|
|
|
|
|
Nodes []*Node `json:"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-24 17:59:00 -05:00
|
|
|
nodeMap map[enode.ID]int
|
2017-09-25 03:08:07 -05:00
|
|
|
|
|
|
|
Conns []*Conn `json:"conns"`
|
|
|
|
connMap map[string]int
|
|
|
|
|
|
|
|
nodeAdapter adapters.NodeAdapter
|
|
|
|
events event.Feed
|
|
|
|
lock sync.RWMutex
|
|
|
|
quitc chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNetwork returns a Network which uses the given NodeAdapter and NetworkConfig
|
|
|
|
func NewNetwork(nodeAdapter adapters.NodeAdapter, conf *NetworkConfig) *Network {
|
|
|
|
return &Network{
|
|
|
|
NetworkConfig: *conf,
|
|
|
|
nodeAdapter: nodeAdapter,
|
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-24 17:59:00 -05:00
|
|
|
nodeMap: make(map[enode.ID]int),
|
2017-09-25 03:08:07 -05:00
|
|
|
connMap: make(map[string]int),
|
|
|
|
quitc: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Events returns the output event feed of the Network.
|
2018-05-08 06:08:43 -05:00
|
|
|
func (net *Network) Events() *event.Feed {
|
|
|
|
return &net.events
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeWithConfig adds a new node to the network with the given config,
|
|
|
|
// returning an error if a node with the same ID or name already exists
|
2018-05-08 06:08:43 -05:00
|
|
|
func (net *Network) NewNodeWithConfig(conf *adapters.NodeConfig) (*Node, error) {
|
|
|
|
net.lock.Lock()
|
|
|
|
defer net.lock.Unlock()
|
2017-09-25 03:08:07 -05:00
|
|
|
|
2017-12-01 05:49:04 -06:00
|
|
|
if conf.Reachable == 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-24 17:59:00 -05:00
|
|
|
conf.Reachable = func(otherID enode.ID) bool {
|
2018-05-08 06:08:43 -05:00
|
|
|
_, err := net.InitConn(conf.ID, otherID)
|
2018-06-20 07:06:27 -05:00
|
|
|
if err != nil && bytes.Compare(conf.ID.Bytes(), otherID.Bytes()) < 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2017-12-01 05:49:04 -06:00
|
|
|
}
|
|
|
|
}
|
2017-09-25 03:08:07 -05:00
|
|
|
|
|
|
|
// check the node doesn't already exist
|
2018-06-20 07:06:27 -05:00
|
|
|
if node := net.getNode(conf.ID); node != nil {
|
|
|
|
return nil, fmt.Errorf("node with ID %q already exists", conf.ID)
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
if node := net.getNodeByName(conf.Name); node != nil {
|
2017-09-25 03:08:07 -05:00
|
|
|
return nil, fmt.Errorf("node with name %q already exists", conf.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no services are configured, use the default service
|
|
|
|
if len(conf.Services) == 0 {
|
2018-05-08 06:08:43 -05:00
|
|
|
conf.Services = []string{net.DefaultService}
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// use the NodeAdapter to create the node
|
2018-05-08 06:08:43 -05:00
|
|
|
adapterNode, err := net.nodeAdapter.NewNode(conf)
|
2017-09-25 03:08:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
node := &Node{
|
|
|
|
Node: adapterNode,
|
|
|
|
Config: conf,
|
|
|
|
}
|
2018-10-11 13:32:14 -05:00
|
|
|
log.Trace("Node created", "id", conf.ID)
|
2018-06-20 07:06:27 -05:00
|
|
|
net.nodeMap[conf.ID] = len(net.Nodes)
|
2018-05-08 06:08:43 -05:00
|
|
|
net.Nodes = append(net.Nodes, node)
|
2017-09-25 03:08:07 -05:00
|
|
|
|
|
|
|
// emit a "control" event
|
2018-05-08 06:08:43 -05:00
|
|
|
net.events.Send(ControlEvent(node))
|
2017-09-25 03:08:07 -05:00
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config returns the network configuration
|
2018-05-08 06:08:43 -05:00
|
|
|
func (net *Network) Config() *NetworkConfig {
|
|
|
|
return &net.NetworkConfig
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartAll starts all nodes in the network
|
2018-05-08 06:08:43 -05:00
|
|
|
func (net *Network) StartAll() error {
|
|
|
|
for _, node := range net.Nodes {
|
2017-09-25 03:08:07 -05:00
|
|
|
if node.Up {
|
|
|
|
continue
|
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
if err := net.Start(node.ID()); err != nil {
|
2017-09-25 03:08:07 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopAll stops all nodes in the network
|
2018-05-08 06:08:43 -05:00
|
|
|
func (net *Network) StopAll() error {
|
|
|
|
for _, node := range net.Nodes {
|
2017-09-25 03:08:07 -05:00
|
|
|
if !node.Up {
|
|
|
|
continue
|
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
if err := net.Stop(node.ID()); err != nil {
|
2017-09-25 03:08:07 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the node with the given ID
|
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-24 17:59:00 -05:00
|
|
|
func (net *Network) Start(id enode.ID) error {
|
2018-05-08 06:08:43 -05:00
|
|
|
return net.startWithSnapshots(id, nil)
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// startWithSnapshots starts the node with the given ID using the give
|
|
|
|
// snapshots
|
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-24 17:59:00 -05:00
|
|
|
func (net *Network) startWithSnapshots(id enode.ID, snapshots map[string][]byte) error {
|
2018-06-20 07:06:27 -05:00
|
|
|
net.lock.Lock()
|
2018-10-11 13:32:14 -05:00
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
node := net.getNode(id)
|
2017-09-25 03:08:07 -05:00
|
|
|
if node == nil {
|
2019-01-22 13:11:43 -06:00
|
|
|
net.lock.Unlock()
|
2017-09-25 03:08:07 -05:00
|
|
|
return fmt.Errorf("node %v does not exist", id)
|
|
|
|
}
|
|
|
|
if node.Up {
|
2019-01-22 13:11:43 -06:00
|
|
|
net.lock.Unlock()
|
2017-09-25 03:08:07 -05:00
|
|
|
return fmt.Errorf("node %v already up", id)
|
|
|
|
}
|
2018-10-11 13:32:14 -05:00
|
|
|
log.Trace("Starting node", "id", id, "adapter", net.nodeAdapter.Name())
|
2017-09-25 03:08:07 -05:00
|
|
|
if err := node.Start(snapshots); err != nil {
|
2019-01-22 13:11:43 -06:00
|
|
|
net.lock.Unlock()
|
2018-10-11 13:32:14 -05:00
|
|
|
log.Warn("Node startup failed", "id", id, "err", err)
|
2017-09-25 03:08:07 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
node.Up = true
|
2018-10-11 13:32:14 -05:00
|
|
|
log.Info("Started node", "id", id)
|
2019-01-22 13:11:43 -06:00
|
|
|
ev := NewEvent(node)
|
|
|
|
net.lock.Unlock()
|
2017-09-25 03:08:07 -05:00
|
|
|
|
2019-01-22 13:11:43 -06:00
|
|
|
net.events.Send(ev)
|
2017-09-25 03:08:07 -05:00
|
|
|
|
|
|
|
// subscribe to peer events
|
|
|
|
client, err := node.Client()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting rpc client for node %v: %s", id, err)
|
|
|
|
}
|
|
|
|
events := make(chan *p2p.PeerEvent)
|
|
|
|
sub, err := client.Subscribe(context.Background(), "admin", events, "peerEvents")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting peer events for node %v: %s", id, err)
|
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
go net.watchPeerEvents(id, events, sub)
|
2017-09-25 03:08:07 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// watchPeerEvents reads peer events from the given channel and emits
|
|
|
|
// corresponding network events
|
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-24 17:59:00 -05:00
|
|
|
func (net *Network) watchPeerEvents(id enode.ID, events chan *p2p.PeerEvent, sub event.Subscription) {
|
2017-09-25 03:08:07 -05:00
|
|
|
defer func() {
|
|
|
|
sub.Unsubscribe()
|
|
|
|
|
|
|
|
// assume the node is now down
|
2018-05-08 06:08:43 -05:00
|
|
|
net.lock.Lock()
|
2018-06-20 07:06:27 -05:00
|
|
|
defer net.lock.Unlock()
|
2019-01-22 13:11:43 -06:00
|
|
|
|
2018-05-08 06:08:43 -05:00
|
|
|
node := net.getNode(id)
|
2018-06-20 07:06:27 -05:00
|
|
|
if node == nil {
|
|
|
|
return
|
|
|
|
}
|
2017-09-25 03:08:07 -05:00
|
|
|
node.Up = false
|
2019-01-22 13:11:43 -06:00
|
|
|
ev := NewEvent(node)
|
|
|
|
net.events.Send(ev)
|
2017-09-25 03:08:07 -05:00
|
|
|
}()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event, ok := <-events:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
peer := event.Peer
|
|
|
|
switch event.Type {
|
|
|
|
|
|
|
|
case p2p.PeerEventTypeAdd:
|
2018-05-08 06:08:43 -05:00
|
|
|
net.DidConnect(id, peer)
|
2017-09-25 03:08:07 -05:00
|
|
|
|
|
|
|
case p2p.PeerEventTypeDrop:
|
2018-05-08 06:08:43 -05:00
|
|
|
net.DidDisconnect(id, peer)
|
2017-09-25 03:08:07 -05:00
|
|
|
|
|
|
|
case p2p.PeerEventTypeMsgSend:
|
2018-05-08 06:08:43 -05:00
|
|
|
net.DidSend(id, peer, event.Protocol, *event.MsgCode)
|
2017-09-25 03:08:07 -05:00
|
|
|
|
|
|
|
case p2p.PeerEventTypeMsgRecv:
|
2018-05-08 06:08:43 -05:00
|
|
|
net.DidReceive(peer, id, event.Protocol, *event.MsgCode)
|
2017-09-25 03:08:07 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case err := <-sub.Err():
|
|
|
|
if err != nil {
|
2018-10-11 13:32:14 -05:00
|
|
|
log.Error("Error in peer event subscription", "id", id, "err", err)
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the node with the given ID
|
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-24 17:59:00 -05:00
|
|
|
func (net *Network) Stop(id enode.ID) error {
|
2018-06-20 07:06:27 -05:00
|
|
|
net.lock.Lock()
|
|
|
|
node := net.getNode(id)
|
2017-09-25 03:08:07 -05:00
|
|
|
if node == nil {
|
2019-01-22 13:11:43 -06:00
|
|
|
net.lock.Unlock()
|
2017-09-25 03:08:07 -05:00
|
|
|
return fmt.Errorf("node %v does not exist", id)
|
|
|
|
}
|
|
|
|
if !node.Up {
|
2019-01-22 13:11:43 -06:00
|
|
|
net.lock.Unlock()
|
2017-09-25 03:08:07 -05:00
|
|
|
return fmt.Errorf("node %v already down", id)
|
|
|
|
}
|
|
|
|
node.Up = false
|
2018-10-11 13:32:14 -05:00
|
|
|
net.lock.Unlock()
|
2017-09-25 03:08:07 -05:00
|
|
|
|
2018-10-11 13:32:14 -05:00
|
|
|
err := node.Stop()
|
|
|
|
if err != nil {
|
|
|
|
net.lock.Lock()
|
|
|
|
node.Up = true
|
|
|
|
net.lock.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Info("Stopped node", "id", id, "err", err)
|
2019-01-22 13:11:43 -06:00
|
|
|
net.lock.Lock()
|
|
|
|
ev := ControlEvent(node)
|
|
|
|
net.lock.Unlock()
|
|
|
|
net.events.Send(ev)
|
2017-09-25 03:08:07 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect connects two nodes together by calling the "admin_addPeer" RPC
|
|
|
|
// method on the "one" node so that it connects to the "other" 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-24 17:59:00 -05:00
|
|
|
func (net *Network) Connect(oneID, otherID enode.ID) error {
|
2018-10-11 13:32:14 -05:00
|
|
|
log.Debug("Connecting nodes with addPeer", "id", oneID, "other", otherID)
|
2018-05-08 06:08:43 -05:00
|
|
|
conn, err := net.InitConn(oneID, otherID)
|
2017-09-25 03:08:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
client, err := conn.one.Client()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
net.events.Send(ControlEvent(conn))
|
2017-09-25 03:08:07 -05:00
|
|
|
return client.Call(nil, "admin_addPeer", string(conn.other.Addr()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect disconnects two nodes by calling the "admin_removePeer" RPC
|
|
|
|
// method on the "one" node so that it disconnects from the "other" 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-24 17:59:00 -05:00
|
|
|
func (net *Network) Disconnect(oneID, otherID enode.ID) error {
|
2018-05-08 06:08:43 -05:00
|
|
|
conn := net.GetConn(oneID, otherID)
|
2017-09-25 03:08:07 -05:00
|
|
|
if conn == nil {
|
|
|
|
return fmt.Errorf("connection between %v and %v does not exist", oneID, otherID)
|
|
|
|
}
|
|
|
|
if !conn.Up {
|
|
|
|
return fmt.Errorf("%v and %v already disconnected", oneID, otherID)
|
|
|
|
}
|
|
|
|
client, err := conn.one.Client()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
net.events.Send(ControlEvent(conn))
|
2017-09-25 03:08:07 -05:00
|
|
|
return client.Call(nil, "admin_removePeer", string(conn.other.Addr()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DidConnect tracks the fact that the "one" node connected to the "other" 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-24 17:59:00 -05:00
|
|
|
func (net *Network) DidConnect(one, other enode.ID) error {
|
2018-06-20 07:06:27 -05:00
|
|
|
net.lock.Lock()
|
|
|
|
defer net.lock.Unlock()
|
|
|
|
conn, err := net.getOrCreateConn(one, other)
|
2017-09-25 03:08:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("connection between %v and %v does not exist", one, other)
|
|
|
|
}
|
|
|
|
if conn.Up {
|
|
|
|
return fmt.Errorf("%v and %v already connected", one, other)
|
|
|
|
}
|
|
|
|
conn.Up = true
|
2018-05-08 06:08:43 -05:00
|
|
|
net.events.Send(NewEvent(conn))
|
2017-09-25 03:08:07 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DidDisconnect tracks the fact that the "one" node disconnected from the
|
|
|
|
// "other" 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-24 17:59:00 -05:00
|
|
|
func (net *Network) DidDisconnect(one, other enode.ID) error {
|
2018-06-20 07:06:27 -05:00
|
|
|
net.lock.Lock()
|
|
|
|
defer net.lock.Unlock()
|
|
|
|
conn := net.getConn(one, other)
|
2017-12-01 05:49:04 -06:00
|
|
|
if conn == nil {
|
2017-09-25 03:08:07 -05:00
|
|
|
return fmt.Errorf("connection between %v and %v does not exist", one, other)
|
|
|
|
}
|
|
|
|
if !conn.Up {
|
|
|
|
return fmt.Errorf("%v and %v already disconnected", one, other)
|
|
|
|
}
|
|
|
|
conn.Up = false
|
2018-06-20 07:06:27 -05:00
|
|
|
conn.initiated = time.Now().Add(-DialBanTimeout)
|
2018-05-08 06:08:43 -05:00
|
|
|
net.events.Send(NewEvent(conn))
|
2017-09-25 03:08:07 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DidSend tracks the fact that "sender" sent a message to "receiver"
|
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-24 17:59:00 -05:00
|
|
|
func (net *Network) DidSend(sender, receiver enode.ID, proto string, code uint64) error {
|
2017-09-25 03:08:07 -05:00
|
|
|
msg := &Msg{
|
|
|
|
One: sender,
|
|
|
|
Other: receiver,
|
|
|
|
Protocol: proto,
|
|
|
|
Code: code,
|
|
|
|
Received: false,
|
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
net.events.Send(NewEvent(msg))
|
2017-09-25 03:08:07 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DidReceive tracks the fact that "receiver" received a message from "sender"
|
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-24 17:59:00 -05:00
|
|
|
func (net *Network) DidReceive(sender, receiver enode.ID, proto string, code uint64) error {
|
2017-09-25 03:08:07 -05:00
|
|
|
msg := &Msg{
|
|
|
|
One: sender,
|
|
|
|
Other: receiver,
|
|
|
|
Protocol: proto,
|
|
|
|
Code: code,
|
|
|
|
Received: true,
|
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
net.events.Send(NewEvent(msg))
|
2017-09-25 03:08:07 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNode gets the node with the given ID, returning nil if the node does not
|
|
|
|
// exist
|
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-24 17:59:00 -05:00
|
|
|
func (net *Network) GetNode(id enode.ID) *Node {
|
2018-12-17 05:19:01 -06:00
|
|
|
net.lock.RLock()
|
|
|
|
defer net.lock.RUnlock()
|
2018-05-08 06:08:43 -05:00
|
|
|
return net.getNode(id)
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetNode gets the node with the given name, returning nil if the node does
|
|
|
|
// not exist
|
2018-05-08 06:08:43 -05:00
|
|
|
func (net *Network) GetNodeByName(name string) *Node {
|
2018-12-17 05:19:01 -06:00
|
|
|
net.lock.RLock()
|
|
|
|
defer net.lock.RUnlock()
|
2018-05-08 06:08:43 -05:00
|
|
|
return net.getNodeByName(name)
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
2018-12-17 05:19:01 -06:00
|
|
|
func (net *Network) getNodeByName(name string) *Node {
|
|
|
|
for _, node := range net.Nodes {
|
|
|
|
if node.Config.Name == name {
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-14 04:21:17 -05:00
|
|
|
// GetNodes returns the existing nodes
|
|
|
|
func (net *Network) GetNodes() (nodes []*Node) {
|
2018-12-17 05:19:01 -06:00
|
|
|
net.lock.RLock()
|
|
|
|
defer net.lock.RUnlock()
|
2018-06-14 04:21:17 -05:00
|
|
|
|
|
|
|
nodes = append(nodes, net.Nodes...)
|
|
|
|
return 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-24 17:59:00 -05:00
|
|
|
func (net *Network) getNode(id enode.ID) *Node {
|
2018-05-08 06:08:43 -05:00
|
|
|
i, found := net.nodeMap[id]
|
2017-09-25 03:08:07 -05:00
|
|
|
if !found {
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
return net.Nodes[i]
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
2018-12-17 05:19:01 -06:00
|
|
|
// GetRandomUpNode returns a random node on the network, which is running.
|
|
|
|
func (net *Network) GetRandomUpNode(excludeIDs ...enode.ID) *Node {
|
|
|
|
net.lock.RLock()
|
|
|
|
defer net.lock.RUnlock()
|
|
|
|
return net.getRandomNode(net.getUpNodeIDs(), excludeIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (net *Network) getUpNodeIDs() (ids []enode.ID) {
|
2018-05-08 06:08:43 -05:00
|
|
|
for _, node := range net.Nodes {
|
2018-12-17 05:19:01 -06:00
|
|
|
if node.Up {
|
|
|
|
ids = append(ids, node.ID())
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
}
|
2018-12-17 05:19:01 -06:00
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRandomDownNode returns a random node on the network, which is stopped.
|
|
|
|
func (net *Network) GetRandomDownNode(excludeIDs ...enode.ID) *Node {
|
|
|
|
net.lock.RLock()
|
|
|
|
defer net.lock.RUnlock()
|
|
|
|
return net.getRandomNode(net.getDownNodeIDs(), excludeIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (net *Network) getDownNodeIDs() (ids []enode.ID) {
|
|
|
|
for _, node := range net.GetNodes() {
|
|
|
|
if !node.Up {
|
|
|
|
ids = append(ids, node.ID())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
|
|
|
func (net *Network) getRandomNode(ids []enode.ID, excludeIDs []enode.ID) *Node {
|
|
|
|
filtered := filterIDs(ids, excludeIDs)
|
|
|
|
|
|
|
|
l := len(filtered)
|
|
|
|
if l == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return net.GetNode(filtered[rand.Intn(l)])
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterIDs(ids []enode.ID, excludeIDs []enode.ID) []enode.ID {
|
|
|
|
exclude := make(map[enode.ID]bool)
|
|
|
|
for _, id := range excludeIDs {
|
|
|
|
exclude[id] = true
|
|
|
|
}
|
|
|
|
var filtered []enode.ID
|
|
|
|
for _, id := range ids {
|
|
|
|
if _, found := exclude[id]; !found {
|
|
|
|
filtered = append(filtered, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filtered
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetConn returns the connection which exists between "one" and "other"
|
|
|
|
// regardless of which node initiated the connection
|
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-24 17:59:00 -05:00
|
|
|
func (net *Network) GetConn(oneID, otherID enode.ID) *Conn {
|
2018-12-17 05:19:01 -06:00
|
|
|
net.lock.RLock()
|
|
|
|
defer net.lock.RUnlock()
|
2018-05-08 06:08:43 -05:00
|
|
|
return net.getConn(oneID, otherID)
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrCreateConn is like GetConn but creates the connection if it doesn't
|
|
|
|
// already exist
|
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-24 17:59:00 -05:00
|
|
|
func (net *Network) GetOrCreateConn(oneID, otherID enode.ID) (*Conn, error) {
|
2018-05-08 06:08:43 -05:00
|
|
|
net.lock.Lock()
|
|
|
|
defer net.lock.Unlock()
|
|
|
|
return net.getOrCreateConn(oneID, otherID)
|
2017-12-01 05:49:04 -06: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-24 17:59:00 -05:00
|
|
|
func (net *Network) getOrCreateConn(oneID, otherID enode.ID) (*Conn, error) {
|
2018-05-08 06:08:43 -05:00
|
|
|
if conn := net.getConn(oneID, otherID); conn != nil {
|
2017-09-25 03:08:07 -05:00
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2018-05-08 06:08:43 -05:00
|
|
|
one := net.getNode(oneID)
|
2017-09-25 03:08:07 -05:00
|
|
|
if one == nil {
|
|
|
|
return nil, fmt.Errorf("node %v does not exist", oneID)
|
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
other := net.getNode(otherID)
|
2017-09-25 03:08:07 -05:00
|
|
|
if other == nil {
|
|
|
|
return nil, fmt.Errorf("node %v does not exist", otherID)
|
|
|
|
}
|
|
|
|
conn := &Conn{
|
|
|
|
One: oneID,
|
|
|
|
Other: otherID,
|
|
|
|
one: one,
|
|
|
|
other: other,
|
|
|
|
}
|
|
|
|
label := ConnLabel(oneID, otherID)
|
2018-05-08 06:08:43 -05:00
|
|
|
net.connMap[label] = len(net.Conns)
|
|
|
|
net.Conns = append(net.Conns, conn)
|
2017-09-25 03:08:07 -05:00
|
|
|
return conn, 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-24 17:59:00 -05:00
|
|
|
func (net *Network) getConn(oneID, otherID enode.ID) *Conn {
|
2017-09-25 03:08:07 -05:00
|
|
|
label := ConnLabel(oneID, otherID)
|
2018-05-08 06:08:43 -05:00
|
|
|
i, found := net.connMap[label]
|
2017-09-25 03:08:07 -05:00
|
|
|
if !found {
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
return net.Conns[i]
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
2019-01-06 04:58:57 -06:00
|
|
|
// InitConn(one, other) retrieves the connection model for the connection between
|
2017-12-01 05:49:04 -06:00
|
|
|
// peers one and other, or creates a new one if it does not exist
|
|
|
|
// the order of nodes does not matter, i.e., Conn(i,j) == Conn(j, i)
|
|
|
|
// it checks if the connection is already up, and if the nodes are running
|
|
|
|
// NOTE:
|
|
|
|
// it also checks whether there has been recent attempt to connect the peers
|
|
|
|
// this is cheating as the simulation is used as an oracle and know about
|
|
|
|
// remote peers attempt to connect to a node which will then not initiate the connection
|
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-24 17:59:00 -05:00
|
|
|
func (net *Network) InitConn(oneID, otherID enode.ID) (*Conn, error) {
|
2018-05-08 06:08:43 -05:00
|
|
|
net.lock.Lock()
|
|
|
|
defer net.lock.Unlock()
|
2017-12-01 05:49:04 -06:00
|
|
|
if oneID == otherID {
|
|
|
|
return nil, fmt.Errorf("refusing to connect to self %v", oneID)
|
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
conn, err := net.getOrCreateConn(oneID, otherID)
|
2017-12-01 05:49:04 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if conn.Up {
|
|
|
|
return nil, fmt.Errorf("%v and %v already connected", oneID, otherID)
|
|
|
|
}
|
2018-06-20 07:06:27 -05:00
|
|
|
if time.Since(conn.initiated) < DialBanTimeout {
|
|
|
|
return nil, fmt.Errorf("connection between %v and %v recently attempted", oneID, otherID)
|
|
|
|
}
|
|
|
|
|
2017-12-01 05:49:04 -06:00
|
|
|
err = conn.nodesUp()
|
|
|
|
if err != nil {
|
2018-10-11 13:32:14 -05:00
|
|
|
log.Trace("Nodes not up", "err", err)
|
2017-12-01 05:49:04 -06:00
|
|
|
return nil, fmt.Errorf("nodes not up: %v", err)
|
|
|
|
}
|
2018-10-11 13:32:14 -05:00
|
|
|
log.Debug("Connection initiated", "id", oneID, "other", otherID)
|
2017-12-01 05:49:04 -06:00
|
|
|
conn.initiated = time.Now()
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2017-09-25 03:08:07 -05:00
|
|
|
// Shutdown stops all nodes in the network and closes the quit channel
|
2018-05-08 06:08:43 -05:00
|
|
|
func (net *Network) Shutdown() {
|
|
|
|
for _, node := range net.Nodes {
|
2018-10-11 13:32:14 -05:00
|
|
|
log.Debug("Stopping node", "id", node.ID())
|
2017-09-25 03:08:07 -05:00
|
|
|
if err := node.Stop(); err != nil {
|
2018-10-11 13:32:14 -05:00
|
|
|
log.Warn("Can't stop node", "id", node.ID(), "err", err)
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
close(net.quitc)
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
2019-01-06 04:58:57 -06:00
|
|
|
// Reset resets all network properties:
|
|
|
|
// empties the nodes and the connection list
|
2018-05-08 06:08:43 -05:00
|
|
|
func (net *Network) Reset() {
|
|
|
|
net.lock.Lock()
|
|
|
|
defer net.lock.Unlock()
|
2017-12-12 12:10:41 -06:00
|
|
|
|
|
|
|
//re-initialize the maps
|
2018-05-08 06:08:43 -05:00
|
|
|
net.connMap = make(map[string]int)
|
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-24 17:59:00 -05:00
|
|
|
net.nodeMap = make(map[enode.ID]int)
|
2017-12-12 12:10:41 -06:00
|
|
|
|
2018-05-08 06:08:43 -05:00
|
|
|
net.Nodes = nil
|
|
|
|
net.Conns = nil
|
2017-12-12 12:10:41 -06:00
|
|
|
}
|
|
|
|
|
2017-09-25 03:08:07 -05:00
|
|
|
// Node is a wrapper around adapters.Node which is used to track the status
|
|
|
|
// of a node in the network
|
|
|
|
type Node struct {
|
|
|
|
adapters.Node `json:"-"`
|
|
|
|
|
|
|
|
// Config if the config used to created the node
|
|
|
|
Config *adapters.NodeConfig `json:"config"`
|
|
|
|
|
|
|
|
// Up tracks whether or not the node is running
|
|
|
|
Up bool `json:"up"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the ID of the 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-24 17:59:00 -05:00
|
|
|
func (n *Node) ID() enode.ID {
|
2018-05-08 06:08:43 -05:00
|
|
|
return n.Config.ID
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a log-friendly string
|
2018-05-08 06:08:43 -05:00
|
|
|
func (n *Node) String() string {
|
|
|
|
return fmt.Sprintf("Node %v", n.ID().TerminalString())
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NodeInfo returns information about the node
|
2018-05-08 06:08:43 -05:00
|
|
|
func (n *Node) NodeInfo() *p2p.NodeInfo {
|
2017-09-25 03:08:07 -05:00
|
|
|
// avoid a panic if the node is not started yet
|
2018-05-08 06:08:43 -05:00
|
|
|
if n.Node == nil {
|
2017-09-25 03:08:07 -05:00
|
|
|
return nil
|
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
info := n.Node.NodeInfo()
|
|
|
|
info.Name = n.Config.Name
|
2017-09-25 03:08:07 -05:00
|
|
|
return info
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the json.Marshaler interface so that the encoded
|
|
|
|
// JSON includes the NodeInfo
|
2018-05-08 06:08:43 -05:00
|
|
|
func (n *Node) MarshalJSON() ([]byte, error) {
|
2017-09-25 03:08:07 -05:00
|
|
|
return json.Marshal(struct {
|
|
|
|
Info *p2p.NodeInfo `json:"info,omitempty"`
|
|
|
|
Config *adapters.NodeConfig `json:"config,omitempty"`
|
|
|
|
Up bool `json:"up"`
|
|
|
|
}{
|
2018-05-08 06:08:43 -05:00
|
|
|
Info: n.NodeInfo(),
|
|
|
|
Config: n.Config,
|
|
|
|
Up: n.Up,
|
2017-09-25 03:08:07 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conn represents a connection between two nodes in the network
|
|
|
|
type Conn struct {
|
|
|
|
// One is the node which initiated the connection
|
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-24 17:59:00 -05:00
|
|
|
One enode.ID `json:"one"`
|
2017-09-25 03:08:07 -05:00
|
|
|
|
|
|
|
// Other is the node which the connection was made to
|
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-24 17:59:00 -05:00
|
|
|
Other enode.ID `json:"other"`
|
2017-09-25 03:08:07 -05:00
|
|
|
|
|
|
|
// Up tracks whether or not the connection is active
|
|
|
|
Up bool `json:"up"`
|
2017-12-01 05:49:04 -06:00
|
|
|
// Registers when the connection was grabbed to dial
|
|
|
|
initiated time.Time
|
2017-09-25 03:08:07 -05:00
|
|
|
|
|
|
|
one *Node
|
|
|
|
other *Node
|
|
|
|
}
|
|
|
|
|
|
|
|
// nodesUp returns whether both nodes are currently up
|
2018-05-08 06:08:43 -05:00
|
|
|
func (c *Conn) nodesUp() error {
|
|
|
|
if !c.one.Up {
|
|
|
|
return fmt.Errorf("one %v is not up", c.One)
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
if !c.other.Up {
|
|
|
|
return fmt.Errorf("other %v is not up", c.Other)
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a log-friendly string
|
2018-05-08 06:08:43 -05:00
|
|
|
func (c *Conn) String() string {
|
|
|
|
return fmt.Sprintf("Conn %v->%v", c.One.TerminalString(), c.Other.TerminalString())
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Msg represents a p2p message sent between two nodes in the network
|
|
|
|
type Msg struct {
|
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-24 17:59:00 -05:00
|
|
|
One enode.ID `json:"one"`
|
|
|
|
Other enode.ID `json:"other"`
|
|
|
|
Protocol string `json:"protocol"`
|
|
|
|
Code uint64 `json:"code"`
|
|
|
|
Received bool `json:"received"`
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a log-friendly string
|
2018-05-08 06:08:43 -05:00
|
|
|
func (m *Msg) String() string {
|
|
|
|
return fmt.Sprintf("Msg(%d) %v->%v", m.Code, m.One.TerminalString(), m.Other.TerminalString())
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConnLabel generates a deterministic string which represents a connection
|
|
|
|
// between two nodes, used to compare if two connections are between the same
|
|
|
|
// 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-24 17:59:00 -05:00
|
|
|
func ConnLabel(source, target enode.ID) string {
|
|
|
|
var first, second enode.ID
|
2017-09-25 03:08:07 -05:00
|
|
|
if bytes.Compare(source.Bytes(), target.Bytes()) > 0 {
|
|
|
|
first = target
|
|
|
|
second = source
|
|
|
|
} else {
|
|
|
|
first = source
|
|
|
|
second = target
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%v-%v", first, second)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Snapshot represents the state of a network at a single point in time and can
|
|
|
|
// be used to restore the state of a network
|
|
|
|
type Snapshot struct {
|
|
|
|
Nodes []NodeSnapshot `json:"nodes,omitempty"`
|
|
|
|
Conns []Conn `json:"conns,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeSnapshot represents the state of a node in the network
|
|
|
|
type NodeSnapshot struct {
|
|
|
|
Node Node `json:"node,omitempty"`
|
|
|
|
|
|
|
|
// Snapshots is arbitrary data gathered from calling node.Snapshots()
|
|
|
|
Snapshots map[string][]byte `json:"snapshots,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Snapshot creates a network snapshot
|
2018-05-08 06:08:43 -05:00
|
|
|
func (net *Network) Snapshot() (*Snapshot, error) {
|
2018-11-12 07:57:17 -06:00
|
|
|
return net.snapshot(nil, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (net *Network) SnapshotWithServices(addServices []string, removeServices []string) (*Snapshot, error) {
|
|
|
|
return net.snapshot(addServices, removeServices)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (net *Network) snapshot(addServices []string, removeServices []string) (*Snapshot, error) {
|
2018-05-08 06:08:43 -05:00
|
|
|
net.lock.Lock()
|
|
|
|
defer net.lock.Unlock()
|
2017-09-25 03:08:07 -05:00
|
|
|
snap := &Snapshot{
|
2018-05-08 06:08:43 -05:00
|
|
|
Nodes: make([]NodeSnapshot, len(net.Nodes)),
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
for i, node := range net.Nodes {
|
2017-09-25 03:08:07 -05:00
|
|
|
snap.Nodes[i] = NodeSnapshot{Node: *node}
|
|
|
|
if !node.Up {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
snapshots, err := node.Snapshots()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
snap.Nodes[i].Snapshots = snapshots
|
2018-11-12 07:57:17 -06:00
|
|
|
for _, addSvc := range addServices {
|
|
|
|
haveSvc := false
|
|
|
|
for _, svc := range snap.Nodes[i].Node.Config.Services {
|
|
|
|
if svc == addSvc {
|
|
|
|
haveSvc = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !haveSvc {
|
|
|
|
snap.Nodes[i].Node.Config.Services = append(snap.Nodes[i].Node.Config.Services, addSvc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(removeServices) > 0 {
|
|
|
|
var cleanedServices []string
|
|
|
|
for _, svc := range snap.Nodes[i].Node.Config.Services {
|
|
|
|
haveSvc := false
|
|
|
|
for _, rmSvc := range removeServices {
|
|
|
|
if rmSvc == svc {
|
|
|
|
haveSvc = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !haveSvc {
|
|
|
|
cleanedServices = append(cleanedServices, svc)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
snap.Nodes[i].Node.Config.Services = cleanedServices
|
|
|
|
}
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
2018-11-12 07:57:17 -06:00
|
|
|
for _, conn := range net.Conns {
|
|
|
|
if conn.Up {
|
|
|
|
snap.Conns = append(snap.Conns, *conn)
|
|
|
|
}
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
return snap, nil
|
|
|
|
}
|
|
|
|
|
2018-12-06 23:51:40 -06:00
|
|
|
var snapshotLoadTimeout = 120 * time.Second
|
|
|
|
|
2017-09-25 03:08:07 -05:00
|
|
|
// Load loads a network snapshot
|
2018-05-08 06:08:43 -05:00
|
|
|
func (net *Network) Load(snap *Snapshot) error {
|
2018-12-06 23:51:40 -06:00
|
|
|
// Start nodes.
|
2017-09-25 03:08:07 -05:00
|
|
|
for _, n := range snap.Nodes {
|
2018-05-08 06:08:43 -05:00
|
|
|
if _, err := net.NewNodeWithConfig(n.Node.Config); err != nil {
|
2017-09-25 03:08:07 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !n.Node.Up {
|
|
|
|
continue
|
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
if err := net.startWithSnapshots(n.Node.Config.ID, n.Snapshots); err != nil {
|
2017-09-25 03:08:07 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-12-06 23:51:40 -06:00
|
|
|
|
|
|
|
// Prepare connection events counter.
|
|
|
|
allConnected := make(chan struct{}) // closed when all connections are established
|
|
|
|
done := make(chan struct{}) // ensures that the event loop goroutine is terminated
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
// Subscribe to event channel.
|
|
|
|
// It needs to be done outside of the event loop goroutine (created below)
|
|
|
|
// to ensure that the event channel is blocking before connect calls are made.
|
|
|
|
events := make(chan *Event)
|
|
|
|
sub := net.Events().Subscribe(events)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Expected number of connections.
|
|
|
|
total := len(snap.Conns)
|
|
|
|
// Set of all established connections from the snapshot, not other connections.
|
|
|
|
// Key array element 0 is the connection One field value, and element 1 connection Other field.
|
|
|
|
connections := make(map[[2]enode.ID]struct{}, total)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case e := <-events:
|
|
|
|
// Ignore control events as they do not represent
|
|
|
|
// connect or disconnect (Up) state change.
|
|
|
|
if e.Control {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Detect only connection events.
|
|
|
|
if e.Type != EventTypeConn {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
connection := [2]enode.ID{e.Conn.One, e.Conn.Other}
|
|
|
|
// Nodes are still not connected or have been disconnected.
|
|
|
|
if !e.Conn.Up {
|
|
|
|
// Delete the connection from the set of established connections.
|
|
|
|
// This will prevent false positive in case disconnections happen.
|
|
|
|
delete(connections, connection)
|
|
|
|
log.Warn("load snapshot: unexpected disconnection", "one", e.Conn.One, "other", e.Conn.Other)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Check that the connection is from the snapshot.
|
|
|
|
for _, conn := range snap.Conns {
|
|
|
|
if conn.One == e.Conn.One && conn.Other == e.Conn.Other {
|
|
|
|
// Add the connection to the set of established connections.
|
|
|
|
connections[connection] = struct{}{}
|
|
|
|
if len(connections) == total {
|
|
|
|
// Signal that all nodes are connected.
|
|
|
|
close(allConnected)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-done:
|
|
|
|
// Load function returned, terminate this goroutine.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Start connecting.
|
2017-09-25 03:08:07 -05:00
|
|
|
for _, conn := range snap.Conns {
|
2017-12-12 12:10:41 -06:00
|
|
|
|
2018-05-08 06:08:43 -05:00
|
|
|
if !net.GetNode(conn.One).Up || !net.GetNode(conn.Other).Up {
|
2017-12-12 12:10:41 -06:00
|
|
|
//in this case, at least one of the nodes of a connection is not up,
|
|
|
|
//so it would result in the snapshot `Load` to fail
|
|
|
|
continue
|
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
if err := net.Connect(conn.One, conn.Other); err != nil {
|
2017-09-25 03:08:07 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-12-06 23:51:40 -06:00
|
|
|
|
|
|
|
select {
|
|
|
|
// Wait until all connections from the snapshot are established.
|
|
|
|
case <-allConnected:
|
|
|
|
// Make sure that we do not wait forever.
|
|
|
|
case <-time.After(snapshotLoadTimeout):
|
|
|
|
return errors.New("snapshot connections not established")
|
|
|
|
}
|
2017-09-25 03:08:07 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscribe reads control events from a channel and executes them
|
2018-05-08 06:08:43 -05:00
|
|
|
func (net *Network) Subscribe(events chan *Event) {
|
2017-09-25 03:08:07 -05:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event, ok := <-events:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if event.Control {
|
2018-05-08 06:08:43 -05:00
|
|
|
net.executeControlEvent(event)
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
case <-net.quitc:
|
2017-09-25 03:08:07 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 06:08:43 -05:00
|
|
|
func (net *Network) executeControlEvent(event *Event) {
|
2018-10-11 13:32:14 -05:00
|
|
|
log.Trace("Executing control event", "type", event.Type, "event", event)
|
2017-09-25 03:08:07 -05:00
|
|
|
switch event.Type {
|
|
|
|
case EventTypeNode:
|
2018-05-08 06:08:43 -05:00
|
|
|
if err := net.executeNodeEvent(event); err != nil {
|
2018-10-11 13:32:14 -05:00
|
|
|
log.Error("Error executing node event", "event", event, "err", err)
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
case EventTypeConn:
|
2018-05-08 06:08:43 -05:00
|
|
|
if err := net.executeConnEvent(event); err != nil {
|
2018-10-11 13:32:14 -05:00
|
|
|
log.Error("Error executing conn event", "event", event, "err", err)
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
case EventTypeMsg:
|
2018-10-11 13:32:14 -05:00
|
|
|
log.Warn("Ignoring control msg event")
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 06:08:43 -05:00
|
|
|
func (net *Network) executeNodeEvent(e *Event) error {
|
2017-09-25 03:08:07 -05:00
|
|
|
if !e.Node.Up {
|
2018-05-08 06:08:43 -05:00
|
|
|
return net.Stop(e.Node.ID())
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
2018-05-08 06:08:43 -05:00
|
|
|
if _, err := net.NewNodeWithConfig(e.Node.Config); err != nil {
|
2017-09-25 03:08:07 -05:00
|
|
|
return err
|
|
|
|
}
|
2018-05-08 06:08:43 -05:00
|
|
|
return net.Start(e.Node.ID())
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
|
2018-05-08 06:08:43 -05:00
|
|
|
func (net *Network) executeConnEvent(e *Event) error {
|
2017-09-25 03:08:07 -05:00
|
|
|
if e.Conn.Up {
|
2018-05-08 06:08:43 -05:00
|
|
|
return net.Connect(e.Conn.One, e.Conn.Other)
|
|
|
|
} else {
|
|
|
|
return net.Disconnect(e.Conn.One, e.Conn.Other)
|
2017-09-25 03:08:07 -05:00
|
|
|
}
|
|
|
|
}
|