Skip to content

Commit

Permalink
fix: resolve build issues in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IaroslavMazur committed Dec 1, 2023
1 parent 53ec62c commit 5be5b00
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 37 deletions.
2 changes: 1 addition & 1 deletion bins/revm-test/src/bin/transfer.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 3 additions & 1 deletion bins/revme/src/cmd/statetest/merkle_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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::<KeccakHasher, _, _, _>(
acc.storage
.iter()
Expand Down
4 changes: 2 additions & 2 deletions bins/revme/src/cmd/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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),
)]);
Expand Down
7 changes: 2 additions & 5 deletions crates/primitives/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion crates/revm/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
25 changes: 14 additions & 11 deletions crates/revm/src/evm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand All @@ -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(())
Expand Down Expand Up @@ -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() {
Expand All @@ -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,
},
Expand All @@ -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::<BedrockSpec, InMemoryDB>::commit_mint_value(
Expand All @@ -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]
Expand Down Expand Up @@ -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,
},
Expand All @@ -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]
Expand All @@ -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,
},
Expand Down
31 changes: 17 additions & 14 deletions crates/revm/src/handler/optimism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -124,7 +124,7 @@ pub fn reward_beneficiary<SPEC: Spec, DB: Database>(
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
Expand All @@ -134,11 +134,13 @@ pub fn reward_beneficiary<SPEC: Spec, DB: Database>(
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(())
}
Expand All @@ -161,7 +163,7 @@ pub fn main_return<SPEC: Spec, DB: Database>(
let optimism_regolith = context.env.cfg.optimism && SPEC::enabled(REGOLITH);
if is_deposit && optimism_regolith {
return Err(EVMError::Transaction(
InvalidTransaction::HaltedDepositPostRegolith,
InvalidTransactionReason::HaltedDepositPostRegolith,
));
}
}
Expand Down Expand Up @@ -198,10 +200,11 @@ pub fn end_handle<SPEC: Spec, DB: Database>(
.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
};
Expand All @@ -225,7 +228,7 @@ pub fn end_handle<SPEC: Spec, DB: Database>(

Ok(ResultAndState {
result: ExecutionResult::Halt {
reason: Halt::FailedDeposit,
reason: HaltReason::FailedDeposit,
gas_used,
},
state,
Expand Down
2 changes: 1 addition & 1 deletion crates/revm/src/inspector/customprinter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/fork_ref_transact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5be5b00

Please sign in to comment.