Skip to content

Commit

Permalink
libwaky better params validation and a bit more clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivansete-status committed Aug 29, 2024
1 parent bd2cf99 commit 88e3755
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 80 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ cwaku_example: | build libwaku
./examples/cbindings/base64.c \
-lwaku -Lbuild/ \
-pthread -ldl -lm \
-lnegentropy -Lvendor/negentropy/cpp/ \
-lminiupnpc -Lvendor/nim-nat-traversal/vendor/miniupnp/miniupnpc/build/ \
-lnatpmp -Lvendor/nim-nat-traversal/vendor/libnatpmp-upstream/ \
vendor/nim-libbacktrace/libbacktrace_wrapper.o \
Expand All @@ -439,6 +440,7 @@ cppwaku_example: | build libwaku
./examples/cpp/base64.cpp \
-lwaku -Lbuild/ \
-pthread -ldl -lm \
-lnegentropy -Lvendor/negentropy/cpp/ \
-lminiupnpc -Lvendor/nim-nat-traversal/vendor/miniupnp/miniupnpc/build/ \
-lnatpmp -Lvendor/nim-nat-traversal/vendor/libnatpmp-upstream/ \
vendor/nim-libbacktrace/libbacktrace_wrapper.o \
Expand Down
8 changes: 8 additions & 0 deletions library/callback.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import ./waku_thread/waku_thread

type WakuCallBack* = proc(
callerRet: cint, msg: ptr cchar, len: csize_t, userData: pointer
) {.cdecl, gcsafe, raises: [].}

template checkLibwakuParams*(ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer) =
ctx[].userData = userData

if isNil(callback):
return RET_MISSING_CALLBACK
127 changes: 59 additions & 68 deletions library/libwaku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
when defined(linux):
{.passl: "-Wl,-soname,libwaku.so".}

import std/[json, sequtils, atomics, times, strformat, options, atomics, strutils, os]
import std/[json, sequtils, atomics, strformat, options, atomics]
import chronicles, chronos
import
waku/common/base64,
Expand Down Expand Up @@ -52,7 +52,7 @@ template foreignThreadGc(body: untyped) =
when declared(tearDownForeignThreadGc):
tearDownForeignThreadGc()

