Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ability to configure default nonce #1557

Closed
wants to merge 15 commits into from
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
use crate as ethereum_beacon_client;
use frame_support::parameter_types;
use frame_support::{derive_impl, parameter_types};
use pallet_timestamp;
use primitives::{Fork, ForkVersions};
use sp_core::H256;
Expand Down Expand Up @@ -33,6 +33,7 @@ pub mod minimal {
pub const SS58Prefix: u8 = 42;
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type OnSetCode = ();
Expand Down Expand Up @@ -202,6 +203,7 @@ pub mod mainnet {
pub const SS58Prefix: u8 = 42;
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type OnSetCode = ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use super::*;

use frame_support::{
parameter_types,
derive_impl, parameter_types,
traits::{ConstU128, ConstU32, Everything},
weights::IdentityFee,
};
Expand Down Expand Up @@ -46,6 +46,7 @@ parameter_types! {

type Balance = u128;

#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::Config for Test {
type BaseCallFilter = Everything;
type BlockWeights = ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use super::*;

use frame_support::{
parameter_types,
derive_impl, parameter_types,
traits::{Everything, Hooks},
weights::IdentityFee,
};
Expand Down Expand Up @@ -37,6 +37,7 @@ parameter_types! {
pub const BlockHashCount: u64 = 250;
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::Config for Test {
type BaseCallFilter = Everything;
type BlockWeights = ();
Expand Down
3 changes: 2 additions & 1 deletion bridges/snowbridge/parachain/pallets/system/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
use crate as snowbridge_system;
use frame_support::{
parameter_types,
derive_impl, parameter_types,
traits::{tokens::fungible::Mutate, ConstU128, ConstU16, ConstU64, ConstU8},
weights::IdentityFee,
PalletId,
Expand Down Expand Up @@ -95,6 +95,7 @@ frame_support::construct_runtime!(
}
);

#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
Expand Down
17 changes: 17 additions & 0 deletions prdoc/pr_1557.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
title: Adds ability to configure default account nonce

doc:
- audience: Runtime Dev
description: |
Introduces the ability to configure the default nonce. This will also be used if the account is reaped.
By default, the current behaviour is maintained and nonce starts from 0.

migrations:
db: []

runtime: []

crates:
- name: frame-system

host_functions: []
27 changes: 25 additions & 2 deletions substrate/frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ pub mod pallet {
type BaseCallFilter = frame_support::traits::Everything;
type BlockHashCount = frame_support::traits::ConstU64<10>;
type OnSetCode = ();
type DefaultNonce = ();
}

/// Default configurations of this pallet in a solo-chain environment.
Expand Down Expand Up @@ -392,6 +393,9 @@ pub mod pallet {

/// The set code logic, just the default since we're not a parachain.
type OnSetCode = ();

/// The default nonce when an account is created.
type DefaultNonce = ();
}

/// Default configurations of this pallet in a relay-chain environment.
Expand Down Expand Up @@ -463,6 +467,7 @@ pub mod pallet {
type RuntimeTask: Task;

/// This stores the number of previous transactions associated with a sender account.
#[pallet::no_default_bounds]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now there are two notations of default, once from the Default bound here, and one from the DefaultNonce.

type Nonce: Parameter
+ Member
+ MaybeSerializeDeserialize
Expand Down Expand Up @@ -570,6 +575,9 @@ pub mod pallet {

/// The maximum number of consumers allowed on a single account.
type MaxConsumers: ConsumerLimits;

/// The default nonce when an account is created.
type DefaultNonce: Get<Self::Nonce>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intentionally not a #[pallet::constant]?

}

#[pallet::pallet]
Expand Down Expand Up @@ -837,6 +845,7 @@ pub mod pallet {
T::AccountId,
AccountInfo<T::Nonce, T::AccountData>,
ValueQuery,
GetDefaultAccountInfo<T>,
>;

/// Total extrinsics count for the current block.
Expand Down Expand Up @@ -1036,7 +1045,10 @@ pub type RefCount = u32;
/// Information of an account.
#[derive(Clone, Eq, PartialEq, Default, RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct AccountInfo<Nonce, AccountData> {
/// The number of transactions this account has sent.
/// A value that increases with every transaction that the account has sent.
///
/// Gets reset when the account gets reaped and is initialized to the default nonce as provided
/// in the config.
pub nonce: Nonce,
/// The number of other modules that currently depend on this account's existence. The account
/// cannot be reaped until this is zero.
Expand All @@ -1052,6 +1064,17 @@ pub struct AccountInfo<Nonce, AccountData> {
pub data: AccountData,
}

type NonceOf<T> = <T as Config>::Nonce;
type AccountDataOf<T> = <T as Config>::AccountData;
type AccountInfoOf<T> = AccountInfo<NonceOf<T>, AccountDataOf<T>>;

pub struct GetDefaultAccountInfo<T>(PhantomData<T>);
gupnik marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub struct GetDefaultAccountInfo<T>(PhantomData<T>);
/// Provides the `Default` for value an account that does not yet exist.
pub struct GetDefaultAccountInfo<T>(PhantomData<T>);

impl<T: pallet::Config> Get<AccountInfoOf<T>> for GetDefaultAccountInfo<T> {
fn get() -> AccountInfoOf<T> {
AccountInfo { nonce: T::DefaultNonce::get(), ..Default::default() }
}
}

/// Stores the `spec_version` and `spec_name` of when the last runtime upgrade
/// happened.
#[derive(sp_runtime::RuntimeDebug, Encode, Decode, TypeInfo)]
Expand Down Expand Up @@ -1909,7 +1932,7 @@ impl<T: Config> Pallet<T> {

/// Increment a particular account's nonce by 1.
pub fn inc_account_nonce(who: impl EncodeLike<T::AccountId>) {
Account::<T>::mutate(who, |a| a.nonce += T::Nonce::one());
Account::<T>::mutate(who, |a| a.nonce.saturating_inc());
}

/// Note what the extrinsic data of the current extrinsic index is.
Expand Down
8 changes: 8 additions & 0 deletions substrate/frame/system/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ impl OnKilledAccount<u64> for RecordKilled {
}
}

pub struct SetDefaultNonceToBlockNumber;
impl Get<u64> for SetDefaultNonceToBlockNumber {
fn get() -> u64 {
System::block_number().into()
}
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
Expand All @@ -110,6 +117,7 @@ impl Config for Test {
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = ConstU32<16>;
type DefaultNonce = SetDefaultNonceToBlockNumber;
}

pub type SysEvent = frame_system::Event<Test>;
Expand Down
17 changes: 17 additions & 0 deletions substrate/frame/system/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,23 @@ fn ensure_signed_stuff_works() {
}
}

#[test]
fn test_default_account_nonce() {
new_test_ext().execute_with(|| {
System::set_block_number(2);
assert_eq!(System::account_nonce(&1), 2);

System::inc_account_nonce(&1);
assert_eq!(System::account_nonce(&1), 3);

System::set_block_number(5);
assert_eq!(System::account_nonce(&1), 3);

Account::<Test>::remove(&1);
assert_eq!(System::account_nonce(&1), 5);
});
}

pub fn from_actual_ref_time(ref_time: Option<u64>) -> PostDispatchInfo {
PostDispatchInfo {
actual_weight: ref_time.map(|t| Weight::from_all(t)),
Expand Down