Skip to content

Commit

Permalink
Simplify logic using a local enum
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipleblanc committed Nov 4, 2023
1 parent 1f99be3 commit 18ab2a9
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions crates/rpc/rpc-types/src/eth/trace/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,33 +63,32 @@ pub struct TraceFilterMatcher {
impl TraceFilterMatcher {
/// Returns `true` if the given `from` and `to` addresses match this filter.
pub fn matches(&self, from: Address, to: Option<Address>) -> bool {
// 1. If both `from_addresses` and `to_addresses` are empty, match everything.
if self.from_addresses.is_empty() && self.to_addresses.is_empty() {
return true;
}

let from_matches = self.from_addresses.contains(&from);
let to_matches = to.map_or(false, |to_addr| self.to_addresses.contains(&to_addr));

// 2. Use the filter mode to match if both `from_addresses` and `to_addresses` are defined.
if !self.from_addresses.is_empty() && !self.to_addresses.is_empty() {
return match self.mode {
TraceFilterMode::Union => from_matches || to_matches,
TraceFilterMode::Intersection => from_matches && to_matches,
};
enum Match {
All,
From,
To,
Both(TraceFilterMode),
}

// 3. Match only against `to_addresses` if `from_addresses` is empty.
if self.from_addresses.is_empty() {
return to_matches;
}

// 4. Match only against `from_addresses` if `to_addresses` is empty.
if self.to_addresses.is_empty() {
return from_matches;
let match_type = match (self.from_addresses.is_empty(), self.to_addresses.is_empty()) {
(true, true) => Match::All,
(false, true) => Match::From,
(true, false) => Match::To,
(false, false) => Match::Both(self.mode),
};

match match_type {
Match::All => true,
Match::From => from_matches,
Match::To => to_matches,
Match::Both(mode) => match mode {
TraceFilterMode::Union => from_matches || to_matches,
TraceFilterMode::Intersection => from_matches && to_matches,
},
}

unreachable!("Should not happen, all cases are covered above")
}
}

Expand Down

0 comments on commit 18ab2a9

Please sign in to comment.