2024-06-28 16:04:57 +05:30
|
|
|
{.push raises: [].}
|
2024-04-26 12:42:47 +02:00
|
|
|
|
|
|
|
|
## Notice that the REST /lightpush requests normally assume that the node
|
|
|
|
|
## is acting as a lightpush-client that will trigger the service provider node
|
|
|
|
|
## to relay the message.
|
|
|
|
|
## In this module, we allow that a lightpush service node (full node) can be
|
|
|
|
|
## triggered directly through the REST /lightpush endpoint.
|
|
|
|
|
## The typical use case for that is when using `nwaku-compose`,
|
|
|
|
|
## which spawn a full service Waku node
|
|
|
|
|
## that could be used also as a lightpush client, helping testing and development.
|
|
|
|
|
|
2025-01-08 20:52:44 +01:00
|
|
|
import results, chronos, chronicles, std/options, metrics, stew/byteutils
|
2024-04-26 12:42:47 +02:00
|
|
|
import
|
|
|
|
|
../waku_core,
|
|
|
|
|
./protocol,
|
|
|
|
|
./common,
|
|
|
|
|
./rpc,
|
|
|
|
|
./rpc_codec,
|
|
|
|
|
./protocol_metrics,
|
|
|
|
|
../utils/requests
|
|
|
|
|
|
|
|
|
|
proc handleSelfLightPushRequest*(
|
2025-03-05 12:07:56 +01:00
|
|
|
self: WakuLightPush, pubSubTopic: Option[PubsubTopic], message: WakuMessage
|
|
|
|
|
): Future[WakuLightPushResult] {.async.} =
|
2024-04-26 12:42:47 +02:00
|
|
|
## Handles the lightpush requests made by the node to itself.
|
|
|
|
|
## Normally used in REST-lightpush requests
|
2025-01-08 20:52:44 +01:00
|
|
|
## On success, returns the msg_hash of the published message.
|
2024-04-26 12:42:47 +02:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# provide self peerId as now this node is used directly, thus there is no light client sender peer.
|
|
|
|
|
let selfPeerId = self.peerManager.switch.peerInfo.peerId
|
|
|
|
|
|
2025-03-05 12:07:56 +01:00
|
|
|
let req = LightpushRequest(
|
|
|
|
|
requestId: generateRequestId(self.rng), pubSubTopic: pubSubTopic, message: message
|
|
|
|
|
)
|
2024-04-26 12:42:47 +02:00
|
|
|
|
2025-03-05 12:07:56 +01:00
|
|
|
let response = await self.handleRequest(selfPeerId, req.encode().buffer)
|
2024-04-26 12:42:47 +02:00
|
|
|
|
2025-03-05 12:07:56 +01:00
|
|
|
return response.toPushResult()
|
2024-04-26 12:42:47 +02:00
|
|
|
except Exception:
|
2025-03-05 12:07:56 +01:00
|
|
|
return lightPushResultInternalError(
|
|
|
|
|
"exception in handleSelfLightPushRequest: " & getCurrentExceptionMsg()
|
|
|
|
|
)
|