-
Notifications
You must be signed in to change notification settings - Fork 61
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
feat: running validators in REST API #2373
Conversation
You can find the image built from this PR at
Built from 2f820b7 |
c71585e
to
8cf79cd
Compare
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.
👍 This approach seams sensible. No strong opinion either way. Maybe WakuMessage
s could check their own size?
(await node.wakuRelay.validateMessage(pubsubTopic, message)).isOkOr: | ||
return RestApiResponse.badRequest("Failed to publish: " & error) |
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.
Notice that for RLN's case for example, the validator's inside logic is used directly in the handler and therefore we had more possible error messages to return the user.
Once we use our validators directly, which only return if validation passed or not, we can only have one possible error message per validator.
The specific details still get logged, but one con of this approach vs hardcoding the checks in the handlers is that we can only have one unique error message per validator
Makes sense! Thought about it and found 2 issues to this. First one, is that we don't use a Apart from that, the easiest way to calculate the size in bytes of a LMK what you all think :)) |
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.
Thanks for the PR!
I've added a few comments. If possible, I'd try to simplify the approach and just have one kind of validator (avoiding the use of the generic/default validators list) but ofc I might be missing something :)
What I was thinking is something like
Plus some tests. |
Oooh got it! So basically having the check inside a proc. That's cool, 100% can be useful and nicer :) |
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.
Overall thank you for this great stuff. I agree with the comments added before me, but I can approve it from my side, bet you will answer the rest. ;-)
d82b19c
to
298fe4c
Compare
@gabrielmer you are aware of #1700 right? |
Ooooh, was definitely not aware of that GitHub issue! I was aware of the overall idea, that's why I took this approach here with validators instead of hardcoding the checks. Thanks! |
Changing PR to draft as there's still many changes to be done |
b919235
to
df60564
Compare
return ValidationResult.Accept | ||
return wrappedValidator | ||
|
||
proc isValidSize(message: WakuMessage): Future[Result[void, string]] {.async.} = |
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.
I'll also add to this proc a check that the message size is smaller than GossipSub's max.
Currently the max size is defined in nim-libp2p
here: https://github.com/status-im/nim-libp2p/blob/e3c967ad1939fb33b8e13759037d193734acd202/libp2p/protocols/pubsub/pubsub.nim#L556
I don't want to have it hardcoded, so I'll first open a PR in nim-libp2p
defining it as a global const
and will then add an if
condition in this proc
I think I implemented all the feedback received, please let me know if I missed something or if there's anything else we can improve :) So this PR doesn't get way too big, I'll implement the same validation logic in a separate PR for lightpush's and autosharding's REST handlers. |
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.
LGTM thanks!
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.
Looks great indeed! However, I can't approve yet as per the doubt I have re rest/relay/handlers.nim
:)
@@ -135,16 +135,8 @@ proc installRelayApiHandlers*(router: var RestRouter, node: WakuNode, cache: Mes | |||
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.validateMessageAndUpdateLog(message) |
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.
Why are we removing this wakuRlnRelay
validation? This is quite important :) Or are we making this validation in another place?
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.
Yes!
So now we register the validator here:
Line 990 in 6171f31
node.wakuRelay.addValidator(validator, "RLN validation failed") |
And this runs in the REST handler here:
nwaku/waku/waku_api/rest/relay/handlers.nim
Line 138 in 6171f31
(await node.wakuRelay.validateMessage(pubsubTopic, message)).isOkOr: |
So instead of hardcoding again the RLN validation in the handler, this gets run when going over all the registered validators
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.
Beautiful!, I see that the validation is performed in the following place:
nwaku/waku/waku_rln_relay/rln_relay.nim
Line 331 in 3e65cc1
let validationRes = wakuRlnRelay.validateMessageAndUpdateLog(message) |
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.
LGTM! Thanks for the master-piece PR and the patience! 🥳
@@ -135,16 +135,8 @@ proc installRelayApiHandlers*(router: var RestRouter, node: WakuNode, cache: Mes | |||
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.validateMessageAndUpdateLog(message) |
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.
Beautiful!, I see that the validation is performed in the following place:
nwaku/waku/waku_rln_relay/rln_relay.nim
Line 331 in 3e65cc1
let validationRes = wakuRlnRelay.validateMessageAndUpdateLog(message) |
6171f31
to
f08420f
Compare
Description
We currently add message validators to libp2p's gossipsub, which are run before sending a message. In this PR, we're running the validators in POST
/relay/v1/messages/{pubsubTopic}
REST handler in order to catch earlier any wrong message and be able to return the user an error in case it's invalid and the message is not published.In addition to that, a check for messages bigger than the maximum configured size was added to the handler. That's also a check currently made at the libp2p layer before relaying the message, and we want to catch these messages earlier in the flow.
A big change of this PR is that we got rid of topic-specific validators. Now the same validators will run for every pubsub topic.
After completing this PR, a new PR will be opened implementing the same validation logic for other autosharding and lightpush's REST handlers.
Changes
/relay/v1/messages/{pubsubTopic}
REST handlerIssue
Part of #2284