-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: Enabling to use a full node for lightpush via rest api without lightpush client configured #2626
Merged
NagyZoltanPeter
merged 3 commits into
master
from
chore-make-rest-lightpush-avail-on-service-node
Apr 26, 2024
Merged
chore: Enabling to use a full node for lightpush via rest api without lightpush client configured #2626
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -16,6 +16,7 @@ import | |||||||||
../../waku/node/peer_manager, | ||||||||||
../../../waku_node, | ||||||||||
../../waku/waku_lightpush/common, | ||||||||||
../../waku/waku_lightpush/self_req_handler, | ||||||||||
../../handlers, | ||||||||||
../serdes, | ||||||||||
../responses, | ||||||||||
|
@@ -35,6 +36,9 @@ const NoPeerNoDiscoError = | |||||||||
const NoPeerNoneFoundError = | ||||||||||
RestApiResponse.serviceUnavailable("No suitable service peer & none discovered") | ||||||||||
|
||||||||||
proc useSelfHostedLightPush(node: WakuNode): bool = | ||||||||||
return node.wakuLightPush != nil and node.wakuLightPushClient == nil | ||||||||||
|
||||||||||
#### Request handlers | ||||||||||
|
||||||||||
const ROUTE_LIGHTPUSH* = "/lightpush/v1/message" | ||||||||||
|
@@ -60,15 +64,19 @@ proc installLightPushRequestHandler*( | |||||||||
let msg = req.message.toWakuMessage().valueOr: | ||||||||||
return RestApiResponse.badRequest("Invalid message: " & $error) | ||||||||||
|
||||||||||
let peer = node.peerManager.selectPeer(WakuLightPushCodec).valueOr: | ||||||||||
let handler = discHandler.valueOr: | ||||||||||
return NoPeerNoDiscoError | ||||||||||
var peer = RemotePeerInfo.init($node.switch.peerInfo.peerId) | ||||||||||
if useSelfHostedLightPush(node): | ||||||||||
discard | ||||||||||
else: | ||||||||||
Comment on lines
+68
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
peer = node.peerManager.selectPeer(WakuLightPushCodec).valueOr: | ||||||||||
let handler = discHandler.valueOr: | ||||||||||
return NoPeerNoDiscoError | ||||||||||
|
||||||||||
let peerOp = (await handler()).valueOr: | ||||||||||
return RestApiResponse.internalServerError("No value in peerOp: " & $error) | ||||||||||
let peerOp = (await handler()).valueOr: | ||||||||||
return RestApiResponse.internalServerError("No value in peerOp: " & $error) | ||||||||||
|
||||||||||
peerOp.valueOr: | ||||||||||
return NoPeerNoneFoundError | ||||||||||
peerOp.valueOr: | ||||||||||
return NoPeerNoneFoundError | ||||||||||
|
||||||||||
let subFut = node.lightpushPublish(req.pubsubTopic, msg, peer) | ||||||||||
|
||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
when (NimMajor, NimMinor) < (1, 4): | ||
{.push raises: [Defect].} | ||
else: | ||
{.push raises: [].} | ||
|
||
## 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. | ||
|
||
import stew/results, chronos, chronicles, std/options, metrics | ||
import | ||
../waku_core, | ||
./protocol, | ||
./common, | ||
./rpc, | ||
./rpc_codec, | ||
./protocol_metrics, | ||
../utils/requests | ||
|
||
proc handleSelfLightPushRequest*( | ||
self: WakuLightPush, pubSubTopic: PubsubTopic, message: WakuMessage | ||
): Future[WakuLightPushResult[void]] {.async.} = | ||
## Handles the lightpush requests made by the node to itself. | ||
## Normally used in REST-lightpush requests | ||
|
||
try: | ||
NagyZoltanPeter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# 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 | ||
|
||
let req = PushRequest(pubSubTopic: pubSubTopic, message: message) | ||
let rpc = PushRPC(requestId: generateRequestId(self.rng), request: some(req)) | ||
|
||
let respRpc = await self.handleRequest(selfPeerId, rpc.encode().buffer) | ||
|
||
if respRpc.response.isNone(): | ||
waku_lightpush_errors.inc(labelValues = [emptyResponseBodyFailure]) | ||
return err(emptyResponseBodyFailure) | ||
|
||
let response = respRpc.response.get() | ||
NagyZoltanPeter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if not response.isSuccess: | ||
if response.info.isSome(): | ||
return err(response.info.get()) | ||
else: | ||
return err("unknown failure") | ||
|
||
return ok() | ||
except Exception: | ||
return err("exception in handleSelfLightPushRequest: " & getCurrentExceptionMsg()) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe rephrase to
local
vsremote
lightpush ?