11use crate :: {
22 states:: EvmBlockComplete , BasicContext , Block , BlockComplete , BlockContext , Cfg , ErroredState ,
3- EvmErrored , EvmNeedsCfg , EvmNeedsFirstBlock , EvmNeedsNextBlock , EvmNeedsTx , EvmReady ,
4- EvmTransacted , HasCfg , HasOutputs , NeedsBlock , NeedsCfg , NeedsNextBlock , NeedsTx , Ready ,
5- TransactedState , Tx ,
3+ EvmErrored , EvmExtUnchecked , EvmNeedsCfg , EvmNeedsFirstBlock , EvmNeedsNextBlock , EvmNeedsTx ,
4+ EvmReady , EvmTransacted , HasCfg , HasContext , HasOutputs , NeedsBlock , NeedsCfg , NeedsNextBlock ,
5+ NeedsTx , Ready , TransactedState , Tx ,
66} ;
7- use alloy_consensus:: constants:: KECCAK_EMPTY ;
87use alloy_primitives:: { Address , Bytes , U256 } ;
98use revm:: {
109 db:: { states:: bundle_state:: BundleRetention , BundleState } ,
1110 primitives:: {
12- Account , AccountInfo , AccountStatus , Bytecode , EVMError , EvmState , EvmStorageSlot ,
13- ExecutionResult , InvalidTransaction , ResultAndState , SpecId ,
11+ AccountInfo , Bytecode , EVMError , EvmState , ExecutionResult , InvalidTransaction ,
12+ ResultAndState , SpecId ,
1413 } ,
1514 Database , DatabaseCommit , DatabaseRef , Evm , State ,
1615} ;
@@ -252,21 +251,7 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState> Trevm<'a, Ext, Db, Trev
252251 address : Address ,
253252 f : F ,
254253 ) -> Result < AccountInfo , Db :: Error > {
255- let db = self . inner_mut_unchecked ( ) . db_mut ( ) ;
256-
257- let mut info = db. basic ( address) ?. unwrap_or_default ( ) ;
258- let old = info. clone ( ) ;
259- f ( & mut info) ;
260-
261- // Make a new account with the modified info
262- let mut acct = Account { info, status : AccountStatus :: Touched , ..Default :: default ( ) } ;
263- acct. mark_touch ( ) ;
264-
265- // Create a state object with the modified account.
266- let state = [ ( address, acct) ] . iter ( ) . cloned ( ) . collect ( ) ;
267- self . commit_unchecked ( state) ;
268-
269- Ok ( old)
254+ self . inner . modify_account ( address, f)
270255 }
271256
272257 /// Set the nonce of an account, returning the previous nonce. This is a
@@ -276,7 +261,7 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState> Trevm<'a, Ext, Db, Trev
276261 address : Address ,
277262 nonce : u64 ,
278263 ) -> Result < u64 , Db :: Error > {
279- self . try_modify_account_unchecked ( address, |info| info . nonce = nonce ) . map ( |info| info . nonce )
264+ self . inner . set_nonce ( address, nonce)
280265 }
281266
282267 /// Increment the nonce of an account, returning the previous nonce. This is
@@ -285,17 +270,15 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState> Trevm<'a, Ext, Db, Trev
285270 /// If the nonce is already at the maximum value, it will not be
286271 /// incremented.
287272 pub fn try_increment_nonce_unchecked ( & mut self , address : Address ) -> Result < u64 , Db :: Error > {
288- self . try_modify_account_unchecked ( address, |info| info. nonce = info. nonce . saturating_add ( 1 ) )
289- . map ( |info| info. nonce )
273+ self . inner . increment_nonce ( address)
290274 }
291275
292276 /// Decrement the nonce of an account, returning the previous nonce. This is
293277 /// a low-level API, and is not intended for general use.
294278 ///
295279 /// If the nonce is already 0, it will not be decremented.
296280 pub fn try_decrement_nonce_unchecked ( & mut self , address : Address ) -> Result < u64 , Db :: Error > {
297- self . try_modify_account_unchecked ( address, |info| info. nonce = info. nonce . saturating_sub ( 1 ) )
298- . map ( |info| info. nonce )
281+ self . inner . decrement_nonce ( address)
299282 }
300283
301284 /// Set the EVM storage at a slot. This is a low-level API, and is not
@@ -306,22 +289,7 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState> Trevm<'a, Ext, Db, Trev
306289 slot : U256 ,
307290 value : U256 ,
308291 ) -> Result < U256 , Db :: Error > {
309- let db = self . inner_mut_unchecked ( ) . db_mut ( ) ;
310- let info = db. basic ( address) ?. unwrap_or_default ( ) ;
311- let old = db. storage ( address, slot) ?;
312-
313- let change = EvmStorageSlot :: new_changed ( old, value) ;
314-
315- // Make a new account with the modified storage
316- let storage = [ ( slot, change) ] . iter ( ) . cloned ( ) . collect ( ) ;
317- let mut acct = Account { storage, info, ..Default :: default ( ) } ;
318- acct. mark_touch ( ) ;
319-
320- // Create a state object with the modified account.
321- let state = [ ( address, acct) ] . iter ( ) . cloned ( ) . collect ( ) ;
322- self . commit_unchecked ( state) ;
323-
324- Ok ( old)
292+ self . inner . set_storage ( address, slot, value)
325293 }
326294
327295 /// Set the bytecode at a specific address, returning the previous bytecode
@@ -332,24 +300,7 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState> Trevm<'a, Ext, Db, Trev
332300 address : Address ,
333301 bytecode : Bytecode ,
334302 ) -> Result < Option < Bytecode > , Db :: Error > {
335- let db = self . inner_mut_unchecked ( ) . db_mut ( ) ;
336- let mut info = db. basic ( address) ?. unwrap_or_default ( ) ;
337-
338- let old = if info. code_hash != KECCAK_EMPTY {
339- Some ( db. code_by_hash ( info. code_hash ) ?)
340- } else {
341- None
342- } ;
343-
344- info. code_hash = if bytecode. is_empty ( ) { KECCAK_EMPTY } else { bytecode. hash_slow ( ) } ;
345- info. code = Some ( bytecode) ;
346-
347- let mut acct = Account { info, ..Default :: default ( ) } ;
348- acct. mark_touch ( ) ;
349- let state = [ ( address, acct) ] . iter ( ) . cloned ( ) . collect ( ) ;
350- self . commit_unchecked ( state) ;
351-
352- Ok ( old)
303+ self . inner . set_bytecode ( address, bytecode)
353304 }
354305
355306 /// Increase the balance of an account. Returns the previous balance. This
@@ -362,10 +313,7 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState> Trevm<'a, Ext, Db, Trev
362313 address : Address ,
363314 amount : U256 ,
364315 ) -> Result < U256 , Db :: Error > {
365- self . try_modify_account_unchecked ( address, |info| {
366- info. balance = info. balance . saturating_add ( amount)
367- } )
368- . map ( |info| info. balance )
316+ self . inner . increase_balance ( address, amount)
369317 }
370318
371319 /// Decrease the balance of an account. Returns the previous balance. This
@@ -377,10 +325,7 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState> Trevm<'a, Ext, Db, Trev
377325 address : Address ,
378326 amount : U256 ,
379327 ) -> Result < U256 , Db :: Error > {
380- self . try_modify_account_unchecked ( address, |info| {
381- info. balance = info. balance . saturating_sub ( amount)
382- } )
383- . map ( |info| info. balance )
328+ self . inner . decrease_balance ( address, amount)
384329 }
385330
386331 /// Set the balance of an account. Returns the previous balance. This is a
@@ -390,8 +335,7 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState> Trevm<'a, Ext, Db, Trev
390335 address : Address ,
391336 amount : U256 ,
392337 ) -> Result < U256 , Db :: Error > {
393- self . try_modify_account_unchecked ( address, |info| info. balance = amount)
394- . map ( |info| info. balance )
338+ self . inner . set_balance ( address, amount)
395339 }
396340}
397341
@@ -798,6 +742,8 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState: NeedsBlock>
798742 }
799743}
800744
745+ // --- HAS OUTPUTS
746+
801747impl < ' a , Ext , Db : Database + DatabaseCommit , Missing : HasOutputs >
802748 Trevm < ' a , Ext , State < Db > , Missing >
803749{
@@ -820,6 +766,7 @@ impl<'a, Ext, Db: Database + DatabaseCommit, Missing: HasOutputs>
820766 bundle
821767 }
822768}
769+ // --- BLOCK COMPLETE
823770
824771impl < ' a , Ext , Db : Database + DatabaseCommit , C > EvmBlockComplete < ' a , Ext , Db , C > {
825772 /// Destructure the EVM and return the block context and the EVM ready for
@@ -834,6 +781,36 @@ impl<'a, Ext, Db: Database + DatabaseCommit, C> EvmBlockComplete<'a, Ext, Db, C>
834781 }
835782}
836783
784+ // --- HAS CONTEXT
785+
786+ impl < ' a , Ext , Db : Database + DatabaseCommit , TrevmState : HasContext >
787+ Trevm < ' a , Ext , Db , TrevmState >
788+ {
789+ /// Get a reference to the block context.
790+ pub fn block_context ( & self ) -> & TrevmState :: Context {
791+ self . state . context ( )
792+ }
793+
794+ /// Get a mutable reference to the block context.
795+ pub fn block_context_mut ( & mut self ) -> & mut TrevmState :: Context {
796+ self . state . context_mut ( )
797+ }
798+
799+ /// Apply a closure to the components. This allows for modifying the block
800+ /// context and the EVM in a single operation, by (e.g.) passing the EVM
801+ /// to a block context method.
802+ ///
803+ /// This is a low-level API and is not intended for general use.
804+ pub fn with_parts_unchecked < F > ( self , f : F ) -> Self
805+ where
806+ F : FnOnce ( & mut TrevmState , & mut Evm < ' a , Ext , Db > ) ,
807+ {
808+ let Trevm { mut inner, mut state } = self ;
809+ f ( & mut state, & mut inner) ;
810+ Trevm { inner, state }
811+ }
812+ }
813+
837814// --- NEEDS FIRST TX
838815
839816impl < ' a , Ext , Db : Database + DatabaseCommit , C : BlockContext < Ext , Db > > EvmNeedsTx < ' a , Ext , Db , C > {
0 commit comments