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(rpc/trace): filter matches with trace #1090

Merged
merged 6 commits into from
Jul 23, 2024
Merged
Changes from 4 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
338 changes: 243 additions & 95 deletions crates/rpc-types-trace/src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
//! `trace_filter` types and support
use crate::parity::{
Action, CallAction, CreateAction, CreateOutput, RewardAction, SelfdestructAction, TraceOutput,
TransactionTrace,
};
use alloy_primitives::Address;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
Expand Down Expand Up @@ -93,38 +97,79 @@ pub enum TraceFilterMode {
Intersection,
}

/// Address filter.
/// This is a set of addresses to match against.
/// An empty set matches all addresses.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct AddressFilter(HashSet<Address>);
jsvisa marked this conversation as resolved.
Show resolved Hide resolved

impl FromIterator<Address> for AddressFilter {
fn from_iter<I: IntoIterator<Item = Address>>(iter: I) -> Self {
Self(iter.into_iter().collect())
}
}

impl From<Vec<Address>> for AddressFilter {
fn from(addrs: Vec<Address>) -> Self {
Self::from_iter(addrs)
}
}

impl AddressFilter {
/// Returns `true` if the given address is in the filter.
jsvisa marked this conversation as resolved.
Show resolved Hide resolved
pub fn matches(&self, addr: &Address) -> bool {
self.0.is_empty() || self.0.contains(addr)
}

/// Returns `true` if the filter is empty.
pub fn matches_all(&self) -> bool {
self.0.is_empty()
}
}

/// Helper type for matching `from` and `to` addresses. Empty sets match all addresses.
#[derive(Clone, Debug, PartialEq, Eq)]
jsvisa marked this conversation as resolved.
Show resolved Hide resolved
pub struct TraceFilterMatcher {
mode: TraceFilterMode,
from_addresses: HashSet<Address>,
to_addresses: HashSet<Address>,
from_addresses: AddressFilter,
to_addresses: AddressFilter,
}

