Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Add on_idle hook #8209

Merged
merged 48 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
d49a114
add in idle hook
JesseAbram Feb 26, 2021
7bb95a3
remaining weight passed through to on_idle
JesseAbram Feb 26, 2021
3bf72c5
added weight return
JesseAbram Feb 26, 2021
c4bebca
remove TODO
JesseAbram Feb 26, 2021
ad2adb9
weight adjustment fix
JesseAbram Mar 1, 2021
99a8bce
added adjusted weight into tuple
JesseAbram Mar 1, 2021
848f147
Update frame/support/src/dispatch.rs
JesseAbram Mar 1, 2021
cf010e4
Update frame/support/src/dispatch.rs
JesseAbram Mar 1, 2021
ff4826e
Update frame/support/src/dispatch.rs
JesseAbram Mar 1, 2021
1a41fd1
Update frame/support/src/dispatch.rs
JesseAbram Mar 1, 2021
4879b79
Update frame/support/src/dispatch.rs
JesseAbram Mar 1, 2021
84820f7
Update frame/support/src/dispatch.rs
JesseAbram Mar 1, 2021
4933986
Update frame/support/src/dispatch.rs
JesseAbram Mar 1, 2021
45fb696
compile errors for on_idle in dispatch
JesseAbram Mar 1, 2021
c62f496
Update frame/support/src/dispatch.rs
JesseAbram Mar 1, 2021
5f54ce2
Update frame/support/src/dispatch.rs
JesseAbram Mar 1, 2021
39ba7a9
Update frame/support/src/dispatch.rs
JesseAbram Mar 1, 2021
5e0e9d2
on idle tuple clean up
JesseAbram Mar 1, 2021
423d5bf
register reduced weight
JesseAbram Mar 1, 2021
faeb2ce
collect and add reduced wait from on idle call
JesseAbram Mar 1, 2021
fbc2947
better demo example
JesseAbram Mar 2, 2021
4c49899
Update frame/support/procedural/src/pallet/expand/hooks.rs
JesseAbram Mar 2, 2021
d36b806
added tests to dispatch.rs
JesseAbram Mar 3, 2021
188bbdf
idle test on executive
JesseAbram Mar 3, 2021
a05b383
skip on idle if remaining weight is 0
JesseAbram Mar 4, 2021
fc8ba2b
Update frame/executive/src/lib.rs
JesseAbram Mar 4, 2021
ebb2d98
Update frame/support/src/dispatch.rs
JesseAbram Mar 4, 2021
3501d4d
abstract common logic out to functions
JesseAbram Mar 4, 2021
9e03788
docs
JesseAbram Mar 4, 2021
7f404ca
remove demo example
JesseAbram Mar 4, 2021
4e03e2c
remove debug
JesseAbram Mar 4, 2021
3c30ff3
spacing
JesseAbram Mar 4, 2021
3c76f0b
docs
JesseAbram Mar 4, 2021
b9f7bdf
revert template pallet to master
JesseAbram Mar 5, 2021
ae9a00d
Merge branch 'master' of github.com:paritytech/substrate into jesse-o…
JesseAbram Mar 5, 2021
84ead6f
change reduced weight to used weight
JesseAbram Mar 5, 2021
622fe37
remove empty line
apopiak Mar 5, 2021
7ae5ad6
lint
JesseAbram Mar 5, 2021
d3660cd
spacing
JesseAbram Mar 8, 2021
5d52fba
Update frame/support/src/traits.rs
JesseAbram Mar 8, 2021
ffbc58a
documentation
JesseAbram Mar 8, 2021
17393fc
Update frame/support/procedural/src/pallet/expand/hooks.rs
JesseAbram Mar 9, 2021
4623537
docs
JesseAbram Mar 9, 2021
84c7180
Update frame/support/src/traits.rs
JesseAbram Mar 9, 2021
9386c71
docs
JesseAbram Mar 9, 2021
02a42e3
Update frame/support/src/traits.rs
JesseAbram Mar 11, 2021
b4061e4
Update frame/support/src/traits.rs
JesseAbram Mar 11, 2021
855413b
Update frame/support/src/traits.rs
JesseAbram Mar 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions bin/node-template/pallets/template/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod tests;

#[frame_support::pallet]
pub mod pallet {
use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::*};
use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::*, debug};
use frame_system::pallet_prelude::*;

/// Configure the pallet by specifying the parameters and types on which it depends.
Expand Down Expand Up @@ -46,7 +46,7 @@ pub mod pallet {
/// parameters. [something, who]
SomethingStored(u32, T::AccountId),
}

