|
| 1 | +import |
| 2 | + chronicles, |
| 3 | + chronos, |
| 4 | + confutils, |
| 5 | + eth/keys, |
| 6 | + eth/p2p/discoveryv5/enr, |
| 7 | + libp2p/crypto/crypto, |
| 8 | + std/random, |
| 9 | + stew/byteutils, |
| 10 | + strformat, |
| 11 | + waku/[ |
| 12 | + common/logging, |
| 13 | + node/peer_manager, |
| 14 | + waku_core, |
| 15 | + waku_node, |
| 16 | + waku_enr, |
| 17 | + discovery/waku_discv5, |
| 18 | + factory/builder, |
| 19 | + waku_filter_v2/client, |
| 20 | + ] |
| 21 | + |
| 22 | +import utils |
| 23 | + |
| 24 | + |
| 25 | +const |
| 26 | + StaticPeer = "/ip4/64.225.80.192/tcp/30303/p2p/16Uiu2HAmNaeL4p3WEYzC9mgXBmBWSgWjPHRvatZTXnp8Jgv3iKsb" |
| 27 | + FilterContentTopic = ContentTopic("/chatsdk/test/proto") |
| 28 | + |
| 29 | + |
| 30 | +type PayloadHandler* = proc(pubsubTopic: string, message: seq[byte]): Future[void] {. |
| 31 | + gcsafe, raises: [Defect] |
| 32 | + .} |
| 33 | + |
| 34 | +type WakuConfig* = object |
| 35 | + port*: uint16 |
| 36 | + clusterId*: uint16 |
| 37 | + shardId*: seq[uint16] ## @[0'u16] |
| 38 | + pubsubTopic*: string |
| 39 | + |
| 40 | +type |
| 41 | + WakuClient* = ref object |
| 42 | + cfg: WakuConfig |
| 43 | + node*: WakuNode |
| 44 | + handlers: seq[PayloadHandler] |
| 45 | + |
| 46 | + |
| 47 | +proc DefaultConfig*(): WakuConfig = |
| 48 | + |
| 49 | + let clusterId = 1'u16 |
| 50 | + let shardId = 3'u16 |
| 51 | + var port: uint16 = 50000'u16 + uint16(rand(200)) |
| 52 | + |
| 53 | + result = WakuConfig(port: port, clusterId: clusterId, shardId: @[shardId], |
| 54 | + pubsubTopic: &"/waku/2/rs/{clusterId}/{shardId}") |
| 55 | + |
| 56 | + |
| 57 | +proc sendMessage*(client: WakuClient, payload: string): Future[void] {.async.} = |
| 58 | + let bytes = payload.toBytes |
| 59 | + |
| 60 | + var msg = WakuMessage( |
| 61 | + payload: bytes, |
| 62 | + contentTopic: FilterContentTopic, |
| 63 | + ephemeral: true, |
| 64 | + version: 0, |
| 65 | + timestamp: getTimestamp() |
| 66 | + ) |
| 67 | + |
| 68 | + let pubMsg = WakuMessage(payload: bytes) |
| 69 | + let res = await client.node.publish(some(PubsubTopic(client.cfg.pubsubTopic)), msg) |
| 70 | + if res.isErr: |
| 71 | + error "Failed to Publish", err = res.error, |
| 72 | + pubsubTopic = client.cfg.pubsubTopic |
| 73 | + |
| 74 | + info "SendMessage", payload = payload, pubsubTopic = client.cfg.pubsubTopic, msg = msg |
| 75 | + |
| 76 | +proc buildWakuNode(cfg: WakuConfig): WakuNode = |
| 77 | + |
| 78 | + let |
| 79 | + nodeKey = crypto.PrivateKey.random(Secp256k1, crypto.newRng()[])[] |
| 80 | + ip = parseIpAddress("0.0.0.0") |
| 81 | + flags = CapabilitiesBitfield.init(relay = true) |
| 82 | + |
| 83 | + let relayShards = RelayShards.init(cfg.clusterId, cfg.shardId).valueOr: |
| 84 | + error "Relay shards initialization failed", error = error |
| 85 | + quit(QuitFailure) |
| 86 | + |
| 87 | + var enrBuilder = EnrBuilder.init(nodeKey) |
| 88 | + enrBuilder.withWakuRelaySharding(relayShards).expect( |
| 89 | + "Building ENR with relay sharding failed" |
| 90 | + ) |
| 91 | + |
| 92 | + let recordRes = enrBuilder.build() |
| 93 | + let record = |
| 94 | + if recordRes.isErr(): |
| 95 | + error "failed to create enr record", error = recordRes.error |
| 96 | + quit(QuitFailure) |
| 97 | + else: |
| 98 | + recordRes.get() |
| 99 | + |
| 100 | + var builder = WakuNodeBuilder.init() |
| 101 | + builder.withNodeKey(nodeKey) |
| 102 | + builder.withRecord(record) |
| 103 | + builder.withNetworkConfigurationDetails(ip, Port(cfg.port)).tryGet() |
| 104 | + let node = builder.build().tryGet() |
| 105 | + |
| 106 | + node.mountMetadata(cfg.clusterId).expect("failed to mount waku metadata protocol") |
| 107 | + |
| 108 | + result = node |
| 109 | + |
| 110 | +proc messageHandler(client: WakuClient, pubsubTopic: PubsubTopic, |
| 111 | + message: WakuMessage |
| 112 | +) {.async, gcsafe.} = |
| 113 | + let payloadStr = string.fromBytes(message.payload) |
| 114 | + notice "message received", |
| 115 | + payload = payloadStr, |
| 116 | + pubsubTopic = pubsubTopic, |
| 117 | + contentTopic = message.contentTopic, |
| 118 | + timestamp = message.timestamp |
| 119 | + |
| 120 | + |
| 121 | + for handler in client.handlers: |
| 122 | + discard handler(pubsubTopic, message.payload) |
| 123 | + |
| 124 | + |
| 125 | +proc taskKeepAlive(client: WakuClient) {.async.} = |
| 126 | + let peer = parsePeerInfo(StaticPeer).get() |
| 127 | + while true: |
| 128 | + notice "maintaining subscription" |
| 129 | + # First use filter-ping to check if we have an active subscription |
| 130 | + let pingRes = await client.node.wakuFilterClient.ping(peer) |
| 131 | + if pingRes.isErr(): |
| 132 | + # No subscription found. Let's subscribe. |
| 133 | + notice "no subscription found. Sending subscribe request" |
| 134 | + |
| 135 | + let subscribeRes = await client.node.wakuFilterClient.subscribe( |
| 136 | + peer, client.cfg.pubsubTopic, @[FilterContentTopic] |
| 137 | + ) |
| 138 | + |
| 139 | + if subscribeRes.isErr(): |
| 140 | + notice "subscribe request failed. Quitting.", err = subscribeRes.error |
| 141 | + break |
| 142 | + else: |
| 143 | + notice "subscribe request successful." |
| 144 | + else: |
| 145 | + notice "subscription found." |
| 146 | + |
| 147 | + await sleepAsync(60.seconds) # Subscription maintenance interval |
| 148 | + |
| 149 | + |
| 150 | +proc taskPublishDemo(client: WakuClient){.async.} = |
| 151 | + for i in 0 ..< 15: |
| 152 | + await client.sendMessage("Hello") |
| 153 | + await sleepAsync(30.seconds) # Subscription maintenance interval |
| 154 | + |
| 155 | +proc start*(client: WakuClient) {.async.} = |
| 156 | + setupLog(logging.LogLevel.NOTICE, logging.LogFormat.TEXT) |
| 157 | + |
| 158 | + await client.node.mountFilterClient() |
| 159 | + |
| 160 | + await client.node.start() |
| 161 | + (await client.node.mountRelay()).isOkOr: |
| 162 | + error "failed to mount relay", error = error |
| 163 | + quit(1) |
| 164 | + |
| 165 | + client.node.peerManager.start() |
| 166 | + |
| 167 | + let subscription: SubscriptionEvent = (kind: PubsubSub, topic: |
| 168 | + client.cfg.pubsubTopic) |
| 169 | + |
| 170 | + let msg_handler = proc(pubsubTopic: PubsubTopic, |
| 171 | + message: WakuMessage) {.async, gcsafe.} = discard client.messageHandler( |
| 172 | + pubsubTopic, message) |
| 173 | + |
| 174 | + let res = subscribe(client.node, subscription, msg_handler) |
| 175 | + if res.isErr: |
| 176 | + error "Subscribe failed", err = res.error |
| 177 | + |
| 178 | + await allFutures(taskKeepAlive(client), taskPublishDemo(client)) |
| 179 | + |
| 180 | +proc initWakuClient*(cfg: WakuConfig, handlers: seq[ |
| 181 | + PayloadHandler]): WakuClient = |
| 182 | + result = WakuClient(cfg: cfg, node: buildWakuNode(cfg), handlers: handlers) |
0 commit comments