Replace 'Module' with 'Pallet' in construct_runtime macro (#8372)

* Use 'Pallet' struct in construct_runtime.

* Fix genesis and metadata macro.

* Fix 'Pallet' type alias.

* Replace 'Module' with 'Pallet' for all construct_runtime use cases.

* Replace more deprecated 'Module' struct.

* Bring back AllModules and AllPalletsWithSystem type, but deprecate them.

* Replace deprecated 'Module' struct from merge master.

* Minor fix.

* Fix UI tests.

* Revert UI override in derive_no_bound.

* Fix more deprecated 'Module' use from master branch.

* Fix more deprecated 'Module' use from master branch.
This commit is contained in:
Shaun Wang
2021-03-18 21:50:08 +13:00
committed by GitHub
parent 05f24931a9
commit 2e5522444a
157 changed files with 881 additions and 864 deletions
+64 -64
View File
@@ -59,7 +59,7 @@
//! # type Context = frame_system::ChainContext<Runtime>;
//! # pub type Block = generic::Block<Header, UncheckedExtrinsic>;
//! # pub type Balances = u64;
//! # pub type AllModules = u64;
//! # pub type AllPallets = u64;
//! # pub enum Runtime {};
//! # use sp_runtime::transaction_validity::{
//! # TransactionValidity, UnknownTransaction, TransactionSource,
@@ -73,7 +73,7 @@
//! # }
//! # }
//! /// Executive: handles dispatch to the various modules.
//! pub type Executive = executive::Executive<Runtime, Block, Context, Runtime, AllModules>;
//! pub type Executive = executive::Executive<Runtime, Block, Context, Runtime, AllPallets>;
//! ```
//!
//! ### Custom `OnRuntimeUpgrade` logic
@@ -90,7 +90,7 @@
//! # type Context = frame_system::ChainContext<Runtime>;
//! # pub type Block = generic::Block<Header, UncheckedExtrinsic>;
//! # pub type Balances = u64;
//! # pub type AllModules = u64;
//! # pub type AllPallets = u64;
//! # pub enum Runtime {};
//! # use sp_runtime::transaction_validity::{
//! # TransactionValidity, UnknownTransaction, TransactionSource,
@@ -111,7 +111,7 @@
//! }
//! }
//!
//! pub type Executive = executive::Executive<Runtime, Block, Context, Runtime, AllModules, CustomOnRuntimeUpgrade>;
//! pub type Executive = executive::Executive<Runtime, Block, Context, Runtime, AllPallets, CustomOnRuntimeUpgrade>;
//! ```
#![cfg_attr(not(feature = "std"), no_std)]
@@ -144,12 +144,12 @@ pub type OriginOf<E, C> = <CallOf<E, C> as Dispatchable>::Origin;
/// - `Block`: The block type of the runtime
/// - `Context`: The context that is used when checking an extrinsic.
/// - `UnsignedValidator`: The unsigned transaction validator of the runtime.
/// - `AllModules`: Tuple that contains all modules. Will be used to call e.g. `on_initialize`.
/// - `AllPallets`: Tuple that contains all modules. Will be used to call e.g. `on_initialize`.
/// - `OnRuntimeUpgrade`: Custom logic that should be called after a runtime upgrade. Modules are
/// already called by `AllModules`. It will be called before all modules will
/// already called by `AllPallets`. It will be called before all modules will
/// be called.
pub struct Executive<System, Block, Context, UnsignedValidator, AllModules, OnRuntimeUpgrade = ()>(
PhantomData<(System, Block, Context, UnsignedValidator, AllModules, OnRuntimeUpgrade)>
pub struct Executive<System, Block, Context, UnsignedValidator, AllPallets, OnRuntimeUpgrade = ()>(
PhantomData<(System, Block, Context, UnsignedValidator, AllPallets, OnRuntimeUpgrade)>
);
impl<
@@ -157,7 +157,7 @@ impl<
Block: traits::Block<Header=System::Header, Hash=System::Hash>,
Context: Default,
UnsignedValidator,
AllModules:
AllPallets:
OnRuntimeUpgrade +
OnInitialize<System::BlockNumber> +
OnIdle<System::BlockNumber> +
@@ -165,7 +165,7 @@ impl<
OffchainWorker<System::BlockNumber>,
COnRuntimeUpgrade: OnRuntimeUpgrade,
> ExecuteBlock<Block> for
Executive<System, Block, Context, UnsignedValidator, AllModules, COnRuntimeUpgrade>
Executive<System, Block, Context, UnsignedValidator, AllPallets, COnRuntimeUpgrade>
where
Block::Extrinsic: Checkable<Context> + Codec,
CheckedOf<Block::Extrinsic, Context>:
@@ -176,7 +176,7 @@ where
UnsignedValidator: ValidateUnsigned<Call=CallOf<Block::Extrinsic, Context>>,
{
fn execute_block(block: Block) {
Executive::<System, Block, Context, UnsignedValidator, AllModules>::execute_block(block);
Executive::<System, Block, Context, UnsignedValidator, AllPallets>::execute_block(block);
}
}
@@ -185,13 +185,13 @@ impl<
Block: traits::Block<Header = System::Header, Hash = System::Hash>,
Context: Default,
UnsignedValidator,
AllModules: OnRuntimeUpgrade
AllPallets: OnRuntimeUpgrade
+ OnInitialize<System::BlockNumber>
+ OnIdle<System::BlockNumber>
+ OnFinalize<System::BlockNumber>
+ OffchainWorker<System::BlockNumber>,
COnRuntimeUpgrade: OnRuntimeUpgrade,
> Executive<System, Block, Context, UnsignedValidator, AllModules, COnRuntimeUpgrade>
> Executive<System, Block, Context, UnsignedValidator, AllPallets, COnRuntimeUpgrade>
where
Block::Extrinsic: Checkable<Context> + Codec,
CheckedOf<Block::Extrinsic, Context>: Applyable + GetDispatchInfo,
@@ -204,10 +204,10 @@ where
pub fn execute_on_runtime_upgrade() -> frame_support::weights::Weight {
let mut weight = 0;
weight = weight.saturating_add(
<frame_system::Module<System> as OnRuntimeUpgrade>::on_runtime_upgrade(),
<frame_system::Pallet<System> as OnRuntimeUpgrade>::on_runtime_upgrade(),
);
weight = weight.saturating_add(COnRuntimeUpgrade::on_runtime_upgrade());
weight = weight.saturating_add(<AllModules as OnRuntimeUpgrade>::on_runtime_upgrade());
weight = weight.saturating_add(<AllPallets as OnRuntimeUpgrade>::on_runtime_upgrade());
weight
}
@@ -218,7 +218,7 @@ where
#[cfg(feature = "try-runtime")]
pub fn try_runtime_upgrade() -> Result<frame_support::weights::Weight, &'static str> {
<
(frame_system::Module::<System>, COnRuntimeUpgrade, AllModules)
(frame_system::Pallet::<System>, COnRuntimeUpgrade, AllPallets)
as
OnRuntimeUpgrade
>::pre_upgrade()?;
@@ -226,7 +226,7 @@ where
let weight = Self::execute_on_runtime_upgrade();
<
(frame_system::Module::<System>, COnRuntimeUpgrade, AllModules)
(frame_system::Pallet::<System>, COnRuntimeUpgrade, AllPallets)
as
OnRuntimeUpgrade
>::post_upgrade()?;
@@ -265,24 +265,24 @@ where
if Self::runtime_upgraded() {
weight = weight.saturating_add(Self::execute_on_runtime_upgrade());
}
<frame_system::Module<System>>::initialize(
<frame_system::Pallet<System>>::initialize(
block_number,
parent_hash,
digest,
frame_system::InitKind::Full,
);
weight = weight.saturating_add(
<frame_system::Module<System> as OnInitialize<System::BlockNumber>>::on_initialize(*block_number)
<frame_system::Pallet<System> as OnInitialize<System::BlockNumber>>::on_initialize(*block_number)
);
weight = weight.saturating_add(
<AllModules as OnInitialize<System::BlockNumber>>::on_initialize(*block_number)
<AllPallets as OnInitialize<System::BlockNumber>>::on_initialize(*block_number)
);
weight = weight.saturating_add(
<System::BlockWeights as frame_support::traits::Get<_>>::get().base_block
);
<frame_system::Module::<System>>::register_extra_weight_unchecked(weight, DispatchClass::Mandatory);
<frame_system::Pallet::<System>>::register_extra_weight_unchecked(weight, DispatchClass::Mandatory);
frame_system::Module::<System>::note_finished_initialize();
frame_system::Pallet::<System>::note_finished_initialize();
}
/// Returns if the runtime was upgraded since the last time this function was called.
@@ -308,7 +308,7 @@ where
let n = header.number().clone();
assert!(
n > System::BlockNumber::zero()
&& <frame_system::Module<System>>::block_hash(n - System::BlockNumber::one()) == *header.parent_hash(),
&& <frame_system::Pallet<System>>::block_hash(n - System::BlockNumber::one()) == *header.parent_hash(),
"Parent hash should be valid.",
);
}
@@ -350,7 +350,7 @@ where
});
// post-extrinsics book-keeping
<frame_system::Module<System>>::note_finished_extrinsics();
<frame_system::Pallet<System>>::note_finished_extrinsics();
Self::idle_and_finalize_hook(block_number);
}
@@ -360,36 +360,36 @@ where
pub fn finalize_block() -> System::Header {
sp_io::init_tracing();
sp_tracing::enter_span!( sp_tracing::Level::TRACE, "finalize_block" );
<frame_system::Module<System>>::note_finished_extrinsics();
let block_number = <frame_system::Module<System>>::block_number();
<frame_system::Pallet<System>>::note_finished_extrinsics();
let block_number = <frame_system::Pallet<System>>::block_number();
Self::idle_and_finalize_hook(block_number);
<frame_system::Module<System>>::finalize()
<frame_system::Pallet<System>>::finalize()
}
fn idle_and_finalize_hook(block_number: NumberFor<Block>) {
let weight = <frame_system::Module<System>>::block_weight();
let weight = <frame_system::Pallet<System>>::block_weight();
let max_weight = <System::BlockWeights as frame_support::traits::Get<_>>::get().max_block;
let mut remaining_weight = max_weight.saturating_sub(weight.total());
if remaining_weight > 0 {
let mut used_weight =
<frame_system::Module<System> as OnIdle<System::BlockNumber>>::on_idle(
<frame_system::Pallet<System> as OnIdle<System::BlockNumber>>::on_idle(
block_number,
remaining_weight
);
remaining_weight = remaining_weight.saturating_sub(used_weight);
used_weight = <AllModules as OnIdle<System::BlockNumber>>::on_idle(
used_weight = <AllPallets as OnIdle<System::BlockNumber>>::on_idle(
block_number,
remaining_weight
)
.saturating_add(used_weight);
<frame_system::Module::<System>>::register_extra_weight_unchecked(used_weight, DispatchClass::Mandatory);
<frame_system::Pallet::<System>>::register_extra_weight_unchecked(used_weight, DispatchClass::Mandatory);
}
<frame_system::Module<System> as OnFinalize<System::BlockNumber>>::on_finalize(block_number);
<AllModules as OnFinalize<System::BlockNumber>>::on_finalize(block_number);
<frame_system::Pallet<System> as OnFinalize<System::BlockNumber>>::on_finalize(block_number);
<AllPallets as OnFinalize<System::BlockNumber>>::on_finalize(block_number);
}
/// Apply extrinsic outside of the block execution function.
@@ -419,7 +419,7 @@ where
// We don't need to make sure to `note_extrinsic` only after we know it's going to be
// executed to prevent it from leaking in storage since at this point, it will either
// execute or panic (and revert storage changes).
<frame_system::Module<System>>::note_extrinsic(to_note);
<frame_system::Pallet<System>>::note_extrinsic(to_note);
// AUDIT: Under no circumstances may this function panic from here onwards.
@@ -427,7 +427,7 @@ where
let dispatch_info = xt.get_dispatch_info();
let r = Applyable::apply::<UnsignedValidator>(xt, &dispatch_info, encoded_len)?;
<frame_system::Module<System>>::note_applied_extrinsic(&r, dispatch_info);
<frame_system::Pallet<System>>::note_applied_extrinsic(&r, dispatch_info);
Ok(r.map(|_| ()).map_err(|e| e.error))
}
@@ -435,7 +435,7 @@ where
fn final_checks(header: &System::Header) {
sp_tracing::enter_span!(sp_tracing::Level::TRACE, "final_checks");
// remove temporaries
let new_header = <frame_system::Module<System>>::finalize();
let new_header = <frame_system::Pallet<System>>::finalize();
// check digest
assert_eq!(
@@ -499,7 +499,7 @@ where
// OffchainWorker RuntimeApi should skip initialization.
let digests = header.digest().clone();
<frame_system::Module<System>>::initialize(
<frame_system::Pallet<System>>::initialize(
header.number(),
header.parent_hash(),
&digests,
@@ -511,7 +511,7 @@ where
// as well.
frame_system::BlockHash::<System>::insert(header.number(), header.hash());
<AllModules as OffchainWorker<System::BlockNumber>>::offchain_worker(*header.number())
<AllPallets as OffchainWorker<System::BlockNumber>>::offchain_worker(*header.number())
}
}
@@ -628,9 +628,9 @@ mod tests {
NodeBlock = TestBlock,
UncheckedExtrinsic = TestUncheckedExtrinsic
{
System: frame_system::{Module, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Module, Call, Storage, Config<T>, Event<T>},
Custom: custom::{Module, Call, ValidateUnsigned},
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
Custom: custom::{Pallet, Call, ValidateUnsigned},
}
);
@@ -741,7 +741,7 @@ mod tests {
Block<TestXt>,
ChainContext<Runtime>,
Runtime,
AllModules,
AllPallets,
CustomOnRuntimeUpgrade
>;
@@ -780,8 +780,8 @@ mod tests {
));
let r = Executive::apply_extrinsic(xt);
assert!(r.is_ok());
assert_eq!(<pallet_balances::Module<Runtime>>::total_balance(&1), 142 - fee);
assert_eq!(<pallet_balances::Module<Runtime>>::total_balance(&2), 69);
assert_eq!(<pallet_balances::Pallet<Runtime>>::total_balance(&1), 142 - fee);
assert_eq!(<pallet_balances::Pallet<Runtime>>::total_balance(&2), 69);
});
}
@@ -857,7 +857,7 @@ mod tests {
Digest::default(),
));
assert!(Executive::apply_extrinsic(xt).is_err());
assert_eq!(<frame_system::Module<Runtime>>::extrinsic_index(), Some(0));
assert_eq!(<frame_system::Pallet<Runtime>>::extrinsic_index(), Some(0));
});
}
@@ -883,7 +883,7 @@ mod tests {
Digest::default(),
));
// Base block execution weight + `on_initialize` weight from the custom module.
assert_eq!(<frame_system::Module<Runtime>>::block_weight().total(), base_block_weight);
assert_eq!(<frame_system::Pallet<Runtime>>::block_weight().total(), base_block_weight);
for nonce in 0..=num_to_exhaust_block {
let xt = TestXt::new(
@@ -893,11 +893,11 @@ mod tests {
if nonce != num_to_exhaust_block {
assert!(res.is_ok());
assert_eq!(
<frame_system::Module<Runtime>>::block_weight().total(),
<frame_system::Pallet<Runtime>>::block_weight().total(),
//--------------------- on_initialize + block_execution + extrinsic_base weight
(encoded_len + 5) * (nonce + 1) + base_block_weight,
);
assert_eq!(<frame_system::Module<Runtime>>::extrinsic_index(), Some(nonce as u32 + 1));
assert_eq!(<frame_system::Pallet<Runtime>>::extrinsic_index(), Some(nonce as u32 + 1));
} else {
assert_eq!(res, Err(InvalidTransaction::ExhaustsResources.into()));
}
@@ -924,8 +924,8 @@ mod tests {
Digest::default(),
));
assert_eq!(<frame_system::Module<Runtime>>::block_weight().total(), base_block_weight);
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_len(), 0);
assert_eq!(<frame_system::Pallet<Runtime>>::block_weight().total(), base_block_weight);
assert_eq!(<frame_system::Pallet<Runtime>>::all_extrinsics_len(), 0);
assert!(Executive::apply_extrinsic(xt.clone()).unwrap().is_ok());
assert!(Executive::apply_extrinsic(x1.clone()).unwrap().is_ok());
@@ -935,14 +935,14 @@ mod tests {
let extrinsic_weight = len as Weight + <Runtime as frame_system::Config>::BlockWeights
::get().get(DispatchClass::Normal).base_extrinsic;
assert_eq!(
<frame_system::Module<Runtime>>::block_weight().total(),
<frame_system::Pallet<Runtime>>::block_weight().total(),
base_block_weight + 3 * extrinsic_weight,
);
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_len(), 3 * len);
assert_eq!(<frame_system::Pallet<Runtime>>::all_extrinsics_len(), 3 * len);
let _ = <frame_system::Module<Runtime>>::finalize();
let _ = <frame_system::Pallet<Runtime>>::finalize();
// All extrinsics length cleaned on `System::finalize`
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_len(), 0);
assert_eq!(<frame_system::Pallet<Runtime>>::all_extrinsics_len(), 0);
// New Block
Executive::initialize_block(&Header::new(
@@ -954,7 +954,7 @@ mod tests {
));
// Block weight cleaned up on `System::initialize`
assert_eq!(<frame_system::Module<Runtime>>::block_weight().total(), base_block_weight);
assert_eq!(<frame_system::Pallet<Runtime>>::block_weight().total(), base_block_weight);
});
}
@@ -989,7 +989,7 @@ mod tests {
let execute_with_lock = |lock: WithdrawReasons| {
let mut t = new_test_ext(1);
t.execute_with(|| {
<pallet_balances::Module<Runtime> as LockableCurrency<Balance>>::set_lock(
<pallet_balances::Pallet<Runtime> as LockableCurrency<Balance>>::set_lock(
id,
&1,
110,
@@ -1017,13 +1017,13 @@ mod tests {
if lock == WithdrawReasons::except(WithdrawReasons::TRANSACTION_PAYMENT) {
assert!(Executive::apply_extrinsic(xt).unwrap().is_ok());
// tx fee has been deducted.
assert_eq!(<pallet_balances::Module<Runtime>>::total_balance(&1), 111 - fee);
assert_eq!(<pallet_balances::Pallet<Runtime>>::total_balance(&1), 111 - fee);
} else {
assert_eq!(
Executive::apply_extrinsic(xt),
Err(InvalidTransaction::Payment.into()),
);
assert_eq!(<pallet_balances::Module<Runtime>>::total_balance(&1), 111);
assert_eq!(<pallet_balances::Pallet<Runtime>>::total_balance(&1), 111);
}
});
};
@@ -1041,7 +1041,7 @@ mod tests {
// NOTE: might need updates over time if new weights are introduced.
// For now it only accounts for the base block execution weight and
// the `on_initialize` weight defined in the custom test module.
assert_eq!(<frame_system::Module<Runtime>>::block_weight().total(), 175 + 175 + 10);
assert_eq!(<frame_system::Pallet<Runtime>>::block_weight().total(), 175 + 175 + 10);
})
}
@@ -1159,16 +1159,16 @@ mod tests {
));
// All weights that show up in the `initialize_block_impl`
let frame_system_upgrade_weight = frame_system::Module::<Runtime>::on_runtime_upgrade();
let frame_system_upgrade_weight = frame_system::Pallet::<Runtime>::on_runtime_upgrade();
let custom_runtime_upgrade_weight = CustomOnRuntimeUpgrade::on_runtime_upgrade();
let runtime_upgrade_weight = <AllModules as OnRuntimeUpgrade>::on_runtime_upgrade();
let frame_system_on_initialize_weight = frame_system::Module::<Runtime>::on_initialize(block_number);
let on_initialize_weight = <AllModules as OnInitialize<u64>>::on_initialize(block_number);
let runtime_upgrade_weight = <AllPallets as OnRuntimeUpgrade>::on_runtime_upgrade();
let frame_system_on_initialize_weight = frame_system::Pallet::<Runtime>::on_initialize(block_number);
let on_initialize_weight = <AllPallets as OnInitialize<u64>>::on_initialize(block_number);
let base_block_weight = <Runtime as frame_system::Config>::BlockWeights::get().base_block;
// Weights are recorded correctly
assert_eq!(
frame_system::Module::<Runtime>::block_weight().total(),
frame_system::Pallet::<Runtime>::block_weight().total(),
frame_system_upgrade_weight +
custom_runtime_upgrade_weight +
runtime_upgrade_weight +