-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2723 from subspace/evm_fixes
Domains: update evm runtimes to account for nonce non updates during the `pre_dispatch`
- Loading branch information
Showing
13 changed files
with
517 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[package] | ||
name = "pallet-evm-nonce-tracker" | ||
version = "0.1.0" | ||
authors = ["Subspace Labs <https://subspace.network>"] | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
homepage = "https://subspace.network" | ||
repository = "https://github.com/subspace/subspace" | ||
description = "Subspace node pallet for EVM account nonce tracker." | ||
include = [ | ||
"/src", | ||
"/Cargo.toml", | ||
] | ||
|
||
[dependencies] | ||
codec = { package = "parity-scale-codec", version = "3.6.5", default-features = false, features = ["derive"] } | ||
frame-support = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "44d742b90e7852aed1f08ab5299d5d88cfa1c6ed" } | ||
frame-system = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "44d742b90e7852aed1f08ab5299d5d88cfa1c6ed" } | ||
scale-info = { version = "2.11.1", default-features = false, features = ["derive"] } | ||
sp-core = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "44d742b90e7852aed1f08ab5299d5d88cfa1c6ed" } | ||
sp-runtime = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "44d742b90e7852aed1f08ab5299d5d88cfa1c6ed" } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"codec/std", | ||
"frame-support/std", | ||
"frame-system/std", | ||
"scale-info/std", | ||
"sp-core/std", | ||
"sp-runtime/std", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use crate::Config; | ||
#[cfg(not(feature = "std"))] | ||
use alloc::fmt; | ||
#[cfg(not(feature = "std"))] | ||
use alloc::vec; | ||
use codec::{Decode, Encode}; | ||
use core::cmp::max; | ||
use core::result::Result; | ||
use frame_support::dispatch::DispatchInfo; | ||
use frame_support::pallet_prelude::{ | ||
InvalidTransaction, TransactionLongevity, TransactionValidity, TransactionValidityError, | ||
TypeInfo, ValidTransaction, | ||
}; | ||
use frame_support::sp_runtime::traits::{DispatchInfoOf, One, SignedExtension}; | ||
use sp_runtime::traits::{Dispatchable, Zero}; | ||
#[cfg(feature = "std")] | ||
use std::fmt; | ||
#[cfg(feature = "std")] | ||
use std::vec; | ||
|
||
#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo)] | ||
#[scale_info(skip_type_params(T))] | ||
pub struct CheckNonce<T: Config>(#[codec(compact)] pub T::Nonce); | ||
|
||
impl<T: Config> CheckNonce<T> { | ||
/// utility constructor. Used only in client/factory code. | ||
pub fn from(nonce: T::Nonce) -> Self { | ||
Self(nonce) | ||
} | ||
} | ||
|
||
impl<T: Config> fmt::Debug for CheckNonce<T> { | ||
#[cfg(feature = "std")] | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "CheckNonce({})", self.0) | ||
} | ||
|
||
#[cfg(not(feature = "std"))] | ||
fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result { | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<T: Config> SignedExtension for CheckNonce<T> | ||
where | ||
T::RuntimeCall: Dispatchable<Info = DispatchInfo>, | ||
{ | ||
const IDENTIFIER: &'static str = "CheckNonce"; | ||
type AccountId = T::AccountId; | ||
type Call = T::RuntimeCall; | ||
type AdditionalSigned = (); | ||
type Pre = (); | ||
|
||
fn additional_signed(&self) -> Result<(), TransactionValidityError> { | ||
Ok(()) | ||
} | ||
|
||
fn validate( | ||
&self, | ||
who: &Self::AccountId, | ||
_call: &Self::Call, | ||
_info: &DispatchInfoOf<Self::Call>, | ||
_len: usize, | ||
) -> TransactionValidity { | ||
let account = frame_system::Account::<T>::get(who); | ||
if account.providers.is_zero() && account.sufficients.is_zero() { | ||
// Nonce storage not paid for | ||
return InvalidTransaction::Payment.into(); | ||
} | ||
if self.0 < account.nonce { | ||
return InvalidTransaction::Stale.into(); | ||
} | ||
|
||
let provides = vec![Encode::encode(&(who, self.0))]; | ||
let requires = if account.nonce < self.0 { | ||
vec![Encode::encode(&(who, self.0 - One::one()))] | ||
} else { | ||
vec![] | ||
}; | ||
|
||
Ok(ValidTransaction { | ||
priority: 0, | ||
requires, | ||
provides, | ||
longevity: TransactionLongevity::MAX, | ||
propagate: true, | ||
}) | ||
} | ||
|
||
fn pre_dispatch( | ||
self, | ||
who: &Self::AccountId, | ||
_call: &Self::Call, | ||
_info: &DispatchInfoOf<Self::Call>, | ||
_len: usize, | ||
) -> Result<(), TransactionValidityError> { | ||
let mut account = frame_system::Account::<T>::get(who); | ||
if account.providers.is_zero() && account.sufficients.is_zero() { | ||
// Nonce storage not paid for | ||
return Err(InvalidTransaction::Payment.into()); | ||
} | ||
// if a sender sends an evm transaction first and substrate transaction | ||
// after with same nonce, then reject the second transaction | ||
// if sender reverse the transaction types, substrate first and evm second, | ||
// evm transaction will be rejected, since substrate updates nonce in pre_dispatch. | ||
let account_nonce = if let Some(tracked_nonce) = crate::AccountNonce::<T>::get(who.clone()) | ||
{ | ||
max(tracked_nonce.as_u32().into(), account.nonce) | ||
} else { | ||
account.nonce | ||
}; | ||
|
||
if self.0 != account_nonce { | ||
return Err(if self.0 < account.nonce { | ||
InvalidTransaction::Stale | ||
} else { | ||
InvalidTransaction::Future | ||
} | ||
.into()); | ||
} | ||
account.nonce += T::Nonce::one(); | ||
frame_system::Account::<T>::insert(who, account); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (C) 2023 Subspace Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! Pallet EVM Account nonce tracker | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
#[cfg(not(feature = "std"))] | ||
extern crate alloc; | ||
|
||
mod check_nonce; | ||
pub use check_nonce::CheckNonce; | ||
pub use pallet::*; | ||
use sp_core::U256; | ||
|
||
#[frame_support::pallet] | ||
mod pallet { | ||
use frame_support::pallet_prelude::*; | ||
use frame_system::pallet_prelude::BlockNumberFor; | ||
use sp_core::U256; | ||
|
||
#[pallet::config] | ||
pub trait Config: frame_system::Config {} | ||
|
||
/// Storage to hold evm account nonces. | ||
/// This is only used for pre_dispatch since EVM pre_dispatch does not | ||
/// increment account nonce. | ||
#[pallet::storage] | ||
pub(super) type AccountNonce<T: Config> = | ||
StorageMap<_, Identity, T::AccountId, U256, OptionQuery>; | ||
|
||
/// Pallet EVM account nonce tracker. | ||
#[pallet::pallet] | ||
#[pallet::without_storage_info] | ||
pub struct Pallet<T>(_); | ||
|
||
#[pallet::hooks] | ||
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> { | ||
fn on_finalize(_now: BlockNumberFor<T>) { | ||
// clear the storage since we would start with updated nonce | ||
// during the pre_dispatch in next block | ||
let _ = AccountNonce::<T>::clear(u32::MAX, None); | ||
} | ||
} | ||
} | ||
|
||
impl<T: Config> Pallet<T> { | ||
/// Returns current nonce for the given account. | ||
pub fn account_nonce(account: T::AccountId) -> Option<U256> { | ||
AccountNonce::<T>::get(account) | ||
} | ||
|
||
/// Set nonce to the account. | ||
pub fn set_account_nonce(account: T::AccountId, nonce: U256) { | ||
AccountNonce::<T>::set(account, Some(nonce)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.