Skip to main content

foundry_evm_core/backend/
bal.rs

1//! Block access list (BAL) reads for executing one transaction against its parent block's state.
2
3use super::{Backend, DatabaseError, DatabaseResult};
4use crate::evm::FoundryEvmNetwork;
5use alloy_primitives::{Address, U256};
6use revm::{
7    database_interface::bal::BalState,
8    state::{
9        AccountInfo,
10        bal::{Bal, BlockAccessIndex},
11    },
12};
13use std::sync::Arc;
14
15impl<FEN: FoundryEvmNetwork> Backend<FEN> {
16    /// Serves reads of state the block wrote before `index` from `bal`, and everything else from
17    /// the underlying database.
18    ///
19    /// Index `0` holds the pre-block system writes and transaction `i` is index `i + 1`, so
20    /// positioning the reads at a transaction's own index yields its prestate without replaying
21    /// the earlier transactions. Committing the transaction's state removes the list again, so
22    /// the backend must not execute further transactions of that block afterwards.
23    pub fn set_bal(&mut self, bal: Arc<Bal>, index: BlockAccessIndex) {
24        self.bal = Some(BalState {
25            bal: Some(bal),
26            bal_index: index,
27            allow_db_fallback: true,
28            ..Default::default()
29        });
30    }
31
32    pub(super) fn apply_bal_account(
33        &self,
34        address: Address,
35        account: &mut Option<AccountInfo>,
36    ) -> DatabaseResult<()> {
37        if let Some(bal) = &self.bal {
38            bal.basic(address, account)
39                .map_err(|err| DatabaseError::GetAccount(address, Arc::new(err.into())))?;
40        }
41        Ok(())
42    }
43
44    pub(super) fn bal_storage(
45        &self,
46        address: Address,
47        index: U256,
48    ) -> DatabaseResult<Option<U256>> {
49        let Some(bal) = &self.bal else { return Ok(None) };
50        bal.storage(&address, index)
51            .map_err(|err| DatabaseError::GetStorage(address, index, Arc::new(err.into())))
52    }
53}
54
55#[cfg(test)]
56mod tests;