1
1
//! A wrapper around `Backend` that is clone-on-write used for fuzzing.
2
2
3
- use super :: BackendError ;
3
+ use super :: { BackendError , JournaledState } ;
4
4
use crate :: {
5
5
backend:: {
6
6
diagnostic:: RevertDiagnostic , Backend , DatabaseExt , LocalForkId , RevertStateSnapshotAction ,
7
- } ,
8
- fork:: { CreateFork , ForkId } ,
9
- InspectorExt ,
7
+ } , fork:: { CreateFork , ForkId } , AsEnvMut , Env , EnvMut , InspectorExt
10
8
} ;
11
9
use alloy_genesis:: GenesisAccount ;
12
10
use alloy_primitives:: { Address , B256 , U256 } ;
13
11
use alloy_rpc_types:: TransactionRequest ;
14
12
use eyre:: WrapErr ;
15
13
use foundry_fork_db:: DatabaseError ;
16
14
use revm:: {
17
- db:: DatabaseRef ,
18
- primitives:: {
19
- Account , AccountInfo , Bytecode , Env , EnvWithHandlerCfg , HashMap as Map , ResultAndState ,
20
- SpecId ,
21
- } ,
22
- Database , DatabaseCommit , JournaledState ,
15
+ context:: { result:: ResultAndState , JournalInit } ,
16
+ database:: DatabaseRef ,
17
+ primitives:: { hardfork:: SpecId , HashMap as Map } ,
18
+ state:: { Account , AccountInfo , Bytecode } ,
19
+ Database , DatabaseCommit , ExecuteEvm ,
23
20
} ;
24
21
use std:: { borrow:: Cow , collections:: BTreeMap } ;
25
22
@@ -64,7 +61,7 @@ impl<'a> CowBackend<'a> {
64
61
#[ instrument( name = "inspect" , level = "debug" , skip_all) ]
65
62
pub fn inspect < I : InspectorExt > (
66
63
& mut self ,
67
- env : & mut EnvWithHandlerCfg ,
64
+ env : & mut Env ,
68
65
inspector : & mut I ,
69
66
) -> eyre:: Result < ResultAndState > {
70
67
// this is a new call to inspect with a new env, so even if we've cloned the backend
@@ -73,9 +70,9 @@ impl<'a> CowBackend<'a> {
73
70
self . spec_id = env. handler_cfg . spec_id ;
74
71
let mut evm = crate :: utils:: new_evm_with_inspector ( self , env. clone ( ) , inspector) ;
75
72
76
- let res = evm. transact ( ) . wrap_err ( "EVM error" ) ?;
73
+ let res = evm. replay ( ) . wrap_err ( "EVM error" ) ?;
77
74
78
- env. env = evm. context . evm . inner . env ;
75
+ * env = evm. data . ctx . as_env_mut ( ) . to_owned ( ) ;
79
76
80
77
Ok ( res)
81
78
}
@@ -90,45 +87,47 @@ impl<'a> CowBackend<'a> {
90
87
/// Returns a mutable instance of the Backend.
91
88
///
92
89
/// If this is the first time this is called, the backed is cloned and initialized.
93
- fn backend_mut ( & mut self , env : & Env ) -> & mut Backend {
90
+ fn backend_mut ( & mut self , env : EnvMut < ' _ > ) -> & mut Backend {
91
+ let env = env. as_env_mut ( ) . to_owned ( ) ;
94
92
if !self . is_initialized {
95
93
let backend = self . backend . to_mut ( ) ;
96
- let env = EnvWithHandlerCfg :: new_with_spec_id ( Box :: new ( env. clone ( ) ) , self . spec_id ) ;
94
+ let mut env = env. to_owned ( ) ;
95
+ env. evm_env . spec = self . spec_id ;
97
96
backend. initialize ( & env) ;
98
97
self . is_initialized = true ;
99
- return backend
98
+ return backend;
100
99
}
101
100
self . backend . to_mut ( )
102
101
}
103
102
104
103
/// Returns a mutable instance of the Backend if it is initialized.
105
104
fn initialized_backend_mut ( & mut self ) -> Option < & mut Backend > {
106
105
if self . is_initialized {
107
- return Some ( self . backend . to_mut ( ) )
106
+ return Some ( self . backend . to_mut ( ) ) ;
108
107
}
109
108
None
110
109
}
111
110
}
112
111
113
112
impl DatabaseExt for CowBackend < ' _ > {
114
- fn snapshot_state ( & mut self , journaled_state : & JournaledState , env : & Env ) -> U256 {
113
+ fn snapshot_state ( & mut self , journaled_state : & JournaledState < ' _ > , env : & Env ) -> U256 {
115
114
self . backend_mut ( env) . snapshot_state ( journaled_state, env)
116
115
}
117
116
118
117
fn revert_state (
119
118
& mut self ,
120
119
id : U256 ,
121
- journaled_state : & JournaledState ,
122
- current : & mut Env ,
120
+ journaled_state : & JournaledState < ' _ > ,
121
+ current : EnvMut < ' _ > ,
123
122
action : RevertStateSnapshotAction ,
124
- ) -> Option < JournaledState > {
123
+ ) -> Option < JournalInit > {
125
124
self . backend_mut ( current) . revert_state ( id, journaled_state, current, action)
126
125
}
127
126
128
127
fn delete_state_snapshot ( & mut self , id : U256 ) -> bool {
129
128
// delete state snapshot requires a previous snapshot to be initialized
130
129
if let Some ( backend) = self . initialized_backend_mut ( ) {
131
- return backend. delete_state_snapshot ( id)
130
+ return backend. delete_state_snapshot ( id) ;
132
131
}
133
132
false
134
133
}
@@ -154,38 +153,38 @@ impl DatabaseExt for CowBackend<'_> {
154
153
fn select_fork (
155
154
& mut self ,
156
155
id : LocalForkId ,
157
- env : & mut Env ,
158
- journaled_state : & mut JournaledState ,
156
+ env : EnvMut < ' _ > ,
157
+ journaled_state : & mut JournaledState < ' _ > ,
159
158
) -> eyre:: Result < ( ) > {
160
- self . backend_mut ( env) . select_fork ( id, env, journaled_state)
159
+ self . backend_mut ( & mut env) . select_fork ( id, env, journaled_state)
161
160
}
162
161
163
162
fn roll_fork (
164
163
& mut self ,
165
164
id : Option < LocalForkId > ,
166
165
block_number : u64 ,
167
- env : & mut Env ,
168
- journaled_state : & mut JournaledState ,
166
+ env : EnvMut < ' _ > ,
167
+ journaled_state : & mut JournaledState < ' _ > ,
169
168
) -> eyre:: Result < ( ) > {
170
- self . backend_mut ( env) . roll_fork ( id, block_number, env, journaled_state)
169
+ self . backend_mut ( env. as_env_mut ( ) ) . roll_fork ( id, block_number, env, journaled_state)
171
170
}
172
171
173
172
fn roll_fork_to_transaction (
174
173
& mut self ,
175
174
id : Option < LocalForkId > ,
176
175
transaction : B256 ,
177
- env : & mut Env ,
178
- journaled_state : & mut JournaledState ,
176
+ mut env : EnvMut < ' _ > ,
177
+ journaled_state : & mut JournaledState < ' _ > ,
179
178
) -> eyre:: Result < ( ) > {
180
- self . backend_mut ( env) . roll_fork_to_transaction ( id, transaction, env, journaled_state)
179
+ self . backend_mut ( env. as_env_mut ( ) ) . roll_fork_to_transaction ( id, transaction, env, journaled_state)
181
180
}
182
181
183
182
fn transact (
184
183
& mut self ,
185
184
id : Option < LocalForkId > ,
186
185
transaction : B256 ,
187
186
env : Env ,
188
- journaled_state : & mut JournaledState ,
187
+ journaled_state : & mut JournaledState < ' _ > ,
189
188
inspector : & mut dyn InspectorExt ,
190
189
) -> eyre:: Result < ( ) > {
191
190
self . backend_mut ( & env) . transact ( id, transaction, env, journaled_state, inspector)
@@ -195,7 +194,7 @@ impl DatabaseExt for CowBackend<'_> {
195
194
& mut self ,
196
195
transaction : & TransactionRequest ,
197
196
env : Env ,
198
- journaled_state : & mut JournaledState ,
197
+ journaled_state : & mut JournaledState < ' _ > ,
199
198
inspector : & mut dyn InspectorExt ,
200
199
) -> eyre:: Result < ( ) > {
201
200
self . backend_mut ( & env) . transact_from_tx ( transaction, env, journaled_state, inspector)
@@ -220,15 +219,15 @@ impl DatabaseExt for CowBackend<'_> {
220
219
fn diagnose_revert (
221
220
& self ,
222
221
callee : Address ,
223
- journaled_state : & JournaledState ,
222
+ journaled_state : & JournaledState < ' _ > ,
224
223
) -> Option < RevertDiagnostic > {
225
224
self . backend . diagnose_revert ( callee, journaled_state)
226
225
}
227
226
228
227
fn load_allocs (
229
228
& mut self ,
230
229
allocs : & BTreeMap < Address , GenesisAccount > ,
231
- journaled_state : & mut JournaledState ,
230
+ journaled_state : & mut JournaledState < ' _ > ,
232
231
) -> Result < ( ) , BackendError > {
233
232
self . backend_mut ( & Env :: default ( ) ) . load_allocs ( allocs, journaled_state)
234
233
}
@@ -237,7 +236,7 @@ impl DatabaseExt for CowBackend<'_> {
237
236
& mut self ,
238
237
source : & GenesisAccount ,
239
238
target : & Address ,
240
- journaled_state : & mut JournaledState ,
239
+ journaled_state : & mut JournaledState < ' _ > ,
241
240
) -> Result < ( ) , BackendError > {
242
241
self . backend_mut ( & Env :: default ( ) ) . clone_account ( source, target, journaled_state)
243
242
}
0 commit comments