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 1 commit
Commits
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions core/sr-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ impl-trait-for-tuples = "0.1.3"
serde_json = "1.0.41"
rand = "0.7.2"
substrate-offchain = { path = "../offchain" }
support = { package = "srml-support", path = "../../srml/support" }
system = { package = "srml-system", path = "../../srml/system" }

[features]
bench = []
Expand Down
11 changes: 11 additions & 0 deletions core/sr-primitives/src/generic/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ impl<Number, Hash> Header<Number, Hash> where
}
}

#[cfg(all(test, feature = "std"))]
impl Header where Self::Hash: Default {
/// 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()
}
}
}

#[cfg(all(test, feature = "std"))]
mod tests {
use super::*;
Expand Down
3 changes: 3 additions & 0 deletions core/sr-primitives/src/generic/unchecked_extrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ impl<Address, Call, Signature, Extra: SignedExtension> Extrinsic
}
}


pub trait Marker {}

impl<Address, AccountId, Call, Signature, Extra, Lookup>
Checkable<Lookup>
for
Expand Down
12 changes: 11 additions & 1 deletion core/sr-primitives/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)?;
Expand Down
39 changes: 38 additions & 1 deletion core/sr-primitives/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
/// Return the weight of the block's on_initialize hook.
fn on_initialize() -> Weight { Zero::zero() }
/// Return the weight of the block's on_finalize hook.
fn on_finalize() -> Weight { Zero::zero() }
}

/// Maybe I can do something to remove the duplicate code here.
#[impl_for_tuples(5)]
impl WeighBlock for SingleModule {
fn on_initialize() -> Weight {
let mut accumulated_weight: Weight = Zero::zero();
for_tuples!(
#( accumulated_weight = accumulated_weight.saturating_add(SingleModule::on_initialize()); )*
);
accumulated_weight
}

fn on_finalize() -> Weight {
let mut accumulated_weight: Weight = Zero::zero();
for_tuples!(
#( accumulated_weight = accumulated_weight.saturating_add(SingleModule::on_finalize()); )*
);
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))]
Expand Down Expand Up @@ -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)
}
}
29 changes: 29 additions & 0 deletions node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ impl_runtime_apis! {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -719,4 +720,32 @@ mod tests {
let x = SubmitTransaction::default();
is_submit_signed_transaction(x);
}

#[test]
fn block_hooks_weight_should_not_exceed_limits() {
use sr_primitives::weights::WeighBlock;
let block_hooks_weight =
<AllModules as WeighBlock>::on_initialize() + <AllModules as WeighBlock>::on_finalize();

assert_eq!(
block_hooks_weight,
0,
"This test might fail simply because the value being compared to has increased to a \
module declaring a new weight for a hook or call. In this case update the test and \
happily move on.",
);

// Invariant. Always must be like this to have a sane chain.
// TODO: Maybe add a post-create function for all module that check this stuff.
assert!(block_hooks_weight < MaximumBlockWeight::get());

// Warning.
if block_hooks_weight > MaximumBlockWeight::get() / 2 {
println!(
"lock hooks weight is consuming more than a block's capacity. You probably want to \
re-think this. This test will fail now."
);
assert!(false);
}
}
}
6 changes: 5 additions & 1 deletion srml/example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,13 +498,17 @@ decl_module! {
<Dummy<T>>::put(new_value);
}

// The signature could also look like: `fn on_initialize()`
// The signature could also look like: `fn on_initialize()`.
// This function could also very well have a weight annotation, similar to any other. The
// only difference being the default value.
#[weight = SimpleDispatchInfo::FixedNormal(1000)]
fn on_initialize(_n: T::BlockNumber) {
// Anything that needs to be done at the start of the block.
// We don't do anything here.
}

// The signature could also look like: `fn on_finalize()`
#[weight = SimpleDispatchInfo::FixedNormal(2000)]
fn on_finalize(_n: T::BlockNumber) {
// Anything that needs to be done at the end of the block.
// We just kill our dummy storage item.
Expand Down
2 changes: 1 addition & 1 deletion srml/executive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ system = { package = "srml-system", path = "../system", default-features = false
[dev-dependencies]
hex-literal = "0.2.1"
primitives = { package = "substrate-primitives", path = "../../core/primitives" }
srml-indices = { path = "../indices" }
balances = { package = "srml-balances", path = "../balances" }
indices = { package = "srml-indices", path = "../indices" }
transaction-payment = { package = "srml-transaction-payment", path = "../transaction-payment" }

[features]
Expand Down
Loading