Skip to content

Commit d85f775

Browse files
authored
feat: closure guards for with_env (#28)
* feat: closure guards for with_env * lint: clippy
1 parent 891822b commit d85f775

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

src/evm.rs

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::{
22
states::EvmBlockComplete, BasicContext, Block, BlockComplete, BlockContext, Cfg, ErroredState,
33
EvmErrored, EvmExtUnchecked, EvmNeedsCfg, EvmNeedsFirstBlock, EvmNeedsNextBlock, EvmNeedsTx,
4-
EvmReady, EvmTransacted, HasCfg, HasContext, HasOutputs, NeedsBlock, NeedsCfg, NeedsNextBlock,
5-
NeedsTx, Ready, TransactedState, Tx,
4+
EvmReady, EvmTransacted, HasBlock, HasCfg, HasContext, HasOutputs, HasTx, NeedsBlock, NeedsCfg,
5+
NeedsNextBlock, NeedsTx, Ready, TransactedState, Tx,
66
};
77
use alloy_primitives::{Address, Bytes, U256};
88
use revm::{
@@ -481,6 +481,22 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState: HasCfg> Trevm<'a, Ext,
481481
self.set_code_size_limit(0x6000)
482482
}
483483

484+
/// Run a function with the provided configuration, then restore the
485+
/// previous configuration. This will not affect the block and tx, if those
486+
/// have been filled.
487+
pub fn with_cfg<F, C, NewState>(mut self, f: F, cfg: &C) -> Trevm<'a, Ext, Db, NewState>
488+
where
489+
F: FnOnce(Self) -> Trevm<'a, Ext, Db, NewState>,
490+
C: Cfg,
491+
NewState: HasCfg,
492+
{
493+
let previous = std::mem::take(self.inner.cfg_mut());
494+
cfg.fill_cfg_env(self.inner.cfg_mut());
495+
let mut this = f(self);
496+
*this.inner.cfg_mut() = previous;
497+
this
498+
}
499+
484500
/// Set the KZG settings used for point evaluation precompiles. By default
485501
/// this is set to the settings used in the Ethereum mainnet.
486502
///
@@ -742,6 +758,24 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState: NeedsBlock>
742758
}
743759
}
744760

761+
// --- HAS BLOCK
762+
763+
impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState: HasBlock> Trevm<'a, Ext, Db, TrevmState> {
764+
/// Run a function with the provided block, then restore the previous block.
765+
pub fn with_block<F, B, NewState>(mut self, f: F, b: &B) -> Trevm<'a, Ext, Db, NewState>
766+
where
767+
F: FnOnce(Self) -> Trevm<'a, Ext, Db, NewState>,
768+
B: Block,
769+
NewState: HasBlock,
770+
{
771+
let previous = std::mem::take(self.inner.block_mut());
772+
b.fill_block_env(self.inner.block_mut());
773+
let mut this = f(self);
774+
*this.inner.block_mut() = previous;
775+
this
776+
}
777+
}
778+
745779
// --- HAS OUTPUTS
746780

747781
impl<'a, Ext, Db: Database + DatabaseCommit, Missing: HasOutputs>
@@ -811,7 +845,7 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState: HasContext>
811845
}
812846
}
813847

814-
// --- NEEDS FIRST TX
848+
// --- NEEDS TX
815849

816850
impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext<Ext, Db>> EvmNeedsTx<'a, Ext, Db, C> {
817851
/// Close the current block, applying some logic, and returning the EVM
@@ -860,6 +894,25 @@ impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext<Ext, Db>> EvmNeedsT
860894
}
861895
}
862896

897+
// --- HAS TX
898+
899+
impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState: HasTx> Trevm<'a, Ext, Db, TrevmState> {
900+
/// Run a function with the provided transaction, then restore the previous
901+
/// transaction.
902+
pub fn with_tx<F, T, NewState>(mut self, f: F, t: &T) -> Trevm<'a, Ext, Db, NewState>
903+
where
904+
F: FnOnce(Self) -> Trevm<'a, Ext, Db, NewState>,
905+
T: Tx,
906+
NewState: HasTx,
907+
{
908+
let previous = std::mem::take(self.inner.tx_mut());
909+
t.fill_tx_env(self.inner.tx_mut());
910+
let mut this = f(self);
911+
*this.inner.tx_mut() = previous;
912+
this
913+
}
914+
}
915+
863916
// --- READY
864917

865918
impl<'a, Ext, Db: Database + DatabaseCommit, C: BlockContext<Ext, Db>> EvmReady<'a, Ext, Db, C> {

src/states.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@ pub(crate) mod sealed {
153153
impl<T, E> HasCfg for ErroredState<T, E> {}
154154
impl<T> HasCfg for Ready<T> {}
155155

156+
pub trait HasBlock {}
157+
impl<T> HasBlock for NeedsTx<T> {}
158+
impl<T> HasBlock for TransactedState<T> {}
159+
impl<T, E> HasBlock for ErroredState<T, E> {}
160+
impl<T> HasBlock for Ready<T> {}
161+
162+
pub trait HasTx {}
163+
impl<T> HasTx for TransactedState<T> {}
164+
impl<T, E> HasTx for ErroredState<T, E> {}
165+
impl<T> HasTx for Ready<T> {}
166+
156167
pub trait HasContext {
157168
type Context;
158169

0 commit comments

Comments
 (0)