Skip to content

Commit

Permalink
fix: correct gas limit and gas used results in trace API
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonberg1997 committed Jan 10, 2025
1 parent c601712 commit 9cd1233
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 28 deletions.
12 changes: 10 additions & 2 deletions crates/rpc/rpc-eth-api/src/helpers/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ pub trait Trace:
Self: LoadPendingBlock + LoadTransaction + Call,
F: FnOnce(
TransactionInfo,
u64, // tx gas limit
TracingInspector,
ResultAndState,
StateCacheDb<'_>,
Expand Down Expand Up @@ -179,6 +180,7 @@ pub trait Trace:
Self: LoadPendingBlock + LoadTransaction + Call,
F: FnOnce(
TransactionInfo,
u64, // tx gas limit
Insp,
ResultAndState,
StateCacheDb<'_>,
Expand Down Expand Up @@ -228,9 +230,10 @@ pub trait Trace:
block_env,
RpcNodeCore::evm_config(&this).tx_env(tx.tx(), tx.signer()),
);
let tx_gas_limit = env.tx.gas_limit;
let (res, _) =
this.inspect(StateCacheDbRefMutWrapper(&mut db), env, &mut inspector)?;
f(tx_info, inspector, res, db)
f(tx_info, tx_gas_limit, inspector, res, db)
})
.await
.map(Some)
Expand All @@ -255,6 +258,7 @@ pub trait Trace:
Self: LoadBlock,
F: Fn(
TransactionInfo,
u64, // tx gas limit
TracingInspector,
ExecutionResult,
&EvmState,
Expand Down Expand Up @@ -295,6 +299,7 @@ pub trait Trace:
Self: LoadBlock,
F: Fn(
TransactionInfo,
u64, // tx gas limit
Insp,
ExecutionResult,
&EvmState,
Expand Down Expand Up @@ -379,12 +384,13 @@ pub trait Trace:
block_env.clone(),
tx,
);
let tx_gas_limit = env.tx.gas_limit;

let mut inspector = inspector_setup();
let (res, _) =
this.inspect(StateCacheDbRefMutWrapper(&mut db), env, &mut inspector)?;
let ResultAndState { result, state } = res;
results.push(f(tx_info, inspector, result, &state, &db)?);
results.push(f(tx_info, tx_gas_limit, inspector, result, &state, &db)?);

// need to apply the state changes of this transaction before executing the
// next transaction, but only if there's a next transaction
Expand Down Expand Up @@ -423,6 +429,7 @@ pub trait Trace:
// state and db
F: Fn(
TransactionInfo,
u64, // tx gas limit
TracingInspector,
ExecutionResult,
&EvmState,
Expand Down Expand Up @@ -462,6 +469,7 @@ pub trait Trace:
// state and db
F: Fn(
TransactionInfo,
u64, // tx gas limit
Insp,
ExecutionResult,
&EvmState,
Expand Down
4 changes: 2 additions & 2 deletions crates/rpc/rpc/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,11 @@ where
.inner
.eth_api
.spawn_with_call_at(call, at, overrides, move |db, env| {
let (_res, env) =
this.eth_api().inspect(db, env, &mut inspector)?;
let (res, env) = this.eth_api().inspect(db, env, &mut inspector)?;
let tx_info = TransactionInfo::default();
let frame: FlatCallFrame = inspector
.with_transaction_gas_limit(env.tx.gas_limit)
.with_transaction_gas_used(res.result.gas_used())
.into_parity_builder()
.into_localized_transaction_traces(tx_info);
Ok(frame)
Expand Down
8 changes: 5 additions & 3 deletions crates/rpc/rpc/src/otterscan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ where
.spawn_trace_transaction_in_block_with_inspector(
tx_hash,
TransferInspector::new(false),
|_tx_info, inspector, _, _| Ok(inspector.into_transfers()),
|_tx_info, _tx_gas_limit, inspector, _, _| Ok(inspector.into_transfers()),
)
.await
.map_err(Into::into)?
Expand Down Expand Up @@ -146,7 +146,9 @@ where
.spawn_trace_transaction_in_block(
tx_hash,
TracingInspectorConfig::default_parity(),
move |_tx_info, inspector, _, _| Ok(inspector.into_traces().into_nodes()),
move |_tx_info, _tx_gas_limit, inspector, _, _| {
Ok(inspector.into_traces().into_nodes())
},
)
.await
.map_err(Into::into)?
Expand Down Expand Up @@ -351,7 +353,7 @@ where
num.into(),
None,
TracingInspectorConfig::default_parity(),
|tx_info, inspector, _, _, _| {
|tx_info, _, inspector, _, _, _| {
Ok(inspector.into_parity_builder().into_localized_transaction_traces(tx_info))
},
)
Expand Down
69 changes: 48 additions & 21 deletions crates/rpc/rpc/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,12 @@ where
// wrapper is hack to get around 'higher-ranked lifetime error', see
// <https://github.com/rust-lang/rust/issues/100013>
let db = db.0;
let tx_gas_limit = env.tx.gas_limit;

let (res, _) = this.eth_api().inspect(&mut *db, env, &mut inspector)?;
let trace_res = inspector
.with_transaction_gas_limit(tx_gas_limit)
.with_transaction_gas_used(res.result.gas_used())
.into_parity_builder()
.into_trace_results_with_state(&res, &trace_request.trace_types, &db)
.map_err(Eth::Error::from_eth_err)?;
Expand Down Expand Up @@ -125,10 +128,13 @@ where
);

let config = TracingInspectorConfig::from_parity_config(&trace_types);
let tx_gas_limit = env.tx.gas_limit;

self.eth_api()
.spawn_trace_at_with_state(env, config, at, move |inspector, res, db| {
inspector
.with_transaction_gas_limit(tx_gas_limit)
.with_transaction_gas_used(res.result.gas_used())
.into_parity_builder()
.into_trace_results_with_state(&res, &trace_types, &db)
.map_err(Eth::Error::from_eth_err)
Expand Down Expand Up @@ -166,11 +172,14 @@ where
&mut db,
Default::default(),
)?;
let tx_gas_limit = env.tx.gas_limit;
let config = TracingInspectorConfig::from_parity_config(&trace_types);
let mut inspector = TracingInspector::new(config);
let (res, _) = this.eth_api().inspect(&mut db, env, &mut inspector)?;

let trace_res = inspector
.with_transaction_gas_limit(tx_gas_limit)
.with_transaction_gas_used(res.result.gas_used())
.into_parity_builder()
.into_trace_results_with_state(&res, &trace_types, &db)
.map_err(Eth::Error::from_eth_err)?;
Expand Down Expand Up @@ -199,13 +208,19 @@ where
) -> Result<TraceResults, Eth::Error> {
let config = TracingInspectorConfig::from_parity_config(&trace_types);
self.eth_api()
.spawn_trace_transaction_in_block(hash, config, move |_, inspector, res, db| {
let trace_res = inspector
.into_parity_builder()
.into_trace_results_with_state(&res, &trace_types, &db)
.map_err(Eth::Error::from_eth_err)?;
Ok(trace_res)
})
.spawn_trace_transaction_in_block(
hash,
config,
move |_, tx_gas_limit, inspector, res, db| {
let trace_res = inspector
.with_transaction_gas_limit(tx_gas_limit)
.with_transaction_gas_used(res.result.gas_used())
.into_parity_builder()
.into_trace_results_with_state(&res, &trace_types, &db)
.map_err(Eth::Error::from_eth_err)?;
Ok(trace_res)
},
)
.await
.transpose()
.ok_or(EthApiError::TransactionNotFound)?
Expand Down Expand Up @@ -292,9 +307,12 @@ where
Some(block.clone()),
None,
TracingInspectorConfig::default_parity(),
move |tx_info, inspector, _, _, _| {
let mut traces =
inspector.into_parity_builder().into_localized_transaction_traces(tx_info);
move |tx_info, tx_gas_limit, inspector, res, _, _| {
let mut traces = inspector
.with_transaction_gas_limit(tx_gas_limit)
.with_transaction_gas_used(res.gas_used())
.into_parity_builder()
.into_localized_transaction_traces(tx_info);
traces.retain(|trace| matcher.matches(&trace.trace));
Ok(Some(traces))
},
Expand Down Expand Up @@ -359,9 +377,12 @@ where
.spawn_trace_transaction_in_block(
hash,
TracingInspectorConfig::default_parity(),
move |tx_info, inspector, _, _| {
let traces =
inspector.into_parity_builder().into_localized_transaction_traces(tx_info);
move |tx_info, tx_gas_limit, inspector, res, _| {
let traces = inspector
.with_transaction_gas_limit(tx_gas_limit)
.with_transaction_gas_used(res.result.gas_used())
.into_parity_builder()
.into_localized_transaction_traces(tx_info);
Ok(traces)
},
)
Expand All @@ -377,9 +398,12 @@ where
block_id,
None,
TracingInspectorConfig::default_parity(),
|tx_info, inspector, _, _, _| {
let traces =
inspector.into_parity_builder().into_localized_transaction_traces(tx_info);
|tx_info, tx_gas_limit, inspector, res, _, _| {
let traces = inspector
.with_transaction_gas_limit(tx_gas_limit)
.with_transaction_gas_used(res.gas_used())
.into_parity_builder()
.into_localized_transaction_traces(tx_info);
Ok(traces)
},
);
Expand Down Expand Up @@ -414,9 +438,12 @@ where
block_id,
None,
TracingInspectorConfig::from_parity_config(&trace_types),
move |tx_info, inspector, res, state, db| {
let mut full_trace =
inspector.into_parity_builder().into_trace_results(&res, &trace_types);
move |tx_info, tx_gas_limit, inspector, res, state, db| {
let mut full_trace = inspector
.with_transaction_gas_limit(tx_gas_limit)
.with_transaction_gas_used(res.gas_used())
.into_parity_builder()
.into_trace_results(&res, &trace_types);

// If statediffs were requested, populate them with the account balance and
// nonce from pre-state
Expand Down Expand Up @@ -445,7 +472,7 @@ where
.spawn_trace_transaction_in_block_with_inspector(
tx_hash,
OpcodeGasInspector::default(),
move |_tx_info, inspector, _res, _| {
move |_tx_info, _tx_gas_limit, inspector, _res, _| {
let trace = TransactionOpcodeGas {
transaction_hash: tx_hash,
opcode_gas: inspector.opcode_gas_iter().collect(),
Expand All @@ -470,7 +497,7 @@ where
block_id,
None,
OpcodeGasInspector::default,
move |tx_info, inspector, _res, _, _| {
move |tx_info, _tx_gas_limit, inspector, _res, _, _| {
let trace = TransactionOpcodeGas {
transaction_hash: tx_info.hash.expect("tx hash is set"),
opcode_gas: inspector.opcode_gas_iter().collect(),
Expand Down

0 comments on commit 9cd1233

Please sign in to comment.