Skip to content

Commit

Permalink
fix: breaking changes geth trace data (#3183)
Browse files Browse the repository at this point in the history
* fix: breaking changes geth trace data

* bump ethers
  • Loading branch information
mattsse authored Sep 13, 2022
1 parent 152e200 commit 135af21
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 37 deletions.
47 changes: 24 additions & 23 deletions Cargo.lock

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

54 changes: 40 additions & 14 deletions evm/src/trace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,25 +353,18 @@ impl From<&CallTraceStep> for StructLog {
error: step.error.clone(),
gas: step.gas,
gas_cost: step.gas_cost,
memory: Some(step.memory.data().clone()),
memory: Some(convert_memory(step.memory.data())),
op: step.op.to_string(),
pc: U256::from(step.pc),
pc: step.pc as u64,
refund_counter: Some(step.gas_refund_counter),
stack: Some(step.stack.data().clone()),
storage: Some(
step.state
.iter()
.map(|(key, value)| {
(
*key,
value
.storage
.iter()
.map(|(key, value)| {
(H256::from_uint(key), H256::from_uint(&value.present_value()))
})
.collect(),
)
.values()
.flat_map(|acc| {
acc.storage.iter().map(|(key, value)| {
(H256::from_uint(key), H256::from_uint(&value.present_value()))
})
})
.collect(),
),
Expand Down Expand Up @@ -548,3 +541,36 @@ pub fn load_contracts(
BTreeMap::new()
}
}

/// creates the memory data in 32byte chunks
/// see <https://github.com/ethereum/go-ethereum/blob/366d2169fbc0e0f803b68c042b77b6b480836dbc/eth/tracers/logger/logger.go#L450-L452>
fn convert_memory(data: &[u8]) -> Vec<String> {
let mut memory = Vec::with_capacity((data.len() + 31) / 32);
for idx in (0..data.len()).step_by(32) {
let len = std::cmp::min(idx + 32, data.len());
memory.push(hex::encode(&data[idx..len]));
}
memory
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn can_convert_memory() {
let mut data = vec![0u8; 32];
assert_eq!(
convert_memory(&data),
vec!["0000000000000000000000000000000000000000000000000000000000000000".to_string()]
);
data.extend(data.clone());
assert_eq!(
convert_memory(&data),
vec![
"0000000000000000000000000000000000000000000000000000000000000000".to_string(),
"0000000000000000000000000000000000000000000000000000000000000000".to_string()
]
);
}
}

0 comments on commit 135af21

Please sign in to comment.