Merge pull request #240 from ethersphere/discovery-tests-exec-node

Fix discovery tests to run with all adapters - sim, sock, exec
This commit is contained in:
Anton Evangelatov 2018-02-08 14:12:49 +01:00 committed by GitHub
commit 21771c11d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 5 deletions

View file

@ -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,

View file

@ -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 {

View file

@ -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)

View file

@ -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