From f28cc7118cf101c6d9694a0596c4604a41336b09 Mon Sep 17 00:00:00 2001 From: NagyZoltanPeter <113987313+NagyZoltanPeter@users.noreply.github.com> Date: Mon, 13 Jul 2026 14:17:09 +0200 Subject: [PATCH] Fix FFI peerId pretty print (#4023) * Add import fix to node_api too as that has a single stringify peerId spot. Thx @fcecin --- examples/cpp/waku.cpp | 28 +++++++++++++++++++++++++ library/logos_delivery_api/node_api.nim | 1 + logos_delivery/waku/api/debug.nim | 1 + 3 files changed, 30 insertions(+) diff --git a/examples/cpp/waku.cpp b/examples/cpp/waku.cpp index d2808537f..17fc00f89 100644 --- a/examples/cpp/waku.cpp +++ b/examples/cpp/waku.cpp @@ -20,6 +20,8 @@ pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; int callback_executed = 0; +// Return code (RET_OK / RET_ERR) reported by the most recent FFI callback. +volatile int last_callback_ret = RET_OK; void waitForCallback() { @@ -108,6 +110,7 @@ auto cify(F &&f) static F fn = std::forward(f); return [](int callerRet, const char *msg, size_t len, void *userData) { + last_callback_ret = callerRet; signal_cond(); return fn(msg, len); }; @@ -259,6 +262,23 @@ int main(int argc, char **argv) nullptr); waitForCallback(); + // Node creation is asynchronous: logosdelivery_create_node returns a non-null + // ctx as soon as the request is enqueued, but the config may still fail to + // parse on the worker thread. Using ctx after a failed creation dereferences + // a nil node inside the library and crashes (SIGSEGV). Bail out cleanly. + if (ctx == nullptr || last_callback_ret != RET_OK) + { + std::cerr << "Failed to create the node. Aborting." << std::endl; + if (ctx != nullptr) + { + logosdelivery_destroy(ctx, + cify([](const char *msg, size_t len) {}), + nullptr); + waitForCallback(); + } + return 1; + } + // example on how to retrieve a value from the `libwaku` callback. std::string defaultPubsubTopic; WAKU_CALL( @@ -303,6 +323,14 @@ int main(int argc, char **argv) nullptr, defaultPubsubTopic.c_str())); + std::string myPeerId; + WAKU_CALL(waku_get_my_peerid(ctx, + cify([&myPeerId](const char *msg, size_t len) + { myPeerId = msg; }), + nullptr)); + + std::cout << "My peer id: " << myPeerId << std::endl; + show_main_menu(); while (1) { diff --git a/library/logos_delivery_api/node_api.nim b/library/logos_delivery_api/node_api.nim index d70c34252..9547257b1 100644 --- a/library/logos_delivery_api/node_api.nim +++ b/library/logos_delivery_api/node_api.nim @@ -1,5 +1,6 @@ import std/json import chronos, chronicles, results, ffi +import libp2p/peerid # pull PeerId pretty string formatting import logos_delivery/waku/common/base64 import logos_delivery, diff --git a/logos_delivery/waku/api/debug.nim b/logos_delivery/waku/api/debug.nim index 10582316b..8b228b7ef 100644 --- a/logos_delivery/waku/api/debug.nim +++ b/logos_delivery/waku/api/debug.nim @@ -3,6 +3,7 @@ import results, chronos, chronicles, metrics import eth/p2p/discoveryv5/enr +import libp2p/peerid # pull PeerId pretty string formatting import logos_delivery/waku/waku import logos_delivery/waku/[waku_core, node/waku_node]