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
|
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)
|
2020-05-28 03:28:44 +00:00
|
|
|
port = 60000 + shift
|
|
|
|
#DefaultAddr = "/ip4/127.0.0.1/tcp/55505"
|
|
|
|
address = "/ip4/127.0.0.1/tcp/" & $port
|
|
|
|
hostAddress = MultiAddress.init(address)
|
2020-05-19 05:00:03 +00:00
|
|
|
|
2020-05-28 03:40:41 +00:00
|
|
|
info "Address", address
|
2020-05-28 03:28:44 +00:00
|
|
|
# TODO: Need to port shift
|
2020-05-19 05:00:03 +00:00
|
|
|
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-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)
|
2020-05-28 02:58:37 +00:00
|
|
|
result.add(initNodeCmd(portOffset + i, staticnodes, label = "full node"))
|
2020-05-27 05:37:27 +00:00
|
|
|
|
2020-05-21 04:36:30 +00:00
|
|
|
when isMainModule:
|
|
|
|
# TODO: WakuNetworkConf
|
|
|
|
var nodes: seq[NodeInfo]
|
2020-05-28 02:58:37 +00:00
|
|
|
let topology = FullMesh
|
2020-05-27 04:25:10 +00:00
|
|
|
let amount = 6
|
2020-05-27 05:37:27 +00:00
|
|
|
|
|
|
|
case topology:
|
|
|
|
of Star:
|
|
|
|
nodes = starNetwork(amount)
|
|
|
|
of FullMesh:
|
|
|
|
nodes = fullMeshNetwork(amount)
|
|
|
|
|
2020-05-28 03:28:44 +00:00
|
|
|
var staticnodes: seq[string]
|
|
|
|
for i in 0..<amount:
|
|
|
|
# TODO: could also select nodes randomly
|
|
|
|
staticnodes.add(nodes[i].address)
|
|
|
|
|
|
|
|
# TODO: Here we could add a light node, but not clear thats what we want to test?
|
2020-05-21 04:36:30 +00:00
|
|
|
|
|
|
|
var commandStr = "multitail -s 2 -M 0 -x \"Waku Simulation\""
|
|
|
|
var count = 0
|
|
|
|
var sleepDuration = 0
|
|
|
|
for node in nodes:
|
2020-05-28 03:28:44 +00:00
|
|
|
if topology in {Star}: #DiscoveryBased
|
|
|
|
sleepDuration = if node.master: 0
|
|
|
|
else: 1
|
2020-05-21 04:36:30 +00:00
|
|
|
commandStr &= &" -cT ansi -t 'node #{count} {node.label}' -l 'sleep {sleepDuration}; {node.cmd}; echo [node execution completed]; while true; do sleep 100; done'"
|
2020-05-28 03:28:44 +00:00
|
|
|
if topology == FullMesh:
|
|
|
|
sleepDuration += 1
|
|
|
|
count += 1
|
2020-05-21 04:36:30 +00:00
|
|
|
|
|
|
|
let errorCode = execCmd(commandStr)
|
|
|
|
if errorCode != 0:
|
|
|
|
error "launch command failed", command=commandStr
|