Add on_idle hook (#8209)

* add in idle hook

* remaining weight passed through to on_idle

* added weight return

* remove TODO

* weight adjustment fix

* added adjusted weight into tuple

* Update frame/support/src/dispatch.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update frame/support/src/dispatch.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update frame/support/src/dispatch.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update frame/support/src/dispatch.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update frame/support/src/dispatch.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update frame/support/src/dispatch.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update frame/support/src/dispatch.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* compile errors for on_idle in dispatch

* Update frame/support/src/dispatch.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update frame/support/src/dispatch.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update frame/support/src/dispatch.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* on idle tuple clean up

* register reduced weight

* collect and add reduced wait from on idle call

* better demo example

* Update frame/support/procedural/src/pallet/expand/hooks.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* added tests to dispatch.rs

* idle test on executive

* skip on idle if remaining weight is 0

* Update frame/executive/src/lib.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* Update frame/support/src/dispatch.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* abstract common logic out to functions

* docs

* remove demo example

* remove debug

* spacing

* docs

* revert template pallet to master

* change reduced weight to used weight

* remove empty line

* lint

* spacing

* Update frame/support/src/traits.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* documentation

* Update frame/support/procedural/src/pallet/expand/hooks.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* docs

* Update frame/support/src/traits.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* docs

* Update frame/support/src/traits.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* Update frame/support/src/traits.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* Update frame/support/src/traits.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
JesseAbram
2021-03-11 18:04:14 +01:00
committed by GitHub
parent fb4b9755bb
commit c06e556906
5 changed files with 277 additions and 11 deletions
+39 -7
View File
@@ -119,7 +119,7 @@
use sp_std::{prelude::*, marker::PhantomData};
use frame_support::{
weights::{GetDispatchInfo, DispatchInfo, DispatchClass},
traits::{OnInitialize, OnFinalize, OnRuntimeUpgrade, OffchainWorker, ExecuteBlock},
traits::{OnInitialize, OnIdle, OnFinalize, OnRuntimeUpgrade, OffchainWorker, ExecuteBlock},
dispatch::PostDispatchInfo,
};
use sp_runtime::{
@@ -160,6 +160,7 @@ impl<
AllModules:
OnRuntimeUpgrade +
OnInitialize<System::BlockNumber> +
OnIdle<System::BlockNumber> +
OnFinalize<System::BlockNumber> +
OffchainWorker<System::BlockNumber>,
COnRuntimeUpgrade: OnRuntimeUpgrade,
@@ -186,6 +187,7 @@ impl<
UnsignedValidator,
AllModules: OnRuntimeUpgrade
+ OnInitialize<System::BlockNumber>
+ OnIdle<System::BlockNumber>
+ OnFinalize<System::BlockNumber>
+ OffchainWorker<System::BlockNumber>,
COnRuntimeUpgrade: OnRuntimeUpgrade,
@@ -349,8 +351,8 @@ where
// post-extrinsics book-keeping
<frame_system::Module<System>>::note_finished_extrinsics();
<frame_system::Module<System> as OnFinalize<System::BlockNumber>>::on_finalize(block_number);
<AllModules as OnFinalize<System::BlockNumber>>::on_finalize(block_number);
Self::idle_and_finalize_hook(block_number);
}
/// Finalize the block - it is up the caller to ensure that all header fields are valid
@@ -360,12 +362,36 @@ where
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::Module<System> as OnFinalize<System::BlockNumber>>::on_finalize(block_number);
<AllModules as OnFinalize<System::BlockNumber>>::on_finalize(block_number);
Self::idle_and_finalize_hook(block_number);
<frame_system::Module<System>>::finalize()
}
fn idle_and_finalize_hook(block_number: NumberFor<Block>) {
let weight = <frame_system::Module<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(
block_number,
remaining_weight
);
remaining_weight = remaining_weight.saturating_sub(used_weight);
used_weight = <AllModules 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::Module<System> as OnFinalize<System::BlockNumber>>::on_finalize(block_number);
<AllModules as OnFinalize<System::BlockNumber>>::on_finalize(block_number);
}
/// Apply extrinsic outside of the block execution function.
///
/// This doesn't attempt to validate anything regarding the block, but it builds a list of uxt
@@ -555,6 +581,11 @@ mod tests {
175
}
fn on_idle(n: T::BlockNumber, remaining_weight: Weight) -> Weight {
println!("on_idle{}, {})", n, remaining_weight);
175
}
fn on_finalize() {
println!("on_finalize(?)");
}
@@ -769,7 +800,7 @@ mod tests {
header: Header {
parent_hash: [69u8; 32].into(),
number: 1,
state_root: hex!("2c01e6f33d595793119823478b45b36978a8f65a731b5ae3fdfb6330b4cd4b11").into(),
state_root: hex!("6e70de4fa07bac443dc7f8a812c8a0c941aacfa892bb373c5899f7d511d4c25b").into(),
extrinsics_root: hex!("03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314").into(),
digest: Digest { logs: vec![], },
},
@@ -1006,10 +1037,11 @@ mod tests {
new_test_ext(1).execute_with(|| {
Executive::initialize_block(&Header::new_from_number(1));
Executive::finalize_block();
// 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 + 10);
assert_eq!(<frame_system::Module<Runtime>>::block_weight().total(), 175 + 175 + 10);
})
}