Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions modules/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,6 @@ pub mod module {
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(T::GasToWeight::convert(*gas_limit))]
#[transactional]
pub fn eth_call(
origin: OriginFor<T>,
action: TransactionAction,
Expand All @@ -550,7 +549,6 @@ pub mod module {
/// - `gas_limit`: the maximum gas the call can use
/// - `storage_limit`: the total bytes the contract's storage can increase by
#[pallet::weight(T::GasToWeight::convert(*gas_limit))]
#[transactional]
pub fn call(
origin: OriginFor<T>,
target: EvmAddress,
Expand Down Expand Up @@ -648,7 +646,6 @@ pub mod module {
/// - `gas_limit`: the maximum gas the call can use
/// - `storage_limit`: the total bytes the contract's storage can increase by
#[pallet::weight(T::GasToWeight::convert(*gas_limit))]
#[transactional]
pub fn create(
origin: OriginFor<T>,
init: Vec<u8>,
Expand Down Expand Up @@ -678,7 +675,6 @@ pub mod module {
/// - `gas_limit`: the maximum gas the call can use
/// - `storage_limit`: the total bytes the contract's storage can increase by
#[pallet::weight(T::GasToWeight::convert(*gas_limit))]
#[transactional]
pub fn create2(
origin: OriginFor<T>,
init: Vec<u8>,
Expand Down Expand Up @@ -708,7 +704,6 @@ pub mod module {
/// - `gas_limit`: the maximum gas the call can use
/// - `storage_limit`: the total bytes the contract's storage can increase by
#[pallet::weight(T::GasToWeight::convert(*gas_limit))]
#[transactional]
pub fn create_nft_contract(
origin: OriginFor<T>,
init: Vec<u8>,
Expand Down Expand Up @@ -742,7 +737,6 @@ pub mod module {
/// - `gas_limit`: the maximum gas the call can use
/// - `storage_limit`: the total bytes the contract's storage can increase by
#[pallet::weight(T::GasToWeight::convert(*gas_limit))]
#[transactional]
Comment thread
zjb0807 marked this conversation as resolved.
pub fn create_predeploy_contract(
origin: OriginFor<T>,
target: EvmAddress,
Expand Down Expand Up @@ -1113,6 +1107,18 @@ impl<T: Config> Pallet<T> {
Self::codes(&Self::code_hash_at_address(address))
}

pub fn inc_nonce(address: H160) {
Accounts::<T>::mutate(&address, |maybe_account| {
if let Some(account) = maybe_account.as_mut() {
account.nonce += One::one()
} else {
let mut account_info = <AccountInfo<T::Index>>::new(Default::default(), None);
account_info.nonce += One::one();
*maybe_account = Some(account_info);
}
});
}

pub fn update_contract_storage_size(address: &EvmAddress, change: i32) {
if change == 0 {
return;
Expand Down
23 changes: 12 additions & 11 deletions modules/evm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ use crate::{
state::{StackExecutor, StackSubstateMetadata},
Runner as RunnerT, StackState as StackStateT,
},
AccountInfo, AccountStorages, Accounts, BalanceOf, CallInfo, Config, CreateInfo, Error, Event, ExecutionInfo, One,
Pallet, STORAGE_SIZE,
AccountStorages, BalanceOf, CallInfo, Config, CreateInfo, Error, Event, ExecutionInfo, Pallet, STORAGE_SIZE,
};
use frame_support::{
dispatch::DispatchError,
ensure, log,
traits::{Currency, ExistenceRequirement, Get},
transactional,
};
use module_evm_utiltity::{
ethereum::Log,
Expand All @@ -55,6 +55,7 @@ pub struct Runner<T: Config> {

impl<T: Config> Runner<T> {
/// Execute an EVM operation.
#[transactional]
pub fn execute<'config, F, R>(
source: H160,
origin: H160,
Expand Down Expand Up @@ -228,6 +229,8 @@ impl<T: Config> RunnerT<T> for Runner<T> {
Error::<T>::NoPermission
);

Pallet::<T>::inc_nonce(origin);

let value = U256::from(UniqueSaturatedInto::<u128>::unique_saturated_into(value));
let info = Self::execute(source, origin, value, gas_limit, storage_limit, config, |executor| {
// TODO: EIP-2930
Expand Down Expand Up @@ -273,6 +276,8 @@ impl<T: Config> RunnerT<T> for Runner<T> {
)
})?;

Pallet::<T>::inc_nonce(source);
Comment thread
zjb0807 marked this conversation as resolved.
Outdated

if info.exit_reason.is_succeed() {
Pallet::<T>::deposit_event(Event::<T>::Created {
from: source,
Expand Down Expand Up @@ -317,6 +322,8 @@ impl<T: Config> RunnerT<T> for Runner<T> {
)
})?;

Pallet::<T>::inc_nonce(source);
Comment thread
zjb0807 marked this conversation as resolved.
Outdated

if info.exit_reason.is_succeed() {
Pallet::<T>::deposit_event(Event::<T>::Created {
from: source,
Expand Down Expand Up @@ -353,6 +360,8 @@ impl<T: Config> RunnerT<T> for Runner<T> {
)
})?;

Pallet::<T>::inc_nonce(source);
Comment thread
zjb0807 marked this conversation as resolved.
Outdated

if info.exit_reason.is_succeed() {
Pallet::<T>::deposit_event(Event::<T>::Created {
from: source,
Expand Down Expand Up @@ -607,15 +616,7 @@ impl<'vicinity, 'config, T: Config> StackStateT<'config> for SubstrateStackState
}

fn inc_nonce(&mut self, address: H160) {
Accounts::<T>::mutate(&address, |maybe_account| {
if let Some(account) = maybe_account.as_mut() {
account.nonce += One::one()
} else {
let mut account_info = <AccountInfo<T::Index>>::new(Default::default(), None);
account_info.nonce += One::one();
*maybe_account = Some(account_info);
}
});
Pallet::<T>::inc_nonce(address);
}

fn set_storage(&mut self, address: H160, index: H256, value: H256) {
Expand Down
119 changes: 74 additions & 45 deletions modules/evm/src/runner/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,25 @@ impl<'config, S: StackState<'config>> StackExecutor<'config, S> {
Err(e) => return e.into(),
}

match self.create_inner(
let scheme = CreateScheme::Legacy { caller };

let address = match self.create_address(scheme) {
Err(e) => return ExitReason::Error(e),
Ok(address) => address,
};

let target_gas = Some(gas_limit);

event!(Create {
caller,
CreateScheme::Legacy { caller },
address,
scheme,
value,
init_code,
Some(gas_limit),
false,
) {
init_code: &init_code,
target_gas
});

match self.create_inner_at(address, caller, value, init_code, target_gas, false) {
Capture::Exit((s, _, _)) => s,
Capture::Trap(_) => unreachable!(),
}
Expand Down Expand Up @@ -309,18 +320,29 @@ impl<'config, S: StackState<'config>> StackExecutor<'config, S> {
}
let code_hash = H256::from_slice(Keccak256::digest(&init_code).as_slice());

match self.create_inner(
let scheme = CreateScheme::Create2 {
caller,
CreateScheme::Create2 {
caller,
code_hash,
salt,
},
code_hash,
salt,
};

let address = match self.create_address(scheme) {
Err(e) => return ExitReason::Error(e),
Ok(address) => address,
};

let target_gas = Some(gas_limit);

event!(Create {
caller,
address,
scheme,
value,
init_code,
Some(gas_limit),
false,
) {
init_code: &init_code,
target_gas
});

match self.create_inner_at(address, caller, value, init_code, target_gas, false) {
Capture::Exit((s, _, _)) => s,
Capture::Trap(_) => unreachable!(),
}
Expand All @@ -347,14 +369,25 @@ impl<'config, S: StackState<'config>> StackExecutor<'config, S> {
Err(e) => return e.into(),
}

match self.create_inner(
let scheme = CreateScheme::Fixed(address);

let address = match self.create_address(scheme) {
Err(e) => return ExitReason::Error(e),
Ok(address) => address,
};

let target_gas = Some(gas_limit);

event!(Create {
caller,
CreateScheme::Fixed(address),
address,
scheme,
value,
init_code,
Some(gas_limit),
false,
) {
init_code: &init_code,
target_gas
});

match self.create_inner_at(address, caller, value, init_code, target_gas, false) {
Capture::Exit((s, _, _)) => s,
Capture::Trap(_) => unreachable!(),
}
Expand All @@ -381,8 +414,6 @@ impl<'config, S: StackState<'config>> StackExecutor<'config, S> {
Err(e) => return (e.into(), Vec::new()),
}

self.state.inc_nonce(caller);

let context = Context {
caller,
address,
Expand Down Expand Up @@ -505,10 +536,10 @@ impl<'config, S: StackState<'config>> StackExecutor<'config, S> {
}
}

fn create_inner(
fn create_inner_at(
&mut self,
address: H160,
caller: H160,
scheme: CreateScheme,
value: U256,
init_code: Vec<u8>,
target_gas: Option<u64>,
Expand All @@ -527,25 +558,9 @@ impl<'config, S: StackState<'config>> StackExecutor<'config, S> {
gas - gas / 64
}

let address = match self.create_address(scheme) {
Err(e) => {
return Capture::Exit((ExitReason::Error(e), None, Vec::new()));
}
Ok(address) => address,
};

*self.state.metadata_mut().caller_mut() = Some(caller);
*self.state.metadata_mut().target_mut() = Some(address);

event!(Create {
caller,
address,
scheme,
value,
init_code: &init_code,
target_gas
});

if let Some(depth) = self.state.metadata().depth() {
if depth > self.config.call_stack_limit {
return Capture::Exit((ExitError::CallTooDeep.into(), None, Vec::new()));
Expand Down Expand Up @@ -574,8 +589,6 @@ impl<'config, S: StackState<'config>> StackExecutor<'config, S> {
let gas_limit = min(after_gas, target_gas);
try_or_fail!(self.state.metadata_mut().gasometer_mut().record_cost(gas_limit));

self.state.inc_nonce(caller);

self.enter_substate(gas_limit, false);

{
Expand Down Expand Up @@ -933,7 +946,23 @@ impl<'config, S: StackState<'config>> Handler for StackExecutor<'config, S> {
init_code: Vec<u8>,
target_gas: Option<u64>,
) -> Capture<(ExitReason, Option<H160>, Vec<u8>), Self::CreateInterrupt> {
self.create_inner(caller, scheme, value, init_code, target_gas, true)
let address = match self.create_address(scheme) {
Err(e) => return Capture::Exit((ExitReason::Error(e), None, Vec::new())),
Ok(address) => address,
};

self.state.inc_nonce(caller);

event!(Create {
caller,
address,
scheme,
value,
init_code: &init_code,
target_gas
});

self.create_inner_at(address, caller, value, init_code, target_gas, true)
}

fn call(
Expand Down
Loading