p2p/sim, swarm/network: docker discovery tests

This commit is contained in:
Anton Evangelatov 2018-03-04 13:39:45 +01:00
parent aa46e840bf
commit 90c0190b7e
4 changed files with 29 additions and 9 deletions

View file

@ -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"
)
@ -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 a localhost 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{

View file

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

View file

@ -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()),
}
}

View file

@ -8,6 +8,7 @@ import (
"fmt"
"io/ioutil"
"math/rand"
"net"
"os"
"sync"
"testing"
@ -20,6 +21,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
@ -44,7 +46,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
@ -70,7 +73,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)
}
@ -231,7 +234,7 @@ func discoverySimulation(nodes, conns int, adapter adapters.NodeAdapter) (*simul
// 64 nodes ~ 1min
// 128 nodes ~
timeout := 300 * time.Second
timeout := 60 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
result := simulations.NewSimulation(net).Run(ctx, &simulations.Step{
@ -304,8 +307,23 @@ func triggerChecks(trigger chan discover.NodeID, net *simulations.Network, id di
return nil
}
// getOutboundIP gets preferred outbound ip of this machine/container
func getOutboundIP() net.IP {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
panic(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP
}
func newService(ctx *adapters.ServiceContext) (node.Service, error) {
addr := network.NewAddrFromNodeIDAndPort(ctx.Config.ID, ctx.Config.Port)
host := getOutboundIP()
addr := network.NewAddrFromNodeIDAndPort(ctx.Config.ID, host, ctx.Config.Port)
kp := network.NewKadParams()
kp.MinProxBinSize = testMinProxBinSize