Skip to content

Commit

Permalink
[MOON-1812] attempt using pallet_randomness for auth_slot_filter (#1956)
Browse files Browse the repository at this point in the history
* attempt using pallet_randomness for auth_slot_filter

* use subject with local vrf output for randomness

* fix slice copy
  • Loading branch information
nbaztec committed Dec 21, 2022
1 parent b917fa7 commit 2c7c152
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
39 changes: 37 additions & 2 deletions pallets/randomness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::pallet;

pub use pallet::*;
use sp_std::vec::Vec;

#[cfg(any(test, feature = "runtime-benchmarks"))]
mod benchmarks;
Expand Down Expand Up @@ -85,7 +85,7 @@ pub mod pallet {
use pallet_evm::AddressMapping;
use session_keys_primitives::{InherentError, KeysLookup, VrfId, INHERENT_IDENTIFIER};
use sp_core::{H160, H256};
use sp_runtime::traits::{AccountIdConversion, Saturating};
use sp_runtime::traits::{AccountIdConversion, Hash, Saturating};
use sp_std::convert::TryInto;

/// The Randomness's pallet id
Expand Down Expand Up @@ -225,6 +225,12 @@ pub mod pallet {
pub type RandomnessResults<T: Config> =
StorageMap<_, Twox64Concat, RequestType<T>, RandomnessResult<T::Hash>>;

/// Previous local per-block VRF randomness
/// Set in `on_finalize` of last block
#[pallet::storage]
#[pallet::getter(fn previous_local_vrf_output)]
pub type PreviousLocalVrfOutput<T: Config> = StorageValue<_, T::Hash, ValueQuery>;

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Populates `RandomnessResults` due this epoch with BABE epoch randomness
Expand Down Expand Up @@ -306,6 +312,35 @@ pub mod pallet {
<InherentIncluded<T>>::take().is_some(),
"Mandatory randomness inherent not included; InherentIncluded storage item is empty"
);

// set previous vrf output
PreviousLocalVrfOutput::<T>::put(
LocalVrfOutput::<T>::get().expect("LocalVrfOutput must exist; qed"),
);
}
}

// Randomness trait
impl<T: Config> frame_support::traits::Randomness<T::Hash, BlockNumberFor<T>> for Pallet<T> {
/// Uses the vrf output of previous block to generate a random seed. The provided `subject`
/// must have the property to uniquely generate different randomness given the same vrf
/// output (e.g. relay block number).
///
/// In our case the `subject` is provided via Nimbus and consists of three parts:
/// 1. Constant string *b"filter" - to identify author-slot-filter pallet
/// 2. First 2 bytes of index.to_le_bytes() when selecting the ith eligible author
/// 3. First 4 bytes of slot_number.to_be_bytes()
///
/// Note: This needs to be updated when asynchronous backing is in effect,
/// as it will be unsafe.
fn random(subject: &[u8]) -> (T::Hash, BlockNumberFor<T>) {
let local_vrf_output = PreviousLocalVrfOutput::<T>::get();
let block_number = frame_system::Pallet::<T>::block_number();
let mut digest = Vec::new();
digest.extend_from_slice(local_vrf_output.as_ref());
digest.extend_from_slice(subject);
let randomness = T::Hashing::hash(digest.as_slice());
(randomness, block_number)
}
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/moonbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ impl pallet_author_inherent::Config for Runtime {

impl pallet_author_slot_filter::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RandomnessSource = RandomnessCollectiveFlip;
type RandomnessSource = Randomness;
type PotentialAuthors = ParachainStaking;
type WeightInfo = pallet_author_slot_filter::weights::SubstrateWeight<Runtime>;
}
Expand Down

0 comments on commit 2c7c152

Please sign in to comment.