-
Notifications
You must be signed in to change notification settings - Fork 562
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(example): deploy bytecode from scratch #1767
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cc2e87e
feat(example): deploy bytecode from stratch
clearloop 56f5914
feat(example): passing constructor parameters via creation code
clearloop a642002
chore(example): use opcode constants for example deploy
clearloop fe366e0
chore(example): typo EOF
clearloop 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
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,65 @@ | ||
use anyhow::{anyhow, bail}; | ||
use revm::{ | ||
primitives::{bytes::Bytes, hex, EthereumWiring, ExecutionResult, Output, TxKind, U256}, | ||
Evm, InMemoryDB, | ||
}; | ||
|
||
/// [ PUSH1, 0x01, PUSH1, 0x17, PUSH1, 0x1f, CODECOPY, PUSH0, MLOAD, PUSH0, SSTORE ] | ||
const INIT_CODE: &[u8] = &[ | ||
0x60, 0x01, 0x60, 0x17, 0x60, 0x1f, 0x39, 0x5f, 0x51, 0x5f, 0x55, | ||
]; | ||
|
||
/// [ PUSH1, 0x02, PUSH1, 0x15, PUSH0, CODECOPY, PUSH1, 0x02, PUSH0, RETURN] | ||
const RET: &[u8] = &[0x60, 0x02, 0x60, 0x15, 0x5f, 0x39, 0x60, 0x02, 0x5f, 0xf3]; | ||
|
||
/// [ PUSH0 SLOAD ] | ||
const RUNTIME_BYTECODE: &[u8] = &[0x5f, 0x54]; | ||
|
||
fn main() -> anyhow::Result<()> { | ||
let param = 0x42; | ||
let bytecode: Bytes = [INIT_CODE, RET, RUNTIME_BYTECODE, &[param]].concat().into(); | ||
let mut evm: Evm<'_, EthereumWiring<InMemoryDB, ()>> = | ||
Evm::<EthereumWiring<InMemoryDB, ()>>::builder() | ||
.with_default_db() | ||
.with_default_ext_ctx() | ||
.modify_tx_env(|tx| { | ||
tx.transact_to = TxKind::Create; | ||
*tx.data = bytecode.clone(); | ||
}) | ||
.build(); | ||
|
||
println!("bytecode: {}", hex::encode(bytecode)); | ||
let ref_tx = evm.transact_commit()?; | ||
let ExecutionResult::Success { | ||
output: Output::Create(_, Some(address)), | ||
.. | ||
} = ref_tx | ||
else { | ||
bail!("Failed to create contract: {ref_tx:#?}"); | ||
}; | ||
|
||
println!("Created contract at {address}"); | ||
evm = evm | ||
.modify() | ||
.modify_tx_env(|tx| { | ||
tx.transact_to = TxKind::Call(address); | ||
*tx.data = Default::default(); | ||
tx.nonce += 1; | ||
}) | ||
.build(); | ||
|
||
let result = evm.transact()?; | ||
let Some(storage0) = result | ||
.state | ||
.get(&address) | ||
.ok_or_else(|| anyhow!("Contract not found"))? | ||
.storage | ||
.get::<U256>(&Default::default()) | ||
else { | ||
bail!("Failed to write storage in the init code: {result:#?}"); | ||
}; | ||
|
||
println!("storage U256(0) at{address}: {storage0:#?}"); | ||
assert_eq!(storage0.present_value(), param.try_into()?, "{result:#?}"); | ||
Ok(()) | ||
} |
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.
Nit, you could use exact notion from the comment if you include,
revm::interpreter::opcode::*