Skip to content

Commit

Permalink
Move DefaultNonceProvider to a shared crate
Browse files Browse the repository at this point in the history
Signed-off-by: linning <[email protected]>
  • Loading branch information
NingLin-P committed Feb 24, 2025
1 parent 31e6dba commit f38656f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 67 deletions.
15 changes: 15 additions & 0 deletions crates/subspace-runtime-primitives/src/utility.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! Runtime primitives for pallet-utility.
use core::marker::PhantomData;
use frame_support::pallet_prelude::TypeInfo;
use frame_system::pallet_prelude::RuntimeCallFor;
use scale_info::prelude::collections::VecDeque;
use sp_runtime::traits::{BlockNumberProvider, Get};

/// Trait used to convert from a generated `RuntimeCall` type to `pallet_utility::Call<Runtime>`.
pub trait MaybeIntoUtilityCall<Runtime>
Expand Down Expand Up @@ -52,3 +55,15 @@ where
Some(call)
})
}

// `DefaultNonceProvider` uses the current block number as the nonce of the new account,
// this is used to prevent the replay attack see https://wiki.polkadot.network/docs/transaction-attacks#replay-attack
// for more detail.
#[derive(Debug, TypeInfo)]
pub struct DefaultNonceProvider<T, N>(PhantomData<(T, N)>);

