Skip to content

Instantly share code, notes, and snippets.

@lmars
Created March 27, 2017 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmars/d52bf928eb72f0df3dfc5ed35882370c to your computer and use it in GitHub Desktop.
Save lmars/d52bf928eb72f0df3dfc5ed35882370c to your computer and use it in GitHub Desktop.
Swarm Network Testing

Integration testing

Network simulation

The aim is to fully simulate devp2p networks, manage connections between nodes and record interactions in a replayable journal.

A simulation framework is currently in progress on the network-testing-framework branch and currently includes the following packages:

p2p/adapters

An adapter is responsible for creating nodes and connecting them together, examples being:

  • inproc - creates in-memory simulator nodes and connects them with in-memory pipes
  • rlpx - creates p2p.Server nodes and connects them using TCP connections
  • docker - starts Docker containers and connects them using ssh+ipc connections

p2p/protocols

Provides an interface to simplify defining sub-protocols to run on devp2p, abstracting away the following:

  • provide the forever loop to read incoming messages
  • automatic RLP encoding / decoding of protocol messages
  • standardise error handling related to communication

See this test protocol.

p2p/simulations

Implements an event-based simulation Network which accepts incoming events to manage nodes using an adapter and send messages between nodes, and outputs simulation events to subscribers.

Example events:

  • node up - a new node has started
  • node down - a node has gone down
  • conn up - a connection has been established between two nodes
  • conn down - a connection has been severed between two nodes
  • msg - a message has been sent between two nodes

A Journal is a network subscriber which records events which can then be replayed into a new network simulation.

cmd/p2psim

p2psim is a simulator REPL to perform in-memory simulations, currently implementing the following commands:

p2psim> help
add                             add new node
start <node>                    start <node>
stop <node>                     stop <node>
connect <nodeA> <nodeB>         connect <nodeA> with <nodeB>
disconnect <nodeA> <nodeB>      disconnect <nodeA> from <nodeB>
list                            list nodes
info <node>                     display info on <node>
help                            show usage
exit                            exit the simulator

A sample session:

p2psim> add
added node1 (049ffad1729ebd43e5a44b7127f9aaabf0f6a5a20848837db6b4639876b5c4bd11591d46de15bde0570fe1a26716209c1502e33a0bcdc7dafb4a67447d775138)

p2psim> add
added node2 (a21cd8771c9b6a15713b0cb042c646833dc978b5a049ca151488a5dc704b7770c9cf33c8cfc5521121fbbab09d1d3f82d635cf6419da51297d8a71a3a9853a62)

p2psim> add
added node3 (d09d6e2077f0527a97bb7347783182a8e1ba8020885ba85f970f9c72307db5b3c6f5af34b652def7342781781e2e3cb26cb103e3254c4ffa66a8865c4d1d9126)

p2psim> list
NAME    UP      ID
node1   false   049ffad1729ebd43e5a44b7127f9aaabf0f6a5a20848837db6b4639876b5c4bd11591d46de15bde0570fe1a26716209c1502e33a0bcdc7dafb4a67447d775138
node2   false   a21cd8771c9b6a15713b0cb042c646833dc978b5a049ca151488a5dc704b7770c9cf33c8cfc5521121fbbab09d1d3f82d635cf6419da51297d8a71a3a9853a62
node3   false   d09d6e2077f0527a97bb7347783182a8e1ba8020885ba85f970f9c72307db5b3c6f5af34b652def7342781781e2e3cb26cb103e3254c4ffa66a8865c4d1d9126

p2psim> start node1
started node1 (049ffad1729ebd43e5a44b7127f9aaabf0f6a5a20848837db6b4639876b5c4bd11591d46de15bde0570fe1a26716209c1502e33a0bcdc7dafb4a67447d775138)

p2psim> start node3
started node3 (d09d6e2077f0527a97bb7347783182a8e1ba8020885ba85f970f9c72307db5b3c6f5af34b652def7342781781e2e3cb26cb103e3254c4ffa66a8865c4d1d9126)

p2psim> connect node1 node3
connected node1 to node3

p2psim> info node1
ID      049ffad1729ebd43e5a44b7127f9aaabf0f6a5a20848837db6b4639876b5c4bd11591d46de15bde0570fe1a26716209c1502e33a0bcdc7dafb4a67447d775138
Up      true

Tasks

  • update simulations.Network to handle message events
  • implement remote node adapters (rlpx / docker) with event subscriptions
  • run protocols in p2psim
  • journal support in p2psim (e.g. load <journal>, save <journal>)
  • add a logical clock to allow the simulation to be paused / resumed and tests to wait for logical clock durations before making assertions
  • add next and step <n> commands to p2psim to step through a discrete number of events
  • implement p2psim scripts for writing full network simulation scenarios
  • add VM adapters to create more realistic networks (e.g. KVM / AWS / Azure)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment