pallet-evm: optional nonce parameter (#4893)

* pallet-evm: optional nonce parameter

* Consume all gases when nonce mismatches

* Bump node runtime version
This commit is contained in:
Wei Tang
2020-02-11 18:52:59 +01:00
committed by GitHub
parent a90b4fdb8c
commit b7582a4645
2 changed files with 24 additions and 16 deletions
+2 -2
View File
@@ -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,
};
+22 -14
View File
@@ -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)
}
@@ -197,6 +197,8 @@ decl_error! {
ExitReasonRevert,
/// Call returned VM fatal error
ExitReasonFatal,
/// Nonce is invalid
InvalidNonce,
}
}
@@ -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);
@@ -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,
@@ -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);
@@ -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,