Skip to content

Conversation

@prestwich
Copy link
Member

Motivation

Filtering is a bit wonky and crufty, presumably because the logic was ported from elsewhere.

Solution

  • remove FilteredParams, as it was a dead struct providing functionality that should just be on the filter
  • memoize blooms for FilterSet when "std" is enabled
  • delete the BloomFilter type, use a SINGLE bloom filter per FilterSet instead of Vec<Bloom>. This appears to have been resource over-use. Blooms are designed to be accrued
  • manually impl Default as the derived impl has a T: Default bound that is unnecessary
  • make FilterSet inner HashSet private, to avoid invalidating memoized blooms
  • expose basic mutators for the FilterSet

drive-by

  • add a bunch of missing [cfg(feature = "serde")] in tests
  • fix up some docs

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

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.

some qs, hesitant to touch any of the filter logic -.-

}
#[cfg(feature = "std")]
#[cfg_attr(feature = "serde", serde(skip, default))]
bloom: std::sync::OnceLock<Bloom>,
Copy link
Member

Choose a reason for hiding this comment

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

I think we can use once_cell here and get rid of the std features like this once_cell should be already part of the dep graph anyway

Copy link
Member Author

Choose a reason for hiding this comment

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

once_cell's no_std support is a little wonky. do you use it with race or critical section?

}

/// Returns true if the filter matches the given block number
pub fn filter_block_range(&self, block_number: u64) -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

what's the equivalent of this now?

because we need this on the server side

Copy link
Member Author

Choose a reason for hiding this comment

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

  • matches_block_range for numbers
  • matches_block_hash for hashes
  • matches_block for numhashes

Comment on lines -803 to -847
/// Support for matching [Filter]s
#[derive(Debug, Default)]
pub struct FilteredParams {
/// The original filter, if any
pub filter: Option<Filter>,
}
Copy link
Member

Choose a reason for hiding this comment

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

what's the right way to handle an optional filter now?

Copy link
Member Author

@prestwich prestwich Mar 4, 2025

Choose a reason for hiding this comment

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

an empty Filter via Filter::default() should have the same behavior

Copy link
Member Author

Choose a reason for hiding this comment

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

another easy approach would be filter_opt.map(|f| f.matches(thing)).unwrap_or(true)

@mattsse mattsse requested a review from yash-atreya as a code owner March 26, 2025 13:37
@prestwich
Copy link
Member Author

bump

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.

this is def better, only have 1 serde nit

dealing with filters and logs makes me feel dizzy, and I dread touching any of this https://github.com/paradigmxyz/reth/blob/f24570844545b55f2293a3a3c475133a3f738b68/crates/rpc/rpc/src/eth/filter.rs#L523-L525

so I'd need to prep a reth pr first just to make sure we're not missing anything

Comment on lines 699 to 700
let Some(number) = log.block_number else { return false };
let Some(hash) = log.block_hash else { return false };
Copy link
Member

Choose a reason for hiding this comment

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

this will never match for pending logs,
should this behave like an OR

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed by adding matches_log_block

Comment on lines 20 to 22
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct FilterSet<T: Eq + Hash> {
set: HashSet<T>,
Copy link
Member

Choose a reason for hiding this comment

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

I believe this deserde impl no longer behaves like before, but unsure where this is even used, but we should treat this as serde transparent

Copy link
Member Author

Choose a reason for hiding this comment

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

serde(from = "HashSet<T>") should cover this, ya?

@github-project-automation github-project-automation bot moved this to In Progress in Alloy May 5, 2025
@mattsse
Copy link
Member

mattsse commented May 6, 2025

@prestwich I haven't started migrating paradigmxyz/reth#16077 but maybe this is actually quite easy

@prestwich
Copy link
Member Author

it should be a straightforward migration. I can look more later today if you'd like

@mattsse
Copy link
Member

mattsse commented May 6, 2025

that'd be much appreciated

@prestwich prestwich force-pushed the prestwich/filter-improvements branch from 5f20830 to 427cdee Compare May 6, 2025 14:41
@github-project-automation github-project-automation bot moved this from In Progress to Reviewed in Alloy May 12, 2025
@mattsse mattsse merged commit 4ce6d88 into main May 12, 2025
54 checks passed
@mattsse mattsse deleted the prestwich/filter-improvements branch May 12, 2025 08:14
@github-project-automation github-project-automation bot moved this from Reviewed to Done in Alloy May 12, 2025
@mattsse mattsse mentioned this pull request May 20, 2025
jgomez360 pushed a commit to jgomez360/alloy that referenced this pull request Jun 15, 2025
* refactor: improve and simplify event filters

* docs: minor fixes

* feat: mutable filter access with unsealing

* fix: cfg_attr

* touchup

* fix: test

* fix: preserve deserialize behavior

* fix: matches_log_block and type of rpc_matches_parsed

* fix: don't disregard address bloom

---------

Co-authored-by: Matthias Seitz <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants