From 5f1948a09be77e624bf0b44ab7335cac9ab89b77 Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Wed, 8 Nov 2023 09:18:56 +0400 Subject: [PATCH 1/2] chore: Macros to ease Config implementations in pallet tests (#3463) --- Cargo.lock | 1 + common/Cargo.toml | 1 + common/src/lib.rs | 3 + common/src/pallet_tests.rs | 149 +++++++++++++++++++++ pallets/gas/src/lib.rs | 9 ++ pallets/gas/src/mock.rs | 64 ++------- pallets/gear-bank/Cargo.toml | 1 + pallets/gear-bank/src/lib.rs | 11 ++ pallets/gear-bank/src/mock.rs | 73 ++-------- pallets/gear-debug/Cargo.toml | 1 + pallets/gear-debug/src/mock.rs | 136 ++----------------- pallets/gear-messenger/Cargo.toml | 1 + pallets/gear-messenger/src/lib.rs | 2 + pallets/gear-messenger/src/mock.rs | 56 +------- pallets/gear-messenger/src/pallet_tests.rs | 47 +++++++ pallets/gear-program/Cargo.toml | 1 + pallets/gear-program/src/lib.rs | 2 + pallets/gear-program/src/mock.rs | 105 ++------------- pallets/gear-program/src/pallet_tests.rs | 27 ++++ pallets/gear-scheduler/Cargo.toml | 1 + pallets/gear-scheduler/src/lib.rs | 14 ++ pallets/gear-scheduler/src/mock.rs | 137 ++----------------- pallets/gear-voucher/Cargo.toml | 1 + pallets/gear-voucher/src/mock.rs | 45 +------ pallets/gear/Cargo.toml | 1 + pallets/gear/src/lib.rs | 2 + pallets/gear/src/mock.rs | 142 ++------------------ pallets/gear/src/pallet_tests.rs | 92 +++++++++++++ pallets/payment/Cargo.toml | 1 + pallets/payment/src/mock.rs | 138 ++----------------- pallets/staking-rewards/Cargo.toml | 1 + pallets/staking-rewards/src/mock.rs | 81 ++--------- 32 files changed, 470 insertions(+), 876 deletions(-) create mode 100644 common/src/pallet_tests.rs create mode 100644 pallets/gear-messenger/src/pallet_tests.rs create mode 100644 pallets/gear-program/src/pallet_tests.rs create mode 100644 pallets/gear/src/pallet_tests.rs diff --git a/Cargo.lock b/Cargo.lock index 6a8d750bd2d..2798fd5a74f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7809,6 +7809,7 @@ dependencies = [ "frame-executive", "frame-support", "frame-system", + "gear-common", "impl-trait-for-tuples", "log", "pallet-authorship", diff --git a/common/Cargo.toml b/common/Cargo.toml index 7bdfcb6a03c..e5b5cbbc57f 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -45,6 +45,7 @@ std = [ "sp-io/std", "sp-arithmetic/std", "frame-support/std", + "frame-system/std", "primitive-types/std", "gear-wasm-instrument?/std", ] diff --git a/common/src/lib.rs b/common/src/lib.rs index 6f295e18115..0f700e56d72 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -39,6 +39,9 @@ pub mod gas_provider; #[cfg(feature = "runtime-benchmarks")] pub mod benchmarking; +#[cfg(feature = "std")] +pub mod pallet_tests; + use core::fmt; use frame_support::{ codec::{self, Decode, Encode}, diff --git a/common/src/pallet_tests.rs b/common/src/pallet_tests.rs new file mode 100644 index 00000000000..f58e8844998 --- /dev/null +++ b/common/src/pallet_tests.rs @@ -0,0 +1,149 @@ +// This file is part of Gear. + +// Copyright (C) 2023 Gear Technologies Inc. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +//! Module contains macros that help to implement Config type +//! for various pallets of Substrate. +//! All used types should be in scope. + +use frame_support::{pallet_prelude::*, weights::RuntimeDbWeight}; +use frame_system::limits::BlockWeights; +use sp_arithmetic::Perbill; + +#[macro_export] +macro_rules! impl_pallet_balances { + ($runtime:ty) => { + impl pallet_balances::Config for $runtime { + type MaxLocks = (); + type MaxReserves = (); + type ReserveIdentifier = [u8; 8]; + type Balance = Balance; + type DustRemoval = (); + type RuntimeEvent = RuntimeEvent; + type ExistentialDeposit = ExistentialDeposit; + type AccountStore = System; + type WeightInfo = (); + } + }; +} + +pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); +pub const MAX_BLOCK: u64 = 250_000_000_000; + +frame_support::parameter_types! { + pub RuntimeBlockWeights: BlockWeights = BlockWeights::with_sensible_defaults( + Weight::from_parts(MAX_BLOCK, u64::MAX), + NORMAL_DISPATCH_RATIO, + ); + pub const SS58Prefix: u8 = 42; + pub const DbWeight: RuntimeDbWeight = RuntimeDbWeight { read: 1_110, write: 2_300 }; + pub const MinimumPeriod: u64 = 500; +} + +#[macro_export] +macro_rules! impl_pallet_system { + ($( $tokens:tt )*) => { + #[allow(dead_code)] + type SystemConfigDbWeight = $crate::pallet_tests::DbWeight; + #[allow(dead_code)] + type SystemConfigBlockWeights = $crate::pallet_tests::RuntimeBlockWeights; + + mod pallet_tests_system_config_impl { + use super::*; + + $crate::impl_pallet_system_inner!($( $tokens )*); + } + }; +} + +#[macro_export] +macro_rules! impl_pallet_system_inner { + ($runtime:ty$(,)?) => { + impl frame_system::Config for $runtime { + type BaseCallFilter = frame_support::traits::Everything; + type BlockWeights = SystemConfigBlockWeights; + type BlockLength = (); + type DbWeight = SystemConfigDbWeight; + type RuntimeOrigin = RuntimeOrigin; + type RuntimeCall = RuntimeCall; + type Index = u64; + type BlockNumber = BlockNumber; + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = AccountId; + type Lookup = IdentityLookup; + type Header = generic::Header; + type RuntimeEvent = RuntimeEvent; + type BlockHashCount = BlockHashCount; + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = pallet_balances::AccountData; + type OnNewAccount = (); + type OnKilledAccount = (); + type SystemWeightInfo = (); + type SS58Prefix = $crate::pallet_tests::SS58Prefix; + type OnSetCode = (); + type MaxConsumers = frame_support::traits::ConstU32<16>; + } + }; + + ($runtime:ty, DbWeight = $db_weight:ty $(, $( $rest:tt )*)?) => { + type SystemConfigDbWeight = $db_weight; + + $crate::impl_pallet_system_inner!($runtime, $($( $rest )*)?); + }; + + ($runtime:ty, BlockWeights = $block_weights:ty $(, $( $rest:tt )*)?) => { + type SystemConfigBlockWeights = $block_weights; + + $crate::impl_pallet_system_inner!($runtime, $($( $rest )*)?); + }; +} + +#[macro_export] +macro_rules! impl_pallet_timestamp { + ($runtime:ty) => { + impl pallet_timestamp::Config for Test { + type Moment = u64; + type OnTimestampSet = (); + type MinimumPeriod = $crate::pallet_tests::MinimumPeriod; + type WeightInfo = (); + } + }; +} + +#[macro_export] +macro_rules! impl_pallet_authorship { + ($runtime:ty, EventHandler = $event_handler:ty) => { + pub struct FixedBlockAuthor; + + impl FindAuthor for FixedBlockAuthor { + fn find_author<'a, I: 'a>(_: I) -> Option { + Some(BLOCK_AUTHOR) + } + } + + impl pallet_authorship::Config for $runtime { + type FindAuthor = FixedBlockAuthor; + type EventHandler = $event_handler; + } + }; + + ($runtime:ty) => { + $crate::impl_pallet_authorship!($runtime, EventHandler = ()); + }; +} diff --git a/pallets/gas/src/lib.rs b/pallets/gas/src/lib.rs index db681e5ca60..21775b42b7d 100644 --- a/pallets/gas/src/lib.rs +++ b/pallets/gas/src/lib.rs @@ -145,6 +145,15 @@ type AccountIdOf = ::AccountId; /// The current storage version. const STORAGE_VERSION: StorageVersion = StorageVersion::new(3); +#[macro_export] +macro_rules! impl_config { + ($runtime:ty) => { + impl pallet_gear_gas::Config for $runtime { + type BlockGasLimit = BlockGasLimit; + } + }; +} + #[frame_support::pallet] pub mod pallet { use super::*; diff --git a/pallets/gas/src/mock.rs b/pallets/gas/src/mock.rs index af98db3f605..ad38d831d75 100644 --- a/pallets/gas/src/mock.rs +++ b/pallets/gas/src/mock.rs @@ -16,12 +16,12 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate as pallet_gas; +use crate as pallet_gear_gas; use frame_support::{construct_runtime, parameter_types, weights::constants::RocksDbWeight}; use frame_system as system; use primitive_types::H256; use sp_runtime::{ - testing::Header, + generic, traits::{BlakeTwo256, IdentityLookup}, }; use sp_std::convert::{TryFrom, TryInto}; @@ -31,6 +31,7 @@ type Block = frame_system::mocking::MockBlock; type AccountId = u64; type BlockNumber = u64; type Balance = u128; +type BlockGasLimit = (); pub const ALICE: AccountId = 1; pub const BOB: AccountId = 2; @@ -45,69 +46,22 @@ construct_runtime!( { System: system, GearMessenger: pallet_gear_messenger, - Gas: pallet_gas, + GearGas: pallet_gear_gas, Balances: pallet_balances, } ); -impl pallet_balances::Config for Test { - type MaxLocks = (); - type MaxReserves = (); - type ReserveIdentifier = [u8; 8]; - type Balance = Balance; - type DustRemoval = (); - type RuntimeEvent = RuntimeEvent; - type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; - type WeightInfo = (); -} +common::impl_pallet_system!(Test, DbWeight = RocksDbWeight, BlockWeights = ()); +pallet_gear_messenger::impl_config!(Test, CurrentBlockNumber = GearBlockNumber); +pallet_gear_gas::impl_config!(Test); +common::impl_pallet_balances!(Test); parameter_types! { - pub const BlockHashCount: u64 = 250; - pub const SS58Prefix: u8 = 42; + pub const BlockHashCount: BlockNumber = 250; pub const ExistentialDeposit: Balance = 1; -} - -impl system::Config for Test { - type BaseCallFilter = frame_support::traits::Everything; - type BlockWeights = (); - type BlockLength = (); - type DbWeight = RocksDbWeight; - type RuntimeOrigin = RuntimeOrigin; - type RuntimeCall = RuntimeCall; - type Index = u64; - type BlockNumber = BlockNumber; - type Hash = H256; - type Hashing = BlakeTwo256; - type AccountId = AccountId; - type Lookup = IdentityLookup; - type Header = Header; - type RuntimeEvent = RuntimeEvent; - type BlockHashCount = BlockHashCount; - type Version = (); - type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = SS58Prefix; - type OnSetCode = (); - type MaxConsumers = frame_support::traits::ConstU32<16>; -} - -parameter_types! { pub const GearBlockNumber: BlockNumber = 100; } -impl pallet_gear_messenger::Config for Test { - type BlockLimiter = Gas; - type CurrentBlockNumber = GearBlockNumber; -} - -impl pallet_gas::Config for Test { - type BlockGasLimit = (); -} - // Build genesis storage according to the mock runtime. pub fn new_test_ext() -> sp_io::TestExternalities { let mut t = system::GenesisConfig::default() diff --git a/pallets/gear-bank/Cargo.toml b/pallets/gear-bank/Cargo.toml index 7075b18ace2..0db24956ff8 100644 --- a/pallets/gear-bank/Cargo.toml +++ b/pallets/gear-bank/Cargo.toml @@ -23,6 +23,7 @@ pallet-authorship.workspace = true sp-runtime.workspace = true [dev-dependencies] +common = { workspace = true, features = ["std"] } primitive-types.workspace = true # Substrate deps diff --git a/pallets/gear-bank/src/lib.rs b/pallets/gear-bank/src/lib.rs index 7254daa1e56..240eae05b74 100644 --- a/pallets/gear-bank/src/lib.rs +++ b/pallets/gear-bank/src/lib.rs @@ -30,6 +30,17 @@ pub use pallet::*; use frame_support::traits::{Currency, StorageVersion}; +#[macro_export] +macro_rules! impl_config { + ($runtime:ty) => { + impl pallet_gear_bank::Config for $runtime { + type Currency = Balances; + type BankAddress = BankAddress; + type GasMultiplier = GasMultiplier; + } + }; +} + pub(crate) type AccountIdOf = ::AccountId; pub(crate) type BalanceOf = as Currency>>::Balance; pub(crate) type CurrencyOf = ::Currency; diff --git a/pallets/gear-bank/src/mock.rs b/pallets/gear-bank/src/mock.rs index 21182ff87e5..c20e4bf6fd2 100644 --- a/pallets/gear-bank/src/mock.rs +++ b/pallets/gear-bank/src/mock.rs @@ -18,21 +18,19 @@ use crate as pallet_gear_bank; use frame_support::{ - construct_runtime, parameter_types, - traits::{Everything, FindAuthor}, - weights::constants::RocksDbWeight, + construct_runtime, parameter_types, traits::FindAuthor, weights::constants::RocksDbWeight, }; use frame_system::mocking::{MockBlock, MockUncheckedExtrinsic}; -use pallet_balances::AccountData; use primitive_types::H256; use sp_io::TestExternalities; use sp_runtime::{ - testing::Header, - traits::{BlakeTwo256, ConstU32, IdentityLookup}, + generic, + traits::{BlakeTwo256, IdentityLookup}, }; pub type AccountId = u8; pub type Balance = u128; +type BlockNumber = u64; mod consts { #![allow(unused)] @@ -62,7 +60,7 @@ pub use consts::*; parameter_types! { pub const BankAddress: AccountId = BANK_ADDRESS; pub const GasMultiplier: common::GasMultiplier = common::GasMultiplier::ValuePerGas(VALUE_PER_GAS); - pub const BlockHashCount: u64 = 250; + pub const BlockHashCount: BlockNumber = 250; pub const ExistentialDeposit: Balance = EXISTENTIAL_DEPOSIT; } @@ -79,63 +77,10 @@ construct_runtime!( } ); -impl frame_system::Config for Test { - type BaseCallFilter = Everything; - type BlockWeights = (); - type BlockLength = (); - type DbWeight = RocksDbWeight; - type RuntimeOrigin = RuntimeOrigin; - type RuntimeCall = RuntimeCall; - type Index = u64; - type BlockNumber = u64; - type Hash = H256; - type Hashing = BlakeTwo256; - type AccountId = AccountId; - type Lookup = IdentityLookup; - type Header = Header; - type RuntimeEvent = RuntimeEvent; - type BlockHashCount = BlockHashCount; - type Version = (); - type PalletInfo = PalletInfo; - type AccountData = AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = (); - type OnSetCode = (); - type MaxConsumers = ConstU32<16>; -} - -pub struct FixedBlockAuthor; - -impl FindAuthor for FixedBlockAuthor { - fn find_author<'a, I: 'a>(_: I) -> Option { - Some(BLOCK_AUTHOR) - } -} - -impl pallet_authorship::Config for Test { - type FindAuthor = FixedBlockAuthor; - type EventHandler = (); -} - -impl pallet_balances::Config for Test { - type MaxLocks = (); - type MaxReserves = (); - type ReserveIdentifier = [u8; 8]; - type Balance = Balance; - type DustRemoval = (); - type RuntimeEvent = RuntimeEvent; - type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; - type WeightInfo = (); -} - -impl pallet_gear_bank::Config for Test { - type Currency = Balances; - type BankAddress = BankAddress; - type GasMultiplier = GasMultiplier; -} +common::impl_pallet_system!(Test, DbWeight = RocksDbWeight, BlockWeights = ()); +common::impl_pallet_authorship!(Test); +common::impl_pallet_balances!(Test); +pallet_gear_bank::impl_config!(Test); pub fn new_test_ext() -> TestExternalities { let mut storage = frame_system::GenesisConfig::default() diff --git a/pallets/gear-debug/Cargo.toml b/pallets/gear-debug/Cargo.toml index 449d6f13eee..e0c9efb3478 100644 --- a/pallets/gear-debug/Cargo.toml +++ b/pallets/gear-debug/Cargo.toml @@ -36,6 +36,7 @@ pallet-authorship.workspace = true [dev-dependencies] serde.workspace = true env_logger.workspace = true +common = { workspace = true, features = ["std"] } wabt.workspace = true hex-literal.workspace = true frame-support-test = { workspace = true, features = ["std"] } diff --git a/pallets/gear-debug/src/mock.rs b/pallets/gear-debug/src/mock.rs index ae669dd7f33..0306045e380 100644 --- a/pallets/gear-debug/src/mock.rs +++ b/pallets/gear-debug/src/mock.rs @@ -29,7 +29,7 @@ use pallet_gear::GasAllowanceOf; use primitive_types::H256; use sp_core::ConstBool; use sp_runtime::{ - testing::Header, + generic, traits::{BlakeTwo256, ConstU64, IdentityLookup}, }; use sp_std::convert::{TryFrom, TryInto}; @@ -42,51 +42,11 @@ type Balance = u128; pub const BLOCK_AUTHOR: AccountId = 255; -impl pallet_balances::Config for Test { - type MaxLocks = (); - type MaxReserves = (); - type ReserveIdentifier = [u8; 8]; - type Balance = Balance; - type DustRemoval = (); - type RuntimeEvent = RuntimeEvent; - type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; - type WeightInfo = (); -} - parameter_types! { - pub const BlockHashCount: u64 = 250; - pub const SS58Prefix: u8 = 42; + pub const BlockHashCount: BlockNumber = 250; pub const ExistentialDeposit: Balance = 1; } -impl system::Config for Test { - type BaseCallFilter = frame_support::traits::Everything; - type BlockWeights = (); - type BlockLength = (); - 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; - type Header = Header; - type RuntimeEvent = RuntimeEvent; - type BlockHashCount = BlockHashCount; - type Version = (); - type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = SS58Prefix; - type OnSetCode = (); - type MaxConsumers = frame_support::traits::ConstU32<16>; -} - impl pallet_gear_debug::Config for Test { type RuntimeEvent = RuntimeEvent; type WeightInfo = (); @@ -95,42 +55,12 @@ impl pallet_gear_debug::Config for Test { type Messenger = GearMessenger; } -pub struct FixedBlockAuthor; - -impl FindAuthor for FixedBlockAuthor { - fn find_author<'a, I>(_digests: I) -> Option - where - I: 'a + IntoIterator, - { - Some(BLOCK_AUTHOR) - } -} - -impl pallet_authorship::Config for Test { - type FindAuthor = FixedBlockAuthor; - - type EventHandler = (); -} - parameter_types! { - pub const MinimumPeriod: u64 = 500; pub const OutgoingLimit: u32 = 1024; pub const BlockGasLimit: u64 = 100_000_000_000; pub const PerformanceMultiplier: u32 = 100; } -impl pallet_timestamp::Config for Test { - type Moment = u64; - type OnTimestampSet = (); - type MinimumPeriod = MinimumPeriod; - type WeightInfo = (); -} - -impl pallet_gear_program::Config for Test { - type Scheduler = GearScheduler; - type CurrentBlockNumber = (); -} - parameter_types! { pub RentFreePeriod: BlockNumber = 1_000; pub RentCostPerBlock: Balance = 11; @@ -138,56 +68,7 @@ parameter_types! { pub ResumeSessionDuration: BlockNumber = 1_000; pub const BankAddress: AccountId = 15082001; pub const GasMultiplier: common::GasMultiplier = common::GasMultiplier::ValuePerGas(25); -} - -impl pallet_gear_bank::Config for Test { - type Currency = Balances; - type BankAddress = BankAddress; - type GasMultiplier = GasMultiplier; -} - -impl pallet_gear::Config for Test { - type RuntimeEvent = RuntimeEvent; - type Randomness = TestRandomness; - type WeightInfo = (); - type OutgoingLimit = OutgoingLimit; - type PerformanceMultiplier = PerformanceMultiplier; - type DebugInfo = super::Pallet; - type Schedule = (); - type CodeStorage = GearProgram; - type ProgramStorage = GearProgram; - type MailboxThreshold = ConstU64<3000>; - type ReservationsLimit = ConstU64<256>; - type Messenger = GearMessenger; - type GasProvider = GearGas; - type BlockLimiter = GearGas; - type Scheduler = GearScheduler; - type QueueRunner = Gear; - type Voucher = (); - type ProgramRentFreePeriod = RentFreePeriod; - type ProgramResumeMinimalRentPeriod = ResumeMinimalPeriod; - type ProgramRentCostPerBlock = RentCostPerBlock; - type ProgramResumeSessionDuration = ResumeSessionDuration; - type ProgramRentEnabled = ConstBool; - type ProgramRentDisabledDelta = RentFreePeriod; -} - -impl pallet_gear_messenger::Config for Test { - type BlockLimiter = GearGas; - type CurrentBlockNumber = Gear; -} - -impl pallet_gear_scheduler::Config for Test { - type BlockLimiter = GearGas; - type ReserveThreshold = ConstU64<1>; - type WaitlistCost = ConstU64<100>; - type MailboxCost = ConstU64<100>; - type ReservationCost = ConstU64<100>; - type DispatchHoldCost = ConstU64<100>; -} - -impl pallet_gear_gas::Config for Test { - type BlockGasLimit = BlockGasLimit; + pub ReserveThreshold: BlockNumber = 1; } // Configure a mock runtime to test the pallet. @@ -211,6 +92,17 @@ construct_runtime!( } ); +common::impl_pallet_system!(Test, DbWeight = (), BlockWeights = ()); +common::impl_pallet_balances!(Test); +common::impl_pallet_authorship!(Test); +common::impl_pallet_timestamp!(Test); +pallet_gear_program::impl_config!(Test); +pallet_gear_messenger::impl_config!(Test, CurrentBlockNumber = Gear); +pallet_gear_scheduler::impl_config!(Test); +pallet_gear_bank::impl_config!(Test); +pallet_gear::impl_config!(Test, DebugInfo = pallet_gear_debug::Pallet, ProgramRentEnabled = ConstBool); +pallet_gear_gas::impl_config!(Test); + // Build genesis storage according to the mock runtime. pub fn new_test_ext() -> sp_io::TestExternalities { let mut t = system::GenesisConfig::default() diff --git a/pallets/gear-messenger/Cargo.toml b/pallets/gear-messenger/Cargo.toml index 8037b76a09f..b04b8338531 100644 --- a/pallets/gear-messenger/Cargo.toml +++ b/pallets/gear-messenger/Cargo.toml @@ -37,6 +37,7 @@ pallet-balances = { workspace = true, features = ["std"] } pallet-authorship = { workspace = true, features = ["std"] } pallet-timestamp = { workspace = true, features = ["std"] } env_logger.workspace = true +common = { workspace = true, features = ["std"] } [features] default = ['std'] diff --git a/pallets/gear-messenger/src/lib.rs b/pallets/gear-messenger/src/lib.rs index cc32d5e3a8b..0c0330ec073 100644 --- a/pallets/gear-messenger/src/lib.rs +++ b/pallets/gear-messenger/src/lib.rs @@ -142,6 +142,8 @@ mod mock; #[cfg(test)] mod tests; +pub mod pallet_tests; + // Public exports from pallet. pub use pallet::*; diff --git a/pallets/gear-messenger/src/mock.rs b/pallets/gear-messenger/src/mock.rs index 0f714caafbe..1ce9591ae1e 100644 --- a/pallets/gear-messenger/src/mock.rs +++ b/pallets/gear-messenger/src/mock.rs @@ -26,7 +26,7 @@ use frame_support::{ use frame_system as system; use primitive_types::H256; use sp_runtime::{ - testing::Header, + generic, traits::{BlakeTwo256, IdentityLookup}, }; use sp_std::convert::{TryFrom, TryInto}; @@ -51,61 +51,17 @@ construct_runtime!( } ); -impl pallet_balances::Config for Test { - type MaxLocks = (); - type MaxReserves = (); - type ReserveIdentifier = [u8; 8]; - type Balance = Balance; - type DustRemoval = (); - type RuntimeEvent = RuntimeEvent; - type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; - type WeightInfo = (); -} +common::impl_pallet_system!(Test, DbWeight = (), BlockWeights = ()); +common::impl_pallet_balances!(Test); +pallet_gear_gas::impl_config!(Test); +pallet_gear_messenger::impl_config!(Test); parameter_types! { - pub const BlockHashCount: u64 = 250; - pub const SS58Prefix: u8 = 42; + pub const BlockHashCount: BlockNumber = 250; pub const ExistentialDeposit: Balance = 1; pub const BlockGasLimit: u64 = 100_000_000; } -impl pallet_gear_gas::Config for Test { - type BlockGasLimit = BlockGasLimit; -} - -impl system::Config for Test { - type BaseCallFilter = frame_support::traits::Everything; - type BlockWeights = (); - type BlockLength = (); - 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; - type Header = Header; - type RuntimeEvent = RuntimeEvent; - type BlockHashCount = BlockHashCount; - type Version = (); - type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = SS58Prefix; - type OnSetCode = (); - type MaxConsumers = frame_support::traits::ConstU32<16>; -} - -impl pallet_gear_messenger::Config for Test { - type BlockLimiter = GearGas; - type CurrentBlockNumber = (); -} - // Build genesis storage according to the mock runtime. pub fn new_test_ext() -> sp_io::TestExternalities { let mut t = system::GenesisConfig::default() diff --git a/pallets/gear-messenger/src/pallet_tests.rs b/pallets/gear-messenger/src/pallet_tests.rs new file mode 100644 index 00000000000..3d1adbfa780 --- /dev/null +++ b/pallets/gear-messenger/src/pallet_tests.rs @@ -0,0 +1,47 @@ +// This file is part of Gear. + +// Copyright (C) 2023 Gear Technologies Inc. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#[macro_export] +macro_rules! impl_config { + ($( $tokens:tt )*) => { + #[allow(dead_code)] + type GearMessengerCurrentBlockNumber = (); + + mod pallet_tests_gear_messenger_config_impl { + use super::*; + + $crate::impl_config_inner!($( $tokens )*); + } + }; +} + +#[macro_export] +macro_rules! impl_config_inner { + ($runtime:ty$(,)?) => { + impl pallet_gear_messenger::Config for $runtime { + type BlockLimiter = GearGas; + type CurrentBlockNumber = GearMessengerCurrentBlockNumber; + } + }; + + ($runtime:ty, CurrentBlockNumber = $current_block_number:ty $(, $( $rest:tt )*)?) => { + type GearMessengerCurrentBlockNumber = $current_block_number; + + $crate::impl_config_inner!($runtime, $($( $rest )*)?); + }; +} diff --git a/pallets/gear-program/Cargo.toml b/pallets/gear-program/Cargo.toml index d39b18f9446..7b1a063b145 100644 --- a/pallets/gear-program/Cargo.toml +++ b/pallets/gear-program/Cargo.toml @@ -33,6 +33,7 @@ sp-io.workspace = true sp-runtime.workspace = true [dev-dependencies] +common = { workspace = true, features = ["std"] } pallet-balances = { workspace = true, features = ["std"] } pallet-authorship = { workspace = true, features = ["std"] } pallet-timestamp = { workspace = true, features = ["std"] } diff --git a/pallets/gear-program/src/lib.rs b/pallets/gear-program/src/lib.rs index bb3fd47b935..f6ea7129d3e 100644 --- a/pallets/gear-program/src/lib.rs +++ b/pallets/gear-program/src/lib.rs @@ -138,6 +138,8 @@ pub mod migrations; #[cfg(test)] mod mock; +pub mod pallet_tests; + #[frame_support::pallet] pub mod pallet { use super::*; diff --git a/pallets/gear-program/src/mock.rs b/pallets/gear-program/src/mock.rs index 6937c6b4cdb..b9b7007f684 100644 --- a/pallets/gear-program/src/mock.rs +++ b/pallets/gear-program/src/mock.rs @@ -20,6 +20,7 @@ use crate as pallet_gear_program; use crate::*; +use common::pallet_tests::MAX_BLOCK; use frame_support::{ construct_runtime, pallet_prelude::*, @@ -30,7 +31,7 @@ use frame_support::{ use frame_system::{self as system, limits::BlockWeights}; use sp_core::H256; use sp_runtime::{ - testing::Header, + generic, traits::{BlakeTwo256, IdentityLookup}, Perbill, }; @@ -47,8 +48,6 @@ pub(crate) const USER_2: AccountId = 2; pub(crate) const USER_3: AccountId = 3; pub(crate) const LOW_BALANCE_USER: AccountId = 4; pub(crate) const BLOCK_AUTHOR: AccountId = 255; -const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); -const MAX_BLOCK: u64 = 100_000_000_000; // Configure a mock runtime to test the pallet. construct_runtime!( @@ -67,101 +66,19 @@ construct_runtime!( } ); -impl pallet_balances::Config for Test { - type MaxLocks = (); - type MaxReserves = (); - type ReserveIdentifier = [u8; 8]; - type Balance = Balance; - type DustRemoval = (); - type RuntimeEvent = RuntimeEvent; - type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; - type WeightInfo = (); -} +common::impl_pallet_system!(Test); +pallet_gear_program::impl_config!(Test); +pallet_gear_scheduler::impl_config!(Test); +pallet_gear_gas::impl_config!(Test); +common::impl_pallet_balances!(Test); +common::impl_pallet_authorship!(Test); +common::impl_pallet_timestamp!(Test); parameter_types! { pub const BlockGasLimit: u64 = MAX_BLOCK; - pub const BlockHashCount: u64 = 250; - pub RuntimeBlockWeights: BlockWeights = BlockWeights::with_sensible_defaults( - Weight::from_parts(MAX_BLOCK, u64::MAX), - NORMAL_DISPATCH_RATIO, - ); - pub const SS58Prefix: u8 = 42; + pub const BlockHashCount: BlockNumber = 250; pub const ExistentialDeposit: Balance = 500; - pub const DbWeight: RuntimeDbWeight = RuntimeDbWeight { read: 1110, write: 2300 }; -} - -impl system::Config for Test { - type BaseCallFilter = frame_support::traits::Everything; - type BlockWeights = RuntimeBlockWeights; - type BlockLength = (); - type DbWeight = 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; - type Header = Header; - type RuntimeEvent = RuntimeEvent; - type BlockHashCount = BlockHashCount; - type Version = (); - type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = SS58Prefix; - type OnSetCode = (); - type MaxConsumers = frame_support::traits::ConstU32<16>; -} - -impl pallet_gear_program::Config for Test { - type Scheduler = GearScheduler; - type CurrentBlockNumber = (); -} - -impl pallet_gear_scheduler::Config for Test { - type BlockLimiter = GearGas; - type ReserveThreshold = ConstU64<1>; - type WaitlistCost = ConstU64<100>; - type MailboxCost = ConstU64<100>; - type ReservationCost = ConstU64<100>; - type DispatchHoldCost = ConstU64<100>; -} - -impl pallet_gear_gas::Config for Test { - type BlockGasLimit = BlockGasLimit; -} - -pub struct FixedBlockAuthor; - -impl FindAuthor for FixedBlockAuthor { - fn find_author<'a, I>(_digests: I) -> Option - where - I: 'a + IntoIterator, - { - Some(BLOCK_AUTHOR) - } -} - -impl pallet_authorship::Config for Test { - type FindAuthor = FixedBlockAuthor; - - type EventHandler = (); -} - -parameter_types! { - pub const MinimumPeriod: u64 = 500; -} - -impl pallet_timestamp::Config for Test { - type Moment = u64; - type OnTimestampSet = (); - type MinimumPeriod = MinimumPeriod; - type WeightInfo = (); + pub ReserveThreshold: BlockNumber = 1; } // Build genesis storage according to the mock runtime. diff --git a/pallets/gear-program/src/pallet_tests.rs b/pallets/gear-program/src/pallet_tests.rs new file mode 100644 index 00000000000..7878f15f04b --- /dev/null +++ b/pallets/gear-program/src/pallet_tests.rs @@ -0,0 +1,27 @@ +// This file is part of Gear. + +// Copyright (C) 2023 Gear Technologies Inc. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#[macro_export] +macro_rules! impl_config { + ($runtime:ty) => { + impl pallet_gear_program::Config for $runtime { + type Scheduler = GearScheduler; + type CurrentBlockNumber = (); + } + }; +} diff --git a/pallets/gear-scheduler/Cargo.toml b/pallets/gear-scheduler/Cargo.toml index d85e5827e3b..5e310760e9f 100644 --- a/pallets/gear-scheduler/Cargo.toml +++ b/pallets/gear-scheduler/Cargo.toml @@ -44,6 +44,7 @@ pallet-balances = { workspace = true, features = ["std"] } pallet-authorship = { workspace = true, features = ["std"] } pallet-timestamp = { workspace = true, features = ["std"] } env_logger.workspace = true +common = { workspace = true, features = ["std"] } [features] default = ['std'] diff --git a/pallets/gear-scheduler/src/lib.rs b/pallets/gear-scheduler/src/lib.rs index eadecd43ca0..1049aefdd1b 100644 --- a/pallets/gear-scheduler/src/lib.rs +++ b/pallets/gear-scheduler/src/lib.rs @@ -28,6 +28,20 @@ mod mock; #[cfg(test)] mod tests; +#[macro_export] +macro_rules! impl_config { + ($runtime:ty) => { + impl pallet_gear_scheduler::Config for $runtime { + type BlockLimiter = GearGas; + type ReserveThreshold = ReserveThreshold; + type WaitlistCost = ConstU64<100>; + type MailboxCost = ConstU64<100>; + type ReservationCost = ConstU64<100>; + type DispatchHoldCost = ConstU64<100>; + } + }; +} + // Public exports from pallet. pub use pallet::*; diff --git a/pallets/gear-scheduler/src/mock.rs b/pallets/gear-scheduler/src/mock.rs index 8b662f87b0b..65e97e9802d 100644 --- a/pallets/gear-scheduler/src/mock.rs +++ b/pallets/gear-scheduler/src/mock.rs @@ -30,7 +30,7 @@ use frame_system::{self as system, limits::BlockWeights}; use pallet_gear::GasAllowanceOf; use sp_core::{ConstBool, H256}; use sp_runtime::{ - testing::Header, + generic, traits::{BlakeTwo256, IdentityLookup}, }; @@ -68,54 +68,21 @@ construct_runtime!( } ); -impl pallet_balances::Config for Test { - type MaxLocks = (); - type MaxReserves = (); - type ReserveIdentifier = [u8; 8]; - type Balance = Balance; - type DustRemoval = (); - type RuntimeEvent = RuntimeEvent; - type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; - type WeightInfo = (); -} +common::impl_pallet_system!(Test, DbWeight = RocksDbWeight, BlockWeights = ()); +common::impl_pallet_timestamp!(Test); +common::impl_pallet_authorship!(Test); +common::impl_pallet_balances!(Test); +pallet_gear_program::impl_config!(Test); +pallet_gear_messenger::impl_config!(Test, CurrentBlockNumber = Gear); +pallet_gear_scheduler::impl_config!(Test); +pallet_gear_bank::impl_config!(Test); +pallet_gear::impl_config!(Test, Schedule = GearSchedule); +pallet_gear_gas::impl_config!(Test); parameter_types! { - pub const BlockHashCount: u64 = 250; - pub const SS58Prefix: u8 = 42; + pub const BlockHashCount: BlockNumber = 250; pub const ExistentialDeposit: Balance = 500; -} - -impl system::Config for Test { - type BaseCallFilter = frame_support::traits::Everything; - type BlockWeights = (); - type BlockLength = (); - type DbWeight = RocksDbWeight; - type RuntimeOrigin = RuntimeOrigin; - type RuntimeCall = RuntimeCall; - type Index = u64; - type BlockNumber = BlockNumber; - type Hash = H256; - type Hashing = BlakeTwo256; - type AccountId = AccountId; - type Lookup = IdentityLookup; - type Header = Header; - type RuntimeEvent = RuntimeEvent; - type BlockHashCount = BlockHashCount; - type Version = (); - type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = SS58Prefix; - type OnSetCode = (); - type MaxConsumers = frame_support::traits::ConstU32<16>; -} - -impl pallet_gear_program::Config for Test { - type Scheduler = GearScheduler; - type CurrentBlockNumber = (); + pub ReserveThreshold: BlockNumber = 1; } parameter_types! { @@ -131,84 +98,6 @@ parameter_types! { pub const GasMultiplier: common::GasMultiplier = common::GasMultiplier::ValuePerGas(25); } -impl pallet_gear_bank::Config for Test { - type Currency = Balances; - type BankAddress = BankAddress; - type GasMultiplier = GasMultiplier; -} - -impl pallet_gear::Config for Test { - type RuntimeEvent = RuntimeEvent; - type Randomness = TestRandomness; - type WeightInfo = (); - type Schedule = GearSchedule; - type OutgoingLimit = OutgoingLimit; - type PerformanceMultiplier = PerformanceMultiplier; - type DebugInfo = (); - type CodeStorage = GearProgram; - type ProgramStorage = GearProgram; - type MailboxThreshold = ConstU64<3000>; - type ReservationsLimit = ConstU64<256>; - type Messenger = GearMessenger; - type GasProvider = GearGas; - type BlockLimiter = GearGas; - type Scheduler = GearScheduler; - type QueueRunner = Gear; - type Voucher = (); - type ProgramRentFreePeriod = RentFreePeriod; - type ProgramResumeMinimalRentPeriod = ResumeMinimalPeriod; - type ProgramRentCostPerBlock = RentCostPerBlock; - type ProgramResumeSessionDuration = ResumeSessionDuration; - type ProgramRentEnabled = ConstBool; - type ProgramRentDisabledDelta = RentFreePeriod; -} - -impl pallet_gear_scheduler::Config for Test { - type BlockLimiter = GearGas; - type ReserveThreshold = ConstU64<1>; - type WaitlistCost = ConstU64<100>; - type MailboxCost = ConstU64<100>; - type ReservationCost = ConstU64<100>; - type DispatchHoldCost = ConstU64<100>; -} - -impl pallet_gear_gas::Config for Test { - type BlockGasLimit = BlockGasLimit; -} - -impl pallet_gear_messenger::Config for Test { - type BlockLimiter = GearGas; - type CurrentBlockNumber = Gear; -} - -pub struct FixedBlockAuthor; - -impl FindAuthor for FixedBlockAuthor { - fn find_author<'a, I>(_digests: I) -> Option - where - I: 'a + IntoIterator, - { - Some(BLOCK_AUTHOR) - } -} - -impl pallet_authorship::Config for Test { - type FindAuthor = FixedBlockAuthor; - - type EventHandler = (); -} - -parameter_types! { - pub const MinimumPeriod: u64 = 500; -} - -impl pallet_timestamp::Config for Test { - type Moment = u64; - type OnTimestampSet = (); - type MinimumPeriod = MinimumPeriod; - type WeightInfo = (); -} - // Build genesis storage according to the mock runtime. pub fn new_test_ext() -> sp_io::TestExternalities { let mut t = system::GenesisConfig::default() diff --git a/pallets/gear-voucher/Cargo.toml b/pallets/gear-voucher/Cargo.toml index 95a87591564..dcbf7094a70 100644 --- a/pallets/gear-voucher/Cargo.toml +++ b/pallets/gear-voucher/Cargo.toml @@ -35,6 +35,7 @@ pallet-balances.workspace = true [dev-dependencies] serde.workspace = true env_logger.workspace = true +common = { workspace = true, features = ["std"] } sp-core = {workspace = true, features = ["std"] } [features] diff --git a/pallets/gear-voucher/src/mock.rs b/pallets/gear-voucher/src/mock.rs index 180772bd8c5..418dedc28bc 100644 --- a/pallets/gear-voucher/src/mock.rs +++ b/pallets/gear-voucher/src/mock.rs @@ -23,7 +23,7 @@ use frame_support::{ use frame_system as system; use primitive_types::H256; use sp_runtime::{ - testing::Header, + generic, traits::{BlakeTwo256, IdentityLookup}, }; use sp_std::convert::{TryFrom, TryInto}; @@ -51,49 +51,12 @@ construct_runtime!( ); parameter_types! { - pub const BlockHashCount: u64 = 250; - pub const SS58Prefix: u8 = 42; + pub const BlockHashCount: BlockNumber = 250; pub const ExistentialDeposit: Balance = 1; } -impl system::Config for Test { - type BaseCallFilter = frame_support::traits::Everything; - type BlockWeights = (); - type BlockLength = (); - type DbWeight = RocksDbWeight; - type RuntimeOrigin = RuntimeOrigin; - type RuntimeCall = RuntimeCall; - type Index = u64; - type BlockNumber = BlockNumber; - type Hash = H256; - type Hashing = BlakeTwo256; - type AccountId = AccountId; - type Lookup = IdentityLookup; - type Header = Header; - type RuntimeEvent = RuntimeEvent; - type BlockHashCount = BlockHashCount; - type Version = (); - type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = SS58Prefix; - type OnSetCode = (); - type MaxConsumers = frame_support::traits::ConstU32<16>; -} - -impl pallet_balances::Config for Test { - type MaxLocks = (); - type MaxReserves = (); - type ReserveIdentifier = [u8; 8]; - type Balance = Balance; - type DustRemoval = (); - type RuntimeEvent = RuntimeEvent; - type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; - type WeightInfo = (); -} +common::impl_pallet_system!(Test, DbWeight = RocksDbWeight, BlockWeights = ()); +common::impl_pallet_balances!(Test); parameter_types! { pub const VoucherPalletId: PalletId = PalletId(*b"py/vouch"); diff --git a/pallets/gear/Cargo.toml b/pallets/gear/Cargo.toml index 6b412909f43..b0e76956394 100644 --- a/pallets/gear/Cargo.toml +++ b/pallets/gear/Cargo.toml @@ -116,6 +116,7 @@ demo-custom.workspace = true test-syscalls.workspace = true page_size.workspace = true frame-support-test = { workspace = true, features = ["std"] } +common = { workspace = true, features = ["std"] } pallet-gear-gas = { workspace = true, features = ["std"] } pallet-gear-messenger = { workspace = true, features = ["std"] } pallet-gear-scheduler = { workspace = true, features = ["std"] } diff --git a/pallets/gear/src/lib.rs b/pallets/gear/src/lib.rs index 4204074235c..0a173351c35 100644 --- a/pallets/gear/src/lib.rs +++ b/pallets/gear/src/lib.rs @@ -38,6 +38,8 @@ mod mock; #[cfg(test)] mod tests; +pub mod pallet_tests; + pub use crate::{ manager::{ExtManager, HandleKind}, pallet::*, diff --git a/pallets/gear/src/mock.rs b/pallets/gear/src/mock.rs index d0702ce788f..84d3a630975 100644 --- a/pallets/gear/src/mock.rs +++ b/pallets/gear/src/mock.rs @@ -18,12 +18,12 @@ use crate as pallet_gear; use crate::*; +use common::pallet_tests::MAX_BLOCK; use frame_support::{ construct_runtime, pallet_prelude::*, parameter_types, traits::{ConstU64, FindAuthor, Get}, - weights::RuntimeDbWeight, PalletId, }; use frame_support_test::TestRandomness; @@ -32,7 +32,6 @@ use sp_core::H256; use sp_runtime::{ generic, traits::{BlakeTwo256, IdentityLookup}, - Perbill, }; use sp_std::{ cell::RefCell, @@ -52,8 +51,6 @@ pub(crate) const USER_2: AccountId = 2; pub(crate) const USER_3: AccountId = 3; pub(crate) const LOW_BALANCE_USER: AccountId = 4; pub(crate) const BLOCK_AUTHOR: AccountId = 255; -const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); -const MAX_BLOCK: u64 = 250_000_000_000; macro_rules! dry_run { ( @@ -91,59 +88,20 @@ construct_runtime!( } ); -impl pallet_balances::Config for Test { - type MaxLocks = (); - type MaxReserves = (); - type ReserveIdentifier = [u8; 8]; - type Balance = Balance; - type DustRemoval = (); - type RuntimeEvent = RuntimeEvent; - type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; - type WeightInfo = (); -} +common::impl_pallet_system!(Test); +pallet_gear_program::impl_config!(Test); +pallet_gear_messenger::impl_config!(Test, CurrentBlockNumber = Gear); +pallet_gear_scheduler::impl_config!(Test); +pallet_gear_bank::impl_config!(Test); +pallet_gear::impl_config!(Test, Schedule = DynamicSchedule, Voucher = GearVoucher); +pallet_gear_gas::impl_config!(Test); +common::impl_pallet_balances!(Test); +common::impl_pallet_authorship!(Test); +common::impl_pallet_timestamp!(Test); parameter_types! { pub const BlockHashCount: BlockNumber = 250; - pub RuntimeBlockWeights: BlockWeights = BlockWeights::with_sensible_defaults( - Weight::from_parts(MAX_BLOCK, u64::MAX), - NORMAL_DISPATCH_RATIO, - ); - pub const SS58Prefix: u8 = 42; pub const ExistentialDeposit: Balance = 500; - pub const DbWeight: RuntimeDbWeight = RuntimeDbWeight { read: 1110, write: 2300 }; -} - -impl system::Config for Test { - type BaseCallFilter = frame_support::traits::Everything; - type BlockWeights = RuntimeBlockWeights; - type BlockLength = (); - type DbWeight = 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; - type Header = generic::Header; - type RuntimeEvent = RuntimeEvent; - type BlockHashCount = BlockHashCount; - type Version = (); - type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = SS58Prefix; - type OnSetCode = (); - type MaxConsumers = frame_support::traits::ConstU32<16>; -} - -impl pallet_gear_program::Config for Test { - type Scheduler = GearScheduler; - type CurrentBlockNumber = (); } parameter_types! { @@ -206,84 +164,6 @@ parameter_types! { pub const GasMultiplier: common::GasMultiplier = common::GasMultiplier::ValuePerGas(25); } -impl pallet_gear_bank::Config for Test { - type Currency = Balances; - type BankAddress = BankAddress; - type GasMultiplier = GasMultiplier; -} - -impl pallet_gear::Config for Test { - type RuntimeEvent = RuntimeEvent; - type Randomness = TestRandomness; - type WeightInfo = pallet_gear::weights::SubstrateWeight; - type Schedule = DynamicSchedule; - type OutgoingLimit = OutgoingLimit; - type PerformanceMultiplier = PerformanceMultiplier; - type DebugInfo = (); - type CodeStorage = GearProgram; - type ProgramStorage = GearProgram; - type MailboxThreshold = ConstU64<3000>; - type ReservationsLimit = ConstU64<256>; - type Messenger = GearMessenger; - type GasProvider = GearGas; - type BlockLimiter = GearGas; - type Scheduler = GearScheduler; - type QueueRunner = Gear; - type Voucher = GearVoucher; - type ProgramRentFreePeriod = RentFreePeriod; - type ProgramResumeMinimalRentPeriod = ResumeMinimalPeriod; - type ProgramRentCostPerBlock = RentCostPerBlock; - type ProgramResumeSessionDuration = ResumeSessionDuration; - type ProgramRentEnabled = ConstBool; - type ProgramRentDisabledDelta = RentFreePeriod; -} - -impl pallet_gear_scheduler::Config for Test { - type BlockLimiter = GearGas; - type ReserveThreshold = ReserveThreshold; - type WaitlistCost = ConstU64<100>; - type MailboxCost = ConstU64<100>; - type ReservationCost = ConstU64<100>; - type DispatchHoldCost = ConstU64<100>; -} - -impl pallet_gear_gas::Config for Test { - type BlockGasLimit = BlockGasLimit; -} - -impl pallet_gear_messenger::Config for Test { - type BlockLimiter = GearGas; - type CurrentBlockNumber = Gear; -} - -pub struct FixedBlockAuthor; - -impl FindAuthor for FixedBlockAuthor { - fn find_author<'a, I>(_digests: I) -> Option - where - I: 'a + IntoIterator, - { - Some(BLOCK_AUTHOR) - } -} - -impl pallet_authorship::Config for Test { - type FindAuthor = FixedBlockAuthor; - - type EventHandler = (); -} - -parameter_types! { - pub const MinimumPeriod: u64 = 500; -} - -impl pallet_timestamp::Config for Test { - type Moment = u64; - type OnTimestampSet = (); - type MinimumPeriod = MinimumPeriod; - type WeightInfo = (); -} - parameter_types! { pub const VoucherPalletId: PalletId = PalletId(*b"py/vouch"); } diff --git a/pallets/gear/src/pallet_tests.rs b/pallets/gear/src/pallet_tests.rs new file mode 100644 index 00000000000..7cd98cb72c7 --- /dev/null +++ b/pallets/gear/src/pallet_tests.rs @@ -0,0 +1,92 @@ +// This file is part of Gear. + +// Copyright (C) 2023 Gear Technologies Inc. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#[macro_export] +macro_rules! impl_config { + ($( $tokens:tt )*) => { + #[allow(dead_code)] + type GearConfigDebugInfo = (); + #[allow(dead_code)] + type GearConfigVoucher = (); + #[allow(dead_code)] + type GearConfigProgramRentEnabled = ConstBool; + #[allow(dead_code)] + type GearConfigSchedule = (); + + mod pallet_tests_gear_config_impl { + use super::*; + + $crate::impl_config_inner!($( $tokens )*); + } + }; +} + +#[macro_export] +macro_rules! impl_config_inner { + ($runtime:ty$(,)?) => { + impl pallet_gear::Config for $runtime { + type RuntimeEvent = RuntimeEvent; + type Randomness = TestRandomness; + type WeightInfo = pallet_gear::weights::SubstrateWeight; + type Schedule = GearConfigSchedule; + type OutgoingLimit = OutgoingLimit; + type PerformanceMultiplier = PerformanceMultiplier; + type DebugInfo = GearConfigDebugInfo; + type CodeStorage = GearProgram; + type ProgramStorage = GearProgram; + type MailboxThreshold = ConstU64<3_000>; + type ReservationsLimit = ConstU64<256>; + type Messenger = GearMessenger; + type GasProvider = GearGas; + type BlockLimiter = GearGas; + type Scheduler = GearScheduler; + type QueueRunner = Gear; + type Voucher = GearConfigVoucher; + type ProgramRentFreePeriod = RentFreePeriod; + type ProgramResumeMinimalRentPeriod = ResumeMinimalPeriod; + type ProgramRentCostPerBlock = RentCostPerBlock; + type ProgramResumeSessionDuration = ResumeSessionDuration; + type ProgramRentEnabled = GearConfigProgramRentEnabled; + type ProgramRentDisabledDelta = RentFreePeriod; + } + }; + + ($runtime:ty, Schedule = $schedule:ty $(, $( $rest:tt )*)?) => { + type GearConfigSchedule = $schedule; + + $crate::impl_config_inner!($runtime, $($( $rest )*)?); + }; + + ($runtime:ty, Voucher = $voucher:ty $(, $( $rest:tt )*)?) => { + type GearConfigVoucher = $voucher; + + $crate::impl_config_inner!($runtime, $($( $rest )*)?); + }; + + ($runtime:ty, DebugInfo = $debug_info:ty $(, $( $rest:tt )*)?) => { + type GearConfigDebugInfo = $debug_info; + + $crate::impl_config_inner!($runtime, $($( $rest )*)?); + }; + + ($runtime:ty, ProgramRentEnabled = $program_rent_enabled:ty $(, $( $rest:tt )*)?) => { + type GearConfigProgramRentEnabled = $program_rent_enabled; + + $crate::impl_config_inner!($runtime, $($( $rest )*)?); + }; +} diff --git a/pallets/payment/Cargo.toml b/pallets/payment/Cargo.toml index 84d09e5244c..48a2772ecbf 100644 --- a/pallets/payment/Cargo.toml +++ b/pallets/payment/Cargo.toml @@ -34,6 +34,7 @@ pallet-transaction-payment.workspace = true [dev-dependencies] serde.workspace = true env_logger.workspace = true +common = { workspace = true, features = ["std"] } wabt.workspace = true gear-core.workspace = true sp-io = { workspace = true, features = ["std"] } diff --git a/pallets/payment/src/mock.rs b/pallets/payment/src/mock.rs index 8b8b85077fd..b989c655a8d 100644 --- a/pallets/payment/src/mock.rs +++ b/pallets/payment/src/mock.rs @@ -31,7 +31,8 @@ use frame_system as system; use pallet_transaction_payment::CurrencyAdapter; use primitive_types::H256; use sp_runtime::{ - testing::{Header, TestXt}, + generic, + testing::TestXt, traits::{BlakeTwo256, ConstBool, ConstU64, IdentityLookup}, }; use sp_std::{ @@ -74,80 +75,24 @@ construct_runtime!( } ); -impl pallet_balances::Config for Test { - type MaxLocks = (); - type MaxReserves = (); - type ReserveIdentifier = [u8; 8]; - type Balance = Balance; - type DustRemoval = (); - type RuntimeEvent = RuntimeEvent; - type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; - type WeightInfo = (); -} - -pub struct FixedBlockAuthor; - -impl FindAuthor for FixedBlockAuthor { - fn find_author<'a, I>(_digests: I) -> Option - where - I: 'a + IntoIterator, - { - Some(BLOCK_AUTHOR) - } -} - -impl pallet_authorship::Config for Test { - type FindAuthor = FixedBlockAuthor; - - type EventHandler = (); -} +common::impl_pallet_system!(Test, DbWeight = (), BlockWeights = RuntimeBlockWeights); +pallet_gear::impl_config!(Test, Schedule = GearSchedule); +pallet_gear_gas::impl_config!(Test); +common::impl_pallet_balances!(Test); +common::impl_pallet_authorship!(Test); +common::impl_pallet_timestamp!(Test); +pallet_gear_messenger::impl_config!(Test, CurrentBlockNumber = Gear); +pallet_gear_scheduler::impl_config!(Test); +pallet_gear_program::impl_config!(Test); +pallet_gear_bank::impl_config!(Test); parameter_types! { - pub const MinimumPeriod: u64 = 500; -} - -impl pallet_timestamp::Config for Test { - type Moment = u64; - type OnTimestampSet = (); - type MinimumPeriod = MinimumPeriod; - type WeightInfo = (); -} - -parameter_types! { - pub const BlockHashCount: u64 = 2400; - pub const SS58Prefix: u8 = 42; + pub const BlockHashCount: BlockNumber = 2_400; pub const ExistentialDeposit: Balance = 1; pub RuntimeBlockWeights: frame_system::limits::BlockWeights = frame_system::limits::BlockWeights::simple_max( Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND / 2, u64::MAX) ); -} - -impl system::Config for Test { - type BaseCallFilter = frame_support::traits::Everything; - type BlockWeights = RuntimeBlockWeights; - type BlockLength = (); - 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; - type Header = Header; - type RuntimeEvent = RuntimeEvent; - type BlockHashCount = BlockHashCount; - type Version = (); - type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = SS58Prefix; - type OnSetCode = (); - type MaxConsumers = frame_support::traits::ConstU32<16>; + pub ReserveThreshold: BlockNumber = 1; } parameter_types! { @@ -177,61 +122,6 @@ parameter_types! { pub const GasMultiplier: common::GasMultiplier = common::GasMultiplier::ValuePerGas(25); } -impl pallet_gear_bank::Config for Test { - type Currency = Balances; - type BankAddress = BankAddress; - type GasMultiplier = GasMultiplier; -} - -impl pallet_gear::Config for Test { - type RuntimeEvent = RuntimeEvent; - type Randomness = TestRandomness; - type WeightInfo = (); - type Schedule = GearSchedule; - type OutgoingLimit = OutgoingLimit; - type PerformanceMultiplier = PerformanceMultiplier; - type DebugInfo = (); - type CodeStorage = GearProgram; - type ProgramStorage = GearProgram; - type MailboxThreshold = ConstU64<3000>; - type ReservationsLimit = ConstU64<256>; - type Messenger = GearMessenger; - type GasProvider = GearGas; - type BlockLimiter = GearGas; - type Scheduler = GearScheduler; - type QueueRunner = Gear; - type Voucher = (); - type ProgramRentFreePeriod = RentFreePeriod; - type ProgramResumeMinimalRentPeriod = ResumeMinimalPeriod; - type ProgramRentCostPerBlock = RentCostPerBlock; - type ProgramResumeSessionDuration = ResumeSessionDuration; - type ProgramRentEnabled = ConstBool; - type ProgramRentDisabledDelta = RentFreePeriod; -} - -impl pallet_gear_program::Config for Test { - type Scheduler = GearScheduler; - type CurrentBlockNumber = (); -} - -impl pallet_gear_gas::Config for Test { - type BlockGasLimit = BlockGasLimit; -} - -impl pallet_gear_scheduler::Config for Test { - type BlockLimiter = GearGas; - type ReserveThreshold = ConstU64<1>; - type WaitlistCost = ConstU64<100>; - type MailboxCost = ConstU64<100>; - type ReservationCost = ConstU64<100>; - type DispatchHoldCost = ConstU64<100>; -} - -impl pallet_gear_messenger::Config for Test { - type BlockLimiter = GearGas; - type CurrentBlockNumber = Gear; -} - type NegativeImbalance = >::NegativeImbalance; pub struct DealWithFees; diff --git a/pallets/staking-rewards/Cargo.toml b/pallets/staking-rewards/Cargo.toml index 3dd27074128..4a78a67f8b0 100644 --- a/pallets/staking-rewards/Cargo.toml +++ b/pallets/staking-rewards/Cargo.toml @@ -45,6 +45,7 @@ pallet-utility = { workspace = true, features = ["std"] } pallet-election-provider-multi-phase = { workspace = true, features = ["std"] } frame-executive = { workspace = true, features = ["std"] } env_logger.workspace = true +common = { workspace = true, features = ["std"] } [features] default = ["std"] diff --git a/pallets/staking-rewards/src/mock.rs b/pallets/staking-rewards/src/mock.rs index e5ca6aad641..7568b19e9ff 100644 --- a/pallets/staking-rewards/src/mock.rs +++ b/pallets/staking-rewards/src/mock.rs @@ -23,8 +23,8 @@ use frame_election_provider_support::{ use frame_support::{ construct_runtime, parameter_types, traits::{ - ConstU32, Contains, Currency, Everything, FindAuthor, GenesisBuild, NeverEnsureOrigin, - OnFinalize, OnInitialize, U128CurrencyToVote, + ConstU32, Contains, Currency, FindAuthor, GenesisBuild, NeverEnsureOrigin, OnFinalize, + OnInitialize, U128CurrencyToVote, }, weights::{constants::RocksDbWeight, Weight}, PalletId, @@ -34,7 +34,8 @@ use pallet_election_provider_multi_phase::{self as multi_phase}; use pallet_session::historical::{self as pallet_session_historical}; use sp_core::{crypto::key_types, H256}; use sp_runtime::{ - testing::{Block as TestBlock, Header, UintAuthorityId}, + generic, + testing::{Block as TestBlock, UintAuthorityId}, traits::{BlakeTwo256, IdentityLookup, OpaqueKeys}, KeyTypeId, Perbill, Permill, Perquintill, }; @@ -58,6 +59,7 @@ pub(crate) type Executive = frame_executive::Executive< pub(crate) const SIGNER: AccountId = 1; pub(crate) const VAL_1_STASH: AccountId = 10; +pub(crate) const BLOCK_AUTHOR: AccountId = VAL_1_STASH; pub(crate) const VAL_1_CONTROLLER: AccountId = 11; pub(crate) const VAL_1_AUTH_ID: UintAuthorityId = UintAuthorityId(12); pub(crate) const VAL_2_STASH: AccountId = 20; @@ -103,79 +105,16 @@ construct_runtime!( } ); -impl pallet_balances::Config for Test { - type MaxLocks = (); - type MaxReserves = (); - type ReserveIdentifier = [u8; 8]; - type Balance = Balance; - type DustRemoval = (); - type RuntimeEvent = RuntimeEvent; - type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; - type WeightInfo = (); -} +common::impl_pallet_system!(Test, DbWeight = RocksDbWeight, BlockWeights = ()); +common::impl_pallet_balances!(Test); +common::impl_pallet_authorship!(Test, EventHandler = Staking); +common::impl_pallet_timestamp!(Test); parameter_types! { - pub const BlockHashCount: u64 = 250; - pub const SS58Prefix: u8 = 42; + pub const BlockHashCount: BlockNumber = 250; pub const ExistentialDeposit: Balance = EXISTENTIAL_DEPOSIT; } -impl system::Config for Test { - type BaseCallFilter = Everything; - type BlockWeights = (); - type BlockLength = (); - type DbWeight = RocksDbWeight; - type RuntimeOrigin = RuntimeOrigin; - type RuntimeCall = RuntimeCall; - type Index = u64; - type BlockNumber = BlockNumber; - type Hash = H256; - type Hashing = BlakeTwo256; - type AccountId = AccountId; - type Lookup = IdentityLookup; - type Header = Header; - type RuntimeEvent = RuntimeEvent; - type BlockHashCount = BlockHashCount; - type Version = (); - type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = SS58Prefix; - type OnSetCode = (); - type MaxConsumers = ConstU32<16>; -} - -pub struct FixedBlockAuthor; - -impl FindAuthor for FixedBlockAuthor { - fn find_author<'a, I>(_digests: I) -> Option - where - I: 'a + IntoIterator, - { - Some(VAL_1_STASH) - } -} - -impl pallet_authorship::Config for Test { - type FindAuthor = FixedBlockAuthor; - - type EventHandler = Staking; -} - -parameter_types! { - pub const MinimumPeriod: u64 = 500; -} - -impl pallet_timestamp::Config for Test { - type Moment = u64; - type OnTimestampSet = (); - type MinimumPeriod = MinimumPeriod; - type WeightInfo = (); -} - impl pallet_sudo::Config for Test { type RuntimeEvent = RuntimeEvent; type RuntimeCall = RuntimeCall; From 35c1234daacee69133f8dcfc5b6f32beddfedfb5 Mon Sep 17 00:00:00 2001 From: Dmitry Novikov Date: Wed, 8 Nov 2023 13:13:01 +0400 Subject: [PATCH 2/2] fix(gtest): Run delayed dispatches in `externalities` context (#3481) --- gtest/src/manager.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gtest/src/manager.rs b/gtest/src/manager.rs index 018a1b5bbcf..60b09973bb5 100644 --- a/gtest/src/manager.rs +++ b/gtest/src/manager.rs @@ -313,7 +313,7 @@ impl ExtManager { .map(|dispatches| { dispatches .into_iter() - .map(|dispatch| self.run_dispatch(dispatch)) + .map(|dispatch| self.with_externalities(|this| this.run_dispatch(dispatch))) .collect() }) .unwrap_or_default()