Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
46 changes: 39 additions & 7 deletions frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -160,6 +160,7 @@ impl<
AllModules:
OnRuntimeUpgrade +
OnInitialize<System::BlockNumber> +
OnIdle<System::BlockNumber> +
OnFinalize<System::BlockNumber> +
OffchainWorker<System::BlockNumber>,
COnRuntimeUpgrade: OnRuntimeUpgrade,
Expand All @@ -186,6 +187,7 @@ impl<
UnsignedValidator,
AllModules: OnRuntimeUpgrade
+ OnInitialize<System::BlockNumber>
+ OnIdle<System::BlockNumber>
+ OnFinalize<System::BlockNumber>
+ OffchainWorker<System::BlockNumber>,
COnRuntimeUpgrade: OnRuntimeUpgrade,
Expand Down Expand Up @@ -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
Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is a little strange to me you have coupled these two together

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya, at first there were just two lines of duplicate code, then after adding the hook, there was 8, so I thought it was prudent to handle all of them. I was thinking ok both hooks, they can go in the same function block. If you feel strongly that I should decouple them, I can make an on idle function and on finalize one.


<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
Expand Down Expand Up @@ -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(?)");
}
Expand Down Expand Up @@ -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![], },
},
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like you should create some new tests for this new functionality

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm so the test is named block_hooks_weight_is_stored and the note indicates that if new weights are introduced it should be added here (that is how I read it, but it would seem as if you wrote the note so if I mis-interpreted it no worries) . To me that made sense to put it in there, but if you think it shouldn't be I am happy to make a new test for it

})
}

Expand Down
16 changes: 16 additions & 0 deletions frame/support/procedural/src/pallet/expand/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ 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 {
<
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