nwaku/waku/node/v2/start_network.nim

102 lines
3.0 KiB
Nim
Raw Normal View History

2020-05-19 05:00:03 +00:00
import
2020-05-27 04:07:11 +00:00
strformat, os, osproc, net, strformat, chronicles,
2020-05-19 05:00:03 +00:00
libp2p/multiaddress,
libp2p/crypto/crypto,
2020-05-20 05:34:43 +00:00
libp2p/crypto/secp,
2020-05-19 05:00:03 +00:00
libp2p/peerinfo
2020-05-20 05:34:43 +00:00
# Fix ambiguous call error
import strutils except fromHex
2020-05-20 06:21:17 +00:00
const
defaults ="--log-level:DEBUG --log-metrics --metrics-server --rpc"
wakuNodeBin = "build" / "wakunode"
2020-05-21 04:28:57 +00:00
portOffset = 2
2020-05-20 06:21:17 +00:00
2020-05-19 05:00:03 +00:00
type
NodeInfo* = object
cmd: string
2020-05-21 04:28:57 +00:00
master: bool
2020-05-19 05:00:03 +00:00
address: string
2020-05-20 06:21:17 +00:00
shift: int
2020-05-19 05:00:03 +00:00
label: string
2020-05-27 05:37:27 +00:00
Topology = enum
Star,
FullMesh
2020-05-19 05:00:03 +00:00
2020-05-27 05:37:27 +00:00
# NOTE: Don't distinguish between node types here a la full node, light node etc
2020-05-21 04:28:57 +00:00
proc initNodeCmd(shift: int, staticNodes: seq[string] = @[], master = false, label: string): NodeInfo =
2020-05-19 05:00:03 +00:00
let
key = SkPrivateKey.random()[] #assumes ok
2020-05-20 05:34:43 +00:00
hkey = key.getBytes().toHex()
rkey = SkPrivateKey.init(fromHex(hkey))[] #assumes ok
2020-05-20 05:34:43 +00:00
privKey = PrivateKey(scheme: Secp256k1, skkey: rkey)
#privKey = PrivateKey.random(Secp256k1)
pubkey = privKey.getKey()[] #assumes ok
keys = KeyPair(seckey: privKey, pubkey: pubkey)
2020-05-19 05:00:03 +00:00
peerInfo = PeerInfo.init(privKey)
# XXX
DefaultAddr = "/ip4/127.0.0.1/tcp/55505"
hostAddress = MultiAddress.init(DefaultAddr)
peerInfo.addrs.add(hostAddress)
2020-05-20 06:21:17 +00:00
let id = peerInfo.id
info "PeerInfo", id = id, addrs = peerInfo.addrs
let listenStr = $peerInfo.addrs[0] & "/p2p/" & id
result.cmd = wakuNodeBin & " " & defaults & " "
result.cmd &= "--nodekey:" & hkey & " "
result.cmd &= "--ports-shift:" & $shift & " "
if staticNodes.len > 0:
for staticNode in staticNodes:
result.cmd &= "--staticnode:" & staticNode & " "
result.shift = shift
result.label = label
2020-05-21 04:28:57 +00:00
result.master = master
2020-05-20 06:21:17 +00:00
result.address = listenStr
info "Node command created.", cmd=result.cmd, address = result.address
2020-05-19 05:00:03 +00:00
proc starNetwork(amount: int): seq[NodeInfo] =
let masterNode = initNodeCmd(portOffset, master = true, label = "master node")
result.add(masterNode)
for i in 1..<amount:
result.add(initNodeCmd(portOffset + i, @[masterNode.address], label = "full node"))
2020-05-19 05:00:03 +00:00
2020-05-27 05:37:27 +00:00
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.address)
result.add(initNodeCmd(portOffset + i, staticnodes, label = "full node"))
when isMainModule:
# TODO: WakuNetworkConf
var nodes: seq[NodeInfo]
2020-05-27 05:37:27 +00:00
let topology = Star
let amount = 6
2020-05-27 05:37:27 +00:00
# XXX: For some reason Mesh hangs with multitail
case topology:
of Star:
nodes = starNetwork(amount)
of FullMesh:
nodes = fullMeshNetwork(amount)
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'"
let errorCode = execCmd(commandStr)
if errorCode != 0:
error "launch command failed", command=commandStr