2022-10-24 13:06:53 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
|
|
use anyhow::Result;
|
2022-10-26 16:39:31 +02:00
|
|
|
use eth_trie_utils::partial_trie::PartialTrie;
|
2022-10-27 09:55:33 +02:00
|
|
|
use ethereum_types::{Address, BigEndianHash, H256, U256};
|
|
|
|
|
use keccak_hash::keccak;
|
|
|
|
|
use rand::{thread_rng, Rng};
|
2022-10-24 13:06:53 +02:00
|
|
|
|
2022-10-27 09:56:52 +02:00
|
|
|
use crate::cpu::kernel::aggregator::KERNEL;
|
2022-10-26 16:39:31 +02:00
|
|
|
use crate::cpu::kernel::constants::global_metadata::GlobalMetadata;
|
2022-10-24 13:06:53 +02:00
|
|
|
use crate::cpu::kernel::interpreter::Interpreter;
|
2022-10-27 09:56:52 +02:00
|
|
|
use crate::cpu::kernel::tests::mpt::nibbles_64;
|
2022-10-24 13:06:53 +02:00
|
|
|
use crate::generation::mpt::{all_mpt_prover_inputs_reversed, AccountRlp};
|
|
|
|
|
|
2022-10-27 09:55:33 +02:00
|
|
|
fn test_account(code: &[u8]) -> AccountRlp {
|
2022-10-24 13:06:53 +02:00
|
|
|
AccountRlp {
|
|
|
|
|
nonce: U256::from(1111),
|
|
|
|
|
balance: U256::from(2222),
|
2022-10-26 16:39:31 +02:00
|
|
|
storage_root: PartialTrie::Empty.calc_hash(),
|
2022-10-27 09:55:33 +02:00
|
|
|
code_hash: keccak(code),
|
2022-10-24 13:06:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-27 09:55:33 +02:00
|
|
|
fn random_code() -> Vec<u8> {
|
|
|
|
|
let mut rng = thread_rng();
|
|
|
|
|
let num_bytes = rng.gen_range(0..10000);
|
|
|
|
|
(0..num_bytes).map(|_| rng.gen()).collect()
|
2022-10-24 13:06:53 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-27 09:55:33 +02:00
|
|
|
// Stolen from `tests/mpt/insert.rs`
|
|
|
|
|
fn prepare_interpreter(
|
|
|
|
|
interpreter: &mut Interpreter,
|
|
|
|
|
address: Address,
|
|
|
|
|
account: &AccountRlp,
|
|
|
|
|
) -> Result<()> {
|
2022-10-26 16:39:31 +02:00
|
|
|
let load_all_mpts = KERNEL.global_labels["load_all_mpts"];
|
|
|
|
|
let mpt_insert_state_trie = KERNEL.global_labels["mpt_insert_state_trie"];
|
|
|
|
|
let mpt_hash_state_trie = KERNEL.global_labels["mpt_hash_state_trie"];
|
2022-10-27 09:55:33 +02:00
|
|
|
let state_trie: PartialTrie = Default::default();
|
|
|
|
|
let trie_inputs = Default::default();
|
|
|
|
|
|
|
|
|
|
interpreter.offset = load_all_mpts;
|
|
|
|
|
interpreter.push(0xDEADBEEFu32.into());
|
2022-10-26 16:39:31 +02:00
|
|
|
|
2022-10-24 13:06:53 +02:00
|
|
|
interpreter.generation_state.mpt_prover_inputs = all_mpt_prover_inputs_reversed(&trie_inputs);
|
2022-10-26 16:39:31 +02:00
|
|
|
interpreter.run()?;
|
|
|
|
|
assert_eq!(interpreter.stack(), vec![]);
|
|
|
|
|
|
2022-10-27 09:55:33 +02:00
|
|
|
let k = nibbles_64(U256::from_big_endian(
|
|
|
|
|
keccak(address.to_fixed_bytes()).as_bytes(),
|
|
|
|
|
));
|
2022-10-26 16:39:31 +02:00
|
|
|
// Next, execute mpt_insert_state_trie.
|
|
|
|
|
interpreter.offset = mpt_insert_state_trie;
|
|
|
|
|
let trie_data = interpreter.get_trie_data_mut();
|
|
|
|
|
if trie_data.is_empty() {
|
|
|
|
|
// In the assembly we skip over 0, knowing trie_data[0] = 0 by default.
|
|
|
|
|
// Since we don't explicitly set it to 0, we need to do so here.
|
|
|
|
|
trie_data.push(0.into());
|
|
|
|
|
}
|
|
|
|
|
let value_ptr = trie_data.len();
|
|
|
|
|
trie_data.push(account.nonce);
|
|
|
|
|
trie_data.push(account.balance);
|
|
|
|
|
// In memory, storage_root gets interpreted as a pointer to a storage trie,
|
|
|
|
|
// so we have to ensure the pointer is valid. It's easiest to set it to 0,
|
|
|
|
|
// which works as an empty node, since trie_data[0] = 0 = MPT_TYPE_EMPTY.
|
|
|
|
|
trie_data.push(H256::zero().into_uint());
|
|
|
|
|
trie_data.push(account.code_hash.into_uint());
|
|
|
|
|
let trie_data_len = trie_data.len().into();
|
|
|
|
|
interpreter.set_global_metadata_field(GlobalMetadata::TrieDataSize, trie_data_len);
|
|
|
|
|
interpreter.push(0xDEADBEEFu32.into());
|
|
|
|
|
interpreter.push(value_ptr.into()); // value_ptr
|
|
|
|
|
interpreter.push(k.packed); // key
|
|
|
|
|
|
|
|
|
|
interpreter.run()?;
|
|
|
|
|
assert_eq!(
|
|
|
|
|
interpreter.stack().len(),
|
|
|
|
|
0,
|
|
|
|
|
"Expected empty stack after insert, found {:?}",
|
|
|
|
|
interpreter.stack()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Now, execute mpt_hash_state_trie.
|
|
|
|
|
interpreter.offset = mpt_hash_state_trie;
|
|
|
|
|
interpreter.push(0xDEADBEEFu32.into());
|
|
|
|
|
interpreter.run()?;
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
interpreter.stack().len(),
|
|
|
|
|
1,
|
|
|
|
|
"Expected 1 item on stack after hashing, found {:?}",
|
|
|
|
|
interpreter.stack()
|
|
|
|
|
);
|
|
|
|
|
let hash = H256::from_uint(&interpreter.stack()[0]);
|
|
|
|
|
|
2022-10-27 09:55:33 +02:00
|
|
|
let updated_trie = state_trie.insert(k, rlp::encode(account).to_vec());
|
2022-10-26 16:39:31 +02:00
|
|
|
let expected_state_trie_hash = updated_trie.calc_hash();
|
|
|
|
|
assert_eq!(hash, expected_state_trie_hash);
|
|
|
|
|
|
2022-10-27 09:55:33 +02:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_extcodecopy() -> Result<()> {
|
|
|
|
|
let code = random_code();
|
|
|
|
|
let account = test_account(&code);
|
|
|
|
|
|
|
|
|
|
let mut interpreter = Interpreter::new_with_kernel(0, vec![]);
|
|
|
|
|
let address: Address = thread_rng().gen();
|
|
|
|
|
prepare_interpreter(&mut interpreter, address, &account)?;
|
|
|
|
|
|
|
|
|
|
let extcodecopy = KERNEL.global_labels["extcodecopy"];
|
|
|
|
|
let extcodesize = KERNEL.global_labels["extcodesize"];
|
|
|
|
|
|
2022-10-26 16:39:31 +02:00
|
|
|
interpreter.pop();
|
2022-10-27 09:55:33 +02:00
|
|
|
interpreter.push(0xDEADBEEFu32.into());
|
|
|
|
|
interpreter.push(U256::from_big_endian(address.as_bytes()));
|
2022-10-26 16:39:31 +02:00
|
|
|
interpreter.offset = extcodesize;
|
2022-10-27 09:55:33 +02:00
|
|
|
interpreter.generation_state.inputs.contract_code =
|
|
|
|
|
HashMap::from([(keccak(&code), code.clone())]);
|
2022-10-24 13:06:53 +02:00
|
|
|
interpreter.run()?;
|
2022-10-27 09:55:33 +02:00
|
|
|
assert_eq!(interpreter.stack(), vec![code.len().into()]);
|
2022-10-24 13:06:53 +02:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|