-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update evm runtimes to account for nonce non updates during the pre_d…
…ispatch
- Loading branch information
Showing
7 changed files
with
316 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[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" } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"codec/std", | ||
"frame-support/std", | ||
"frame-system/std", | ||
"scale-info/std", | ||
"sp-core/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,63 @@ | ||
// 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)] | ||
|
||
pub use pallet::*; | ||
use sp_core::{H160, U256}; | ||
|
||
#[frame_support::pallet] | ||
mod pallet { | ||
use frame_support::pallet_prelude::*; | ||
use frame_system::pallet_prelude::BlockNumberFor; | ||
use sp_core::{H160, 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> = StorageMap<_, Identity, H160, 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: H160) -> Option<U256> { | ||
AccountNonce::<T>::get(account) | ||
} | ||
|
||
/// Set nonce to the account. | ||
pub fn set_account_nonce(account: H160, 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
Oops, something went wrong.