2022-11-04 12:40:24 +00:00
|
|
|
|
import
|
|
|
|
|
std/[tables, sequtils],
|
|
|
|
|
stew/byteutils,
|
|
|
|
|
stew/shims/net,
|
|
|
|
|
chronicles,
|
|
|
|
|
chronos,
|
|
|
|
|
confutils,
|
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
|
eth/keys,
|
|
|
|
|
eth/p2p/discoveryv5/enr
|
|
|
|
|
|
|
|
|
|
import
|
2022-12-07 11:30:32 +00:00
|
|
|
|
../../../waku/common/logging,
|
2023-02-06 09:03:30 +00:00
|
|
|
|
../../../waku/v2/node/peer_manager,
|
2023-04-19 11:29:23 +00:00
|
|
|
|
../../../waku/v2/waku_core,
|
2023-04-19 14:39:52 +00:00
|
|
|
|
../../../waku/v2/waku_node,
|
2023-04-18 13:22:10 +00:00
|
|
|
|
../../../waku/v2/waku_enr,
|
|
|
|
|
../../../waku/v2/waku_discv5
|
2022-11-04 12:40:24 +00:00
|
|
|
|
|
|
|
|
|
# An accesible bootstrap node. See wakuv2.prod fleets.status.im
|
2023-03-30 07:35:13 +00:00
|
|
|
|
const bootstrapNode = "enr:-Nm4QOdTOKZJKTUUZ4O_W932CXIET-M9NamewDnL78P5u9DOGnZl" &
|
|
|
|
|
"K0JFZ4k0inkfe6iY-0JAaJVovZXc575VV3njeiABgmlkgnY0gmlwhAjS" &
|
|
|
|
|
"3ueKbXVsdGlhZGRyc7g6ADg2MW5vZGUtMDEuYWMtY24taG9uZ2tvbmct" &
|
|
|
|
|
"Yy53YWt1djIucHJvZC5zdGF0dXNpbS5uZXQGH0DeA4lzZWNwMjU2azGh" &
|
|
|
|
|
"Ao0C-VvfgHiXrxZi3umDiooXMGY9FvYj5_d1Q4EeS7eyg3RjcIJ2X4N1" &
|
|
|
|
|
"ZHCCIyiFd2FrdTIP"
|
2022-11-04 12:40:24 +00:00
|
|
|
|
|
|
|
|
|
# careful if running pub and sub in the same machine
|
|
|
|
|
const wakuPort = 50000
|
|
|
|
|
const discv5Port = 8000
|
|
|
|
|
|
2023-02-06 11:53:05 +00:00
|
|
|
|
proc setupAndSubscribe(rng: ref HmacDrbgContext) {.async.} =
|
2022-11-04 12:40:24 +00:00
|
|
|
|
# use notice to filter all waku messaging
|
2022-12-07 11:30:32 +00:00
|
|
|
|
setupLogLevel(logging.LogLevel.NOTICE)
|
2022-11-04 12:40:24 +00:00
|
|
|
|
notice "starting subscriber", wakuPort=wakuPort, discv5Port=discv5Port
|
|
|
|
|
let
|
2023-02-06 11:53:05 +00:00
|
|
|
|
nodeKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
2022-11-04 12:40:24 +00:00
|
|
|
|
ip = ValidIpAddress.init("0.0.0.0")
|
2023-03-08 14:44:10 +00:00
|
|
|
|
flags = CapabilitiesBitfield.init(lightpush = false, filter = false, store = false, relay = true)
|
2022-11-04 12:40:24 +00:00
|
|
|
|
|
2023-04-05 12:27:11 +00:00
|
|
|
|
var builder = WakuNodeBuilder.init()
|
|
|
|
|
builder.withNodeKey(nodeKey)
|
|
|
|
|
builder.withNetworkConfigurationDetails(ip, Port(wakuPort)).tryGet()
|
|
|
|
|
let node = builder.build().tryGet()
|
|
|
|
|
|
2023-03-30 07:35:13 +00:00
|
|
|
|
var bootstrapNodeEnr: enr.Record
|
|
|
|
|
discard bootstrapNodeEnr.fromURI(bootstrapNode)
|
|
|
|
|
|
2022-11-04 12:40:24 +00:00
|
|
|
|
# assumes behind a firewall, so not care about being discoverable
|
|
|
|
|
node.wakuDiscv5 = WakuDiscoveryV5.new(
|
|
|
|
|
extIp= none(ValidIpAddress),
|
|
|
|
|
extTcpPort = none(Port),
|
|
|
|
|
extUdpPort = none(Port),
|
|
|
|
|
bindIP = ip,
|
|
|
|
|
discv5UdpPort = Port(discv5Port),
|
2023-03-30 07:35:13 +00:00
|
|
|
|
bootstrapEnrs = @[bootstrapNodeEnr],
|
2022-11-04 12:40:24 +00:00
|
|
|
|
privateKey = keys.PrivateKey(nodeKey.skkey),
|
|
|
|
|
flags = flags,
|
|
|
|
|
rng = node.rng)
|
|
|
|
|
|
|
|
|
|
await node.start()
|
|
|
|
|
await node.mountRelay()
|
2023-02-07 16:42:17 +00:00
|
|
|
|
node.peerManager.start()
|
2022-11-04 12:40:24 +00:00
|
|
|
|
if not await node.startDiscv5():
|
|
|
|
|
error "failed to start discv5"
|
|
|
|
|
quit(1)
|
|
|
|
|
|
|
|
|
|
# wait for a minimum of peers to be connected, otherwise messages wont be gossiped
|
|
|
|
|
while true:
|
2022-11-24 13:11:23 +00:00
|
|
|
|
let numConnectedPeers = node.peerManager.peerStore[ConnectionBook].book.values().countIt(it == Connected)
|
2022-11-04 12:40:24 +00:00
|
|
|
|
if numConnectedPeers >= 6:
|
|
|
|
|
notice "subscriber is ready", connectedPeers=numConnectedPeers, required=6
|
|
|
|
|
break
|
|
|
|
|
notice "waiting to be ready", connectedPeers=numConnectedPeers, required=6
|
|
|
|
|
await sleepAsync(5000)
|
|
|
|
|
|
|
|
|
|
# Make sure it matches the publisher. Use default value
|
|
|
|
|
# see spec: https://rfc.vac.dev/spec/23/
|
|
|
|
|
let pubSubTopic = PubsubTopic("/waku/2/default-waku/proto")
|
|
|
|
|
|
|
|
|
|
# any content topic can be chosen. make sure it matches the publisher
|
|
|
|
|
let contentTopic = ContentTopic("/examples/1/pubsub-example/proto")
|
|
|
|
|
|
|
|
|
|
proc handler(pubsubTopic: PubsubTopic, data: seq[byte]) {.async, gcsafe.} =
|
2022-11-07 15:24:16 +00:00
|
|
|
|
let message = WakuMessage.decode(data).value
|
2022-11-04 12:40:24 +00:00
|
|
|
|
let payloadStr = string.fromBytes(message.payload)
|
|
|
|
|
if message.contentTopic == contentTopic:
|
|
|
|
|
notice "message received", payload=payloadStr,
|
|
|
|
|
pubsubTopic=pubsubTopic,
|
|
|
|
|
contentTopic=message.contentTopic,
|
|
|
|
|
timestamp=message.timestamp
|
|
|
|
|
node.subscribe(pubSubTopic, handler)
|
|
|
|
|
|
2023-02-06 11:53:05 +00:00
|
|
|
|
when isMainModule:
|
|
|
|
|
let rng = crypto.newRng()
|
|
|
|
|
asyncSpawn setupAndSubscribe(rng)
|
|
|
|
|
runForever()
|