Skip to content

Commit

Permalink
[revm] Interface. Inspector added, Env cleanup. revm-test passes
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita committed Oct 27, 2021
1 parent 351d4e0 commit 1b1ebd8
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 166 deletions.
4 changes: 2 additions & 2 deletions bins/revm-ethereum-tests/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use sha3::{Digest, Keccak256};

use indicatif::ProgressBar;
use primitive_types::{H160, H256, U256};
use revm::{CreateScheme, GlobalEnv, Inspector, SpecId, TransactTo};
use revm::{CreateScheme, Env, Inspector, SpecId, TransactTo};
use std::sync::atomic::Ordering;
use walkdir::{DirEntry, WalkDir};

Expand Down Expand Up @@ -132,7 +132,7 @@ pub fn execute_test_suit(
let spec_id = spec_name.to_spec_id();

let block_basefee = unit.env.current_base_fee;
let global_env = GlobalEnv {
let global_env = Env {
gas_max_fee: unit
.transaction
.gas_price
Expand Down
49 changes: 18 additions & 31 deletions bins/revm-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::str::FromStr;

use bytes::Bytes;
use primitive_types::{H160, U256};
use revm::{AccountInfo, DummyStateDB, GlobalEnv, SpecId, TransactOut, TransactTo, KECCAK_EMPTY};
use revm::{AccountInfo, DummyStateDB, TransactOut, TransactTo, KECCAK_EMPTY};

extern crate alloc;

Expand All @@ -22,45 +22,32 @@ pub fn simple_example() {
);

// execution globals block hash/gas_limit/coinbase/timestamp..
let envs = GlobalEnv::default();
let mut evm = revm::new(&mut db);
evm.tx_caller(caller.clone());

let (_, out, _, state) = revm::new(SpecId::BERLIN, envs.clone(), &mut db).transact(
caller.clone(),
TransactTo::create(),
U256::zero(),
Bytes::from(hex::decode("608060405234801561001057600080fd5b50610150806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80632e64cec11461003b5780636057361d14610059575b600080fd5b610043610075565b60405161005091906100d9565b60405180910390f35b610073600480360381019061006e919061009d565b61007e565b005b60008054905090565b8060008190555050565b60008135905061009781610103565b92915050565b6000602082840312156100b3576100b26100fe565b5b60006100c184828501610088565b91505092915050565b6100d3816100f4565b82525050565b60006020820190506100ee60008301846100ca565b92915050565b6000819050919050565b600080fd5b61010c816100f4565b811461011757600080fd5b5056fea2646970667358221220404e37f487a89a932dca5e77faaf6ca2de3b991f93d230604b1b8daaef64766264736f6c63430008070033").unwrap()),
u64::MAX,
Vec::new(),
);
db.apply(state);
evm.tx_transact_to(TransactTo::create());
evm.tx_data(Bytes::from(hex::decode("608060405234801561001057600080fd5b50610150806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80632e64cec11461003b5780636057361d14610059575b600080fd5b610043610075565b60405161005091906100d9565b60405180910390f35b610073600480360381019061006e919061009d565b61007e565b005b60008054905090565b8060008190555050565b60008135905061009781610103565b92915050565b6000602082840312156100b3576100b26100fe565b5b60006100c184828501610088565b91505092915050565b6100d3816100f4565b82525050565b60006020820190506100ee60008301846100ca565b92915050565b6000819050919050565b600080fd5b61010c816100f4565b811461011757600080fd5b5056fea2646970667358221220404e37f487a89a932dca5e77faaf6ca2de3b991f93d230604b1b8daaef64766264736f6c63430008070033").unwrap()));

let (_, out, _) = evm.transact();
let contract_address = match out {
TransactOut::Create(_, Some(add)) => add,
_ => panic!("not gona happen"),
};

let (_, _, _, state) = revm::new(SpecId::BERLIN, envs.clone(), &mut db).transact(
caller,
TransactTo::Call(contract_address),
U256::zero(), // value transfered
Bytes::from(
hex::decode("6057361d0000000000000000000000000000000000000000000000000000000000000015")
.unwrap(),
),
u64::MAX, //gas_limit
Vec::new(), // access_list
evm.tx_transact_to(TransactTo::Call(contract_address));
evm.tx_data(
hex::decode("6057361d0000000000000000000000000000000000000000000000000000000000000015")
.unwrap()
.into(),
);
db.apply(state);
evm.transact();

evm.tx_transact_to(TransactTo::Call(contract_address));
evm.tx_data(hex::decode("2e64cec1").unwrap().into());

let (_, out, _) = evm.transact();

let (_, out, _, state) = revm::new(SpecId::BERLIN, envs.clone(), &mut db).transact(
caller,
TransactTo::Call(contract_address),
U256::zero(), // value transfered
Bytes::from(hex::decode("2e64cec1").unwrap()),
u64::MAX, // gas_limit
Vec::new(), // access_list
);
println!("get value (StaticCall): {:?}\n", out);
db.apply(state);
}

fn main() {
Expand Down
54 changes: 29 additions & 25 deletions crates/revm/src/db/dummy_db_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::{Account, AccountInfo, Log};
use bytes::Bytes;
use sha3::{Digest, Keccak256};

use super::WriteDatabase;

/// Memory backend, storing all state values in a `Map` in memory.
#[derive(Debug, Clone)]
pub struct DummyStateDB {
Expand Down Expand Up @@ -49,31 +51,6 @@ impl DummyStateDB {
self.storage.entry(address).or_default().insert(slot, value);
}

pub fn apply(&mut self, changes: Map<H160, Account>) {
for (add, acc) in changes {
if acc.is_empty() || matches!(acc.filth, Filth::Destroyed) {
self.cache.remove(&add);
self.storage.remove(&add);
} else {
self.insert_cache(add, acc.info);
let storage = self.storage.entry(add.clone()).or_default();
if acc.filth.abandon_old_storage() {
storage.clear();
}
for (index, value) in acc.storage {
if value == H256::zero() {
storage.remove(&index);
} else {
storage.insert(index, value);
}
}
if storage.is_empty() {
self.storage.remove(&add);
}
}
}
}

/// Create a new memory backend.
pub fn new() -> Self {
let mut contracts = Map::new();
Expand Down Expand Up @@ -107,6 +84,33 @@ impl DummyStateDB {
}
}

impl WriteDatabase for DummyStateDB {
fn apply(&mut self, changes: Map<H160, Account>) {
for (add, acc) in changes {
if acc.is_empty() || matches!(acc.filth, Filth::Destroyed) {
self.cache.remove(&add);
self.storage.remove(&add);
} else {
self.insert_cache(add, acc.info);
let storage = self.storage.entry(add.clone()).or_default();
if acc.filth.abandon_old_storage() {
storage.clear();
}
for (index, value) in acc.storage {
if value == H256::zero() {
storage.remove(&index);
} else {
storage.insert(index, value);
}
}
if storage.is_empty() {
self.storage.remove(&add);
}
}
}
}
}

impl Database for DummyStateDB {
fn block_hash(&mut self, _number: U256) -> H256 {
// if number >= self.vicinity.block_number
Expand Down
Loading

0 comments on commit 1b1ebd8

Please sign in to comment.