Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alrevuelta committed Aug 31, 2023
1 parent 4d64ed0 commit f5ba118
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
24 changes: 21 additions & 3 deletions tests/wakunode_jsonrpc/test_jsonrpc_relay.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{.used.}

import
std/[options, sequtils],
std/[options, sequtils, tempfiles],
stew/shims/net as stewNet,
testutils/unittests,
chronicles,
Expand All @@ -21,6 +21,10 @@ import
../testlib/wakucore,
../testlib/wakunode

when defined(rln):
import
../../../waku/waku_rln_relay


proc newTestMessageCache(): relay_api.MessageCache =
relay_api.MessageCache.init(capacity=30)
Expand Down Expand Up @@ -100,6 +104,15 @@ suite "Waku v2 JSON-RPC API - Relay":
await srcNode.mountRelay(@[pubSubTopic])
await dstNode.mountRelay(@[pubSubTopic])

when defined(rln):
await srcNode.mountRlnRelay(WakuRlnConfig(rlnRelayDynamic: false,
rlnRelayCredIndex: 1,
rlnRelayTreePath: genTempPath("rln_tree", "wakunode_1")))

await dstNode.mountRlnRelay(WakuRlnConfig(rlnRelayDynamic: false,
rlnRelayCredIndex: 2,
rlnRelayTreePath: genTempPath("rln_tree", "wakunode_2")))

await srcNode.connectToNodes(@[dstNode.peerInfo.toRemotePeerInfo()])


Expand Down Expand Up @@ -139,7 +152,12 @@ suite "Waku v2 JSON-RPC API - Relay":
response == true
await dstHandlerFut.withTimeout(chronos.seconds(5))

let (topic, msg) = dstHandlerFut.read()
var (topic, msg) = dstHandlerFut.read()

# proof is injected under the hood, we compare just the message
when defined(rln):
msg.proof = @[]

check:
topic == pubSubTopic
msg == message
Expand Down Expand Up @@ -171,7 +189,7 @@ suite "Waku v2 JSON-RPC API - Relay":

# RPC server (destination node)
let
rpcPort = Port(8548)
rpcPort = Port(8549)
ta = initTAddress(ValidIpAddress.init("0.0.0.0"), rpcPort)
server = newRpcHttpServer([ta])

Expand Down
9 changes: 8 additions & 1 deletion tests/wakunode_rest/test_rest_relay.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{.used.}

import
std/sequtils,
std/[sequtils,tempfiles],
stew/byteutils,
stew/shims/net,
testutils/unittests,
Expand All @@ -22,6 +22,9 @@ import
../testlib/wakucore,
../testlib/wakunode

when defined(rln):
import
../../../waku/waku_rln_relay

proc testWakuNode(): WakuNode =
let
Expand Down Expand Up @@ -183,6 +186,10 @@ suite "Waku v2 Rest API - Relay":
let node = testWakuNode()
await node.start()
await node.mountRelay()
when defined(rln):
await node.mountRlnRelay(WakuRlnConfig(rlnRelayDynamic: false,
rlnRelayCredIndex: 1,
rlnRelayTreePath: genTempPath("rln_tree", "wakunode_1")))

# RPC server setup
let restPort = Port(58014)
Expand Down
13 changes: 11 additions & 2 deletions waku/node/jsonrpc/relay/handlers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,37 @@ proc installRelayApiHandlers*(node: WakuNode, server: RpcServer, cache: MessageC
if pubsubTopic notin node.wakuRelay.subscribedTopics():
raise newException(ValueError, "Failed to publish: Node not subscribed to pubsubTopic: " & pubsubTopic)

# if RLN is mounted, append the proof to the message
when defined(rln):
if not node.wakuRlnRelay.isNil():
# append the proof to the message
let success = node.wakuRlnRelay.appendRLNProof(message,
float64(getTime().toUnix()))
if not success:
raise newException(ValueError, "Failed to publish: error appending RLN proof to message")

# validate the message before sending it
let result = node.wakuRlnRelay.validateMessage(message)
if result == MessageValidationResult.Invalid:
raise newException(ValueError, "Failed to publish: invalid RLN proof")
elif result == MessageValidationResult.Spam:
raise newException(ValueError, "Failed to publish: limit exceeded, try again later")
elif result == MessageValidationResult.Valid:
debug "Publishing message WITH RLN proof", pubSubTopic=pubSubTopic
let publishFut = node.publish(pubsubTopic, message)

if not await publishFut.withTimeout(futTimeout):
raise newException(ValueError, "Failed to publish: timed out")
else:
raise newException(ValueError, "Failed to publish: unknown RLN proof validation result")
else:
raise newException(ValueError, "Failed to publish: RLN enabled but not mounted")

# if RLN is not mounted, publish the message as is
else:
debug "Publishing message WITHOUT RLN proof", pubSubTopic=pubSubTopic
let publishFut = node.publish(pubsubTopic, message)

if not await publishFut.withTimeout(futTimeout):
raise newException(ValueError, "Failed to publish: timed out")

return true

Expand Down
16 changes: 11 additions & 5 deletions waku/node/rest/relay/handlers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ proc installRelayPostMessagesV1Handler*(router: var RestRouter, node: WakuNode)

var message = resMessage.get()

# if RLN is mounted, append the proof to the message
when defined(rln):
if not node.wakuRlnRelay.isNil():
# append the proof to the message
Expand All @@ -175,16 +176,21 @@ proc installRelayPostMessagesV1Handler*(router: var RestRouter, node: WakuNode)
elif result == MessageValidationResult.Spam:
return RestApiResponse.badRequest("Failed to publish: limit exceeded, try again later")
elif result == MessageValidationResult.Valid:
debug "Publishing message WITH RLN proof", pubSubTopic=pubSubTopic
let publishFut = node.publish(pubSubTopic, message)

if not await publishFut.withTimeout(futTimeout):
return RestApiResponse.internalServerError("Failed to publish: timed out")
else:
return RestApiResponse.internalServerError("Failed to publish: unknown RLN proof validation result")

if not (waitFor node.publish(pubSubTopic, resMessage.value).withTimeout(futTimeout)):
error "Failed to publish message to topic", topic=pubSubTopic
return RestApiResponse.internalServerError("Failed to publish: timedout")
else:
return RestApiResponse.internalServerError("Failed to publish: RLN enabled but not mounted")

# if RLN is not enabled, just publish the message
else:
debug "Publishing message WITHOUT RLN proof", pubSubTopic=pubSubTopic
if not (waitFor node.publish(pubSubTopic, resMessage.value).withTimeout(futTimeout)):
error "Failed to publish message to topic", pubSubTopic=pubSubTopic
return RestApiResponse.internalServerError("Failed to publish: timedout")

return RestApiResponse.ok()

Expand Down

0 comments on commit f5ba118

Please sign in to comment.