Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions pallets/author-slot-filter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
version = "0.9.0"

[dependencies]
environmental = { workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
log = { workspace = true }
Expand Down
26 changes: 25 additions & 1 deletion pallets/author-slot-filter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,23 @@ pub mod pallet {
use super::*;
use crate::num::NonZeroU32;
use crate::weights::WeightInfo;
use frame_support::{pallet_prelude::*, traits::Randomness};
use frame_support::{pallet_prelude::*, storage::unhashed, traits::Randomness};
use frame_system::pallet_prelude::*;
use log::debug;
use nimbus_primitives::CanAuthor;
use sp_core::H256;
use sp_runtime::Percent;
use sp_std::vec::Vec;

/// Storage key to be used for fake author logic
const IS_FAKE_AUTHOR_KEY: &[u8] = b"AuthorSlotFilter:IsFakeAuthor";

environmental::environmental!(IS_FAKE_AUTHOR: ());
/// Use fake author logic
pub fn using_fake_author<R, F: FnOnce() -> R>(mutator: F) -> R {
IS_FAKE_AUTHOR::using(&mut (), mutator)
}

/// The Author Filter pallet
#[pallet::pallet]
#[pallet::without_storage_info]
Expand All @@ -76,6 +85,16 @@ pub mod pallet {
type WeightInfo: WeightInfo;
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_now: BlockNumberFor<T>) -> Weight {
Comment thread
arturgontijo marked this conversation as resolved.
if IS_FAKE_AUTHOR::with(|_| ()).is_some() {
unhashed::put(IS_FAKE_AUTHOR_KEY, &());
}
T::DbWeight::get().reads_writes(0, 1)
Comment thread
arturgontijo marked this conversation as resolved.
Outdated
}
}

/// Compute a pseudo-random subset of the input accounts by using Pallet's
/// source of randomness, `Config::RandomnessSource`.
/// Returns (Eligible, Ineligible), each is a set of accounts
Expand Down Expand Up @@ -127,6 +146,11 @@ pub mod pallet {
impl<T: Config> CanAuthor<T::AccountId> for Pallet<T> {
#[cfg(not(feature = "try-runtime"))]
fn can_author(author: &T::AccountId, slot: &u32) -> bool {
// Check if fake author logic is being used
if unhashed::get::<()>(IS_FAKE_AUTHOR_KEY).is_some() {
return true;
}

// Compute pseudo-random subset of potential authors
let (eligible, ineligible) =
compute_pseudo_random_subset::<T>(T::PotentialAuthors::get(), slot);
Expand Down
13 changes: 12 additions & 1 deletion pallets/author-slot-filter/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ pub use crate::mock::*;
use crate::num::NonZeroU32;

use frame_support::assert_ok;
use frame_support::traits::OnRuntimeUpgrade;
use frame_support::traits::{OnInitialize, OnRuntimeUpgrade};
use frame_support::weights::Weight;
use nimbus_primitives::CanAuthor;
use sp_runtime::Percent;

#[test]
Expand Down Expand Up @@ -97,3 +98,13 @@ fn test_migration_inserts_default_value_for_missing_eligible_ratio() {
assert_eq!(expected_default_eligible_count, actual_eligible_count);
});
}

#[test]
fn test_using_fake_author_works() {
new_test_ext().execute_with(|| {
using_fake_author(|| {
AuthorSlotFilter::on_initialize(System::block_number());
assert!(AuthorSlotFilter::can_author(&42, &42));
Comment thread
arturgontijo marked this conversation as resolved.
});
});
}
Loading