Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

pss: Port tests to network/simulation #1682

Merged
merged 3 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pss/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/ethersphere/swarm/log"
"github.com/ethersphere/swarm/network/simulation"
)

// asymmetrical key exchange between two directly connected peers
Expand All @@ -43,9 +44,11 @@ func testHandshake(t *testing.T) {
addrsizestring := strings.Split(t.Name(), "/")
addrsize, _ = strconv.ParseInt(addrsizestring[1], 10, 0)

sim := simulation.NewInProc(newServices(false))
defer sim.Close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The simulation constructor seems to belong inside the setupNetwork

// set up two nodes directly connected
// (we are not testing pss routing here)
clients, err := setupNetwork(2, true)
clients, err := setupNetwork(sim, 2)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 5 additions & 1 deletion pss/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethersphere/swarm/log"
"github.com/ethersphere/swarm/network/simulation"
)

type protoCtrl struct {
Expand All @@ -54,7 +55,10 @@ func testProtocol(t *testing.T) {

topic := PingTopic.String()

clients, err := setupNetwork(2, false)
sim := simulation.NewInProc(newServices(false))
defer sim.Close()

clients, err := setupNetwork(sim, 2)
if err != nil {
t.Fatal(err)
}
Expand Down
190 changes: 89 additions & 101 deletions pss/pss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ import (
"crypto/ecdsa"
"encoding/binary"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"math/rand"
"os"
"strconv"
Expand All @@ -41,11 +39,12 @@ import (
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/simulations"
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
"github.com/ethereum/go-ethereum/rpc"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"

"github.com/ethersphere/swarm/network"
"github.com/ethersphere/swarm/network/simulation"
"github.com/ethersphere/swarm/p2p/protocols"
"github.com/ethersphere/swarm/pot"
"github.com/ethersphere/swarm/state"
Expand All @@ -68,8 +67,6 @@ var (
func init() {
flag.Parse()
rand.Seed(time.Now().Unix())

adapters.RegisterServices(newServices(false))
initTest()
}

Expand Down Expand Up @@ -879,7 +876,10 @@ func TestRawAllow(t *testing.T) {

// tests that the API layer can handle edge case values
func TestApi(t *testing.T) {
clients, err := setupNetwork(2, true)
sim := simulation.NewInProc(newServices(true))
defer sim.Close()

clients, err := setupNetwork(sim, 2)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -920,7 +920,10 @@ func testSendRaw(t *testing.T) {
addrsize, _ = strconv.ParseInt(paramstring[1], 10, 0)
log.Info("raw send test", "addrsize", addrsize)

clients, err := setupNetwork(2, true)
sim := simulation.NewInProc(newServices(true))
defer sim.Close()

clients, err := setupNetwork(sim, 2)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1002,7 +1005,10 @@ func testSendSym(t *testing.T) {
addrsize, _ = strconv.ParseInt(paramstring[1], 10, 0)
log.Info("sym send test", "addrsize", addrsize)

clients, err := setupNetwork(2, false)
sim := simulation.NewInProc(newServices(false))
defer sim.Close()

clients, err := setupNetwork(sim, 2)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1117,7 +1123,10 @@ func testSendAsym(t *testing.T) {
addrsize, _ = strconv.ParseInt(paramstring[1], 10, 0)
log.Info("asym send test", "addrsize", addrsize)

clients, err := setupNetwork(2, false)
sim := simulation.NewInProc(newServices(false))
defer sim.Close()

clients, err := setupNetwork(sim, 2)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1278,37 +1287,20 @@ func testNetwork(t *testing.T) {

trigger := make(chan enode.ID)

var a adapters.NodeAdapter
var sim = &simulation.Simulation{}
if adapter == "exec" {
dirname, err := ioutil.TempDir(".", "")
if err != nil {
t.Fatal(err)
}
a = adapters.NewExecAdapter(dirname)
} else if adapter == "tcp" {
a = adapters.NewTCPAdapter(newServices(false))
} else if adapter == "sim" {
a = adapters.NewSimAdapter(newServices(false))
}
net := simulations.NewNetwork(a, &simulations.NetworkConfig{
ID: "0",
})
defer net.Shutdown()

f, err := os.Open(fmt.Sprintf("testdata/snapshot_%d.json", nodecount))
if err != nil {
t.Fatal(err)
}
jsonbyte, err := ioutil.ReadAll(f)
if err != nil {
t.Fatal(err)
}
var snap simulations.Snapshot
err = json.Unmarshal(jsonbyte, &snap)
if err != nil {
t.Fatal(err)
sim, _ = simulation.NewExec(newServices(false))
} else if adapter == "tcp" || adapter == "sim" {
sim = simulation.NewInProc(newServices(false))
}
err = net.Load(&snap)
defer sim.Close()

net := sim.Net

ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()

err := sim.UploadSnapshot(ctx, fmt.Sprintf("testdata/snapshot_%d.json", nodecount))
if err != nil {
//TODO: Fix p2p simulation framework to not crash when loading 32-nodes
//t.Fatal(err)
Expand Down Expand Up @@ -1412,8 +1404,6 @@ func testNetwork(t *testing.T) {
}

finalmsgcount := 0
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
outer:
for i := 0; i < int(msgcount); i++ {
select {
Expand Down Expand Up @@ -1444,7 +1434,10 @@ outer:
func TestDeduplication(t *testing.T) {
var err error

clients, err := setupNetwork(3, false)
sim := simulation.NewInProc(newServices(false))
defer sim.Close()

clients, err := setupNetwork(sim, 3)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1693,7 +1686,6 @@ func benchmarkSymkeyBruteforceChangeaddr(b *testing.B) {
}
}
}

func BenchmarkSymkeyBruteforceSameaddr(b *testing.B) {
for i := 100; i < 100000; i = i * 10 {
for j := 32; j < 10000; j = j * 8 {
Expand Down Expand Up @@ -1777,55 +1769,29 @@ func benchmarkSymkeyBruteforceSameaddr(b *testing.B) {
}
}

// setup simulated network with bzz/discovery and pss services.
// setup network in a simulation
// connects nodes in a circle
// if allowRaw is set, omission of builtin pss encryption is enabled (see PssParams)
func setupNetwork(numnodes int, allowRaw bool) (clients []*rpc.Client, err error) {
nodes := make([]*simulations.Node, numnodes)
func setupNetwork(sim *simulation.Simulation, numnodes int) (clients []*rpc.Client, err error) {
clients = make([]*rpc.Client, numnodes)
if numnodes < 2 {
return nil, fmt.Errorf("Minimum two nodes in network")
}
adapter := adapters.NewSimAdapter(newServices(allowRaw))
net := simulations.NewNetwork(adapter, &simulations.NetworkConfig{
ID: "0",
DefaultService: "bzz",
})
for i := 0; i < numnodes; i++ {
nodeconf := adapters.RandomNodeConfig()
nodeconf.Services = []string{"bzz", protocolName}
nodes[i], err = net.NewNodeWithConfig(nodeconf)
if err != nil {
return nil, fmt.Errorf("error creating node 1: %v", err)
}
err = net.Start(nodes[i].ID())
if err != nil {
return nil, fmt.Errorf("error starting node 1: %v", err)
}
if i > 0 {
err = net.Connect(nodes[i].ID(), nodes[i-1].ID())
if err != nil {
return nil, fmt.Errorf("error connecting nodes: %v", err)
}
}
clients[i], err = nodes[i].Client()
if err != nil {
return nil, fmt.Errorf("create node 1 rpc client fail: %v", err)
}
return nil, fmt.Errorf("minimum two nodes in network")
}
if numnodes > 2 {
err = net.Connect(nodes[0].ID(), nodes[len(nodes)-1].ID())
sim.AddNodesAndConnectRing(numnodes) //Already starts the nodes
nodes := sim.Net.GetNodes()
for id, node := range nodes {
client, err := node.Client()
if err != nil {
return nil, fmt.Errorf("error connecting first and last nodes")
return nil, fmt.Errorf("error getting the nodes clients")
}
clients[id] = client
}
return clients, nil
}

func newServices(allowRaw bool) adapters.Services {
func newServices(allowRaw bool) map[string]simulation.ServiceFunc {
stateStore := state.NewInmemoryStore()
kademlias := make(map[enode.ID]*network.Kademlia)
kademlia := func(id enode.ID) *network.Kademlia {
kademlia := func(id enode.ID, bzzKey []byte) *network.Kademlia {
if k, ok := kademlias[id]; ok {
return k
}
Expand All @@ -1836,11 +1802,30 @@ func newServices(allowRaw bool) adapters.Services {
params.MaxRetries = 1000
params.RetryExponent = 2
params.RetryInterval = 1000000
kademlias[id] = network.NewKademlia(id[:], params)
kademlias[id] = network.NewKademlia(bzzKey, params)
return kademlias[id]
}
return adapters.Services{
protocolName: func(ctx *adapters.ServiceContext) (node.Service, error) {
return map[string]simulation.ServiceFunc{
"bzz": func(ctx *adapters.ServiceContext, bucket *sync.Map) (s node.Service, cleanup func(), err error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't move blocks of code when making changes in the future. It's easier to compare if you leave them where they are, then we can always reorganize the blocks separately.

addr := network.NewAddr(ctx.Config.Node())
bzzPrivateKey, err := simulation.BzzPrivateKeyFromConfig(ctx.Config)
if err != nil {
return nil, nil, err
}
addr.OAddr = network.PrivateKeyToBzzKey(bzzPrivateKey)
nolash marked this conversation as resolved.
Show resolved Hide resolved
bucket.Store(simulation.BucketKeyBzzPrivateKey, bzzPrivateKey)
hp := network.NewHiveParams()
hp.Discovery = false
config := &network.BzzConfig{
OverlayAddr: addr.Over(),
UnderlayAddr: addr.Under(),
HiveParams: hp,
}
pskad := kademlia(ctx.Config.ID, addr.OAddr)
bucket.Store(simulation.BucketKeyKademlia, pskad)
return network.NewBzz(config, pskad, stateStore, nil, nil), nil, nil
},
protocolName: func(ctx *adapters.ServiceContext, bucket *sync.Map) (node.Service, func(), error) {
// execadapter does not exec init()
initTest()

Expand All @@ -1850,30 +1835,36 @@ func newServices(allowRaw bool) adapters.Services {
privkey, err := w.GetPrivateKey(keys)
pssp := NewParams().WithPrivateKey(privkey)
pssp.AllowRaw = allowRaw
pskad := kademlia(ctx.Config.ID)
bzzPrivateKey, err := simulation.BzzPrivateKeyFromConfig(ctx.Config)
if err != nil {
return nil, nil, err
}
bzzKey := network.PrivateKeyToBzzKey(bzzPrivateKey)
pskad := kademlia(ctx.Config.ID, bzzKey)
nolash marked this conversation as resolved.
Show resolved Hide resolved
bucket.Store(simulation.BucketKeyKademlia, pskad)
ps, err := New(pskad, pssp)
if err != nil {
return nil, err
return nil, nil, err
}

ping := &Ping{
OutC: make(chan bool),
Pong: true,
}
p2pp := NewPingProtocol(ping)
pp, err := RegisterProtocol(ps, &PingTopic, PingProtocol, p2pp, &ProtocolParams{Asymmetric: true})
if err != nil {
return nil, err
return nil, nil, err
}
if useHandshake {
SetHandshakeController(ps, NewHandshakeParams())
}
ps.Register(&PingTopic, &handler{
var cleanupFuncs []func()
aperonam marked this conversation as resolved.
Show resolved Hide resolved
cleanupFuncs = append(cleanupFuncs, ps.Register(&PingTopic, &handler{
f: pp.Handle,
caps: &handlerCaps{
raw: true,
},
})
}))
ps.addAPI(rpc.API{
Namespace: "psstest",
Version: "0.3",
Expand All @@ -1885,18 +1876,12 @@ func newServices(allowRaw bool) adapters.Services {
protocol: pp,
run: p2pp.Run,
}
return ps, nil
},
"bzz": func(ctx *adapters.ServiceContext) (node.Service, error) {
addr := network.NewAddr(ctx.Config.Node())
hp := network.NewHiveParams()
hp.Discovery = false
config := &network.BzzConfig{
OverlayAddr: addr.Over(),
UnderlayAddr: addr.Under(),
HiveParams: hp,
}
return network.NewBzz(config, kademlia(ctx.Config.ID), stateStore, nil, nil), nil
return ps, func() {
for i := len(cleanupFuncs); i > 0; i-- {
cleanupFuncs[i-1]()
}
}, nil

},
}
}
Expand All @@ -1919,7 +1904,10 @@ func newTestPss(privkey *ecdsa.PrivateKey, kad *network.Kademlia, ppextra *Param
if err != nil {
return nil
}
ps.Start(nil)
err = ps.Start(nil)
if err != nil {
return nil
}

return ps
}
Expand Down