2022-11-04 09:52:27 +00:00
|
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
else:
|
|
|
|
|
{.push raises: [].}
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
|
|
import
|
2023-02-13 14:22:24 +00:00
|
|
|
|
std/sequtils,
|
2022-06-10 11:30:51 +00:00
|
|
|
|
stew/byteutils,
|
|
|
|
|
chronicles,
|
|
|
|
|
json_serialization,
|
|
|
|
|
json_serialization/std/options,
|
2023-02-13 14:22:24 +00:00
|
|
|
|
presto/route,
|
|
|
|
|
presto/common
|
|
|
|
|
import
|
2023-09-22 13:36:46 +00:00
|
|
|
|
../../../waku_node,
|
2023-09-01 13:03:59 +00:00
|
|
|
|
../../../waku_relay/protocol,
|
2023-09-11 06:32:31 +00:00
|
|
|
|
../../../waku_rln_relay,
|
2023-09-26 11:33:52 +00:00
|
|
|
|
../../../node/waku_node,
|
|
|
|
|
../../message_cache,
|
|
|
|
|
../../cache_handlers,
|
2022-08-29 14:54:11 +00:00
|
|
|
|
../serdes,
|
2023-02-13 14:22:24 +00:00
|
|
|
|
../responses,
|
2023-09-26 11:33:52 +00:00
|
|
|
|
./types
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
2023-09-01 13:03:59 +00:00
|
|
|
|
from std/times import getTime
|
|
|
|
|
from std/times import toUnix
|
|
|
|
|
|
|
|
|
|
|
2023-02-13 14:22:24 +00:00
|
|
|
|
export types
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logScope:
|
2022-11-03 15:36:24 +00:00
|
|
|
|
topics = "waku node rest relay_api"
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##### Topic cache
|
|
|
|
|
|
|
|
|
|
const futTimeout* = 5.seconds # Max time to wait for futures
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### Request handlers
|
|
|
|
|
|
|
|
|
|
const ROUTE_RELAY_SUBSCRIPTIONSV1* = "/relay/v1/subscriptions"
|
2023-09-26 19:53:46 +00:00
|
|
|
|
const ROUTE_RELAY_MESSAGESV1* = "/relay/v1/messages/{pubsubTopic}"
|
2023-09-26 11:33:52 +00:00
|
|
|
|
const ROUTE_RELAY_AUTO_SUBSCRIPTIONSV1* = "/relay/v1/auto/subscriptions"
|
2023-09-26 19:53:46 +00:00
|
|
|
|
const ROUTE_RELAY_AUTO_MESSAGESV1* = "/relay/v1/auto/messages/{contentTopic}"
|
|
|
|
|
const ROUTE_RELAY_AUTO_MESSAGESV1_NO_TOPIC* = "/relay/v1/auto/messages"
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
2023-09-26 11:33:52 +00:00
|
|
|
|
proc installRelayApiHandlers*(router: var RestRouter, node: WakuNode, cache: MessageCache[string]) =
|
2022-06-10 11:30:51 +00:00
|
|
|
|
router.api(MethodPost, ROUTE_RELAY_SUBSCRIPTIONSV1) do (contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
|
|
|
# ## Subscribes a node to a list of PubSub topics
|
|
|
|
|
# debug "post_waku_v2_relay_v1_subscriptions"
|
|
|
|
|
|
|
|
|
|
# Check the request body
|
|
|
|
|
if contentBody.isNone():
|
|
|
|
|
return RestApiResponse.badRequest()
|
2023-02-13 14:22:24 +00:00
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
|
let reqBodyContentType = MediaType.init($contentBody.get().contentType)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
if reqBodyContentType != MIMETYPE_JSON:
|
|
|
|
|
return RestApiResponse.badRequest()
|
|
|
|
|
|
|
|
|
|
let reqBodyData = contentBody.get().data
|
2023-09-26 19:53:46 +00:00
|
|
|
|
let reqResult = decodeFromJsonBytes(seq[PubsubTopic], reqBodyData)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
if reqResult.isErr():
|
|
|
|
|
return RestApiResponse.badRequest()
|
|
|
|
|
|
2023-09-26 19:53:46 +00:00
|
|
|
|
let req: seq[PubsubTopic] = reqResult.get()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
2023-08-23 13:53:17 +00:00
|
|
|
|
# Only subscribe to topics for which we have no subscribed topic handlers yet
|
|
|
|
|
let newTopics = req.filterIt(not cache.isSubscribed(it))
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
2023-09-26 11:33:52 +00:00
|
|
|
|
for pubsubTopic in newTopics:
|
|
|
|
|
cache.subscribe(pubsubTopic)
|
|
|
|
|
node.subscribe((kind: PubsubSub, topic: pubsubTopic), some(messageCacheHandler(cache)))
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
|
|
return RestApiResponse.ok()
|
|
|
|
|
|
|
|
|
|
router.api(MethodDelete, ROUTE_RELAY_SUBSCRIPTIONSV1) do (contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
|
|
|
# ## Subscribes a node to a list of PubSub topics
|
|
|
|
|
# debug "delete_waku_v2_relay_v1_subscriptions"
|
|
|
|
|
|
|
|
|
|
# Check the request body
|
|
|
|
|
if contentBody.isNone():
|
|
|
|
|
return RestApiResponse.badRequest()
|
2023-02-13 14:22:24 +00:00
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
|
let reqBodyContentType = MediaType.init($contentBody.get().contentType)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
if reqBodyContentType != MIMETYPE_JSON:
|
|
|
|
|
return RestApiResponse.badRequest()
|
|
|
|
|
|
|
|
|
|
let reqBodyData = contentBody.get().data
|
2023-09-26 19:53:46 +00:00
|
|
|
|
let reqResult = decodeFromJsonBytes(seq[PubsubTopic], reqBodyData)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
if reqResult.isErr():
|
|
|
|
|
return RestApiResponse.badRequest()
|
|
|
|
|
|
2023-09-26 19:53:46 +00:00
|
|
|
|
let req: seq[PubsubTopic] = reqResult.get()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
|
|
# Unsubscribe all handlers from requested topics
|
2023-09-26 11:33:52 +00:00
|
|
|
|
for pubsubTopic in req:
|
|
|
|
|
node.unsubscribe((kind: PubsubUnsub, topic: pubsubTopic))
|
|
|
|
|
cache.unsubscribe(pubsubTopic)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
|
|
# Successfully unsubscribed from all requested topics
|
2023-02-13 14:22:24 +00:00
|
|
|
|
return RestApiResponse.ok()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
2023-09-26 19:53:46 +00:00
|
|
|
|
router.api(MethodGet, ROUTE_RELAY_MESSAGESV1) do (pubsubTopic: string) -> RestApiResponse:
|
2022-06-10 11:30:51 +00:00
|
|
|
|
# ## Returns all WakuMessages received on a PubSub topic since the
|
|
|
|
|
# ## last time this method was called
|
|
|
|
|
# ## TODO: ability to specify a return message limit
|
|
|
|
|
# debug "get_waku_v2_relay_v1_messages", topic=topic
|
|
|
|
|
|
2023-09-26 19:53:46 +00:00
|
|
|
|
if pubsubTopic.isErr():
|
2022-06-10 11:30:51 +00:00
|
|
|
|
return RestApiResponse.badRequest()
|
2023-09-26 19:53:46 +00:00
|
|
|
|
let pubSubTopic = pubsubTopic.get()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
2023-02-13 14:22:24 +00:00
|
|
|
|
let messages = cache.getMessages(pubSubTopic, clear=true)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
if messages.isErr():
|
|
|
|
|
debug "Not subscribed to topic", topic=pubSubTopic
|
2023-02-13 14:22:24 +00:00
|
|
|
|
return RestApiResponse.notFound()
|
|
|
|
|
|
2022-06-10 11:30:51 +00:00
|
|
|
|
let data = RelayGetMessagesResponse(messages.get().map(toRelayWakuMessage))
|
|
|
|
|
let resp = RestApiResponse.jsonResponse(data, status=Http200)
|
|
|
|
|
if resp.isErr():
|
2023-02-13 14:22:24 +00:00
|
|
|
|
debug "An error ocurred while building the json respose", error=resp.error
|
2022-06-10 11:30:51 +00:00
|
|
|
|
return RestApiResponse.internalServerError()
|
|
|
|
|
|
|
|
|
|
return resp.get()
|
2023-02-13 14:22:24 +00:00
|
|
|
|
|
2023-09-26 19:53:46 +00:00
|
|
|
|
router.api(MethodPost, ROUTE_RELAY_MESSAGESV1) do (pubsubTopic: string, contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
|
|
|
if pubsubTopic.isErr():
|
2022-06-10 11:30:51 +00:00
|
|
|
|
return RestApiResponse.badRequest()
|
2023-09-26 19:53:46 +00:00
|
|
|
|
let pubSubTopic = pubsubTopic.get()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
2023-09-01 13:03:59 +00:00
|
|
|
|
# ensure the node is subscribed to the topic. otherwise it risks publishing
|
|
|
|
|
# to a topic with no connected peers
|
|
|
|
|
if pubSubTopic notin node.wakuRelay.subscribedTopics():
|
|
|
|
|
return RestApiResponse.badRequest("Failed to publish: Node not subscribed to topic: " & pubsubTopic)
|
|
|
|
|
|
2022-06-10 11:30:51 +00:00
|
|
|
|
# Check the request body
|
|
|
|
|
if contentBody.isNone():
|
|
|
|
|
return RestApiResponse.badRequest()
|
2023-02-13 14:22:24 +00:00
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
|
let reqBodyContentType = MediaType.init($contentBody.get().contentType)
|
2022-06-10 11:30:51 +00:00
|
|
|
|
if reqBodyContentType != MIMETYPE_JSON:
|
|
|
|
|
return RestApiResponse.badRequest()
|
|
|
|
|
|
|
|
|
|
let reqBodyData = contentBody.get().data
|
|
|
|
|
let reqResult = decodeFromJsonBytes(RelayPostMessagesRequest, reqBodyData)
|
|
|
|
|
if reqResult.isErr():
|
|
|
|
|
return RestApiResponse.badRequest()
|
|
|
|
|
|
2022-08-29 14:54:11 +00:00
|
|
|
|
let resMessage = reqResult.value.toWakuMessage(version = 0)
|
|
|
|
|
if resMessage.isErr():
|
|
|
|
|
return RestApiResponse.badRequest()
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
2023-09-01 13:03:59 +00:00
|
|
|
|
var message = resMessage.get()
|
|
|
|
|
|
|
|
|
|
# if RLN is mounted, append the proof to the message
|
2023-09-11 06:32:31 +00:00
|
|
|
|
if not node.wakuRlnRelay.isNil():
|
|
|
|
|
# append the proof to the message
|
|
|
|
|
let success = node.wakuRlnRelay.appendRLNProof(message,
|
|
|
|
|
float64(getTime().toUnix()))
|
|
|
|
|
if not success:
|
|
|
|
|
return RestApiResponse.internalServerError("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:
|
|
|
|
|
return RestApiResponse.internalServerError("Failed to publish: invalid RLN proof")
|
|
|
|
|
elif result == MessageValidationResult.Spam:
|
|
|
|
|
return RestApiResponse.badRequest("Failed to publish: limit exceeded, try again later")
|
|
|
|
|
elif result == MessageValidationResult.Valid:
|
|
|
|
|
debug "RLN proof validated successfully", pubSubTopic=pubSubTopic
|
2023-09-01 13:03:59 +00:00
|
|
|
|
else:
|
2023-09-11 06:32:31 +00:00
|
|
|
|
return RestApiResponse.internalServerError("Failed to publish: unknown RLN proof validation result")
|
2023-09-01 13:03:59 +00:00
|
|
|
|
|
|
|
|
|
# if we reach here its either a non-RLN message or a RLN message with a valid proof
|
2023-09-26 11:33:52 +00:00
|
|
|
|
debug "Publishing message", pubSubTopic=pubSubTopic, rln=defined(rln)
|
|
|
|
|
if not (waitFor node.publish(some(pubSubTopic), resMessage.value).withTimeout(futTimeout)):
|
2023-09-01 13:03:59 +00:00
|
|
|
|
error "Failed to publish message to topic", pubSubTopic=pubSubTopic
|
|
|
|
|
return RestApiResponse.internalServerError("Failed to publish: timedout")
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
|
|
|
|
return RestApiResponse.ok()
|
|
|
|
|
|
2023-09-26 11:33:52 +00:00
|
|
|
|
# Autosharding API
|
|
|
|
|
|
|
|
|
|
router.api(MethodPost, ROUTE_RELAY_AUTO_SUBSCRIPTIONSV1) do (contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
|
|
|
# ## Subscribes a node to a list of content topics
|
|
|
|
|
# debug "post_waku_v2_relay_v1_auto_subscriptions"
|
|
|
|
|
|
|
|
|
|
# Check the request body
|
|
|
|
|
if contentBody.isNone():
|
|
|
|
|
return RestApiResponse.badRequest()
|
|
|
|
|
|
|
|
|
|
let reqBodyContentType = MediaType.init($contentBody.get().contentType)
|
|
|
|
|
if reqBodyContentType != MIMETYPE_JSON:
|
|
|
|
|
return RestApiResponse.badRequest()
|
|
|
|
|
|
|
|
|
|
let reqBodyData = contentBody.get().data
|
2023-09-26 19:53:46 +00:00
|
|
|
|
let reqResult = decodeFromJsonBytes(seq[ContentTopic], reqBodyData)
|
2023-09-26 11:33:52 +00:00
|
|
|
|
if reqResult.isErr():
|
|
|
|
|
return RestApiResponse.badRequest()
|
|
|
|
|
|
2023-09-26 19:53:46 +00:00
|
|
|
|
let req: seq[ContentTopic] = reqResult.get()
|
2023-09-26 11:33:52 +00:00
|
|
|
|
|
|
|
|
|
# Only subscribe to topics for which we have no subscribed topic handlers yet
|
|
|
|
|
let newTopics = req.filterIt(not cache.isSubscribed(it))
|
|
|
|
|
|
|
|
|
|
for contentTopic in newTopics:
|
|
|
|
|
cache.subscribe(contentTopic)
|
|
|
|
|
node.subscribe((kind: ContentSub, topic: contentTopic), some(autoMessageCacheHandler(cache)))
|
|
|
|
|
|
|
|
|
|
return RestApiResponse.ok()
|
|
|
|
|
|
|
|
|
|
router.api(MethodDelete, ROUTE_RELAY_AUTO_SUBSCRIPTIONSV1) do (contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
|
|
|
# ## Subscribes a node to a list of content topics
|
|
|
|
|
# debug "delete_waku_v2_relay_v1_auto_subscriptions"
|
|
|
|
|
|
|
|
|
|
# Check the request body
|
|
|
|
|
if contentBody.isNone():
|
|
|
|
|
return RestApiResponse.badRequest()
|
|
|
|
|
|
|
|
|
|
let reqBodyContentType = MediaType.init($contentBody.get().contentType)
|
|
|
|
|
if reqBodyContentType != MIMETYPE_JSON:
|
|
|
|
|
return RestApiResponse.badRequest()
|
|
|
|
|
|
|
|
|
|
let reqBodyData = contentBody.get().data
|
2023-09-26 19:53:46 +00:00
|
|
|
|
let reqResult = decodeFromJsonBytes(seq[ContentTopic], reqBodyData)
|
2023-09-26 11:33:52 +00:00
|
|
|
|
if reqResult.isErr():
|
|
|
|
|
return RestApiResponse.badRequest()
|
|
|
|
|
|
2023-09-26 19:53:46 +00:00
|
|
|
|
let req: seq[ContentTopic] = reqResult.get()
|
2023-09-26 11:33:52 +00:00
|
|
|
|
|
|
|
|
|
# Unsubscribe all handlers from requested topics
|
|
|
|
|
for contentTopic in req:
|
|
|
|
|
cache.unsubscribe(contentTopic)
|
|
|
|
|
node.unsubscribe((kind: ContentUnsub, topic: contentTopic))
|
|
|
|
|
|
|
|
|
|
# Successfully unsubscribed from all requested topics
|
|
|
|
|
return RestApiResponse.ok()
|
|
|
|
|
|
2023-09-26 19:53:46 +00:00
|
|
|
|
router.api(MethodGet, ROUTE_RELAY_AUTO_MESSAGESV1) do (contentTopic: string) -> RestApiResponse:
|
2023-09-26 11:33:52 +00:00
|
|
|
|
# ## Returns all WakuMessages received on a content topic since the
|
|
|
|
|
# ## last time this method was called
|
|
|
|
|
# ## TODO: ability to specify a return message limit
|
|
|
|
|
# debug "get_waku_v2_relay_v1_auto_messages", topic=topic
|
|
|
|
|
|
2023-09-26 19:53:46 +00:00
|
|
|
|
if contentTopic.isErr():
|
2023-09-26 11:33:52 +00:00
|
|
|
|
return RestApiResponse.badRequest()
|
2023-09-26 19:53:46 +00:00
|
|
|
|
let contentTopic = contentTopic.get()
|
2023-09-26 11:33:52 +00:00
|
|
|
|
|
|
|
|
|
let messages = cache.getMessages(contentTopic, clear=true)
|
|
|
|
|
if messages.isErr():
|
|
|
|
|
debug "Not subscribed to topic", topic=contentTopic
|
|
|
|
|
return RestApiResponse.notFound()
|
|
|
|
|
|
|
|
|
|
let data = RelayGetMessagesResponse(messages.get().map(toRelayWakuMessage))
|
|
|
|
|
let resp = RestApiResponse.jsonResponse(data, status=Http200)
|
|
|
|
|
if resp.isErr():
|
|
|
|
|
debug "An error ocurred while building the json respose", error=resp.error
|
|
|
|
|
return RestApiResponse.internalServerError()
|
|
|
|
|
|
|
|
|
|
return resp.get()
|
|
|
|
|
|
2023-09-26 19:53:46 +00:00
|
|
|
|
router.api(MethodPost, ROUTE_RELAY_AUTO_MESSAGESV1_NO_TOPIC) do (contentBody: Option[ContentBody]) -> RestApiResponse:
|
2023-09-26 11:33:52 +00:00
|
|
|
|
# Check the request body
|
|
|
|
|
if contentBody.isNone():
|
|
|
|
|
return RestApiResponse.badRequest()
|
|
|
|
|
|
|
|
|
|
let reqBodyContentType = MediaType.init($contentBody.get().contentType)
|
|
|
|
|
if reqBodyContentType != MIMETYPE_JSON:
|
|
|
|
|
return RestApiResponse.badRequest()
|
|
|
|
|
|
|
|
|
|
let reqBodyData = contentBody.get().data
|
|
|
|
|
let reqResult = decodeFromJsonBytes(RelayPostMessagesRequest, reqBodyData)
|
|
|
|
|
if reqResult.isErr():
|
|
|
|
|
return RestApiResponse.badRequest()
|
|
|
|
|
|
|
|
|
|
if reqResult.value.contentTopic.isNone():
|
|
|
|
|
return RestApiResponse.badRequest()
|
|
|
|
|
|
|
|
|
|
let resMessage = reqResult.value.toWakuMessage(version = 0)
|
|
|
|
|
if resMessage.isErr():
|
|
|
|
|
return RestApiResponse.badRequest()
|
|
|
|
|
|
|
|
|
|
var message = resMessage.get()
|
|
|
|
|
|
|
|
|
|
# if RLN is mounted, append the proof to the message
|
|
|
|
|
if not node.wakuRlnRelay.isNil():
|
|
|
|
|
# append the proof to the message
|
|
|
|
|
let success = node.wakuRlnRelay.appendRLNProof(message,
|
|
|
|
|
float64(getTime().toUnix()))
|
|
|
|
|
if not success:
|
|
|
|
|
return RestApiResponse.internalServerError("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:
|
|
|
|
|
return RestApiResponse.internalServerError("Failed to publish: invalid RLN proof")
|
|
|
|
|
elif result == MessageValidationResult.Spam:
|
|
|
|
|
return RestApiResponse.badRequest("Failed to publish: limit exceeded, try again later")
|
|
|
|
|
elif result == MessageValidationResult.Valid:
|
|
|
|
|
debug "RLN proof validated successfully", contentTopic=message.contentTopic
|
|
|
|
|
else:
|
|
|
|
|
return RestApiResponse.internalServerError("Failed to publish: unknown RLN proof validation result")
|
|
|
|
|
|
|
|
|
|
# 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=defined(rln)
|
|
|
|
|
if not (waitFor node.publish(none(PubSubTopic), message).withTimeout(futTimeout)):
|
|
|
|
|
error "Failed to publish message to topic", contentTopic=message.contentTopic
|
|
|
|
|
return RestApiResponse.internalServerError("Failed to publish: timedout")
|
2022-06-10 11:30:51 +00:00
|
|
|
|
|
2023-09-26 11:33:52 +00:00
|
|
|
|
return RestApiResponse.ok()
|