Add quick waku simulation with hardcoded network

This commit is contained in:
kdeme 2020-01-15 00:09:39 +01:00 committed by zah
parent 574c5ec0af
commit ed79c4dfdc
6 changed files with 124 additions and 38 deletions

19
nimbus/rpc/wakusim.nim Normal file
View File

@ -0,0 +1,19 @@
import
json_rpc/rpcserver, stew/endians2,
eth/[p2p, async_utils], eth/p2p/rlpx_protocols/waku_protocol
proc generateTraffic(node: EthereumNode, amount = 100) {.async.} =
var topicNumber = 0'u32
let payload = @[byte 0]
for i in 0..<amount:
discard waku_protocol.postMessage(node, ttl = 10,
topic = toBytesLE(i.uint32), payload = payload)
await sleepAsync(1.milliseconds)
proc setupWakuSimRPC*(node: EthereumNode, rpcsrv: RpcServer) =
rpcsrv.rpc("wakusim_generateTraffic") do(amount: int) -> bool:
traceAsyncErrors node.generateTraffic(amount)
return true
# TODO: add random traffic generation

View File

@ -23,13 +23,11 @@ The JSON-RPC interface is currently the same as the one of Whisper. The only
difference is the addition of broadcasting the topics interest when a filter
with a certain set of topics is subcribed.
Example of a quick test with nim-web3:
```
./build/wakunode --log-level:DEBUG --bootnode-only --nodekey:5dc5381cae54ba3174dc0d46040fe11614d0cc94d41185922585198b4fcef9d3
./build/wakunode --log-level:DEBUG --bootnodes:enode://e5fd642a0f630bbb1e4cd7df629d7b8b019457a9a74f983c0484a045cebb176def86a54185b50bbba6bbf97779173695e92835d63109c23471e6da382f922fdb@0.0.0.0:30303 --rpc --ports-shift:1 --waku-mode:WakuSan
./build/wakunode --log-level:DEBUG --bootnodes:enode://e5fd642a0f630bbb1e4cd7df629d7b8b019457a9a74f983c0484a045cebb176def86a54185b50bbba6bbf97779173695e92835d63109c23471e6da382f922fdb@0.0.0.0:30303 --rpc --ports-shift:2 --waku-mode:WakuChan
Example of a quick simulation test using this approach:
```bash
./waku/start_network.sh
# Or when multitail is installed
USE_MULTITAIL="yes" ./waku/start_network.sh
./build/quicksim
```

View File

@ -9,47 +9,50 @@ from strutils import rsplit
template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0]
# TODO: move this to rpc folder? Or just directly to nim-web3 and import that?
const sigPath = &"{sourceDir}{DirSep}..{DirSep}tests{DirSep}rpcclient{DirSep}ethcallsigs.nim"
createRpcSigs(RpcHttpClient, sigPath)
const sigEthPath = &"{sourceDir}{DirSep}..{DirSep}tests{DirSep}rpcclient{DirSep}ethcallsigs.nim"
createRpcSigs(RpcHttpClient, sigEthPath)
const sigWakuPath = &"{sourceDir}{DirSep}rpc{DirSep}wakucallsigs.nim"
createRpcSigs(RpcHttpClient, sigWakuPath)
let
bob = newRpcHttpClient()
alice = newRpcHttpClient()
trafficNode = newRpcHttpClient()
lightWakuNode = newRpcHttpClient()
lightNode = newRpcHttpClient()
waitFor bob.connect("localhost", Port(8546))
waitFor alice.connect("localhost", Port(8547))
let symKey = "0x0000000000000000000000000000000000000000000000000000000000000001"
waitFor lightWakuNode.connect("localhost", Port(8546))
waitFor lightNode.connect("localhost", Port(8547))
waitFor trafficNode.connect("localhost", Port(8549))
let
topic = "0x12345678".toTopic()
symKeyID = waitFor alice.shh_addSymKey(symKey)
symKey = "0x0000000000000000000000000000000000000000000000000000000000000001"
topic = "0x01000000".toTopic()
symKeyID = waitFor lightWakuNode.shh_addSymKey(symKey)
options = WhisperFilterOptions(symKeyID: some(symKeyID),
topics: some(@[topic]))
filterID = waitFor alice.shh_newMessageFilter(options)
filterID = waitFor lightWakuNode.shh_newMessageFilter(options)
let
symkeyID2 = waitFor bob.shh_addSymKey(symKey)
symKeyID2 = waitFor lightNode.shh_addSymKey(symKey)
options2 = WhisperFilterOptions(symKeyID: some(symKeyID2),
topics: some(@[topic]))
filterID2 = waitFor lightNode.shh_newMessageFilter(options2)
var i = 1
while i <= 10000:
var message = WhisperPostMessage(symKeyID: some(symkeyID2),
ttl: 30,
topic: some(topic),
payload: "0x45879632".HexDataStr,
powTime: 1.0,
powTarget: 0.002)
debug "Message", i, message
sleep(100)
discard waitFor bob.shh_post(message)
inc(i)
symkeyID3 = waitFor trafficNode.shh_addSymKey(symKey)
var message = WhisperPostMessage(symKeyID: some(symkeyID3),
ttl: 30,
topic: some(topic),
payload: "0x45879632".HexDataStr,
powTime: 1.0,
powTarget: 0.002)
discard waitFor trafficNode.shh_post(message)
var messages: seq[WhisperFilterMessage]
debug "Messages len", length = messages.len
# Check if the subscription for the topic works
while messages.len == 0:
messages = waitFor alice.shh_getFilterMessages(filterID)
waitFor sleepAsync(100.milliseconds)
debug "Received message", payload = messages[0].payload
messages = waitFor lightWakuNode.shh_getFilterMessages(filterID)
waitFor sleepAsync(1000.milliseconds)
info "Received test message", payload = messages[0].payload
debug "Received messages", length = messages.len
# Generate test traffic on node
discard waitFor trafficNode.wakusim_generateTraffic(10_000)

View File

@ -0,0 +1 @@
proc wakusim_generateTraffic(amount: int): bool

64
waku/start_network.sh Executable file
View File

@ -0,0 +1,64 @@
#!/bin/bash
set -e
# TODO: improve this bin location
WAKU_NODE_BIN="./build/wakunode"
NODE_PK="5dc5381cae54ba3174dc0d46040fe11614d0cc94d41185922585198b4fcef9d3"
NODE_ENODE="enode://e5fd642a0f630bbb1e4cd7df629d7b8b019457a9a74f983c0484a045cebb176def86a54185b50bbba6bbf97779173695e92835d63109c23471e6da382f922fdb@0.0.0.0:30303"
DEFAULTS="--log-level:DEBUG --discovery:0 --log-metrics"
LIGHT_NODE="--light-node:1"
WAKU_LIGHT_NODE="--waku-mode:WakuChan ${LIGHT_NODE}"
# multitail support
MULTITAIL="${MULTITAIL:-multitail}" # to allow overriding the program name
USE_MULTITAIL="${USE_MULTITAIL:-no}" # make it an opt-in
type "$MULTITAIL" &>/dev/null || USE_MULTITAIL="no"
# TODO: metrics configs
if [[ "$USE_MULTITAIL" != "no" ]]; then
SLEEP=0
# Direct connect with staticnodes, simple star topology for now
# Master node to connect to
CMD="$WAKU_NODE_BIN $DEFAULTS --rpc --nodekey:${NODE_PK} --waku-mode:WakuSan"
COMMANDS+=( " -cT ansi -t 'master node' -l 'sleep $SLEEP; $CMD; echo [node execution completed]; while true; do sleep 100; done'" )
SLEEP=1
# Node under test 1: light waku node (topics)
CMD="$WAKU_NODE_BIN $DEFAULTS --rpc --staticnodes:${NODE_ENODE} --ports-shift:1 ${WAKU_LIGHT_NODE}"
COMMANDS+=( " -cT ansi -t 'light waku node' -l 'sleep $SLEEP; $CMD; echo [node execution completed]; while true; do sleep 100; done'" )
# Node under test 2: light node (bloomfilter)
CMD="$WAKU_NODE_BIN $DEFAULTS --rpc --staticnodes:${NODE_ENODE} --ports-shift:2 ${LIGHT_NODE}"
COMMANDS+=( " -cT ansi -t 'light node' -l 'sleep $SLEEP; $CMD; echo [node execution completed]; while true; do sleep 100; done'" )
# Node under test 3: full node
CMD="$WAKU_NODE_BIN $DEFAULTS --rpc --staticnodes:${NODE_ENODE} --ports-shift:3"
COMMANDS+=( " -cT ansi -t 'full node' -l 'sleep $SLEEP; $CMD; echo [node execution completed]; while true; do sleep 100; done'" )
# Traffic generation node(s)
CMD="$WAKU_NODE_BIN $DEFAULTS --rpc --staticnodes:${NODE_ENODE} --ports-shift:4"
COMMANDS+=( " -cT ansi -t 'traffic full node' -l 'sleep $SLEEP; $CMD; echo [node execution completed]; while true; do sleep 100; done'" )
else
# Direct connect with staticnodes, simple star topology for now
# Master node to connect to
CMD="$WAKU_NODE_BIN $DEFAULTS --rpc --nodekey:${NODE_PK} --waku-mode:WakuSan"
eval ${CMD} &
sleep 1
# Node under test 1: light waku node (topics)
CMD="$WAKU_NODE_BIN $DEFAULTS --rpc --staticnodes:${NODE_ENODE} --ports-shift:1 ${WAKU_LIGHT_NODE}"
eval ${CMD} &
# Node under test 2: light node (bloomfilter)
CMD="$WAKU_NODE_BIN $DEFAULTS --rpc --staticnodes:${NODE_ENODE} --ports-shift:2 ${LIGHT_NODE}"
eval ${CMD} &
# Node under test 3: full node
CMD="$WAKU_NODE_BIN $DEFAULTS --rpc --staticnodes:${NODE_ENODE} --ports-shift:3"
eval ${CMD} &
# Traffic generation node(s)
CMD="$WAKU_NODE_BIN $DEFAULTS --rpc --staticnodes:${NODE_ENODE} --ports-shift:4"
eval ${CMD} &
fi
if [[ "$USE_MULTITAIL" != "no" ]]; then
eval $MULTITAIL -s 2 -M 0 -x \"Waku Simulation\" "${COMMANDS[@]}"
else
wait # Stop when all nodes have gone down
fi

View File

@ -4,7 +4,7 @@ import
eth/[keys, p2p, async_utils], eth/common/utils,
eth/p2p/[discovery, enode, peer_pool, bootnodes, whispernodes],
eth/p2p/rlpx_protocols/[whisper_protocol, waku_protocol, waku_bridge],
../nimbus/rpc/waku
../nimbus/rpc/waku, ../nimbus/rpc/wakusim
proc setBootNodes(nodes: openArray[string]): seq[ENode] =
var bootnode: ENode
@ -71,6 +71,7 @@ proc run(config: WakuNodeConf) =
var rpcServer = newRpcHttpServer([ta])
let keys = newWakuKeys()
setupWakuRPC(node, keys, rpcServer)
setupWakuSimRPC(node, rpcServer)
rpcServer.start()
when defined(insecure):