Add start_network.nim to start different types of networks

This commit is contained in:
kdeme 2020-01-17 17:52:34 +01:00 committed by zah
parent 48391744ff
commit a03d0be31f
5 changed files with 141 additions and 9 deletions

View File

@ -110,4 +110,4 @@ wakunode: | build deps
wakusim: | build deps wakunode wakusim: | build deps wakunode
echo -e $(BUILD_MSG) "build/$@" && \ echo -e $(BUILD_MSG) "build/$@" && \
$(ENV_SCRIPT) nim quicksim $(NIM_PARAMS) nimbus.nims $(ENV_SCRIPT) nim wakusim $(NIM_PARAMS) nimbus.nims

View File

@ -43,5 +43,6 @@ task nimbus, "Build Nimbus":
task wakunode, "Build Waku node": task wakunode, "Build Waku node":
buildBinary "wakunode", "waku/", "-d:chronicles_log_level=TRACE" buildBinary "wakunode", "waku/", "-d:chronicles_log_level=TRACE"
task quicksim, "Build quicksim": task wakusim, "Build Waku simulation tools":
buildBinary "quicksim", "waku/", "-d:chronicles_log_level=INFO" buildBinary "quicksim", "waku/", "-d:chronicles_log_level=INFO"
buildBinary "start_network", "waku/", "-d:chronicles_log_level=DEBUG"

View File

@ -19,9 +19,9 @@ let
lightWakuNode = newRpcHttpClient() lightWakuNode = newRpcHttpClient()
lightNode = newRpcHttpClient() lightNode = newRpcHttpClient()
waitFor lightWakuNode.connect("localhost", Port(8546)) waitFor lightWakuNode.connect("localhost", Port(8545))
waitFor lightNode.connect("localhost", Port(8547)) waitFor lightNode.connect("localhost", Port(8546))
waitFor trafficNode.connect("localhost", Port(8549)) waitFor trafficNode.connect("localhost", Port(8548))
proc generateTopics(amount = 100): seq[waku_protocol.Topic] = proc generateTopics(amount = 100): seq[waku_protocol.Topic] =
var topic: waku_protocol.Topic var topic: waku_protocol.Topic

131
waku/start_network.nim Normal file
View File

@ -0,0 +1,131 @@
import
strformat, osproc, net, confutils, strformat, chronicles,
eth/keys, eth/p2p/enode
const
defaults ="--log-level:DEBUG --log-metrics --metrics-server --rpc"
wakuNodeBin = "./build/wakunode"
portOffset = 2
type
NodeType = enum
FullNode = "--waku-mode:WakuSan",
LightNode = "--light-node:on",
WakuNode = "--light-node:on --waku-mode:WakuChan"
Topology = enum
Star,
FullMesh,
DiscoveryBased # Whatever topology the discovery brings
WakuNetworkConf* = object
topology* {.
desc: "Set the network topology."
defaultValue: Star
name: "topology" }: Topology
amount* {.
desc: "Amount of full nodes to be started."
defaultValue: 4
name: "amount" }: int
testNodePeers* {.
desc: "Amount of peers a test node should connect to."
defaultValue: 1
name: "test-node-peers" }: int
NodeInfo* = object
cmd: string
master: bool
enode: string
label: string
proc initNodeCmd(nodeType: NodeType, shift: int, staticNodes: seq[string] = @[],
discovery = false, bootNodes: seq[string] = @[], master = false,
label: string): NodeInfo =
let
keypair = newKeyPair()
address = Address(ip: parseIpAddress("127.0.0.1"),
udpPort: (30303 + shift).Port, tcpPort: (30303 + shift).Port)
enode = ENode(pubkey: keypair.pubkey, address: address)
result.cmd = wakuNodeBin & " " & defaults & " "
result.cmd &= $nodeType & " "
result.cmd &= "--nodekey:" & $keypair.seckey & " "
result.cmd &= "--ports-shift:" & $shift & " "
if discovery:
result.cmd &= "--discovery:on" & " "
if bootNodes.len > 0:
for bootNode in bootNodes:
result.cmd &= "--bootnodes:" & bootNode & " "
else:
result.cmd &= "--discovery:off" & " "
if staticNodes.len > 0:
for staticNode in staticNodes:
result.cmd &= "--staticnodes:" & staticNode & " "
result.master = master
result.enode = $enode
result.label = label
debug "Node command created.", cmd=result.cmd
proc starNetwork(amount: int): seq[NodeInfo] =
let masterNode = initNodeCmd(FullNode, portOffset, master = true,
label = "master node")
result.add(masterNode)
for i in 1..<amount:
result.add(initNodeCmd(FullNode, portOffset + i, @[masterNode.enode],
label = "full node"))
proc fullMeshNetwork(amount: int): seq[NodeInfo] =
debug "amount", amount
for i in 0..<amount:
var staticnodes: seq[string]
for item in result:
staticnodes.add(item.enode)
result.add(initNodeCmd(FullNode, portOffset + i, staticnodes,
label = "full node"))
proc discoveryNetwork(amount: int): seq[NodeInfo] =
let bootNode = initNodeCmd(FullNode, portOffset, discovery = true,
master = true, label = "boot node")
result.add(bootNode)
for i in 1..<amount:
result.add(initNodeCmd(FullNode, portOffset + i, label = "full node",
discovery = true, bootNodes = @[bootNode.enode]))
let conf = WakuNetworkConf.load()
var nodes: seq[NodeInfo]
case conf.topology:
of Star:
nodes = starNetwork(conf.amount)
of FullMesh:
nodes = fullMeshNetwork(conf.amount)
of DiscoveryBased:
nodes = discoveryNetwork(conf.amount)
var staticnodes: seq[string]
for i in 0..<conf.testNodePeers:
staticnodes.add(nodes[i].enode)
# Waku light node
nodes.add(initNodeCmd(WakuNode, 0, staticnodes, label = "light Waku node"))
# Regular light node
nodes.add(initNodeCmd(LightNode, 1, staticnodes, label = "light node"))
var commandStr = "multitail -s 2 -M 0 -x \"Waku Simulation\""
var count = 0
var sleepDuration = 0
for node in nodes:
if conf.topology in {Star, DiscoveryBased}:
sleepDuration = if node.master: 0
else: 1
commandStr &= &" -cT ansi -t 'node #{count} {node.label}' -l 'sleep {sleepDuration}; {node.cmd}; echo [node execution completed]; while true; do sleep 100; done'"
if conf.topology == FullMesh:
sleepDuration += 1
count += 1
let errorCode = execCmd(commandStr)
if errorCode != 0:
error "launch command failed", command=commandStr

