Skip to content
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

add range test in FilterBlockOption #939

Merged
merged 4 commits into from
Jun 25, 2024

Conversation

tcoratger
Copy link
Contributor

Summary

Currently there are no checks in the with_from_block and with_to_block methods to ensure the validity of the given block numbers and thus avoid false ranges.

This pull request adds unit tests for the FilterBlockOption struct, specifically focusing on the with_from_block and with_to_block methods. These tests ensure that the methods correctly handle various scenarios, including valid and invalid block ranges.

Changes

FilterBlockOption Methods

  • with_from_block: Added a check to ensure that the from_block value is not greater than the to_block value. If this condition is violated, the method will panic with an appropriate error message.
  • with_to_block: Added a check to ensure that the to_block value is not less than the from_block value. If this condition is violated, the method will panic with an appropriate error message.

Unit Tests

Unit tests are added to verify the behavior of the with_from_block and with_to_block methods.

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced that panicking is the right call here.

perhaps we can solve this with a separate function fn ensure_valid_block_range(self) -> Result instead?
this way it's up to the caller to include this check or not

Comment on lines 218 to 226
// Check if from_block is greater than to_block
if self
.get_to_block()
.and_then(|to| to.as_number())
.zip(block.as_number())
.map_or(false, |(to, from)| from > to)
{
panic!("from_block cannot be greater than to_block");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, we should not panic here.

worst thing that can happen when sending an invalid range is an rpc error as response

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes true, didn't think about this properly

@tcoratger
Copy link
Contributor Author

I'm not convinced that panicking is the right call here.

perhaps we can solve this with a separate function fn ensure_valid_block_range(self) -> Result instead? this way it's up to the caller to include this check or not

@mattsse I suggest something else, tell me what you think.

I renamed the methods that existed before with an _unchecked suffix so the user knows explicitly that nothing will be checked for them.

I created two new methods without the suffix for cases where the user wants there to be checking.

I know that this breaks the previous API but the user will notice it because the return type is not the same.

@@ -213,34 +222,44 @@ impl FilterBlockOption {
}

/// Sets the block number this range filter should start at.
#[must_use]
pub fn with_from_block(&self, block: BlockNumberOrTag) -> Self {
pub fn with_from_block(&self, block: BlockNumberOrTag) -> Result<Self, FilterBlockError> {
Copy link
Member

@mattsse mattsse Jun 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is very inconvenient, we should not do this.

I'm fine with an additional ensure_ function that does this check, but this should not be part of the builder functions because not useful

is there a particular use case that requires this check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just fixed that, let me know :)

I tried, apparently you didn't like it haha

In fact, it was while testing our RPC that I realized that it seemed much too easy to produce erroneous block ranges and then spend several minutes before understanding what was wrong.

For example in our case we filter with the MongoDB Rust client and therefore obviously if I filter the blocks from 15 to 2 it doesn't work but I had to do a lot of prints before understanding that I was doing that :)

So I thought that an unchecked method (for the more confident) and a checked method (for the more cautious) could be a good idea. But let's do this with an ensure_ method that at least allows us to have the option of adding risk insurance.

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry forgot about this one,

one smol thing missing

crates/rpc-types-eth/src/filter.rs Outdated Show resolved Hide resolved
crates/rpc-types-eth/src/filter.rs Outdated Show resolved Hide resolved
@mattsse mattsse merged commit 67881cb into alloy-rs:main Jun 25, 2024
22 checks passed
ben186 pushed a commit to ben186/alloy that referenced this pull request Jul 27, 2024
* add range test in FilterBlockOption

* fix comments

* fix comments

* fix comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants