mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-05 23:33:07 +00:00
Working with random address and code
This commit is contained in:
parent
8b1152c3b3
commit
4c0b3b6079
@ -3,7 +3,10 @@ use std::str::FromStr;
|
|||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use eth_trie_utils::partial_trie::PartialTrie;
|
use eth_trie_utils::partial_trie::PartialTrie;
|
||||||
use ethereum_types::{BigEndianHash, H256, U256};
|
use ethereum_types::{Address, BigEndianHash, H256, U256};
|
||||||
|
use hex_literal::hex;
|
||||||
|
use keccak_hash::keccak;
|
||||||
|
use rand::{thread_rng, Rng};
|
||||||
|
|
||||||
use crate::cpu::kernel::aggregator::{combined_kernel, KERNEL};
|
use crate::cpu::kernel::aggregator::{combined_kernel, KERNEL};
|
||||||
use crate::cpu::kernel::constants::global_metadata::GlobalMetadata;
|
use crate::cpu::kernel::constants::global_metadata::GlobalMetadata;
|
||||||
@ -12,43 +15,47 @@ use crate::cpu::kernel::tests::mpt::{extension_to_leaf, nibbles_64};
|
|||||||
use crate::generation::mpt::{all_mpt_prover_inputs_reversed, AccountRlp};
|
use crate::generation::mpt::{all_mpt_prover_inputs_reversed, AccountRlp};
|
||||||
use crate::generation::TrieInputs;
|
use crate::generation::TrieInputs;
|
||||||
|
|
||||||
fn test_account_1() -> AccountRlp {
|
fn test_account(code: &[u8]) -> AccountRlp {
|
||||||
AccountRlp {
|
AccountRlp {
|
||||||
nonce: U256::from(1111),
|
nonce: U256::from(1111),
|
||||||
balance: U256::from(2222),
|
balance: U256::from(2222),
|
||||||
storage_root: PartialTrie::Empty.calc_hash(),
|
storage_root: PartialTrie::Empty.calc_hash(),
|
||||||
code_hash: H256::from_str(
|
code_hash: keccak(code),
|
||||||
"2636a8beb2c41b8ccafa9a55a5a5e333892a83b491df3a67d2768946a9f9c6dc",
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn test_account_1_rlp() -> Vec<u8> {
|
fn random_code() -> Vec<u8> {
|
||||||
rlp::encode(&test_account_1()).to_vec()
|
let mut rng = thread_rng();
|
||||||
|
let num_bytes = rng.gen_range(0..10000);
|
||||||
|
(0..num_bytes).map(|_| rng.gen()).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
fn test_account_rlp(code: &[u8]) -> Vec<u8> {
|
||||||
fn test_extcodecopy() -> Result<()> {
|
rlp::encode(&test_account(code)).to_vec()
|
||||||
let state_trie: PartialTrie = Default::default();
|
}
|
||||||
let trie_inputs = Default::default();
|
|
||||||
let account = test_account_1();
|
|
||||||
|
|
||||||
|
// Stolen from `tests/mpt/insert.rs`
|
||||||
|
fn prepare_interpreter(
|
||||||
|
interpreter: &mut Interpreter,
|
||||||
|
address: Address,
|
||||||
|
account: &AccountRlp,
|
||||||
|
) -> Result<()> {
|
||||||
let load_all_mpts = KERNEL.global_labels["load_all_mpts"];
|
let load_all_mpts = KERNEL.global_labels["load_all_mpts"];
|
||||||
let mpt_insert_state_trie = KERNEL.global_labels["mpt_insert_state_trie"];
|
let mpt_insert_state_trie = KERNEL.global_labels["mpt_insert_state_trie"];
|
||||||
let mpt_hash_state_trie = KERNEL.global_labels["mpt_hash_state_trie"];
|
let mpt_hash_state_trie = KERNEL.global_labels["mpt_hash_state_trie"];
|
||||||
// let extcodecopy = KERNEL.global_labels["extcodecopy"];
|
let state_trie: PartialTrie = Default::default();
|
||||||
let extcodesize = KERNEL.global_labels["extcodesize"];
|
let trie_inputs = Default::default();
|
||||||
|
|
||||||
|
interpreter.offset = load_all_mpts;
|
||||||
|
interpreter.push(0xDEADBEEFu32.into());
|
||||||
|
|
||||||
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.generation_state.mpt_prover_inputs = all_mpt_prover_inputs_reversed(&trie_inputs);
|
||||||
interpreter.run()?;
|
interpreter.run()?;
|
||||||
assert_eq!(interpreter.stack(), vec![]);
|
assert_eq!(interpreter.stack(), vec![]);
|
||||||
|
|
||||||
let k = nibbles_64(U256::from_str(
|
let k = nibbles_64(U256::from_big_endian(
|
||||||
"5380c7b7ae81a58eb98d9c78de4a1fd7fd9535fc953ed2be602daaa41767312a",
|
keccak(address.to_fixed_bytes()).as_bytes(),
|
||||||
)?);
|
));
|
||||||
// Next, execute mpt_insert_state_trie.
|
// Next, execute mpt_insert_state_trie.
|
||||||
interpreter.offset = mpt_insert_state_trie;
|
interpreter.offset = mpt_insert_state_trie;
|
||||||
let trie_data = interpreter.get_trie_data_mut();
|
let trie_data = interpreter.get_trie_data_mut();
|
||||||
@ -71,7 +78,6 @@ fn test_extcodecopy() -> Result<()> {
|
|||||||
interpreter.push(value_ptr.into()); // value_ptr
|
interpreter.push(value_ptr.into()); // value_ptr
|
||||||
interpreter.push(k.packed); // key
|
interpreter.push(k.packed); // key
|
||||||
|
|
||||||
dbg!(interpreter.stack());
|
|
||||||
interpreter.run()?;
|
interpreter.run()?;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
interpreter.stack().len(),
|
interpreter.stack().len(),
|
||||||
@ -93,21 +99,33 @@ fn test_extcodecopy() -> Result<()> {
|
|||||||
);
|
);
|
||||||
let hash = H256::from_uint(&interpreter.stack()[0]);
|
let hash = H256::from_uint(&interpreter.stack()[0]);
|
||||||
|
|
||||||
let updated_trie = state_trie.insert(k, rlp::encode(&account).to_vec());
|
let updated_trie = state_trie.insert(k, rlp::encode(account).to_vec());
|
||||||
let expected_state_trie_hash = updated_trie.calc_hash();
|
let expected_state_trie_hash = updated_trie.calc_hash();
|
||||||
assert_eq!(hash, expected_state_trie_hash);
|
assert_eq!(hash, expected_state_trie_hash);
|
||||||
|
|
||||||
// let initial_stack = vec![0.into()];
|
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"];
|
||||||
|
|
||||||
interpreter.pop();
|
interpreter.pop();
|
||||||
interpreter.push(0xDEADBEEFu64.into());
|
interpreter.push(0xDEADBEEFu32.into());
|
||||||
interpreter.push(U256::zero());
|
interpreter.push(U256::from_big_endian(address.as_bytes()));
|
||||||
interpreter.offset = extcodesize;
|
interpreter.offset = extcodesize;
|
||||||
interpreter.generation_state.inputs.contract_code = HashMap::from([(
|
interpreter.generation_state.inputs.contract_code =
|
||||||
H256::from_str("2636a8beb2c41b8ccafa9a55a5a5e333892a83b491df3a67d2768946a9f9c6dc")?,
|
HashMap::from([(keccak(&code), code.clone())]);
|
||||||
vec![0x13, 0x37],
|
|
||||||
)]);
|
|
||||||
interpreter.run()?;
|
interpreter.run()?;
|
||||||
assert_eq!(interpreter.stack(), vec![2.into()]);
|
assert_eq!(interpreter.stack(), vec![code.len().into()]);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user