Skip to content

Commit

Permalink
Fixed compilation and addressed some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
PlamenHristov committed Jul 26, 2023
1 parent 1b98611 commit 7fb836e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 50 deletions.
85 changes: 45 additions & 40 deletions crates/revm/revm-inspectors/src/tracing/builder/geth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,65 +150,70 @@ impl GethTraceBuilder {
}
}

/// Returns the accounts necessary for transaction execution.
///
/// The prestate mode returns the accounts necessary to execute a given transaction.
/// diff_mode returns the differences between the transaction's pre and post-state.
///
/// * `state` - The state post-transaction execution.
/// * `diff_mode` - if prestate is in diff or prestate mode.
/// * `db` - The database to fetch state pre-transaction execution.
pub fn geth_prestate_traces<DB>(
&self,
ResultAndState { state, .. }: &ResultAndState,
PreStateConfig { diff_mode }: PreStateConfig,
db: DB,
) -> Result<PreStateFrame, DB::Error>
where DB: DatabaseRef {
match diff_mode {
Some(_) => {
let account_diffs = state.into_iter().map(|(addr, acc)| (*addr, &acc.info));
let mut prestate_result = account_diffs.into_iter().try_fold(PreStateMode::default(), |mut prestate, (addr, _)| {
let db_acc = db.basic(addr)?.unwrap_or_default();
prestate.0.insert(addr.clone(), AccountState {
balance: Some(db_acc.balance),
nonce: Some(U256::from(db_acc.nonce)),
code: db_acc.code.as_ref().map(|code| Bytes::from(code.original_bytes())),
storage: None,
});
Ok(prestate)
})?;
self.get_storage_from_state(&mut prestate_result.0, false);
Ok(PreStateFrame::Default(prestate_result))

let account_diffs: Vec<_> = state.into_iter().map(|(addr, acc)| (*addr, &acc.info)).collect();

if diff_mode.unwrap_or_default() {
let mut prestate = PreStateMode::default();
for (addr, _) in account_diffs {
let db_acc = db.basic(addr)?.unwrap_or_default();
prestate.0.insert(addr, AccountState {
balance: Some(db_acc.balance),
nonce: Some(U256::from(db_acc.nonce)),
code: db_acc.code.as_ref().map(|code| Bytes::from(code.original_bytes())),
storage: None,
});
}
None => {
let account_diffs = state.into_iter().map(|(addr, acc)| (*addr, &acc.info));
let mut state_diff_result = account_diffs.into_iter().try_fold(DiffMode::default(), |mut state_diff, (addr, changed_acc)| {
let db_acc = db.basic(addr)?.unwrap_or_default();
let pre_state = AccountState {
balance: Some(db_acc.balance),
nonce: Some(U256::from(db_acc.nonce)),
code: db_acc.code.as_ref().map(|code| Bytes::from(code.original_bytes())),
storage: None,
};
let post_state = AccountState {
balance: Some(changed_acc.balance),
nonce: Some(U256::from(changed_acc.nonce)),
code: changed_acc.code.as_ref().map(|code| Bytes::from(code.original_bytes())),
storage: None,
};
state_diff.pre.insert(addr.clone(), pre_state);
state_diff.post.insert(addr.clone(), post_state);
Ok(state_diff)
})?;
self.get_storage_from_state(&mut state_diff_result.pre, false);
self.get_storage_from_state(&mut state_diff_result.post, true);
Ok(PreStateFrame::Diff(state_diff_result))
self.update_storage_from_trace(&mut prestate.0, false);
Ok(PreStateFrame::Default(prestate))
} else {
let mut state_diff = DiffMode::default();
for (addr, changed_acc) in account_diffs {
let db_acc = db.basic(addr)?.unwrap_or_default();
let pre_state = AccountState {
balance: Some(db_acc.balance),
nonce: Some(U256::from(db_acc.nonce)),
code: db_acc.code.as_ref().map(|code| Bytes::from(code.original_bytes())),
storage: None,
};
let post_state = AccountState {
balance: Some(changed_acc.balance),
nonce: Some(U256::from(changed_acc.nonce)),
code: changed_acc.code.as_ref().map(|code| Bytes::from(code.original_bytes())),
storage: None,
};
state_diff.pre.insert(addr, pre_state);
state_diff.post.insert(addr, post_state);
}
self.update_storage_from_trace(&mut state_diff.pre, false);
self.update_storage_from_trace(&mut state_diff.post, true);
Ok(PreStateFrame::Diff(state_diff))
}
}

fn get_storage_from_state(
fn update_storage_from_trace(
&self,
account_states: &mut BTreeMap<Address, AccountState>,
post_value: bool
) {
for node in self.iter_traceable_nodes() {
node.geth_update_account_storage(account_states, post_value);
}

}

fn iter_traceable_nodes(&self) -> impl Iterator<Item = &CallTraceNode> {
Expand Down
9 changes: 5 additions & 4 deletions crates/revm/revm-inspectors/src/tracing/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,15 +445,16 @@ impl CallTraceNode {
call_frame
}

/// Adds storage in-place to account state for all accounts that were touched in the trace [CallTrace] execution.
///
/// * `account_states` - the account map updated in place.
/// * `post_value` - if true, it adds storage values after trace transaction execution, if false, returns the storage values before trace execution.
pub(crate) fn geth_update_account_storage(&self, account_states: &mut BTreeMap<Address, AccountState>, post_value: bool) {
let addr = self.trace.address;
let acc_state = account_states.entry(addr).or_insert_with(AccountState::default);
for change in self.trace.steps.iter().filter_map(|s| s.storage_change) {
let StorageChange { key, value, had_value } = change;
if acc_state.storage.is_none() {
acc_state.storage = Some(BTreeMap::new());
}
let storage_map = acc_state.storage.as_mut().unwrap();
let storage_map = acc_state.storage.get_or_insert_with(BTreeMap::new);
let value_to_insert = if post_value {
H256::from(value)
} else {
Expand Down
12 changes: 6 additions & 6 deletions crates/rpc/rpc/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ where
.inspect_call_at_and_return_state(call, at, overrides, &mut inspector)
.await?;

let frame = inspector.into_geth_builder().geth_prestate_traces(&res, prestate_config, &db);
return Ok(frame.unwrap().into())
let frame = inspector.into_geth_builder().geth_prestate_traces(&res, prestate_config, &db)?;
return Ok(frame.into())
}
GethDebugBuiltInTracerType::NoopTracer => Ok(NoopFrame::default().into()),
},
Expand Down Expand Up @@ -377,11 +377,11 @@ where
let mut inspector = TracingInspector::new(
TracingInspectorConfig::from_geth_config(&config)
);
let (res, _) = inspect(db, env, &mut inspector)?;
let frame = inspector.into_geth_builder().geth_prestate_traces(&res, prestate_config, db);
let (res, _) = inspect(&mut *db, env, &mut inspector)?;

let state = res.state.clone();
return Ok((frame.unwrap().into(), state))
let frame = inspector.into_geth_builder().geth_prestate_traces(&res, prestate_config, &*db)?;

return Ok((frame.into(), res.state))
}
GethDebugBuiltInTracerType::NoopTracer => {
Ok((NoopFrame::default().into(), Default::default()))
Expand Down

0 comments on commit 7fb836e

Please sign in to comment.