240 lines
7.5 KiB
Rust
Raw Normal View History

2022-09-22 20:09:48 -07:00
use anyhow::Result;
use ethereum_types::{BigEndianHash, H256, U256};
2022-09-22 20:09:48 -07:00
2022-10-31 15:15:10 -06:00
use crate::cpu::kernel::aggregator::KERNEL;
2022-10-03 12:08:29 -07:00
use crate::cpu::kernel::constants::global_metadata::GlobalMetadata;
2022-09-22 20:09:48 -07:00
use crate::cpu::kernel::constants::trie_type::PartialTrieType;
use crate::cpu::kernel::interpreter::Interpreter;
2022-10-08 15:09:07 -07:00
use crate::cpu::kernel::tests::mpt::{extension_to_leaf, test_account_1, test_account_1_rlp};
use crate::generation::mpt::all_mpt_prover_inputs_reversed;
2022-09-22 20:09:48 -07:00
use crate::generation::TrieInputs;
2023-03-27 17:30:11 -06:00
use crate::Node;
2022-09-22 20:09:48 -07:00
#[test]
2022-10-08 15:09:07 -07:00
fn load_all_mpts_empty() -> Result<()> {
let trie_inputs = TrieInputs {
state_trie: Default::default(),
transactions_trie: Default::default(),
receipts_trie: Default::default(),
storage_tries: vec![],
};
let load_all_mpts = KERNEL.global_labels["load_all_mpts"];
let initial_stack = vec![0xDEADBEEFu32.into()];
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![]);
assert_eq!(interpreter.get_trie_data(), vec![]);
assert_eq!(
interpreter.get_global_metadata_field(GlobalMetadata::StateTrieRoot),
0.into()
);
assert_eq!(
interpreter.get_global_metadata_field(GlobalMetadata::TransactionTrieRoot),
0.into()
);
assert_eq!(
interpreter.get_global_metadata_field(GlobalMetadata::ReceiptTrieRoot),
0.into()
);
Ok(())
}
#[test]
fn load_all_mpts_leaf() -> Result<()> {
let trie_inputs = TrieInputs {
2023-03-27 17:30:11 -06:00
state_trie: Node::Leaf {
2022-10-31 15:15:10 -06:00
nibbles: 0xABC_u64.into(),
2022-10-08 15:09:07 -07:00
value: test_account_1_rlp(),
2023-03-27 17:30:11 -06:00
}
.into(),
2022-10-08 15:09:07 -07:00
transactions_trie: Default::default(),
receipts_trie: Default::default(),
storage_tries: vec![],
};
let load_all_mpts = KERNEL.global_labels["load_all_mpts"];
let initial_stack = vec![0xDEADBEEFu32.into()];
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![]);
let type_leaf = U256::from(PartialTrieType::Leaf as u32);
assert_eq!(
interpreter.get_trie_data(),
vec![
0.into(),
type_leaf,
3.into(),
0xABC.into(),
5.into(), // value ptr
test_account_1().nonce,
test_account_1().balance,
9.into(), // pointer to storage trie root
2022-10-08 15:09:07 -07:00
test_account_1().code_hash.into_uint(),
// These last two elements encode the storage trie, which is a hash node.
(PartialTrieType::Hash as u32).into(),
test_account_1().storage_root.into_uint(),
2022-10-08 15:09:07 -07:00
]
);
assert_eq!(
interpreter.get_global_metadata_field(GlobalMetadata::TransactionTrieRoot),
0.into()
);
assert_eq!(
interpreter.get_global_metadata_field(GlobalMetadata::ReceiptTrieRoot),
0.into()
);
Ok(())
}
#[test]
fn load_all_mpts_hash() -> Result<()> {
let hash = H256::random();
let trie_inputs = TrieInputs {
2023-03-27 17:30:11 -06:00
state_trie: Node::Hash(hash).into(),
transactions_trie: Default::default(),
receipts_trie: Default::default(),
storage_tries: vec![],
};
let load_all_mpts = KERNEL.global_labels["load_all_mpts"];
let initial_stack = vec![0xDEADBEEFu32.into()];
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![]);
let type_hash = U256::from(PartialTrieType::Hash as u32);
assert_eq!(
interpreter.get_trie_data(),
vec![0.into(), type_hash, hash.into_uint(),]
);
assert_eq!(
interpreter.get_global_metadata_field(GlobalMetadata::TransactionTrieRoot),
0.into()
);
2022-10-08 15:09:07 -07:00
assert_eq!(
interpreter.get_global_metadata_field(GlobalMetadata::ReceiptTrieRoot),
0.into()
2022-10-08 15:09:07 -07:00
);
Ok(())
}
#[test]
fn load_all_mpts_empty_branch() -> Result<()> {
2023-03-27 17:30:11 -06:00
let children = core::array::from_fn(|_| Node::Empty.into());
let state_trie = Node::Branch {
2022-10-08 15:09:07 -07:00
children,
value: vec![],
2023-03-27 17:30:11 -06:00
}
.into();
2022-10-08 15:09:07 -07:00
let trie_inputs = TrieInputs {
state_trie,
transactions_trie: Default::default(),
receipts_trie: Default::default(),
storage_tries: vec![],
2022-10-04 15:12:44 -07:00
};
2022-09-22 20:09:48 -07:00
2022-10-08 15:09:07 -07:00
let load_all_mpts = KERNEL.global_labels["load_all_mpts"];
let initial_stack = vec![0xDEADBEEFu32.into()];
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![]);
let type_branch = U256::from(PartialTrieType::Branch as u32);
assert_eq!(
interpreter.get_trie_data(),
vec![
0.into(), // First address is unused, so that 0 can be treated as a null pointer.
type_branch,
0.into(), // child 0
0.into(), // ...
0.into(),
0.into(),
0.into(),
0.into(),
0.into(),
0.into(),
0.into(),
0.into(),
0.into(),
0.into(),
0.into(),
0.into(),
0.into(),
0.into(), // child 16
0.into(), // value_ptr
]
);
assert_eq!(
interpreter.get_global_metadata_field(GlobalMetadata::TransactionTrieRoot),
0.into()
);
assert_eq!(
interpreter.get_global_metadata_field(GlobalMetadata::ReceiptTrieRoot),
0.into()
);
Ok(())
}
#[test]
fn load_all_mpts_ext_to_leaf() -> Result<()> {
2022-09-22 20:09:48 -07:00
let trie_inputs = TrieInputs {
2022-10-08 15:09:07 -07:00
state_trie: extension_to_leaf(test_account_1_rlp()),
2022-09-22 20:09:48 -07:00
transactions_trie: Default::default(),
receipts_trie: Default::default(),
storage_tries: vec![],
};
let load_all_mpts = KERNEL.global_labels["load_all_mpts"];
2022-10-08 15:09:07 -07:00
let initial_stack = vec![0xDEADBEEFu32.into()];
2022-09-22 20:09:48 -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![]);
2022-09-27 10:49:13 -07:00
let type_extension = U256::from(PartialTrieType::Extension as u32);
2022-09-22 20:09:48 -07:00
let type_leaf = U256::from(PartialTrieType::Leaf as u32);
assert_eq!(
interpreter.get_trie_data(),
vec![
2022-09-27 10:49:13 -07:00
0.into(), // First address is unused, so that 0 can be treated as a null pointer.
type_extension,
3.into(), // 3 nibbles
0xABC.into(), // key part
5.into(), // Pointer to the leaf node immediately below.
2022-09-22 20:09:48 -07:00
type_leaf,
2022-09-27 10:49:13 -07:00
3.into(), // 3 nibbles
0xDEF.into(), // key part
2022-10-08 13:51:52 -07:00
9.into(), // value pointer
2022-10-08 15:09:07 -07:00
test_account_1().nonce,
test_account_1().balance,
13.into(), // pointer to storage trie root
2022-10-08 15:09:07 -07:00
test_account_1().code_hash.into_uint(),
// These last two elements encode the storage trie, which is a hash node.
(PartialTrieType::Hash as u32).into(),
test_account_1().storage_root.into_uint(),
2022-09-22 20:09:48 -07:00
]
);
Ok(())
}