Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
refactor(concurrency): use state reader generic, instead of updatable…
Browse files Browse the repository at this point in the history
…, when possible (#1965)
  • Loading branch information
noaov1 authored Jun 6, 2024
1 parent 1bb3d97 commit 952373d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
22 changes: 12 additions & 10 deletions crates/blockifier/src/state/cached_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,16 +476,16 @@ impl StateCache {

/// Wraps a mutable reference to a `State` object, exposing its API.
/// Used to pass ownership to a `CachedState`.
pub struct MutRefState<'a, U: UpdatableState + ?Sized>(&'a mut U);
pub struct MutRefState<'a, S: StateReader + ?Sized>(&'a mut S);

impl<'a, U: UpdatableState + ?Sized> MutRefState<'a, U> {
pub fn new(state: &'a mut U) -> Self {
impl<'a, S: StateReader + ?Sized> MutRefState<'a, S> {
pub fn new(state: &'a mut S) -> Self {
Self(state)
}
}

/// Proxies inner object to expose `State` functionality.
impl<'a, U: UpdatableState + ?Sized> StateReader for MutRefState<'a, U> {
impl<'a, S: StateReader + ?Sized> StateReader for MutRefState<'a, S> {
fn get_storage_at(
&self,
contract_address: ContractAddress,
Expand Down Expand Up @@ -513,16 +513,21 @@ impl<'a, U: UpdatableState + ?Sized> StateReader for MutRefState<'a, U> {

pub type TransactionalState<'a, U> = CachedState<MutRefState<'a, U>>;

/// Adds the ability to perform a transactional execution.
impl<'a, U: UpdatableState> TransactionalState<'a, U> {
impl<'a, S: StateReader> TransactionalState<'a, S> {
/// Creates a transactional instance from the given updatable state.
/// It allows performing buffered modifying actions on the given state, which
/// will either all happen (will be updated in the state and committed)
/// or none of them (will be discarded).
pub fn create_transactional(state: &mut U) -> TransactionalState<'_, U> {
pub fn create_transactional(state: &mut S) -> TransactionalState<'_, S> {
CachedState::new(MutRefState::new(state))
}

/// Drops `self`.
pub fn abort(self) {}
}

/// Adds the ability to perform a transactional execution.
impl<'a, U: UpdatableState> TransactionalState<'a, U> {
/// Commits changes in the child (wrapping) state to its parent.
pub fn commit(self) {
let state = self.state.0;
Expand All @@ -533,9 +538,6 @@ impl<'a, U: UpdatableState> TransactionalState<'a, U> {
&self.visited_pcs,
)
}

/// Drops `self`.
pub fn abort(self) {}
}

type StorageDiff = IndexMap<ContractAddress, IndexMap<StorageKey, StarkFelt>>;
Expand Down
20 changes: 10 additions & 10 deletions crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,9 @@ impl AccountTransaction {
Ok(())
}

fn handle_fee<U: UpdatableState>(
fn handle_fee<S: StateReader>(
&self,
state: &mut TransactionalState<'_, U>,
state: &mut TransactionalState<'_, S>,
tx_context: Arc<TransactionContext>,
actual_fee: Fee,
charge_fee: bool,
Expand Down Expand Up @@ -372,8 +372,8 @@ impl AccountTransaction {
/// manipulates the state to avoid that part.
/// Note: the returned transfer call info is partial, and should be completed at the commit
/// stage, as well as the actual sequencer balance.
fn concurrency_execute_fee_transfer<U: UpdatableState>(
state: &mut TransactionalState<'_, U>,
fn concurrency_execute_fee_transfer<S: StateReader>(
state: &mut TransactionalState<'_, S>,
tx_context: Arc<TransactionContext>,
actual_fee: Fee,
) -> TransactionExecutionResult<CallInfo> {
Expand Down Expand Up @@ -413,9 +413,9 @@ impl AccountTransaction {
}
}

fn run_non_revertible<U: UpdatableState>(
fn run_non_revertible<S: StateReader>(
&self,
state: &mut TransactionalState<'_, U>,
state: &mut TransactionalState<'_, S>,
tx_context: Arc<TransactionContext>,
remaining_gas: &mut u64,
validate: bool,
Expand Down Expand Up @@ -476,9 +476,9 @@ impl AccountTransaction {
}
}

fn run_revertible<U: UpdatableState>(
fn run_revertible<S: StateReader>(
&self,
state: &mut TransactionalState<'_, U>,
state: &mut TransactionalState<'_, S>,
tx_context: Arc<TransactionContext>,
remaining_gas: &mut u64,
validate: bool,
Expand Down Expand Up @@ -617,9 +617,9 @@ impl AccountTransaction {
}

/// Runs validation and execution.
fn run_or_revert<U: UpdatableState>(
fn run_or_revert<S: StateReader>(
&self,
state: &mut TransactionalState<'_, U>,
state: &mut TransactionalState<'_, S>,
remaining_gas: &mut u64,
tx_context: Arc<TransactionContext>,
validate: bool,
Expand Down

0 comments on commit 952373d

Please sign in to comment.