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

feat: helper function to check pending block filter #130

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions crates/rpc-types/src/eth/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,13 @@ impl Filter {
self
}

/// Return `true` if filter configured to match pending block.
/// This means that both from_block and to_block are set to the pending tag.
pub fn is_pending_block_filter(&self) -> bool {
self.block_option.get_from_block().map_or(false, BlockNumberOrTag::is_pending)
&& self.block_option.get_to_block().map_or(false, BlockNumberOrTag::is_pending)
}

/// Pins the block hash for the filter
#[must_use]
pub fn at_block_hash<T: Into<B256>>(mut self, hash: T) -> Self {
Expand Down Expand Up @@ -798,6 +805,13 @@ impl FilteredParams {
true
}

/// Return `true` if the filter configured to match pending block.
/// This means that both from_block and to_block are set to the pending tag.
/// It calls [`Filter::is_pending_block_filter`] undercover.
pub fn is_pending_block_filter(&self) -> bool {
allnil marked this conversation as resolved.
Show resolved Hide resolved
self.filter.as_ref().map_or(false, |f| f.is_pending_block_filter())
}

/// Returns `true` if the filter matches the given log.
pub fn filter_address(&self, log: &Log) -> bool {
self.filter.as_ref().map(|f| f.address.matches(&log.address)).unwrap_or(true)
Expand Down Expand Up @@ -1359,4 +1373,65 @@ mod tests {
}
);
}

#[test]
fn test_is_pending_block_filter() {
let filter = Filter {
block_option: FilterBlockOption::Range {
from_block: Some(BlockNumberOrTag::Pending),
to_block: Some(BlockNumberOrTag::Pending),
},
address: "0xb59f67a8bff5d8cd03f6ac17265c550ed8f33907"
.parse::<Address>()
.unwrap()
.into(),
topics: [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
.parse::<B256>()
.unwrap()
.into(),
"0x00000000000000000000000000b46c2526e227482e2ebb8f4c69e4674d262e75"
.parse::<B256>()
.unwrap()
.into(),
"0x00000000000000000000000054a2d42a40f51259dedd1978f6c118a0f0eff078"
.parse::<B256>()
.unwrap()
.into(),
Default::default(),
],
};
assert!(filter.is_pending_block_filter());
let filter_params = FilteredParams::new(Some(filter));
assert!(filter_params.is_pending_block_filter());

let filter = Filter {
block_option: FilterBlockOption::Range {
from_block: Some(4365627u64.into()),
to_block: Some(4365627u64.into()),
},
address: "0xb59f67a8bff5d8cd03f6ac17265c550ed8f33907"
.parse::<Address>()
.unwrap()
.into(),
topics: [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
.parse::<B256>()
.unwrap()
.into(),
"0x00000000000000000000000000b46c2526e227482e2ebb8f4c69e4674d262e75"
.parse::<B256>()
.unwrap()
.into(),
"0x00000000000000000000000054a2d42a40f51259dedd1978f6c118a0f0eff078"
.parse::<B256>()
.unwrap()
.into(),
Default::default(),
],
};
assert!(!filter.is_pending_block_filter());
let filter_params = FilteredParams::new(Some(filter));
assert!(!filter_params.is_pending_block_filter());
}
}