From ab2d22a9daa474bf74a8fa3aa69f9a23846e293b Mon Sep 17 00:00:00 2001 From: bgallois <benjamin@gallois.cc> Date: Thu, 18 Jan 2024 17:58:55 +0100 Subject: [PATCH] wip: fix all pallets tests --- Cargo.lock | 3 +++ pallets/authority-members/Cargo.toml | 4 +++ pallets/authority-members/src/mock.rs | 35 ++++++++++-------------- pallets/certification/src/mock.rs | 21 +++++---------- pallets/distance/src/mock.rs | 39 +++++++++++++-------------- pallets/duniter-account/Cargo.toml | 3 ++- pallets/duniter-wot/Cargo.toml | 4 +++ pallets/duniter-wot/src/mock.rs | 33 +++++++++++------------ pallets/identity/Cargo.toml | 1 + pallets/identity/src/mock.rs | 27 +++++++++---------- 10 files changed, 80 insertions(+), 90 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index df99173ba..aab224f96 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6234,6 +6234,7 @@ dependencies = [ "sp-membership", "sp-runtime 24.0.0", "sp-staking", + "sp-state-machine 0.28.0", "sp-std 8.0.0 (git+https://github.com/bgallois/duniter-polkadot-sdk?branch=duniter-v1.6.0)", ] @@ -6418,6 +6419,7 @@ dependencies = [ "sp-io 23.0.0", "sp-membership", "sp-runtime 24.0.0", + "sp-state-machine 0.28.0", "sp-std 8.0.0 (git+https://github.com/bgallois/duniter-polkadot-sdk?branch=duniter-v1.6.0)", ] @@ -6459,6 +6461,7 @@ dependencies = [ "sp-io 23.0.0", "sp-keystore 0.27.0", "sp-runtime 24.0.0", + "sp-state-machine 0.28.0", "sp-std 8.0.0 (git+https://github.com/bgallois/duniter-polkadot-sdk?branch=duniter-v1.6.0)", ] diff --git a/pallets/authority-members/Cargo.toml b/pallets/authority-members/Cargo.toml index cd3731cec..981bc00d6 100644 --- a/pallets/authority-members/Cargo.toml +++ b/pallets/authority-members/Cargo.toml @@ -109,3 +109,7 @@ version = '1.0.119' [dev-dependencies.sp-io] git = 'https://github.com/bgallois/duniter-polkadot-sdk' branch = 'duniter-v1.6.0' + +[dev-dependencies.sp-state-machine] +git = 'https://github.com/bgallois/duniter-polkadot-sdk' +branch = 'duniter-v1.6.0' diff --git a/pallets/authority-members/src/mock.rs b/pallets/authority-members/src/mock.rs index 4c818534a..01498945a 100644 --- a/pallets/authority-members/src/mock.rs +++ b/pallets/authority-members/src/mock.rs @@ -16,28 +16,24 @@ use super::*; use crate::{self as pallet_authority_members}; -use frame_support::{ - pallet_prelude::*, - parameter_types, - traits::{Everything, GenesisBuild}, - BasicExternalities, -}; +use frame_support::{pallet_prelude::*, parameter_types, traits::Everything}; use frame_system as system; use pallet_offences::traits::OnOffenceHandler; use pallet_offences::SlashStrategy; use pallet_session::ShouldEndSession; use sp_core::{crypto::key_types::DUMMY, H256}; +use sp_runtime::BuildStorage; use sp_runtime::{ impl_opaque_keys, - testing::{Header, UintAuthorityId}, + testing::UintAuthorityId, traits::{BlakeTwo256, ConvertInto, IdentityLookup, IsMember, OpaqueKeys}, KeyTypeId, }; use sp_staking::offence::OffenceDetails; +use sp_state_machine::BasicExternalities; type AccountId = u64; type Block = frame_system::mocking::MockBlock<Test>; -type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>; impl_opaque_keys! { pub struct MockSessionKeys { @@ -53,14 +49,11 @@ impl From<UintAuthorityId> for MockSessionKeys { // Configure a mock runtime to test the pallet. frame_support::construct_runtime!( - pub enum Test where - Block = Block, - NodeBlock = Block, - UncheckedExtrinsic = UncheckedExtrinsic, + pub enum Test { - System: frame_system::{Pallet, Call, Config, Storage, Event<T>}, - Session: pallet_session::{Pallet, Call, Storage, Config<T>, Event}, - AuthorityMembers: pallet_authority_members::{Pallet, Call, Storage, Config<T>, Event<T>}, + System: frame_system, + Session: pallet_session, + AuthorityMembers: pallet_authority_members, } ); @@ -73,16 +66,14 @@ impl system::Config for Test { type BaseCallFilter = Everything; type BlockWeights = (); type BlockLength = (); + type Block = Block; type DbWeight = (); type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; - type Index = u64; - type BlockNumber = u64; type Hash = H256; type Hashing = BlakeTwo256; type AccountId = AccountId; type Lookup = IdentityLookup<Self::AccountId>; - type Header = Header; type RuntimeEvent = RuntimeEvent; type BlockHashCount = BlockHashCount; type Version = (); @@ -94,6 +85,8 @@ impl system::Config for Test { type SS58Prefix = SS58Prefix; type OnSetCode = (); type MaxConsumers = frame_support::traits::ConstU32<16>; + type RuntimeTask = (); + type Nonce = u64; } pub struct TestSessionHandler; @@ -172,10 +165,10 @@ pub fn new_test_ext(initial_authorities_len: u64) -> sp_io::TestExternalities { .map(|i| (i * 3, i * 3, UintAuthorityId(i * 3).into())) .collect(); - let mut t = frame_system::GenesisConfig::default() - .build_storage::<Test>() + let mut t = frame_system::GenesisConfig::<Test>::default() + .build_storage() .unwrap(); - BasicExternalities::execute_with_storage(&mut t, || { + BasicExternalities::build_storage(&mut t, || { for (ref k, ..) in &keys { frame_system::Pallet::<Test>::inc_providers(k); } diff --git a/pallets/certification/src/mock.rs b/pallets/certification/src/mock.rs index 6d730c280..033800b34 100644 --- a/pallets/certification/src/mock.rs +++ b/pallets/certification/src/mock.rs @@ -22,26 +22,19 @@ use frame_support::{ use frame_system as system; use sp_core::H256; use sp_runtime::{ - testing::Header, traits::{BlakeTwo256, IdentityLookup}, BuildStorage, }; type AccountId = u64; -type BlockNumber = u64; type Block = frame_system::mocking::MockBlock<Test>; pub type IdtyIndex = u64; -type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>; // Configure a mock runtime to test the pallet. frame_support::construct_runtime!( - pub enum Test where - Block = Block, - NodeBlock = Block, - UncheckedExtrinsic = UncheckedExtrinsic, - { - System: frame_system::{Pallet, Call, Config, Storage, Event<T>}, - DefaultCertification: pallet_certification::{Pallet, Call, Event<T>, Storage, Config<T>}, + pub enum Test { + System: frame_system, + DefaultCertification: pallet_certification, } ); @@ -54,16 +47,14 @@ impl system::Config for Test { type BaseCallFilter = Everything; type BlockWeights = (); type BlockLength = (); + type Block = Block; type DbWeight = (); type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; - type Index = u64; - type BlockNumber = BlockNumber; type Hash = H256; type Hashing = BlakeTwo256; type AccountId = AccountId; type Lookup = IdentityLookup<Self::AccountId>; - type Header = Header; type RuntimeEvent = RuntimeEvent; type BlockHashCount = BlockHashCount; type Version = (); @@ -75,6 +66,8 @@ impl system::Config for Test { type SS58Prefix = SS58Prefix; type OnSetCode = (); type MaxConsumers = frame_support::traits::ConstU32<16>; + type RuntimeTask = (); + type Nonce = u64; } parameter_types! { @@ -102,7 +95,7 @@ impl pallet_certification::Config for Test { pub fn new_test_ext( gen_conf: pallet_certification::GenesisConfig<Test>, ) -> sp_io::TestExternalities { - GenesisConfig { + RuntimeGenesisConfig { system: SystemConfig::default(), default_certification: gen_conf, } diff --git a/pallets/distance/src/mock.rs b/pallets/distance/src/mock.rs index 6861b3c43..d56212bd7 100644 --- a/pallets/distance/src/mock.rs +++ b/pallets/distance/src/mock.rs @@ -19,23 +19,23 @@ use crate::{self as pallet_distance}; use core::marker::PhantomData; use frame_support::{ parameter_types, - traits::{Everything, GenesisBuild, OnFinalize, OnInitialize}, + traits::{Everything, OnFinalize, OnInitialize}, }; use frame_system as system; use pallet_balances::AccountData; use pallet_session::ShouldEndSession; use sp_core::{ConstU32, H256}; +use sp_runtime::BuildStorage; use sp_runtime::{ impl_opaque_keys, key_types::DUMMY, - testing::{Header, TestSignature, UintAuthorityId}, + testing::{TestSignature, UintAuthorityId}, traits::{BlakeTwo256, ConvertInto, IdentityLookup, IsMember, OpaqueKeys}, KeyTypeId, Perbill, }; type Balance = u64; type Block = frame_system::mocking::MockBlock<Test>; -type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>; pub type AccountId = u64; impl_opaque_keys! { @@ -52,18 +52,14 @@ impl From<UintAuthorityId> for MockSessionKeys { // Configure a mock runtime to test the pallet. frame_support::construct_runtime!( - pub enum Test where - Block = Block, - NodeBlock = Block, - UncheckedExtrinsic = UncheckedExtrinsic, - { - System: frame_system::{Pallet, Call, Config, Storage, Event<T>}, - Session: pallet_session::{Pallet, Call, Storage, Config<T>, Event}, - Authorship: pallet_authorship::{Pallet, Storage}, - AuthorityMembers: pallet_authority_members::{Pallet, Call, Storage, Config<T>, Event<T>}, - Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>}, - Identity: pallet_identity::{Pallet, Call, Storage, Config<T>, Event<T>}, - Distance: pallet_distance::{Pallet, Call, Storage, Event<T>}, + pub enum Test { + System: frame_system, + Session: pallet_session, + Authorship: pallet_authorship, + AuthorityMembers: pallet_authority_members, + Balances: pallet_balances, + Identity: pallet_identity, + Distance: pallet_distance, } ); @@ -76,16 +72,14 @@ impl system::Config for Test { type BaseCallFilter = Everything; type BlockWeights = (); type BlockLength = (); + type Block = Block; type DbWeight = (); type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; - type Index = u64; - type BlockNumber = u64; type Hash = H256; type Hashing = BlakeTwo256; type AccountId = AccountId; type Lookup = IdentityLookup<Self::AccountId>; - type Header = Header; type RuntimeEvent = RuntimeEvent; type BlockHashCount = BlockHashCount; type Version = (); @@ -97,6 +91,8 @@ impl system::Config for Test { type SS58Prefix = SS58Prefix; type OnSetCode = (); type MaxConsumers = frame_support::traits::ConstU32<16>; + type RuntimeTask = (); + type Nonce = u64; } pub struct TestSessionHandler; @@ -207,10 +203,11 @@ impl pallet_balances::Config for Test { type MaxReserves = (); type ReserveIdentifier = [u8; 8]; type RuntimeEvent = RuntimeEvent; - type HoldIdentifier = (); type FreezeIdentifier = (); type MaxHolds = ConstU32<0>; type MaxFreezes = ConstU32<0>; + type RuntimeHoldReason = (); + type RuntimeFreezeReason = (); } parameter_types! { @@ -265,8 +262,8 @@ impl pallet_distance::Config for Test { // Build genesis storage according to the mock runtime. #[allow(dead_code)] // ??? Clippy triggers dead code for new_test_ext while it is used during test benchmark pub fn new_test_ext() -> sp_io::TestExternalities { - let mut t = frame_system::GenesisConfig::default() - .build_storage::<Test>() + let mut t = frame_system::GenesisConfig::<Test>::default() + .build_storage() .unwrap(); pub const NAMES: [&str; 6] = ["Alice", "Bob", "Charlie", "Dave", "Eve", "Ferdie"]; diff --git a/pallets/duniter-account/Cargo.toml b/pallets/duniter-account/Cargo.toml index d6625d274..6d565548d 100644 --- a/pallets/duniter-account/Cargo.toml +++ b/pallets/duniter-account/Cargo.toml @@ -20,6 +20,7 @@ std = [ 'frame-system/std', 'frame-benchmarking/std', 'pallet-balances/std', + 'pallet-identity/std', 'pallet-provide-randomness/std', 'pallet-treasury/std', 'serde', @@ -117,4 +118,4 @@ version = '1.0.119' [dev-dependencies.sp-io] git = 'https://github.com/bgallois/duniter-polkadot-sdk' -branch = 'duniter-v1.6.0' \ No newline at end of file +branch = 'duniter-v1.6.0' diff --git a/pallets/duniter-wot/Cargo.toml b/pallets/duniter-wot/Cargo.toml index 20d134b33..d4dcaa649 100644 --- a/pallets/duniter-wot/Cargo.toml +++ b/pallets/duniter-wot/Cargo.toml @@ -101,3 +101,7 @@ version = '1.0.119' [dev-dependencies.sp-io] git = 'https://github.com/bgallois/duniter-polkadot-sdk' branch = 'duniter-v1.6.0' + +[dev-dependencies.sp-state-machine] +git = 'https://github.com/bgallois/duniter-polkadot-sdk' +branch = 'duniter-v1.6.0' diff --git a/pallets/duniter-wot/src/mock.rs b/pallets/duniter-wot/src/mock.rs index 3ecb6c233..3ac80658c 100644 --- a/pallets/duniter-wot/src/mock.rs +++ b/pallets/duniter-wot/src/mock.rs @@ -19,15 +19,16 @@ use crate::{self as pallet_duniter_wot}; use frame_support::{parameter_types, traits::Everything}; use frame_system as system; use sp_core::H256; +use sp_runtime::BuildStorage; use sp_runtime::{ - testing::{Header, TestSignature, UintAuthorityId}, + testing::{TestSignature, UintAuthorityId}, traits::{BlakeTwo256, IdentityLookup}, }; +use sp_state_machine::BasicExternalities; use std::collections::BTreeMap; type AccountId = u64; type Block = frame_system::mocking::MockBlock<Test>; -type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>; pub struct IdentityIndexOf<T: pallet_identity::Config>(PhantomData<T>); impl<T: pallet_identity::Config> sp_runtime::traits::Convert<T::AccountId, Option<T::IdtyIndex>> @@ -40,16 +41,12 @@ impl<T: pallet_identity::Config> sp_runtime::traits::Convert<T::AccountId, Optio // Configure a mock runtime to test the pallet. frame_support::construct_runtime!( - pub enum Test where - Block = Block, - NodeBlock = Block, - UncheckedExtrinsic = UncheckedExtrinsic, - { - System: frame_system::{Pallet, Call, Config, Storage, Event<T>}, - DuniterWot: pallet_duniter_wot::{Pallet}, - Identity: pallet_identity::{Pallet, Call, Config<T>, Storage, Event<T>}, - Membership: pallet_membership::{Pallet, Config<T>, Storage, Event<T>}, - Cert: pallet_certification::{Pallet, Call, Config<T>, Storage, Event<T>}, + pub enum Test { + System: frame_system, + DuniterWot: pallet_duniter_wot, + Identity: pallet_identity, + Membership: pallet_membership, + Cert: pallet_certification, } ); @@ -64,15 +61,13 @@ impl system::Config for Test { type BlockWeights = (); type BlockLength = (); type DbWeight = (); + type Block = Block; type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; - type Index = u64; - type BlockNumber = u64; type Hash = H256; type Hashing = BlakeTwo256; type AccountId = AccountId; type Lookup = IdentityLookup<Self::AccountId>; - type Header = Header; type RuntimeEvent = RuntimeEvent; type BlockHashCount = BlockHashCount; type Version = (); @@ -84,6 +79,8 @@ impl system::Config for Test { type SS58Prefix = SS58Prefix; type OnSetCode = (); type MaxConsumers = frame_support::traits::ConstU32<16>; + type RuntimeTask = (); + type Nonce = u64; } // DuniterWot @@ -181,8 +178,8 @@ pub const NAMES: [&str; 6] = ["Alice", "Bob", "Charlie", "Dave", "Eve", "Ferdie" // Build genesis storage according to the mock runtime. pub fn new_test_ext(initial_identities_len: usize) -> sp_io::TestExternalities { - let mut t = frame_system::GenesisConfig::default() - .build_storage::<Test>() + let mut t = frame_system::GenesisConfig::<Test>::default() + .build_storage() .unwrap(); pallet_identity::GenesisConfig::<Test> { @@ -226,7 +223,7 @@ pub fn new_test_ext(initial_identities_len: usize) -> sp_io::TestExternalities { .assimilate_storage(&mut t) .unwrap(); - frame_support::BasicExternalities::execute_with_storage(&mut t, || { + BasicExternalities::execute_with_storage(&mut t, || { // manually increment genesis identities sufficient counter // In real world, this is done by pallet-identity for i in 1..=initial_identities_len { diff --git a/pallets/identity/Cargo.toml b/pallets/identity/Cargo.toml index 3ec1233c7..a9b44f3e8 100644 --- a/pallets/identity/Cargo.toml +++ b/pallets/identity/Cargo.toml @@ -77,3 +77,4 @@ branch = 'duniter-v1.6.0' serde = { version = "1", features = ["derive"] } sp-io = { git = 'https://github.com/bgallois/duniter-polkadot-sdk', branch = 'duniter-v1.6.0' } sp-keystore = { git = 'https://github.com/bgallois/duniter-polkadot-sdk', branch = 'duniter-v1.6.0' } +sp-state-machine = { git = 'https://github.com/bgallois/duniter-polkadot-sdk', branch = 'duniter-v1.6.0' } diff --git a/pallets/identity/src/mock.rs b/pallets/identity/src/mock.rs index 59242164b..2458ca8f0 100644 --- a/pallets/identity/src/mock.rs +++ b/pallets/identity/src/mock.rs @@ -18,20 +18,20 @@ use super::*; use crate::{self as pallet_identity}; use frame_support::{ parameter_types, - traits::{Everything, GenesisBuild, OnFinalize, OnInitialize}, + traits::{Everything, OnFinalize, OnInitialize}, }; use frame_system as system; use sp_core::{Pair, H256}; use sp_keystore::{testing::MemoryKeystore, KeystoreExt}; +use sp_runtime::BuildStorage; use sp_runtime::{ - testing::Header, traits::{BlakeTwo256, IdentityLookup}, MultiSignature, MultiSigner, }; +use sp_state_machine::BasicExternalities; use std::sync::Arc; type Block = frame_system::mocking::MockBlock<Test>; -type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>; pub type Signature = MultiSignature; pub type AccountPublic = <Signature as Verify>::Signer; pub type AccountId = <AccountPublic as IdentifyAccount>::AccountId; @@ -43,13 +43,10 @@ fn account(id: u8) -> AccountId { // Configure a mock runtime to test the pallet. frame_support::construct_runtime!( - pub enum Test where - Block = Block, - NodeBlock = Block, - UncheckedExtrinsic = UncheckedExtrinsic, + pub enum Test { - System: frame_system::{Pallet, Call, Config, Storage, Event<T>}, - Identity: pallet_identity::{Pallet, Call, Storage, Config<T>, Event<T>}, + System: frame_system, + Identity: pallet_identity, } ); @@ -63,15 +60,13 @@ impl system::Config for Test { type BlockWeights = (); type BlockLength = (); type DbWeight = (); + type Block = Block; type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; - type Index = u64; - type BlockNumber = u64; type Hash = H256; type Hashing = BlakeTwo256; type AccountId = AccountId; type Lookup = IdentityLookup<Self::AccountId>; - type Header = Header; type RuntimeEvent = RuntimeEvent; type BlockHashCount = BlockHashCount; type Version = (); @@ -83,6 +78,8 @@ impl system::Config for Test { type SS58Prefix = SS58Prefix; type OnSetCode = (); type MaxConsumers = frame_support::traits::ConstU32<16>; + type RuntimeTask = (); + type Nonce = u64; } parameter_types! { @@ -122,13 +119,13 @@ impl pallet_identity::Config for Test { // Build genesis storage according to the mock runtime. pub fn new_test_ext(gen_conf: pallet_identity::GenesisConfig<Test>) -> sp_io::TestExternalities { - let mut t = frame_system::GenesisConfig::default() - .build_storage::<Test>() + let mut t = frame_system::GenesisConfig::<Test>::default() + .build_storage() .unwrap(); gen_conf.assimilate_storage(&mut t).unwrap(); - frame_support::BasicExternalities::execute_with_storage(&mut t, || { + BasicExternalities::execute_with_storage(&mut t, || { frame_system::Pallet::<Test>::inc_providers(&account(2)); frame_system::Pallet::<Test>::inc_providers(&account(3)); }); -- GitLab