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
@@ -54,7 +54,10 @@
use sp_std::{prelude::*, convert::TryInto};
use sp_runtime::traits::Hash;
use frame_support::{decl_module, decl_storage, traits::Randomness};
use frame_support::{
decl_module, decl_storage, traits::Randomness,
weights::{Weight, SimpleDispatchInfo, WeighData}
};
use safe_mix::TripletMix;
use codec::Encode;
use frame_system::Trait;
@@ -69,7 +72,7 @@ fn block_number_to_index<T: Trait>(block_number: T::BlockNumber) -> usize {
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn on_initialize(block_number: T::BlockNumber) {
fn on_initialize(block_number: T::BlockNumber) -> Weight {
let parent_hash = <frame_system::Module<T>>::parent_hash();
<RandomMaterial<T>>::mutate(|ref mut values| if values.len() < RANDOM_MATERIAL_LEN as usize {
@@ -78,6 +81,8 @@ decl_module! {
let index = block_number_to_index::<T>(block_number);
values[index] = parent_hash;
});
SimpleDispatchInfo::default().weigh_data(())
}
}
}
@@ -156,9 +161,11 @@ mod tests {
use sp_runtime::{
Perbill,
testing::Header,
traits::{BlakeTwo256, OnInitialize, Header as _, IdentityLookup},
traits::{BlakeTwo256, Header as _, IdentityLookup},
};
use frame_support::{
impl_outer_origin, parameter_types, weights::Weight, traits::{Randomness, OnInitialize},
};
use frame_support::{impl_outer_origin, parameter_types, weights::Weight, traits::Randomness};
#[derive(Clone, PartialEq, Eq)]
pub struct Test;