diff --git a/examples/lightpush_publisher_mix.nim b/examples/lightpush_publisher_mix.nim index b43df09c1..8eca6ad8e 100644 --- a/examples/lightpush_publisher_mix.nim +++ b/examples/lightpush_publisher_mix.nim @@ -1,5 +1,5 @@ import - std/[tables, times, sequtils], + std/[tables, times, sequtils, strutils], stew/byteutils, stew/shims/net, chronicles, @@ -26,12 +26,11 @@ import waku_enr, discovery/waku_discv5, factory/builder, - waku_lightpush/client + waku_lightpush/client, ], ./lightpush_publisher_mix_config, ./lightpush_publisher_mix_metrics - proc now*(): Timestamp = getNanosecondTime(getTime().toUnixFloat()) @@ -42,6 +41,17 @@ const LightpushPubsubTopic = PubsubTopic("/waku/2/rs/66/0") LightpushContentTopic = ContentTopic("/examples/1/light-pubsub-mix-example/proto") +proc splitPeerIdAndAddr(maddr: string): (string, string) = + let parts = maddr.split("/p2p/") + if parts.len != 2: + error "Invalid multiaddress format", parts = parts + return + + let + address = parts[0] + peerId = parts[1] + return (address, peerId) + proc setupAndPublish(rng: ref HmacDrbgContext, conf: LPMixConf) {.async.} = # use notice to filter all waku messaging setupLog(logging.LogLevel.DEBUG, logging.LogFormat.TEXT) @@ -86,17 +96,15 @@ proc setupAndPublish(rng: ref HmacDrbgContext, conf: LPMixConf) {.async.} = errmsg = getCurrentExceptionMsg() return - let pxPeerInfo = RemotePeerInfo.init( - conf.destPeerId, - @[MultiAddress.init(conf.destPeerAddr).get()], - ) + let (destPeerAddr, destPeerId) = splitPeerIdAndAddr(conf.destPeerAddr) + let (pxPeerAddr, pxPeerId) = splitPeerIdAndAddr(conf.pxAddr) + + let pxPeerInfo = + RemotePeerInfo.init(destPeerId, @[MultiAddress.init(destPeerAddr).get()]) node.peerManager.addServicePeer(pxPeerInfo, WakuPeerExchangeCodec) - - let pxPeerInfo1 = RemotePeerInfo.init( - conf.pxId, - @[MultiAddress.init(conf.pxAddr).get()], - ) + let pxPeerInfo1 = + RemotePeerInfo.init(pxPeerId, @[MultiAddress.init(pxPeerAddr).get()]) node.peerManager.addServicePeer(pxPeerInfo1, WakuPeerExchangeCodec) let keyPairResult = generateKeyPair() @@ -104,21 +112,15 @@ proc setupAndPublish(rng: ref HmacDrbgContext, conf: LPMixConf) {.async.} = return let (mixPrivKey, mixPubKey) = keyPairResult.get() - ( - await node.mountMix(mixPrivKey) - ).isOkOr: + (await node.mountMix(mixPrivKey)).isOkOr: error "failed to mount waku mix protocol: ", error = $error return - - let destPeerId = PeerId.init(conf.destPeerId).valueOr: + let dPeerId = PeerId.init(destPeerId).valueOr: error "Failed to initialize PeerId", err = error return let conn = MixEntryConnection.newConn( - conf.destPeerAddr, - destPeerId, - ProtocolType.fromString(WakuLightPushCodec), - node.mix, + destPeerAddr, dPeerId, ProtocolType.fromString(WakuLightPushCodec), node.mix ) await node.start() @@ -136,7 +138,8 @@ proc setupAndPublish(rng: ref HmacDrbgContext, conf: LPMixConf) {.async.} = currentpoolSize = node.getMixNodePoolSize() await sleepAsync(1000) - notice "publisher service started with mix node pool size ", currentpoolSize = node.getMixNodePoolSize() + notice "publisher service started with mix node pool size ", + currentpoolSize = node.getMixNodePoolSize() var i = 0 while i < conf.numMsgs: i = i + 1 diff --git a/examples/lightpush_publisher_mix_config.nim b/examples/lightpush_publisher_mix_config.nim index 63b59f730..d494b5319 100644 --- a/examples/lightpush_publisher_mix_config.nim +++ b/examples/lightpush_publisher_mix_config.nim @@ -1,51 +1,24 @@ -import - confutils/defs +import confutils/defs +type LPMixConf* = object + destPeerAddr* {.desc: "Destination peer address with peerId.", name: "dp-addr".}: + string -type - LPMixConf* = object + pxAddr* {.desc: "Peer exchange address with peerId.", name: "px-addr".}: string - destPeerAddr* {. - desc: "Destination peer address.", - name: "dp-addr", - }: string + port* {.desc: "Port to listen on.", defaultValue: 50000, name: "port".}: int - destPeerId* {. - desc: "Destination peer ID.", - name: "dp-id", - }: string + numMsgs* {.desc: "Number of messages to send.", defaultValue: 1, name: "num-msgs".}: + int - pxAddr* {. - desc: "Peer exchange address.", - defaultValue: "localhost:50001", - name: "px-addr", - }: string - pxId* {. - desc: "Peer exchange ID.", - defaultValue: "waku-v2-peer-exchange", - name: "px-id", - }: string + msgInterval* {. + desc: "Interval between messages in milliseconds.", + defaultValue: 1000, + name: "msg-interval" + .}: int - port* {. - desc: "Port to listen on.", - defaultValue: 50000, - name: "port", - }: int - - numMsgs* {. - desc: "Number of messages to send.", - defaultValue: 1, - name: "num-msgs", - }: int - - msgInterval*{. - desc: "Interval between messages in milliseconds.", - defaultValue: 1000, - name: "msg-interval", - }: int - - minMixPoolSize* {. - desc: "Number of messages to wait for before sending.", - defaultValue: 3, - name: "min-mix-pool-size", - }: int \ No newline at end of file + minMixPoolSize* {. + desc: "Number of messages to wait for before sending.", + defaultValue: 3, + name: "min-mix-pool-size" + .}: int