Skip to content

Commit

Permalink
Merge pull request #3081 from ChenxingLi/fix_trace
Browse files Browse the repository at this point in the history
Fix filter logic for new trace matching implementation
  • Loading branch information
Pana authored Feb 21, 2025
2 parents 1b2037d + f7143dc commit 9dee81c
Show file tree
Hide file tree
Showing 17 changed files with 900 additions and 282 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ members = [
"crates/rpc/rpc-eth-api",
"crates/rpc/parity-trace-types",
"crates/rpc/rpc-eth-impl",
"crates/rpc/rpc-common-impl",
"crates/util/util-macros",
"crates/rpc/rpc-utils",
"crates/rpc/rpc-builder",
Expand Down Expand Up @@ -191,6 +192,7 @@ cfx-rpc = { path = "./crates/rpc/rpc-eth-impl" }
cfx-rpc-utils = { path = "./crates/rpc/rpc-utils" }
cfx-rpc-builder = { path = "./crates/rpc/rpc-builder" }
cfx-rpc-cfx-impl = { path = "./crates/rpc/rpc-cfx-impl" }
cfx-rpc-common-impl = { path = "./crates/rpc/rpc-common-impl" }
cfx-rpc-middlewares = { path = "./crates/rpc/rpc-middlewares" }
bounded-executor = { path = "./crates/pos/common/bounded-executor" }
#diem-channel = { path = "./crates/pos/common/channel", package = "channel" }
Expand Down Expand Up @@ -384,6 +386,7 @@ winapi = "0.3.7"
synstructure = "0.12"
lru-cache = "0.1"
lru_time_cache = "0.9.0"
slice-group-by = "0.3.1"

# num misc
bigdecimal = "0.1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub use cfx_parity_trace_types::{
self, Action, ActionType, Call, CallResult, Create, CreateResult,
InternalTransferAction, Outcome,
},
conversion::{construct_parity_trace, TraceWithPosition},
filter::{self, TraceFilter},
trace_types::{
self, BlockExecTraces, ExecTrace, LocalizedTrace, TransactionExecTraces,
Expand Down
2 changes: 2 additions & 0 deletions crates/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ cfx-rpc = { workspace = true }
cfx-rpc-utils = { workspace = true }
cfx-rpc-builder = { workspace = true }
jsonrpsee = { workspace = true }
cfx-rpc-common-impl = { workspace = true }
cfx-parity-trace-types = { workspace = true }

[dev-dependencies]
criterion = { workspace = true }
Expand Down
17 changes: 5 additions & 12 deletions crates/client/src/rpc/impls/cfx/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ use crate::{
rpc::{
traits::trace::Trace,
types::{
eth::LocalizedTrace as EthLocalizedTrace,
EpochNumber as RpcEpochNumber, LocalizedBlockTrace,
LocalizedTrace as RpcLocalizedTrace, LocalizedTrace,
EpochNumber as RpcEpochNumber, LocalizedBlockTrace, LocalizedTrace,
TraceFilter as RpcTraceFilter,
},
CoreResult,
},
};
use cfx_addr::Network;
use cfx_execute_helper::exec_tracer::TraceFilter as PrimitiveTraceFilter;
use cfx_parity_trace_types::LocalizedTrace as PrimitiveLocalizedTrace;
use cfx_rpc_cfx_impl::TraceHandler as CfxTraceHandler;
use cfx_rpc_cfx_types::trace::EpochTrace;
use cfx_types::H256;
Expand All @@ -40,16 +39,10 @@ impl TraceHandler {
self.inner.consensus_graph()
}

pub(crate) fn filter_traces_impl(
pub(crate) fn filter_primitives_traces_impl(
&self, filter: PrimitiveTraceFilter,
) -> CoreResult<Option<Vec<RpcLocalizedTrace>>> {
self.inner.filter_traces_impl(filter)
}

pub fn to_eth_traces(
traces: Vec<LocalizedTrace>,
) -> JsonRpcResult<Vec<EthLocalizedTrace>> {
CfxTraceHandler::to_eth_traces(traces)
) -> CoreResult<Option<Vec<PrimitiveLocalizedTrace>>> {
self.inner.filter_primitives_traces_impl(filter)
}
}

Expand Down
138 changes: 44 additions & 94 deletions crates/client/src/rpc/impls/eth/eth_trace.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use crate::rpc::{
impls::cfx::TraceHandler,
traits::eth_space::trace::Trace as EthTrace,
types::{
eth::{
BlockNumber, LocalizedTrace as EthLocalizedTrace, Res as EthRes,
TraceFilter as EthTraceFilter,
},
Action as RpcAction,
types::eth::{
BlockNumber, LocalizedTrace as EthLocalizedTrace,
TraceFilter as EthTraceFilter,
},
};
use cfx_execute_helper::exec_tracer::{
construct_parity_trace, TraceWithPosition,
use cfx_rpc_common_impl::trace::{
into_eth_localized_traces, primitive_traces_to_eth_localized_traces,
};
use cfx_types::H256;
use cfx_util_macros::unwrap_option_or_return_result_none as unwrap_or_return;
Expand Down Expand Up @@ -51,47 +48,23 @@ impl EthTrace for EthTraceHandler {
let block_number = phantom_block.pivot_header.height();
let block_hash = phantom_block.pivot_header.hash();

let network = self.trace_handler.inner.network;

for (idx, tx_traces) in phantom_block.traces.into_iter().enumerate() {
let tx_hash = phantom_block.transactions[idx].hash();

for TraceWithPosition {
action,
result,
child_count,
trace_path,
} in construct_parity_trace(&tx_traces).map_err(|e| {
let tx_eth_traces = into_eth_localized_traces(
&tx_traces.0,
block_number,
block_hash,
tx_hash,
idx,
network,
)
.map_err(|e| {
warn!("Internal error on trace reconstruction: {}", e);
JsonRpcError::internal_error()
})? {
let mut eth_trace = EthLocalizedTrace {
action: RpcAction::try_from(
action.action.clone(),
self.trace_handler.inner.network,
)
.map_err(|_| JsonRpcError::internal_error())?
.try_into()
.map_err(|_| JsonRpcError::internal_error())?,
result: EthRes::None,
trace_address: trace_path,
subtraces: child_count,
transaction_position: Some(idx),
transaction_hash: Some(tx_hash),
block_number,
block_hash,
// action and its result should have the same `valid`.
valid: action.valid,
};

eth_trace.set_result(
RpcAction::try_from(
result.action.clone(),
self.trace_handler.inner.network,
)
.map_err(|_| JsonRpcError::internal_error())?,
)?;

eth_traces.push(eth_trace);
}
})?;
eth_traces.extend(tx_eth_traces);
}

Ok(Some(eth_traces))
Expand All @@ -100,17 +73,24 @@ impl EthTrace for EthTraceHandler {
fn filter_traces(
&self, filter: EthTraceFilter,
) -> JsonRpcResult<Option<Vec<EthLocalizedTrace>>> {
// TODO(lpl): Use `TransactionExecTraces::filter_trace_pairs` to avoid
// pairing twice.
let primitive_filter = filter.into_primitive()?;

let traces =
match self.trace_handler.filter_traces_impl(primitive_filter)? {
None => return Ok(None),
Some(traces) => traces,
};
let Some(primitive_traces) = self
.trace_handler
.filter_primitives_traces_impl(primitive_filter)?
else {
return Ok(None);
};

Ok(Some(TraceHandler::to_eth_traces(traces)?))
let traces = primitive_traces_to_eth_localized_traces(
&primitive_traces,
self.trace_handler.inner.network,
)
.map_err(|e| {
warn!("Internal error on trace reconstruction: {}", e);
JsonRpcError::internal_error()
})?;
Ok(Some(traces))
}

fn transaction_traces(
Expand Down Expand Up @@ -154,51 +134,21 @@ impl EthTrace for EthTraceHandler {
let tx = &phantom_block.transactions[id];
let tx_traces = phantom_block.traces[id].clone();

// convert traces
let trace_pairs = construct_parity_trace(&tx_traces).map_err(|e| {
let network = self.trace_handler.inner.network;

let eth_traces = into_eth_localized_traces(
&tx_traces.0,
epoch_num,
phantom_block.pivot_header.hash(),
tx.hash,
id,
network,
)
.map_err(|e| {
warn!("Internal error on trace reconstruction: {}", e);
JsonRpcError::internal_error()
})?;

let mut eth_traces = Vec::new();

for TraceWithPosition {
action,
result,
child_count,
trace_path,
} in trace_pairs
{
let mut eth_trace = EthLocalizedTrace {
action: RpcAction::try_from(
action.action.clone(),
self.trace_handler.inner.network,
)
.map_err(|_| JsonRpcError::internal_error())?
.try_into()
.map_err(|_| JsonRpcError::internal_error())?,
result: EthRes::None,
trace_address: trace_path,
subtraces: child_count,
transaction_position: Some(id),
transaction_hash: Some(tx.hash()),
block_number: epoch_num,
block_hash: phantom_block.pivot_header.hash(),
// action and its result should have the same `valid`.
valid: action.valid,
};

eth_trace.set_result(
RpcAction::try_from(
result.action.clone(),
self.trace_handler.inner.network,
)
.map_err(|_| JsonRpcError::internal_error())?,
)?;

eth_traces.push(eth_trace);
}

Ok(Some(eth_traces))
}
}
2 changes: 0 additions & 2 deletions crates/rpc/parity-trace-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod action_types;
pub mod address_pocket;
pub mod conversion;
pub mod filter;
pub mod trace_types;

Expand All @@ -9,6 +8,5 @@ mod tests;

pub use action_types::*;
pub use address_pocket::AddressPocket;
pub use conversion::{construct_parity_trace, TraceWithPosition};
pub use filter::*;
pub use trace_types::*;
2 changes: 2 additions & 0 deletions crates/rpc/rpc-cfx-impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ cfxcore = { workspace = true }
primitives = { workspace = true }
cfx-rpc-cfx-types = { workspace = true }
cfx-util-macros = { workspace = true }
cfx-parity-trace-types = { workspace = true }
cfx-rpc-common-impl = { workspace = true }
Loading

0 comments on commit 9dee81c

Please sign in to comment.