diff --git a/crates/pallet-domains/src/tests.rs b/crates/pallet-domains/src/tests.rs index 9b44c72401..35adea190d 100644 --- a/crates/pallet-domains/src/tests.rs +++ b/crates/pallet-domains/src/tests.rs @@ -1028,6 +1028,12 @@ fn test_type_with_default_nonce_encode() { } } + let nonce_1_default = 0; + let nonce_2_default = TypeWithDefault::::min_value(); + let encode_1 = nonce_1_default.encode(); + let encode_2 = nonce_2_default.encode(); + assert_eq!(encode_1, encode_2); + let nonce_1_default = 13452234; let nonce_2_default = TypeWithDefault::::default(); let encode_1 = nonce_1_default.encode(); diff --git a/crates/sp-domains-fraud-proof/src/tests.rs b/crates/sp-domains-fraud-proof/src/tests.rs index 64db4fd601..4f8791b598 100644 --- a/crates/sp-domains-fraud-proof/src/tests.rs +++ b/crates/sp-domains-fraud-proof/src/tests.rs @@ -89,11 +89,17 @@ async fn benchmark_bundle_with_evm_tx( tx_type_to_use <= TX_TYPES, "random tx_type is out of bounds" ); + let nonce = alice + .client + .runtime_api() + .account_basic(alice.client.info().best_hash, account_info.address) + .unwrap() + .nonce; let extrinsic = match tx_type_to_use { 0 => { let evm_tx = generate_eip1559_tx::( account_info.clone(), - U256::zero(), + nonce, ethereum::TransactionAction::Create, vec![1u8; 100], gas_price, @@ -103,7 +109,7 @@ async fn benchmark_bundle_with_evm_tx( 1 => { let evm_tx = generate_eip2930_tx::( account_info.clone(), - U256::zero(), + nonce, ethereum::TransactionAction::Create, vec![1u8; 100], gas_price, @@ -113,7 +119,7 @@ async fn benchmark_bundle_with_evm_tx( 2 => { let evm_tx = generate_legacy_tx::( account_info.clone(), - U256::zero(), + nonce, ethereum::TransactionAction::Create, vec![1u8; 100], gas_price, @@ -138,7 +144,7 @@ async fn benchmark_bundle_with_evm_tx( genesis_block_hash, function.clone(), false, - 0, + nonce.try_into().unwrap(), 1, ); let signature = raw_payload.using_encoded(|e| { @@ -656,9 +662,15 @@ async fn test_evm_domain_block_fee() { .zip(tx_generators.into_iter()) .enumerate() { + let nonce = alice + .client + .runtime_api() + .account_basic(alice.client.info().best_hash, acc.address) + .unwrap() + .nonce; let tx = generate_eth_domain_sc_extrinsic(tx_generator( acc.clone(), - U256::zero(), + nonce, ethereum::TransactionAction::Create, vec![(i + 1) as u8; 100], gas_price, diff --git a/crates/subspace-runtime/src/lib.rs b/crates/subspace-runtime/src/lib.rs index 201af62268..d1227e0cef 100644 --- a/crates/subspace-runtime/src/lib.rs +++ b/crates/subspace-runtime/src/lib.rs @@ -99,7 +99,7 @@ use subspace_core_primitives::solutions::{ pieces_to_solution_range, solution_range_to_pieces, SolutionRange, }; use subspace_core_primitives::{PublicKey, Randomness, SlotNumber, U256}; -use subspace_runtime_primitives::utility::MaybeIntoUtilityCall; +use subspace_runtime_primitives::utility::{DefaultNonceProvider, MaybeIntoUtilityCall}; use subspace_runtime_primitives::{ maximum_normal_block_length, AccountId, Balance, BlockNumber, FindBlockRewardAddress, Hash, HoldIdentifier, Moment, Nonce, Signature, SlowAdjustingFeeUpdate, BLOCK_WEIGHT_FOR_2_SEC, @@ -192,18 +192,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 for DefaultNonceProvider { - fn get() -> Nonce { - System::block_number() - } -} - // Configure FRAME pallets to include in runtime. impl frame_system::Config for Runtime { @@ -225,7 +213,7 @@ impl frame_system::Config for Runtime { /// The lookup mechanism to get account ID from whatever is passed in dispatchers. type Lookup = AccountIdLookup; /// The type for storing how many extrinsics an account has signed. - type Nonce = TypeWithDefault; + type Nonce = TypeWithDefault>; /// The type for hashing blocks and tries. type Hash = Hash; /// The hashing algorithm used. diff --git a/domains/client/domain-operator/src/tests.rs b/domains/client/domain-operator/src/tests.rs index 488336e6b7..8b5e5c15f7 100644 --- a/domains/client/domain-operator/src/tests.rs +++ b/domains/client/domain-operator/src/tests.rs @@ -314,7 +314,12 @@ async fn test_private_evm_domain_create_contracts_with_allow_list_default() { .unwrap(); // Any account should be able to create contracts by default - let mut eth_nonce = U256::zero(); + let mut eth_nonce = alice + .client + .runtime_api() + .account_basic(alice.client.info().best_hash, account_infos[0].address) + .unwrap() + .nonce; let eth_tx = generate_eth_domain_extrinsic( account_infos[0].clone(), ethereum::TransactionAction::Create, @@ -330,7 +335,12 @@ async fn test_private_evm_domain_create_contracts_with_allow_list_default() { "Unexpectedly failed to send self-contained extrinsic" ); - let mut evm_nonce = U256::zero(); + let mut evm_nonce = alice + .client + .runtime_api() + .account_basic(alice.client.info().best_hash, account_infos[1].address) + .unwrap() + .nonce; let mut evm_tx = generate_evm_domain_call( account_infos[1].clone(), ethereum::TransactionAction::Create, @@ -431,7 +441,12 @@ async fn test_public_evm_domain_create_contracts() { .unwrap(); // Any account should be able to create contracts in a public EVM domain - let mut eth_nonce = U256::zero(); + let mut eth_nonce = alice + .client + .runtime_api() + .account_basic(alice.client.info().best_hash, account_infos[0].address) + .unwrap() + .nonce; let eth_tx = generate_eth_domain_extrinsic( account_infos[0].clone(), ethereum::TransactionAction::Create, @@ -447,7 +462,12 @@ async fn test_public_evm_domain_create_contracts() { "Unexpectedly failed to send self-contained extrinsic" ); - let mut evm_nonce = U256::zero(); + let mut evm_nonce = alice + .client + .runtime_api() + .account_basic(alice.client.info().best_hash, account_infos[1].address) + .unwrap() + .nonce; let mut evm_tx = generate_evm_domain_call( account_infos[1].clone(), ethereum::TransactionAction::Create, @@ -548,7 +568,12 @@ async fn test_evm_domain_create_contracts_with_allow_list_reject_all() { .unwrap(); // Create contracts used for testing contract calls - let mut eth_nonce = U256::zero(); + let mut eth_nonce = alice + .client + .runtime_api() + .account_basic(alice.client.info().best_hash, account_infos[0].address) + .unwrap() + .nonce; let mut eth_tx = generate_eth_domain_extrinsic( account_infos[0].clone(), ethereum::TransactionAction::Create, @@ -560,7 +585,12 @@ async fn test_evm_domain_create_contracts_with_allow_list_reject_all() { alice.send_extrinsic(eth_tx).await.unwrap(); - let mut evm_nonce = U256::zero(); + let mut evm_nonce = alice + .client + .runtime_api() + .account_basic(alice.client.info().best_hash, account_infos[1].address) + .unwrap() + .nonce; let mut evm_tx = generate_evm_domain_call( account_infos[1].clone(), ethereum::TransactionAction::Create, @@ -771,7 +801,12 @@ async fn test_evm_domain_create_contracts_with_allow_list_single() { .unwrap(); // Create contracts used for testing contract calls - let mut eth_nonce = U256::zero(); + let mut eth_nonce = alice + .client + .runtime_api() + .account_basic(alice.client.info().best_hash, account_infos[0].address) + .unwrap() + .nonce; let mut eth_tx = generate_eth_domain_extrinsic( account_infos[0].clone(), ethereum::TransactionAction::Create, @@ -782,7 +817,12 @@ async fn test_evm_domain_create_contracts_with_allow_list_single() { alice.send_extrinsic(eth_tx).await.unwrap(); - let mut evm_nonce = U256::zero(); + let mut evm_nonce = alice + .client + .runtime_api() + .account_basic(alice.client.info().best_hash, account_infos[1].address) + .unwrap() + .nonce; let mut evm_tx = generate_evm_domain_call( account_infos[1].clone(), ethereum::TransactionAction::Create, @@ -1015,7 +1055,12 @@ async fn test_evm_domain_create_contracts_with_allow_list_multiple() { .unwrap(); // Accounts 0-2 should be able to create contracts - let mut eth_nonce = U256::zero(); + let mut eth_nonce = alice + .client + .runtime_api() + .account_basic(alice.client.info().best_hash, account_infos[1].address) + .unwrap() + .nonce; let mut eth_tx = generate_eth_domain_extrinsic( account_infos[1].clone(), ethereum::TransactionAction::Create, @@ -1031,7 +1076,12 @@ async fn test_evm_domain_create_contracts_with_allow_list_multiple() { "Unexpectedly failed to send self-contained extrinsic" ); - let mut evm_nonce = U256::zero(); + let mut evm_nonce = alice + .client + .runtime_api() + .account_basic(alice.client.info().best_hash, account_infos[2].address) + .unwrap() + .nonce; let mut evm_tx = generate_evm_domain_call( account_infos[2].clone(), ethereum::TransactionAction::Create, diff --git a/test/subspace-test-service/src/lib.rs b/test/subspace-test-service/src/lib.rs index 7bbbcbf9e1..7da8d1350d 100644 --- a/test/subspace-test-service/src/lib.rs +++ b/test/subspace-test-service/src/lib.rs @@ -1118,7 +1118,7 @@ impl MockConsensusNode { .expect("Fail to get account nonce") } - /// Get the nonce of the node account + /// Get the nonce of the given account pub fn account_nonce_of(&self, account_id: AccountId) -> u32 { self.client .runtime_api()