From 204cf36085558d1dbdecfb0c3bb2d79c7df3a4d6 Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Sun, 14 Dec 2025 00:43:43 +0100 Subject: [PATCH] adapt library codebase to latest nim-ffi --- .../debug_node_request.nim | 8 ++++---- .../discovery_request.nim | 8 ++++---- .../node_lifecycle_request.nim | 9 +++++---- .../peer_manager_request.nim | 18 ++++++++--------- library/waku_thread_requests/ping_request.nim | 4 ++-- .../protocols/filter_request.nim | 20 +++++++++---------- .../protocols/lightpush_request.nim | 6 +++--- .../protocols/relay_request.nim | 16 +++++++-------- .../protocols/store_request.nim | 2 +- 9 files changed, 46 insertions(+), 45 deletions(-) diff --git a/library/waku_thread_requests/debug_node_request.nim b/library/waku_thread_requests/debug_node_request.nim index 8f67e0d50..98f5332b4 100644 --- a/library/waku_thread_requests/debug_node_request.nim +++ b/library/waku_thread_requests/debug_node_request.nim @@ -26,17 +26,17 @@ proc waku_listen_addresses( ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer ) {.ffi.} = ## returns a comma-separated string of the listen addresses - return ok(ctx.myLib.node.getMultiaddresses().join(",")) + return ok(ctx.myLib[].node.getMultiaddresses().join(",")) proc waku_get_my_enr( ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer ) {.ffi.} = - return ok(ctx.myLib.node.enr.toURI()) + return ok(ctx.myLib[].node.enr.toURI()) proc waku_get_my_peerid( ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer ) {.ffi.} = - return ok($ctx.myLib.node.peerId()) + return ok($ctx.myLib[].node.peerId()) proc waku_get_metrics( ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer @@ -46,4 +46,4 @@ proc waku_get_metrics( proc waku_is_online( ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer ) {.ffi.} = - return ok($ctx.myLib.healthMonitor.onlineMonitor.amIOnline()) + return ok($ctx.myLib[].healthMonitor.onlineMonitor.amIOnline()) diff --git a/library/waku_thread_requests/discovery_request.nim b/library/waku_thread_requests/discovery_request.nim index 5bad0f4ec..f61b7bad1 100644 --- a/library/waku_thread_requests/discovery_request.nim +++ b/library/waku_thread_requests/discovery_request.nim @@ -47,7 +47,7 @@ proc waku_discv5_update_bootnodes( ## Updates the bootnode list used for discovering new peers via DiscoveryV5 ## bootnodes - JSON array containing the bootnode ENRs i.e. `["enr:...", "enr:..."]` - updateDiscv5BootstrapNodes($bootnodes, ctx.myLib).isOkOr: + updateDiscv5BootstrapNodes($bootnodes, ctx.myLib[]).isOkOr: error "UPDATE_DISCV5_BOOTSTRAP_NODES failed", error = error return err($error) @@ -71,7 +71,7 @@ proc waku_dns_discovery( proc waku_start_discv5( ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer ) {.ffi.} = - (await ctx.myLib.wakuDiscv5.start()).isOkOr: + (await ctx.myLib[].wakuDiscv5.start()).isOkOr: error "START_DISCV5 failed", error = error return err("error starting discv5: " & $error) @@ -80,7 +80,7 @@ proc waku_start_discv5( proc waku_stop_discv5( ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer ) {.ffi.} = - await ctx.myLib.wakuDiscv5.stop() + await ctx.myLib[].wakuDiscv5.stop() return ok("discv5 stopped correctly") proc waku_peer_exchange_request( @@ -89,7 +89,7 @@ proc waku_peer_exchange_request( userData: pointer, numPeers: uint64, ) {.ffi.} = - let numValidPeers = (await performPeerExchangeRequestTo(numPeers, ctx.myLib)).valueOr: + let numValidPeers = (await performPeerExchangeRequestTo(numPeers, ctx.myLib[])).valueOr: error "waku_peer_exchange_request failed", error = error return err("failed peer exchange: " & $error) diff --git a/library/waku_thread_requests/node_lifecycle_request.nim b/library/waku_thread_requests/node_lifecycle_request.nim index 93313d03c..a2bb25609 100644 --- a/library/waku_thread_requests/node_lifecycle_request.nim +++ b/library/waku_thread_requests/node_lifecycle_request.nim @@ -58,19 +58,20 @@ proc createWaku( return ok(wakuRes) -registerReqFFI(CreateNodeRequest, waku: ptr Waku): +registerReqFFI(CreateNodeRequest, ctx: ptr FFIContext[Waku]): proc( configJson: cstring, appCallbacks: AppCallbacks ): Future[Result[string, string]] {.async.} = - waku[] = (await createWaku(configJson, cast[AppCallbacks](appCallbacks))).valueOr: + ctx.myLib[] = (await createWaku(configJson, cast[AppCallbacks](appCallbacks))).valueOr: error "CreateNodeRequest failed", error = error return err($error) + return ok("") proc waku_start( ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer ) {.ffi.} = - (await startWaku(addr ctx.myLib)).isOkOr: + (await startWaku(ctx[].myLib)).isOkOr: error "START_NODE failed", error = error return err("failed to start: " & $error) return ok("") @@ -79,7 +80,7 @@ proc waku_stop( ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer ) {.ffi.} = try: - await ctx.myLib.stop() + await ctx.myLib[].stop() except Exception as exc: error "STOP_NODE failed", error = exc.msg return err("failed to stop: " & exc.msg) diff --git a/library/waku_thread_requests/peer_manager_request.nim b/library/waku_thread_requests/peer_manager_request.nim index 3dbd50fde..f0ae37f00 100644 --- a/library/waku_thread_requests/peer_manager_request.nim +++ b/library/waku_thread_requests/peer_manager_request.nim @@ -11,7 +11,7 @@ proc waku_get_peerids_from_peerstore( ) {.ffi.} = ## returns a comma-separated string of peerIDs let peerIDs = - ctx.myLib.node.peerManager.switch.peerStore.peers().mapIt($it.peerId).join(",") + ctx.myLib[].node.peerManager.switch.peerStore.peers().mapIt($it.peerId).join(",") return ok(peerIDs) proc waku_connect( @@ -22,7 +22,7 @@ proc waku_connect( timeoutMs: cuint, ) {.ffi.} = let peers = ($peerMultiAddr).split(",").mapIt(strip(it)) - await ctx.myLib.node.connectToNodes(peers, source = "static") + await ctx.myLib[].node.connectToNodes(peers, source = "static") return ok("") proc waku_disconnect_peer_by_id( @@ -31,13 +31,13 @@ proc waku_disconnect_peer_by_id( let pId = PeerId.init($peerId).valueOr: error "DISCONNECT_PEER_BY_ID failed", error = $error return err($error) - await ctx.myLib.node.peerManager.disconnectNode(pId) + await ctx.myLib[].node.peerManager.disconnectNode(pId) return ok("") proc waku_disconnect_all_peers( ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer ) {.ffi.} = - await ctx.myLib.node.peerManager.disconnectAllPeers() + await ctx.myLib[].node.peerManager.disconnectAllPeers() return ok("") proc waku_dial_peer( @@ -51,7 +51,7 @@ proc waku_dial_peer( let remotePeerInfo = parsePeerInfo($peerMultiAddr).valueOr: error "DIAL_PEER failed", error = $error return err($error) - let conn = await ctx.myLib.node.peerManager.dialPeer(remotePeerInfo, $protocol) + let conn = await ctx.myLib[].node.peerManager.dialPeer(remotePeerInfo, $protocol) if conn.isNone(): let msg = "failed dialing peer" error "DIAL_PEER failed", error = msg, peerId = $remotePeerInfo.peerId @@ -69,7 +69,7 @@ proc waku_dial_peer_by_id( let pId = PeerId.init($peerId).valueOr: error "DIAL_PEER_BY_ID failed", error = $error return err($error) - let conn = await ctx.myLib.node.peerManager.dialPeer(pId, $protocol) + let conn = await ctx.myLib[].node.peerManager.dialPeer(pId, $protocol) if conn.isNone(): let msg = "failed dialing peer" error "DIAL_PEER_BY_ID failed", error = msg, peerId = $peerId @@ -83,7 +83,7 @@ proc waku_get_connected_peers_info( ## returns a JSON string mapping peerIDs to objects with protocols and addresses var peersMap = initTable[string, PeerInfo]() - let peers = ctx.myLib.node.peerManager.switch.peerStore.peers().filterIt( + let peers = ctx.myLib[].node.peerManager.switch.peerStore.peers().filterIt( it.connectedness == Connected ) @@ -103,7 +103,7 @@ proc waku_get_connected_peers( ) {.ffi.} = ## returns a comma-separated string of peerIDs let - (inPeerIds, outPeerIds) = ctx.myLib.node.peerManager.connectedPeers() + (inPeerIds, outPeerIds) = ctx.myLib[].node.peerManager.connectedPeers() connectedPeerids = concat(inPeerIds, outPeerIds) return ok(connectedPeerids.mapIt($it).join(",")) @@ -115,7 +115,7 @@ proc waku_get_peerids_by_protocol( protocol: cstring, ) {.ffi.} = ## returns a comma-separated string of peerIDs that mount the given protocol - let connectedPeers = ctx.myLib.node.peerManager.switch.peerStore + let connectedPeers = ctx.myLib[].node.peerManager.switch.peerStore .peers($protocol) .filterIt(it.connectedness == Connected) .mapIt($it.peerId) diff --git a/library/waku_thread_requests/ping_request.nim b/library/waku_thread_requests/ping_request.nim index 3bc7268f6..4f10dcf59 100644 --- a/library/waku_thread_requests/ping_request.nim +++ b/library/waku_thread_requests/ping_request.nim @@ -17,11 +17,11 @@ proc waku_ping_peer( proc ping(): Future[Result[Duration, string]] {.async, gcsafe.} = try: let conn = - await ctx.myLib.node.switch.dial(peerInfo.peerId, peerInfo.addrs, PingCodec) + await ctx.myLib[].node.switch.dial(peerInfo.peerId, peerInfo.addrs, PingCodec) defer: await conn.close() - let pingRTT = await ctx.myLib.node.libp2pPing.ping(conn) + let pingRTT = await ctx.myLib[].node.libp2pPing.ping(conn) if pingRTT == 0.nanos: return err("could not ping peer: rtt-0") return ok(pingRTT) diff --git a/library/waku_thread_requests/protocols/filter_request.nim b/library/waku_thread_requests/protocols/filter_request.nim index 552d740bf..c4f99510a 100644 --- a/library/waku_thread_requests/protocols/filter_request.nim +++ b/library/waku_thread_requests/protocols/filter_request.nim @@ -36,18 +36,18 @@ proc waku_filter_subscribe( callEventCallback(ctx, "onReceivedMessage"): $JsonMessageEvent.new(pubsubTopic, msg) - checkFilterClientMounted(ctx.myLib).isOkOr: + checkFilterClientMounted(ctx.myLib[]).isOkOr: return err($error) var filterPushEventCallback = FilterPushHandler(onReceivedMessage(ctx)) - ctx.myLib.node.wakuFilterClient.registerPushHandler(filterPushEventCallback) + ctx.myLib[].node.wakuFilterClient.registerPushHandler(filterPushEventCallback) - let peer = ctx.myLib.node.peerManager.selectPeer(WakuFilterSubscribeCodec).valueOr: + let peer = ctx.myLib[].node.peerManager.selectPeer(WakuFilterSubscribeCodec).valueOr: let errorMsg = "could not find peer with WakuFilterSubscribeCodec when subscribing" error "fail filter subscribe", error = errorMsg return err(errorMsg) - let subFut = ctx.myLib.node.filterSubscribe( + let subFut = ctx.myLib[].node.filterSubscribe( some(PubsubTopic($pubsubTopic)), ($contentTopics).split(",").mapIt(ContentTopic(it)), peer, @@ -67,16 +67,16 @@ proc waku_filter_unsubscribe( pubSubTopic: cstring, contentTopics: cstring, ) {.ffi.} = - checkFilterClientMounted(ctx.myLib).isOkOr: + checkFilterClientMounted(ctx.myLib[]).isOkOr: return err($error) - let peer = ctx.myLib.node.peerManager.selectPeer(WakuFilterSubscribeCodec).valueOr: + let peer = ctx.myLib[].node.peerManager.selectPeer(WakuFilterSubscribeCodec).valueOr: let errorMsg = "could not find peer with WakuFilterSubscribeCodec when unsubscribing" error "fail filter process", error = errorMsg return err(errorMsg) - let subFut = ctx.myLib.node.filterUnsubscribe( + let subFut = ctx.myLib[].node.filterUnsubscribe( some(PubsubTopic($pubsubTopic)), ($contentTopics).split(",").mapIt(ContentTopic(it)), peer, @@ -90,16 +90,16 @@ proc waku_filter_unsubscribe( proc waku_filter_unsubscribe_all( ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer ) {.ffi.} = - checkFilterClientMounted(ctx.myLib).isOkOr: + checkFilterClientMounted(ctx.myLib[]).isOkOr: return err($error) - let peer = ctx.myLib.node.peerManager.selectPeer(WakuFilterSubscribeCodec).valueOr: + let peer = ctx.myLib[].node.peerManager.selectPeer(WakuFilterSubscribeCodec).valueOr: let errorMsg = "could not find peer with WakuFilterSubscribeCodec when unsubscribing all" error "fail filter unsubscribe all", error = errorMsg return err(errorMsg) - let unsubFut = ctx.myLib.node.filterUnsubscribeAll(peer) + let unsubFut = ctx.myLib[].node.filterUnsubscribeAll(peer) if not await unsubFut.withTimeout(FilterOpTimeout): let errorMsg = "filter un-subscription all timed out" diff --git a/library/waku_thread_requests/protocols/lightpush_request.nim b/library/waku_thread_requests/protocols/lightpush_request.nim index 05dc773fb..e9251a3f3 100644 --- a/library/waku_thread_requests/protocols/lightpush_request.nim +++ b/library/waku_thread_requests/protocols/lightpush_request.nim @@ -18,7 +18,7 @@ proc waku_lightpush_publish( pubSubTopic: cstring, jsonWakuMessage: cstring, ) {.ffi.} = - if ctx.myLib.node.wakuLightpushClient.isNil(): + if ctx.myLib[].node.wakuLightpushClient.isNil(): let errorMsg = "LightpushRequest waku.node.wakuLightpushClient is nil" error "PUBLISH failed", error = errorMsg return err(errorMsg) @@ -34,14 +34,14 @@ proc waku_lightpush_publish( let msg = json_message_event.toWakuMessage(jsonMessage).valueOr: return err("Problem building the WakuMessage: " & $error) - let peerOpt = ctx.myLib.node.peerManager.selectPeer(WakuLightPushCodec) + let peerOpt = ctx.myLib[].node.peerManager.selectPeer(WakuLightPushCodec) if peerOpt.isNone(): let errorMsg = "failed to lightpublish message, no suitable remote peers" error "PUBLISH failed", error = errorMsg return err(errorMsg) let msgHashHex = ( - await ctx.myLib.node.wakuLegacyLightpushClient.publish( + await ctx.myLib[].node.wakuLegacyLightpushClient.publish( $pubsubTopic, msg, peer = peerOpt.get() ) ).valueOr: diff --git a/library/waku_thread_requests/protocols/relay_request.nim b/library/waku_thread_requests/protocols/relay_request.nim index cd56ae24f..b184d6011 100644 --- a/library/waku_thread_requests/protocols/relay_request.nim +++ b/library/waku_thread_requests/protocols/relay_request.nim @@ -19,7 +19,7 @@ proc waku_relay_get_peers_in_mesh( userData: pointer, pubSubTopic: cstring, ) {.ffi.} = - let meshPeers = ctx.myLib.node.wakuRelay.getPeersInMesh($pubsubTopic).valueOr: + let meshPeers = ctx.myLib[].node.wakuRelay.getPeersInMesh($pubsubTopic).valueOr: error "LIST_MESH_PEERS failed", error = error return err($error) ## returns a comma-separated string of peerIDs @@ -31,7 +31,7 @@ proc waku_relay_get_num_peers_in_mesh( userData: pointer, pubSubTopic: cstring, ) {.ffi.} = - let numPeersInMesh = ctx.myLib.node.wakuRelay.getNumPeersInMesh($pubsubTopic).valueOr: + let numPeersInMesh = ctx.myLib[].node.wakuRelay.getNumPeersInMesh($pubsubTopic).valueOr: error "NUM_MESH_PEERS failed", error = error return err($error) return ok($numPeersInMesh) @@ -43,7 +43,7 @@ proc waku_relay_get_connected_peers( pubSubTopic: cstring, ) {.ffi.} = ## Returns the list of all connected peers to an specific pubsub topic - let connPeers = ctx.myLib.node.wakuRelay.getConnectedPeers($pubsubTopic).valueOr: + let connPeers = ctx.myLib[].node.wakuRelay.getConnectedPeers($pubsubTopic).valueOr: error "LIST_CONNECTED_PEERS failed", error = error return err($error) ## returns a comma-separated string of peerIDs @@ -55,7 +55,7 @@ proc waku_relay_get_num_connected_peers( userData: pointer, pubSubTopic: cstring, ) {.ffi.} = - let numConnPeers = ctx.myLib.node.wakuRelay.getNumConnectedPeers($pubsubTopic).valueOr: + let numConnPeers = ctx.myLib[].node.wakuRelay.getNumConnectedPeers($pubsubTopic).valueOr: error "NUM_CONNECTED_PEERS failed", error = error return err($error) return ok($numConnPeers) @@ -72,7 +72,7 @@ proc waku_relay_add_protected_shard( try: let relayShard = RelayShard(clusterId: uint16(clusterId), shardId: uint16(shardId)) let protectedShard = ProtectedShard.parseCmdArg($relayShard & ":" & $publicKey) - ctx.myLib.node.wakuRelay.addSignedShardsValidator( + ctx.myLib[].node.wakuRelay.addSignedShardsValidator( @[protectedShard], uint16(clusterId) ) except ValueError as exc: @@ -94,7 +94,7 @@ proc waku_relay_subscribe( var cb = onReceivedMessage(ctx) - ctx.myLib.node.subscribe( + ctx.myLib[].node.subscribe( (kind: SubscriptionKind.PubsubSub, topic: $pubsubTopic), handler = WakuRelayHandler(cb), ).isOkOr: @@ -108,7 +108,7 @@ proc waku_relay_unsubscribe( userData: pointer, pubSubTopic: cstring, ) {.ffi.} = - ctx.myLib.node.unsubscribe((kind: SubscriptionKind.PubsubSub, topic: $pubsubTopic)).isOkOr: + ctx.myLib[].node.unsubscribe((kind: SubscriptionKind.PubsubSub, topic: $pubsubTopic)).isOkOr: error "UNSUBSCRIBE failed", error = error return err($error) @@ -135,7 +135,7 @@ proc waku_relay_publish( let msg = json_message_event.toWakuMessage(jsonMessage).valueOr: return err("Problem building the WakuMessage: " & $error) - (await ctx.myLib.node.wakuRelay.publish($pubsubTopic, msg)).isOkOr: + (await ctx.myLib[].node.wakuRelay.publish($pubsubTopic, msg)).isOkOr: error "PUBLISH failed", error = error return err($error) diff --git a/library/waku_thread_requests/protocols/store_request.nim b/library/waku_thread_requests/protocols/store_request.nim index e49452ef7..0df4d9b1f 100644 --- a/library/waku_thread_requests/protocols/store_request.nim +++ b/library/waku_thread_requests/protocols/store_request.nim @@ -86,7 +86,7 @@ proc waku_store_query( return err("StoreRequest failed to parse peer addr: " & $error) let queryResponse = ( - await ctx.myLib.node.wakuStoreClient.query(storeQueryRequest, peer) + await ctx.myLib[].node.wakuStoreClient.query(storeQueryRequest, peer) ).valueOr: return err("StoreRequest failed store query: " & $error)