impl TraceFilterMatcher {
/// Returns `true` if the given `from` and `to` addresses match this filter.
pub fn matches(&self, from: Address, to: Option<Address>) -> bool {
match (self.from_addresses.is_empty(), self.to_addresses.is_empty()) {
(true, true) => true,
(false, true) => self.from_addresses.contains(&from),
(true, false) => to.map_or(false, |to_addr| self.to_addresses.contains(&to_addr)),
(false, false) => match self.mode {
TraceFilterMode::Union => {
self.from_addresses.contains(&from)
|| to.map_or(false, |to_addr| self.to_addresses.contains(&to_addr))
}
TraceFilterMode::Intersection => {
self.from_addresses.contains(&from)
&& to.map_or(false, |to_addr| self.to_addresses.contains(&to_addr))
}
},
/// Returns `true` if the given `TransactionTrace` matches this filter.
pub fn matches(&self, trace: &TransactionTrace) -> bool {
jsvisa marked this conversation as resolved.
Show resolved Hide resolved
let (from_matches, to_matches) = match trace.action {
Action::Call(CallAction { from, to, .. }) => {
(self.from_addresses.matches(&from), self.to_addresses.matches(&to))
}
Action::Create(CreateAction { from, .. }) => (
self.from_addresses.matches(&from),
match trace.result {
Some(TraceOutput::Create(CreateOutput { address: to, .. })) => {
self.to_addresses.matches(&to)
}
_ => self.to_addresses.matches_all(),
},
),
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))
}
};

match self.mode {
TraceFilterMode::Union => from_matches || to_matches,
TraceFilterMode::Intersection => from_matches && to_matches,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::{Bytes, U256};
use serde_json::json;

#[test]
Expand All @@ -137,88 +182,191 @@ mod tests {

#[test]
fn test_filter_matcher_addresses_unspecified() {
let test_addr_d8 = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045".parse().unwrap();
let test_addr_16 = "0x160f5f00288e9e1cc8655b327e081566e580a71d".parse().unwrap();
let filter_json = json!({
"fromBlock": "0x3",
"toBlock": "0x5",
});
let filter: TraceFilter =
serde_json::from_value(filter_json).expect("Failed to parse filter");
let matcher = filter.matcher();
assert!(matcher.matches(test_addr_d8, None));
assert!(matcher.matches(test_addr_16, None));
assert!(matcher.matches(test_addr_d8, Some(test_addr_16)));
assert!(matcher.matches(test_addr_16, Some(test_addr_d8)));
}
let filter_json = json!({ "fromBlock": "0x3", "toBlock": "0x5" });
let matcher = serde_json::from_value::<TraceFilter>(filter_json).unwrap().matcher();
let s = r#"{
"action": {
"from": "0x66e29f0b6b1b07071f2fde4345d512386cb66f5f",
"callType": "call",
"gas": "0x10bfc",
"input": "0x",
"to": "0x160f5f00288e9e1cc8655b327e081566e580a71d",
"value": "0x244b"
},
"error": "Reverted",
"result": {
"gasUsed": "0x9daf",
"output": "0x"
},
"subtraces": 3,
"traceAddress": [],
"type": "call"
}"#;
let trace = serde_json::from_str::<TransactionTrace>(s).unwrap();

#[test]
fn test_filter_matcher_from_address() {
let test_addr_d8 = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045".parse().unwrap();
let test_addr_16 = "0x160f5f00288e9e1cc8655b327e081566e580a71d".parse().unwrap();
let filter_json = json!({
"fromBlock": "0x3",
"toBlock": "0x5",
"fromAddress": [test_addr_d8]
});
let filter: TraceFilter = serde_json::from_value(filter_json).unwrap();
let matcher = filter.matcher();
assert!(matcher.matches(test_addr_d8, None));
assert!(!matcher.matches(test_addr_16, None));
assert!(matcher.matches(test_addr_d8, Some(test_addr_16)));
assert!(!matcher.matches(test_addr_16, Some(test_addr_d8)));
assert!(matcher.matches(&trace));
}

#[test]
fn test_filter_matcher_to_address() {
let test_addr_d8 = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045".parse().unwrap();
let test_addr_16 = "0x160f5f00288e9e1cc8655b327e081566e580a71d".parse().unwrap();
let filter_json = json!({
"fromBlock": "0x3",
"toBlock": "0x5",
"toAddress": [test_addr_d8],
});
let filter: TraceFilter = serde_json::from_value(filter_json).unwrap();
let matcher = filter.matcher();
assert!(matcher.matches(test_addr_16, Some(test_addr_d8)));
assert!(!matcher.matches(test_addr_16, None));
assert!(!matcher.matches(test_addr_d8, Some(test_addr_16)));
}
fn test_filter_matcher() {
let addr0 = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045".parse().unwrap();
let addr1 = "0x160f5f00288e9e1cc8655b327e081566e580a71d".parse().unwrap();
let addr2 = "0x160f5f00288e9e1cc8655b327e081566e580a71f".parse().unwrap();

#[test]
fn test_filter_matcher_both_addresses_union() {
let test_addr_d8 = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045".parse().unwrap();
let test_addr_16 = "0x160f5f00288e9e1cc8655b327e081566e580a71d".parse().unwrap();
let filter_json = json!({
"fromBlock": "0x3",
"toBlock": "0x5",
"fromAddress": [test_addr_16],
"toAddress": [test_addr_d8],
});
let filter: TraceFilter = serde_json::from_value(filter_json).unwrap();
let matcher = filter.matcher();
assert!(matcher.matches(test_addr_16, Some(test_addr_d8)));
assert!(matcher.matches(test_addr_16, None));
assert!(matcher.matches(test_addr_d8, Some(test_addr_d8)));
assert!(!matcher.matches(test_addr_d8, Some(test_addr_16)));
}
let m0 = TraceFilterMatcher {
mode: TraceFilterMode::Union,
from_addresses: Default::default(),
to_addresses: Default::default(),
};

#[test]
fn test_filter_matcher_both_addresses_intersection() {
let test_addr_d8 = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045".parse().unwrap();
let test_addr_16 = "0x160f5f00288e9e1cc8655b327e081566e580a71d".parse().unwrap();
let filter_json = json!({
"fromBlock": "0x3",
"toBlock": "0x5",
"fromAddress": [test_addr_16],
"toAddress": [test_addr_d8],
"mode": "intersection",
});
let filter: TraceFilter = serde_json::from_value(filter_json).unwrap();
let matcher = filter.matcher();
assert!(matcher.matches(test_addr_16, Some(test_addr_d8)));
assert!(!matcher.matches(test_addr_16, None));
assert!(!matcher.matches(test_addr_d8, Some(test_addr_d8)));
assert!(!matcher.matches(test_addr_d8, Some(test_addr_16)));
let m1 = TraceFilterMatcher {
mode: TraceFilterMode::Union,
from_addresses: AddressFilter::from(vec![addr0]),
to_addresses: Default::default(),
};

let m2 = TraceFilterMatcher {
mode: TraceFilterMode::Union,
from_addresses: AddressFilter::from(vec![]),
to_addresses: AddressFilter::from(vec![addr1]),
};

let m3 = TraceFilterMatcher {
mode: TraceFilterMode::Union,
from_addresses: AddressFilter::from(vec![addr0]),
to_addresses: AddressFilter::from(vec![addr1]),
};

let m4 = TraceFilterMatcher {
mode: TraceFilterMode::Intersection,
from_addresses: Default::default(),
to_addresses: Default::default(),
};

let m5 = TraceFilterMatcher {
mode: TraceFilterMode::Intersection,
from_addresses: AddressFilter::from(vec![addr0]),
to_addresses: Default::default(),
};

let m6 = TraceFilterMatcher {
mode: TraceFilterMode::Intersection,
from_addresses: Default::default(),
to_addresses: AddressFilter::from(vec![addr1]),
};

let m7 = TraceFilterMatcher {
mode: TraceFilterMode::Intersection,
from_addresses: AddressFilter::from(vec![addr0]),
to_addresses: AddressFilter::from(vec![addr1]),
};

// normal call 0
let trace = TransactionTrace {
action: Action::Call(CallAction { from: addr0, to: addr1, ..Default::default() }),
..Default::default()
};
assert!(m0.matches(&trace));
assert!(m1.matches(&trace));
assert!(m2.matches(&trace));
assert!(m3.matches(&trace));
assert!(m4.matches(&trace));
assert!(m5.matches(&trace));
assert!(m6.matches(&trace));
assert!(m7.matches(&trace));

// normal call 1
let trace = TransactionTrace {
action: Action::Call(CallAction { from: addr0, to: addr2, ..Default::default() }),
..Default::default()
};
assert!(m0.matches(&trace));
assert!(m1.matches(&trace));
assert!(m2.matches(&trace));
assert!(m3.matches(&trace));
assert!(m4.matches(&trace));
assert!(m5.matches(&trace));
assert!(!m6.matches(&trace));
assert!(!m7.matches(&trace));

// create success
let trace = TransactionTrace {
action: Action::Create(CreateAction {
from: addr0,
gas: 10240,
init: Bytes::new(),
value: U256::from(0),
}),
result: Some(TraceOutput::Create(CreateOutput {
address: addr1,
code: Bytes::new(),
gas_used: 1025,
})),
..Default::default()
};
assert!(m0.matches(&trace));
assert!(m1.matches(&trace));
assert!(m2.matches(&trace));
assert!(m3.matches(&trace));
assert!(m4.matches(&trace));
assert!(m5.matches(&trace));
assert!(m6.matches(&trace));
assert!(m7.matches(&trace));

// create failure
let trace = TransactionTrace {
action: Action::Create(CreateAction {
from: addr0,
gas: 100,
init: Bytes::new(),
value: U256::from(0),
}),
error: Some("out of gas".into()),
..Default::default()
};
assert!(m0.matches(&trace));
assert!(m1.matches(&trace));
assert!(m2.matches(&trace));
assert!(m3.matches(&trace));
assert!(m4.matches(&trace));
assert!(m5.matches(&trace));
assert!(!m6.matches(&trace));
assert!(!m7.matches(&trace));

// selfdestruct
let trace = TransactionTrace {
action: Action::Selfdestruct(SelfdestructAction {
address: addr0,
refund_address: addr1,
balance: U256::from(0),
}),
..Default::default()
};
assert!(m0.matches(&trace));
assert!(m1.matches(&trace));
assert!(m2.matches(&trace));
assert!(m3.matches(&trace));
assert!(m4.matches(&trace));
assert!(m5.matches(&trace));
assert!(m6.matches(&trace));
assert!(m7.matches(&trace));

// reward
let trace = TransactionTrace {
action: Action::Reward(RewardAction {
author: addr0,
reward_type: crate::parity::RewardType::Block,
value: U256::from(0),
}),
..Default::default()
};
assert!(m0.matches(&trace));
assert!(m1.matches(&trace));
assert!(m2.matches(&trace));
assert!(!m3.matches(&trace));
assert!(m4.matches(&trace));
assert!(!m5.matches(&trace));
assert!(!m6.matches(&trace));
assert!(!m7.matches(&trace));
}
}