Skip to content

Commit

Permalink
Merge pull request ethereum#240 from ethersphere/discovery-tests-exec…
Browse files Browse the repository at this point in the history
…-node

Fix discovery tests to run with all adapters - sim, sock, exec
  • Loading branch information
nonsense committed Feb 8, 2018
2 parents 5fb5298 + 544d0a9 commit 21771c1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
6 changes: 3 additions & 3 deletions p2p/simulations/adapters/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions p2p/simulations/adapters/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"net"
"os"
"strconv"

"github.com/docker/docker/pkg/reexec"
"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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
}
Expand All @@ -165,10 +171,32 @@ 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
Expand Down
9 changes: 9 additions & 0 deletions swarm/network/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions swarm/network/simulations/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 21771c1

Please sign in to comment.