difftreelog
feat add test pallets mechanism
in: master
14 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -5345,6 +5345,7 @@
"pallet-structure",
"pallet-sudo",
"pallet-template-transaction-payment",
+ "pallet-test-utils",
"pallet-timestamp",
"pallet-transaction-payment",
"pallet-transaction-payment-rpc-runtime-api",
@@ -6669,6 +6670,16 @@
]
[[package]]
+name = "pallet-test-utils"
+version = "0.1.0"
+dependencies = [
+ "frame-support",
+ "frame-system",
+ "parity-scale-codec 3.1.5",
+ "scale-info",
+]
+
+[[package]]
name = "pallet-timestamp"
version = "4.0.0-dev"
source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.30#a3ed0119c45cdd0d571ad34e5b3ee7518c8cef8d"
@@ -8793,6 +8804,7 @@
"pallet-structure",
"pallet-sudo",
"pallet-template-transaction-payment",
+ "pallet-test-utils",
"pallet-timestamp",
"pallet-transaction-payment",
"pallet-transaction-payment-rpc-runtime-api",
@@ -12866,6 +12878,7 @@
"pallet-structure",
"pallet-sudo",
"pallet-template-transaction-payment",
+ "pallet-test-utils",
"pallet-timestamp",
"pallet-transaction-payment",
"pallet-transaction-payment-rpc-runtime-api",
Cargo.tomldiffbeforeafterboth--- a/Cargo.toml
+++ b/Cargo.toml
@@ -14,6 +14,7 @@
'runtime/tests',
]
default-members = ['node/*', 'runtime/opal']
+
[profile.release]
panic = 'unwind'
node/cli/Cargo.tomldiffbeforeafterboth--- a/node/cli/Cargo.toml
+++ b/node/cli/Cargo.toml
@@ -335,3 +335,8 @@
'quartz-runtime?/try-runtime',
'opal-runtime?/try-runtime',
]
+
+test-pallets = [
+ 'unique-runtime?/test-pallets',
+ 'quartz-runtime?/test-pallets',
+]
runtime/common/config/mod.rsdiffbeforeafterboth--- a/runtime/common/config/mod.rs
+++ b/runtime/common/config/mod.rs
@@ -21,3 +21,6 @@
pub mod sponsoring;
pub mod substrate;
pub mod xcm;
+
+#[cfg(feature = "test-pallets")]
+pub mod test_pallets;
runtime/common/config/test_pallets.rsdiffbeforeafterboth--- /dev/null
+++ b/runtime/common/config/test_pallets.rs
@@ -0,0 +1,21 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network 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.
+
+// Unique Network 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 Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+use crate::{Runtime, Event};
+
+impl pallet_test_utils::Config for Runtime {
+ type Event = Event;
+}
runtime/common/construct_runtime/mod.rsdiffbeforeafterboth--- a/runtime/common/construct_runtime/mod.rs
+++ b/runtime/common/construct_runtime/mod.rs
@@ -93,6 +93,11 @@
EvmContractHelpers: pallet_evm_contract_helpers::{Pallet, Storage, Event<T>} = 151,
EvmTransactionPayment: pallet_evm_transaction_payment::{Pallet} = 152,
EvmMigration: pallet_evm_migration::{Pallet, Call, Storage} = 153,
+
+ #[test_pallets]
+ {
+ TestUtils: pallet_test_utils,
+ }
}
}
}
runtime/common/construct_runtime/util.rsdiffbeforeafterboth--- a/runtime/common/construct_runtime/util.rs
+++ b/runtime/common/construct_runtime/util.rs
@@ -27,11 +27,24 @@
$pallet_name:ident: $pallet_mod:ident$(::{$($pallet_parts:ty),*})? = $index:literal
),*
$(,)?
+
+ #[test_pallets]
+ {
+ $(
+ $test_pallet_name:ident: $test_pallet_mod:ident
+ ),*
+ $(,)?
+ }
}
) => {
$crate::construct_runtime_helper! {
select_runtime($select_runtime),
selected_pallets(),
+ test_pallets(
+ $(
+ $test_pallet_name: $test_pallet_mod
+ ),*
+ ),
where_clause($($where_ident = $where_ty),*),
pallets(
@@ -49,6 +62,7 @@
(
select_runtime($select_runtime:ident),
selected_pallets($($selected_pallets:tt)*),
+ test_pallets($($test_pallets:tt)*),
where_clause($($where_clause:tt)*),
pallets(
@@ -62,6 +76,7 @@
select_runtime($select_runtime),
runtimes($($pallet_runtimes),+,),
selected_pallets($($selected_pallets)*),
+ test_pallets($($test_pallets)*),
where_clause($($where_clause)*),
pallets(
@@ -74,6 +89,7 @@
(
select_runtime($select_runtime:ident),
selected_pallets($($selected_pallets:tt)*),
+ test_pallets($($test_pallets:tt)*),
where_clause($($where_clause:tt)*),
pallets(
@@ -88,6 +104,7 @@
$($selected_pallets)*
$pallet_name: $pallet_mod$(::{$($pallet_parts),*})? = $index,
),
+ test_pallets($($test_pallets)*),
where_clause($($where_clause)*),
pallets($($pallets_tl)*)
@@ -97,10 +114,12 @@
(
select_runtime($select_runtime:ident),
selected_pallets($($selected_pallets:tt)*),
+ test_pallets($($test_pallets:tt)*),
where_clause($($where_clause:tt)*),
pallets()
) => {
+ #[cfg(not(feature = "test-pallets"))]
frame_support::construct_runtime! {
pub enum Runtime where
$($where_clause)*
@@ -108,6 +127,16 @@
$($selected_pallets)*
}
}
+
+ #[cfg(feature = "test-pallets")]
+ frame_support::construct_runtime! {
+ pub enum Runtime where
+ $($where_clause)*
+ {
+ $($selected_pallets)*
+ $($test_pallets)*
+ }
+ }
};
}
@@ -117,6 +146,7 @@
select_runtime(opal),
runtimes(opal, $($_runtime_tl:tt)*),
selected_pallets($($selected_pallets:tt)*),
+ test_pallets($($test_pallets:tt)*),
where_clause($($where_clause:tt)*),
pallets(
@@ -130,6 +160,7 @@
$($selected_pallets)*
$pallet_name: $pallet_mod$(::{$($pallet_parts),*})? = $index,
),
+ test_pallets($($test_pallets)*),
where_clause($($where_clause)*),
pallets($($pallets_tl)*)
@@ -140,6 +171,7 @@
select_runtime(quartz),
runtimes(quartz, $($_runtime_tl:tt)*),
selected_pallets($($selected_pallets:tt)*),
+ test_pallets($($test_pallets:tt)*),
where_clause($($where_clause:tt)*),
pallets(
@@ -153,6 +185,7 @@
$($selected_pallets)*
$pallet_name: $pallet_mod$(::{$($pallet_parts),*})? = $index,
),
+ test_pallets($($test_pallets)*),
where_clause($($where_clause)*),
pallets($($pallets_tl)*)
@@ -163,6 +196,7 @@
select_runtime(unique),
runtimes(unique, $($_runtime_tl:tt)*),
selected_pallets($($selected_pallets:tt)*),
+ test_pallets($($test_pallets:tt)*),
where_clause($($where_clause:tt)*),
pallets(
@@ -176,6 +210,7 @@
$($selected_pallets)*
$pallet_name: $pallet_mod$(::{$($pallet_parts),*})? = $index,
),
+ test_pallets($($test_pallets)*),
where_clause($($where_clause)*),
pallets($($pallets_tl)*)
@@ -186,6 +221,7 @@
select_runtime($select_runtime:ident),
runtimes($_current_runtime:ident, $($runtime_tl:tt)*),
selected_pallets($($selected_pallets:tt)*),
+ test_pallets($($test_pallets:tt)*),
where_clause($($where_clause:tt)*),
pallets($($pallets:tt)*)
@@ -194,6 +230,7 @@
select_runtime($select_runtime),
runtimes($($runtime_tl)*),
selected_pallets($($selected_pallets)*),
+ test_pallets($($test_pallets)*),
where_clause($($where_clause)*),
pallets($($pallets)*)
@@ -204,6 +241,7 @@
select_runtime($select_runtime:ident),
runtimes(),
selected_pallets($($selected_pallets:tt)*),
+ test_pallets($($test_pallets:tt)*),
where_clause($($where_clause:tt)*),
pallets(
@@ -214,6 +252,7 @@
$crate::construct_runtime_helper! {
select_runtime($select_runtime),
selected_pallets($($selected_pallets)*),
+ test_pallets($($test_pallets)*),
where_clause($($where_clause)*),
pallets($($pallets_tl)*)
runtime/opal/Cargo.tomldiffbeforeafterboth--- a/runtime/opal/Cargo.toml
+++ b/runtime/opal/Cargo.toml
@@ -167,7 +167,10 @@
"orml-xtokens/std",
"orml-traits/std",
"pallet-foreign-assets/std",
+
+ 'pallet-test-utils?/std',
]
+test-pallets = ['pallet-test-utils']
limit-testing = ['pallet-unique/limit-testing', 'up-data-structs/limit-testing']
opal-runtime = ['refungible', 'rmrk', 'app-promotion', 'foreign-assets']
@@ -495,6 +498,11 @@
version = "2.0.0"
################################################################################
+# Test dependencies
+pallet-test-utils = { optional = true, default-features = false, path = "../../test-pallets/utils" }
+
+
+################################################################################
# Build Dependencies
[build-dependencies.substrate-wasm-builder]
runtime/quartz/Cargo.tomldiffbeforeafterboth--- a/runtime/quartz/Cargo.toml
+++ b/runtime/quartz/Cargo.toml
@@ -165,7 +165,10 @@
"orml-xtokens/std",
"orml-traits/std",
"pallet-foreign-assets/std",
+
+ 'pallet-test-utils?/std',
]
+test-pallets = ['pallet-test-utils']
limit-testing = ['pallet-unique/limit-testing', 'up-data-structs/limit-testing']
quartz-runtime = ['refungible']
@@ -500,6 +503,10 @@
version = "2.0.0"
################################################################################
+# Test dependencies
+pallet-test-utils = { optional = true, default-features = false, path = "../../test-pallets/utils" }
+
+################################################################################
# Build Dependencies
[build-dependencies.substrate-wasm-builder]
runtime/unique/Cargo.tomldiffbeforeafterboth--- a/runtime/unique/Cargo.toml
+++ b/runtime/unique/Cargo.toml
@@ -166,7 +166,10 @@
"orml-xtokens/std",
"orml-traits/std",
"pallet-foreign-assets/std",
+
+ 'pallet-test-utils?/std',
]
+test-pallets = ['pallet-test-utils']
limit-testing = ['pallet-unique/limit-testing', 'up-data-structs/limit-testing']
unique-runtime = []
@@ -494,6 +497,10 @@
version = "2.0.0"
################################################################################
+# Test dependencies
+pallet-test-utils = { optional = true, default-features = false, path = "../../test-pallets/utils" }
+
+################################################################################
# Build Dependencies
[build-dependencies.substrate-wasm-builder]
test-pallets/utils/Cargo.lockdiffbeforeafterbothno changes
test-pallets/utils/Cargo.tomldiffbeforeafterboth--- /dev/null
+++ b/test-pallets/utils/Cargo.toml
@@ -0,0 +1,21 @@
+[package]
+name = "pallet-test-utils"
+version = "0.1.0"
+license = "GPLv3"
+edition = "2021"
+publish = false
+
+[dependencies]
+codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
+scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
+frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" }
+frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" }
+
+[features]
+default = ["std"]
+std = [
+ "codec/std",
+ "scale-info/std",
+ "frame-support/std",
+ "frame-system/std",
+]
test-pallets/utils/src/lib.rsdiffbeforeafterboth--- /dev/null
+++ b/test-pallets/utils/src/lib.rs
@@ -0,0 +1,73 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network 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.
+
+// Unique Network 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 Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+#![cfg_attr(not(feature = "std"), no_std)]
+
+pub use pallet::*;
+
+#[frame_support::pallet]
+pub mod pallet {
+ use frame_support::pallet_prelude::*;
+ use frame_system::pallet_prelude::*;
+
+ #[pallet::config]
+ pub trait Config: frame_system::Config {
+ type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
+ }
+
+ #[pallet::event]
+ #[pallet::generate_deposit(pub(super) fn deposit_event)]
+ pub enum Event<T: Config> {
+ ValueIsSet,
+ ShouldRollback,
+ }
+
+ #[pallet::pallet]
+ #[pallet::generate_store(pub(super) trait Store)]
+ pub struct Pallet<T>(_);
+
+ #[pallet::storage]
+ #[pallet::getter(fn something)]
+ pub type TestValue<T> = StorageValue<_, u32, ValueQuery>;
+
+ #[pallet::error]
+ pub enum Error<T> {
+ TriggerRollback
+ }
+
+ #[pallet::call]
+ impl<T: Config> Pallet<T> {
+ #[pallet::weight(10_000)]
+ pub fn set_test_value(origin: OriginFor<T>, value: u32) -> DispatchResult {
+ ensure_signed(origin)?;
+
+ <TestValue<T>>::put(value);
+
+ Self::deposit_event(Event::ValueIsSet);
+
+ Ok(())
+ }
+
+ #[pallet::weight(10_000)]
+ pub fn set_test_value_and_rollback(origin: OriginFor<T>, value: u32) -> DispatchResult {
+ Self::set_test_value(origin, value)?;
+
+ Self::deposit_event(Event::ShouldRollback);
+
+ Err(<Error<T>>::TriggerRollback.into())
+ }
+ }
+}
tests/src/pallet-presence.test.tsdiffbeforeafterboth--- a/tests/src/pallet-presence.test.ts
+++ b/tests/src/pallet-presence.test.ts
@@ -49,6 +49,10 @@
'xtokens',
];
+const testPallets = [
+ 'testutils',
+];
+
// Pallets that depend on consensus and governance configuration
const consensusPallets = [
'sudo',
@@ -87,6 +91,10 @@
expect(helper.fetchAllPalletNames()).to.contain.members([...requiredPallets]);
});
+ itSub('Test pallets are present', async ({helper}) => {
+ expect(helper.fetchAllPalletNames()).to.contain.members([...testPallets]);
+ });
+
itSub('Governance and consensus pallets are present', async ({helper}) => {
expect(helper.fetchAllPalletNames()).to.contain.members([...consensusPallets]);
});