From a03d0be31ff002adc6b0552d1b501a1f90434de2 Mon Sep 17 00:00:00 2001 From: kdeme Date: Fri, 17 Jan 2020 17:52:34 +0100 Subject: [PATCH] Add start_network.nim to start different types of networks --- Makefile | 2 +- nimbus.nimble | 3 +- waku/quicksim.nim | 6 +- waku/start_network.nim | 131 +++++++++++++++++++++++++++++++++++++++++ waku/start_network.sh | 8 +-- 5 files changed, 141 insertions(+), 9 deletions(-) create mode 100644 waku/start_network.nim diff --git a/Makefile b/Makefile index 47ec90b75..79c78c005 100644 --- a/Makefile +++ b/Makefile @@ -110,4 +110,4 @@ wakunode: | build deps wakusim: | build deps wakunode echo -e $(BUILD_MSG) "build/$@" && \ - $(ENV_SCRIPT) nim quicksim $(NIM_PARAMS) nimbus.nims + $(ENV_SCRIPT) nim wakusim $(NIM_PARAMS) nimbus.nims diff --git a/nimbus.nimble b/nimbus.nimble index 67be84c5d..868f1d0bd 100644 --- a/nimbus.nimble +++ b/nimbus.nimble @@ -43,5 +43,6 @@ task nimbus, "Build Nimbus": task wakunode, "Build Waku node": 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 "start_network", "waku/", "-d:chronicles_log_level=DEBUG" diff --git a/waku/quicksim.nim b/waku/quicksim.nim index 7e9bcd4ce..b4bec02d6 100644 --- a/waku/quicksim.nim +++ b/waku/quicksim.nim @@ -19,9 +19,9 @@ let lightWakuNode = newRpcHttpClient() lightNode = newRpcHttpClient() -waitFor lightWakuNode.connect("localhost", Port(8546)) -waitFor lightNode.connect("localhost", Port(8547)) -waitFor trafficNode.connect("localhost", Port(8549)) +waitFor lightWakuNode.connect("localhost", Port(8545)) +waitFor lightNode.connect("localhost", Port(8546)) +waitFor trafficNode.connect("localhost", Port(8548)) proc generateTopics(amount = 100): seq[waku_protocol.Topic] = var topic: waku_protocol.Topic diff --git a/waku/start_network.nim b/waku/start_network.nim new file mode 100644 index 000000000..9446a3bfd --- /dev/null +++ b/waku/start_network.nim @@ -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..