@@ -12,40 +12,35 @@ const WARM_AUTHORITY_GAS: u64 = 100;
12
12
const COLD_AUTHORITY_GAS : u64 = 2600 ;
13
13
const FIXED_FEE_GAS : u64 = 3100 ;
14
14
15
- #[ derive( Default ) ]
16
- struct CustomContext {
17
- authority : Address ,
15
+ #[ derive( Default , Clone ) ]
16
+ pub ( crate ) struct CustomContext {
17
+ pub ( crate ) authority : Rc < RefCell < Address > > ,
18
18
}
19
19
20
20
/// eip3074 boxed instructions.
21
21
pub fn boxed_instructions < ' a , EXT : ' a , DB : Database + ' a > (
22
22
) -> impl Iterator < Item = BoxedInstructionWithOpCode < ' a , Evm < ' a , EXT , DB > > > {
23
- let shared_context = Rc :: new ( RefCell :: new ( CustomContext :: default ( ) ) ) ;
23
+ let shared_context = CustomContext :: default ( ) ;
24
+ let to_capture = shared_context. clone ( ) ;
24
25
25
- let auth_context = Rc :: clone ( & shared_context) ;
26
- let wrapped_auth_instruction = {
27
- move |interpreter : & mut Interpreter , evm : & mut Evm < ' a , EXT , DB > | {
28
- let mut ctx = auth_context. borrow_mut ( ) ;
29
- auth_instruction ( interpreter, evm, ctx)
30
- }
31
- } ;
26
+ let wrapped_auth_instruction =
27
+ Box :: new ( move |interpreter : & mut Interpreter , evm : & mut Evm < ' a , EXT , DB > | {
28
+ auth_instruction ( interpreter, evm, & to_capture) ;
29
+ } ) ;
32
30
33
- let authcall_context = Rc :: clone ( & shared_context) ;
34
- let wrapped_authcall_instruction = {
35
- move |interpreter : & mut Interpreter , evm : & mut Evm < ' a , EXT , DB > | {
36
- let mut ctx = authcall_context. borrow_mut ( ) ;
37
- authcall_instruction ( interpreter, evm, ctx)
38
- }
39
- } ;
31
+ let wrapped_authcall_instruction =
32
+ Box :: new ( move |interpreter : & mut Interpreter , evm : & mut Evm < ' a , EXT , DB > | {
33
+ authcall_instruction ( interpreter, evm, & to_capture) ;
34
+ } ) ;
40
35
41
36
[
42
37
BoxedInstructionWithOpCode {
43
38
opcode : AUTH_OPCODE ,
44
- boxed_instruction : Box :: new ( wrapped_auth_instruction) ,
39
+ boxed_instruction : wrapped_auth_instruction,
45
40
} ,
46
41
BoxedInstructionWithOpCode {
47
42
opcode : AUTHCALL_OPCODE ,
48
- boxed_instruction : Box :: new ( wrapped_authcall_instruction) ,
43
+ boxed_instruction : wrapped_authcall_instruction,
49
44
} ,
50
45
]
51
46
. into_iter ( )
@@ -65,7 +60,7 @@ fn compose_msg(chain_id: u64, nonce: u64, invoker_address: Address, commit: B256
65
60
fn auth_instruction < EXT , DB : Database > (
66
61
interp : & mut Interpreter ,
67
62
evm : & mut Evm < ' _ , EXT , DB > ,
68
- ctx : & mut Rc < RefCell < CustomContext > > ,
63
+ ctx : & CustomContext ,
69
64
) {
70
65
interp. gas . record_cost ( FIXED_FEE_GAS ) ;
71
66
@@ -132,16 +127,13 @@ fn auth_instruction<EXT, DB: Database>(
132
127
}
133
128
} ;
134
129
135
- let context = ctx. borrow_mut ( ) ;
136
130
let ( to_persist_authority, result) = if Address :: from_slice ( & signer[ 12 ..] ) == authority {
137
131
( authority, B256 :: with_last_byte ( 1 ) )
138
132
} else {
139
- context. authority = Address :: default ( ) ;
140
-
141
133
( Address :: default ( ) , B256 :: ZERO )
142
134
} ;
143
- let mut context = ctx. borrow_mut ( ) ;
144
- context . authority = to_persist_authority;
135
+ let mut inner_authority = ctx. authority . borrow_mut ( ) ;
136
+ * inner_authority = to_persist_authority;
145
137
146
138
if let Err ( e) = interp. stack . push_b256 ( result) {
147
139
interp. instruction_result = e;
@@ -151,7 +143,7 @@ fn auth_instruction<EXT, DB: Database>(
151
143
fn authcall_instruction < EXT , DB : Database > (
152
144
interp : & mut Interpreter ,
153
145
_evm : & mut Evm < ' _ , EXT , DB > ,
154
- ctx : & mut Rc < RefCell < CustomContext > > ,
146
+ _ctx : & CustomContext ,
155
147
) {
156
148
interp. gas . record_cost ( 133 ) ;
157
149
}
@@ -189,11 +181,11 @@ mod tests {
189
181
fn setup_evm ( ) -> Evm < ' static , ( ) , CacheDB < EmptyDBTyped < Infallible > > > {
190
182
Evm :: builder ( )
191
183
. with_db ( InMemoryDB :: default ( ) )
192
- . append_handler_register ( |handler| {
184
+ . append_handler_register_box ( Box :: new ( |handler| {
193
185
if let Some ( ref mut table) = handler. instruction_table {
194
186
table. insert_boxed ( AUTH_OPCODE , Box :: new ( move |_interpreter, _handler| { } ) ) ;
195
187
}
196
- } )
188
+ } ) )
197
189
. build ( )
198
190
}
199
191
@@ -237,19 +229,12 @@ mod tests {
237
229
compose_msg ( 1 , 0 , Address :: default ( ) , B256 :: ZERO )
238
230
}
239
231
240
- fn default_context ( ) -> Rc < RefCell < CustomContext > > {
241
- let shared_context = Rc :: new ( RefCell :: new ( CustomContext :: default ( ) ) ) ;
242
- let mut context = Rc :: clone ( & shared_context) ;
243
- context
244
- }
245
-
246
232
#[ test]
247
233
fn test_auth_instruction_stack_underflow ( ) {
248
234
let mut interpreter = setup_interpreter ( ) ;
249
235
let mut evm = setup_evm ( ) ;
250
- let mut context = default_context ( ) ;
251
236
252
- auth_instruction ( & mut interpreter, & mut evm, & mut context ) ;
237
+ auth_instruction ( & mut interpreter, & mut evm, & CustomContext :: default ( ) ) ;
253
238
assert_eq ! ( interpreter. instruction_result, InstructionResult :: StackUnderflow ) ;
254
239
255
240
// check gas
@@ -273,9 +258,8 @@ mod tests {
273
258
setup_shared_memory ( & mut interpreter. shared_memory , y_parity, & r, & s) ;
274
259
275
260
let mut evm = setup_evm ( ) ;
276
- let mut context = default_context ( ) ;
277
261
278
- auth_instruction ( & mut interpreter, & mut evm, & mut context ) ;
262
+ auth_instruction ( & mut interpreter, & mut evm, & CustomContext :: default ( ) ) ;
279
263
280
264
assert_eq ! ( interpreter. instruction_result, InstructionResult :: Continue ) ;
281
265
let result = interpreter. stack . pop ( ) . unwrap ( ) ;
@@ -298,9 +282,8 @@ mod tests {
298
282
setup_stack ( & mut interpreter. stack , authority) ;
299
283
300
284
let mut evm = setup_evm ( ) ;
301
- let mut context = default_context ( ) ;
302
285
303
- auth_instruction ( & mut interpreter, & mut evm, & mut context ) ;
286
+ auth_instruction ( & mut interpreter, & mut evm, & CustomContext :: default ( ) ) ;
304
287
305
288
assert_eq ! ( interpreter. instruction_result, InstructionResult :: Stop ) ;
306
289
@@ -326,9 +309,8 @@ mod tests {
326
309
setup_shared_memory ( & mut interpreter. shared_memory , y_parity, & r, & s) ;
327
310
328
311
let mut evm = setup_evm ( ) ;
329
- let mut context = default_context ( ) ;
330
312
331
- auth_instruction ( & mut interpreter, & mut evm, & mut context ) ;
313
+ auth_instruction ( & mut interpreter, & mut evm, & CustomContext :: default ( ) ) ;
332
314
333
315
assert_eq ! ( interpreter. instruction_result, InstructionResult :: Continue ) ;
334
316
let result = interpreter. stack . pop ( ) . unwrap ( ) ;
@@ -352,9 +334,8 @@ mod tests {
352
334
353
335
let mut evm = setup_evm ( ) ;
354
336
evm. context . evm . journaled_state . state . insert ( authority, Account :: default ( ) ) ;
355
- let mut context = default_context ( ) ;
356
337
357
- auth_instruction ( & mut interpreter, & mut evm, & mut context ) ;
338
+ auth_instruction ( & mut interpreter, & mut evm, & CustomContext :: default ( ) ) ;
358
339
359
340
assert_eq ! ( interpreter. instruction_result, InstructionResult :: Continue ) ;
360
341
let result = interpreter. stack . pop ( ) . unwrap ( ) ;
@@ -381,9 +362,8 @@ mod tests {
381
362
setup_shared_memory ( & mut interpreter. shared_memory , y_parity, & r, & B256 :: ZERO ) ;
382
363
383
364
let mut evm = setup_evm ( ) ;
384
- let mut context = default_context ( ) ;
385
365
386
- auth_instruction ( & mut interpreter, & mut evm, & mut context ) ;
366
+ auth_instruction ( & mut interpreter, & mut evm, & CustomContext :: default ( ) ) ;
387
367
388
368
assert_eq ! ( interpreter. instruction_result, InstructionResult :: Stop ) ;
389
369
0 commit comments