From 5be5b00a4cd2a4b8c10045943dd22c10c9288c9e Mon Sep 17 00:00:00 2001 From: Iaroslav Mazur Date: Tue, 28 Nov 2023 14:47:10 +0200 Subject: [PATCH] fix: resolve build issues in tests --- bins/revm-test/src/bin/transfer.rs | 2 +- bins/revme/src/cmd/statetest/merkle_trie.rs | 4 ++- bins/revme/src/cmd/statetest/runner.rs | 4 +-- crates/primitives/src/state.rs | 7 ++--- crates/revm/benches/bench.rs | 3 +- crates/revm/src/evm_impl.rs | 25 +++++++++-------- crates/revm/src/handler/optimism.rs | 31 +++++++++++---------- crates/revm/src/inspector/customprinter.rs | 2 +- examples/fork_ref_transact.rs | 2 +- 9 files changed, 43 insertions(+), 37 deletions(-) diff --git a/bins/revm-test/src/bin/transfer.rs b/bins/revm-test/src/bin/transfer.rs index ada13c56..230943dc 100644 --- a/bins/revm-test/src/bin/transfer.rs +++ b/bins/revm-test/src/bin/transfer.rs @@ -1,6 +1,6 @@ use revm::{ db::BenchmarkDB, - primitives::{Bytecode, TransactTo, U256}, + primitives::{Bytecode, TransactTo, B256, BASE_ASSET_ID, U256}, }; use std::time::{Duration, Instant}; extern crate alloc; diff --git a/bins/revme/src/cmd/statetest/merkle_trie.rs b/bins/revme/src/cmd/statetest/merkle_trie.rs index c869938b..5c2a5fcd 100644 --- a/bins/revme/src/cmd/statetest/merkle_trie.rs +++ b/bins/revme/src/cmd/statetest/merkle_trie.rs @@ -27,6 +27,8 @@ pub fn state_merkle_trie_root<'a>( #[derive(RlpEncodable, RlpMaxEncodedLen)] struct TrieAccount { nonce: u64, + + //TODO: keep track of all of the NA balances here, instead? balance: U256, root_hash: B256, code_hash: B256, @@ -36,7 +38,7 @@ impl TrieAccount { fn new(acc: &PlainAccount) -> Self { Self { nonce: acc.info.nonce, - balance: acc.info.balance, + balance: acc.info.get_base_balance(), root_hash: sec_trie_root::( acc.storage .iter() diff --git a/bins/revme/src/cmd/statetest/runner.rs b/bins/revme/src/cmd/statetest/runner.rs index 5bb350ab..99eb506d 100644 --- a/bins/revme/src/cmd/statetest/runner.rs +++ b/bins/revme/src/cmd/statetest/runner.rs @@ -8,7 +8,7 @@ use revm::{ interpreter::CreateScheme, primitives::{ address, b256, calc_excess_blob_gas, init_balances, keccak256, Bytecode, Env, HashMap, - SpecId, TransactTo, B256, U256, + SpecId, TransactTo, B256, BASE_ASSET_ID, U256, }, }; use std::{ @@ -217,7 +217,7 @@ pub fn execute_test_suite( .unwrap() .clone(); let base_transfer_value = unit.transaction.value[test.indexes.value]; - evm.env.tx.asset_values = Some(vec![( + env.tx.asset_values = Some(vec![( B256::from(BASE_ASSET_ID), U256::from(base_transfer_value), )]); diff --git a/crates/primitives/src/state.rs b/crates/primitives/src/state.rs index 7ec33b2f..a270b577 100644 --- a/crates/primitives/src/state.rs +++ b/crates/primitives/src/state.rs @@ -259,7 +259,7 @@ impl AccountInfo { /// /// An account is empty if the following conditions are met. /// - code hash is zero or set to the Keccak256 hash of the empty string `""` - /// - balance is zero + /// - the balances of the Account haven't been set /// - nonce is zero pub fn is_empty(&self) -> bool { let code_empty = self.code_hash == KECCAK_EMPTY || self.code_hash == B256::ZERO; @@ -380,11 +380,8 @@ mod tests { let mut account = Account::default(); assert!(account.is_empty()); - account.info.balance = U256::from(1); + account.info.set_base_balance(U256::from(1)); assert!(!account.is_empty()); - - account.info.balance = U256::ZERO; - assert!(account.is_empty()); } #[test] diff --git a/crates/revm/benches/bench.rs b/crates/revm/benches/bench.rs index 421e3c03..29c2d88f 100644 --- a/crates/revm/benches/bench.rs +++ b/crates/revm/benches/bench.rs @@ -5,7 +5,8 @@ use revm::{ db::BenchmarkDB, interpreter::{analysis::to_analysed, BytecodeLocked, Contract, DummyHost, Interpreter}, primitives::{ - address, bytes, hex, BerlinSpec, Bytecode, BytecodeState, Bytes, TransactTo, U256, + address, bytes, hex, BerlinSpec, Bytecode, BytecodeState, Bytes, TransactTo, B256, + BASE_ASSET_ID, U256, }, }; use revm_interpreter::{opcode::make_instruction_table, SharedMemory, EMPTY_SHARED_MEMORY}; diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index b70e7e8a..067c5f72 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -66,15 +66,16 @@ impl<'a, SPEC: Spec, DB: Database> EVMImpl<'a, SPEC, DB> { .load_account(tx_caller, db) .map_err(EVMError::Database)? .0; - if l1_cost.gt(&acc.info.balance) { + if l1_cost.gt(&acc.info.get_base_balance()) { return Err(EVMError::Transaction( InvalidTransactionReason::NotEnoughBaseAssetBalanceForTransferAndMaxFee { fee: Box::new(l1_cost), - balance: Box::new(acc.info.balance), + balance: Box::new(acc.info.get_base_balance()), }, )); } - acc.info.balance = acc.info.balance.saturating_sub(l1_cost); + acc.info + .set_base_balance(acc.info.get_base_balance().saturating_sub(l1_cost)); Ok(()) } @@ -93,7 +94,7 @@ impl<'a, SPEC: Spec, DB: Database> EVMImpl<'a, SPEC, DB> { .map_err(EVMError::Database)? .0 .info - .balance += U256::from(mint); + .increase_base_balance(U256::from(mint)); journal.checkpoint(); } Ok(()) @@ -741,7 +742,9 @@ mod tests { use super::*; use crate::db::InMemoryDB; - use crate::primitives::{specification::BedrockSpec, state::AccountInfo, SpecId}; + use crate::primitives::{ + specification::BedrockSpec, state::AccountInfo, state::Balances, SpecId, BASE_ASSET_ID, + }; #[test] fn test_commit_mint_value() { @@ -752,7 +755,7 @@ mod tests { caller, AccountInfo { nonce: 0, - balance: U256::from(100), + balances: Balances::from([(BASE_ASSET_ID, U256::from(100))]), code_hash: B256::ZERO, code: None, }, @@ -771,7 +774,7 @@ mod tests { // Check the account balance is updated. let (account, _) = journal.load_account(caller, &mut db).unwrap(); - assert_eq!(account.info.balance, U256::from(101)); + assert_eq!(account.info.get_base_balance(), U256::from(101)); // No mint value should be a no-op. assert!(EVMImpl::::commit_mint_value( @@ -782,7 +785,7 @@ mod tests { ) .is_ok(),); let (account, _) = journal.load_account(caller, &mut db).unwrap(); - assert_eq!(account.info.balance, U256::from(101)); + assert_eq!(account.info.get_base_balance(), U256::from(101)); } #[test] @@ -812,7 +815,7 @@ mod tests { caller, AccountInfo { nonce: 0, - balance: U256::from(100), + balances: Balances::from([(BASE_ASSET_ID, U256::from(100))]), code_hash: B256::ZERO, code: None, }, @@ -832,7 +835,7 @@ mod tests { // Check the account balance is updated. let (account, _) = journal.load_account(caller, &mut db).unwrap(); - assert_eq!(account.info.balance, U256::from(99)); + assert_eq!(account.info.get_base_balance(), U256::from(99)); } #[test] @@ -843,7 +846,7 @@ mod tests { caller, AccountInfo { nonce: 0, - balance: U256::from(100), + balances: Balances::from([(BASE_ASSET_ID, U256::from(100))]), code_hash: B256::ZERO, code: None, }, diff --git a/crates/revm/src/handler/optimism.rs b/crates/revm/src/handler/optimism.rs index 49e7c696..1fb1ef00 100644 --- a/crates/revm/src/handler/optimism.rs +++ b/crates/revm/src/handler/optimism.rs @@ -5,8 +5,8 @@ use crate::{ interpreter::{return_ok, return_revert, Gas, InstructionResult}, optimism, primitives::{ - db::Database, Account, EVMError, Env, ExecutionResult, Halt, HashMap, InvalidTransaction, - Output, ResultAndState, Spec, SpecId::REGOLITH, U256, + db::Database, Account, EVMError, Env, ExecutionResult, HaltReason, HashMap, + InvalidTransactionReason, Output, ResultAndState, Spec, SpecId::REGOLITH, U256, }, EvmContext, }; @@ -124,7 +124,7 @@ pub fn reward_beneficiary( panic!("[OPTIMISM] Failed to load L1 Fee Vault account"); }; l1_fee_vault_account.mark_touch(); - l1_fee_vault_account.info.balance += l1_cost; + l1_fee_vault_account.info.increase_base_balance(l1_cost); // Send the base fee of the transaction to the Base Fee Vault. let Ok((base_fee_vault_account, _)) = context @@ -134,11 +134,13 @@ pub fn reward_beneficiary( panic!("[OPTIMISM] Failed to load Base Fee Vault account"); }; base_fee_vault_account.mark_touch(); - base_fee_vault_account.info.balance += context - .env - .block - .basefee - .mul(U256::from(gas.spend() - gas.refunded() as u64)); + base_fee_vault_account.info.increase_base_balance( + context + .env + .block + .basefee + .mul(U256::from(gas.spend() - gas.refunded() as u64)), + ); } Ok(()) } @@ -161,7 +163,7 @@ pub fn main_return( let optimism_regolith = context.env.cfg.optimism && SPEC::enabled(REGOLITH); if is_deposit && optimism_regolith { return Err(EVMError::Transaction( - InvalidTransaction::HaltedDepositPostRegolith, + InvalidTransactionReason::HaltedDepositPostRegolith, )); } } @@ -198,10 +200,11 @@ pub fn end_handle( .unwrap_or_default(), ); acc.info.nonce = acc.info.nonce.saturating_add(1); - acc.info.balance = acc - .info - .balance - .saturating_add(U256::from(context.env().tx.optimism.mint.unwrap_or(0))); + acc.info.set_base_balance( + acc.info + .get_base_balance() + .saturating_add(U256::from(context.env().tx.optimism.mint.unwrap_or(0))), + ); acc.mark_touch(); acc }; @@ -225,7 +228,7 @@ pub fn end_handle( Ok(ResultAndState { result: ExecutionResult::Halt { - reason: Halt::FailedDeposit, + reason: HaltReason::FailedDeposit, gas_used, }, state, diff --git a/crates/revm/src/inspector/customprinter.rs b/crates/revm/src/inspector/customprinter.rs index 109f7a78..dfe08669 100644 --- a/crates/revm/src/inspector/customprinter.rs +++ b/crates/revm/src/inspector/customprinter.rs @@ -113,7 +113,7 @@ mod test { #[test] #[cfg(not(feature = "optimism"))] fn gas_calculation_underflow() { - use crate::primitives::{address, bytes, init_balances}; + use crate::primitives::{address, bytes, init_balances, B256, BASE_ASSET_ID, U256}; // https://github.com/bluealloy/revm/issues/277 // checks this use case diff --git a/examples/fork_ref_transact.rs b/examples/fork_ref_transact.rs index 9ce786c6..e8392731 100644 --- a/examples/fork_ref_transact.rs +++ b/examples/fork_ref_transact.rs @@ -3,7 +3,7 @@ use ethers_core::abi::parse_abi; use ethers_providers::{Http, Provider}; use revm::{ db::{CacheDB, EmptyDB, EthersDB}, - primitives::{address, ExecutionResult, Output, TransactTo, U256}, + primitives::{address, ExecutionResult, Output, TransactTo, B256, BASE_ASSET_ID, U256}, Database, EVM, }; use std::sync::Arc;