Weight annotation for block hooks. (#4058)

* Initial version that works with proper tests.

* get rid of todos and grumbles and such.

* Cleanup and fix line-width

* fix test runtime test
This commit is contained in:
Kian Paimani
2019-11-12 14:42:38 +01:00
committed by Gavin Wood
parent ab1b98fee5
commit efedac734d
10 changed files with 339 additions and 15 deletions
+11 -1
View File
@@ -145,7 +145,7 @@ pub type DigestItem = generic::DigestItem<H256>;
pub type Digest = generic::Digest<H256>;
/// Block Header
#[derive(PartialEq, Eq, Clone, Serialize, Debug, Encode, Decode)]
#[derive(PartialEq, Eq, Clone, Serialize, Debug, Encode, Decode, Default)]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub struct Header {
@@ -198,6 +198,16 @@ impl traits::Header for Header {
}
}
impl Header {
/// A new header with the given number and default hash for all other fields.
pub fn new_from_number(number: <Self as traits::Header>::Number) -> Self {
Self {
number,
..Default::default()
}
}
}
impl<'a> Deserialize<'a> for Header {
fn deserialize<D: Deserializer<'a>>(de: D) -> Result<Self, D::Error> {
let r = <Vec<u8>>::deserialize(de)?;
+38 -1
View File
@@ -37,8 +37,9 @@
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
use impl_trait_for_tuples::impl_for_tuples;
use codec::{Encode, Decode};
use arithmetic::traits::Bounded;
use arithmetic::traits::{Bounded, Zero};
use crate::RuntimeDebug;
/// Re-export priority as type
@@ -62,6 +63,35 @@ pub trait ClassifyDispatch<T> {
fn classify_dispatch(&self, target: T) -> DispatchClass;
}
/// Means of determining the weight of a block's lifecycle hooks: on_initialize, on_finalize and
/// such.
pub trait WeighBlock<BlockNumber> {
/// Return the weight of the block's on_initialize hook.
fn on_initialize(_: BlockNumber) -> Weight { Zero::zero() }
/// Return the weight of the block's on_finalize hook.
fn on_finalize(_: BlockNumber) -> Weight { Zero::zero() }
}
/// Maybe I can do something to remove the duplicate code here.
#[impl_for_tuples(30)]
impl<BlockNumber: Copy> WeighBlock<BlockNumber> for SingleModule {
fn on_initialize(n: BlockNumber) -> Weight {
let mut accumulated_weight: Weight = Zero::zero();
for_tuples!(
#( accumulated_weight = accumulated_weight.saturating_add(SingleModule::on_initialize(n)); )*
);
accumulated_weight
}
fn on_finalize(n: BlockNumber) -> Weight {
let mut accumulated_weight: Weight = Zero::zero();
for_tuples!(
#( accumulated_weight = accumulated_weight.saturating_add(SingleModule::on_finalize(n)); )*
);
accumulated_weight
}
}
/// A generalized group of dispatch types. This is only distinguishing normal, user-triggered transactions
/// (`Normal`) and anything beyond which serves a higher purpose to the system (`Operational`).
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
@@ -181,3 +211,10 @@ impl Default for SimpleDispatchInfo {
SimpleDispatchInfo::FixedNormal(10_000)
}
}
impl SimpleDispatchInfo {
/// An _additive zero_ variant of SimpleDispatchInfo.
pub fn zero() -> Self {
Self::FixedNormal(0)
}
}