Skip to content

Commit

Permalink
feat: helper function to check pending block filter (#130)
Browse files Browse the repository at this point in the history
* add helper function to determine a filter configured to match pending block

* chore: delete temp variables, improve docs of FilteredParams::is_pending_block_filter

* make clippy happy

---------

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
allnil and mattsse authored Jan 18, 2024
1 parent aaf5006 commit 61142b3
Showing 1 changed file with 75 additions and 0 deletions.
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 {
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());
}
}

0 comments on commit 61142b3

Please sign in to comment.