Skip to content

Commit

Permalink
Merge pull request #286 from ethersphere/docker_discovery_tests
Browse files Browse the repository at this point in the history
Fix Docker discovery tests setup
  • Loading branch information
nonsense authored Mar 9, 2018
2 parents aa46e84 + 7328021 commit 9a5a9de
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
6 changes: 4 additions & 2 deletions p2p/simulations/adapters/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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{
Expand Down
35 changes: 18 additions & 17 deletions p2p/simulations/adapters/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions swarm/network/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
}

Expand Down
10 changes: 7 additions & 3 deletions swarm/network/simulations/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9a5a9de

Please sign in to comment.