Skip to content

Commit

Permalink
address conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
cassc committed Nov 22, 2023
1 parent cdb15ce commit afddc3a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
28 changes: 21 additions & 7 deletions src/comparator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ pub fn execute_and_compare(
code: Some(Bytecode::new_raw(info.code)),
nonce: info.nonce,
};
cache_state.insert_account_with_storage(address, acc_info, info.storage);
cache_state.insert_account_with_storage(address.into(), acc_info, info.storage);
}

let mut env = Env::default();
env.cfg.chain_id = 1;
env.block.number = unit.env.current_number;
env.block.coinbase = unit.env.current_coinbase;
env.block.coinbase = unit.env.current_coinbase.into();
env.block.timestamp = unit.env.current_timestamp;
env.block.gas_limit = unit.env.current_gas_limit;
env.block.basefee = unit.env.current_base_fee.unwrap_or_default();
Expand All @@ -82,14 +82,14 @@ pub fn execute_and_compare(
// Run test in post generated by cuevm
for (index, test) in unit.post.into_iter().enumerate() {
env.tx.gas_limit = test.msg.gas_limit.saturating_to();
env.tx.caller = test.msg.sender;
env.tx.caller = test.msg.sender.into();
env.tx.gas_price = test.msg.gas_price.unwrap_or_default(); // Note some ethtest has max_fee_per_gas

env.tx.data = test.msg.data.clone();
env.tx.value = test.msg.value;

let to = match test.msg.to {
Some(add) => TransactTo::Call(add),
Some(add) => TransactTo::Call(add.into()),
None => TransactTo::Create(CreateScheme::Create),
};
env.tx.transact_to = to;
Expand Down Expand Up @@ -130,11 +130,25 @@ pub fn execute_and_compare(
);
}

traces.iter().enumerate().skip(1).for_each(|(idx, t)| {
if traces.len() != test.traces.len() {
eprintln!(
"WARN: {} Trace length mismatch, stack comparison result might be wrong. Length expected {}, actual: {}",
index,
traces.len(),
test.traces.len(),
);
}

let traces_iter = traces.iter().enumerate().skip(1);

for (idx, t) in traces_iter {
let revm_stack = t.stack.data().clone();
if idx >= test.traces.len() {
break;
}
let cuevm_stack = test.traces[idx - 1].stack.data.clone();
compare_stack(&test_json, idx, revm_stack, cuevm_stack).unwrap();
});
compare_stack(&test_json, idx, revm_stack, cuevm_stack)?;
}
}
}

Expand Down
39 changes: 28 additions & 11 deletions src/cuevm_test_suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@ use std::collections::BTreeMap;
use revm::primitives::{Address, Bytes, HashMap, B256, U256};
use serde::{de, Deserialize};

#[derive(Debug, PartialEq, Eq, Deserialize, Hash, Clone)]
pub struct BytesAddress(Bytes);

impl From<BytesAddress> for Address {
fn from(val: BytesAddress) -> Self {
let bytes = val.0;
let padded = {
if bytes.len() < 20 {
let mut padded = vec![0; 20];
padded[20 - bytes.len()..].copy_from_slice(&bytes);
padded
} else {
bytes.to_vec()
}
};
Address::from_slice(&padded)
}
}

pub fn deserialize_str_as_u64<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
D: de::Deserializer<'de>,
Expand Down Expand Up @@ -32,12 +51,10 @@ where
} else {
stripped.to_owned()
}
} else if is_odd {
format!("0{}", string)
} else {
if is_odd {
format!("0{}", string)
} else {
string.to_owned()
}
string.to_owned()
}
};

Expand All @@ -62,7 +79,7 @@ pub struct AccountInfo {
#[derive(Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Env {
pub current_coinbase: Address,
pub current_coinbase: BytesAddress,
pub current_difficulty: U256,
pub current_gas_limit: U256,
pub current_number: U256,
Expand All @@ -81,7 +98,7 @@ pub struct Env {
#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct CuEvmTestUnit {
pub env: Env,
pub pre: HashMap<Address, AccountInfo>,
pub pre: HashMap<BytesAddress, AccountInfo>,
pub post: Vec<CuEvmTest>,
}

Expand All @@ -98,11 +115,11 @@ pub struct CuEvmTest {
#[derive(Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CuEvmMsg {
pub sender: Address,
pub sender: BytesAddress,
pub value: U256,
pub to: Option<Address>,
pub to: Option<BytesAddress>,
pub nonce: U256,
pub origin: Address,
pub origin: BytesAddress,
pub gas_price: Option<U256>,
pub gas_limit: U256,
pub data: Bytes,
Expand All @@ -111,7 +128,7 @@ pub struct CuEvmMsg {
#[derive(Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CuEvmTrace {
pub address: Address,
pub address: BytesAddress,
pub pc: usize,
pub opcode: u8,
pub stack: CuEvmStack,
Expand Down

0 comments on commit afddc3a

Please sign in to comment.