From 1b13616fd4611cbe2e814716a7d271246a8cb512 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 27 Jun 2023 20:37:23 +0200 Subject: [PATCH] fix(rpc): commit call state in trace call many (#3437) --- crates/rpc/rpc/src/trace.rs | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/crates/rpc/rpc/src/trace.rs b/crates/rpc/rpc/src/trace.rs index ae0c1193f..bfa45863c 100644 --- a/crates/rpc/rpc/src/trace.rs +++ b/crates/rpc/rpc/src/trace.rs @@ -193,7 +193,9 @@ where let mut results = Vec::with_capacity(calls.len()); let mut db = SubState::new(State::new(state)); - for (call, trace_types) in calls { + let mut calls = calls.into_iter().peekable(); + + while let Some((call, trace_types)) = calls.next() { let env = prepare_call_env( cfg.clone(), block_env.clone(), @@ -204,12 +206,30 @@ where let config = tracing_config(&trace_types); let mut inspector = TracingInspector::new(config); let (res, _) = inspect(&mut db, env, &mut inspector)?; - let trace_res = inspector.into_parity_builder().into_trace_results_with_state( - res, - &trace_types, - &db, - )?; + let ResultAndState { result, state } = res; + + let mut trace_res = + inspector.into_parity_builder().into_trace_results(result, &trace_types); + + // If statediffs were requested, populate them with the account balance and + // nonce from pre-state + if let Some(ref mut state_diff) = trace_res.state_diff { + populate_account_balance_nonce_diffs( + state_diff, + &db, + state.iter().map(|(addr, acc)| (*addr, acc.info.clone())), + )?; + } + results.push(trace_res); + + // need to apply the state changes of this call before executing the + // next call + if calls.peek().is_some() { + // need to apply the state changes of this call before executing + // the next call + db.commit(state) + } } Ok(results) @@ -403,6 +423,9 @@ where move |tx_info, inspector, res, state, db| { let mut full_trace = inspector.into_parity_builder().into_trace_results(res, &trace_types); + + // If statediffs were requested, populate them with the account balance and nonce + // from pre-state if let Some(ref mut state_diff) = full_trace.state_diff { populate_account_balance_nonce_diffs( state_diff,