Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions chain-evm/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ use ethereum_types::{H160, H256, U256};
use evm::{
backend::{Backend, Basic},
executor::stack::{Accessed, StackExecutor, StackState, StackSubstateMetadata},
Context, ExitFatal, ExitReason, ExitRevert, Transfer,
Context, CreateScheme, ExitFatal, ExitReason, ExitRevert, Transfer,
};
use ripemd::Digest;
use sha3::Keccak256;
use std::collections::{BTreeMap, BTreeSet};
use thiserror::Error;

Expand Down Expand Up @@ -219,6 +221,36 @@ impl<'a, T> VirtualMachine<'a, T> {
}
}

pub fn generate_address_create<State: EvmState>(
vm: VirtualMachine<State>,
caller: Address,
) -> Address {
let precompiles = Precompiles::new();
let config = vm.config;
let executor = StackExecutor::new_with_precompiles(vm, config, &precompiles);

executor.create_address(CreateScheme::Legacy { caller })
}

pub fn generate_address_create2<State: EvmState>(
vm: VirtualMachine<State>,
caller: Address,
init_code: ByteCode,
salt: H256,
) -> Address {
let precompiles = Precompiles::new();
let config = vm.config;
let executor = StackExecutor::new_with_precompiles(vm, config, &precompiles);

let code_hash = H256::from_slice(Keccak256::digest(&init_code).as_slice());

executor.create_address(CreateScheme::Create2 {
caller,
code_hash,
salt,
})
}

/// Top-level abstraction for the EVM with the
/// necessary types used to get the runtime going.
fn execute_transaction<State: EvmState, F, T>(vm: VirtualMachine<State>, f: F) -> Result<T, Error>
Expand All @@ -231,7 +263,6 @@ where
let config = vm.config;
let gas_price = vm.gas_price();

// let memory_stack_state = MemoryStackState::new(vm.substate.metadata.clone(), &vm);
let mut executor = StackExecutor::new_with_precompiles(vm, config, &precompiles);

let (exit_reason, val) = f(&mut executor);
Expand Down
43 changes: 13 additions & 30 deletions chain-evm/src/precompiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,38 +73,21 @@ pub struct Precompiles(pub prelude::BTreeMap<prelude::Address, PrecompileFn>);

impl Precompiles {
pub fn new() -> Self {
let addresses = prelude::vec![
ECRecover::ADDRESS,
SHA256::ADDRESS,
RIPEMD160::ADDRESS,
Identity::ADDRESS,
ModExp::ADDRESS,
Bn128Add::ADDRESS,
Bn128Mul::ADDRESS,
Bn128Pair::ADDRESS,
Blake2F::ADDRESS,
ExitToNear::ADDRESS,
ExitToEthereum::ADDRESS,
let fun: Vec<(prelude::Address, PrecompileFn)> = vec![
(ECRecover::ADDRESS, ECRecover::run),
(SHA256::ADDRESS, SHA256::run),
(RIPEMD160::ADDRESS, RIPEMD160::run),
(Identity::ADDRESS, Identity::run),
(ModExp::ADDRESS, ModExp::run),
(Bn128Add::ADDRESS, Bn128Add::run),
(Bn128Mul::ADDRESS, Bn128Mul::run),
(Bn128Pair::ADDRESS, Bn128Pair::run),
(Blake2F::ADDRESS, Blake2F::run),
(ExitToNear::ADDRESS, ExitToNear::run),
(ExitToEthereum::ADDRESS, ExitToEthereum::run),
];
let fun: prelude::Vec<PrecompileFn> = prelude::vec![
ECRecover::run,
SHA256::run,
RIPEMD160::run,
Identity::run,
ModExp::run,
Bn128Add::run,
Bn128Mul::run,
Bn128Pair::run,
Blake2F::run,
ExitToNear::run,
ExitToEthereum::run,
];
let mut map = prelude::BTreeMap::new();
for (address, fun) in addresses.into_iter().zip(fun) {
map.insert(address, fun);
}

Precompiles(map)
Precompiles(fun.into_iter().collect())
}
}

Expand Down
6 changes: 2 additions & 4 deletions chain-evm/src/state/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,13 @@ pub struct AccountState {

impl Account {
pub fn is_empty(&self) -> bool {
self.state.nonce.is_zero()
&& self.balance == Balance::zero()
&& self.state.storage.is_empty()
self.balance == Balance::zero() && self.state.is_empty()
}
}

impl AccountState {
pub fn is_empty(&self) -> bool {
self.nonce == Nonce::zero() && self.code.is_empty() && self.storage.is_empty()
self.nonce.is_zero() && self.code.is_empty() && self.storage.is_empty()
}
}

Expand Down
Loading