diff --git a/frame/support/procedural/src/lib.rs b/frame/support/procedural/src/lib.rs index 009e02f3c265b..a6fb58846cbad 100644 --- a/frame/support/procedural/src/lib.rs +++ b/frame/support/procedural/src/lib.rs @@ -21,6 +21,7 @@ mod storage; mod construct_runtime; +mod pallet_version; mod transactional; mod debug_no_bound; mod clone_no_bound; @@ -402,3 +403,8 @@ pub fn derive_eq_no_bound(input: TokenStream) -> TokenStream { pub fn require_transactional(attr: TokenStream, input: TokenStream) -> TokenStream { transactional::require_transactional(attr, input).unwrap_or_else(|e| e.to_compile_error().into()) } + +#[proc_macro] +pub fn crate_to_pallet_version(input: TokenStream) -> TokenStream { + pallet_version::crate_to_pallet_version(input).unwrap_or_else(|e| e.to_compile_error()).into() +} diff --git a/frame/support/procedural/src/pallet_version.rs b/frame/support/procedural/src/pallet_version.rs new file mode 100644 index 0000000000000..ffd4b41208d56 --- /dev/null +++ b/frame/support/procedural/src/pallet_version.rs @@ -0,0 +1,64 @@ +// This file is part of Substrate. + +// Copyright (C) 2020 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Implementation of macros related to pallet versioning. + +use proc_macro2::{TokenStream, Span}; +use syn::{Result, Error}; +use std::{env, str::FromStr}; +use frame_support_procedural_tools::generate_crate_access_2018; + +/// Get the version from the given version environment variable. +/// +/// The version is parsed into the requested destination type. +fn get_version(version_env: &str) -> std::result::Result { + let version = env::var(version_env) + .expect(&format!("`{}` is always set by cargo; qed", version_env)); + + T::from_str(&version).map_err(drop) +} + +/// Create an error that will be shown by rustc at the call site of the macro. +fn create_error(message: &str) -> Error { + Error::new(Span::call_site(), message) +} + +/// Implementation of the `crate_to_pallet_version!` macro. +pub fn crate_to_pallet_version(input: proc_macro::TokenStream) -> Result { + if !input.is_empty() { + return Err(create_error("No arguments expected!")) + } + + let major_version = get_version::("CARGO_PKG_VERSION_MAJOR") + .map_err(|_| create_error("Major version needs to fit into `u16`"))?; + + let minor_version = get_version::("CARGO_PKG_VERSION_MINOR") + .map_err(|_| create_error("Minor version needs to fit into `u8`"))?; + + let patch_version = get_version::("CARGO_PKG_VERSION_PATCH") + .map_err(|_| create_error("Patch version needs to fit into `u8`"))?; + + let crate_ = generate_crate_access_2018()?; + + Ok(quote::quote! { + #crate_::traits::PalletVersion { + major: #major_version, + minor: #minor_version, + patch: #patch_version, + } + }) +} diff --git a/frame/support/src/dispatch.rs b/frame/support/src/dispatch.rs index 057bba6b8f74e..b96d6194ebffe 100644 --- a/frame/support/src/dispatch.rs +++ b/frame/support/src/dispatch.rs @@ -29,7 +29,9 @@ pub use crate::weights::{ PaysFee, PostDispatchInfo, WithPostDispatchInfo, }; pub use sp_runtime::{traits::Dispatchable, DispatchError}; -pub use crate::traits::{CallMetadata, GetCallMetadata, GetCallName, UnfilteredDispatchable}; +pub use crate::traits::{ + CallMetadata, GetCallMetadata, GetCallName, UnfilteredDispatchable, GetPalletVersion, +}; /// The return typ of a `Dispatchable` in frame. When returned explicitly from /// a dispatchable function it allows overriding the default `PostDispatchInfo` @@ -230,11 +232,11 @@ impl Parameter for T where T: Codec + EncodeLike + Clone + Eq + fmt::Debug {} /// # #[macro_use] /// # extern crate frame_support; /// # use frame_support::dispatch; -/// # use frame_system::{self as system, ensure_signed}; +/// # use frame_system::ensure_signed; /// # pub struct DefaultInstance; -/// # pub trait Instance {} +/// # pub trait Instance: 'static {} /// # impl Instance for DefaultInstance {} -/// pub trait Trait: system::Trait {} +/// pub trait Trait: frame_system::Trait {} /// /// decl_module! { /// pub struct Module, I: Instance = DefaultInstance> for enum Call where origin: T::Origin { @@ -1310,6 +1312,7 @@ macro_rules! decl_module { }; (@impl_on_runtime_upgrade + { $system:ident } $module:ident<$trait_instance:ident: $trait_name:ident$(, $instance:ident: $instantiable:path)?>; { $( $other_where_bounds:tt )* } fn on_runtime_upgrade() -> $return:ty { $( $impl:tt )* } @@ -1320,19 +1323,46 @@ macro_rules! decl_module { { fn on_runtime_upgrade() -> $return { $crate::sp_tracing::enter_span!($crate::sp_tracing::trace_span!("on_runtime_upgrade")); - { $( $impl )* } + let result: $return = (|| { $( $impl )* })(); + + let key = $crate::traits::PalletVersion::storage_key::< + <$trait_instance as $system::Trait>::PalletInfo, Self + >().expect("Every active pallet has a name in the runtime; qed"); + let version = $crate::crate_to_pallet_version!(); + $crate::storage::unhashed::put(&key, &version); + + let additional_write = < + <$trait_instance as $system::Trait>::DbWeight as $crate::traits::Get<_> + >::get().writes(1); + + result.saturating_add(additional_write) } } }; (@impl_on_runtime_upgrade + { $system:ident } $module:ident<$trait_instance:ident: $trait_name:ident$(, $instance:ident: $instantiable:path)?>; { $( $other_where_bounds:tt )* } ) => { impl<$trait_instance: $trait_name$(, $instance: $instantiable)?> $crate::traits::OnRuntimeUpgrade for $module<$trait_instance$(, $instance)?> where $( $other_where_bounds )* - {} + { + fn on_runtime_upgrade() -> $crate::dispatch::Weight { + $crate::sp_tracing::enter_span!($crate::sp_tracing::trace_span!("on_runtime_upgrade")); + + let key = $crate::traits::PalletVersion::storage_key::< + <$trait_instance as $system::Trait>::PalletInfo, Self + >().expect("Every active pallet has a name in the runtime; qed"); + let version = $crate::crate_to_pallet_version!(); + $crate::storage::unhashed::put(&key, &version); + + < + <$trait_instance as $system::Trait>::DbWeight as $crate::traits::Get<_> + >::get().writes(1) + } + } }; (@impl_integrity_test @@ -1652,6 +1682,7 @@ macro_rules! decl_module { $crate::decl_module! { @impl_on_runtime_upgrade + { $system } $mod_type<$trait_instance: $trait_name $(, $instance: $instantiable)?>; { $( $other_where_bounds )* } $( $on_runtime_upgrade )* @@ -1787,6 +1818,25 @@ macro_rules! decl_module { } } + // Bring `GetPalletVersion` into scope to make it easily usable. + pub use $crate::traits::GetPalletVersion as _; + // Implement `GetPalletVersion` for `Module` + impl<$trait_instance: $trait_name $(, $instance: $instantiable)?> $crate::traits::GetPalletVersion + for $mod_type<$trait_instance $(, $instance)?> where $( $other_where_bounds )* + { + fn current_version() -> $crate::traits::PalletVersion { + $crate::crate_to_pallet_version!() + } + + fn storage_version() -> Option<$crate::traits::PalletVersion> { + let key = $crate::traits::PalletVersion::storage_key::< + <$trait_instance as $system::Trait>::PalletInfo, Self + >().expect("Every active pallet has a name in the runtime; qed"); + + $crate::storage::unhashed::get(&key) + } + } + // manual implementation of clone/eq/partialeq because using derive erroneously requires // clone/eq/partialeq from T. impl<$trait_instance: $trait_name $(, $instance: $instantiable)?> $crate::dispatch::Clone @@ -1802,6 +1852,7 @@ macro_rules! decl_module { } } } + impl<$trait_instance: $trait_name $(, $instance: $instantiable)?> $crate::dispatch::PartialEq for $call_type<$trait_instance $(, $instance)?> where $( $other_where_bounds )* { @@ -1824,6 +1875,7 @@ macro_rules! decl_module { } } } + impl<$trait_instance: $trait_name $(, $instance: $instantiable)?> $crate::dispatch::Eq for $call_type<$trait_instance $(, $instance)?> where $( $other_where_bounds )* {} @@ -2350,23 +2402,25 @@ macro_rules! __check_reserved_fn_name { #[allow(dead_code)] mod tests { use super::*; - use crate::weights::{DispatchInfo, DispatchClass, Pays}; + use crate::weights::{DispatchInfo, DispatchClass, Pays, RuntimeDbWeight}; use crate::traits::{ CallMetadata, GetCallMetadata, GetCallName, OnInitialize, OnFinalize, OnRuntimeUpgrade, - IntegrityTest, + IntegrityTest, Get, }; pub trait Trait: system::Trait + Sized where Self::AccountId: From { } pub mod system { - use codec::{Encode, Decode}; + use super::*; - pub trait Trait { + pub trait Trait: 'static { type AccountId; type Call; type BaseCallFilter; type Origin: crate::traits::OriginTrait; type BlockNumber: Into; + type PalletInfo: crate::traits::PalletInfo; + type DbWeight: Get; } #[derive(Clone, PartialEq, Eq, Debug, Encode, Decode)] @@ -2510,6 +2564,8 @@ mod tests { type Call = OuterCall; type BaseCallFilter = (); type BlockNumber = u32; + type PalletInfo = (); + type DbWeight = (); } #[test] @@ -2565,7 +2621,9 @@ mod tests { #[test] fn on_runtime_upgrade_should_work() { - assert_eq!( as OnRuntimeUpgrade>::on_runtime_upgrade(), 10); + sp_io::TestExternalities::default().execute_with(|| + assert_eq!( as OnRuntimeUpgrade>::on_runtime_upgrade(), 10) + ); } #[test] diff --git a/frame/support/src/event.rs b/frame/support/src/event.rs index 0f889f97f40a0..3538748c30faf 100644 --- a/frame/support/src/event.rs +++ b/frame/support/src/event.rs @@ -551,13 +551,15 @@ mod tests { use codec::{Encode, Decode}; mod system { - pub trait Trait { + pub trait Trait: 'static { type Origin; type BlockNumber; + type PalletInfo: crate::traits::PalletInfo; + type DbWeight: crate::traits::Get; } decl_module! { - pub struct Module for enum Call where origin: T::Origin {} + pub struct Module for enum Call where origin: T::Origin, system=self {} } decl_event!( @@ -568,13 +570,15 @@ mod tests { } mod system_renamed { - pub trait Trait { + pub trait Trait: 'static { type Origin; type BlockNumber; + type PalletInfo: crate::traits::PalletInfo; + type DbWeight: crate::traits::Get; } decl_module! { - pub struct Module for enum Call where origin: T::Origin {} + pub struct Module for enum Call where origin: T::Origin, system=self {} } decl_event!( @@ -585,19 +589,19 @@ mod tests { } mod event_module { - pub trait Trait { - type Origin; + use super::system; + + pub trait Trait: system::Trait { type Balance; - type BlockNumber; } decl_module! { - pub struct Module for enum Call where origin: T::Origin {} + pub struct Module for enum Call where origin: T::Origin, system=system {} } decl_event!( /// Event without renaming the generic parameter `Balance` and `Origin`. - pub enum Event where ::Balance, ::Origin + pub enum Event where ::Balance, ::Origin { /// Hi, I am a comment. TestEvent(Balance, Origin), @@ -608,21 +612,21 @@ mod tests { } mod event_module2 { - pub trait Trait { - type Origin; + use super::system; + + pub trait Trait: system::Trait { type Balance; - type BlockNumber; } decl_module! { - pub struct Module for enum Call where origin: T::Origin {} + pub struct Module for enum Call where origin: T::Origin, system=system {} } decl_event!( /// Event with renamed generic parameter pub enum Event where BalanceRenamed = ::Balance, - OriginRenamed = ::Origin + OriginRenamed = ::Origin { TestEvent(BalanceRenamed), TestOrigin(OriginRenamed), @@ -639,21 +643,21 @@ mod tests { } mod event_module4 { - pub trait Trait { - type Origin; + use super::system; + + pub trait Trait: system::Trait { type Balance; - type BlockNumber; } decl_module! { - pub struct Module for enum Call where origin: T::Origin {} + pub struct Module for enum Call where origin: T::Origin, system=system {} } decl_event!( /// Event finish formatting on an unnamed one with trailing comma pub enum Event where ::Balance, - ::Origin, + ::Origin, { TestEvent(Balance, Origin), } @@ -661,21 +665,21 @@ mod tests { } mod event_module5 { - pub trait Trait { - type Origin; + use super::system; + + pub trait Trait: system::Trait { type Balance; - type BlockNumber; } decl_module! { - pub struct Module for enum Call where origin: T::Origin {} + pub struct Module for enum Call where origin: T::Origin, system=system {} } decl_event!( /// Event finish formatting on an named one with trailing comma pub enum Event where BalanceRenamed = ::Balance, - OriginRenamed = ::Origin, + OriginRenamed = ::Origin, { TestEvent(BalanceRenamed, OriginRenamed), TrailingCommaInArgs( @@ -711,37 +715,40 @@ mod tests { } impl event_module::Trait for TestRuntime { - type Origin = u32; type Balance = u32; - type BlockNumber = u32; } impl event_module2::Trait for TestRuntime { - type Origin = u32; type Balance = u32; - type BlockNumber = u32; } impl system::Trait for TestRuntime { type Origin = u32; type BlockNumber = u32; + type PalletInfo = (); + type DbWeight = (); } impl event_module::Trait for TestRuntime2 { - type Origin = u32; type Balance = u32; - type BlockNumber = u32; } impl event_module2::Trait for TestRuntime2 { - type Origin = u32; type Balance = u32; - type BlockNumber = u32; } impl system_renamed::Trait for TestRuntime2 { type Origin = u32; type BlockNumber = u32; + type PalletInfo = (); + type DbWeight = (); + } + + impl system::Trait for TestRuntime2 { + type Origin = u32; + type BlockNumber = u32; + type PalletInfo = (); + type DbWeight = (); } const EXPECTED_METADATA: OuterEventMetadata = OuterEventMetadata { diff --git a/frame/support/src/lib.rs b/frame/support/src/lib.rs index 58fb3d031cf06..2380c8127d7af 100644 --- a/frame/support/src/lib.rs +++ b/frame/support/src/lib.rs @@ -375,6 +375,21 @@ pub use frame_support_procedural::DebugNoBound; /// ``` pub use frame_support_procedural::require_transactional; +/// Convert the current crate version into a [`PalletVersion`](crate::traits::PalletVersion). +/// +/// It uses the `CARGO_PKG_VERSION_MAJOR`, `CARGO_PKG_VERSION_MINOR` and +/// `CARGO_PKG_VERSION_PATCH` environment variables to fetch the crate version. +/// This means that the [`PalletVersion`](crate::traits::PalletVersion) +/// object will correspond to the version of the crate the macro is called in! +/// +/// # Example +/// +/// ``` +/// # use frame_support::{traits::PalletVersion, crate_to_pallet_version}; +/// const Version: PalletVersion = crate_to_pallet_version!(); +/// ``` +pub use frame_support_procedural::crate_to_pallet_version; + /// Return Err of the expression: `return Err($expression);`. /// /// Used as `fail!(expression)`. @@ -485,9 +500,11 @@ mod tests { use sp_std::{marker::PhantomData, result}; use sp_io::TestExternalities; - pub trait Trait { + pub trait Trait: 'static { type BlockNumber: Codec + EncodeLike + Default; type Origin; + type PalletInfo: crate::traits::PalletInfo; + type DbWeight: crate::traits::Get; } mod module { @@ -496,7 +513,7 @@ mod tests { use super::Trait; decl_module! { - pub struct Module for enum Call where origin: T::Origin {} + pub struct Module for enum Call where origin: T::Origin, system=self {} } } use self::module::Module; @@ -527,6 +544,8 @@ mod tests { impl Trait for Test { type BlockNumber = u32; type Origin = u32; + type PalletInfo = (); + type DbWeight = (); } fn new_test_ext() -> TestExternalities { diff --git a/frame/support/src/metadata.rs b/frame/support/src/metadata.rs index 9ae1d6ce663d5..80737e4b13d6f 100644 --- a/frame/support/src/metadata.rs +++ b/frame/support/src/metadata.rs @@ -27,12 +27,14 @@ pub use frame_metadata::{ /// Example: /// ``` ///# mod module0 { -///# pub trait Trait { +///# pub trait Trait: 'static { ///# type Origin; ///# type BlockNumber; +///# type PalletInfo: frame_support::traits::PalletInfo; +///# type DbWeight: frame_support::traits::Get; ///# } ///# frame_support::decl_module! { -///# pub struct Module for enum Call where origin: T::Origin {} +///# pub struct Module for enum Call where origin: T::Origin, system=self {} ///# } ///# ///# frame_support::decl_storage! { @@ -44,6 +46,8 @@ pub use frame_metadata::{ ///# impl module0::Trait for Runtime { ///# type Origin = u32; ///# type BlockNumber = u32; +///# type PalletInfo = (); +///# type DbWeight = (); ///# } ///# ///# type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic<(), (), (), ()>; @@ -302,11 +306,12 @@ mod tests { type BlockNumber: From + Encode; type SomeValue: Get; type PalletInfo: crate::traits::PalletInfo; + type DbWeight: crate::traits::Get; type Call; } decl_module! { - pub struct Module for enum Call where origin: T::Origin { + pub struct Module for enum Call where origin: T::Origin, system=self { /// Hi, I am a comment. const BlockNumber: T::BlockNumber = 100.into(); const GetType: T::AccountId = T::SomeValue::get().into(); @@ -341,8 +346,9 @@ mod tests { mod event_module { use crate::dispatch::DispatchResult; + use super::system; - pub trait Trait: super::system::Trait { + pub trait Trait: system::Trait { type Balance; } @@ -355,7 +361,7 @@ mod tests { ); decl_module! { - pub struct Module for enum Call where origin: T::Origin { + pub struct Module for enum Call where origin: T::Origin, system=system { type Error = Error; #[weight = 0] @@ -375,10 +381,10 @@ mod tests { } mod event_module2 { - pub trait Trait { - type Origin; + use super::system; + + pub trait Trait: system::Trait { type Balance; - type BlockNumber; } decl_event!( @@ -389,7 +395,7 @@ mod tests { ); decl_module! { - pub struct Module for enum Call where origin: T::Origin {} + pub struct Module for enum Call where origin: T::Origin, system=system {} } crate::decl_storage! { @@ -432,9 +438,7 @@ mod tests { } impl event_module2::Trait for TestRuntime { - type Origin = Origin; type Balance = u32; - type BlockNumber = u32; } crate::parameter_types! { @@ -448,6 +452,7 @@ mod tests { type BlockNumber = u32; type SomeValue = SystemValue; type PalletInfo = (); + type DbWeight = (); type Call = Call; } diff --git a/frame/support/src/storage/generator/double_map.rs b/frame/support/src/storage/generator/double_map.rs index 9454ab401da28..cbc62c83de886 100644 --- a/frame/support/src/storage/generator/double_map.rs +++ b/frame/support/src/storage/generator/double_map.rs @@ -425,13 +425,15 @@ mod test_iterators { storage::{generator::StorageDoubleMap, IterableStorageDoubleMap, unhashed}, }; - pub trait Trait { + pub trait Trait: 'static { type Origin; type BlockNumber; + type PalletInfo: crate::traits::PalletInfo; + type DbWeight: crate::traits::Get; } crate::decl_module! { - pub struct Module for enum Call where origin: T::Origin {} + pub struct Module for enum Call where origin: T::Origin, system=self {} } #[derive(PartialEq, Eq, Clone, Encode, Decode)] diff --git a/frame/support/src/storage/generator/map.rs b/frame/support/src/storage/generator/map.rs index 1c13de52e1640..601fd4c4a8dd2 100644 --- a/frame/support/src/storage/generator/map.rs +++ b/frame/support/src/storage/generator/map.rs @@ -325,13 +325,15 @@ mod test_iterators { storage::{generator::StorageMap, IterableStorageMap, unhashed}, }; - pub trait Trait { + pub trait Trait: 'static { type Origin; type BlockNumber; + type PalletInfo: crate::traits::PalletInfo; + type DbWeight: crate::traits::Get; } crate::decl_module! { - pub struct Module for enum Call where origin: T::Origin {} + pub struct Module for enum Call where origin: T::Origin, system=self {} } #[derive(PartialEq, Eq, Clone, Encode, Decode)] diff --git a/frame/support/src/storage/generator/mod.rs b/frame/support/src/storage/generator/mod.rs index 7df7dfd317399..9346718f63481 100644 --- a/frame/support/src/storage/generator/mod.rs +++ b/frame/support/src/storage/generator/mod.rs @@ -40,19 +40,24 @@ mod tests { use crate::storage::{unhashed, generator::StorageValue, IterableStorageMap}; use crate::{assert_noop, assert_ok}; - struct Runtime {} - pub trait Trait { + struct Runtime; + + pub trait Trait: 'static { type Origin; type BlockNumber; + type PalletInfo: crate::traits::PalletInfo; + type DbWeight: crate::traits::Get; } impl Trait for Runtime { type Origin = u32; type BlockNumber = u32; + type PalletInfo = (); + type DbWeight = (); } decl_module! { - pub struct Module for enum Call where origin: T::Origin {} + pub struct Module for enum Call where origin: T::Origin, system=self {} } crate::decl_storage! { diff --git a/frame/support/src/traits.rs b/frame/support/src/traits.rs index 377bfaa56a551..af7a7ee3635e7 100644 --- a/frame/support/src/traits.rs +++ b/frame/support/src/traits.rs @@ -1709,7 +1709,7 @@ impl IsType for T { /// "InstanceNMyModule". pub trait Instance: 'static { /// Unique module prefix. E.g. "InstanceNMyModule" or "MyModule" - const PREFIX: &'static str ; + const PREFIX: &'static str; } /// A trait similar to `Convert` to convert values from `B` an abstract balance type @@ -1826,6 +1826,96 @@ pub trait IsSubType { fn is_sub_type(&self) -> Option<&T>; } +/// The storage key postfix that is used to store the [`PalletVersion`] per pallet. +/// +/// The full storage key is built by using: +/// Twox128([`PalletInfo::name`]) ++ Twox128([`PALLET_VERSION_STORAGE_KEY_POSTFIX`]) +pub const PALLET_VERSION_STORAGE_KEY_POSTFIX: &[u8] = b":__PALLET_VERSION__:"; + +/// The version of a pallet. +/// +/// Each pallet version is stored in the state under a fixed key. See +/// [`PALLET_VERSION_STORAGE_KEY_POSTFIX`] for how this key is built. +#[derive(RuntimeDebug, Eq, PartialEq, Encode, Decode, Ord)] +pub struct PalletVersion { + /// The major version of the pallet. + pub major: u16, + /// The minor version of the pallet. + pub minor: u8, + /// The patch version of the pallet. + pub patch: u8, +} + +impl PalletVersion { + /// Creates a new instance of `Self`. + pub fn new(major: u16, minor: u8, patch: u8) -> Self { + Self { + major, + minor, + patch, + } + } + + /// Returns the storage key for a pallet version. + /// + /// See [`PALLET_VERSION_STORAGE_KEY_POSTIFX`] on how this key is built. + /// + /// Returns `None` if the given `PI` returned a `None` as name for the given + /// `Pallet`. + pub fn storage_key() -> Option<[u8; 32]> { + let pallet_name = PI::name::()?; + + let pallet_name = sp_io::hashing::twox_128(pallet_name.as_bytes()); + let postfix = sp_io::hashing::twox_128(PALLET_VERSION_STORAGE_KEY_POSTFIX); + + let mut final_key = [0u8; 32]; + final_key[..16].copy_from_slice(&pallet_name); + final_key[16..].copy_from_slice(&postfix); + + Some(final_key) + } +} + +impl sp_std::cmp::PartialOrd for PalletVersion { + fn partial_cmp(&self, other: &Self) -> Option { + let res = self.major + .cmp(&other.major) + .then_with(|| + self.minor + .cmp(&other.minor) + .then_with(|| self.patch.cmp(&other.patch) + )); + + Some(res) + } +} + +/// Provides version information about a pallet. +/// +/// This trait provides two functions for returning the version of a +/// pallet. There is a state where both functions can return distinct versions. +/// See [`GetPalletVersion::storage_version`] for more information about this. +pub trait GetPalletVersion { + /// Returns the current version of the pallet. + fn current_version() -> PalletVersion; + + /// Returns the version of the pallet that is stored in storage. + /// + /// Most of the time this will return the exact same version as + /// [`GetPalletVersion::current_version`]. Only when being in + /// a state after a runtime upgrade happened and the pallet did + /// not yet updated its version in storage, this will return a + /// different(the previous, seen from the time of calling) version. + /// + /// See [`PalletVersion`] for more information. + /// + /// # Note + /// + /// If there was no previous version of the pallet stored in the state, + /// this function returns `None`. + fn storage_version() -> Option; +} + #[cfg(test)] mod tests { use super::*; @@ -1847,4 +1937,18 @@ mod tests { assert_eq!(<(Test, Test)>::on_initialize(0), 20); assert_eq!(<(Test, Test)>::on_runtime_upgrade(), 40); } + + #[test] + fn check_pallet_version_ordering() { + let version = PalletVersion::new(1, 0, 0); + assert!(version > PalletVersion::new(0, 1, 2)); + assert!(version == PalletVersion::new(1, 0, 0)); + assert!(version < PalletVersion::new(1, 0, 1)); + assert!(version < PalletVersion::new(1, 1, 0)); + + let version = PalletVersion::new(2, 50, 50); + assert!(version < PalletVersion::new(2, 50, 51)); + assert!(version > PalletVersion::new(2, 49, 51)); + assert!(version < PalletVersion::new(3, 49, 51)); + } } diff --git a/frame/support/src/weights.rs b/frame/support/src/weights.rs index 1d19eeef70d79..74f0773aa541f 100644 --- a/frame/support/src/weights.rs +++ b/frame/support/src/weights.rs @@ -701,11 +701,12 @@ mod tests { use crate::{decl_module, parameter_types, traits::Get}; use super::*; - pub trait Trait { + pub trait Trait: 'static { type Origin; type Balance; type BlockNumber; type DbWeight: Get; + type PalletInfo: crate::traits::PalletInfo; } pub struct TraitImpl {} @@ -722,10 +723,11 @@ mod tests { type BlockNumber = u32; type Balance = u32; type DbWeight = DbWeight; + type PalletInfo = (); } decl_module! { - pub struct Module for enum Call where origin: T::Origin { + pub struct Module for enum Call where origin: T::Origin, system=self { // no arguments, fixed weight #[weight = 1000] fn f00(_origin) { unimplemented!(); } diff --git a/frame/support/test/src/lib.rs b/frame/support/test/src/lib.rs index d5f49299880ca..a917c781c065c 100644 --- a/frame/support/test/src/lib.rs +++ b/frame/support/test/src/lib.rs @@ -22,12 +22,19 @@ #![warn(missing_docs)] #![deny(warnings)] +#[cfg(test)] +mod pallet_version; + /// The configuration trait -pub trait Trait { +pub trait Trait: 'static { /// The runtime origin type. - type Origin; + type Origin: codec::Codec + codec::EncodeLike + Default; /// The block number type. - type BlockNumber; + type BlockNumber: codec::Codec + codec::EncodeLike + Default; + /// The information about the pallet setup in the runtime. + type PalletInfo: frame_support::traits::PalletInfo; + /// The db weights. + type DbWeight: frame_support::traits::Get; } frame_support::decl_module! { diff --git a/frame/support/test/src/pallet_version.rs b/frame/support/test/src/pallet_version.rs new file mode 100644 index 0000000000000..5912bd5b8e47f --- /dev/null +++ b/frame/support/test/src/pallet_version.rs @@ -0,0 +1,32 @@ +// This file is part of Substrate. + +// Copyright (C) 2020 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use frame_support::{crate_to_pallet_version, traits::PalletVersion}; + +#[test] +fn ensure_that_current_pallet_version_is_correct() { + let expected = PalletVersion { + major: env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap(), + minor: env!("CARGO_PKG_VERSION_MINOR").parse().unwrap(), + patch: env!("CARGO_PKG_VERSION_PATCH").parse().unwrap(), + }; + + assert_eq!( + expected, + crate_to_pallet_version!(), + ) +} diff --git a/frame/support/test/tests/construct_runtime.rs b/frame/support/test/tests/construct_runtime.rs index 9a17e44dbef4c..4ff4fc6828604 100644 --- a/frame/support/test/tests/construct_runtime.rs +++ b/frame/support/test/tests/construct_runtime.rs @@ -129,6 +129,7 @@ impl system::Trait for Runtime { type Event = Event; type PalletInfo = PalletInfo; type Call = Call; + type DbWeight = (); } frame_support::construct_runtime!( diff --git a/frame/support/test/tests/decl_storage.rs b/frame/support/test/tests/decl_storage.rs index 800ce459fed35..8d5727ce9104b 100644 --- a/frame/support/test/tests/decl_storage.rs +++ b/frame/support/test/tests/decl_storage.rs @@ -22,16 +22,12 @@ mod tests { use frame_support::metadata::*; use sp_io::TestExternalities; use std::marker::PhantomData; - use codec::{Encode, Decode, EncodeLike}; frame_support::decl_module! { - pub struct Module for enum Call where origin: T::Origin, system=self {} + pub struct Module for enum Call where origin: T::Origin, system=frame_support_test {} } - pub trait Trait { - type Origin: Encode + Decode + EncodeLike + std::default::Default; - type BlockNumber; - } + pub trait Trait: frame_support_test::Trait {} frame_support::decl_storage! { trait Store for Module as TestStorage { @@ -74,7 +70,7 @@ mod tests { pub PUBGETMAPU32MYDEF get(fn pub_map_u32_getter_mydef): map hasher(blake2_128_concat) u32 => String = "pubmap".into(); - COMPLEXTYPE1: ::std::vec::Vec<::Origin>; + COMPLEXTYPE1: ::std::vec::Vec; COMPLEXTYPE2: (Vec)>>, u32); COMPLEXTYPE3: [u32; 25]; } @@ -85,11 +81,15 @@ mod tests { struct TraitImpl {} - impl Trait for TraitImpl { + impl frame_support_test::Trait for TraitImpl { type Origin = u32; type BlockNumber = u32; + type PalletInfo = (); + type DbWeight = (); } + impl Trait for TraitImpl {} + const EXPECTED_METADATA: StorageMetadata = StorageMetadata { prefix: DecodeDifferent::Encode("TestStorage"), entries: DecodeDifferent::Encode( @@ -353,7 +353,7 @@ mod tests { StorageEntryMetadata { name: DecodeDifferent::Encode("COMPLEXTYPE1"), modifier: StorageEntryModifier::Default, - ty: StorageEntryType::Plain(DecodeDifferent::Encode("::std::vec::Vec<::Origin>")), + ty: StorageEntryType::Plain(DecodeDifferent::Encode("::std::vec::Vec")), default: DecodeDifferent::Encode( DefaultByteGetter(&__GetByteStructCOMPLEXTYPE1(PhantomData::)) ), @@ -414,13 +414,10 @@ mod tests { #[cfg(test)] #[allow(dead_code)] mod test2 { - pub trait Trait { - type Origin; - type BlockNumber; - } + pub trait Trait: frame_support_test::Trait {} frame_support::decl_module! { - pub struct Module for enum Call where origin: T::Origin, system=self {} + pub struct Module for enum Call where origin: T::Origin, system=frame_support_test {} } type PairOf = (T, T); @@ -441,21 +438,23 @@ mod test2 { struct TraitImpl {} - impl Trait for TraitImpl { + impl frame_support_test::Trait for TraitImpl { type Origin = u32; type BlockNumber = u32; + type PalletInfo = (); + type DbWeight = (); } + + impl Trait for TraitImpl {} } #[cfg(test)] #[allow(dead_code)] mod test3 { - pub trait Trait { - type Origin; - type BlockNumber; - } + pub trait Trait: frame_support_test::Trait {} + frame_support::decl_module! { - pub struct Module for enum Call where origin: T::Origin, system=self {} + pub struct Module for enum Call where origin: T::Origin, system=frame_support_test {} } frame_support::decl_storage! { trait Store for Module as Test { @@ -467,10 +466,14 @@ mod test3 { struct TraitImpl {} - impl Trait for TraitImpl { + impl frame_support_test::Trait for TraitImpl { type Origin = u32; type BlockNumber = u32; + type PalletInfo = (); + type DbWeight = (); } + + impl Trait for TraitImpl {} } #[cfg(test)] @@ -479,13 +482,10 @@ mod test_append_and_len { use sp_io::TestExternalities; use codec::{Encode, Decode}; - pub trait Trait { - type Origin; - type BlockNumber; - } + pub trait Trait: frame_support_test::Trait {} frame_support::decl_module! { - pub struct Module for enum Call where origin: T::Origin, system=self {} + pub struct Module for enum Call where origin: T::Origin, system=frame_support_test {} } #[derive(PartialEq, Eq, Clone, Encode, Decode)] @@ -511,11 +511,15 @@ mod test_append_and_len { struct Test {} - impl Trait for Test { + impl frame_support_test::Trait for Test { type Origin = u32; type BlockNumber = u32; + type PalletInfo = (); + type DbWeight = (); } + impl Trait for Test {} + #[test] fn default_for_option() { TestExternalities::default().execute_with(|| { diff --git a/frame/support/test/tests/decl_storage_ui/config_duplicate.rs b/frame/support/test/tests/decl_storage_ui/config_duplicate.rs index f4f4ad7d48a97..58923ed19297c 100644 --- a/frame/support/test/tests/decl_storage_ui/config_duplicate.rs +++ b/frame/support/test/tests/decl_storage_ui/config_duplicate.rs @@ -15,13 +15,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub trait Trait { - type Origin; - type BlockNumber: codec::Codec + codec::EncodeLike + Default + Clone; -} +pub trait Trait: frame_support_test::Trait {} frame_support::decl_module! { - pub struct Module for enum Call where origin: T::Origin, system=self {} + pub struct Module for enum Call where origin: T::Origin, system=frame_support_test {} } frame_support::decl_storage!{ diff --git a/frame/support/test/tests/decl_storage_ui/config_duplicate.stderr b/frame/support/test/tests/decl_storage_ui/config_duplicate.stderr index 61f7c0bbe64a5..f6303f277b56b 100644 --- a/frame/support/test/tests/decl_storage_ui/config_duplicate.stderr +++ b/frame/support/test/tests/decl_storage_ui/config_duplicate.stderr @@ -1,5 +1,5 @@ error: `config()`/`get()` with the same name already defined. - --> $DIR/config_duplicate.rs:30:21 + --> $DIR/config_duplicate.rs:27:21 | -30 | pub Value2 config(value): u32; +27 | pub Value2 config(value): u32; | ^^^^^ diff --git a/frame/support/test/tests/decl_storage_ui/config_get_duplicate.rs b/frame/support/test/tests/decl_storage_ui/config_get_duplicate.rs index 3caa2d9c33608..e77dcea404ccb 100644 --- a/frame/support/test/tests/decl_storage_ui/config_get_duplicate.rs +++ b/frame/support/test/tests/decl_storage_ui/config_get_duplicate.rs @@ -15,13 +15,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub trait Trait { - type Origin; - type BlockNumber: codec::Codec + codec::EncodeLike + Default + Clone; -} +pub trait Trait: frame_support_test::Trait {} frame_support::decl_module! { - pub struct Module for enum Call where origin: T::Origin, system=self {} + pub struct Module for enum Call where origin: T::Origin, system=frame_support_test {} } frame_support::decl_storage!{ diff --git a/frame/support/test/tests/decl_storage_ui/config_get_duplicate.stderr b/frame/support/test/tests/decl_storage_ui/config_get_duplicate.stderr index 02e7d41080339..9377b718c0660 100644 --- a/frame/support/test/tests/decl_storage_ui/config_get_duplicate.stderr +++ b/frame/support/test/tests/decl_storage_ui/config_get_duplicate.stderr @@ -1,5 +1,5 @@ error: `config()`/`get()` with the same name already defined. - --> $DIR/config_get_duplicate.rs:30:21 + --> $DIR/config_get_duplicate.rs:27:21 | -30 | pub Value2 config(value): u32; +27 | pub Value2 config(value): u32; | ^^^^^ diff --git a/frame/support/test/tests/decl_storage_ui/get_duplicate.rs b/frame/support/test/tests/decl_storage_ui/get_duplicate.rs index 1c24b3bf28eec..b6ccb7ebb7b7b 100644 --- a/frame/support/test/tests/decl_storage_ui/get_duplicate.rs +++ b/frame/support/test/tests/decl_storage_ui/get_duplicate.rs @@ -15,13 +15,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub trait Trait { - type Origin; - type BlockNumber: codec::Codec + codec::EncodeLike + Default + Clone; -} +pub trait Trait: frame_support_test::Trait {} frame_support::decl_module! { - pub struct Module for enum Call where origin: T::Origin, system=self {} + pub struct Module for enum Call where origin: T::Origin, system=frame_support_test {} } frame_support::decl_storage!{ diff --git a/frame/support/test/tests/decl_storage_ui/get_duplicate.stderr b/frame/support/test/tests/decl_storage_ui/get_duplicate.stderr index d9ce420a6f214..0039b10fb43b6 100644 --- a/frame/support/test/tests/decl_storage_ui/get_duplicate.stderr +++ b/frame/support/test/tests/decl_storage_ui/get_duplicate.stderr @@ -1,5 +1,5 @@ error: `config()`/`get()` with the same name already defined. - --> $DIR/get_duplicate.rs:30:21 + --> $DIR/get_duplicate.rs:27:21 | -30 | pub Value2 get(fn value) config(): u32; +27 | pub Value2 get(fn value) config(): u32; | ^^^^^ diff --git a/frame/support/test/tests/final_keys.rs b/frame/support/test/tests/final_keys.rs index a9f0cdc8f184b..6bd1252825466 100644 --- a/frame/support/test/tests/final_keys.rs +++ b/frame/support/test/tests/final_keys.rs @@ -21,15 +21,10 @@ use frame_support::{StorageDoubleMap, StorageMap, StorageValue, StoragePrefixedM use sp_io::{TestExternalities, hashing::{twox_64, twox_128, blake2_128}}; mod no_instance { - use codec::{Encode, Decode, EncodeLike}; - - pub trait Trait { - type Origin; - type BlockNumber: Encode + Decode + EncodeLike + Default + Clone; - } + pub trait Trait: frame_support_test::Trait {} frame_support::decl_module! { - pub struct Module for enum Call where origin: T::Origin, system=self {} + pub struct Module for enum Call where origin: T::Origin, system=frame_support_test {} } frame_support::decl_storage!{ @@ -50,13 +45,11 @@ mod no_instance { } mod instance { - use super::no_instance; - - pub trait Trait: super::no_instance::Trait {} + pub trait Trait: frame_support_test::Trait {} frame_support::decl_module! { pub struct Module, I: Instance = DefaultInstance> - for enum Call where origin: T::Origin, system=no_instance {} + for enum Call where origin: T::Origin, system=frame_support_test {} } frame_support::decl_storage!{ diff --git a/frame/support/test/tests/genesisconfig.rs b/frame/support/test/tests/genesisconfig.rs index af8b393800cf9..f268f11a4dc15 100644 --- a/frame/support/test/tests/genesisconfig.rs +++ b/frame/support/test/tests/genesisconfig.rs @@ -15,13 +15,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub trait Trait { - type BlockNumber: codec::Codec + codec::EncodeLike + Default; - type Origin; -} +pub trait Trait: frame_support_test::Trait {} frame_support::decl_module! { - pub struct Module for enum Call where origin: T::Origin, system=self {} + pub struct Module for enum Call where origin: T::Origin, system=frame_support_test {} } frame_support::decl_storage! { @@ -32,11 +29,15 @@ frame_support::decl_storage! { struct Test; -impl Trait for Test { +impl frame_support_test::Trait for Test { type BlockNumber = u32; type Origin = (); + type PalletInfo = (); + type DbWeight = (); } +impl Trait for Test {} + #[test] fn init_genesis_config() { GenesisConfig:: { diff --git a/frame/support/test/tests/instance.rs b/frame/support/test/tests/instance.rs index e1766082dd806..61df5d4eb8182 100644 --- a/frame/support/test/tests/instance.rs +++ b/frame/support/test/tests/instance.rs @@ -250,6 +250,7 @@ impl system::Trait for Runtime { type Event = Event; type PalletInfo = (); type Call = Call; + type DbWeight = (); } frame_support::construct_runtime!( diff --git a/frame/support/test/tests/issue2219.rs b/frame/support/test/tests/issue2219.rs index 34310c2f5876f..596a3b6ffb25d 100644 --- a/frame/support/test/tests/issue2219.rs +++ b/frame/support/test/tests/issue2219.rs @@ -166,6 +166,7 @@ impl system::Trait for Runtime { type Event = Event; type PalletInfo = (); type Call = Call; + type DbWeight = (); } impl module::Trait for Runtime {} diff --git a/frame/support/test/tests/pallet_version.rs b/frame/support/test/tests/pallet_version.rs new file mode 100644 index 0000000000000..f3f4029b0da53 --- /dev/null +++ b/frame/support/test/tests/pallet_version.rs @@ -0,0 +1,175 @@ +// This file is part of Substrate. + +// Copyright (C) 2020 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Tests related to the pallet version. + +#![recursion_limit="128"] + +use codec::{Decode, Encode}; +use sp_runtime::{generic, traits::{BlakeTwo256, Block as _, Verify}}; +use frame_support::{ + traits::{PALLET_VERSION_STORAGE_KEY_POSTFIX, PalletVersion, OnRuntimeUpgrade}, + crate_to_pallet_version, weights::Weight, +}; +use sp_core::{H256, sr25519}; + +mod system; + +/// A version that we will check for in the tests +const SOME_TEST_VERSION: PalletVersion = PalletVersion { major: 3000, minor: 30, patch: 13 }; + +/// Checks that `on_runtime_upgrade` sets the latest pallet version when being called without +/// being provided by the user. +mod module1 { + use super::*; + + pub trait Trait: system::Trait {} + + frame_support::decl_module! { + pub struct Module for enum Call where + origin: ::Origin, + system = system, + {} + } +} + +/// Checks that `on_runtime_upgrade` sets the latest pallet version when being called and also +/// being provided by the user. +mod module2 { + use super::*; + + pub trait Trait: system::Trait {} + + frame_support::decl_module! { + pub struct Module, I: Instance=DefaultInstance> for enum Call where + origin: ::Origin, + system = system + { + fn on_runtime_upgrade() -> Weight { + assert_eq!(crate_to_pallet_version!(), Self::current_version()); + + let version_key = PalletVersion::storage_key::().unwrap(); + let version_value = sp_io::storage::get(&version_key); + + if version_value.is_some() { + assert_eq!(SOME_TEST_VERSION, Self::storage_version().unwrap()); + } else { + // As the storage version does not exist yet, it should be `None`. + assert!(Self::storage_version().is_none()); + } + + 0 + } + } + } + + frame_support::decl_storage! { + trait Store for Module, I: Instance=DefaultInstance> as Module2 {} + } +} + +impl module1::Trait for Runtime {} +impl module2::Trait for Runtime {} +impl module2::Trait for Runtime {} +impl module2::Trait for Runtime {} + +pub type Signature = sr25519::Signature; +pub type AccountId = ::Signer; +pub type BlockNumber = u64; +pub type Index = u64; + +impl system::Trait for Runtime { + type BaseCallFilter= (); + type Hash = H256; + type Origin = Origin; + type BlockNumber = BlockNumber; + type AccountId = AccountId; + type Event = Event; + type PalletInfo = PalletInfo; + type Call = Call; + type DbWeight = (); +} + +frame_support::construct_runtime!( + pub enum Runtime where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic + { + System: system::{Module, Call, Event}, + Module1: module1::{Module, Call}, + Module2: module2::{Module, Call}, + Module2_1: module2::::{Module, Call}, + Module2_2: module2::::{Module, Call}, + } +); + +pub type Header = generic::Header; +pub type Block = generic::Block; +pub type UncheckedExtrinsic = generic::UncheckedExtrinsic; + +/// Returns the storage key for `PalletVersion` for the given `pallet`. +fn get_pallet_version_storage_key_for_pallet(pallet: &str) -> [u8; 32] { + let pallet_name = sp_io::hashing::twox_128(pallet.as_bytes()); + let postfix = sp_io::hashing::twox_128(PALLET_VERSION_STORAGE_KEY_POSTFIX); + + let mut final_key = [0u8; 32]; + final_key[..16].copy_from_slice(&pallet_name); + final_key[16..].copy_from_slice(&postfix); + + final_key +} + +/// Checks the version of the given `pallet`. +/// +/// It is expected that the pallet version can be found in the storage and equals the +/// current crate version. +fn check_pallet_version(pallet: &str) { + let key = get_pallet_version_storage_key_for_pallet(pallet); + let value = sp_io::storage::get(&key).expect("Pallet version exists"); + let version = PalletVersion::decode(&mut &value[..]) + .expect("Pallet version is encoded correctly"); + + assert_eq!(crate_to_pallet_version!(), version); +} + +#[test] +fn on_runtime_upgrade_sets_the_pallet_versions_in_storage() { + sp_io::TestExternalities::new_empty().execute_with(|| { + AllModules::on_runtime_upgrade(); + + check_pallet_version("Module1"); + check_pallet_version("Module2"); + check_pallet_version("Module2_1"); + check_pallet_version("Module2_2"); + }); +} + +#[test] +fn on_runtime_upgrade_overwrites_old_version() { + sp_io::TestExternalities::new_empty().execute_with(|| { + let key = get_pallet_version_storage_key_for_pallet("Module2"); + sp_io::storage::set(&key, &SOME_TEST_VERSION.encode()); + + AllModules::on_runtime_upgrade(); + + check_pallet_version("Module1"); + check_pallet_version("Module2"); + check_pallet_version("Module2_1"); + check_pallet_version("Module2_2"); + }); +} diff --git a/frame/support/test/tests/reserved_keyword/on_initialize.rs b/frame/support/test/tests/reserved_keyword/on_initialize.rs index db71fe9a1e26a..781b72bd04e8c 100644 --- a/frame/support/test/tests/reserved_keyword/on_initialize.rs +++ b/frame/support/test/tests/reserved_keyword/on_initialize.rs @@ -4,10 +4,7 @@ macro_rules! reserved { mod $reserved { pub use frame_support::dispatch; - pub trait Trait { - type Origin; - type BlockNumber: Into; - } + pub trait Trait: frame_support_test::Trait {} pub mod system { use frame_support::dispatch; @@ -18,7 +15,7 @@ macro_rules! reserved { } frame_support::decl_module! { - pub struct Module for enum Call where origin: T::Origin, system=self { + pub struct Module for enum Call where origin: T::Origin, system=frame_support_test { #[weight = 0] fn $reserved(_origin) -> dispatch::DispatchResult { unreachable!() } } diff --git a/frame/support/test/tests/reserved_keyword/on_initialize.stderr b/frame/support/test/tests/reserved_keyword/on_initialize.stderr index dbe07195e89dd..3df392dee9005 100644 --- a/frame/support/test/tests/reserved_keyword/on_initialize.stderr +++ b/frame/support/test/tests/reserved_keyword/on_initialize.stderr @@ -1,39 +1,39 @@ error: Invalid call fn name: `on_finalize`, name is reserved and doesn't match expected signature, please refer to `decl_module!` documentation to see the appropriate usage, or rename it to an unreserved keyword. - --> $DIR/on_initialize.rs:31:1 + --> $DIR/on_initialize.rs:28:1 | -31 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event); +28 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) error: Invalid call fn name: `on_initialize`, name is reserved and doesn't match expected signature, please refer to `decl_module!` documentation to see the appropriate usage, or rename it to an unreserved keyword. - --> $DIR/on_initialize.rs:31:1 + --> $DIR/on_initialize.rs:28:1 | -31 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event); +28 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) error: Invalid call fn name: `on_runtime_upgrade`, name is reserved and doesn't match expected signature, please refer to `decl_module!` documentation to see the appropriate usage, or rename it to an unreserved keyword. - --> $DIR/on_initialize.rs:31:1 + --> $DIR/on_initialize.rs:28:1 | -31 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event); +28 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) error: Invalid call fn name: `offchain_worker`, name is reserved and doesn't match expected signature, please refer to `decl_module!` documentation to see the appropriate usage, or rename it to an unreserved keyword. - --> $DIR/on_initialize.rs:31:1 + --> $DIR/on_initialize.rs:28:1 | -31 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event); +28 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) error: Invalid call fn name: `deposit_event`, name is reserved and doesn't match expected signature, please refer to `decl_module!` documentation to see the appropriate usage, or rename it to an unreserved keyword. - --> $DIR/on_initialize.rs:31:1 + --> $DIR/on_initialize.rs:28:1 | -31 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event); +28 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/frame/support/test/tests/storage_transaction.rs b/frame/support/test/tests/storage_transaction.rs index be8b678c6dfd0..5c687ef05005d 100644 --- a/frame/support/test/tests/storage_transaction.rs +++ b/frame/support/test/tests/storage_transaction.rs @@ -15,23 +15,17 @@ // See the License for the specific language governing permissions and // limitations under the License. -use codec::{Encode, Decode, EncodeLike}; use frame_support::{ - assert_ok, assert_noop, transactional, - StorageMap, StorageValue, - dispatch::{DispatchError, DispatchResult}, - storage::{with_transaction, TransactionOutcome::*}, + assert_ok, assert_noop, transactional, StorageMap, StorageValue, + dispatch::{DispatchError, DispatchResult}, storage::{with_transaction, TransactionOutcome::*}, }; use sp_io::TestExternalities; use sp_std::result; -pub trait Trait { - type Origin; - type BlockNumber: Encode + Decode + EncodeLike + Default + Clone; -} +pub trait Trait: frame_support_test::Trait {} frame_support::decl_module! { - pub struct Module for enum Call where origin: T::Origin, system=self { + pub struct Module for enum Call where origin: T::Origin, system=frame_support_test { #[weight = 0] #[transactional] fn value_commits(_origin, v: u32) { @@ -55,11 +49,16 @@ frame_support::decl_storage!{ } struct Runtime; -impl Trait for Runtime { + +impl frame_support_test::Trait for Runtime { type Origin = u32; type BlockNumber = u32; + type PalletInfo = (); + type DbWeight = (); } +impl Trait for Runtime {} + #[test] fn storage_transaction_basic_commit() { TestExternalities::default().execute_with(|| { diff --git a/frame/support/test/tests/system.rs b/frame/support/test/tests/system.rs index a7d4d43c341a9..f30b6e4c2af9d 100644 --- a/frame/support/test/tests/system.rs +++ b/frame/support/test/tests/system.rs @@ -15,7 +15,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -use frame_support::codec::{Encode, Decode, EncodeLike}; +use frame_support::{ + codec::{Encode, Decode, EncodeLike}, traits::Get, weights::RuntimeDbWeight, +}; pub trait Trait: 'static + Eq + Clone { type Origin: Into, Self::Origin>> @@ -28,6 +30,7 @@ pub trait Trait: 'static + Eq + Clone { type Call; type Event: From>; type PalletInfo: frame_support::traits::PalletInfo; + type DbWeight: Get; } frame_support::decl_module! {