fix: code review

This commit is contained in:
Richard Ramos 2024-12-17 15:25:23 -04:00
parent 38729bc2c8
commit c6f47f8aeb
No known key found for this signature in database
GPG Key ID: 1CE87DB518195760
3 changed files with 25 additions and 17 deletions

View File

@ -84,9 +84,10 @@ int waku_relay_subscribe(void* ctx,
WakuCallBack callback, WakuCallBack callback,
void* userData); void* userData);
int waku_relay_add_protected_topic(void* ctx, int waku_relay_add_protected_shard(void* ctx,
const char* pubSubTopic, int clusterId,
const char* publicKey, int shardId,
char* publicKey,
WakuCallBack callback, WakuCallBack callback,
void* userData); void* userData);

View File

@ -360,20 +360,16 @@ proc waku_relay_subscribe(
userData, userData,
) )
proc waku_relay_add_protected_topic( proc waku_relay_add_protected_shard(
ctx: ptr WakuContext, ctx: ptr WakuContext,
pubSubTopic: cstring, clusterId: cint,
shardId: cint,
publicKey: cstring, publicKey: cstring,
callback: WakuCallBack, callback: WakuCallBack,
userData: pointer, userData: pointer,
): cint {.dynlib, exportc, cdecl.} = ): cint {.dynlib, exportc, cdecl.} =
initializeLibrary() initializeLibrary()
checkLibwakuParams(ctx, callback, userData) checkLibwakuParams(ctx, callback, userData)
let pst = pubSubTopic.alloc()
defer:
deallocShared(pst)
let pubk = publicKey.alloc() let pubk = publicKey.alloc()
defer: defer:
deallocShared(pubk) deallocShared(pubk)
@ -382,7 +378,10 @@ proc waku_relay_add_protected_topic(
ctx, ctx,
RequestType.RELAY, RequestType.RELAY,
RelayRequest.createShared( RelayRequest.createShared(
RelayMsgType.ADD_PROTECTED_TOPIC, PubsubTopic($pst), publicKey = $pubk RelayMsgType.ADD_PROTECTED_SHARD,
clusterId = clusterId,
shardId = shardId,
publicKey = $pubk,
), ),
callback, callback,
userData, userData,

View File

@ -17,7 +17,7 @@ type RelayMsgType* = enum
## to return the list of all connected peers to an specific pubsub topic ## to return the list of all connected peers to an specific pubsub topic
LIST_MESH_PEERS LIST_MESH_PEERS
## to return the list of only the peers that conform the mesh for a particular pubsub topic ## to return the list of only the peers that conform the mesh for a particular pubsub topic
ADD_PROTECTED_TOPIC ## Protects a pubsub topic with a public key ADD_PROTECTED_SHARD ## Protects a shard with a public key
type ThreadSafeWakuMessage* = object type ThreadSafeWakuMessage* = object
payload: SharedSeq[byte] payload: SharedSeq[byte]
@ -34,19 +34,25 @@ type RelayRequest* = object
pubsubTopic: cstring pubsubTopic: cstring
relayEventCallback: WakuRelayHandler # not used in 'PUBLISH' requests relayEventCallback: WakuRelayHandler # not used in 'PUBLISH' requests
message: ThreadSafeWakuMessage # only used in 'PUBLISH' requests message: ThreadSafeWakuMessage # only used in 'PUBLISH' requests
publicKey: cstring # only used in 'ADD_PROTECTED_TOPIC' requests clusterId: cint # only used in 'ADD_PROTECTED_SHARD' requests
shardId: cint # only used in 'ADD_PROTECTED_SHARD' requests
publicKey: cstring # only used in 'ADD_PROTECTED_SHARD' requests
proc createShared*( proc createShared*(
T: type RelayRequest, T: type RelayRequest,
op: RelayMsgType, op: RelayMsgType,
pubsubTopic: PubsubTopic, pubsubTopic: PubsubTopic = "",
relayEventCallback: WakuRelayHandler = nil, relayEventCallback: WakuRelayHandler = nil,
m = WakuMessage(), m = WakuMessage(),
clusterId: cint = 0,
shardId: cint = 0,
publicKey: string = "", publicKey: string = "",
): ptr type T = ): ptr type T =
var ret = createShared(T) var ret = createShared(T)
ret[].operation = op ret[].operation = op
ret[].pubsubTopic = pubsubTopic.alloc() ret[].pubsubTopic = pubsubTopic.alloc()
ret[].clusterId = clusterId
ret[].shardId = shardId
ret[].publicKey = publicKey.alloc() ret[].publicKey = publicKey.alloc()
ret[].relayEventCallback = relayEventCallback ret[].relayEventCallback = relayEventCallback
ret[].message = ThreadSafeWakuMessage( ret[].message = ThreadSafeWakuMessage(
@ -125,12 +131,14 @@ proc process*(
error "LIST_MESH_PEERS failed", error = error error "LIST_MESH_PEERS failed", error = error
return err($error) return err($error)
return ok($numPeersInMesh) return ok($numPeersInMesh)
of ADD_PROTECTED_TOPIC: of ADD_PROTECTED_SHARD:
try: try:
let relayShard =
RelayShard(clusterId: uint16(self.clusterId), shardId: uint16(self.shardId))
let protectedShard = let protectedShard =
ProtectedShard.parseCmdArg($self.pubsubTopic & ":" & $self.publicKey) ProtectedShard.parseCmdArg($relayShard & ":" & $self.publicKey)
waku.node.wakuRelay.addSignedShardsValidator( waku.node.wakuRelay.addSignedShardsValidator(
@[protectedShard], uint16(waku.node.wakuMetadata.clusterId) @[protectedShard], uint16(self.clusterId)
) )
except ValueError: except ValueError:
return err(getCurrentExceptionMsg()) return err(getCurrentExceptionMsg())