Skip to content
This repository was archived by the owner on Nov 25, 2024. It is now read-only.

Commit d93216c

Browse files
committed
pass CustomContext to instructions
1 parent fc15cb1 commit d93216c

File tree

1 file changed

+27
-47
lines changed

1 file changed

+27
-47
lines changed

crates/instructions/src/eip3074.rs

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,35 @@ const WARM_AUTHORITY_GAS: u64 = 100;
1212
const COLD_AUTHORITY_GAS: u64 = 2600;
1313
const FIXED_FEE_GAS: u64 = 3100;
1414

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>>,
1818
}
1919

2020
/// eip3074 boxed instructions.
2121
pub fn boxed_instructions<'a, EXT: 'a, DB: Database + 'a>(
2222
) -> 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();
2425

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+
});
3230

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+
});
4035

4136
[
4237
BoxedInstructionWithOpCode {
4338
opcode: AUTH_OPCODE,
44-
boxed_instruction: Box::new(wrapped_auth_instruction),
39+
boxed_instruction: wrapped_auth_instruction,
4540
},
4641
BoxedInstructionWithOpCode {
4742
opcode: AUTHCALL_OPCODE,
48-
boxed_instruction: Box::new(wrapped_authcall_instruction),
43+
boxed_instruction: wrapped_authcall_instruction,
4944
},
5045
]
5146
.into_iter()
@@ -65,7 +60,7 @@ fn compose_msg(chain_id: u64, nonce: u64, invoker_address: Address, commit: B256
6560
fn auth_instruction<EXT, DB: Database>(
6661
interp: &mut Interpreter,
6762
evm: &mut Evm<'_, EXT, DB>,
68-
ctx: &mut Rc<RefCell<CustomContext>>,
63+
ctx: &CustomContext,
6964
) {
7065
interp.gas.record_cost(FIXED_FEE_GAS);
7166

@@ -132,16 +127,13 @@ fn auth_instruction<EXT, DB: Database>(
132127
}
133128
};
134129

135-
let context = ctx.borrow_mut();
136130
let (to_persist_authority, result) = if Address::from_slice(&signer[12..]) == authority {
137131
(authority, B256::with_last_byte(1))
138132
} else {
139-
context.authority = Address::default();
140-
141133
(Address::default(), B256::ZERO)
142134
};
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;
145137

146138
if let Err(e) = interp.stack.push_b256(result) {
147139
interp.instruction_result = e;
@@ -151,7 +143,7 @@ fn auth_instruction<EXT, DB: Database>(
151143
fn authcall_instruction<EXT, DB: Database>(
152144
interp: &mut Interpreter,
153145
_evm: &mut Evm<'_, EXT, DB>,
154-
ctx: &mut Rc<RefCell<CustomContext>>,
146+
_ctx: &CustomContext,
155147
) {
156148
interp.gas.record_cost(133);
157149
}
@@ -189,11 +181,11 @@ mod tests {
189181
fn setup_evm() -> Evm<'static, (), CacheDB<EmptyDBTyped<Infallible>>> {
190182
Evm::builder()
191183
.with_db(InMemoryDB::default())
192-
.append_handler_register(|handler| {
184+
.append_handler_register_box(Box::new(|handler| {
193185
if let Some(ref mut table) = handler.instruction_table {
194186
table.insert_boxed(AUTH_OPCODE, Box::new(move |_interpreter, _handler| {}));
195187
}
196-
})
188+
}))
197189
.build()
198190
}
199191

@@ -237,19 +229,12 @@ mod tests {
237229
compose_msg(1, 0, Address::default(), B256::ZERO)
238230
}
239231

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-
246232
#[test]
247233
fn test_auth_instruction_stack_underflow() {
248234
let mut interpreter = setup_interpreter();
249235
let mut evm = setup_evm();
250-
let mut context = default_context();
251236

252-
auth_instruction(&mut interpreter, &mut evm, &mut context);
237+
auth_instruction(&mut interpreter, &mut evm, &CustomContext::default());
253238
assert_eq!(interpreter.instruction_result, InstructionResult::StackUnderflow);
254239

255240
// check gas
@@ -273,9 +258,8 @@ mod tests {
273258
setup_shared_memory(&mut interpreter.shared_memory, y_parity, &r, &s);
274259

275260
let mut evm = setup_evm();
276-
let mut context = default_context();
277261

278-
auth_instruction(&mut interpreter, &mut evm, &mut context);
262+
auth_instruction(&mut interpreter, &mut evm, &CustomContext::default());
279263

280264
assert_eq!(interpreter.instruction_result, InstructionResult::Continue);
281265
let result = interpreter.stack.pop().unwrap();
@@ -298,9 +282,8 @@ mod tests {
298282
setup_stack(&mut interpreter.stack, authority);
299283

300284
let mut evm = setup_evm();
301-
let mut context = default_context();
302285

303-
auth_instruction(&mut interpreter, &mut evm, &mut context);
286+
auth_instruction(&mut interpreter, &mut evm, &CustomContext::default());
304287

305288
assert_eq!(interpreter.instruction_result, InstructionResult::Stop);
306289

@@ -326,9 +309,8 @@ mod tests {
326309
setup_shared_memory(&mut interpreter.shared_memory, y_parity, &r, &s);
327310

328311
let mut evm = setup_evm();
329-
let mut context = default_context();
330312

331-
auth_instruction(&mut interpreter, &mut evm, &mut context);
313+
auth_instruction(&mut interpreter, &mut evm, &CustomContext::default());
332314

333315
assert_eq!(interpreter.instruction_result, InstructionResult::Continue);
334316
let result = interpreter.stack.pop().unwrap();
@@ -352,9 +334,8 @@ mod tests {
352334

353335
let mut evm = setup_evm();
354336
evm.context.evm.journaled_state.state.insert(authority, Account::default());
355-
let mut context = default_context();
356337

357-
auth_instruction(&mut interpreter, &mut evm, &mut context);
338+
auth_instruction(&mut interpreter, &mut evm, &CustomContext::default());
358339

359340
assert_eq!(interpreter.instruction_result, InstructionResult::Continue);
360341
let result = interpreter.stack.pop().unwrap();
@@ -381,9 +362,8 @@ mod tests {
381362
setup_shared_memory(&mut interpreter.shared_memory, y_parity, &r, &B256::ZERO);
382363

383364
let mut evm = setup_evm();
384-
let mut context = default_context();
385365

386-
auth_instruction(&mut interpreter, &mut evm, &mut context);
366+
auth_instruction(&mut interpreter, &mut evm, &CustomContext::default());
387367

388368
assert_eq!(interpreter.instruction_result, InstructionResult::Stop);
389369

0 commit comments

Comments
 (0)