// Errors inform users that something went wrong.
#[pallet::error]
pub enum Error<T> {
Expand All @@ -57,7 +57,18 @@ pub mod pallet {
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
// TODO remove and add as an example
fn on_idle(block_number: T::BlockNumber, remaining_weight: frame_support::weights::Weight) -> Weight{
// TODO, better example implementation
if remaining_weight > 1994237262000 {
debug::info!("on idle: do something weight :{}", remaining_weight);
} else {
debug::info!("on idle: meh im busy weight :{}", remaining_weight);
}
0
}
}

// Dispatchable functions allows users to interact with the pallet and invoke state changes.
// These functions materialize as "extrinsics", which are often compared to transactions.
Expand Down
22 changes: 20 additions & 2 deletions frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@
use sp_std::{prelude::*, marker::PhantomData};
use frame_support::{
weights::{GetDispatchInfo, DispatchInfo, DispatchClass},
traits::{OnInitialize, OnFinalize, OnRuntimeUpgrade, OffchainWorker},
dispatch::PostDispatchInfo,
traits::{OnInitialize, OnIdle, OnFinalize, OnRuntimeUpgrade, OffchainWorker},
dispatch::PostDispatchInfo
};
use sp_runtime::{
generic::Digest, ApplyExtrinsicResult,
Expand Down Expand Up @@ -166,6 +166,7 @@ impl<
AllModules:
OnRuntimeUpgrade +
OnInitialize<System::BlockNumber> +
OnIdle<System::BlockNumber> +
OnFinalize<System::BlockNumber> +
OffchainWorker<System::BlockNumber>,
COnRuntimeUpgrade: OnRuntimeUpgrade,
Expand All @@ -192,6 +193,7 @@ impl<
UnsignedValidator,
AllModules: OnRuntimeUpgrade
+ OnInitialize<System::BlockNumber>
+ OnIdle<System::BlockNumber>
+ OnFinalize<System::BlockNumber>
+ OffchainWorker<System::BlockNumber>,
COnRuntimeUpgrade: OnRuntimeUpgrade,
Expand Down Expand Up @@ -355,6 +357,13 @@ where

// post-extrinsics book-keeping
<frame_system::Module<System>>::note_finished_extrinsics();
//TODO: does this need to be implemented here too?
// let weight = frame_system::BlockWeight;
// debug::info!("book keeping {}", weight);

// <frame_system::Module<System> as OnIdle<System::BlockNumber>>::on_idle(block_number);
// <AllModules as OnIdle<System::BlockNumber>>::on_idle(block_number);

JesseAbram marked this conversation as resolved.
Show resolved Hide resolved
<frame_system::Module<System> as OnFinalize<System::BlockNumber>>::on_finalize(block_number);
<AllModules as OnFinalize<System::BlockNumber>>::on_finalize(block_number);
}
Expand All @@ -366,6 +375,15 @@ 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();


apopiak marked this conversation as resolved.
Show resolved Hide resolved
let weight = <frame_system::Module<System>>::block_weight();
let max_weight = <System::BlockWeights as frame_support::traits::Get<_>>::get().max_block;
let remaining_weight = max_weight.saturating_sub(weight.total());

<frame_system::Module<System> as OnIdle<System::BlockNumber>>::on_idle(block_number, remaining_weight);
JesseAbram marked this conversation as resolved.
Show resolved Hide resolved
<AllModules as OnIdle<System::BlockNumber>>::on_idle(block_number, remaining_weight);

JesseAbram marked this conversation as resolved.
Show resolved Hide resolved
<frame_system::Module<System> as OnFinalize<System::BlockNumber>>::on_finalize(block_number);
<AllModules as OnFinalize<System::BlockNumber>>::on_finalize(block_number);

Expand Down
13 changes: 13 additions & 0 deletions frame/support/procedural/src/pallet/expand/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream {
}
}

impl<#type_impl_gen>
#frame_support::traits::OnIdle<<T as #frame_system::Config>::BlockNumber>
for #pallet_ident<#type_use_gen> #where_clause
{
fn on_idle(n: <T as #frame_system::Config>::BlockNumber, _remaining_weight: frame_support::weights::Weight) -> #frame_support::weights::Weight {
JesseAbram marked this conversation as resolved.
Show resolved Hide resolved
<
Self as #frame_support::traits::Hooks<
<T as #frame_system::Config>::BlockNumber
>
>::on_idle(n, _remaining_weight)
}
}

impl<#type_impl_gen>
#frame_support::traits::OnInitialize<<T as #frame_system::Config>::BlockNumber>
for #pallet_ident<#type_use_gen> #where_clause
Expand Down
Loading