Moves Block to frame_system instead of construct_runtime and removes Header and BlockNumber (#14437)

* Initial setup

* Adds node block

* Uses UncheckedExtrinsic and removes Where section

* Updates frame_system to use Block

* Adds deprecation warning

* Fixes pallet-timestamp

* Removes Header and BlockNumber

* Addresses review comments

* Addresses review comments

* Adds comment about compiler bug

* Removes where clause

* Refactors code

* Fixes errors in cargo check

* Fixes errors in cargo check

* Fixes warnings in cargo check

* Formatting

* Fixes construct_runtime tests

* Uses import instead of full path for BlockNumber

* Uses import instead of full path for Header

* Formatting

* Fixes construct_runtime tests

* Fixes imports in benchmarks

* Formatting

* Fixes construct_runtime tests

* Formatting

* Minor updates

* Fixes construct_runtime ui tests

* Fixes construct_runtime ui tests with 1.70

* Fixes docs

* Fixes docs

* Adds u128 mock block type

* Fixes split example

* fixes for cumulus

* ".git/.scripts/commands/fmt/fmt.sh"

* Updates new tests

* Fixes fully-qualified path in few places

* Formatting

* Update frame/examples/default-config/src/lib.rs

Co-authored-by: Juan <juangirini@gmail.com>

* Update frame/support/procedural/src/construct_runtime/mod.rs

Co-authored-by: Juan <juangirini@gmail.com>

* ".git/.scripts/commands/fmt/fmt.sh"

* Addresses some review comments

* Fixes build

* ".git/.scripts/commands/fmt/fmt.sh"

* Update frame/democracy/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update frame/democracy/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update frame/support/procedural/src/construct_runtime/mod.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update frame/support/procedural/src/construct_runtime/mod.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Addresses review comments

* Updates trait bounds

* Minor fix

* ".git/.scripts/commands/fmt/fmt.sh"

* Removes unnecessary bound

* ".git/.scripts/commands/fmt/fmt.sh"

* Updates test

* Fixes build

* Adds a bound for header

* ".git/.scripts/commands/fmt/fmt.sh"

* Removes where block

* Minor fix

* Minor fix

* Fixes tests

* ".git/.scripts/commands/update-ui/update-ui.sh" 1.70

* Updates test

* Update primitives/runtime/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/runtime/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Updates doc

* Updates doc

---------

Co-authored-by: command-bot <>
Co-authored-by: Juan <juangirini@gmail.com>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
gupnik
2023-07-13 17:31:34 +05:30
committed by GitHub
parent e42a669c50
commit 5e7b27e98c
277 changed files with 2017 additions and 2450 deletions
+7 -259
View File
@@ -645,259 +645,6 @@ impl<T> PaysFee<T> for (u64, Pays) {
// END TODO
/// Declares a `Module` struct and a `Call` enum, which implements the dispatch logic.
///
/// ## Declaration
///
/// ```
/// # #[macro_use]
/// # extern crate frame_support;
/// # use frame_support::dispatch;
/// # use frame_system::{Config, ensure_signed};
/// decl_module! {
/// pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin {
///
/// // Private functions are dispatchable, but not available to other
/// // FRAME pallets.
/// #[weight = 0]
/// fn my_function(origin, var: u64) -> dispatch::DispatchResult {
/// // Your implementation
/// Ok(())
/// }
///
/// // Public functions are both dispatchable and available to other
/// // FRAME pallets.
/// #[weight = 0]
/// pub fn my_public_function(origin) -> dispatch::DispatchResult {
/// // Your implementation
/// Ok(())
/// }
/// }
/// }
/// # fn main() {}
/// ```
///
/// The declaration is set with the header where:
///
/// * `Module`: The struct generated by the macro, with type `Config`.
/// * `Call`: The enum generated for every pallet, which implements
/// [`Callable`](./dispatch/trait.Callable.html).
/// * `origin`: Alias of `T::RuntimeOrigin`.
/// * `Result`: The expected return type from pallet functions.
///
/// The first parameter of dispatchable functions must always be `origin`.
///
/// ### Shorthand Example
///
/// The macro automatically expands a shorthand function declaration to return the
/// [`DispatchResult`] type. These functions are the same:
///
/// ```
/// # #[macro_use]
/// # extern crate frame_support;
/// # use frame_support::dispatch;
/// # use frame_system::{Config, ensure_signed};
/// decl_module! {
/// pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin {
/// #[weight = 0]
/// fn my_long_function(origin) -> dispatch::DispatchResult {
/// // Your implementation
/// Ok(())
/// }
///
/// #[weight = 0]
/// fn my_short_function(origin) {
/// // Your implementation
/// }
/// }
/// }
/// # fn main() {}
/// ```
///
/// ### Consuming only portions of the annotated static weight
///
/// Per default a callable function consumes all of its static weight as declared via
/// the #\[weight\] attribute. However, there are use cases where only a portion of this
/// weight should be consumed. In that case the static weight is charged pre dispatch and
/// the difference is refunded post dispatch.
///
/// In order to make use of this feature the function must return `DispatchResultWithPostInfo`
/// in place of the default `DispatchResult`. Then the actually consumed weight can be returned.
/// To consume a non default weight while returning an error
/// [`WithPostDispatchInfo::with_weight`](./weight/trait.WithPostDispatchInfo.html) can be used
/// to augment any error with custom weight information.
///
/// ```
/// # #[macro_use]
/// # extern crate frame_support;
/// # use frame_support::{weights::Weight, dispatch::{DispatchResultWithPostInfo, WithPostDispatchInfo, PostDispatchInfo}};
/// # use frame_system::{Config, ensure_signed};
/// decl_module! {
/// pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin {
/// #[weight = 1_000_000]
/// fn my_long_function(origin, do_expensive_calc: bool) -> DispatchResultWithPostInfo {
/// ensure_signed(origin).map_err(|e| e.with_weight(Weight::from_parts(100_000, 0)))?;
/// if do_expensive_calc {
/// // do the expensive calculation
/// // ...
/// // return None to indicate that we are using all weight (the default)
/// return Ok(None::<Weight>.into());
/// }
/// // expensive calculation not executed: use only a portion of the weight
/// Ok(PostDispatchInfo { actual_weight: Some(Weight::from_parts(100_000, 0)), ..Default::default() })
/// }
/// }
/// }
/// # fn main() {}
/// ```
///
/// ### Transactional Function Example
///
/// Transactional function discards all changes to storage if it returns `Err`, or commits if
/// `Ok`, via the #\[transactional\] attribute. Note the attribute must be after #\[weight\].
/// The #\[transactional\] attribute is deprecated since it is the default behaviour.
///
/// ```
/// # #[macro_use]
/// # extern crate frame_support;
/// # use frame_support::transactional;
/// # use frame_system::Config;
/// decl_module! {
/// pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin {
/// #[weight = 0]
/// #[transactional]
/// fn my_short_function(origin) {
/// // Your implementation
/// }
/// }
/// }
/// # fn main() {}
/// ```
///
/// ### Privileged Function Example
///
/// A privileged function checks that the origin of the call is `ROOT`.
///
/// ```
/// # #[macro_use]
/// # extern crate frame_support;
/// # use frame_support::dispatch;
/// # use frame_system::{Config, ensure_signed, ensure_root};
/// decl_module! {
/// pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin {
/// #[weight = 0]
/// fn my_privileged_function(origin) -> dispatch::DispatchResult {
/// ensure_root(origin)?;
/// // Your implementation
/// Ok(())
/// }
/// }
/// }
/// # fn main() {}
/// ```
///
/// ### Attributes on Functions
///
/// Attributes on functions are supported, but must be in the order of:
/// 1. Optional #\[doc\] attribute.
/// 2. #\[weight\] attribute.
/// 3. Optional function attributes, for instance #\[transactional\]. Those function attributes will
/// be written only on the dispatchable functions implemented on `Module`, not on the `Call` enum
/// variant.
///
/// ## Multiple Module Instances Example
///
/// A Substrate module can be built such that multiple instances of the same module can be used
/// within a single runtime. For example, the [Balances module](../pallet_balances/index.html) can
/// be added multiple times to your runtime in order to support multiple, independent currencies for
/// your blockchain. Here is an example of how you would declare such a module using the
/// `decl_module!` macro:
///
/// ```
/// # #[macro_use]
/// # extern crate frame_support;
/// # use frame_support::dispatch;
/// # use frame_system::ensure_signed;
/// # pub struct DefaultInstance;
/// # pub trait Instance: 'static {}
/// # impl Instance for DefaultInstance {}
/// pub trait Config<I: Instance=DefaultInstance>: frame_system::Config {}
///
/// decl_module! {
/// pub struct Module<T: Config<I>, I: Instance = DefaultInstance> for enum Call where origin: T::RuntimeOrigin {
/// // Your implementation
/// }
/// }
/// # fn main() {}
/// ```
///
/// Note: `decl_storage` must be called to generate `Instance` trait and optionally
/// `DefaultInstance` type.
///
/// ## Where clause
///
/// Besides the default `origin: T::RuntimeOrigin`, you can also pass other bounds to the module
/// declaration. This where bound will be replicated to all types generated by this macro. The
/// chaining of multiple trait bounds with `+` is not supported. If multiple bounds for one type are
/// required, it needs to be split up into multiple bounds.
///
/// ```
/// # #[macro_use]
/// # extern crate frame_support;
/// # use frame_support::dispatch;
/// # use frame_system::{self as system, ensure_signed};
/// pub trait Config: system::Config where Self::AccountId: From<u32> {}
///
/// decl_module! {
/// pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, T::AccountId: From<u32> {
/// // Your implementation
/// }
/// }
/// # fn main() {}
/// ```
///
/// ## Reserved Functions
///
/// The following are reserved function signatures:
///
/// * `deposit_event`: Helper function for depositing an [event](https://docs.substrate.io/main-docs/build/events-errors/).
/// The default behavior is to call `deposit_event` from the [System
/// module](../frame_system/index.html). However, you can write your own implementation for events
/// in your runtime. To use the default behavior, add `fn deposit_event() = default;` to your
/// `Module`.
///
/// The following reserved functions also take the block number (with type `T::BlockNumber`) as an
/// optional input:
///
/// * `on_runtime_upgrade`: Executes at the beginning of a block prior to on_initialize when there
/// is a runtime upgrade. This allows each module to upgrade its storage before the storage items
/// are used. As such, **calling other modules must be avoided**!! Using this function will
/// implement the [`OnRuntimeUpgrade`](../sp_runtime/traits/trait.OnRuntimeUpgrade.html) trait.
/// Function signature must be `fn on_runtime_upgrade() -> frame_support::weights::Weight`.
///
/// * `on_initialize`: Executes at the beginning of a block. Using this function will
/// implement the [`OnInitialize`](./trait.OnInitialize.html) trait.
/// Function signature can be either:
/// * `fn on_initialize(n: BlockNumber) -> frame_support::weights::Weight` or
/// * `fn on_initialize() -> frame_support::weights::Weight`
///
/// * `on_idle`: Executes at the end of a block. Passes a remaining weight to provide a threshold
/// for when to execute non vital functions. Using this function will implement the
/// [`OnIdle`](./traits/trait.OnIdle.html) trait.
/// Function signature is:
/// * `fn on_idle(n: BlockNumber, remaining_weight: Weight) -> frame_support::weights::Weight`
///
/// * `on_finalize`: Executes at the end of a block. Using this function will
/// implement the [`OnFinalize`](./traits/trait.OnFinalize.html) trait.
/// Function signature can be either:
/// * `fn on_finalize(n: BlockNumber) -> frame_support::weights::Weight` or
/// * `fn on_finalize() -> frame_support::weights::Weight`
///
/// * `offchain_worker`: Executes at the beginning of a block and produces extrinsics for a future
/// block upon completion. Using this function will implement the
/// [`OffchainWorker`](./traits/trait.OffchainWorker.html) trait.
/// * `integrity_test`: Executes in a test generated by `construct_runtime`, note it doesn't execute
/// in an externalities-provided environment. Implement
/// [`IntegrityTest`](./trait.IntegrityTest.html) trait.
#[macro_export]
#[deprecated(note = "Will be removed after July 2023; use the attribute `#[pallet]` macro instead.
For more info, see: <https://github.com/paritytech/substrate/pull/13705>")]
@@ -3587,7 +3334,7 @@ mod weight_tests {
#[pallet::config]
#[pallet::disable_frame_system_supertrait_check]
pub trait Config: 'static {
type BlockNumber: Parameter + Default + MaxEncodedLen;
type Block: Parameter + sp_runtime::traits::Block;
type AccountId;
type Balance;
type BaseCallFilter: crate::traits::Contains<Self::RuntimeCall>;
@@ -3653,6 +3400,11 @@ mod weight_tests {
pub mod pallet_prelude {
pub type OriginFor<T> = <T as super::Config>::RuntimeOrigin;
pub type HeaderFor<T> =
<<T as super::Config>::Block as sp_runtime::traits::HeaderProvider>::HeaderT;
pub type BlockNumberFor<T> = <HeaderFor<T> as sp_runtime::traits::Header>::Number;
}
}
@@ -3665,10 +3417,6 @@ mod weight_tests {
crate::construct_runtime!(
pub enum Runtime
where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: self::frame_system,
}
@@ -3682,7 +3430,7 @@ mod weight_tests {
}
impl Config for Runtime {
type BlockNumber = BlockNumber;
type Block = Block;
type AccountId = AccountId;
type Balance = Balance;
type BaseCallFilter = crate::traits::Everything;
-45
View File
@@ -21,51 +21,6 @@
pub use sp_runtime::traits::{BadOrigin, LookupError};
/// Declare an error type for a runtime module.
///
/// `decl_error!` supports only variants that do not hold any data. The dispatchable
/// functions return [`DispatchResult`](sp_runtime::DispatchResult). The error type
/// implements `From<ErrorType> for DispatchResult` to make the error type usable as error
/// in the dispatchable functions.
///
/// It is required that the error type is registered in `decl_module!` to make the error
/// exported in the metadata.
///
/// # Usage
///
/// ```
/// # use frame_support::{decl_error, decl_module};
/// #
/// decl_error! {
/// /// Errors that can occur in my module.
/// pub enum MyError for Module<T: Config> {
/// /// Hey this is an error message that indicates bla.
/// MyCoolErrorMessage,
/// /// You are just not cool enough for my module!
/// YouAreNotCoolEnough,
/// }
/// }
///
/// # use frame_system::Config;
///
/// // You need to register the error type in `decl_module!` as well to make the error
/// // exported in the metadata.
///
/// decl_module! {
/// pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin {
/// type Error = MyError<T>;
///
/// #[weight = 0]
/// fn do_something(origin) -> frame_support::dispatch::DispatchResult {
/// Err(MyError::<T>::YouAreNotCoolEnough.into())
/// }
/// }
/// }
///
/// # fn main() {}
/// ```
///
/// For instantiable modules you also need to give the instance generic type and bound to the
/// error declaration.
#[macro_export]
#[deprecated(note = "Will be removed after July 2023; use the attribute `#[pallet]` macro instead.
For more info, see: <https://github.com/paritytech/substrate/pull/13705>")]
+21 -28
View File
@@ -834,7 +834,7 @@ pub mod tests {
use sp_runtime::{generic, traits::BlakeTwo256, BuildStorage};
use sp_std::result;
pub use self::frame_system::{Config, Pallet};
pub use self::frame_system::{pallet_prelude::*, Config, Pallet};
#[pallet]
pub mod frame_system {
@@ -849,7 +849,7 @@ pub mod tests {
#[pallet::config]
#[pallet::disable_frame_system_supertrait_check]
pub trait Config: 'static {
type BlockNumber: Parameter + Default + MaxEncodedLen;
type Block: Parameter + sp_runtime::traits::Block;
type AccountId;
type BaseCallFilter: crate::traits::Contains<Self::RuntimeCall>;
type RuntimeOrigin;
@@ -879,12 +879,12 @@ pub mod tests {
#[pallet::storage]
#[pallet::getter(fn generic_data)]
pub type GenericData<T: Config> =
StorageMap<_, Identity, T::BlockNumber, T::BlockNumber, ValueQuery>;
StorageMap<_, Identity, BlockNumberFor<T>, BlockNumberFor<T>, ValueQuery>;
#[pallet::storage]
#[pallet::getter(fn generic_data2)]
pub type GenericData2<T: Config> =
StorageMap<_, Blake2_128Concat, T::BlockNumber, T::BlockNumber, OptionQuery>;
StorageMap<_, Blake2_128Concat, BlockNumberFor<T>, BlockNumberFor<T>, OptionQuery>;
#[pallet::storage]
pub type DataDM<T> =
@@ -894,10 +894,10 @@ pub mod tests {
pub type GenericDataDM<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
T::BlockNumber,
BlockNumberFor<T>,
Identity,
T::BlockNumber,
T::BlockNumber,
BlockNumberFor<T>,
BlockNumberFor<T>,
ValueQuery,
>;
@@ -905,10 +905,10 @@ pub mod tests {
pub type GenericData2DM<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
T::BlockNumber,
BlockNumberFor<T>,
Twox64Concat,
T::BlockNumber,
T::BlockNumber,
BlockNumberFor<T>,
BlockNumberFor<T>,
OptionQuery,
>;
@@ -919,7 +919,7 @@ pub mod tests {
Blake2_128Concat,
u32,
Blake2_128Concat,
T::BlockNumber,
BlockNumberFor<T>,
Vec<u32>,
ValueQuery,
>;
@@ -956,6 +956,11 @@ pub mod tests {
pub mod pallet_prelude {
pub type OriginFor<T> = <T as super::Config>::RuntimeOrigin;
pub type HeaderFor<T> =
<<T as super::Config>::Block as sp_runtime::traits::HeaderProvider>::HeaderT;
pub type BlockNumberFor<T> = <HeaderFor<T> as sp_runtime::traits::Header>::Number;
}
}
@@ -967,17 +972,13 @@ pub mod tests {
crate::construct_runtime!(
pub enum Runtime
where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: self::frame_system,
}
);
impl Config for Runtime {
type BlockNumber = BlockNumber;
type Block = Block;
type AccountId = AccountId;
type BaseCallFilter = crate::traits::Everything;
type RuntimeOrigin = RuntimeOrigin;
@@ -1005,12 +1006,8 @@ pub mod tests {
fn storage_alias_works() {
new_test_ext().execute_with(|| {
#[crate::storage_alias]
type GenericData2<T> = StorageMap<
System,
Blake2_128Concat,
<T as Config>::BlockNumber,
<T as Config>::BlockNumber,
>;
type GenericData2<T> =
StorageMap<System, Blake2_128Concat, BlockNumberFor<T>, BlockNumberFor<T>>;
assert_eq!(Pallet::<Runtime>::generic_data2(5), None);
GenericData2::<Runtime>::insert(5, 5);
@@ -1018,12 +1015,8 @@ pub mod tests {
/// Some random docs that ensure that docs are accepted
#[crate::storage_alias]
pub type GenericData<T> = StorageMap<
Test2,
Blake2_128Concat,
<T as Config>::BlockNumber,
<T as Config>::BlockNumber,
>;
pub type GenericData<T> =
StorageMap<Test2, Blake2_128Concat, BlockNumberFor<T>, BlockNumberFor<T>>;
});
}
+2 -5
View File
@@ -257,12 +257,9 @@ pub fn migrate_from_pallet_version_to_storage_version<
/// # Examples:
/// ```ignore
/// construct_runtime! {
/// pub enum Runtime where
/// Block = Block,
/// NodeBlock = primitives::Block,
/// UncheckedExtrinsic = UncheckedExtrinsic
/// pub enum Runtime
/// {
/// System: frame_system::{Pallet, Call, Storage, Config, Event<T>} = 0,
/// System: frame_system::{Pallet, Call, Storage, Config<T>, Event<T>} = 0,
///
/// SomePalletToRemove: pallet_something::{Pallet, Call, Storage, Event<T>} = 1,
/// AnotherPalletToRemove: pallet_something_else::{Pallet, Call, Storage, Event<T>} = 2,
@@ -58,7 +58,7 @@ mod tests {
#[pallet::config]
#[pallet::disable_frame_system_supertrait_check]
pub trait Config: 'static {
type BlockNumber;
type Block: sp_runtime::traits::Block;
type AccountId;
type BaseCallFilter: crate::traits::Contains<Self::RuntimeCall>;
type RuntimeOrigin;
@@ -102,6 +102,11 @@ mod tests {
pub mod pallet_prelude {
pub type OriginFor<T> = <T as super::Config>::RuntimeOrigin;
pub type HeaderFor<T> =
<<T as super::Config>::Block as sp_runtime::traits::HeaderProvider>::HeaderT;
pub type BlockNumberFor<T> = <HeaderFor<T> as sp_runtime::traits::Header>::Number;
}
}
@@ -113,18 +118,14 @@ mod tests {
crate::construct_runtime!(
pub enum Runtime
where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: self::frame_system,
}
);
impl self::frame_system::Config for Runtime {
type BlockNumber = BlockNumber;
type AccountId = AccountId;
type Block = Block;
type BaseCallFilter = crate::traits::Everything;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;