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
+46 -2
View File
@@ -1522,6 +1522,38 @@ pub trait OnFinalize<BlockNumber> {
fn on_finalize(_n: BlockNumber) {}
}
/// The block's on idle trait.
///
/// Implementing this lets you express what should happen for your pallet before
/// block finalization (see `on_finalize` hook) in case any remaining weight is left.
pub trait OnIdle<BlockNumber> {
/// The block is being finalized.
/// Implement to have something happen in case there is leftover weight.
/// Check the passed `remaining_weight` to make sure it is high enough to allow for
/// your pallet's extra computation.
///
/// NOTE: This function is called AFTER ALL extrinsics - including inherent extrinsics -
/// in a block are applied but before `on_finalize` is executed.
fn on_idle(
_n: BlockNumber,
_remaining_weight: crate::weights::Weight
) -> crate::weights::Weight {
0
}
}
#[impl_for_tuples(30)]
impl<BlockNumber: Clone> OnIdle<BlockNumber> for Tuple {
fn on_idle(n: BlockNumber, remaining_weight: crate::weights::Weight) -> crate::weights::Weight {
let mut weight = 0;
for_tuples!( #(
let adjusted_remaining_weight = remaining_weight.saturating_sub(weight);
weight = weight.saturating_add(Tuple::on_idle(n.clone(), adjusted_remaining_weight));
)* );
weight
}
}
/// The block initialization trait.
///
/// Implementing this lets you express what should happen for your pallet when the block is
@@ -1539,9 +1571,9 @@ pub trait OnInitialize<BlockNumber> {
#[impl_for_tuples(30)]
impl<BlockNumber: Clone> OnInitialize<BlockNumber> for Tuple {
fn on_initialize(_n: BlockNumber) -> crate::weights::Weight {
fn on_initialize(n: BlockNumber) -> crate::weights::Weight {
let mut weight = 0;
for_tuples!( #( weight = weight.saturating_add(Tuple::on_initialize(_n.clone())); )* );
for_tuples!( #( weight = weight.saturating_add(Tuple::on_initialize(n.clone())); )* );
weight
}
}
@@ -2039,6 +2071,18 @@ pub trait Hooks<BlockNumber> {
/// The block is being finalized. Implement to have something happen.
fn on_finalize(_n: BlockNumber) {}
/// This will be run when the block is being finalized (before `on_finalize`).
/// Implement to have something happen using the remaining weight.
/// Will not fire if the remaining weight is 0.
/// Return the weight used, the hook will subtract it from current weight used
/// and pass the result to the next `on_idle` hook if it exists.
fn on_idle(
_n: BlockNumber,
_remaining_weight: crate::weights::Weight
) -> crate::weights::Weight {
0
}
/// The block is being initialized. Implement to have something happen.
///
/// Return the non-negotiable weight consumed in the block.