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
|
2024-07-05 22:03:38 +00:00
|
|
|
|
waku/[
|
|
|
|
|
common/logging,
|
|
|
|
|
node/peer_manager,
|
|
|
|
|
waku_core,
|
|
|
|
|
waku_node,
|
|
|
|
|
waku_enr,
|
|
|
|
|
discovery/waku_discv5,
|
|
|
|
|
factory/builder,
|
|
|
|
|
waku_relay,
|
|
|
|
|
]
|
2022-11-04 12:40:24 +00:00
|
|
|
|
|
2024-03-11 13:48:20 +00:00
|
|
|
|
# An accesible bootstrap node. See waku.sandbox fleets.status.im
|
2024-03-15 23:08:47 +00:00
|
|
|
|
const bootstrapNode =
|
2024-03-11 13:48:20 +00:00
|
|
|
|
"enr:-QEkuEB3WHNS-xA3RDpfu9A2Qycr3bN3u7VoArMEiDIFZJ6" &
|
|
|
|
|
"6F1EB3d4wxZN1hcdcOX-RfuXB-MQauhJGQbpz3qUofOtLAYJpZI" &
|
|
|
|
|
"J2NIJpcIQI2SVcim11bHRpYWRkcnO4bgA0Ni9ub2RlLTAxLmFjL" &
|
|
|
|
|
"WNuLWhvbmdrb25nLWMud2FrdS5zYW5kYm94LnN0YXR1cy5pbQZ2" &
|
|
|
|
|
"XwA2Ni9ub2RlLTAxLmFjLWNuLWhvbmdrb25nLWMud2FrdS5zYW5" &
|
|
|
|
|
"kYm94LnN0YXR1cy5pbQYfQN4DgnJzkwABCAAAAAEAAgADAAQABQ" &
|
|
|
|
|
"AGAAeJc2VjcDI1NmsxoQPK35Nnz0cWUtSAhBp7zvHEhyU_AqeQU" &
|
|
|
|
|
"lqzLiLxfP2L4oN0Y3CCdl-DdWRwgiMohXdha3UyDw"
|
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.} =
|
2024-03-15 23:08:47 +00:00
|
|
|
|
# use notice to filter all waku messaging
|
2024-05-17 14:28:54 +00:00
|
|
|
|
setupLog(logging.LogLevel.NOTICE, logging.LogFormat.TEXT)
|
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
|
notice "starting subscriber", wakuPort = wakuPort, discv5Port = discv5Port
|
|
|
|
|
let
|
|
|
|
|
nodeKey = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
|
|
|
|
ip = parseIpAddress("0.0.0.0")
|
2024-08-13 11:27:34 +00:00
|
|
|
|
flags = CapabilitiesBitfield.init(relay = true)
|
2023-11-21 20:15:39 +00:00
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
|
var enrBuilder = EnrBuilder.init(nodeKey)
|
|
|
|
|
|
|
|
|
|
let recordRes = enrBuilder.build()
|
|
|
|
|
let record =
|
|
|
|
|
if recordRes.isErr():
|
|
|
|
|
error "failed to create enr record", error = recordRes.error
|
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
else:
|
|
|
|
|
recordRes.get()
|
|
|
|
|
|
|
|
|
|
var builder = WakuNodeBuilder.init()
|
|
|
|
|
builder.withNodeKey(nodeKey)
|
|
|
|
|
builder.withRecord(record)
|
|
|
|
|
builder.withNetworkConfigurationDetails(ip, Port(wakuPort)).tryGet()
|
|
|
|
|
let node = builder.build().tryGet()
|
|
|
|
|
|
|
|
|
|
var bootstrapNodeEnr: enr.Record
|
|
|
|
|
discard bootstrapNodeEnr.fromURI(bootstrapNode)
|
|
|
|
|
|
|
|
|
|
let discv5Conf = WakuDiscoveryV5Config(
|
|
|
|
|
discv5Config: none(DiscoveryConfig),
|
|
|
|
|
address: ip,
|
|
|
|
|
port: Port(discv5Port),
|
|
|
|
|
privateKey: keys.PrivateKey(nodeKey.skkey),
|
|
|
|
|
bootstrapRecords: @[bootstrapNodeEnr],
|
|
|
|
|
autoupdateRecord: true,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# assumes behind a firewall, so not care about being discoverable
|
|
|
|
|
let wakuDiscv5 = WakuDiscoveryV5.new(
|
|
|
|
|
node.rng,
|
|
|
|
|
discv5Conf,
|
|
|
|
|
some(node.enr),
|
|
|
|
|
some(node.peerManager),
|
|
|
|
|
node.topicSubscriptionQueue,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
await node.start()
|
|
|
|
|
await node.mountRelay()
|
|
|
|
|
node.peerManager.start()
|
|
|
|
|
|
|
|
|
|
(await wakuDiscv5.start()).isOkOr:
|
|
|
|
|
error "failed to start discv5", error = error
|
|
|
|
|
quit(1)
|
|
|
|
|
|
|
|
|
|
# wait for a minimum of peers to be connected, otherwise messages wont be gossiped
|
|
|
|
|
while true:
|
2024-09-27 12:46:46 +00:00
|
|
|
|
let numConnectedPeers = node.peerManager.wakuPeerStore[ConnectionBook].book
|
|
|
|
|
.values()
|
|
|
|
|
.countIt(it == Connected)
|
2024-03-15 23:08:47 +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/
|
2024-07-09 15:36:12 +00:00
|
|
|
|
let pubSubTopic = PubsubTopic("/waku/2/rs/0/0")
|
2024-03-15 23:08:47 +00:00
|
|
|
|
|
|
|
|
|
# any content topic can be chosen. make sure it matches the publisher
|
|
|
|
|
let contentTopic = ContentTopic("/examples/1/pubsub-example/proto")
|
|
|
|
|
|
|
|
|
|
proc handler(topic: PubsubTopic, msg: WakuMessage): Future[void] {.async, gcsafe.} =
|
|
|
|
|
let payloadStr = string.fromBytes(msg.payload)
|
|
|
|
|
if msg.contentTopic == contentTopic:
|
|
|
|
|
notice "message received",
|
|
|
|
|
payload = payloadStr,
|
|
|
|
|
pubsubTopic = pubsubTopic,
|
|
|
|
|
contentTopic = msg.contentTopic,
|
|
|
|
|
timestamp = msg.timestamp
|
|
|
|
|
|
2024-04-09 08:17:46 +00:00
|
|
|
|
node.subscribe((kind: PubsubSub, topic: pubsubTopic), some(WakuRelayHandler(handler)))
|
2022-11-04 12:40:24 +00:00
|
|
|
|
|
2023-02-06 11:53:05 +00:00
|
|
|
|
when isMainModule:
|
|
|
|
|
let rng = crypto.newRng()
|
|
|
|
|
asyncSpawn setupAndSubscribe(rng)
|
|
|
|
|
runForever()
|