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]