-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Use stateful instruction in AUTH #22
Conversation
d93216c
to
0d4bedd
Compare
f27d596
to
0056c71
Compare
691ae79
to
7ed825b
Compare
7ed825b
to
08f6307
Compare
crates/instructions/src/eip3074.rs
Outdated
}; | ||
|
||
ctx.set_named_variable(AUTORIZED_VAR_NAME, Vec::from(to_persist_authority)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see we're setting this but, we don't read from it yet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
authcall will read from this, auth doesn't afaik
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
interp.shared_memory.resize(rounded_size); | ||
} | ||
} | ||
resize_memory!(interp, offset, length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!
/// Context variables to be used in instructions. The data set here is expected | ||
/// to live for the duration of a single transaction. | ||
/// Similar to TStore for arbitrary data. | ||
pub struct InstructionsContext { | ||
/// Contains the actual variables. Is meant to be accessed both for reads | ||
/// and writes using interior mutability, so that the Instruction and | ||
/// BoxedInstruction signatures are observed. | ||
inner: Rc<RefCell<HashMap<&'static str, Vec<u8>>>>, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the docs here are great
let to_addr = address!("ffffffffffffffffffffffffffffffffffffffff"); | ||
|
||
// initialize the custom context and make sure it's None for a given key | ||
let custom_context = InstructionsContext::default(); | ||
let key = "my-key"; | ||
assert_eq!(custom_context.get(key), None); | ||
|
||
let to_capture_instructions = custom_context.clone(); | ||
let to_capture_post_execution = custom_context.clone(); | ||
let mut evm = Evm::builder() | ||
.with_db(InMemoryDB::default()) | ||
.modify_db(|db| { | ||
db.insert_account_info(to_addr, AccountInfo::new(U256::ZERO, 0, code_hash, code)) | ||
}) | ||
.modify_tx_env(|tx| tx.transact_to = TransactTo::Call(to_addr)) | ||
.append_handler_register_box(Box::new(move |handler| { | ||
let writer_context = to_capture_instructions.clone(); | ||
let writer_instruction = Box::new( | ||
move |_interp: &mut Interpreter, _host: &mut Evm<'_, (), InMemoryDB>| { | ||
// write into the context variable. | ||
writer_context.set(key, vec![0x01, 0x02]); | ||
}, | ||
); | ||
let reader_context = to_capture_instructions.clone(); | ||
let reader_instruction = Box::new( | ||
move |_interp: &mut Interpreter, _host: &mut Evm<'_, (), InMemoryDB>| { | ||
// read from context variable and clear. | ||
assert_eq!(reader_context.get(key).unwrap(), vec![0x01, 0x02]); | ||
}, | ||
); | ||
|
||
let mut table = handler.take_instruction_table(); | ||
table = table.map(|mut table| { | ||
table.insert_boxed(0xEE, writer_instruction); | ||
table.insert_boxed(0xEF, reader_instruction); | ||
table | ||
}); | ||
handler.instruction_table = table; | ||
|
||
let post_execution_context = to_capture_post_execution.clone(); | ||
#[allow(clippy::arc_with_non_send_sync)] | ||
{ | ||
handler.post_execution.end = Arc::new(move |_, outcome: _| { | ||
post_execution_context.clear(); | ||
outcome | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
love the test
Towards #5
Includes the changes from bluealloy/revm#1194 to use stateful instructions.
CustomContext
with an address for bothAUTH
andAUTHCALL
AUTH
implementation to modify the context's address with the extracted authority (default address if the process failed).