2018-02-14 15:49:11 +03:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
2017-06-21 15:54:23 +03:00
|
|
|
// This file is part of go-ethereum.
|
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// go-ethereum 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 General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-08-09 17:15:59 +03:00
|
|
|
"context"
|
2018-08-15 18:41:52 +03:00
|
|
|
"crypto/ecdsa"
|
2017-06-21 15:54:23 +03:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
2018-08-15 18:41:52 +03:00
|
|
|
"path"
|
2017-06-21 15:54:23 +03:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2018-08-09 17:15:59 +03:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
2017-06-21 15:54:23 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2017-12-12 00:56:06 +03:00
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
2017-06-21 15:54:23 +03:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
|
|
|
"github.com/ethereum/go-ethereum/internal/cmdtest"
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
"github.com/ethereum/go-ethereum/swarm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Run the app if we've been exec'd as "swarm-test" in runSwarm.
|
|
|
|
reexec.Register("swarm-test", func() {
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
// check if we have been reexec'd
|
|
|
|
if reexec.Init() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
|
|
|
func runSwarm(t *testing.T, args ...string) *cmdtest.TestCmd {
|
|
|
|
tt := cmdtest.NewTestCmd(t, nil)
|
|
|
|
|
|
|
|
// Boot "swarm". This actually runs the test binary but the TestMain
|
|
|
|
// function will prevent any tests from running.
|
|
|
|
tt.Run("swarm-test", args...)
|
|
|
|
|
|
|
|
return tt
|
|
|
|
}
|
|
|
|
|
|
|
|
type testCluster struct {
|
|
|
|
Nodes []*testNode
|
|
|
|
TmpDir string
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTestCluster starts a test swarm cluster of the given size.
|
|
|
|
//
|
|
|
|
// A temporary directory is created and each node gets a data directory inside
|
|
|
|
// it.
|
|
|
|
//
|
|
|
|
// Each node listens on 127.0.0.1 with random ports for both the HTTP and p2p
|
|
|
|
// ports (assigned by first listening on 127.0.0.1:0 and then passing the ports
|
|
|
|
// as flags).
|
|
|
|
//
|
|
|
|
// When starting more than one node, they are connected together using the
|
|
|
|
// admin SetPeer RPC method.
|
2018-06-20 15:06:27 +03:00
|
|
|
|
2017-06-21 15:54:23 +03:00
|
|
|
func newTestCluster(t *testing.T, size int) *testCluster {
|
|
|
|
cluster := &testCluster{}
|
|
|
|
defer func() {
|
|
|
|
if t.Failed() {
|
|
|
|
cluster.Shutdown()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "swarm-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cluster.TmpDir = tmpdir
|
|
|
|
|
|
|
|
// start the nodes
|
2018-06-20 15:06:27 +03:00
|
|
|
cluster.StartNewNodes(t, size)
|
2017-06-21 15:54:23 +03:00
|
|
|
|
|
|
|
if size == 1 {
|
|
|
|
return cluster
|
|
|
|
}
|
|
|
|
|
|
|
|
// connect the nodes together
|
|
|
|
for _, node := range cluster.Nodes {
|
|
|
|
if err := node.Client.Call(nil, "admin_addPeer", cluster.Nodes[0].Enode); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait until all nodes have the correct number of peers
|
|
|
|
outer:
|
|
|
|
for _, node := range cluster.Nodes {
|
|
|
|
var peers []*p2p.PeerInfo
|
|
|
|
for start := time.Now(); time.Since(start) < time.Minute; time.Sleep(50 * time.Millisecond) {
|
|
|
|
if err := node.Client.Call(&peers, "admin_peers"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(peers) == len(cluster.Nodes)-1 {
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Fatalf("%s only has %d / %d peers", node.Name, len(peers), len(cluster.Nodes)-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cluster
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testCluster) Shutdown() {
|
|
|
|
for _, node := range c.Nodes {
|
|
|
|
node.Shutdown()
|
|
|
|
}
|
|
|
|
os.RemoveAll(c.TmpDir)
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
func (c *testCluster) Stop() {
|
|
|
|
for _, node := range c.Nodes {
|
|
|
|
node.Shutdown()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testCluster) StartNewNodes(t *testing.T, size int) {
|
|
|
|
c.Nodes = make([]*testNode, 0, size)
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
dir := filepath.Join(c.TmpDir, fmt.Sprintf("swarm%02d", i))
|
|
|
|
if err := os.Mkdir(dir, 0700); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
node := newTestNode(t, dir)
|
|
|
|
node.Name = fmt.Sprintf("swarm%02d", i)
|
|
|
|
|
|
|
|
c.Nodes = append(c.Nodes, node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testCluster) StartExistingNodes(t *testing.T, size int, bzzaccount string) {
|
|
|
|
c.Nodes = make([]*testNode, 0, size)
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
dir := filepath.Join(c.TmpDir, fmt.Sprintf("swarm%02d", i))
|
|
|
|
node := existingTestNode(t, dir, bzzaccount)
|
|
|
|
node.Name = fmt.Sprintf("swarm%02d", i)
|
|
|
|
|
|
|
|
c.Nodes = append(c.Nodes, node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testCluster) Cleanup() {
|
|
|
|
os.RemoveAll(c.TmpDir)
|
|
|
|
}
|
|
|
|
|
2017-06-21 15:54:23 +03:00
|
|
|
type testNode struct {
|
2018-08-15 18:41:52 +03:00
|
|
|
Name string
|
|
|
|
Addr string
|
|
|
|
URL string
|
|
|
|
Enode string
|
|
|
|
Dir string
|
|
|
|
IpcPath string
|
|
|
|
PrivateKey *ecdsa.PrivateKey
|
|
|
|
Client *rpc.Client
|
|
|
|
Cmd *cmdtest.TestCmd
|
2017-06-21 15:54:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const testPassphrase = "swarm-test-passphrase"
|
|
|
|
|
2017-12-12 00:56:06 +03:00
|
|
|
func getTestAccount(t *testing.T, dir string) (conf *node.Config, account accounts.Account) {
|
2017-06-21 15:54:23 +03:00
|
|
|
// create key
|
2017-12-12 00:56:06 +03:00
|
|
|
conf = &node.Config{
|
2017-06-21 15:54:23 +03:00
|
|
|
DataDir: dir,
|
|
|
|
IPCPath: "bzzd.ipc",
|
2017-07-31 16:58:19 +03:00
|
|
|
NoUSB: true,
|
2017-06-21 15:54:23 +03:00
|
|
|
}
|
|
|
|
n, err := node.New(conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-12-12 00:56:06 +03:00
|
|
|
account, err = n.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore).NewAccount(testPassphrase)
|
2017-06-21 15:54:23 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// use a unique IPCPath when running tests on Windows
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
conf.IPCPath = fmt.Sprintf("bzzd-%s.ipc", account.Address.String())
|
|
|
|
}
|
|
|
|
|
2017-12-12 00:56:06 +03:00
|
|
|
return conf, account
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
func existingTestNode(t *testing.T, dir string, bzzaccount string) *testNode {
|
|
|
|
conf, _ := getTestAccount(t, dir)
|
|
|
|
node := &testNode{Dir: dir}
|
|
|
|
|
|
|
|
// use a unique IPCPath when running tests on Windows
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
conf.IPCPath = fmt.Sprintf("bzzd-%s.ipc", bzzaccount)
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign ports
|
2018-08-09 17:15:59 +03:00
|
|
|
ports, err := getAvailableTCPPorts(2)
|
2018-06-20 15:06:27 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-08-09 17:15:59 +03:00
|
|
|
p2pPort := ports[0]
|
|
|
|
httpPort := ports[1]
|
2018-06-20 15:06:27 +03:00
|
|
|
|
|
|
|
// start the node
|
|
|
|
node.Cmd = runSwarm(t,
|
|
|
|
"--port", p2pPort,
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
"--nat", "extip:127.0.0.1",
|
2018-06-20 15:06:27 +03:00
|
|
|
"--nodiscover",
|
|
|
|
"--datadir", dir,
|
|
|
|
"--ipcpath", conf.IPCPath,
|
|
|
|
"--ens-api", "",
|
|
|
|
"--bzzaccount", bzzaccount,
|
|
|
|
"--bzznetworkid", "321",
|
|
|
|
"--bzzport", httpPort,
|
2018-09-28 23:49:15 +03:00
|
|
|
"--verbosity", fmt.Sprint(*loglevel),
|
2018-06-20 15:06:27 +03:00
|
|
|
)
|
|
|
|
node.Cmd.InputLine(testPassphrase)
|
|
|
|
defer func() {
|
|
|
|
if t.Failed() {
|
|
|
|
node.Shutdown()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-08-09 17:15:59 +03:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// ensure that all ports have active listeners
|
|
|
|
// so that the next node will not get the same
|
|
|
|
// when calling getAvailableTCPPorts
|
|
|
|
err = waitTCPPorts(ctx, ports...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// wait for the node to start
|
|
|
|
for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
|
|
|
|
node.Client, err = rpc.Dial(conf.IPCEndpoint())
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if node.Client == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// load info
|
|
|
|
var info swarm.Info
|
|
|
|
if err := node.Client.Call(&info, "bzz_info"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
node.Addr = net.JoinHostPort("127.0.0.1", info.Port)
|
|
|
|
node.URL = "http://" + node.Addr
|
|
|
|
|
|
|
|
var nodeInfo p2p.NodeInfo
|
|
|
|
if err := node.Client.Call(&nodeInfo, "admin_nodeInfo"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
node.Enode = nodeInfo.Enode
|
|
|
|
node.IpcPath = conf.IPCPath
|
2018-06-20 15:06:27 +03:00
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2017-12-12 00:56:06 +03:00
|
|
|
func newTestNode(t *testing.T, dir string) *testNode {
|
|
|
|
|
|
|
|
conf, account := getTestAccount(t, dir)
|
2018-08-15 18:41:52 +03:00
|
|
|
ks := keystore.NewKeyStore(path.Join(dir, "keystore"), 1<<18, 1)
|
|
|
|
|
|
|
|
pk := decryptStoreAccount(ks, account.Address.Hex(), []string{testPassphrase})
|
|
|
|
|
|
|
|
node := &testNode{Dir: dir, PrivateKey: pk}
|
2017-12-12 00:56:06 +03:00
|
|
|
|
2017-06-21 15:54:23 +03:00
|
|
|
// assign ports
|
2018-08-09 17:15:59 +03:00
|
|
|
ports, err := getAvailableTCPPorts(2)
|
2017-06-21 15:54:23 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-08-09 17:15:59 +03:00
|
|
|
p2pPort := ports[0]
|
|
|
|
httpPort := ports[1]
|
2017-06-21 15:54:23 +03:00
|
|
|
|
|
|
|
// start the node
|
|
|
|
node.Cmd = runSwarm(t,
|
|
|
|
"--port", p2pPort,
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
"--nat", "extip:127.0.0.1",
|
2017-06-21 15:54:23 +03:00
|
|
|
"--nodiscover",
|
|
|
|
"--datadir", dir,
|
|
|
|
"--ipcpath", conf.IPCPath,
|
2017-06-30 12:10:41 +03:00
|
|
|
"--ens-api", "",
|
2017-06-21 15:54:23 +03:00
|
|
|
"--bzzaccount", account.Address.String(),
|
|
|
|
"--bzznetworkid", "321",
|
|
|
|
"--bzzport", httpPort,
|
2018-09-28 23:49:15 +03:00
|
|
|
"--verbosity", fmt.Sprint(*loglevel),
|
2017-06-21 15:54:23 +03:00
|
|
|
)
|
|
|
|
node.Cmd.InputLine(testPassphrase)
|
|
|
|
defer func() {
|
|
|
|
if t.Failed() {
|
|
|
|
node.Shutdown()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-08-09 17:15:59 +03:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// ensure that all ports have active listeners
|
|
|
|
// so that the next node will not get the same
|
|
|
|
// when calling getAvailableTCPPorts
|
|
|
|
err = waitTCPPorts(ctx, ports...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-21 15:54:23 +03:00
|
|
|
// wait for the node to start
|
|
|
|
for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
|
|
|
|
node.Client, err = rpc.Dial(conf.IPCEndpoint())
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if node.Client == nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// load info
|
|
|
|
var info swarm.Info
|
|
|
|
if err := node.Client.Call(&info, "bzz_info"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
node.Addr = net.JoinHostPort("127.0.0.1", info.Port)
|
|
|
|
node.URL = "http://" + node.Addr
|
|
|
|
|
|
|
|
var nodeInfo p2p.NodeInfo
|
|
|
|
if err := node.Client.Call(&nodeInfo, "admin_nodeInfo"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
node.Enode = nodeInfo.Enode
|
2018-06-20 15:06:27 +03:00
|
|
|
node.IpcPath = conf.IPCPath
|
2017-06-21 15:54:23 +03:00
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *testNode) Shutdown() {
|
|
|
|
if n.Cmd != nil {
|
|
|
|
n.Cmd.Kill()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 17:15:59 +03:00
|
|
|
// getAvailableTCPPorts returns a set of ports that
|
|
|
|
// nothing is listening on at the time.
|
|
|
|
//
|
|
|
|
// Function assignTCPPort cannot be called in sequence
|
|
|
|
// and guardantee that the same port will be returned in
|
|
|
|
// different calls as the listener is closed within the function,
|
|
|
|
// not after all listeners are started and selected unique
|
|
|
|
// available ports.
|
|
|
|
func getAvailableTCPPorts(count int) (ports []string, err error) {
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// defer close in the loop to be sure the same port will not
|
|
|
|
// be selected in the next iteration
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
_, port, err := net.SplitHostPort(l.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ports = append(ports, port)
|
2017-06-21 15:54:23 +03:00
|
|
|
}
|
2018-08-09 17:15:59 +03:00
|
|
|
return ports, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// waitTCPPorts blocks until tcp connections can be
|
|
|
|
// established on all provided ports. It runs all
|
|
|
|
// ports dialers in parallel, and returns the first
|
|
|
|
// encountered error.
|
|
|
|
// See waitTCPPort also.
|
|
|
|
func waitTCPPorts(ctx context.Context, ports ...string) error {
|
|
|
|
var err error
|
|
|
|
// mu locks err variable that is assigned in
|
|
|
|
// other goroutines
|
|
|
|
var mu sync.Mutex
|
|
|
|
|
|
|
|
// cancel is canceling all goroutines
|
|
|
|
// when the firs error is returned
|
|
|
|
// to prevent unnecessary waiting
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, port := range ports {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(port string) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
e := waitTCPPort(ctx, port)
|
|
|
|
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
if e != nil && err == nil {
|
|
|
|
err = e
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}(port)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// waitTCPPort blocks until tcp connection can be established
|
|
|
|
// ona provided port. It has a 3 minute timeout as maximum,
|
|
|
|
// to prevent long waiting, but it can be shortened with
|
|
|
|
// a provided context instance. Dialer has a 10 second timeout
|
|
|
|
// in every iteration, and connection refused error will be
|
|
|
|
// retried in 100 milliseconds periods.
|
|
|
|
func waitTCPPort(ctx context.Context, port string) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 3*time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
for {
|
|
|
|
c, err := (&net.Dialer{Timeout: 10 * time.Second}).DialContext(ctx, "tcp", "127.0.0.1:"+port)
|
|
|
|
if err != nil {
|
|
|
|
if operr, ok := err.(*net.OpError); ok {
|
|
|
|
if syserr, ok := operr.Err.(*os.SyscallError); ok && syserr.Err == syscall.ECONNREFUSED {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.Close()
|
2017-06-21 15:54:23 +03:00
|
|
|
}
|
|
|
|
}
|