diff --git a/Cargo.lock b/Cargo.lock index ed9375e8200a3..185d2bdf2444f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -387,7 +387,9 @@ dependencies = [ "frame-system", "parity-scale-codec", "sp-authorship", + "sp-core", "sp-inherents", + "sp-io", "sp-runtime", "sp-std", ] diff --git a/pallets/author-inherent/Cargo.toml b/pallets/author-inherent/Cargo.toml index 0faf91195b514..758e73a74892a 100644 --- a/pallets/author-inherent/Cargo.toml +++ b/pallets/author-inherent/Cargo.toml @@ -15,6 +15,10 @@ sp-inherents = { git = "https://github.com/paritytech/substrate", branch = "mast sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } +[dev-dependencies] +sp-io = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } +sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } + [features] default = ["std"] std = [ diff --git a/pallets/author-inherent/src/lib.rs b/pallets/author-inherent/src/lib.rs index 98eb64170929f..5e91aa6a4c61b 100644 --- a/pallets/author-inherent/src/lib.rs +++ b/pallets/author-inherent/src/lib.rs @@ -21,7 +21,8 @@ #![cfg_attr(not(feature = "std"), no_std)] use frame_support::{ - decl_error, decl_event, decl_module, decl_storage, ensure, weights::DispatchClass, + decl_error, decl_event, decl_module, decl_storage, ensure, + weights::{DispatchClass, Weight}, }; use frame_system::{ensure_none, Config as System}; use parity_scale_codec::{Decode, Encode}; @@ -88,6 +89,11 @@ decl_module! { type Error = Error; fn deposit_event() = default; + fn on_initialize() -> Weight { + >::kill(); + 0 + } + /// Inherent to set the author of a block #[weight = ( 0, @@ -116,11 +122,6 @@ decl_module! { Self::deposit_event(Event::::AuthorSet(author, current_block)); } - - fn on_finalize() { - // Do we still need this now that it is required? - assert!(>::take().is_some(), "Author inherent must be in the block"); - } } } @@ -222,3 +223,112 @@ impl ProvideInherent for Module { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + + use frame_support::{ + assert_noop, assert_ok, impl_outer_event, impl_outer_origin, parameter_types, + traits::{OnFinalize, OnInitialize}, + }; + use sp_core::H256; + use sp_io::TestExternalities; + use sp_runtime::{ + testing::Header, + traits::{BlakeTwo256, IdentityLookup}, + }; + + pub fn new_test_ext() -> TestExternalities { + let t = frame_system::GenesisConfig::default() + .build_storage::() + .unwrap(); + TestExternalities::new(t) + } + + impl_outer_origin! { + pub enum Origin for Test where system = frame_system {} + } + + mod author_inherent { + pub use super::super::*; + } + + impl_outer_event! { + pub enum MetaEvent for Test { + frame_system, + author_inherent, + } + } + + impl EventHandler for () { + fn note_author(_author: T) {} + } + + #[derive(Clone, Eq, PartialEq)] + pub struct Test; + parameter_types! { + pub const BlockHashCount: u64 = 250; + } + impl System for Test { + type BaseCallFilter = (); + type BlockWeights = (); + type BlockLength = (); + type DbWeight = (); + type Origin = Origin; + type Index = u64; + type BlockNumber = u64; + type Call = (); + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = u64; + type Lookup = IdentityLookup; + type Header = Header; + type Event = MetaEvent; + type BlockHashCount = BlockHashCount; + type Version = (); + type PalletInfo = (); + type AccountData = (); + type OnNewAccount = (); + type OnKilledAccount = (); + type SystemWeightInfo = (); + type SS58Prefix = (); + } + impl Config for Test { + type Event = MetaEvent; + type EventHandler = (); + type CanAuthor = (); + } + type AuthorInherent = Module; + type Sys = frame_system::Module; + + pub fn roll_to(n: u64) { + while Sys::block_number() < n { + Sys::on_finalize(Sys::block_number()); + Sys::set_block_number(Sys::block_number() + 1); + Sys::on_initialize(Sys::block_number()); + AuthorInherent::on_initialize(Sys::block_number()); + } + } + + #[test] + fn set_author_works() { + new_test_ext().execute_with(|| { + assert_ok!(AuthorInherent::set_author(Origin::none(), 1)); + roll_to(1); + assert_ok!(AuthorInherent::set_author(Origin::none(), 1)); + roll_to(2); + }); + } + + #[test] + fn double_author_fails() { + new_test_ext().execute_with(|| { + assert_ok!(AuthorInherent::set_author(Origin::none(), 1)); + assert_noop!( + AuthorInherent::set_author(Origin::none(), 1), + Error::::AuthorAlreadySet + ); + }); + } +} diff --git a/runtime/src/parachain.rs b/runtime/src/parachain.rs index 9eef9deed822f..eaefe63e86581 100644 --- a/runtime/src/parachain.rs +++ b/runtime/src/parachain.rs @@ -22,7 +22,7 @@ macro_rules! runtime_parachain { spec_name: create_runtime_str!("moonbase-alphanet"), impl_name: create_runtime_str!("moonbase-alphanet"), authoring_version: 3, - spec_version: 12, + spec_version: 13, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 2, diff --git a/runtime/src/standalone.rs b/runtime/src/standalone.rs index 9561badc7d308..05158cdfa35b8 100644 --- a/runtime/src/standalone.rs +++ b/runtime/src/standalone.rs @@ -31,7 +31,7 @@ macro_rules! runtime_standalone { spec_name: create_runtime_str!("moonbeam-standalone"), impl_name: create_runtime_str!("moonbeam-standalone"), authoring_version: 3, - spec_version: 12, + spec_version: 13, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 2,