Skip to content

Commit

Permalink
Merge pull request #11 from init4tech/prestwich/context
Browse files Browse the repository at this point in the history
Prestwich/context
  • Loading branch information
prestwich authored Jul 23, 2024
2 parents 6145bac + 7a704d8 commit 2624b94
Show file tree
Hide file tree
Showing 15 changed files with 1,299 additions and 1,013 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ as the block is already open. You can't call `close_block()` on a `EvmReady`
state, without explicitly clearing or running the transaction that has been
ready.

![typestates are cool](./assets/Screenshot%202024-07-17%20at%2015.19.27.png)
![typestates are cool](./assets/states.png)

[typestate]: https://cliffle.com/blog/rust-typestate/
[revm]: https://github.com/bluealloy/revm
Expand Down
Binary file removed assets/Screenshot 2024-07-17 at 15.19.27.png
Binary file not shown.
Binary file added assets/states.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 10 additions & 8 deletions examples/basic_transact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
//! It simply loads the contract bytecode and executes a transaction.
use revm::{
db::{CacheDB, EmptyDB},
inspector_handle_register,
inspectors::TracerEip3155,
primitives::{hex, AccountInfo, Address, Bytecode, TransactTo, U256},
EvmBuilder,
EvmBuilder, InMemoryDB,
};
use trevm::{Block, Cfg, ShanghaiLifecycle, TrevmBuilder, Tx};
use trevm::{trevm_aliases, Block, Cfg, Shanghai, TrevmBuilder, Tx};

/// Foundry's default Counter.sol contract bytecode.
const CONTRACT_BYTECODE: &str = "0x6080604052348015600f57600080fd5b5060043610603c5760003560e01c80633fb5c1cb1460415780638381f58a146053578063d09de08a14606d575b600080fd5b6051604c3660046083565b600055565b005b605b60005481565b60405190815260200160405180910390f35b6051600080549080607c83609b565b9190505550565b600060208284031215609457600080fd5b5035919050565b60006001820160ba57634e487b7160e01b600052601160045260246000fd5b506001019056fea2646970667358221220091e48831e9eee32d4571d6291233a4fdaaa34b7dced8770f36f5368be825c5264736f6c63430008190033";
Expand Down Expand Up @@ -43,8 +42,11 @@ impl Tx for SampleTx {
}
}

// Produce aliases for the Trevm type
trevm_aliases!(TracerEip3155, InMemoryDB);

fn main() {
let mut db = CacheDB::new(EmptyDB::new());
let mut db = revm::InMemoryDB::default();

let bytecode = Bytecode::new_raw(hex::decode(CONTRACT_BYTECODE).unwrap().into());
let acc_info = AccountInfo::new(U256::ZERO, 1, bytecode.hash_slow(), bytecode);
Expand All @@ -53,7 +55,7 @@ fn main() {
db.insert_contract(&mut acc_info.clone());
db.insert_account_info(CONTRACT_ADDR, acc_info);

let evm = EvmBuilder::default()
let evm: EvmNeedsCfg = EvmBuilder::default()
.with_db(db)
.with_external_context(TracerEip3155::new(Box::new(std::io::stdout())))
.append_handler_register(inspector_handle_register)
Expand All @@ -63,11 +65,11 @@ fn main() {

println!("account: {account:?}");

let evm = evm.fill_cfg(&NoopCfg);
let evm: EvmNeedsFirstBlock = evm.fill_cfg(&NoopCfg);

let evm = evm.open_block(&NoopBlock, &mut ShanghaiLifecycle::default()).unwrap();
let evm = evm.open_block(&NoopBlock, Shanghai::default()).unwrap();

let evm = evm.fill_tx(&SampleTx).execute_tx();
let evm = evm.fill_tx(&SampleTx).run();

match evm {
Ok(res) => {
Expand Down
63 changes: 21 additions & 42 deletions examples/fork_ref_transact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use alloy_provider::ProviderBuilder;
use alloy_sol_types::sol;
use alloy_sol_types::SolCall;
use revm::{
db::{AlloyDB, CacheDB, EmptyDB},
primitives::{address, ExecutionResult, TxKind, U256},
Database, Evm,
db::{AlloyDB, CacheDB},
primitives::{address, TxKind, U256},
Evm,
};
use trevm::Block;
use trevm::Cfg;
use trevm::ShanghaiLifecycle;
use trevm::Shanghai;
use trevm::TrevmBuilder;
use trevm::Tx;

Expand Down Expand Up @@ -66,51 +66,30 @@ async fn main() -> eyre::Result<()> {
// storage[11] = kLast: uint256 //
// =========================================================== //

// choose slot of storage that you would like to transact with
let slot = U256::from(8);

// initialize new AlloyDB
let mut alloydb = AlloyDB::new(client, BlockId::default()).unwrap();

// query basic properties of an account incl bytecode
let acc_info = alloydb.basic(POOL_ADDRESS).unwrap().unwrap();

// query value of storage slot at account address
let value = alloydb.storage(POOL_ADDRESS, slot).unwrap();
let alloydb = AlloyDB::new(client, BlockId::default()).unwrap();

// initialise empty in-memory-db
let mut cache_db = CacheDB::new(EmptyDB::default());

// insert basic account info which was generated via Web3DB with the corresponding address
cache_db.insert_account_info(POOL_ADDRESS, acc_info);

// insert our pre-loaded storage slot to the corresponding contract key (address) in the DB
cache_db.insert_account_storage(POOL_ADDRESS, slot, value).unwrap();
let cache_db = CacheDB::new(alloydb);

// initialise an empty (default) EVM
let evm = Evm::builder().with_db(cache_db).build_trevm();

let evm = evm.fill_cfg(&NoopCfg);
let evm = evm.open_block(&NoopBlock, &mut ShanghaiLifecycle::default()).unwrap();

let evm = evm.fill_tx(&GetReservesFiller).execute_tx();

let value = match evm {
Ok(res) => {
let exec_result = res.result();
println!("Execution result: {:#?}", res.result());
match exec_result {
ExecutionResult::Success { output, .. } => output.to_owned(),
_ => panic!("Execution failed"),
}
}
Err(e) => {
panic!("Execution error: {e:?}");
}
};
let evm = Evm::builder()
.with_db(cache_db)
.build_trevm()
.fill_cfg(&NoopCfg)
.open_block(&NoopBlock, Shanghai::default())
.unwrap()
.fill_tx(&GetReservesFiller)
.run()
.inspect_err(|e| panic!("Execution error {e:?}"))
.unwrap();

// Inspect the outcome of a transaction execution, and get the return value
println!("Execution result: {:#?}", evm.result());
let output = evm.output().expect("Execution halted");

// decode bytes to reserves + ts via alloy's abi decode
let return_vals = getReservesCall::abi_decode_returns(value.data(), true)?;
let return_vals = getReservesCall::abi_decode_returns(output, true)?;

// Print emulated getReserves() call output
println!("Reserve0: {:#?}", return_vals.reserve0);
Expand Down
Loading

0 comments on commit 2624b94

Please sign in to comment.