diff --git a/p2p/simulations/adapters/exec.go b/p2p/simulations/adapters/exec.go index a566fb27d8..f34ac33043 100644 --- a/p2p/simulations/adapters/exec.go +++ b/p2p/simulations/adapters/exec.go @@ -105,9 +105,9 @@ func (e *ExecAdapter) NewNode(config *NodeConfig) (Node, error) { conf.Stack.P2P.NAT = nil conf.Stack.NoUSB = true - // listen on a random localhost port (we'll get the actual port after - // starting the node through the RPC admin.nodeInfo method) - conf.Stack.P2P.ListenAddr = "127.0.0.1:0" + // 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) node := &ExecNode{ ID: config.ID, diff --git a/p2p/simulations/adapters/types.go b/p2p/simulations/adapters/types.go index 3f76b19843..2169d68308 100644 --- a/p2p/simulations/adapters/types.go +++ b/p2p/simulations/adapters/types.go @@ -23,6 +23,7 @@ import ( "fmt" "net" "os" + "strconv" "github.com/docker/docker/pkg/reexec" "github.com/ethereum/go-ethereum/crypto" @@ -97,6 +98,8 @@ type NodeConfig struct { // function to sanction or prevent suggesting a peer Reachable func(id discover.NodeID) bool + + Port uint16 } // nodeConfigJSON is used to encode and decode NodeConfig as JSON by encoding @@ -106,6 +109,7 @@ type nodeConfigJSON struct { PrivateKey string `json:"private_key"` Name string `json:"name"` Services []string `json:"services"` + Port uint16 `json:"port"` } // MarshalJSON implements the json.Marshaler interface by encoding the config @@ -115,6 +119,7 @@ func (n *NodeConfig) MarshalJSON() ([]byte, error) { ID: n.ID.String(), Name: n.Name, Services: n.Services, + Port: n.Port, } if n.PrivateKey != nil { confJSON.PrivateKey = hex.EncodeToString(crypto.FromECDSA(n.PrivateKey)) @@ -152,6 +157,7 @@ func (n *NodeConfig) UnmarshalJSON(data []byte) error { n.Name = confJSON.Name n.Services = confJSON.Services + n.Port = confJSON.Port return nil } @@ -165,12 +171,34 @@ func RandomNodeConfig() *NodeConfig { } id := discover.PubkeyID(&key.PublicKey) + port, err := assignTCPPort() + if err != nil { + panic("unable to assign tcp port") + } return &NodeConfig{ ID: id, PrivateKey: key, + Port: port, } } +func assignTCPPort() (uint16, error) { + l, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + return 0, err + } + l.Close() + _, port, err := net.SplitHostPort(l.Addr().String()) + if err != nil { + return 0, err + } + p, err := strconv.ParseInt(port, 10, 32) + if err != nil { + return 0, err + } + return uint16(p), nil +} + // ServiceContext is a collection of options and methods which can be utilised // when starting services type ServiceContext struct { diff --git a/swarm/network/protocol.go b/swarm/network/protocol.go index 9afa69c3a9..448e722269 100644 --- a/swarm/network/protocol.go +++ b/swarm/network/protocol.go @@ -402,6 +402,15 @@ 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 { + return &BzzAddr{ + OAddr: ToOverlayAddr(id.Bytes()), + UAddr: []byte(discover.NewNode(id, net.IP{127, 0, 0, 1}, port, port).String()), + } +} + // ToOverlayAddr creates an overlayaddress from a byte slice func ToOverlayAddr(id []byte) []byte { return crypto.Keccak256(id) diff --git a/swarm/network/simulations/discovery/discovery_test.go b/swarm/network/simulations/discovery/discovery_test.go index 63674c3c02..cc4373483b 100644 --- a/swarm/network/simulations/discovery/discovery_test.go +++ b/swarm/network/simulations/discovery/discovery_test.go @@ -139,7 +139,7 @@ func benchmarkDiscovery(b *testing.B, nodes, conns int) { for i := 0; i < b.N; i++ { result, err := discoverySimulation(nodes, conns, adapters.NewSimAdapter(services)) if err != nil { - b.Fatalf("setting up simulation failed", result) + b.Fatalf("setting up simulation failed: %s", err) } if result.Error != nil { b.Logf("simulation failed: %s", result.Error) @@ -297,7 +297,7 @@ func triggerChecks(trigger chan discover.NodeID, net *simulations.Network, id di } func newService(ctx *adapters.ServiceContext) (node.Service, error) { - addr := network.NewAddrFromNodeID(ctx.Config.ID) + addr := network.NewAddrFromNodeIDAndPort(ctx.Config.ID, ctx.Config.Port) kp := network.NewKadParams() kp.MinProxBinSize = testMinProxBinSize