-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
dc0277f
added the rest of bls12-381 precompile entrypoints
fgimenez 52d8451
add g1_add test and first failing case
fgimenez 33d2134
initial implementation
fgimenez 18dcbb5
remove impl functions
fgimenez 9cccfe7
use bls12_381 crate, g1add complete
fgimenez 86cd95c
more tests and take into account point of inifinity; more constants
fgimenez 9f36fe0
typo
fgimenez 41074be
add insert_precompiles function
fgimenez a8e2a48
feat: set authorized context variable in AUTH
fgimenez 97bb353
setup context
fgimenez 95a8bf4
set authority
fgimenez 7f70f29
use revm_precompile::u64_to_address
fgimenez 1fa0aaf
use revm_precompile::u64_to_address
fgimenez 4990005
pass CustomContext to instructions
fgimenez 9ca2468
removed leftovers
fgimenez 77aedcd
properly set contexts for each instruction
fgimenez 56e41a9
add missing lifetime and fix variable name
fgimenez a7728c5
use macros from revm_interpreter
fgimenez ab34b06
CustomContext -> Eip3074Context
fgimenez 9278852
add Eip3074Context docs
fgimenez 73fc46a
check authority context variable is set
fgimenez ce43e11
authority -> authorized
fgimenez 5165e33
generalized instructions context
fgimenez b85916e
get and set methods for InstructionsContext
fgimenez 4f86b33
refactor and &self for InstructionsContext.reset
fgimenez 1045560
get and set named variables
fgimenez a7b8e0f
instruction context submodule and test with context writer and reader…
fgimenez 4478554
example of using Inspector to clear instruction context
fgimenez 08f6307
use EndHandle to clear context instructions instead of inspector
fgimenez 860d5d0
InstructionContext handles &'static str keys
fgimenez 29c2f0b
allow clippy
fgimenez 354ff23
InstructionsContext doc
fgimenez 8f397e1
simplify InstructionContext methods
fgimenez 821d4d9
remove inspector
fgimenez cec20f1
typo
fgimenez 0ad5948
merge handler_registers
fgimenez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use std::{cell::RefCell, collections::HashMap, rc::Rc}; | ||
|
||
#[derive(Clone, Default)] | ||
/// 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>>>>, | ||
} | ||
|
||
impl InstructionsContext { | ||
/// Sets a value for the given key. | ||
pub fn set(&self, key: &'static str, value: Vec<u8>) { | ||
let _ = self.inner.borrow_mut().insert(key, value); | ||
} | ||
|
||
/// Gets the value for the given key, if any. | ||
pub fn get(&self, key: &'static str) -> Option<Vec<u8>> { | ||
self.inner.borrow().get(&key).cloned() | ||
} | ||
|
||
/// Empties inner state. | ||
pub fn clear(&self) { | ||
self.inner.borrow_mut().clear(); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use revm::{Evm, InMemoryDB}; | ||
use revm_interpreter::Interpreter; | ||
use revm_primitives::{address, AccountInfo, Bytecode, TransactTo, U256}; | ||
use std::sync::Arc; | ||
|
||
#[test] | ||
fn test_set_get() { | ||
let ctx = InstructionsContext::default(); | ||
let key = "my-key"; | ||
let value = vec![0x01, 0x02]; | ||
|
||
ctx.set(key, value.clone()); | ||
|
||
let cloned_ctx = ctx.clone(); | ||
assert_eq!(cloned_ctx.get(key).unwrap(), value); | ||
} | ||
|
||
#[test] | ||
fn test_context_variables_are_available_during_tx() { | ||
let code = Bytecode::new_raw([0xEE, 0xEF, 0x00].into()); | ||
let code_hash = code.hash_slow(); | ||
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 | ||
}); | ||
Comment on lines
+55
to
+100
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. love the test |
||
} | ||
})) | ||
.build(); | ||
|
||
let _result_and_state = evm.transact().unwrap(); | ||
|
||
// ensure the custom context was cleared | ||
assert_eq!(custom_context.get(key), None); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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