Add OnInitialise handler. (#1690)

* Add OnInitialise handler.

Closes #1686

* Fix typo

* Fix wasm build

* Add tests for initialise and finalise.
This commit is contained in:
Gav Wood
2019-02-06 10:01:28 +01:00
committed by Bastian Köcher
parent 0eeef28382
commit fa2e323478
6 changed files with 197 additions and 64 deletions
@@ -251,6 +251,15 @@ pub trait OnFinalise<BlockNumber> {
impl<N> OnFinalise<N> for () {}
/// The block initialisation trait. Implementing this lets you express what should happen
/// for your module when the block is beginning (right before the first extrinsic is executed).
pub trait OnInitialise<BlockNumber> {
/// The block is being initialised. Implement to have something happen.
fn on_initialise(_n: BlockNumber) {}
}
impl<N> OnInitialise<N> for () {}
macro_rules! tuple_impl {
($one:ident,) => {
impl<Number: Copy, $one: OnFinalise<Number>> OnFinalise<Number> for ($one,) {
@@ -258,6 +267,11 @@ macro_rules! tuple_impl {
$one::on_finalise(n);
}
}
impl<Number: Copy, $one: OnInitialise<Number>> OnInitialise<Number> for ($one,) {
fn on_initialise(n: Number) {
$one::on_initialise(n);
}
}
};
($first:ident, $($rest:ident,)+) => {
impl<
@@ -270,6 +284,16 @@ macro_rules! tuple_impl {
$($rest::on_finalise(n);)+
}
}
impl<
Number: Copy,
$first: OnInitialise<Number>,
$($rest: OnInitialise<Number>),+
> OnInitialise<Number> for ($first, $($rest),+) {
fn on_initialise(n: Number) {
$first::on_initialise(n);
$($rest::on_initialise(n);)+
}
}
tuple_impl!($($rest,)+);
}
}