Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

[MOON-1460] change eligibility ratio to be a number #38

Merged
merged 24 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
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: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ consensus engine simply by creating filters that implement the `CanAuthor` trait

This repository comes with a few example filters already, and additional examples are welcome. The examples are:
* PseudoRandom FixedSized Subset - This filter takes a finite set (eg a staked set) and filters it down to a pseudo-random
subset at each height. The eligible ratio is configurable in the pallet. This is a good learning example.
subset at each height. The eligible count is configurable in the pallet. This is a good learning example.
* Aura - The authority round consensus engine is popular in the Substrate ecosystem because it was one
of the first (and simplest!) engines implemented in Substrate. Aura can be expressed in the Nimbus
filter framework and is included as an example filter. If you are considering using aura, that crate
Expand Down
151 changes: 0 additions & 151 deletions pallets/author-inherent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,154 +218,3 @@ impl InherentError {
}
}
}

#[cfg(test)]
mod tests {
nbaztec marked this conversation as resolved.
Show resolved Hide resolved
use super::*;
use crate as author_inherent;

use frame_support::{
assert_noop, assert_ok, parameter_types,
traits::{OnFinalize, OnInitialize},
};
use sp_core::Public;
use sp_core::H256;
use sp_io::TestExternalities;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};
const TEST_AUTHOR_ID: [u8; 32] = [0u8; 32];
const BOGUS_AUTHOR_ID: [u8; 32] = [1u8; 32];

pub fn new_test_ext() -> TestExternalities {
let t = frame_system::GenesisConfig::default()
.build_storage::<Test>()
.unwrap();
TestExternalities::new(t)
}

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

// Configure a mock runtime to test the pallet.
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
AuthorInherent: author_inherent::{Pallet, Call, Storage, Inherent},
}
);

parameter_types! {
pub const BlockHashCount: u64 = 250;
}
impl frame_system::Config for Test {
type BaseCallFilter = ();
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
type Call = Call;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = ();
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
}
impl Config for Test {
type AuthorId = NimbusId;
type EventHandler = ();
type CanAuthor = ();
type AccountLookup = DummyAccountLookup;
type SlotBeacon = ();
}

pub struct DummyAccountLookup;
impl AccountLookup<NimbusId, u64> for DummyAccountLookup {
fn lookup_account(author: &NimbusId) -> Option<u64> {
if author.as_slice() == &TEST_AUTHOR_ID {
Some(0)
} else {
None
}
}
}

pub fn roll_to(n: u64) {
while System::block_number() < n {
System::on_finalize(System::block_number());
System::set_block_number(System::block_number() + 1);
System::on_initialize(System::block_number());
AuthorInherent::on_initialize(System::block_number());
}
}

#[test]
fn set_author_works() {
new_test_ext().execute_with(|| {
assert_ok!(AuthorInherent::set_author(
Origin::none(),
NimbusId::from_slice(&TEST_AUTHOR_ID)
));
roll_to(1);
assert_ok!(AuthorInherent::set_author(
Origin::none(),
NimbusId::from_slice(&TEST_AUTHOR_ID)
));
roll_to(2);
});
}

#[test]
fn must_be_inherent() {
new_test_ext().execute_with(|| {
assert_noop!(
AuthorInherent::set_author(
Origin::signed(1),
NimbusId::from_slice(&TEST_AUTHOR_ID)
),
sp_runtime::DispatchError::BadOrigin
);
});
}

#[test]
fn double_author_fails() {
new_test_ext().execute_with(|| {
assert_ok!(AuthorInherent::set_author(
Origin::none(),
NimbusId::from_slice(&TEST_AUTHOR_ID)
));
assert_noop!(
AuthorInherent::set_author(Origin::none(), NimbusId::from_slice(&TEST_AUTHOR_ID)),
Error::<Test>::AuthorAlreadySet
);
});
}

#[test]
fn fails_when_account_lookup_fails() {
new_test_ext().execute_with(|| {
assert_noop!(
AuthorInherent::set_author(Origin::none(), NimbusId::from_slice(&BOGUS_AUTHOR_ID)),
Error::<Test>::NoAccountId
);
});
}
}
5 changes: 2 additions & 3 deletions pallets/author-slot-filter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ log = { version = "0.4", default-features = false }
nimbus-primitives = { path = "../../nimbus-primitives", default-features = false }
parity-scale-codec = { version = "2.0.0", default-features = false, features = [ "derive" ] }
scale-info = { version = "1.0.0", default-features = false, features = [ "derive" ] }
serde = { version = "1.0.101", optional = true, features = [ "derive" ] }
serde = { version = "1.0.101", default-features = false, features = [ "derive" ] }
sp-core = { git = "https:/paritytech/substrate", branch = "master", default-features = false }
sp-runtime = { git = "https:/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https:/paritytech/substrate", branch = "master", default-features = false }
Expand All @@ -24,7 +24,6 @@ frame-benchmarking = { git = "https:/paritytech/substrate", branch =
frame-support-test = { version = "3.0.0", git = "https:/paritytech/substrate", branch = "master" }
sp-io = { git = "https:/paritytech/substrate", branch = "master"}


[features]
default = [ "std" ]
std = [
Expand All @@ -35,7 +34,7 @@ std = [
"nimbus-primitives/std",
"parity-scale-codec/std",
"scale-info/std",
"serde",
"serde/std",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
Expand Down
Loading