proc relayEventCallback(ctx: ptr Context): WakuRelayHandler =
proc relayEventCallback(ctx: ptr WakuContext): WakuRelayHandler =
return proc(
pubsubTopic: PubsubTopic, msg: WakuMessage
): Future[system.void] {.async.} =
Expand Down Expand Up @@ -144,10 +144,9 @@ proc waku_new(
return ctx

proc waku_destroy(
ctx: ptr Context, callback: WakuCallBack, userData: pointer
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
if isNil(callback):
return RET_MISSING_CALLBACK
checkLibwakuParams(ctx, callback, userData)

waku_thread.stopWakuThread(ctx).isOkOr:
foreignThreadGc:
Expand All @@ -158,12 +157,9 @@ proc waku_destroy(
return RET_OK

proc waku_version(
ctx: ptr Context, callback: WakuCallBack, userData: pointer
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
ctx[].userData = userData

if isNil(callback):
return RET_MISSING_CALLBACK
checkLibwakuParams(ctx, callback, userData)

foreignThreadGc:
callback(
Expand All @@ -176,13 +172,13 @@ proc waku_version(
return RET_OK

proc waku_set_event_callback(
ctx: ptr Context, callback: WakuCallBack, userData: pointer
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
) {.dynlib, exportc.} =
ctx[].eventCallback = cast[pointer](callback)
ctx[].eventUserData = userData

proc waku_content_topic(
ctx: ptr Context,
ctx: ptr WakuContext,
appName: cstring,
appVersion: cuint,
contentTopicName: cstring,
Expand All @@ -192,10 +188,7 @@ proc waku_content_topic(
): cint {.dynlib, exportc.} =
# https://rfc.vac.dev/spec/36/#extern-char-waku_content_topicchar-applicationname-unsigned-int-applicationversion-char-contenttopicname-char-encoding

ctx[].userData = userData

if isNil(callback):
return RET_MISSING_CALLBACK
checkLibwakuParams(ctx, callback, userData)

let appStr = appName.alloc()
let ctnStr = contentTopicName.alloc()
Expand All @@ -213,14 +206,11 @@ proc waku_content_topic(
return RET_OK

proc waku_pubsub_topic(
ctx: ptr Context, topicName: cstring, callback: WakuCallBack, userData: pointer
ctx: ptr WakuContext, topicName: cstring, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc, cdecl.} =
# https://rfc.vac.dev/spec/36/#extern-char-waku_pubsub_topicchar-name-char-encoding

ctx[].userData = userData

if isNil(callback):
return RET_MISSING_CALLBACK
checkLibwakuParams(ctx, callback, userData)

let topicNameStr = topicName.alloc()

Expand All @@ -234,14 +224,11 @@ proc waku_pubsub_topic(
return RET_OK

proc waku_default_pubsub_topic(
ctx: ptr Context, callback: WakuCallBack, userData: pointer
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
# https://rfc.vac.dev/spec/36/#extern-char-waku_default_pubsub_topic

ctx[].userData = userData

if isNil(callback):
return RET_MISSING_CALLBACK
checkLibwakuParams(ctx, callback, userData)

callback(
RET_OK,
Expand All @@ -253,7 +240,7 @@ proc waku_default_pubsub_topic(
return RET_OK

proc waku_relay_publish(
ctx: ptr Context,
ctx: ptr WakuContext,
pubSubTopic: cstring,
jsonWakuMessage: cstring,
timeoutMs: cuint,
Expand All @@ -262,10 +249,7 @@ proc waku_relay_publish(
): cint {.dynlib, exportc, cdecl.} =
# https://rfc.vac.dev/spec/36/#extern-char-waku_relay_publishchar-messagejson-char-pubsubtopic-int-timeoutms

ctx[].userData = userData

if isNil(callback):
return RET_MISSING_CALLBACK
checkLibwakuParams(ctx, callback, userData)

let jwm = jsonWakuMessage.alloc()
var jsonMessage: JsonMessage
Expand Down Expand Up @@ -315,9 +299,9 @@ proc waku_relay_publish(
return RET_OK

proc waku_start(
ctx: ptr Context, callback: WakuCallBack, userData: pointer
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
ctx[].userData = userData
checkLibwakuParams(ctx, callback, userData)
## TODO: handle the error
discard waku_thread.sendRequestToWakuThread(
ctx,
Expand All @@ -326,9 +310,10 @@ proc waku_start(
)

proc waku_stop(
ctx: ptr Context, callback: WakuCallBack, userData: pointer
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
ctx[].userData = userData
checkLibwakuParams(ctx, callback, userData)

## TODO: handle the error
discard waku_thread.sendRequestToWakuThread(
ctx,
Expand All @@ -337,9 +322,12 @@ proc waku_stop(
)

proc waku_relay_subscribe(
ctx: ptr Context, pubSubTopic: cstring, callback: WakuCallBack, userData: pointer
ctx: ptr WakuContext,
pubSubTopic: cstring,
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
ctx[].userData = userData
checkLibwakuParams(ctx, callback, userData)

let pst = pubSubTopic.alloc()
var cb = relayEventCallback(ctx)
Expand All @@ -360,9 +348,12 @@ proc waku_relay_subscribe(
return RET_OK

proc waku_relay_unsubscribe(
ctx: ptr Context, pubSubTopic: cstring, callback: WakuCallBack, userData: pointer
ctx: ptr WakuContext,
pubSubTopic: cstring,
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
ctx[].userData = userData
checkLibwakuParams(ctx, callback, userData)

let pst = pubSubTopic.alloc()

Expand All @@ -385,9 +376,12 @@ proc waku_relay_unsubscribe(
return RET_OK

proc waku_relay_get_num_connected_peers(
ctx: ptr Context, pubSubTopic: cstring, callback: WakuCallBack, userData: pointer
ctx: ptr WakuContext,
pubSubTopic: cstring,
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
ctx[].userData = userData
checkLibwakuParams(ctx, callback, userData)

let pst = pubSubTopic.alloc()
defer:
Expand All @@ -414,9 +408,12 @@ proc waku_relay_get_num_connected_peers(
return RET_OK

proc waku_relay_get_num_peers_in_mesh(
ctx: ptr Context, pubSubTopic: cstring, callback: WakuCallBack, userData: pointer
ctx: ptr WakuContext,
pubSubTopic: cstring,
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
ctx[].userData = userData
checkLibwakuParams(ctx, callback, userData)

let pst = pubSubTopic.alloc()
defer:
Expand All @@ -443,16 +440,13 @@ proc waku_relay_get_num_peers_in_mesh(
return RET_OK

proc waku_lightpush_publish(
ctx: ptr Context,
ctx: ptr WakuContext,
pubSubTopic: cstring,
jsonWakuMessage: cstring,
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc, cdecl.} =
ctx[].userData = userData

if isNil(callback):
return RET_MISSING_CALLBACK
checkLibwakuParams(ctx, callback, userData)

let jwm = jsonWakuMessage.alloc()
let pst = pubSubTopic.alloc()
Expand Down Expand Up @@ -498,13 +492,13 @@ proc waku_lightpush_publish(
return RET_OK

proc waku_connect(
ctx: ptr Context,
ctx: ptr WakuContext,
peerMultiAddr: cstring,
timeoutMs: cuint,
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
ctx[].userData = userData
checkLibwakuParams(ctx, callback, userData)

let connRes = waku_thread.sendRequestToWakuThread(
ctx,
Expand All @@ -521,22 +515,19 @@ proc waku_connect(
return RET_OK

proc waku_store_query(
ctx: ptr Context,
ctx: ptr WakuContext,
jsonQuery: cstring,
peerAddr: cstring,
timeoutMs: cint,
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
ctx[].userData = userData

if isNil(callback):
return RET_MISSING_CALLBACK
checkLibwakuParams(ctx, callback, userData)

let sendReqRes = waku_thread.sendRequestToWakuThread(
ctx,
RequestType.STORE,
JsonStoreQueryRequest.createShared(jsonQuery, peerAddr, timeoutMs, callback),
JsonStoreQueryRequest.createShared(jsonQuery, peerAddr, timeoutMs),
)

if sendReqRes.isErr():
Expand All @@ -549,9 +540,9 @@ proc waku_store_query(
return RET_OK

proc waku_listen_addresses(
ctx: ptr Context, callback: WakuCallBack, userData: pointer
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
ctx[].userData = userData
checkLibwakuParams(ctx, callback, userData)

let connRes = waku_thread.sendRequestToWakuThread(
ctx,
Expand All @@ -568,14 +559,14 @@ proc waku_listen_addresses(
return RET_OK

proc waku_dns_discovery(
ctx: ptr Context,
ctx: ptr WakuContext,
entTreeUrl: cstring,
nameDnsServer: cstring,
timeoutMs: cint,
callback: WakuCallBack,
userData: pointer,
): cint {.dynlib, exportc.} =
ctx[].userData = userData
checkLibwakuParams(ctx, callback, userData)

let bootstrapPeers = waku_thread.sendRequestToWakuThread(
ctx,
Expand All @@ -593,11 +584,11 @@ proc waku_dns_discovery(
return RET_OK

proc waku_discv5_update_bootnodes(
ctx: ptr Context, bootnodes: cstring, callback: WakuCallBack, userData: pointer
ctx: ptr WakuContext, bootnodes: cstring, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
## Updates the bootnode list used for discovering new peers via DiscoveryV5
## bootnodes - JSON array containing the bootnode ENRs i.e. `["enr:...", "enr:..."]`
ctx[].userData = userData
checkLibwakuParams(ctx, callback, userData)

let resp = waku_thread.sendRequestToWakuThread(
ctx,
Expand All @@ -615,9 +606,9 @@ proc waku_discv5_update_bootnodes(
return RET_OK

proc waku_get_my_enr(
ctx: ptr Context, callback: WakuCallBack, userData: pointer
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
ctx[].userData = userData
checkLibwakuParams(ctx, callback, userData)

let connRes = waku_thread.sendRequestToWakuThread(
ctx,
Expand All @@ -634,9 +625,9 @@ proc waku_get_my_enr(
return RET_OK

proc waku_start_discv5(
ctx: ptr Context, callback: WakuCallBack, userData: pointer
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
ctx[].userData = userData
checkLibwakuParams(ctx, callback, userData)

let resp = waku_thread.sendRequestToWakuThread(
ctx, RequestType.DISCOVERY, DiscoveryRequest.createDiscV5StartRequest()
Expand All @@ -651,9 +642,9 @@ proc waku_start_discv5(
return RET_OK

proc waku_stop_discv5(
ctx: ptr Context, callback: WakuCallBack, userData: pointer
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} =
ctx[].userData = userData
checkLibwakuParams(ctx, callback, userData)

let resp = waku_thread.sendRequestToWakuThread(
ctx, RequestType.DISCOVERY, DiscoveryRequest.createDiscV5StopRequest()
Expand Down
Loading

0 comments on commit 88e3755

Please sign in to comment.