diff --git a/p2p/simulations/adapters/docker.go b/p2p/simulations/adapters/docker.go index 41c3ecdd1a..d145c46b3a 100644 --- a/p2p/simulations/adapters/docker.go +++ b/p2p/simulations/adapters/docker.go @@ -28,7 +28,6 @@ import ( "strings" "github.com/docker/docker/pkg/reexec" - "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/p2p/discover" ) @@ -99,7 +98,10 @@ func (d *DockerAdapter) NewNode(config *NodeConfig) (Node, error) { conf.Stack.P2P.NoDiscovery = true conf.Stack.P2P.NAT = nil conf.Stack.NoUSB = true - conf.Stack.Logger = log.New("node.id", config.ID.String()) + + // listen on all interfaces on a given port, which we set when we + // initialise NodeConfig (usually a random port) + conf.Stack.P2P.ListenAddr = fmt.Sprintf(":%d", config.Port) node := &DockerNode{ ExecNode: ExecNode{ diff --git a/p2p/simulations/adapters/exec.go b/p2p/simulations/adapters/exec.go index e94e98d008..a147e1e7b0 100644 --- a/p2p/simulations/adapters/exec.go +++ b/p2p/simulations/adapters/exec.go @@ -107,7 +107,7 @@ func (e *ExecAdapter) NewNode(config *NodeConfig) (Node, error) { // listen on a localhost port, which we set when we // initialise NodeConfig (usually a random port) - conf.Stack.P2P.ListenAddr = fmt.Sprintf("127.0.0.1:%d", config.Port) + conf.Stack.P2P.ListenAddr = fmt.Sprintf(":%d", config.Port) node := &ExecNode{ ID: config.ID, @@ -338,6 +338,21 @@ type execNodeConfig struct { PeerAddrs map[string]string `json:"peer_addrs,omitempty"` } +// ExternalIP gets an external IP address so that Enode URL is usable +func ExternalIP() net.IP { + addrs, err := net.InterfaceAddrs() + if err != nil { + log.Crit("error getting IP address", "err", err) + } + for _, addr := range addrs { + if ip, ok := addr.(*net.IPNet); ok && !ip.IP.IsLoopback() && !ip.IP.IsLinkLocalUnicast() { + return ip.IP + } + } + log.Crit("unable to determine explicit IP address") + return net.IP{127, 0, 0, 1} +} + // execP2PNode starts a devp2p node when the current binary is executed with // argv[0] being "p2p-node", reading the service / ID from argv[1] / argv[2] // and the node config from the _P2P_NODE_CONFIG environment variable @@ -361,25 +376,11 @@ func execP2PNode() { conf.Stack.P2P.PrivateKey = conf.Node.PrivateKey conf.Stack.Logger = log.New("node.id", conf.Node.ID.String()) - // use explicit IP address in ListenAddr so that Enode URL is usable - externalIP := func() string { - addrs, err := net.InterfaceAddrs() - if err != nil { - log.Crit("error getting IP address", "err", err) - } - for _, addr := range addrs { - if ip, ok := addr.(*net.IPNet); ok && !ip.IP.IsLoopback() { - return ip.IP.String() - } - } - log.Crit("unable to determine explicit IP address") - return "" - } if strings.HasPrefix(conf.Stack.P2P.ListenAddr, ":") { - conf.Stack.P2P.ListenAddr = externalIP() + conf.Stack.P2P.ListenAddr + conf.Stack.P2P.ListenAddr = ExternalIP().String() + conf.Stack.P2P.ListenAddr } if conf.Stack.WSHost == "0.0.0.0" { - conf.Stack.WSHost = externalIP() + conf.Stack.WSHost = ExternalIP().String() } // initialize the devp2p stack diff --git a/swarm/network/protocol.go b/swarm/network/protocol.go index d90ba22f30..ac022c2e86 100644 --- a/swarm/network/protocol.go +++ b/swarm/network/protocol.go @@ -427,10 +427,10 @@ func NewAddrFromNodeID(id discover.NodeID) *BzzAddr { // NewAddrFromNodeIDAndPort constucts a BzzAddr from a discover.NodeID and port uint16 // the overlay address is derived as the hash of the nodeID -func NewAddrFromNodeIDAndPort(id discover.NodeID, port uint16) *BzzAddr { +func NewAddrFromNodeIDAndPort(id discover.NodeID, host net.IP, port uint16) *BzzAddr { return &BzzAddr{ OAddr: ToOverlayAddr(id.Bytes()), - UAddr: []byte(discover.NewNode(id, net.IP{127, 0, 0, 1}, port, port).String()), + UAddr: []byte(discover.NewNode(id, host, port, port).String()), } } diff --git a/swarm/network/simulations/discovery/discovery_test.go b/swarm/network/simulations/discovery/discovery_test.go index 5a29028f47..4372788d47 100644 --- a/swarm/network/simulations/discovery/discovery_test.go +++ b/swarm/network/simulations/discovery/discovery_test.go @@ -20,6 +20,7 @@ import ( "github.com/ethereum/go-ethereum/p2p/simulations" "github.com/ethereum/go-ethereum/p2p/simulations/adapters" "github.com/ethereum/go-ethereum/swarm/network" + colorable "github.com/mattn/go-colorable" ) // serviceName is used with the exec adapter so the exec'd binary knows which @@ -44,7 +45,8 @@ func init() { // protocol when using the exec adapter adapters.RegisterServices(services) - log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(os.Stderr, log.TerminalFormat(false)))) + log.PrintOrigins(true) + log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true)))) } // Benchmarks to test the average time it takes for an N-node ring @@ -70,7 +72,7 @@ func BenchmarkDiscovery_64_4(b *testing.B) { benchmarkDiscovery(b, 64, 4) } func BenchmarkDiscovery_128_4(b *testing.B) { benchmarkDiscovery(b, 128, 4) } func BenchmarkDiscovery_256_4(b *testing.B) { benchmarkDiscovery(b, 256, 4) } -func XTestDiscoverySimulationDockerAdapter(t *testing.T) { +func TestDiscoverySimulationDockerAdapter(t *testing.T) { testDiscoverySimulationDockerAdapter(t, *nodeCount, *initCount) } @@ -305,7 +307,9 @@ func triggerChecks(trigger chan discover.NodeID, net *simulations.Network, id di } func newService(ctx *adapters.ServiceContext) (node.Service, error) { - addr := network.NewAddrFromNodeIDAndPort(ctx.Config.ID, ctx.Config.Port) + host := adapters.ExternalIP() + + addr := network.NewAddrFromNodeIDAndPort(ctx.Config.ID, host, ctx.Config.Port) kp := network.NewKadParams() kp.MinProxBinSize = testMinProxBinSize