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;
4use crate::evm::{FoundryEvmFor, FoundryEvmNetwork};
5use evm2::evm::bal::{Bal, BlockAccessIndex};
6use std::sync::Arc;
7
8impl<FEN: FoundryEvmNetwork> Backend<FEN> {
9    /// Serves reads of state the block wrote before `index` from `bal`, and everything else from
10    /// the underlying database.
11    ///
12    /// Index `0` holds the pre-block system writes and transaction `i` is index `i + 1`, so
13    /// positioning the reads at a transaction's own index yields its prestate without replaying
14    /// the earlier transactions. The list applies to EVMs created afterwards; parking an EVM
15    /// removes it again, so the backend must not execute further transactions of that block
16    /// afterwards.
17    pub fn set_bal(&mut self, bal: Arc<Bal>, index: BlockAccessIndex) {
18        self.bal = Some((bal, index));
19    }
20
21    /// Attaches the pending block access list to a newly created EVM.
22    pub(super) fn apply_bal(&self, evm: &mut FoundryEvmFor<'_, FEN>) {
23        if let Some((bal, index)) = &self.bal {
24            let state = evm.state_mut();
25            state.set_bal(Arc::clone(bal));
26            state.set_bal_index(*index);
27            state.set_allow_bal_db_fallback(true);
28        }
29    }
30}
31
32#[cfg(test)]
33mod tests;