Skip to content

Commit

Permalink
Tweak naming and move to common crate (0xPolygonZero#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashtare authored Sep 11, 2024
1 parent 188397d commit b7bee72
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ethereum_types::H256;
use ethereum_types::{H256, U256};

/// The hash value of an account empty EVM code.
/// 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470
Expand All @@ -14,6 +14,26 @@ pub const EMPTY_TRIE_HASH: H256 = H256([
108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33,
]);

/// Converts an amount in `ETH` to `wei` units.
pub fn eth_to_wei(eth: U256) -> U256 {
// 1 ether = 10^18 wei.
eth * U256::from(10).pow(18.into())
}

/// Converts an amount in `gwei` to `wei` units.
/// This also works for converting `ETH` to `gwei`.
pub fn gwei_to_wei(eth: U256) -> U256 {
// 1 ether = 10^9 gwei = 10^18 wei.
eth * U256::from(10).pow(9.into())
}

#[test]
fn test_eth_conversion() {
assert_eq!(
eth_to_wei(U256::one()),
gwei_to_wei(gwei_to_wei(U256::one()))
);
}
#[test]
fn test_empty_code_hash() {
assert_eq!(EMPTY_CODE_HASH, keccak_hash::keccak([]));
Expand Down
1 change: 1 addition & 0 deletions evm_arithmetization/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ serde-big-array = { workspace = true }
mpt_trie = { workspace = true }
smt_trie = { workspace = true, optional = true }
zk_evm_proc_macro = { workspace = true }
zk_evm_common = { workspace = true }

[dev-dependencies]
criterion = { workspace = true }
Expand Down
6 changes: 0 additions & 6 deletions evm_arithmetization/src/testing_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,3 @@ pub fn scalable_contract_from_storage(storage_trie: &HashedPartialTrie) -> Accou
..Default::default()
}
}

/// Converts an amount in `ETH` to `wei` units.
pub fn eth_to_wei(eth: U256) -> U256 {
// 1 ether = 10^18 wei.
eth * U256::from(10).pow(18.into())
}
3 changes: 2 additions & 1 deletion evm_arithmetization/tests/selfdestruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use evm_arithmetization::generation::{GenerationInputs, TrieInputs};
use evm_arithmetization::proof::{BlockHashes, BlockMetadata, TrieRoots};
use evm_arithmetization::prover::testing::prove_all_segments;
use evm_arithmetization::testing_utils::{
beacon_roots_account_nibbles, beacon_roots_contract_from_storage, eth_to_wei, init_logger,
beacon_roots_account_nibbles, beacon_roots_contract_from_storage, init_logger,
preinitialized_state_and_storage_tries, update_beacon_roots_account_storage,
};
use evm_arithmetization::verifier::testing::verify_all_proofs;
Expand All @@ -21,6 +21,7 @@ use mpt_trie::partial_trie::{HashedPartialTrie, PartialTrie};
use plonky2::field::goldilocks_field::GoldilocksField;
use plonky2::plonk::config::KeccakGoldilocksConfig;
use plonky2::util::timing::TimingTree;
use zk_evm_common::eth_to_wei;

type F = GoldilocksField;
const D: usize = 2;
Expand Down
3 changes: 2 additions & 1 deletion evm_arithmetization/tests/simple_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use evm_arithmetization::generation::{GenerationInputs, TrieInputs};
use evm_arithmetization::proof::{BlockHashes, BlockMetadata, TrieRoots};
use evm_arithmetization::prover::testing::prove_all_segments;
use evm_arithmetization::testing_utils::{
beacon_roots_account_nibbles, beacon_roots_contract_from_storage, eth_to_wei, init_logger,
beacon_roots_account_nibbles, beacon_roots_contract_from_storage, init_logger,
preinitialized_state_and_storage_tries, update_beacon_roots_account_storage,
};
use evm_arithmetization::verifier::testing::verify_all_proofs;
Expand All @@ -22,6 +22,7 @@ use mpt_trie::partial_trie::{HashedPartialTrie, PartialTrie};
use plonky2::field::goldilocks_field::GoldilocksField;
use plonky2::plonk::config::KeccakGoldilocksConfig;
use plonky2::util::timing::TimingTree;
use zk_evm_common::eth_to_wei;

type F = GoldilocksField;
const D: usize = 2;
Expand Down
8 changes: 2 additions & 6 deletions trace_decoder/src/decoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use mpt_trie::{
trie_ops::TrieOpError,
utils::{IntoTrieKey as _, TriePath},
};
use zk_evm_common::gwei_to_wei;

use crate::{
hash,
Expand Down Expand Up @@ -442,7 +443,7 @@ fn add_withdrawals_to_txns(
) -> anyhow::Result<()> {
// Scale withdrawals amounts.
for (_addr, amt) in withdrawals.iter_mut() {
*amt = eth_to_gwei(*amt)
*amt = gwei_to_wei(*amt)
}

let withdrawals_with_hashed_addrs_iter = || {
Expand Down Expand Up @@ -692,11 +693,6 @@ fn create_trie_subset_wrapped(
.context(format!("missing keys when creating {}", trie_type))
}

fn eth_to_gwei(eth: U256) -> U256 {
// 1 ether = 10^9 gwei.
eth * U256::from(10).pow(9.into())
}

// This is just `rlp(0)`.
const ZERO_STORAGE_SLOT_VAL_RLPED: [u8; 1] = [128];

Expand Down

0 comments on commit b7bee72

Please sign in to comment.