Skip to content

Commit

Permalink
Remove redundant author requirement (paritytech#214)
Browse files Browse the repository at this point in the history
* init

* mv kill author to on initialize

* rm unused const

* bump version and address grumble with assert noop import
  • Loading branch information
4meta5 authored Jan 29, 2021
1 parent cce2834 commit f21b048
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 8 deletions.
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.

4 changes: 4 additions & 0 deletions pallets/author-inherent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
122 changes: 116 additions & 6 deletions pallets/author-inherent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -88,6 +89,11 @@ decl_module! {
type Error = Error<T>;
fn deposit_event() = default;

fn on_initialize() -> Weight {
<Author<T>>::kill();
0
}

/// Inherent to set the author of a block
#[weight = (
0,
Expand Down Expand Up @@ -116,11 +122,6 @@ decl_module! {

Self::deposit_event(Event::<T>::AuthorSet(author, current_block));
}

fn on_finalize() {
// Do we still need this now that it is required?
assert!(<Author<T>>::take().is_some(), "Author inherent must be in the block");
}
}
}

Expand Down Expand Up @@ -222,3 +223,112 @@ impl<T: Config> ProvideInherent for Module<T> {
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::<Test>()
.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<T>,
author_inherent<T>,
}
}

impl<T> EventHandler<T> 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<Self::AccountId>;
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<Test>;
type Sys = frame_system::Module<Test>;

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::<Test>::AuthorAlreadySet
);
});
}
}
2 changes: 1 addition & 1 deletion runtime/src/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f21b048

Please sign in to comment.