@@ -4,7 +4,10 @@ use crate::{
44 EvmNeedsCfg , EvmNeedsTx , EvmReady , EvmTransacted , HasBlock , HasCfg , HasTx , NeedsCfg , NeedsTx ,
55 TransactedState , Tx ,
66} ;
7- use alloy:: primitives:: { Address , Bytes , U256 } ;
7+ use alloy:: {
8+ primitives:: { Address , Bytes , U256 } ,
9+ rpc:: types:: { state:: StateOverride , BlockOverrides } ,
10+ } ;
811use core:: convert:: Infallible ;
912use revm:: {
1013 db:: { states:: bundle_state:: BundleRetention , BundleState , State } ,
@@ -137,6 +140,42 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState> Trevm<'a, Ext, Db, Trev
137140 None => Ok ( None ) ,
138141 }
139142 }
143+
144+ /// Apply [`StateOverride`]s to the current state.
145+ pub fn apply_state_overrides (
146+ mut self ,
147+ overrides : & StateOverride ,
148+ ) -> Result < Self , EVMError < Db :: Error > > {
149+ for ( address, account_override) in overrides {
150+ if let Some ( balance) = account_override. balance {
151+ self . inner . set_balance ( * address, balance) . map_err ( EVMError :: Database ) ?;
152+ }
153+ if let Some ( nonce) = account_override. nonce {
154+ self . inner . set_nonce ( * address, nonce) . map_err ( EVMError :: Database ) ?;
155+ }
156+ if let Some ( code) = account_override. code . as_ref ( ) {
157+ self . inner
158+ . set_bytecode (
159+ * address,
160+ Bytecode :: new_raw_checked ( code. clone ( ) )
161+ . map_err ( |_| EVMError :: Custom ( "Invalid bytecode" . to_string ( ) ) ) ?,
162+ )
163+ . map_err ( EVMError :: Database ) ?;
164+ }
165+ if let Some ( state) = account_override. state . as_ref ( ) {
166+ for ( slot, value) in state {
167+ self . inner
168+ . set_storage (
169+ * address,
170+ U256 :: from_be_bytes ( ( * slot) . into ( ) ) ,
171+ U256 :: from_be_bytes ( ( * value) . into ( ) ) ,
172+ )
173+ . map_err ( EVMError :: Database ) ?;
174+ }
175+ }
176+ }
177+ Ok ( self )
178+ }
140179}
141180
142181impl < Ext , Db : Database + DatabaseCommit + DatabaseRef , TrevmState > Trevm < ' _ , Ext , Db , TrevmState > {
@@ -981,6 +1020,21 @@ impl<'a, Ext, Db: Database + DatabaseCommit, TrevmState: HasTx> Trevm<'a, Ext, D
9811020 }
9821021}
9831022
1023+ // -- HAS TX with State<Db>
1024+
1025+ impl < Ext , Db : Database > EvmNeedsTx < ' _ , Ext , State < Db > > {
1026+ /// Apply block overrides to the current block.
1027+ pub fn apply_block_overrides ( mut self , overrides : BlockOverrides ) -> Self {
1028+ overrides. fill_block ( & mut self . inner ) ;
1029+
1030+ if let Some ( hashes) = overrides. block_hash {
1031+ self . inner . db_mut ( ) . block_hashes . extend ( hashes)
1032+ }
1033+
1034+ self
1035+ }
1036+ }
1037+
9841038// --- READY
9851039
9861040impl < ' a , Ext , Db : Database + DatabaseCommit > EvmReady < ' a , Ext , Db > {
0 commit comments