mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 17:33:47 +00:00
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:
commit
21771c11d1
4 changed files with 42 additions and 5 deletions
|
|
@ -105,9 +105,9 @@ func (e *ExecAdapter) NewNode(config *NodeConfig) (Node, error) {
|
||||||
conf.Stack.P2P.NAT = nil
|
conf.Stack.P2P.NAT = nil
|
||||||
conf.Stack.NoUSB = true
|
conf.Stack.NoUSB = true
|
||||||
|
|
||||||
// listen on a random localhost port (we'll get the actual port after
|
// listen on a localhost port, which we set when we
|
||||||
// starting the node through the RPC admin.nodeInfo method)
|
// initialise NodeConfig (usually a random port)
|
||||||
conf.Stack.P2P.ListenAddr = "127.0.0.1:0"
|
conf.Stack.P2P.ListenAddr = fmt.Sprintf("127.0.0.1:%d", config.Port)
|
||||||
|
|
||||||
node := &ExecNode{
|
node := &ExecNode{
|
||||||
ID: config.ID,
|
ID: config.ID,
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/docker/docker/pkg/reexec"
|
"github.com/docker/docker/pkg/reexec"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
|
|
@ -97,6 +98,8 @@ type NodeConfig struct {
|
||||||
|
|
||||||
// function to sanction or prevent suggesting a peer
|
// function to sanction or prevent suggesting a peer
|
||||||
Reachable func(id discover.NodeID) bool
|
Reachable func(id discover.NodeID) bool
|
||||||
|
|
||||||
|
Port uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
// nodeConfigJSON is used to encode and decode NodeConfig as JSON by encoding
|
// nodeConfigJSON is used to encode and decode NodeConfig as JSON by encoding
|
||||||
|
|
@ -106,6 +109,7 @@ type nodeConfigJSON struct {
|
||||||
PrivateKey string `json:"private_key"`
|
PrivateKey string `json:"private_key"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Services []string `json:"services"`
|
Services []string `json:"services"`
|
||||||
|
Port uint16 `json:"port"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON implements the json.Marshaler interface by encoding the config
|
// MarshalJSON implements the json.Marshaler interface by encoding the config
|
||||||
|
|
@ -115,6 +119,7 @@ func (n *NodeConfig) MarshalJSON() ([]byte, error) {
|
||||||
ID: n.ID.String(),
|
ID: n.ID.String(),
|
||||||
Name: n.Name,
|
Name: n.Name,
|
||||||
Services: n.Services,
|
Services: n.Services,
|
||||||
|
Port: n.Port,
|
||||||
}
|
}
|
||||||
if n.PrivateKey != nil {
|
if n.PrivateKey != nil {
|
||||||
confJSON.PrivateKey = hex.EncodeToString(crypto.FromECDSA(n.PrivateKey))
|
confJSON.PrivateKey = hex.EncodeToString(crypto.FromECDSA(n.PrivateKey))
|
||||||
|
|
@ -152,6 +157,7 @@ func (n *NodeConfig) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
n.Name = confJSON.Name
|
n.Name = confJSON.Name
|
||||||
n.Services = confJSON.Services
|
n.Services = confJSON.Services
|
||||||
|
n.Port = confJSON.Port
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -165,12 +171,34 @@ func RandomNodeConfig() *NodeConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
id := discover.PubkeyID(&key.PublicKey)
|
id := discover.PubkeyID(&key.PublicKey)
|
||||||
|
port, err := assignTCPPort()
|
||||||
|
if err != nil {
|
||||||
|
panic("unable to assign tcp port")
|
||||||
|
}
|
||||||
return &NodeConfig{
|
return &NodeConfig{
|
||||||
ID: id,
|
ID: id,
|
||||||
PrivateKey: key,
|
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
|
// ServiceContext is a collection of options and methods which can be utilised
|
||||||
// when starting services
|
// when starting services
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
|
|
|
||||||
|
|
@ -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
|
// ToOverlayAddr creates an overlayaddress from a byte slice
|
||||||
func ToOverlayAddr(id []byte) []byte {
|
func ToOverlayAddr(id []byte) []byte {
|
||||||
return crypto.Keccak256(id)
|
return crypto.Keccak256(id)
|
||||||
|
|
|
||||||
|
|
@ -139,7 +139,7 @@ func benchmarkDiscovery(b *testing.B, nodes, conns int) {
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
result, err := discoverySimulation(nodes, conns, adapters.NewSimAdapter(services))
|
result, err := discoverySimulation(nodes, conns, adapters.NewSimAdapter(services))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatalf("setting up simulation failed", result)
|
b.Fatalf("setting up simulation failed: %s", err)
|
||||||
}
|
}
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
b.Logf("simulation failed: %s", result.Error)
|
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) {
|
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 := network.NewKadParams()
|
||||||
kp.MinProxBinSize = testMinProxBinSize
|
kp.MinProxBinSize = testMinProxBinSize
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue