Skip to content

Commit 74b063c

Browse files
pallet-evm: optional nonce parameter (#4893)
* pallet-evm: optional nonce parameter * Consume all gases when nonce mismatches * Bump node runtime version
1 parent c1f39a6 commit 74b063c

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/lib.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,19 @@ impl Precompiles for () {
117117

118118
struct WeightForCallCreate;
119119

120-
impl WeighData<(&H160, &Vec<u8>, &U256, &u32, &U256)> for WeightForCallCreate {
120+
impl WeighData<(&H160, &Vec<u8>, &U256, &u32, &U256, &Option<U256>)> for WeightForCallCreate {
121121
fn weigh_data(
122122
&self,
123-
(_, _, _, gas_provided, gas_price): (&H160, &Vec<u8>, &U256, &u32, &U256)
123+
(_, _, _, gas_provided, gas_price, _): (&H160, &Vec<u8>, &U256, &u32, &U256, &Option<U256>)
124124
) -> Weight {
125125
(*gas_price).saturated_into::<Weight>().saturating_mul(*gas_provided)
126126
}
127127
}
128128

129-
impl WeighData<(&Vec<u8>, &U256, &u32, &U256)> for WeightForCallCreate {
129+
impl WeighData<(&Vec<u8>, &U256, &u32, &U256, &Option<U256>)> for WeightForCallCreate {
130130
fn weigh_data(
131131
&self,
132-
(_, _, gas_provided, gas_price): (&Vec<u8>, &U256, &u32, &U256)
132+
(_, _, gas_provided, gas_price, _): (&Vec<u8>, &U256, &u32, &U256, &Option<U256>)
133133
) -> Weight {
134134
(*gas_price).saturated_into::<Weight>().saturating_mul(*gas_provided)
135135
}
@@ -197,6 +197,8 @@ decl_error! {
197197
ExitReasonRevert,
198198
/// Call returned VM fatal error
199199
ExitReasonFatal,
200+
/// Nonce is invalid
201+
InvalidNonce,
200202
}
201203
}
202204

@@ -258,6 +260,7 @@ decl_module! {
258260
value: U256,
259261
gas_limit: u32,
260262
gas_price: U256,
263+
nonce: Option<U256>,
261264
) -> DispatchResult {
262265
let sender = ensure_signed(origin)?;
263266
ensure!(gas_price >= T::FeeCalculator::min_gas_price(), Error::<T>::GasPriceTooLow);
@@ -278,13 +281,15 @@ decl_module! {
278281

279282
let total_fee = gas_price.checked_mul(U256::from(gas_limit))
280283
.ok_or(Error::<T>::FeeOverflow)?;
281-
if Accounts::get(&source).balance <
282-
value.checked_add(total_fee).ok_or(Error::<T>::PaymentOverflow)?
283-
{
284-
Err(Error::<T>::BalanceLow)?
285-
}
284+
let total_payment = value.checked_add(total_fee).ok_or(Error::<T>::PaymentOverflow)?;
285+
let source_account = Accounts::get(&source);
286+
ensure!(source_account.balance >= total_payment, Error::<T>::BalanceLow);
286287
executor.withdraw(source, total_fee).map_err(|_| Error::<T>::WithdrawFailed)?;
287288

289+
if let Some(nonce) = nonce {
290+
ensure!(source_account.nonce == nonce, Error::<T>::InvalidNonce);
291+
}
292+
288293
let reason = executor.transact_call(
289294
source,
290295
target,
@@ -317,6 +322,7 @@ decl_module! {
317322
value: U256,
318323
gas_limit: u32,
319324
gas_price: U256,
325+
nonce: Option<U256>,
320326
) -> DispatchResult {
321327
let sender = ensure_signed(origin)?;
322328
ensure!(gas_price >= T::FeeCalculator::min_gas_price(), Error::<T>::GasPriceTooLow);
@@ -338,13 +344,15 @@ decl_module! {
338344

339345
let total_fee = gas_price.checked_mul(U256::from(gas_limit))
340346
.ok_or(Error::<T>::FeeOverflow)?;
341-
if Accounts::get(&source).balance <
342-
value.checked_add(total_fee).ok_or(Error::<T>::PaymentOverflow)?
343-
{
344-
Err(Error::<T>::BalanceLow)?
345-
}
347+
let total_payment = value.checked_add(total_fee).ok_or(Error::<T>::PaymentOverflow)?;
348+
let source_account = Accounts::get(&source);
349+
ensure!(source_account.balance >= total_payment, Error::<T>::BalanceLow);
346350
executor.withdraw(source, total_fee).map_err(|_| Error::<T>::WithdrawFailed)?;
347351

352+
if let Some(nonce) = nonce {
353+
ensure!(source_account.nonce == nonce, Error::<T>::InvalidNonce);
354+
}
355+
348356
let create_address = executor.create_address(source, evm::CreateScheme::Dynamic);
349357
let reason = executor.transact_create(
350358
source,

0 commit comments

Comments
 (0)