impl<N, T: BlockNumberProvider<BlockNumber = N>> Get<N> for DefaultNonceProvider<T, N> {
fn get() -> N {
T::current_block_number()
}
}
15 changes: 2 additions & 13 deletions domains/runtime/auto-id/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ use sp_subspace_mmr::domain_mmr_runtime_interface::{
use sp_subspace_mmr::{ConsensusChainMmrLeafProof, MmrLeaf};
use sp_version::RuntimeVersion;
use static_assertions::const_assert;
use subspace_runtime_primitives::utility::DefaultNonceProvider;
use subspace_runtime_primitives::{
BlockNumber as ConsensusBlockNumber, Hash as ConsensusBlockHash, Moment,
SlowAdjustingFeeUpdate, SHANNON, SSC,
Expand Down Expand Up @@ -139,18 +140,6 @@ parameter_types! {
pub RuntimeBlockWeights: BlockWeights = block_weights();
}

// `DefaultNonceProvider` uses the current block number as the nonce of the new account,
// this is used to prevent the replay attack see https://wiki.polkadot.network/docs/transaction-attacks#replay-attack
// for more detail.
#[derive(Debug, TypeInfo)]
pub struct DefaultNonceProvider;

impl Get<Nonce> for DefaultNonceProvider {
fn get() -> Nonce {
System::block_number()
}
}

impl frame_system::Config for Runtime {
/// The identifier used to distinguish between accounts.
type AccountId = AccountId;
Expand All @@ -161,7 +150,7 @@ impl frame_system::Config for Runtime {
/// The lookup mechanism to get account ID from whatever is passed in dispatchers.
type Lookup = AccountIdLookup<AccountId, ()>;
/// The type for storing how many extrinsics an account has signed.
type Nonce = TypeWithDefault<Nonce, DefaultNonceProvider>;
type Nonce = TypeWithDefault<Nonce, DefaultNonceProvider<System, Nonce>>;
/// The type for hashing blocks and tries.
type Hash = Hash;
/// The hashing algorithm used.
Expand Down
16 changes: 2 additions & 14 deletions domains/runtime/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ use sp_subspace_mmr::domain_mmr_runtime_interface::{
use sp_subspace_mmr::{ConsensusChainMmrLeafProof, MmrLeaf};
use sp_version::RuntimeVersion;
use static_assertions::const_assert;
use subspace_runtime_primitives::utility::MaybeIntoUtilityCall;
use subspace_runtime_primitives::utility::{DefaultNonceProvider, MaybeIntoUtilityCall};
use subspace_runtime_primitives::{
BlockNumber as ConsensusBlockNumber, Hash as ConsensusBlockHash, Moment,
SlowAdjustingFeeUpdate, SHANNON, SSC,
Expand Down Expand Up @@ -317,18 +317,6 @@ parameter_types! {
pub RuntimeBlockWeights: BlockWeights = block_weights();
}

// `DefaultNonceProvider` uses the current block number as the nonce of the new account,
// this is used to prevent the replay attack see https://wiki.polkadot.network/docs/transaction-attacks#replay-attack
// for more detail.
#[derive(Debug, TypeInfo)]
pub struct DefaultNonceProvider;

impl Get<Nonce> for DefaultNonceProvider {
fn get() -> Nonce {
System::block_number()
}
}

impl frame_system::Config for Runtime {
/// The identifier used to distinguish between accounts.
type AccountId = AccountId;
Expand All @@ -339,7 +327,7 @@ impl frame_system::Config for Runtime {
/// The lookup mechanism to get account ID from whatever is passed in dispatchers.
type Lookup = IdentityLookup<AccountId>;
/// The type for storing how many extrinsics an account has signed.
type Nonce = TypeWithDefault<Nonce, DefaultNonceProvider>;
type Nonce = TypeWithDefault<Nonce, DefaultNonceProvider<System, Nonce>>;
/// The type for hashing blocks and tries.
type Hash = Hash;
/// The hashing algorithm used.
Expand Down
15 changes: 2 additions & 13 deletions domains/test/runtime/auto-id/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ use sp_subspace_mmr::domain_mmr_runtime_interface::{
use sp_subspace_mmr::{ConsensusChainMmrLeafProof, MmrLeaf};
use sp_version::RuntimeVersion;
use static_assertions::const_assert;
use subspace_runtime_primitives::utility::DefaultNonceProvider;
use subspace_runtime_primitives::{
BlockNumber as ConsensusBlockNumber, Hash as ConsensusBlockHash, Moment,
SlowAdjustingFeeUpdate, SSC,
Expand Down Expand Up @@ -137,18 +138,6 @@ parameter_types! {
pub RuntimeBlockWeights: BlockWeights = block_weights();
}

// `DefaultNonceProvider` uses the current block number as the nonce of the new account,
// this is used to prevent the replay attack see https://wiki.polkadot.network/docs/transaction-attacks#replay-attack
// for more detail.
#[derive(Debug, TypeInfo)]
pub struct DefaultNonceProvider;

impl Get<Nonce> for DefaultNonceProvider {
fn get() -> Nonce {
System::block_number()
}
}

impl frame_system::Config for Runtime {
/// The identifier used to distinguish between accounts.
type AccountId = AccountId;
Expand All @@ -159,7 +148,7 @@ impl frame_system::Config for Runtime {
/// The lookup mechanism to get account ID from whatever is passed in dispatchers.
type Lookup = AccountIdLookup<AccountId, ()>;
/// The type for storing how many extrinsics an account has signed.
type Nonce = TypeWithDefault<Nonce, DefaultNonceProvider>;
type Nonce = TypeWithDefault<Nonce, DefaultNonceProvider<System, Nonce>>;
/// The type for hashing blocks and tries.
type Hash = Hash;
/// The hashing algorithm used.
Expand Down
16 changes: 2 additions & 14 deletions domains/test/runtime/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ use sp_subspace_mmr::domain_mmr_runtime_interface::{
use sp_subspace_mmr::{ConsensusChainMmrLeafProof, MmrLeaf};
use sp_version::RuntimeVersion;
use static_assertions::const_assert;
use subspace_runtime_primitives::utility::MaybeIntoUtilityCall;
use subspace_runtime_primitives::utility::{DefaultNonceProvider, MaybeIntoUtilityCall};
use subspace_runtime_primitives::{
BlockNumber as ConsensusBlockNumber, Hash as ConsensusBlockHash, Moment, SHANNON, SSC,
};
Expand Down Expand Up @@ -369,18 +369,6 @@ parameter_types! {
pub RuntimeBlockWeights: BlockWeights = block_weights();
}

// `DefaultNonceProvider` uses the current block number as the nonce of the new account,
// this is used to prevent the replay attack see https://wiki.polkadot.network/docs/transaction-attacks#replay-attack
// for more detail.
#[derive(Debug, TypeInfo)]
pub struct DefaultNonceProvider;

impl Get<Nonce> for DefaultNonceProvider {
fn get() -> Nonce {
System::block_number()
}
}

impl frame_system::Config for Runtime {
/// The ubiquitous event type.
type RuntimeEvent = RuntimeEvent;
Expand All @@ -397,7 +385,7 @@ impl frame_system::Config for Runtime {
/// The aggregated `RuntimeTask` type.
type RuntimeTask = RuntimeTask;
/// The type for storing how many extrinsics an account has signed.
type Nonce = TypeWithDefault<Nonce, DefaultNonceProvider>;
type Nonce = TypeWithDefault<Nonce, DefaultNonceProvider<System, Nonce>>;
/// The type for hashing blocks and tries.
type Hash = Hash;
/// The hashing algorithm used.
Expand Down
15 changes: 2 additions & 13 deletions test/subspace-test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ use subspace_core_primitives::segments::{
};
use subspace_core_primitives::solutions::SolutionRange;
use subspace_core_primitives::{hashes, PublicKey, Randomness, SlotNumber, U256};
use subspace_runtime_primitives::utility::DefaultNonceProvider;
use subspace_runtime_primitives::{
AccountId, Balance, BlockNumber, FindBlockRewardAddress, Hash, HoldIdentifier, Moment, Nonce,
Signature, MIN_REPLICATION_FACTOR, SHANNON, SSC,
Expand Down Expand Up @@ -216,18 +217,6 @@ parameter_types! {

pub type SS58Prefix = ConstU16<6094>;

// `DefaultNonceProvider` uses the current block number as the nonce of the new account,
// this is used to prevent the replay attack see https://wiki.polkadot.network/docs/transaction-attacks#replay-attack
// for more detail.
#[derive(Debug, TypeInfo)]
pub struct DefaultNonceProvider;

impl Get<Nonce> for DefaultNonceProvider {
fn get() -> Nonce {
System::block_number()
}
}

// Configure FRAME pallets to include in runtime.

impl frame_system::Config for Runtime {
Expand All @@ -246,7 +235,7 @@ impl frame_system::Config for Runtime {
/// The lookup mechanism to get account ID from whatever is passed in dispatchers.
type Lookup = AccountIdLookup<AccountId, ()>;
/// The type for storing how many extrinsics an account has signed.
type Nonce = TypeWithDefault<Nonce, DefaultNonceProvider>;
type Nonce = TypeWithDefault<Nonce, DefaultNonceProvider<System, Nonce>>;
/// The type for hashing blocks and tries.
type Hash = Hash;
/// The hashing algorithm used.
Expand Down

0 comments on commit f38656f

Please sign in to comment.