on_initialize return weight consumed and default cost to default DispatchInfo instead of zero (#5382)

* frame update

* doc

* move offchain worker trait also

* fix weigh merge

* indentation

* reorder for better git diff

* comment

* fix benchmark

* remove test
This commit is contained in:
thiolliere
2020-03-24 19:51:04 +01:00
committed by GitHub
parent 2e558908e6
commit 8a41ac664b
32 changed files with 331 additions and 365 deletions
+109 -165
View File
@@ -25,7 +25,7 @@ pub use frame_metadata::{
};
pub use crate::weights::{
SimpleDispatchInfo, GetDispatchInfo, DispatchInfo, WeighData, ClassifyDispatch,
TransactionPriority, Weight, WeighBlock, PaysFee,
TransactionPriority, Weight, PaysFee,
};
pub use sp_runtime::{traits::Dispatchable, DispatchError, DispatchResult};
pub use crate::traits::{CallMetadata, GetCallMetadata, GetCallName};
@@ -200,13 +200,23 @@ impl<T> Parameter for T where T: Codec + EncodeLike + Clone + Eq + fmt::Debug {}
/// 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`](../sp_runtime/traits/trait.OnInitialize.html) trait.
/// 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_finalize`: Executes at the end of a block. Using this function will
/// implement the [`OnFinalize`](../sp_runtime/traits/trait.OnFinalize.html) trait.
/// 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`](../sp_runtime/traits/trait.OffchainWorker.html) trait.
/// [`OffchainWorker`](./traits/trait.OffchainWorker.html) trait.
#[macro_export]
macro_rules! decl_module {
// Entry point #1.
@@ -327,7 +337,7 @@ macro_rules! decl_module {
"`deposit_event` function is reserved and must follow the syntax: `$vis:vis fn deposit_event() = default;`"
);
};
// Add on_finalize, without a given weight.
// Add on_finalize
(@normalize
$(#[$attr:meta])*
pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident$(<I>, I: $instantiable:path $(= $module_default_instance:path)?)?>
@@ -354,7 +364,6 @@ macro_rules! decl_module {
{ $( $on_initialize )* }
{ $( $on_runtime_upgrade )* }
{
#[weight = $crate::dispatch::SimpleDispatchInfo::zero()]
fn on_finalize( $( $param_name : $param ),* ) { $( $impl )* }
}
{ $( $offchain )* }
@@ -364,7 +373,7 @@ macro_rules! decl_module {
$($rest)*
);
};
// Add on_finalize, given weight.
// compile_error on_finalize, given weight removed syntax.
(@normalize
$(#[$attr:meta])*
pub struct $mod_type:ident<$trait_instance:ident: $trait_name:ident$(<I>, I: $instantiable:path $(= $module_default_instance:path)?)?>
@@ -383,26 +392,12 @@ macro_rules! decl_module {
fn on_finalize( $( $param_name:ident : $param:ty ),* $(,)? ) { $( $impl:tt )* }
$($rest:tt)*
) => {
$crate::decl_module!(@normalize
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name$(<I>, I: $instantiable $(= $module_default_instance)?)?>
for enum $call_type where origin: $origin_type, system = $system
{ $( $other_where_bounds )* }
{ $( $deposit_event )* }
{ $( $on_initialize )* }
{ $( $on_runtime_upgrade )* }
{
#[weight = $weight]
fn on_finalize( $( $param_name : $param ),* ) { $( $impl )* }
}
{ $( $offchain )* }
{ $( $constants )* }
{ $( $error_type )* }
[ $( $dispatchables )* ]
$($rest)*
compile_error!(
"`on_finalize` can't be given weight attribute anymore, weight must be returned by \
`on_initialize` or `on_runtime_upgrade` instead"
);
};
// Add on_runtime_upgrade, without a given weight.
// compile_error on_runtime_upgrade, without a given weight removed syntax.
(@normalize
$(#[$attr:meta])*
pub struct $mod_type:ident<
@@ -422,26 +417,11 @@ macro_rules! decl_module {
fn on_runtime_upgrade( $( $param_name:ident : $param:ty ),* $(,)? ) { $( $impl:tt )* }
$($rest:tt)*
) => {
$crate::decl_module!(@normalize
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name$(<I>, I: $instantiable $(= $module_default_instance)?)?>
for enum $call_type where origin: $origin_type, system = $system
{ $( $other_where_bounds )* }
{ $( $deposit_event )* }
{ $( $on_initialize )* }
{
#[weight = $crate::dispatch::SimpleDispatchInfo::zero()]
fn on_runtime_upgrade( $( $param_name : $param ),* ) { $( $impl )* }
}
{ $( $on_finalize )* }
{ $( $offchain )* }
{ $( $constants )* }
{ $( $error_type )* }
[ $( $dispatchables )* ]
$($rest)*
compile_error!(
"`on_runtime_upgrade` must return Weight, signature has changed."
);
};
// Add on_runtime_upgrade, given weight.
// compile_error on_runtime_upgrade, given weight removed syntax.
(@normalize
$(#[$attr:meta])*
pub struct $mod_type:ident<
@@ -461,6 +441,31 @@ macro_rules! decl_module {
#[weight = $weight:expr]
fn on_runtime_upgrade( $( $param_name:ident : $param:ty ),* $(,)? ) { $( $impl:tt )* }
$($rest:tt)*
) => {
compile_error!(
"`on_runtime_upgrade` can't be given weight attribute anymore, weight must be returned \
by the function directly."
);
};
// Add on_runtime_upgrade
(@normalize
$(#[$attr:meta])*
pub struct $mod_type:ident<
$trait_instance:ident: $trait_name:ident$(<I>, I: $instantiable:path $(= $module_default_instance:path)?)?
>
for enum $call_type:ident where origin: $origin_type:ty, system = $system:ident
{ $( $other_where_bounds:tt )* }
{ $( $deposit_event:tt )* }
{ $( $on_initialize:tt )* }
{}
{ $( $on_finalize:tt )* }
{ $( $offchain:tt )* }
{ $( $constants:tt )* }
{ $( $error_type:tt )* }
[ $( $dispatchables:tt )* ]
$(#[doc = $doc_attr:tt])*
fn on_runtime_upgrade( $( $param_name:ident : $param:ty ),* $(,)? ) -> $return:ty { $( $impl:tt )* }
$($rest:tt)*
) => {
$crate::decl_module!(@normalize
$(#[$attr])*
@@ -470,8 +475,7 @@ macro_rules! decl_module {
{ $( $deposit_event )* }
{ $( $on_initialize )* }
{
#[weight = $weight]
fn on_runtime_upgrade( $( $param_name : $param ),* ) { $( $impl )* }
fn on_runtime_upgrade( $( $param_name : $param ),* ) -> $return { $( $impl )* }
}
{ $( $on_finalize )* }
{ $( $offchain )* }
@@ -481,7 +485,7 @@ macro_rules! decl_module {
$($rest)*
);
};
// Add on_initialize, without a given weight.
// compile_error on_initialize, without a given weight removed syntax.
(@normalize
$(#[$attr:meta])*
pub struct $mod_type:ident<
@@ -501,26 +505,11 @@ macro_rules! decl_module {
fn on_initialize( $( $param_name:ident : $param:ty ),* $(,)? ) { $( $impl:tt )* }
$($rest:tt)*
) => {
$crate::decl_module!(@normalize
$(#[$attr])*
pub struct $mod_type<$trait_instance: $trait_name$(<I>, I: $instantiable $(= $module_default_instance)?)?>
for enum $call_type where origin: $origin_type, system = $system
{ $( $other_where_bounds )* }
{ $( $deposit_event )* }
{
#[weight = $crate::dispatch::SimpleDispatchInfo::zero()]
fn on_initialize( $( $param_name : $param ),* ) { $( $impl )* }
}
{ $( $on_runtime_upgrade )* }
{ $( $on_finalize )* }
{ $( $offchain )* }
{ $( $constants )* }
{ $( $error_type )* }
[ $( $dispatchables )* ]
$($rest)*
compile_error!(
"`on_initialize` must return Weight, signature has changed."
);
};
// Add on_initialize, given weight.
// compile_error on_initialize, with given weight removed syntax.
(@normalize
$(#[$attr:meta])*
pub struct $mod_type:ident<
@@ -540,6 +529,31 @@ macro_rules! decl_module {
#[weight = $weight:expr]
fn on_initialize( $( $param_name:ident : $param:ty ),* $(,)? ) { $( $impl:tt )* }
$($rest:tt)*
) => {
compile_error!(
"`on_initialize` can't be given weight attribute anymore, weight must be returned \
by the function directly."
);
};
// Add on_initialize
(@normalize
$(#[$attr:meta])*
pub struct $mod_type:ident<
$trait_instance:ident: $trait_name:ident$(<I>, I: $instantiable:path $(= $module_default_instance:path)?)?
>
for enum $call_type:ident where origin: $origin_type:ty, system = $system:ident
{ $( $other_where_bounds:tt )* }
{ $( $deposit_event:tt )* }
{}
{ $( $on_runtime_upgrade:tt )* }
{ $( $on_finalize:tt )* }
{ $( $offchain:tt )* }
{ $( $constants:tt )* }
{ $( $error_type:tt )* }
[ $( $dispatchables:tt )* ]
$(#[doc = $doc_attr:tt])*
fn on_initialize( $( $param_name:ident : $param:ty ),* $(,)? ) -> $return:ty { $( $impl:tt )* }
$($rest:tt)*
) => {
$crate::decl_module!(@normalize
$(#[$attr])*
@@ -548,8 +562,7 @@ macro_rules! decl_module {
{ $( $other_where_bounds )* }
{ $( $deposit_event )* }
{
#[weight = $weight]
fn on_initialize( $( $param_name : $param ),* ) { $( $impl )* }
fn on_initialize( $( $param_name : $param ),* ) -> $return { $( $impl )* }
}
{ $( $on_runtime_upgrade )* }
{ $( $on_finalize )* }
@@ -965,14 +978,13 @@ macro_rules! decl_module {
(@impl_on_initialize
$module:ident<$trait_instance:ident: $trait_name:ident$(<I>, $instance:ident: $instantiable:path)?>;
{ $( $other_where_bounds:tt )* }
#[weight = $weight:expr]
fn on_initialize() { $( $impl:tt )* }
fn on_initialize() -> $return:ty { $( $impl:tt )* }
) => {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?>
$crate::sp_runtime::traits::OnInitialize<$trait_instance::BlockNumber>
$crate::traits::OnInitialize<$trait_instance::BlockNumber>
for $module<$trait_instance$(, $instance)?> where $( $other_where_bounds )*
{
fn on_initialize(_block_number_not_used: $trait_instance::BlockNumber) {
fn on_initialize(_block_number_not_used: $trait_instance::BlockNumber) -> $return {
use $crate::sp_std::if_std;
if_std! {
use $crate::tracing;
@@ -987,14 +999,13 @@ macro_rules! decl_module {
(@impl_on_initialize
$module:ident<$trait_instance:ident: $trait_name:ident$(<I>, $instance:ident: $instantiable:path)?>;
{ $( $other_where_bounds:tt )* }
#[weight = $weight:expr]
fn on_initialize($param:ident : $param_ty:ty) { $( $impl:tt )* }
fn on_initialize($param:ident : $param_ty:ty) -> $return:ty { $( $impl:tt )* }
) => {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?>
$crate::sp_runtime::traits::OnInitialize<$trait_instance::BlockNumber>
$crate::traits::OnInitialize<$trait_instance::BlockNumber>
for $module<$trait_instance$(, $instance)?> where $( $other_where_bounds )*
{
fn on_initialize($param: $param_ty) {
fn on_initialize($param: $param_ty) -> $return {
use $crate::sp_std::if_std;
if_std! {
use $crate::tracing;
@@ -1011,7 +1022,7 @@ macro_rules! decl_module {
{ $( $other_where_bounds:tt )* }
) => {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?>
$crate::sp_runtime::traits::OnInitialize<$trait_instance::BlockNumber>
$crate::traits::OnInitialize<$trait_instance::BlockNumber>
for $module<$trait_instance$(, $instance)?> where $( $other_where_bounds )*
{}
};
@@ -1019,14 +1030,13 @@ macro_rules! decl_module {
(@impl_on_runtime_upgrade
$module:ident<$trait_instance:ident: $trait_name:ident$(<I>, $instance:ident: $instantiable:path)?>;
{ $( $other_where_bounds:tt )* }
#[weight = $weight:expr]
fn on_runtime_upgrade() { $( $impl:tt )* }
fn on_runtime_upgrade() -> $return:ty { $( $impl:tt )* }
) => {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?>
$crate::sp_runtime::traits::OnRuntimeUpgrade
$crate::traits::OnRuntimeUpgrade
for $module<$trait_instance$(, $instance)?> where $( $other_where_bounds )*
{
fn on_runtime_upgrade() {
fn on_runtime_upgrade() -> $return {
use $crate::sp_std::if_std;
if_std! {
use $crate::tracing;
@@ -1043,7 +1053,7 @@ macro_rules! decl_module {
{ $( $other_where_bounds:tt )* }
) => {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?>
$crate::sp_runtime::traits::OnRuntimeUpgrade
$crate::traits::OnRuntimeUpgrade
for $module<$trait_instance$(, $instance)?> where $( $other_where_bounds )*
{}
};
@@ -1052,11 +1062,10 @@ macro_rules! decl_module {
(@impl_on_finalize
$module:ident<$trait_instance:ident: $trait_name:ident$(<I>, $instance:ident: $instantiable:path)?>;
{ $( $other_where_bounds:tt )* }
#[weight = $weight:expr]
fn on_finalize() { $( $impl:tt )* }
) => {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?>
$crate::sp_runtime::traits::OnFinalize<$trait_instance::BlockNumber>
$crate::traits::OnFinalize<$trait_instance::BlockNumber>
for $module<$trait_instance$(, $instance)?> where $( $other_where_bounds )*
{
fn on_finalize(_block_number_not_used: $trait_instance::BlockNumber) {
@@ -1074,11 +1083,10 @@ macro_rules! decl_module {
(@impl_on_finalize
$module:ident<$trait_instance:ident: $trait_name:ident$(<I>, $instance:ident: $instantiable:path)?>;
{ $( $other_where_bounds:tt )* }
#[weight = $weight:expr]
fn on_finalize($param:ident : $param_ty:ty) { $( $impl:tt )* }
) => {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?>
$crate::sp_runtime::traits::OnFinalize<$trait_instance::BlockNumber>
$crate::traits::OnFinalize<$trait_instance::BlockNumber>
for $module<$trait_instance$(, $instance)?> where $( $other_where_bounds )*
{
fn on_finalize($param: $param_ty) {
@@ -1098,57 +1106,19 @@ macro_rules! decl_module {
{ $( $other_where_bounds:tt )* }
) => {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?>
$crate::sp_runtime::traits::OnFinalize<$trait_instance::BlockNumber>
$crate::traits::OnFinalize<$trait_instance::BlockNumber>
for $module<$trait_instance$(, $instance)?> where $( $other_where_bounds )*
{
}
};
(@impl_block_hooks_weight
$module:ident<$trait_instance:ident: $trait_name:ident$(<I>, $instance:ident: $instantiable:path)?>;
{ $( $other_where_bounds:tt )* }
@runtime_upgrade $(
#[weight = $weight_runtime_update:expr]
fn on_runtime_upgrade($( $param_runtime_upgrade:ident : $param_ty_runtime_upgrade:ty )*) { $( $impl_runtime_upgrade:tt )* }
)?
@init $(
#[weight = $weight_initialize:expr]
fn on_initialize($( $param_initialize:ident : $param_ty_initialize:ty )*) { $( $impl_initialize:tt )* }
)?
@fin $(
#[weight = $weight_finalize:expr]
fn on_finalize($( $param_finalize:ident : $param_ty_finalize:ty )*) { $( $impl_finalize:tt )* }
)?
) => {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?>
$crate::dispatch::WeighBlock<$trait_instance::BlockNumber> for $module<$trait_instance$(, $instance)?> where
$( $other_where_bounds )*
{
$(
fn on_runtime_upgrade() -> $crate::dispatch::Weight {
<dyn $crate::dispatch::WeighData<()>>::weigh_data(&$weight_initialize, ())
}
)?
$(
fn on_initialize(n: $trait_instance::BlockNumber) -> $crate::dispatch::Weight {
<dyn $crate::dispatch::WeighData<$trait_instance::BlockNumber>>::weigh_data(&$weight_initialize, n)
}
)?
$(
fn on_finalize(n: $trait_instance::BlockNumber) -> $crate::dispatch::Weight {
<dyn $crate::dispatch::WeighData<$trait_instance::BlockNumber>>::weigh_data(&$weight_finalize, n)
}
)?
}
};
(@impl_offchain
$module:ident<$trait_instance:ident: $trait_name:ident$(<I>, $instance:ident: $instantiable:path)?>;
{ $( $other_where_bounds:tt )* }
fn offchain_worker() { $( $impl:tt )* }
) => {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?>
$crate::sp_runtime::traits::OffchainWorker<$trait_instance::BlockNumber>
$crate::traits::OffchainWorker<$trait_instance::BlockNumber>
for $module<$trait_instance$(, $instance)?> where $( $other_where_bounds )*
{
fn offchain_worker(_block_number_not_used: $trait_instance::BlockNumber) { $( $impl )* }
@@ -1161,7 +1131,7 @@ macro_rules! decl_module {
fn offchain_worker($param:ident : $param_ty:ty) { $( $impl:tt )* }
) => {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?>
$crate::sp_runtime::traits::OffchainWorker<$trait_instance::BlockNumber>
$crate::traits::OffchainWorker<$trait_instance::BlockNumber>
for $module<$trait_instance$(, $instance)?> where $( $other_where_bounds )*
{
fn offchain_worker($param: $param_ty) { $( $impl )* }
@@ -1173,7 +1143,7 @@ macro_rules! decl_module {
{ $( $other_where_bounds:tt )* }
) => {
impl<$trait_instance: $trait_name$(<I>, $instance: $instantiable)?>
$crate::sp_runtime::traits::OffchainWorker<$trait_instance::BlockNumber>
$crate::traits::OffchainWorker<$trait_instance::BlockNumber>
for $module<$trait_instance$(, $instance)?> where $( $other_where_bounds )*
{}
};
@@ -1402,15 +1372,6 @@ macro_rules! decl_module {
$( $on_finalize )*
}
$crate::decl_module! {
@impl_block_hooks_weight
$mod_type<$trait_instance: $trait_name $(<I>, $instance: $instantiable)?>;
{ $( $other_where_bounds )* }
@runtime_upgrade $( $on_runtime_upgrade )*
@init $( $on_initialize )*
@fin $( $on_finalize )*
}
$crate::decl_module! {
@impl_offchain
$mod_type<$trait_instance: $trait_name $(<I>, $instance: $instantiable)?>;
@@ -2076,9 +2037,10 @@ macro_rules! __check_reserved_fn_name {
#[allow(dead_code)]
mod tests {
use super::*;
use crate::sp_runtime::traits::{OnInitialize, OnFinalize, OnRuntimeUpgrade};
use crate::weights::{DispatchInfo, DispatchClass};
use crate::traits::{CallMetadata, GetCallMetadata, GetCallName};
use crate::traits::{
CallMetadata, GetCallMetadata, GetCallName, OnInitialize, OnFinalize, OnRuntimeUpgrade
};
pub trait Trait: system::Trait + Sized where Self::AccountId: From<u32> {
type Origin;
@@ -2098,14 +2060,6 @@ mod tests {
}
}
struct BlockWeight;
impl<BlockNumber: Into<u32>> WeighData<BlockNumber> for BlockWeight {
fn weigh_data(&self, target: BlockNumber) -> Weight {
let target: u32 = target.into();
if target % 2 == 0 { 10 } else { 0 }
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, T::AccountId: From<u32> {
/// Hi, this is a comment.
@@ -2117,12 +2071,9 @@ mod tests {
fn aux_4(_origin, _data: i32) -> DispatchResult { unreachable!() }
fn aux_5(_origin, _data: i32, #[compact] _data2: u32,) -> DispatchResult { unreachable!() }
#[weight = SimpleDispatchInfo::FixedNormal(7)]
fn on_initialize(n: T::BlockNumber,) { if n.into() == 42 { panic!("on_initialize") } }
#[weight = BlockWeight]
fn on_finalize(n: T::BlockNumber) { if n.into() == 42 { panic!("on_finalize") } }
#[weight = SimpleDispatchInfo::FixedOperational(69)]
fn on_runtime_upgrade() { }
fn on_initialize(n: T::BlockNumber,) -> Weight { if n.into() == 42 { panic!("on_initialize") } 7 }
fn on_finalize(n: T::BlockNumber,) { if n.into() == 42 { panic!("on_finalize") } }
fn on_runtime_upgrade() -> Weight { 10 }
fn offchain_worker() {}
#[weight = SimpleDispatchInfo::FixedOperational(5)]
@@ -2254,10 +2205,15 @@ mod tests {
#[test]
#[should_panic(expected = "on_initialize")]
fn on_initialize_should_work() {
fn on_initialize_should_work_1() {
<Module<TraitImpl> as OnInitialize<u32>>::on_initialize(42);
}
#[test]
fn on_initialize_should_work_2() {
assert_eq!(<Module<TraitImpl> as OnInitialize<u32>>::on_initialize(10), 7);
}
#[test]
#[should_panic(expected = "on_finalize")]
fn on_finalize_should_work() {
@@ -2266,7 +2222,7 @@ mod tests {
#[test]
fn on_runtime_upgrade_should_work() {
<Module<TraitImpl> as OnRuntimeUpgrade>::on_runtime_upgrade();
assert_eq!(<Module<TraitImpl> as OnRuntimeUpgrade>::on_runtime_upgrade(), 10);
}
#[test]
@@ -2288,18 +2244,6 @@ mod tests {
);
}
#[test]
fn weight_for_block_hooks() {
// independent of block number
assert_eq!(<Test as WeighBlock<u32>>::on_initialize(0), 7);
assert_eq!(<Test as WeighBlock<u32>>::on_initialize(10), 7);
assert_eq!(<Test as WeighBlock<u32>>::on_initialize(100), 7);
// dependent
assert_eq!(<Test as WeighBlock<u32>>::on_finalize(2), 10);
assert_eq!(<Test as WeighBlock<u32>>::on_finalize(3), 0);
}
#[test]
fn call_name() {
let name = Call::<TraitImpl>::aux_3().get_call_name();
+91 -2
View File
@@ -28,6 +28,7 @@ use sp_runtime::{
};
use crate::dispatch::Parameter;
use crate::storage::StorageMap;
use impl_trait_for_tuples::impl_for_tuples;
/// An abstraction of a value stored within storage, but possibly as part of a larger composite
/// item.
@@ -193,14 +194,14 @@ impl<AccountId> IsDeadAccount<AccountId> for () {
}
/// Handler for when a new account has been created.
#[impl_trait_for_tuples::impl_for_tuples(30)]
#[impl_for_tuples(30)]
pub trait OnNewAccount<AccountId> {
/// A new account `who` has been registered.
fn on_new_account(who: &AccountId);
}
/// The account with the given id was reaped.
#[impl_trait_for_tuples::impl_for_tuples(30)]
#[impl_for_tuples(30)]
pub trait OnKilledAccount<AccountId> {
/// The account with the given id was reaped.
fn on_killed_account(who: &AccountId);
@@ -1042,3 +1043,91 @@ pub trait GetCallMetadata {
/// Return a [`CallMetadata`], containing function and pallet name of the Call.
fn get_call_metadata(&self) -> CallMetadata;
}
/// The block finalization trait. Implementing this lets you express what should happen
/// for your module when the block is ending.
#[impl_for_tuples(30)]
pub trait OnFinalize<BlockNumber> {
/// The block is being finalized. Implement to have something happen.
fn on_finalize(_n: BlockNumber) {}
}
/// The block initialization trait. Implementing this lets you express what should happen
/// for your module when the block is beginning (right before the first extrinsic is executed).
pub trait OnInitialize<BlockNumber> {
/// The block is being initialized. Implement to have something happen.
///
/// Return the non-negotiable weight consumed in the block.
fn on_initialize(_n: BlockNumber) -> crate::weights::Weight { 0 }
}
#[impl_for_tuples(30)]
impl<BlockNumber: Clone> OnInitialize<BlockNumber> for Tuple {
fn on_initialize(_n: BlockNumber) -> crate::weights::Weight {
let mut weight = 0;
for_tuples!( #( weight = weight.saturating_add(Tuple::on_initialize(_n.clone())); )* );
weight
}
}
/// The runtime upgrade trait. Implementing this lets you express what should happen
/// when the runtime upgrades, and changes may need to occur to your module.
pub trait OnRuntimeUpgrade {
/// Perform a module upgrade.
///
/// Return the non-negotiable weight consumed for runtime upgrade.
fn on_runtime_upgrade() -> crate::weights::Weight { 0 }
}
#[impl_for_tuples(30)]
impl OnRuntimeUpgrade for Tuple {
fn on_runtime_upgrade() -> crate::weights::Weight {
let mut weight = 0;
for_tuples!( #( weight = weight.saturating_add(Tuple::on_runtime_upgrade()); )* );
weight
}
}
/// Off-chain computation trait.
///
/// Implementing this trait on a module allows you to perform long-running tasks
/// that make (by default) validators generate transactions that feed results
/// of those long-running computations back on chain.
///
/// NOTE: This function runs off-chain, so it can access the block state,
/// but cannot preform any alterations. More specifically alterations are
/// not forbidden, but they are not persisted in any way after the worker
/// has finished.
#[impl_for_tuples(30)]
pub trait OffchainWorker<BlockNumber> {
/// This function is being called after every block import (when fully synced).
///
/// Implement this and use any of the `Offchain` `sp_io` set of APIs
/// to perform off-chain computations, calls and submit transactions
/// with results to trigger any on-chain changes.
/// Any state alterations are lost and are not persisted.
fn offchain_worker(_n: BlockNumber) {}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn on_initialize_and_on_runtime_upgrade_weight_merge_works() {
struct Test;
impl OnInitialize<u8> for Test {
fn on_initialize(_n: u8) -> crate::weights::Weight {
10
}
}
impl OnRuntimeUpgrade for Test {
fn on_runtime_upgrade() -> crate::weights::Weight {
20
}
}
assert_eq!(<(Test, Test)>::on_initialize(0), 20);
assert_eq!(<(Test, Test)>::on_runtime_upgrade(), 40);
}
}
+1 -41
View File
@@ -37,9 +37,8 @@
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
use impl_trait_for_tuples::impl_for_tuples;
use codec::{Encode, Decode};
use sp_arithmetic::traits::{Bounded, Zero};
use sp_arithmetic::traits::Bounded;
use sp_runtime::{
RuntimeDebug,
traits::SignedExtension,
@@ -67,17 +66,6 @@ pub trait ClassifyDispatch<T> {
fn classify_dispatch(&self, target: T) -> DispatchClass;
}
/// Means of determining the weight of a block's life cycle hooks: `on_initialize`, `on_finalize`,
/// `on_runtime_upgrade`, and such.
pub trait WeighBlock<BlockNumber> {
/// Return the weight of the block's on_runtime_upgrade hook.
fn on_runtime_upgrade() -> Weight { Zero::zero() }
/// Return the weight of the block's on_initialize hook.
fn on_initialize(_: BlockNumber) -> Weight { Zero::zero() }
/// Return the weight of the block's on_finalize hook.
fn on_finalize(_: BlockNumber) -> Weight { Zero::zero() }
}
/// Indicates if dispatch function should pay fees or not.
/// If set to false, the block resource limits are applied, yet no fee is deducted.
pub trait PaysFee<T> {
@@ -86,34 +74,6 @@ pub trait PaysFee<T> {
}
}
/// Maybe I can do something to remove the duplicate code here.
#[impl_for_tuples(30)]
impl<BlockNumber: Copy> WeighBlock<BlockNumber> for SingleModule {
fn on_runtime_upgrade() -> Weight {
let mut accumulated_weight: Weight = Zero::zero();
for_tuples!(
#( accumulated_weight = accumulated_weight.saturating_add(SingleModule::on_runtime_upgrade()); )*
);
accumulated_weight
}
fn on_initialize(n: BlockNumber) -> Weight {
let mut accumulated_weight: Weight = Zero::zero();
for_tuples!(
#( accumulated_weight = accumulated_weight.saturating_add(SingleModule::on_initialize(n)); )*
);
accumulated_weight
}
fn on_finalize(n: BlockNumber) -> Weight {
let mut accumulated_weight: Weight = Zero::zero();
for_tuples!(
#( accumulated_weight = accumulated_weight.saturating_add(SingleModule::on_finalize(n)); )*
);
accumulated_weight
}
}
/// A generalized group of dispatch types. This is only distinguishing normal, user-triggered transactions
/// (`Normal`) and anything beyond which serves a higher purpose to the system (`Operational`).
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]