View File

@ -5,7 +5,7 @@ set -e
# TODO: improve this bin location # TODO: improve this bin location
WAKU_NODE_BIN="./build/wakunode" WAKU_NODE_BIN="./build/wakunode"
NODE_PK="5dc5381cae54ba3174dc0d46040fe11614d0cc94d41185922585198b4fcef9d3" NODE_PK="5dc5381cae54ba3174dc0d46040fe11614d0cc94d41185922585198b4fcef9d3"
NODE_ENODE="enode://e5fd642a0f630bbb1e4cd7df629d7b8b019457a9a74f983c0484a045cebb176def86a54185b50bbba6bbf97779173695e92835d63109c23471e6da382f922fdb@0.0.0.0:30303" NODE_ENODE="enode://e5fd642a0f630bbb1e4cd7df629d7b8b019457a9a74f983c0484a045cebb176def86a54185b50bbba6bbf97779173695e92835d63109c23471e6da382f922fdb@0.0.0.0:30305"
DEFAULTS="--log-level:DEBUG --discovery:off --log-metrics --metrics-server" DEFAULTS="--log-level:DEBUG --discovery:off --log-metrics --metrics-server"
LIGHT_NODE="--light-node:1" LIGHT_NODE="--light-node:1"
WAKU_LIGHT_NODE="--waku-mode:WakuChan ${LIGHT_NODE}" WAKU_LIGHT_NODE="--waku-mode:WakuChan ${LIGHT_NODE}"
@ -56,15 +56,15 @@ if [[ "$USE_MULTITAIL" != "no" ]]; then
SLEEP=0 SLEEP=0
# Direct connect with staticnodes, simple star topology for now # Direct connect with staticnodes, simple star topology for now
# Master node to connect to # Master node to connect to
CMD="$WAKU_NODE_BIN $DEFAULTS --rpc --nodekey:${NODE_PK} --waku-mode:WakuSan" CMD="$WAKU_NODE_BIN $DEFAULTS --rpc --nodekey:${NODE_PK} --ports-shift:2 --waku-mode:WakuSan"
COMMANDS+=( " -cT ansi -t 'master node' -l 'sleep $SLEEP; $CMD; echo [node execution completed]; while true; do sleep 100; done'" ) COMMANDS+=( " -cT ansi -t 'master node' -l 'sleep $SLEEP; $CMD; echo [node execution completed]; while true; do sleep 100; done'" )
SLEEP=1 SLEEP=1
# Node under test 1: light waku node (topics) # Node under test 1: light waku node (topics)
CMD="$WAKU_NODE_BIN $DEFAULTS --rpc --staticnodes:${NODE_ENODE} --ports-shift:1 ${WAKU_LIGHT_NODE}" CMD="$WAKU_NODE_BIN $DEFAULTS --rpc --staticnodes:${NODE_ENODE} --ports-shift:0 ${WAKU_LIGHT_NODE}"
COMMANDS+=( " -cT ansi -t 'light waku node' -l 'sleep $SLEEP; $CMD; echo [node execution completed]; while true; do sleep 100; done'" ) 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) # Node under test 2: light node (bloomfilter)
CMD="$WAKU_NODE_BIN $DEFAULTS --rpc --staticnodes:${NODE_ENODE} --ports-shift:2 ${LIGHT_NODE}" CMD="$WAKU_NODE_BIN $DEFAULTS --rpc --staticnodes:${NODE_ENODE} --ports-shift:1 ${LIGHT_NODE}"
COMMANDS+=( " -cT ansi -t 'light node' -l 'sleep $SLEEP; $CMD; echo [node execution completed]; while true; do sleep 100; done'" ) 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 # Node under test 3: full node
CMD="$WAKU_NODE_BIN $DEFAULTS --rpc --staticnodes:${NODE_ENODE} --ports-shift:3" CMD="$WAKU_NODE_BIN $DEFAULTS --rpc --staticnodes:${NODE_ENODE} --ports-shift:3"