This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
pss: Port tests to network/simulation
#1682
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,10 +22,8 @@ import ( | |
"crypto/ecdsa" | ||
"encoding/binary" | ||
"encoding/hex" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"math/rand" | ||
"os" | ||
"strconv" | ||
|
@@ -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" | ||
|
@@ -68,8 +67,6 @@ var ( | |
func init() { | ||
flag.Parse() | ||
rand.Seed(time.Now().Unix()) | ||
|
||
adapters.RegisterServices(newServices(false)) | ||
initTest() | ||
} | ||
|
||
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
|
@@ -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 { | ||
|
@@ -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) | ||
} | ||
|
@@ -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 { | ||
|
@@ -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 | ||
} | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
||
|
@@ -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", | ||
|
@@ -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 | ||
|
||
}, | ||
} | ||
} | ||
|
@@ -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 | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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