-
Notifications
You must be signed in to change notification settings - Fork 85
feat(code): limit the number of values in a sync response based on upper limit #1171
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6699835
prevent sending a total of values that exceed the max response size
insumity 73d2397
fix formatting issues
insumity 4fed2a7
Merge branch 'main' into insumity/add_response_size_check
romac 19fc510
Cleanup
romac 2ef4ad3
Small refactor using new `update_request` state method
romac cc83928
Add `max_parallel_requests` state method
romac 7160ec4
Re-request range from any peer, not necessarily the same one
romac 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -311,9 +311,36 @@ where | |
| "Received response from different peer than expected" | ||
|
Contributor
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. We should probably use this PR to abort processing the response in the case where we got a response from another peer than the one we expected. |
||
| ); | ||
| } | ||
|
|
||
| let range_len = requested_range.end().as_u64() - requested_range.start().as_u64() + 1; | ||
| if (response.values.len() as u64) < range_len { | ||
| re_request_values_from_peer_except(co, state, metrics, request_id, None).await?; | ||
|
|
||
| if response.values.len() < range_len as usize { | ||
| // NOTE: We cannot simply call `re_request_values_from_peer_except` here. | ||
| // Although we received some values from the peer, these values have not yet been processed | ||
| // by the consensus engine. If we called `re_request_values_from_peer_except`, we would | ||
| // end up re-requesting the entire original range (including values we already received), | ||
| // causing the syncing peer to repeatedly send multiple requests until the already-received | ||
| // values are fully processed. | ||
| // To tackle this, we first update the current pending request with the range of values | ||
| // it provides we received, and then issue a new request with the remaining values. | ||
| let new_start = requested_range | ||
| .start() | ||
| .increment_by(response.values.len() as u64); | ||
|
|
||
| let end = *requested_range.end(); | ||
|
|
||
| if response.values.is_empty() { | ||
| error!(%request_id, %peer_id, "Received response contains no values"); | ||
| } else { | ||
| // The response of this request only provided `response.values.len()` values, | ||
| // so update the pending request accordingly | ||
| let updated_range = *requested_range.start()..=new_start.decrement().unwrap(); | ||
| state.update_request(request_id, peer_id, updated_range); | ||
| } | ||
|
|
||
| // Issue a new request to any peer, not necessarily the same one, for the remaining values | ||
| let new_range = new_start..=end; | ||
| request_values_range(co, state, metrics, new_range).await?; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -360,7 +387,7 @@ where | |
| // Validate response from host | ||
| let batch_size = end.as_u64() - start.as_u64() + 1; | ||
| if batch_size != values.len() as u64 { | ||
| error!( | ||
| warn!( | ||
| "Received {} values from host, expected {batch_size}", | ||
| values.len() | ||
| ) | ||
|
|
@@ -483,7 +510,7 @@ async fn request_values<Ctx>( | |
| where | ||
| Ctx: Context, | ||
| { | ||
| let max_parallel_requests = max(1, state.config.parallel_requests); | ||
| let max_parallel_requests = state.max_parallel_requests(); | ||
|
|
||
| if state.pending_requests.len() as u64 >= max_parallel_requests { | ||
| info!( | ||
|
|
@@ -515,6 +542,41 @@ where | |
| Ok(()) | ||
| } | ||
|
|
||
| /// Request multiple batches of values in parallel. | ||
| async fn request_values_range<Ctx>( | ||
| co: Co<Ctx>, | ||
| state: &mut State<Ctx>, | ||
| metrics: &Metrics, | ||
| range: RangeInclusive<Ctx::Height>, | ||
| ) -> Result<(), Error<Ctx>> | ||
| where | ||
| Ctx: Context, | ||
| { | ||
| let max_parallel_requests = state.max_parallel_requests(); | ||
|
|
||
| if state.pending_requests.len() as u64 >= max_parallel_requests { | ||
| info!( | ||
| %max_parallel_requests, | ||
| pending_requests = %state.pending_requests.len(), | ||
| range = %DisplayRange::<Ctx>(&range), | ||
| "Maximum number of parallel requests reached, skipping request for values" | ||
| ); | ||
|
|
||
| return Ok(()); | ||
| }; | ||
|
|
||
| // Get a random peer that can provide the values in the range. | ||
| let Some((peer, range)) = state.random_peer_with(&range) else { | ||
| // No connected peer reached this height yet, we can stop syncing here. | ||
| debug!(range = %DisplayRange::<Ctx>(&range), "No peer to request sync from"); | ||
| return Ok(()); | ||
| }; | ||
|
|
||
| request_values_from_peer(&co, state, metrics, range, peer).await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| async fn request_values_from_peer<Ctx>( | ||
| co: &Co<Ctx>, | ||
| state: &mut State<Ctx>, | ||
|
|
||
This file contains hidden or 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 hidden or 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
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.
I would declare these variables close to the declarations of the address and signature types. Also, each app will have its own definition of address and signature.
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.
Not sure I fully follow your comment: Are you suggesting contacting the app to receive the sizes of addresses and signatures? If not, where exactly would you declare those constants?
Uh oh!
There was an error while loading. Please reload this page.
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.
We could add them as static const to the
AddressandSigningSchemetraits.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.
But that's pretty brittle as well, and could break things if the upper bound is not tight enough, eg. some application uses a signature scheme with variable-size signatures and defines
Signature::MAX_SIZEto a large value just in case.Uh oh!
There was an error while loading. Please reload this page.
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.
The best way would probably be to encode the commit certificate (or even the full sync response) using the
(Network)Codecand use the actual computed size. Not great for performance, we should probably either measure the impact upfront, or go with it and only optimize this if we see too big a perf impact. What do you think?Uh oh!
There was an error while loading. Please reload this page.
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 agree this is the right away to go about it. I'll rewrite the PR to take this into account.