From 50e9d5c1f450343959bfd7b4c0b72a1ee58280b4 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Fri, 27 Dec 2024 15:03:56 +0800 Subject: [PATCH] Fix tests --- node/src/command.rs | 4 +- pallet/account-migration/src/benchmarking.rs | 2 +- pallet/deposit/src/benchmarking.rs | 2 +- pallet/deposit/src/tests.rs | 56 ++++++++++---------- pallet/staking/src/benchmarking.rs | 2 +- pallet/staking/src/lib.rs | 15 +++++- pallet/staking/src/mock.rs | 2 +- pallet/staking/src/tests.rs | 6 +-- runtime/common/src/test.rs | 14 ++--- runtime/crab/tests/mock.rs | 1 + runtime/darwinia/tests/mock.rs | 1 + runtime/koi/tests/mock.rs | 1 + 12 files changed, 60 insertions(+), 46 deletions(-) diff --git a/node/src/command.rs b/node/src/command.rs index d0a76cb46..56b0e2667 100644 --- a/node/src/command.rs +++ b/node/src/command.rs @@ -436,7 +436,7 @@ pub fn run() -> Result<()> { #[cfg(feature = "runtime-benchmarks")] Some(Subcommand::Benchmark(cmd)) => { // darwinia - use dc_primitives::{Block, Hashing}; + use dc_primitives::Hashing; // polkadot-sdk use cumulus_client_service::storage_proof_size::HostFunctions as ReclaimHostFunctions; use frame_benchmarking_cli::{BenchmarkCmd, SUBSTRATE_REFERENCE_HARDWARE}; @@ -447,7 +447,7 @@ pub fn run() -> Result<()> { match &**cmd { BenchmarkCmd::Pallet(cmd) => - runner.sync_run(|config| cmd.run::(config)), + runner.sync_run(|config| cmd.run_with_spec::(Some(config.chain_spec))), BenchmarkCmd::Storage(cmd) => runner.sync_run(|config| { construct_benchmark_partials!(config, cli, |partials| { let db = partials.backend.expose_db(); diff --git a/pallet/account-migration/src/benchmarking.rs b/pallet/account-migration/src/benchmarking.rs index a1275851a..75dd38010 100644 --- a/pallet/account-migration/src/benchmarking.rs +++ b/pallet/account-migration/src/benchmarking.rs @@ -19,7 +19,7 @@ // darwinia use crate::*; // polkadot-sdk -use frame_benchmarking::v2; +use frame_benchmarking::v2::*; use frame_system::RawOrigin; use sp_std::prelude::*; diff --git a/pallet/deposit/src/benchmarking.rs b/pallet/deposit/src/benchmarking.rs index b50c01e32..6a821db58 100644 --- a/pallet/deposit/src/benchmarking.rs +++ b/pallet/deposit/src/benchmarking.rs @@ -19,7 +19,7 @@ // darwinia use crate::{Deposit, *}; // polkadot-sdk -use frame_benchmarking::v2; +use frame_benchmarking::v2::*; use frame_system::RawOrigin; use sp_std::prelude::*; diff --git a/pallet/deposit/src/tests.rs b/pallet/deposit/src/tests.rs index b0e7a7ce3..349b92920 100644 --- a/pallet/deposit/src/tests.rs +++ b/pallet/deposit/src/tests.rs @@ -37,9 +37,9 @@ fn migrate_should_work() { ]), ); - assert!(Deposit::deposit_of(1).is_some()); + assert!(>::get(1).is_some()); assert_ok!(Deposit::migrate_for(RuntimeOrigin::signed(1), 1)); - assert!(Deposit::deposit_of(1).is_none()); + assert!(>::get(1).is_none()); }); } @@ -68,45 +68,45 @@ fn on_idle_should_work() { >::insert(1, mock_deposits(512)); >::insert(2, mock_deposits(512)); >::insert(3, mock_deposits(512)); - assert_eq!(Deposit::deposit_of(1).unwrap().len(), 512); - assert_eq!(Deposit::deposit_of(2).unwrap().len(), 512); - assert_eq!(Deposit::deposit_of(3).unwrap().len(), 512); + assert_eq!(>::get(1).unwrap().len(), 512); + assert_eq!(>::get(2).unwrap().len(), 512); + assert_eq!(>::get(3).unwrap().len(), 512); >::on_idle(0, Weight::zero()); - assert_eq!(Deposit::deposit_of(1).unwrap().len(), 512); - assert_eq!(Deposit::deposit_of(2).unwrap().len(), 512); - assert_eq!(Deposit::deposit_of(3).unwrap().len(), 512); + assert_eq!(>::get(1).unwrap().len(), 512); + assert_eq!(>::get(2).unwrap().len(), 512); + assert_eq!(>::get(3).unwrap().len(), 512); >::on_idle(0, Weight::zero().add_ref_time(10)); - assert_eq!(Deposit::deposit_of(1).unwrap().len(), 512); - assert_eq!(Deposit::deposit_of(2).unwrap().len(), 512); - assert_eq!(Deposit::deposit_of(3).unwrap().len(), 502); + assert_eq!(>::get(1).unwrap().len(), 512); + assert_eq!(>::get(2).unwrap().len(), 512); + assert_eq!(>::get(3).unwrap().len(), 502); >::on_idle(0, Weight::MAX); - assert_eq!(Deposit::deposit_of(1).unwrap().len(), 512); - assert_eq!(Deposit::deposit_of(2).unwrap().len(), 512); - assert_eq!(Deposit::deposit_of(3).unwrap().len(), 492); + assert_eq!(>::get(1).unwrap().len(), 512); + assert_eq!(>::get(2).unwrap().len(), 512); + assert_eq!(>::get(3).unwrap().len(), 492); (0..50).for_each(|_| { >::on_idle(0, Weight::MAX); }); - assert_eq!(Deposit::deposit_of(1).unwrap().len(), 512); - assert_eq!(Deposit::deposit_of(2).unwrap().len(), 512); - assert!(Deposit::deposit_of(3).is_none()); + assert_eq!(>::get(1).unwrap().len(), 512); + assert_eq!(>::get(2).unwrap().len(), 512); + assert!(>::get(3).is_none()); (0..50).for_each(|_| { >::on_idle(0, Weight::MAX); }); - assert_eq!(Deposit::deposit_of(1).unwrap().len(), 12); - assert_eq!(Deposit::deposit_of(2).unwrap().len(), 512); - assert!(Deposit::deposit_of(3).is_none()); + assert_eq!(>::get(1).unwrap().len(), 12); + assert_eq!(>::get(2).unwrap().len(), 512); + assert!(>::get(3).is_none()); System::reset_events(); (0..54).for_each(|_| { >::on_idle(0, Weight::MAX); }); assert_eq!(events().into_iter().count(), 54); - assert!(Deposit::deposit_of(1).is_none()); - assert!(Deposit::deposit_of(2).is_none()); - assert!(Deposit::deposit_of(3).is_none()); + assert!(>::get(1).is_none()); + assert!(>::get(2).is_none()); + assert!(>::get(3).is_none()); }); new_test_ext().execute_with(|| { System::set_block_number(1); @@ -114,21 +114,21 @@ fn on_idle_should_work() { let _ = Balances::deposit_creating(&0, 10_000); >::insert(1, mock_zero_deposits(512)); - assert_eq!(Deposit::deposit_of(1).unwrap().len(), 512); + assert_eq!(>::get(1).unwrap().len(), 512); System::reset_events(); (0..52).for_each(|_| { >::on_idle(0, Weight::MAX); }); assert_eq!(events().into_iter().count(), 0); - assert!(Deposit::deposit_of(1).is_none()); + assert!(>::get(1).is_none()); }); new_test_ext().execute_with(|| { >::insert(1, mock_deposits(512)); - assert_eq!(Deposit::deposit_of(1).unwrap().len(), 512); + assert_eq!(>::get(1).unwrap().len(), 512); >::on_idle(0, Weight::MAX); - assert!(Deposit::deposit_of(1).is_none()); - assert_eq!(Deposit::migration_failure_of(1).unwrap().0.into_iter().count(), 512); + assert!(>::get(1).is_none()); + assert_eq!(>::get(1).unwrap().0.into_iter().count(), 512); }); } diff --git a/pallet/staking/src/benchmarking.rs b/pallet/staking/src/benchmarking.rs index ae388fc25..61fbbecdd 100644 --- a/pallet/staking/src/benchmarking.rs +++ b/pallet/staking/src/benchmarking.rs @@ -19,7 +19,7 @@ // darwinia use crate::*; // polkadot-sdk -use frame_benchmarking::v2; +use frame_benchmarking::v2::*; use frame_system::RawOrigin; use sp_std::prelude::*; diff --git a/pallet/staking/src/lib.rs b/pallet/staking/src/lib.rs index 9a651f199..65c6abf65 100644 --- a/pallet/staking/src/lib.rs +++ b/pallet/staking/src/lib.rs @@ -244,7 +244,6 @@ pub mod pallet { #[pallet::storage] pub type KtonStakingContract = StorageValue<_, T::AccountId>; - #[derive(DefaultNoBound)] #[pallet::genesis_config] pub struct GenesisConfig { /// Current timestamp. @@ -256,7 +255,19 @@ pub mod pallet { #[allow(missing_docs)] pub _marker: PhantomData, } - + impl Default for GenesisConfig + where + T: Config, + { + fn default() -> Self { + Self { + now: Default::default(), + elapsed_time: Default::default(), + collator_count: 1, + _marker: Default::default(), + } + } + } #[pallet::genesis_build] impl BuildGenesisConfig for GenesisConfig { fn build(&self) { diff --git a/pallet/staking/src/mock.rs b/pallet/staking/src/mock.rs index 74c5079f8..a3dceef1b 100644 --- a/pallet/staking/src/mock.rs +++ b/pallet/staking/src/mock.rs @@ -283,7 +283,7 @@ frame_support::construct_runtime! { pub enum Efflux {} impl Efflux { pub fn time(millis: Moment) { - Timestamp::set_timestamp(Timestamp::now() + millis); + Timestamp::set_timestamp(>::get() + millis); } pub fn block(number: BlockNumber) { diff --git a/pallet/staking/src/tests.rs b/pallet/staking/src/tests.rs index c14878ada..ada7bce25 100644 --- a/pallet/staking/src/tests.rs +++ b/pallet/staking/src/tests.rs @@ -292,7 +292,7 @@ fn set_collator_count_should_work() { ); assert_ok!(Staking::set_collator_count(RuntimeOrigin::root(), 1)); - assert_eq!(Staking::collator_count(), 1); + assert_eq!(>::get(), 1); }); } @@ -305,7 +305,7 @@ fn set_ring_staking_contract_should_work() { ); assert_ok!(Staking::set_ring_staking_contract(RuntimeOrigin::root(), AccountId(1))); - assert_eq!(Staking::ring_staking_contract(), Some(AccountId(1))); + assert_eq!(>::get(), Some(AccountId(1))); }); } @@ -318,6 +318,6 @@ fn set_kton_staking_contract_should_work() { ); assert_ok!(Staking::set_kton_staking_contract(RuntimeOrigin::root(), AccountId(1))); - assert_eq!(Staking::kton_staking_contract(), Some(AccountId(1))); + assert_eq!(>::get(), Some(AccountId(1))); }); } diff --git a/runtime/common/src/test.rs b/runtime/common/src/test.rs index e65bffd31..6afa481bf 100644 --- a/runtime/common/src/test.rs +++ b/runtime/common/src/test.rs @@ -112,8 +112,8 @@ macro_rules! impl_account_migration_tests { extra: (), }; - assert!(AccountMigration::account_of(&account_id_32).is_none()); - assert!(AccountMigration::kton_account_of(&account_id_32).is_none()); + assert!(>::get(&account_id_32).is_none()); + assert!(>::get(&account_id_32).is_none()); >::put(RING_AMOUNT); >::insert( @@ -132,8 +132,8 @@ macro_rules! impl_account_migration_tests { &Blake2_128Concat::hash(account_id_32.as_ref()), asset_account, ); - assert!(AccountMigration::account_of(&account_id_32).is_some()); - assert!(AccountMigration::kton_account_of(&account_id_32).is_some()); + assert!(>::get(&account_id_32).is_some()); + assert!(>::get(&account_id_32).is_some()); } fn migrate(from: Pair, to: AccountId) -> Result<(), E> { @@ -215,7 +215,7 @@ macro_rules! impl_account_migration_tests { preset_state_of(&from); assert_ok!(migrate(from, to)); - assert_eq!(AccountMigration::account_of(from_pk), None); + assert_eq!(>::get(from_pk), None); assert_eq!( System::account(to), AccountInfo { @@ -249,7 +249,7 @@ macro_rules! impl_account_migration_tests { assert_ok!(migrate(from, to)); let asset_details = asset_details(); - assert_eq!(AccountMigration::kton_account_of(from_pk), None); + assert_eq!(>::get(from_pk), None); assert_eq!(Assets::maybe_balance(KTON_ID, to).unwrap(), KTON_AMOUNT); assert_eq!(pre_asset_details.accounts + 1, asset_details.accounts); assert_eq!(pre_asset_details.sufficients + 1, asset_details.sufficients); @@ -305,7 +305,7 @@ macro_rules! impl_account_migration_tests { assert_ok!(migrate(from, to)); assert_eq!(Balances::free_balance(to), 80); assert_eq!(Balances::free_balance(&Treasury::account_id()), 20); - assert_eq!(Deposit::deposit_of(to).unwrap().len(), 2); + assert_eq!(>::get(to).unwrap().len(), 2); assert_eq!(Assets::maybe_balance(KTON_ID, to).unwrap(), 100); }); } diff --git a/runtime/crab/tests/mock.rs b/runtime/crab/tests/mock.rs index a126bcf56..16a57cf23 100644 --- a/runtime/crab/tests/mock.rs +++ b/runtime/crab/tests/mock.rs @@ -52,6 +52,7 @@ impl ExtBuilder { assets: vec![(KTON_ID, ROOT, true, 1)], metadata: vec![(KTON_ID, b"Crab Commitment Token".to_vec(), b"CKTON".to_vec(), 18)], accounts: self.assets_accounts.clone(), + next_asset_id: None, } .assimilate_storage(&mut t) .unwrap(); diff --git a/runtime/darwinia/tests/mock.rs b/runtime/darwinia/tests/mock.rs index 2aee625b3..f0c93ebe0 100644 --- a/runtime/darwinia/tests/mock.rs +++ b/runtime/darwinia/tests/mock.rs @@ -52,6 +52,7 @@ impl ExtBuilder { assets: vec![(KTON_ID, ROOT, true, 1)], metadata: vec![(KTON_ID, b"Darwinia Commitment Token".to_vec(), b"KTON".to_vec(), 18)], accounts: self.assets_accounts.clone(), + next_asset_id: None, } .assimilate_storage(&mut t) .unwrap(); diff --git a/runtime/koi/tests/mock.rs b/runtime/koi/tests/mock.rs index a1916795d..a6b03f494 100644 --- a/runtime/koi/tests/mock.rs +++ b/runtime/koi/tests/mock.rs @@ -52,6 +52,7 @@ impl ExtBuilder { assets: vec![(KTON_ID, ROOT, true, 1)], metadata: vec![(KTON_ID, b"Koi Commitment Token".to_vec(), b"PKTON".to_vec(), 18)], accounts: self.assets_accounts.clone(), + next_asset_id: None, } .assimilate_storage(&mut t) .unwrap();