Skip to content

Commit

Permalink
Refactor types
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashtare committed Nov 7, 2024
1 parent 5b893cd commit 8db31fe
Show file tree
Hide file tree
Showing 29 changed files with 407 additions and 495 deletions.
82 changes: 37 additions & 45 deletions evm_arithmetization/benches/fibonacci_25m_gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use ethereum_types::{Address, BigEndianHash, H256, U256};
use evm_arithmetization::cpu::kernel::aggregator::KERNEL;
use evm_arithmetization::cpu::kernel::opcodes::{get_opcode, get_push_opcode};
use evm_arithmetization::generation::mpt::{
get_h256_from_code_hash, get_u256_from_code_hash, AccountRlp, CodeHashType, EitherRlp,
LegacyReceiptRlp, MptAccountRlp,
get_h256_from_code_hash, get_u256_from_code_hash, Account, CodeHashType, EitherAccount,
LegacyReceiptRlp, MptAccount,
};
use evm_arithmetization::generation::{GenerationInputs, TrieInputs};
use evm_arithmetization::proof::{BlockHashes, BlockMetadata, TrieRoots};
Expand Down Expand Up @@ -96,14 +96,14 @@ fn prepare_setup() -> anyhow::Result<GenerationInputs<F>> {
let empty_trie_root = HashedPartialTrie::from(Node::Empty).hash();

let sender_account_before = if cfg!(feature = "cdk_erigon") {
Either::Right(SmtAccountRlp {
Either::Right(SmtAccount {
nonce: 169.into(),
balance: U256::from_dec_str("999999999998417410153631615")?,
code_hash: hash_bytecode_u256(vec![]),
code_length: 0.into(),
})
} else {
Either::Left(MptAccountRlp {
Either::Left(MptAccount {
nonce: 169.into(),
balance: U256::from_dec_str("999999999998417410153631615")?,
storage_root: empty_trie_root,
Expand All @@ -112,25 +112,21 @@ fn prepare_setup() -> anyhow::Result<GenerationInputs<F>> {
};

let to_account_before = if cfg!(feature = "cdk_erigon") {
EitherRlp {
account_rlp: Either::Right(SmtAccountRlp {
nonce: 1.into(),
balance: 0.into(),
code_hash: get_u256_from_code_hash(code_hash.clone())
.expect("In cdk_erigon, the code_hash is a U256"),
code_length: code.len().into(),
}),
}
EitherAccount(Either::Right(SmtAccount {
nonce: 1.into(),
balance: 0.into(),
code_hash: get_u256_from_code_hash(code_hash.clone())
.expect("In cdk_erigon, the code_hash is a U256"),
code_length: code.len().into(),
}))
} else {
EitherRlp {
account_rlp: Either::Left(MptAccountRlp {
nonce: 1.into(),
balance: 0.into(),
storage_root: empty_trie_root,
code_hash: get_h256_from_code_hash(code_hash.clone())
.expect("In eth_mainnet, the code_hash is a H256"),
}),
}
EitherAccount(Either::Left(MptAccount {
nonce: 1.into(),
balance: 0.into(),
storage_root: empty_trie_root,
code_hash: get_h256_from_code_hash(code_hash.clone())
.expect("In eth_mainnet, the code_hash is a H256"),
}))
};

let mut state_trie_before = StateWorld::default();
Expand Down Expand Up @@ -158,7 +154,7 @@ fn prepare_setup() -> anyhow::Result<GenerationInputs<F>> {
{
let sender_account_before_smt =
sender_account_before.expect_right("The sender account is an SMT.");
let to_account_before_smt = to_account_before.as_smt_account_rlp();
let to_account_before_smt = to_account_before.as_smt_account();
set_account(
&mut state_trie_before,
H160(sender),
Expand Down Expand Up @@ -214,27 +210,23 @@ fn prepare_setup() -> anyhow::Result<GenerationInputs<F>> {
let sender_account_after = if cfg!(feature = "cdk_erigon") {
let sender_account_before_smt =
sender_account_before.expect_right("cdk_erigon expects SMTs.");
EitherRlp {
account_rlp: Either::Right(SmtAccountRlp {
balance: sender_account_before_smt.get_balance()
- value
- gas_used * block_metadata.block_base_fee,
nonce: sender_account_before_smt.get_nonce() + 1,
..sender_account_before_smt
}),
}
EitherAccount(Either::Right(SmtAccount {
balance: sender_account_before_smt.get_balance()
- value
- gas_used * block_metadata.block_base_fee,
nonce: sender_account_before_smt.get_nonce() + 1,
..sender_account_before_smt
}))
} else {
let sender_account_before_mpt =
sender_account_before.expect_left("eth_mainnet expects MPTs.");
EitherRlp {
account_rlp: Either::Left(MptAccountRlp {
balance: sender_account_before_mpt.get_balance()
- value
- gas_used * block_metadata.block_base_fee,
nonce: sender_account_before_mpt.get_nonce() + 1,
..sender_account_before_mpt
}),
}
EitherAccount(Either::Left(MptAccount {
balance: sender_account_before_mpt.get_balance()
- value
- gas_used * block_metadata.block_base_fee,
nonce: sender_account_before_mpt.get_nonce() + 1,
..sender_account_before_mpt
}))
};
let to_account_after = &to_account_before;

Expand Down Expand Up @@ -264,8 +256,8 @@ fn prepare_setup() -> anyhow::Result<GenerationInputs<F>> {

#[cfg(feature = "cdk_erigon")]
{
let sender_account_after_smt = sender_account_after.as_smt_account_rlp();
let to_account_after_smt = to_account_after.as_smt_account_rlp();
let sender_account_after_smt = sender_account_after.as_smt_account();
let to_account_after_smt = to_account_after.as_smt_account();
set_account(
&mut expected_state_trie_after,
H160(sender),
Expand Down Expand Up @@ -348,10 +340,10 @@ fn init_logger() {
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

use evm_arithmetization::generation::mpt::SmtAccountRlp;
use evm_arithmetization::generation::mpt::SmtAccount;

#[cfg(feature = "cdk_erigon")]
fn set_account(world: &mut StateWorld, addr: Address, account: &SmtAccountRlp, code: &[u8]) {
fn set_account(world: &mut StateWorld, addr: Address, account: &SmtAccount, code: &[u8]) {
use evm_arithmetization::world::world::World;

let key = key_balance(addr);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Pre-stack: status, leftover_gas, prev_cum_gas, txn_nb, num_nibbles, retdest
// Post stack: new_cum_gas, txn_nb
// A receipt is stored in MPT_TRIE_DATA as:
// A receipt is stored in TRIE_DATA as:
// [payload_len, status, cum_gas_used, bloom, logs_payload_len, num_logs, [logs]]
//
// In this function, we:
// - compute cum_gas,
// - check if the transaction failed and set number of logs to 0 if it is the case,
// - compute the bloom filter,
// - write the receipt in MPT_TRIE_DATA ,
// - write the receipt in TRIE_DATA ,
// - insert a new node in receipt_trie,
// - set the bloom filter back to 0
global process_receipt:
Expand Down Expand Up @@ -51,7 +51,7 @@ process_receipt_after_bloom:
%rlp_list_len
ADD
// stack: payload_len, status, new_cum_gas, txn_nb, new_cum_gas, txn_nb, num_nibbles, retdest
// Now we can write the receipt in MPT_TRIE_DATA.
// Now we can write the receipt in TRIE_DATA.
%get_trie_data_size
// stack: receipt_ptr, payload_len, status, new_cum_gas, txn_nb, new_cum_gas, txn_nb, num_nibbles, retdest
// Write transaction type if necessary.
Expand Down Expand Up @@ -103,7 +103,7 @@ process_receipt_after_type:
DUP1 %append_to_trie_data
PUSH 0

// Each log is written in MPT_TRIE_DATA as:
// Each log is written in TRIE_DATA as:
// [payload_len, address, num_topics, [topics], data_len, [data]].
process_receipt_logs_loop:
// stack: i, num_logs, receipt_ptr, txn_nb, new_cum_gas, txn_nb, num_nibbles, retdest
Expand Down
8 changes: 4 additions & 4 deletions evm_arithmetization/src/cpu/kernel/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ const LINKED_LISTS_CONSTANTS: [(&str, u16); 8] = [
pub mod cancun_constants {

use super::*;
use crate::generation::mpt::MptAccountRlp;
use crate::generation::mpt::MptAccount;

pub const BLOB_BASE_FEE_UPDATE_FRACTION: U256 = U256([0x32f0ed, 0, 0, 0]);

Expand Down Expand Up @@ -477,7 +477,7 @@ pub mod cancun_constants {
pub const BEACON_ROOTS_CONTRACT_CODE_HASH: [u8; 32] =
hex!("f57acd40259872606d76197ef052f3d35588dadf919ee1f0e3cb9b62d3f4b02c");

pub const BEACON_ROOTS_ACCOUNT: MptAccountRlp = MptAccountRlp {
pub const BEACON_ROOTS_ACCOUNT: MptAccount = MptAccount {
nonce: U256::zero(),
balance: U256::zero(),
// Storage root for this account at genesis.
Expand All @@ -498,7 +498,7 @@ pub mod cancun_constants {

pub mod global_exit_root {
use super::*;
use crate::generation::mpt::MptAccountRlp;
use crate::generation::mpt::MptAccount;

/// Taken from <https://github.com/0xPolygonHermez/cdk-erigon/blob/61f0b6912055c73f6879ea7e9b5bac22ea5fc85c/zk/utils/global_exit_root.go#L16>.
pub const GLOBAL_EXIT_ROOT_MANAGER_L2: (&str, [u8; 20]) = (
Expand Down Expand Up @@ -540,7 +540,7 @@ pub mod global_exit_root {
pub const GLOBAL_EXIT_ROOT_CONTRACT_CODE_HASH: [u8; 32] =
hex!("6bec2bf64f7e824109f6ed55f77dd7665801d6195e461666ad6a5342a9f6daf5");

pub const GLOBAL_EXIT_ROOT_ACCOUNT: MptAccountRlp = MptAccountRlp {
pub const GLOBAL_EXIT_ROOT_ACCOUNT: MptAccount = MptAccount {
nonce: U256::zero(),
balance: U256::zero(),
// Empty storage root
Expand Down
80 changes: 35 additions & 45 deletions evm_arithmetization/src/cpu/kernel/tests/account_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::cpu::kernel::interpreter::Interpreter;
use crate::cpu::kernel::tests::mpt::nibbles_64;
#[cfg(not(feature = "cdk_erigon"))]
use crate::generation::mpt::load_linked_lists_and_txn_and_receipt_mpts;
use crate::generation::mpt::{load_state_mpt, AccountRlp, EitherRlp, MptAccountRlp, SmtAccountRlp};
use crate::generation::mpt::{load_state_mpt, Account, EitherAccount, MptAccount, SmtAccount};
use crate::generation::TrieInputs;
use crate::memory::segments::Segment;
use crate::util::h2u;
Expand Down Expand Up @@ -157,7 +157,7 @@ pub(crate) fn initialize_mpts<F: RichField>(
pub(crate) fn prepare_interpreter<F: RichField>(
interpreter: &mut Interpreter<F>,
address: Address,
account: &EitherRlp,
account: &EitherAccount,
) -> Result<()> {
let mpt_insert_state_trie = KERNEL.global_labels["mpt_insert_state_trie"];
let check_state_trie = KERNEL.global_labels["check_final_state_trie"];
Expand Down Expand Up @@ -267,25 +267,21 @@ pub(crate) fn prepare_interpreter<F: RichField>(
}

// Test account with a given code hash.
fn test_account(code: &[u8]) -> EitherRlp {
fn test_account(code: &[u8]) -> EitherAccount {
if cfg!(feature = "eth_mainnet") {
EitherRlp {
account_rlp: Either::Left(MptAccountRlp {
nonce: U256::from(1111),
balance: U256::from(2222),
storage_root: HashedPartialTrie::from(Node::Empty).hash(),
code_hash: keccak(code),
}),
}
EitherAccount(Either::Left(MptAccount {
nonce: U256::from(1111),
balance: U256::from(2222),
storage_root: HashedPartialTrie::from(Node::Empty).hash(),
code_hash: keccak(code),
}))
} else {
EitherRlp {
account_rlp: Either::Right(SmtAccountRlp {
nonce: U256::from(1111),
balance: U256::from(2222),
code_hash: hash_bytecode_u256(code.to_vec()),
code_length: code.len().into(),
}),
}
EitherAccount(Either::Right(SmtAccount {
nonce: U256::from(1111),
balance: U256::from(2222),
code_hash: hash_bytecode_u256(code.to_vec()),
code_length: code.len().into(),
}))
}
}

Expand Down Expand Up @@ -519,13 +515,11 @@ fn sstore() -> Result<()> {
let code = [0x60, 0x01, 0x60, 0x01, 0x01, 0x60, 0x00, 0x55, 0x00];
let code_hash = keccak(code);

let account_before = EitherRlp {
account_rlp: Either::Left(MptAccountRlp {
balance: 0x0de0b6b3a7640000u64.into(),
code_hash,
..MptAccountRlp::default()
}),
};
let account_before = EitherAccount(Either::Left(MptAccount {
balance: 0x0de0b6b3a7640000u64.into(),
code_hash,
..MptAccount::default()
}));

let mut state_trie_before = HashedPartialTrie::from(Node::Empty);

Expand Down Expand Up @@ -561,18 +555,16 @@ fn sstore() -> Result<()> {

// The code should have added an element to the storage of `to_account`. We run
// `mpt_hash_state_trie` to check that.
let account_after = EitherRlp {
account_rlp: Either::Left(MptAccountRlp {
balance: 0x0de0b6b3a7640000u64.into(),
code_hash,
storage_root: HashedPartialTrie::from(Node::Leaf {
nibbles: Nibbles::from_h256_be(keccak([0u8; 32])),
value: vec![2],
})
.hash(),
..MptAccountRlp::default()
}),
};
let account_after = EitherAccount(Either::Left(MptAccount {
balance: 0x0de0b6b3a7640000u64.into(),
code_hash,
storage_root: HashedPartialTrie::from(Node::Leaf {
nibbles: Nibbles::from_h256_be(keccak([0u8; 32])),
value: vec![2],
})
.hash(),
..MptAccount::default()
}));

let mut expected_state_trie_after = HashedPartialTrie::from(Node::Empty);
expected_state_trie_after.insert(addr_nibbles, account_after.rlp_encode().to_vec())?;
Expand Down Expand Up @@ -625,13 +617,11 @@ fn sload() -> Result<()> {
];
let code_hash = keccak(code);

let account_before = EitherRlp {
account_rlp: Either::Left(MptAccountRlp {
balance: 0x0de0b6b3a7640000u64.into(),
code_hash,
..MptAccountRlp::default()
}),
};
let account_before = EitherAccount(Either::Left(MptAccount {
balance: 0x0de0b6b3a7640000u64.into(),
code_hash,
..MptAccount::default()
}));

let mut state_trie_before = HashedPartialTrie::from(Node::Empty);

Expand Down
Loading

0 comments on commit 8db31fe

Please sign in to comment.