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

fix(types/filter): treat empty filter address as non-matching #1473

Merged
merged 5 commits into from
Oct 15, 2024
Merged
Changes from 3 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
32 changes: 21 additions & 11 deletions crates/rpc-types-trace/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ impl From<Vec<Address>> for AddressFilter {
impl AddressFilter {
/// Returns `true` if the given address is in the filter or the filter address set is empty.
pub fn matches(&self, addr: &Address) -> bool {
self.matches_all() || self.0.contains(addr)
self.is_empty() || self.0.contains(addr)
}

/// Returns `true` if the address set is empty.
pub fn matches_all(&self) -> bool {
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
Expand Down Expand Up @@ -152,7 +152,7 @@ impl TraceFilterMatcher {
///
/// # Behavior
///
/// The function evaluates whether the `trace` matches based on its action type:
/// This function evaluates whether the `trace` matches based on its action type:
/// - `Call`: Matches if either the `from` or `to` addresses in the call action match the
/// filter's address criteria.
/// - `Create`: Matches if the `from` address in action matches, and the result's address (if
Expand All @@ -162,7 +162,9 @@ impl TraceFilterMatcher {
/// - `Reward`: Matches if the `author` address matches the filter's `to_addresses` criteria.
///
/// The overall result depends on the filter mode:
/// - `Union` mode: The trace matches if either the `from` or `to` address matches.
/// - `Union` mode: The trace matches if either the `from` or `to` address matches. If either
/// of the from or to address set is empty, the trace matches only if the other address
/// matches, and if both are empty, the filter matches all traces.
/// - `Intersection` mode: The trace matches only if both the `from` and `to` addresses match.
pub fn matches(&self, trace: &TransactionTrace) -> bool {
let (from_matches, to_matches) = match trace.action {
Expand All @@ -175,19 +177,27 @@ impl TraceFilterMatcher {
Some(TraceOutput::Create(CreateOutput { address: to, .. })) => {
self.to_addresses.matches(&to)
}
_ => self.to_addresses.matches_all(),
_ => self.to_addresses.is_empty(),
},
),
Action::Selfdestruct(SelfdestructAction { address, refund_address, .. }) => {
(self.from_addresses.matches(&address), self.to_addresses.matches(&refund_address))
}
Action::Reward(RewardAction { author, .. }) => {
(self.from_addresses.matches_all(), self.to_addresses.matches(&author))
(self.from_addresses.is_empty(), self.to_addresses.matches(&author))
}
};

match self.mode {
TraceFilterMode::Union => from_matches || to_matches,
TraceFilterMode::Union => {
if self.from_addresses.is_empty() {
to_matches
} else if self.to_addresses.is_empty() {
from_matches
} else {
from_matches || to_matches
}
}
jsvisa marked this conversation as resolved.
Show resolved Hide resolved
TraceFilterMode::Intersection => from_matches && to_matches,
}
}
Expand Down Expand Up @@ -310,7 +320,7 @@ mod tests {
};
assert!(m0.matches(&trace));
assert!(m1.matches(&trace));
assert!(m2.matches(&trace));
assert!(!m2.matches(&trace));
assert!(m3.matches(&trace));
assert!(m4.matches(&trace));
assert!(m5.matches(&trace));
Expand Down Expand Up @@ -354,7 +364,7 @@ mod tests {
};
assert!(m0.matches(&trace));
assert!(m1.matches(&trace));
assert!(m2.matches(&trace));
assert!(!m2.matches(&trace));
assert!(m3.matches(&trace));
assert!(m4.matches(&trace));
assert!(m5.matches(&trace));
Expand Down Expand Up @@ -389,8 +399,8 @@ mod tests {
..Default::default()
};
assert!(m0.matches(&trace));
assert!(m1.matches(&trace));
assert!(m2.matches(&trace));
assert!(!m1.matches(&trace));
assert!(!m2.matches(&trace));
assert!(!m3.matches(&trace));
assert!(m4.matches(&trace));
assert!(!m5.matches(&trace));
Expand Down
Loading