2020-05-19 05:00:03 +00:00
|
|
|
import
|
2020-05-20 05:34:43 +00:00
|
|
|
strformat, os, osproc, net, confutils, strformat, chronicles, json,
|
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/protocols/protocol,
|
|
|
|
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
|
|
|
|
|
|
|
|
# TODO: Then, setup a star network
|
|
|
|
|
|
|
|
# TODO: Create Node command, something like this:
|
|
|
|
# "build/wakunode --log-level:DEBUG --log-metrics --metrics-server --rpc --waku-topic-interest:false --nodekey:e685079b7fa34dd35d3ffb2e40ab970360e94aa7dcc1262d36a8e2320a2c08ce --ports-shift:2 --discovery:off "
|
|
|
|
# Ok cool so it is config.nim parseCmdArg, then use fromHex
|
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
|
2020-05-26 03:55:53 +00:00
|
|
|
key = SkPrivateKey.random()[] #assumes ok
|
2020-05-20 05:34:43 +00:00
|
|
|
hkey = key.getBytes().toHex()
|
2020-05-26 03:55:53 +00:00
|
|
|
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)
|
2020-05-26 03:55:53 +00:00
|
|
|
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
|
|
|
|
2020-05-21 04:36:30 +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-21 04:36:30 +00:00
|
|
|
when isMainModule:
|
|
|
|
# TODO: WakuNetworkConf
|
|
|
|
var nodes: seq[NodeInfo]
|
|
|
|
let amount = 2
|
|
|
|
nodes = starNetwork(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
|