137 lines
3.9 KiB
Rust
Raw Normal View History

2022-10-01 21:55:47 -07:00
use anyhow::Result;
2022-10-11 20:15:33 -06:00
use eth_trie_utils::partial_trie::PartialTrie;
2022-10-01 21:55:47 -07:00
use ethereum_types::{BigEndianHash, H256, U256};
2022-10-11 20:15:33 -06:00
use super::nibbles;
2022-10-01 21:55:47 -07:00
use crate::cpu::kernel::aggregator::KERNEL;
use crate::cpu::kernel::interpreter::Interpreter;
2022-10-06 20:50:32 -07:00
use crate::cpu::kernel::tests::mpt::extension_to_leaf;
2022-10-04 15:12:44 -07:00
use crate::generation::mpt::{all_mpt_prover_inputs_reversed, AccountRlp};
2022-10-01 21:55:47 -07:00
use crate::generation::TrieInputs;
2022-10-06 16:05:28 -07:00
// TODO: Test with short leaf. Might need to be a storage trie.
#[test]
fn mpt_hash_empty() -> Result<()> {
let trie_inputs = TrieInputs {
state_trie: Default::default(),
transactions_trie: Default::default(),
receipts_trie: Default::default(),
storage_tries: vec![],
};
test_state_trie(trie_inputs)
}
2022-10-01 21:55:47 -07:00
#[test]
2022-10-06 16:05:28 -07:00
fn mpt_hash_leaf() -> Result<()> {
2022-10-04 15:12:44 -07:00
let account = AccountRlp {
nonce: U256::from(1111),
balance: U256::from(2222),
storage_root: H256::from_uint(&U256::from(3333)),
code_hash: H256::from_uint(&U256::from(4444)),
};
let account_rlp = rlp::encode(&account);
2022-10-01 21:55:47 -07:00
let state_trie = PartialTrie::Leaf {
2022-10-11 20:15:33 -06:00
nibbles: nibbles(0xABC),
2022-10-01 21:55:47 -07:00
value: account_rlp.to_vec(),
};
let trie_inputs = TrieInputs {
state_trie,
transactions_trie: Default::default(),
receipts_trie: Default::default(),
storage_tries: vec![],
};
2022-10-06 16:05:28 -07:00
test_state_trie(trie_inputs)
}
#[test]
2022-10-06 20:50:32 -07:00
fn mpt_hash_extension_to_leaf() -> Result<()> {
let account = AccountRlp {
nonce: U256::from(1111),
balance: U256::from(2222),
storage_root: H256::from_uint(&U256::from(3333)),
code_hash: H256::from_uint(&U256::from(4444)),
};
let account_rlp = rlp::encode(&account);
let state_trie = extension_to_leaf(account_rlp.to_vec());
let trie_inputs = TrieInputs {
state_trie,
transactions_trie: Default::default(),
receipts_trie: Default::default(),
storage_tries: vec![],
};
test_state_trie(trie_inputs)
}
#[test]
fn mpt_hash_branch_to_leaf() -> Result<()> {
2022-10-06 16:05:28 -07:00
let account = AccountRlp {
nonce: U256::from(1111),
balance: U256::from(2222),
storage_root: H256::from_uint(&U256::from(3333)),
code_hash: H256::from_uint(&U256::from(4444)),
};
let account_rlp = rlp::encode(&account);
let leaf = PartialTrie::Leaf {
2022-10-11 20:15:33 -06:00
nibbles: nibbles(0xABC),
2022-10-06 16:05:28 -07:00
value: account_rlp.to_vec(),
2022-10-11 20:15:33 -06:00
}
.into();
let mut children = std::array::from_fn(|_| PartialTrie::Empty.into());
children[5] = PartialTrie::Branch {
2022-10-11 08:46:40 -07:00
children: children.clone(),
value: vec![],
2022-10-11 20:15:33 -06:00
}
.into();
children[3] = leaf;
2022-10-06 16:05:28 -07:00
let state_trie = PartialTrie::Branch {
children,
value: vec![],
};
let trie_inputs = TrieInputs {
state_trie,
transactions_trie: Default::default(),
receipts_trie: Default::default(),
storage_tries: vec![],
};
test_state_trie(trie_inputs)
}
fn test_state_trie(trie_inputs: TrieInputs) -> Result<()> {
2022-10-01 21:55:47 -07:00
let load_all_mpts = KERNEL.global_labels["load_all_mpts"];
let mpt_hash_state_trie = KERNEL.global_labels["mpt_hash_state_trie"];
2022-10-06 16:05:28 -07:00
let initial_stack = vec![0xDEADBEEFu32.into()];
2022-10-01 21:55:47 -07:00
let mut interpreter = Interpreter::new_with_kernel(load_all_mpts, initial_stack);
interpreter.generation_state.mpt_prover_inputs = all_mpt_prover_inputs_reversed(&trie_inputs);
interpreter.run()?;
assert_eq!(interpreter.stack(), vec![]);
// Now, execute mpt_hash_state_trie.
interpreter.offset = mpt_hash_state_trie;
interpreter.push(0xDEADBEEFu32.into());
interpreter.run()?;
2022-10-06 16:05:28 -07:00
assert_eq!(
interpreter.stack().len(),
1,
"Expected 1 item on stack, found {:?}",
interpreter.stack()
);
2022-10-01 21:55:47 -07:00
let hash = H256::from_uint(&interpreter.stack()[0]);
2022-10-06 16:05:28 -07:00
let expected_state_trie_hash = trie_inputs.state_trie.calc_hash();
assert_eq!(hash, expected_state_trie_hash);
2022-10-01 21:55:47 -07:00
Ok(())
}