mirror of https://github.com/waku-org/nwaku.git
feat: running validators in /relay/v1/auto/messages/{topic} (#2394)
This commit is contained in:
parent
55d1b078b1
commit
e4e147bcbb
|
@ -528,3 +528,47 @@ suite "Waku v2 Rest API - Relay":
|
|||
await restServer.stop()
|
||||
await restServer.closeWait()
|
||||
await node.stop()
|
||||
|
||||
asyncTest "Post a message larger than maximum size - POST /relay/v1/auto/messages/{topic}":
|
||||
# Given
|
||||
let node = testWakuNode()
|
||||
await node.start()
|
||||
await node.mountRelay()
|
||||
await node.mountRlnRelay(WakuRlnConfig(rlnRelayDynamic: false,
|
||||
rlnRelayCredIndex: some(1.uint),
|
||||
rlnRelayTreePath: genTempPath("rln_tree", "wakunode_1")))
|
||||
|
||||
# RPC server setup
|
||||
var restPort = Port(0)
|
||||
let restAddress = parseIpAddress("0.0.0.0")
|
||||
let restServer = RestServerRef.init(restAddress, restPort).tryGet()
|
||||
|
||||
restPort = restServer.server.address.port # update with bound port for client use
|
||||
|
||||
let cache = MessageCache.init()
|
||||
|
||||
installRelayApiHandlers(restServer.router, node, cache)
|
||||
restServer.start()
|
||||
|
||||
let client = newRestHttpClient(initTAddress(restAddress, restPort))
|
||||
|
||||
node.subscribe((kind: PubsubSub, topic: DefaultPubsubTopic))
|
||||
require:
|
||||
toSeq(node.wakuRelay.subscribedTopics).len == 1
|
||||
|
||||
# When
|
||||
let response = await client.relayPostAutoMessagesV1(RelayWakuMessage(
|
||||
payload: base64.encode(getByteSequence(MaxWakuMessageSize)), # Message will be bigger than the max size
|
||||
contentTopic: some(DefaultContentTopic),
|
||||
timestamp: some(int64(2022))
|
||||
))
|
||||
|
||||
# Then
|
||||
check:
|
||||
response.status == 400
|
||||
$response.contentType == $MIMETYPE_TEXT
|
||||
response.data == fmt"Failed to publish: Message size exceeded maximum of {DefaultMaxWakuMessageSizeStr}"
|
||||
|
||||
await restServer.stop()
|
||||
await restServer.closeWait()
|
||||
await node.stop()
|
|
@ -212,24 +212,24 @@ proc installRelayApiHandlers*(router: var RestRouter, node: WakuNode, cache: Mes
|
|||
var message: WakuMessage = req.toWakuMessage(version = 0).valueOr:
|
||||
return RestApiResponse.badRequest()
|
||||
|
||||
let pubsubTopic = getShard(message.contentTopic).valueOr:
|
||||
let msg = "Autosharding error: " & error
|
||||
error "publish error", msg=msg
|
||||
return RestApiResponse.badRequest("Failed to publish. " & msg)
|
||||
|
||||
# if RLN is mounted, append the proof to the message
|
||||
if not node.wakuRlnRelay.isNil():
|
||||
if not node.wakuRlnRelay.appendRLNProof(message, float64(getTime().toUnix())):
|
||||
return RestApiResponse.internalServerError(
|
||||
"Failed to publish: error appending RLN proof to message")
|
||||
|
||||
case node.wakuRlnRelay.validateMessage(message):
|
||||
of MessageValidationResult.Invalid:
|
||||
return RestApiResponse.internalServerError("Failed to publish: invalid RLN proof")
|
||||
of MessageValidationResult.Spam:
|
||||
return RestApiResponse.badRequest("Failed to publish: limit exceeded, try again later")
|
||||
of MessageValidationResult.Valid:
|
||||
debug "RLN proof validated successfully", contentTopic=message.contentTopic
|
||||
(await node.wakuRelay.validateMessage(pubsubTopic, message)).isOkOr:
|
||||
return RestApiResponse.badRequest("Failed to publish: " & error)
|
||||
|
||||
# if we reach here its either a non-RLN message or a RLN message with a valid proof
|
||||
debug "Publishing message", contentTopic=message.contentTopic, rln=not node.wakuRlnRelay.isNil()
|
||||
|
||||
var publishFut = node.publish(none(PubSubTopic), message)
|
||||
var publishFut = node.publish(some(pubsubTopic), message)
|
||||
if not await publishFut.withTimeout(futTimeout):
|
||||
return RestApiResponse.internalServerError("Failed to publish: timedout")
|
||||
|
||||
|
|
Loading…
Reference in New Issue