Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
pallet-evm: optional nonce parameter (#4893)
Browse files Browse the repository at this point in the history
* pallet-evm: optional nonce parameter

* Consume all gases when nonce mismatches

* Bump node runtime version
  • Loading branch information
sorpaas authored Feb 11, 2020
1 parent 657484a commit 7647c39
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 215,
impl_version: 2,
spec_version: 216,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
};

Expand Down
36 changes: 22 additions & 14 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,19 @@ impl Precompiles for () {

struct WeightForCallCreate;

impl WeighData<(&H160, &Vec<u8>, &U256, &u32, &U256)> for WeightForCallCreate {
impl WeighData<(&H160, &Vec<u8>, &U256, &u32, &U256, &Option<U256>)> for WeightForCallCreate {
fn weigh_data(
&self,
(_, _, _, gas_provided, gas_price): (&H160, &Vec<u8>, &U256, &u32, &U256)
(_, _, _, gas_provided, gas_price, _): (&H160, &Vec<u8>, &U256, &u32, &U256, &Option<U256>)
) -> Weight {
(*gas_price).saturated_into::<Weight>().saturating_mul(*gas_provided)
}
}

impl WeighData<(&Vec<u8>, &U256, &u32, &U256)> for WeightForCallCreate {
impl WeighData<(&Vec<u8>, &U256, &u32, &U256, &Option<U256>)> for WeightForCallCreate {
fn weigh_data(
&self,
(_, _, gas_provided, gas_price): (&Vec<u8>, &U256, &u32, &U256)
(_, _, gas_provided, gas_price, _): (&Vec<u8>, &U256, &u32, &U256, &Option<U256>)
) -> Weight {
(*gas_price).saturated_into::<Weight>().saturating_mul(*gas_provided)
}
Expand Down Expand Up @@ -197,6 +197,8 @@ decl_error! {
ExitReasonRevert,
/// Call returned VM fatal error
ExitReasonFatal,
/// Nonce is invalid
InvalidNonce,
}
}

Expand Down Expand Up @@ -258,6 +260,7 @@ decl_module! {
value: U256,
gas_limit: u32,
gas_price: U256,
nonce: Option<U256>,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
ensure!(gas_price >= T::FeeCalculator::min_gas_price(), Error::<T>::GasPriceTooLow);
Expand All @@ -278,13 +281,15 @@ decl_module! {

let total_fee = gas_price.checked_mul(U256::from(gas_limit))
.ok_or(Error::<T>::FeeOverflow)?;
if Accounts::get(&source).balance <
value.checked_add(total_fee).ok_or(Error::<T>::PaymentOverflow)?
{
Err(Error::<T>::BalanceLow)?
}
let total_payment = value.checked_add(total_fee).ok_or(Error::<T>::PaymentOverflow)?;
let source_account = Accounts::get(&source);
ensure!(source_account.balance >= total_payment, Error::<T>::BalanceLow);
executor.withdraw(source, total_fee).map_err(|_| Error::<T>::WithdrawFailed)?;

if let Some(nonce) = nonce {
ensure!(source_account.nonce == nonce, Error::<T>::InvalidNonce);
}

let reason = executor.transact_call(
source,
target,
Expand Down Expand Up @@ -317,6 +322,7 @@ decl_module! {
value: U256,
gas_limit: u32,
gas_price: U256,
nonce: Option<U256>,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
ensure!(gas_price >= T::FeeCalculator::min_gas_price(), Error::<T>::GasPriceTooLow);
Expand All @@ -338,13 +344,15 @@ decl_module! {

let total_fee = gas_price.checked_mul(U256::from(gas_limit))
.ok_or(Error::<T>::FeeOverflow)?;
if Accounts::get(&source).balance <
value.checked_add(total_fee).ok_or(Error::<T>::PaymentOverflow)?
{
Err(Error::<T>::BalanceLow)?
}
let total_payment = value.checked_add(total_fee).ok_or(Error::<T>::PaymentOverflow)?;
let source_account = Accounts::get(&source);
ensure!(source_account.balance >= total_payment, Error::<T>::BalanceLow);
executor.withdraw(source, total_fee).map_err(|_| Error::<T>::WithdrawFailed)?;

if let Some(nonce) = nonce {
ensure!(source_account.nonce == nonce, Error::<T>::InvalidNonce);
}

let create_address = executor.create_address(source, evm::CreateScheme::Dynamic);
let reason = executor.transact_create(
source,
Expand Down

0 comments on commit 7647c39

Please sign in to comment.