chore: libwaku reduce repetitive code by adding a template handling resp returns (#3032)

This commit is contained in:
Ivan FB 2024-09-11 18:11:59 +02:00 committed by GitHub
parent 3ccb6cdf61
commit 1713f56235
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 73 additions and 149 deletions

View File

@ -52,6 +52,25 @@ template foreignThreadGc(body: untyped) =
when declared(tearDownForeignThreadGc): when declared(tearDownForeignThreadGc):
tearDownForeignThreadGc() tearDownForeignThreadGc()
template handleRes[T: string | void](
res: Result[T, string], callback: WakuCallBack, userData: pointer
) =
## Handles the Result responses, which can either be Result[string, string] or
## Result[void, string]. Notice that in case of Result[void, string], it is enough to
## just return RET_OK and not provide any additional feedback through the callback.
if res.isErr():
foreignThreadGc:
let msg = "libwaku error: " & $res.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
when T is string:
let msg = $res.get()
if msg.len > 0:
foreignThreadGc:
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_OK
proc relayEventCallback(ctx: ptr WakuContext): WakuRelayHandler = proc relayEventCallback(ctx: ptr WakuContext): WakuRelayHandler =
return proc( return proc(
pubsubTopic: PubsubTopic, msg: WakuMessage pubsubTopic: PubsubTopic, msg: WakuMessage
@ -148,13 +167,7 @@ proc waku_destroy(
): cint {.dynlib, exportc.} = ): cint {.dynlib, exportc.} =
checkLibwakuParams(ctx, callback, userData) checkLibwakuParams(ctx, callback, userData)
waku_thread.stopWakuThread(ctx).isOkOr: waku_thread.stopWakuThread(ctx).handleRes(callback, userData)
foreignThreadGc:
let msg = $error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
return RET_OK
proc waku_version( proc waku_version(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
@ -270,6 +283,8 @@ proc waku_relay_publish(
return RET_ERR return RET_ERR
let pst = pubSubTopic.alloc() let pst = pubSubTopic.alloc()
defer:
deallocShared(pst)
let targetPubSubTopic = let targetPubSubTopic =
if len(pst) == 0: if len(pst) == 0:
@ -277,7 +292,8 @@ proc waku_relay_publish(
else: else:
$pst $pst
let sendReqRes = waku_thread.sendRequestToWakuThread( waku_thread
.sendRequestToWakuThread(
ctx, ctx,
RequestType.RELAY, RequestType.RELAY,
RelayRequest.createShared( RelayRequest.createShared(
@ -287,16 +303,7 @@ proc waku_relay_publish(
wakuMessage, wakuMessage,
), ),
) )
deallocShared(pst) .handleRes(callback, userData)
if sendReqRes.isErr():
let msg = $sendReqRes.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
let msgHash = $sendReqRes.value
callback(RET_OK, unsafeAddr msgHash[0], cast[csize_t](len(msgHash)), userData)
return RET_OK
proc waku_start( proc waku_start(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
@ -330,22 +337,19 @@ proc waku_relay_subscribe(
checkLibwakuParams(ctx, callback, userData) checkLibwakuParams(ctx, callback, userData)
let pst = pubSubTopic.alloc() let pst = pubSubTopic.alloc()
defer:
deallocShared(pst)
var cb = relayEventCallback(ctx) var cb = relayEventCallback(ctx)
let sendReqRes = waku_thread.sendRequestToWakuThread(
waku_thread
.sendRequestToWakuThread(
ctx, ctx,
RequestType.RELAY, RequestType.RELAY,
RelayRequest.createShared( RelayRequest.createShared(
RelayMsgType.SUBSCRIBE, PubsubTopic($pst), WakuRelayHandler(cb) RelayMsgType.SUBSCRIBE, PubsubTopic($pst), WakuRelayHandler(cb)
), ),
) )
deallocShared(pst) .handleRes(callback, userData)
if sendReqRes.isErr():
let msg = $sendReqRes.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
return RET_OK
proc waku_relay_unsubscribe( proc waku_relay_unsubscribe(
ctx: ptr WakuContext, ctx: ptr WakuContext,
@ -356,8 +360,11 @@ proc waku_relay_unsubscribe(
checkLibwakuParams(ctx, callback, userData) checkLibwakuParams(ctx, callback, userData)
let pst = pubSubTopic.alloc() let pst = pubSubTopic.alloc()
defer:
deallocShared(pst)
let sendReqRes = waku_thread.sendRequestToWakuThread( waku_thread
.sendRequestToWakuThread(
ctx, ctx,
RequestType.RELAY, RequestType.RELAY,
RelayRequest.createShared( RelayRequest.createShared(
@ -366,14 +373,7 @@ proc waku_relay_unsubscribe(
WakuRelayHandler(relayEventCallback(ctx)), WakuRelayHandler(relayEventCallback(ctx)),
), ),
) )
deallocShared(pst) .handleRes(callback, userData)
if sendReqRes.isErr():
let msg = $sendReqRes.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
return RET_OK
proc waku_relay_get_num_connected_peers( proc waku_relay_get_num_connected_peers(
ctx: ptr WakuContext, ctx: ptr WakuContext,
@ -387,25 +387,13 @@ proc waku_relay_get_num_connected_peers(
defer: defer:
deallocShared(pst) deallocShared(pst)
let numConnPeersRes = waku_thread.sendRequestToWakuThread( waku_thread
.sendRequestToWakuThread(
ctx, ctx,
RequestType.RELAY, RequestType.RELAY,
RelayRequest.createShared(RelayMsgType.LIST_CONNECTED_PEERS, PubsubTopic($pst)), RelayRequest.createShared(RelayMsgType.LIST_CONNECTED_PEERS, PubsubTopic($pst)),
) )
.handleRes(callback, userData)
if numConnPeersRes.isErr():
foreignThreadGc:
let msg = "Error in waku_relay_get_num_connected_peers: " & $numConnPeersRes.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
let numConnPeers = numConnPeersRes.get()
foreignThreadGc:
callback(
RET_OK, unsafeAddr numConnPeers[0], cast[csize_t](len(numConnPeers)), userData
)
return RET_OK
proc waku_relay_get_num_peers_in_mesh( proc waku_relay_get_num_peers_in_mesh(
ctx: ptr WakuContext, ctx: ptr WakuContext,
@ -419,25 +407,13 @@ proc waku_relay_get_num_peers_in_mesh(
defer: defer:
deallocShared(pst) deallocShared(pst)
let numPeersInMeshRes = waku_thread.sendRequestToWakuThread( waku_thread
.sendRequestToWakuThread(
ctx, ctx,
RequestType.RELAY, RequestType.RELAY,
RelayRequest.createShared(RelayMsgType.LIST_MESH_PEERS, PubsubTopic($pst)), RelayRequest.createShared(RelayMsgType.LIST_MESH_PEERS, PubsubTopic($pst)),
) )
.handleRes(callback, userData)
if numPeersInMeshRes.isErr():
foreignThreadGc:
let msg = "Error in waku_relay_get_num_peers_in_mesh: " & $numPeersInMeshRes.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
let numPeersInMesh = numPeersInMeshRes.get()
foreignThreadGc:
callback(
RET_OK, unsafeAddr numPeersInMesh[0], cast[csize_t](len(numPeersInMesh)), userData
)
return RET_OK
proc waku_lightpush_publish( proc waku_lightpush_publish(
ctx: ptr WakuContext, ctx: ptr WakuContext,
@ -474,22 +450,15 @@ proc waku_lightpush_publish(
else: else:
$pst $pst
let sendReqRes = waku_thread.sendRequestToWakuThread( waku_thread
.sendRequestToWakuThread(
ctx, ctx,
RequestType.LIGHTPUSH, RequestType.LIGHTPUSH,
LightpushRequest.createShared( LightpushRequest.createShared(
LightpushMsgType.PUBLISH, PubsubTopic($pst), wakuMessage LightpushMsgType.PUBLISH, PubsubTopic($pst), wakuMessage
), ),
) )
.handleRes(callback, userData)
if sendReqRes.isErr():
let msg = $sendReqRes.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
let msg = $sendReqRes.value
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_OK
proc waku_connect( proc waku_connect(
ctx: ptr WakuContext, ctx: ptr WakuContext,
@ -500,19 +469,15 @@ proc waku_connect(
): cint {.dynlib, exportc.} = ): cint {.dynlib, exportc.} =
checkLibwakuParams(ctx, callback, userData) checkLibwakuParams(ctx, callback, userData)
let connRes = waku_thread.sendRequestToWakuThread( waku_thread
.sendRequestToWakuThread(
ctx, ctx,
RequestType.PEER_MANAGER, RequestType.PEER_MANAGER,
PeerManagementRequest.createShared( PeerManagementRequest.createShared(
PeerManagementMsgType.CONNECT_TO, $peerMultiAddr, chronos.milliseconds(timeoutMs) PeerManagementMsgType.CONNECT_TO, $peerMultiAddr, chronos.milliseconds(timeoutMs)
), ),
) )
if connRes.isErr(): .handleRes(callback, userData)
let msg = $connRes.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
return RET_OK
proc waku_get_peerids_from_peerstore( proc waku_get_peerids_from_peerstore(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
@ -564,39 +529,26 @@ proc waku_store_query(
): cint {.dynlib, exportc.} = ): cint {.dynlib, exportc.} =
checkLibwakuParams(ctx, callback, userData) checkLibwakuParams(ctx, callback, userData)
let sendReqRes = waku_thread.sendRequestToWakuThread( waku_thread
.sendRequestToWakuThread(
ctx, ctx,
RequestType.STORE, RequestType.STORE,
JsonStoreQueryRequest.createShared(jsonQuery, peerAddr, timeoutMs), JsonStoreQueryRequest.createShared(jsonQuery, peerAddr, timeoutMs),
) )
.handleRes(callback, userData)
if sendReqRes.isErr():
let msg = $sendReqRes.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
let msg = $sendReqRes.value
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_OK
proc waku_listen_addresses( proc waku_listen_addresses(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} = ): cint {.dynlib, exportc.} =
checkLibwakuParams(ctx, callback, userData) checkLibwakuParams(ctx, callback, userData)
let connRes = waku_thread.sendRequestToWakuThread( waku_thread
.sendRequestToWakuThread(
ctx, ctx,
RequestType.DEBUG, RequestType.DEBUG,
DebugNodeRequest.createShared(DebugNodeMsgType.RETRIEVE_LISTENING_ADDRESSES), DebugNodeRequest.createShared(DebugNodeMsgType.RETRIEVE_LISTENING_ADDRESSES),
) )
if connRes.isErr(): .handleRes(callback, userData)
let msg = $connRes.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
else:
let msg = $connRes.value
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_OK
proc waku_dns_discovery( proc waku_dns_discovery(
ctx: ptr WakuContext, ctx: ptr WakuContext,
@ -608,20 +560,15 @@ proc waku_dns_discovery(
): cint {.dynlib, exportc.} = ): cint {.dynlib, exportc.} =
checkLibwakuParams(ctx, callback, userData) checkLibwakuParams(ctx, callback, userData)
let bootstrapPeers = waku_thread.sendRequestToWakuThread( waku_thread
.sendRequestToWakuThread(
ctx, ctx,
RequestType.DISCOVERY, RequestType.DISCOVERY,
DiscoveryRequest.createRetrieveBootstrapNodesRequest( DiscoveryRequest.createRetrieveBootstrapNodesRequest(
DiscoveryMsgType.GET_BOOTSTRAP_NODES, entTreeUrl, nameDnsServer, timeoutMs DiscoveryMsgType.GET_BOOTSTRAP_NODES, entTreeUrl, nameDnsServer, timeoutMs
), ),
).valueOr: )
let msg = $error .handleRes(callback, userData)
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
let msg = $bootstrapPeers
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_OK
proc waku_discv5_update_bootnodes( proc waku_discv5_update_bootnodes(
ctx: ptr WakuContext, bootnodes: cstring, callback: WakuCallBack, userData: pointer ctx: ptr WakuContext, bootnodes: cstring, callback: WakuCallBack, userData: pointer
@ -630,73 +577,50 @@ proc waku_discv5_update_bootnodes(
## bootnodes - JSON array containing the bootnode ENRs i.e. `["enr:...", "enr:..."]` ## bootnodes - JSON array containing the bootnode ENRs i.e. `["enr:...", "enr:..."]`
checkLibwakuParams(ctx, callback, userData) checkLibwakuParams(ctx, callback, userData)
let resp = waku_thread.sendRequestToWakuThread( waku_thread
.sendRequestToWakuThread(
ctx, ctx,
RequestType.DISCOVERY, RequestType.DISCOVERY,
DiscoveryRequest.createUpdateBootstrapNodesRequest( DiscoveryRequest.createUpdateBootstrapNodesRequest(
DiscoveryMsgType.UPDATE_DISCV5_BOOTSTRAP_NODES, bootnodes DiscoveryMsgType.UPDATE_DISCV5_BOOTSTRAP_NODES, bootnodes
), ),
).valueOr: )
let msg = $error .handleRes(callback, userData)
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
let msg = $resp
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_OK
proc waku_get_my_enr( proc waku_get_my_enr(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} = ): cint {.dynlib, exportc.} =
checkLibwakuParams(ctx, callback, userData) checkLibwakuParams(ctx, callback, userData)
let connRes = waku_thread.sendRequestToWakuThread( waku_thread
.sendRequestToWakuThread(
ctx, ctx,
RequestType.DEBUG, RequestType.DEBUG,
DebugNodeRequest.createShared(DebugNodeMsgType.RETRIEVE_MY_ENR), DebugNodeRequest.createShared(DebugNodeMsgType.RETRIEVE_MY_ENR),
) )
if connRes.isErr(): .handleRes(callback, userData)
let msg = $connRes.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
else:
let msg = $connRes.value
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_OK
proc waku_start_discv5( proc waku_start_discv5(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} = ): cint {.dynlib, exportc.} =
checkLibwakuParams(ctx, callback, userData) checkLibwakuParams(ctx, callback, userData)
let resp = waku_thread.sendRequestToWakuThread( waku_thread
.sendRequestToWakuThread(
ctx, RequestType.DISCOVERY, DiscoveryRequest.createDiscV5StartRequest() ctx, RequestType.DISCOVERY, DiscoveryRequest.createDiscV5StartRequest()
).valueOr: )
let msg = $error .handleRes(callback, userData)
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
let msg = $resp
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_OK
proc waku_stop_discv5( proc waku_stop_discv5(
ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer ctx: ptr WakuContext, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} = ): cint {.dynlib, exportc.} =
checkLibwakuParams(ctx, callback, userData) checkLibwakuParams(ctx, callback, userData)
let resp = waku_thread.sendRequestToWakuThread( waku_thread
.sendRequestToWakuThread(
ctx, RequestType.DISCOVERY, DiscoveryRequest.createDiscV5StopRequest() ctx, RequestType.DISCOVERY, DiscoveryRequest.createDiscV5StopRequest()
).valueOr: )
let msg = $error .handleRes(callback, userData)
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
let msg = $resp
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_OK
proc waku_peer_exchange_request( proc waku_peer_exchange_request(
ctx: ptr WakuContext, numPeers: uint64, callback: WakuCallBack, userData: pointer ctx: ptr WakuContext, numPeers: uint64, callback: WakuCallBack, userData: pointer