Skip to content

Commit

Permalink
feat: storagext fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jmg-duarte committed Dec 13, 2024
1 parent a1bec74 commit 2fd86d5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
19 changes: 14 additions & 5 deletions pallets/randomness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ where

#[frame_support::pallet(dev_mode)]
pub mod pallet {

extern crate alloc;

use alloc::vec::Vec;

use frame_support::{
inherent::ProvideInherent,
pallet_prelude::{ValueQuery, *},
};
use frame_support::{inherent::ProvideInherent, pallet_prelude::*};
use frame_system::pallet_prelude::{BlockNumberFor, *};
use sp_inherents::{InherentData, InherentIdentifier};
use sp_runtime::traits::Hash;
Expand All @@ -39,6 +35,8 @@ pub mod pallet {

pub const LOG_TARGET: &'static str = "runtime::randomness";

const HISTORY_SIZE: u32 = 256;

#[pallet::config]
pub trait Config: frame_system::Config {
/// The Author VRF getter.
Expand All @@ -54,10 +52,16 @@ pub mod pallet {
SeedNotAvailable,
}

/// The latest author VRF randomness from BABE.
#[pallet::storage]
#[pallet::getter(fn author_vrf)]
pub type AuthorVrf<T: Config> = StorageValue<_, T::Hash, ValueQuery>;

/// The last 256 author VRF randomness values from BABE, organized in a ring buffer fashion.
#[pallet::storage]
#[pallet::getter(fn author_vrf_history)]
pub type AuthorVrfHistory<T: Config> = CountedStorageMap<_, _, BlockNumberFor<T>, T::Hash>;

#[pallet::call]
impl<T: Config> Pallet<T> {
pub fn set_author_vrf(origin: OriginFor<T>) -> DispatchResult {
Expand All @@ -71,6 +75,11 @@ pub mod pallet {
// * https://spec.polkadot.network/sect-block-production#defn-babe-secondary-slots
if let Some(author_vrf) = T::AuthorVrfGetter::get_author_vrf() {
AuthorVrf::<T>::put(author_vrf);
let current_block = <frame_system::Pallet<T>>::block_number();
if AuthorVrfHistory::<T>::count() == HISTORY_SIZE {
AuthorVrfHistory::<T>::remove(current_block - HISTORY_SIZE.into());
}
AuthorVrfHistory::<T>::insert(current_block, author_vrf);
} else {
// We don't change the value here, this isn't great but we're not expecting
// leader election to fail often enough that it truly affects security.
Expand Down
Binary file modified storagext/lib/artifacts/metadata.scale
Binary file not shown.
8 changes: 5 additions & 3 deletions storagext/lib/src/clients/randomness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ pub trait RandomnessClientExt {
impl RandomnessClientExt for crate::runtime::client::Client {
async fn get_randomness(
&self,
_block_number: BlockNumber,
block_number: BlockNumber,
) -> Result<Option<[u8; 32]>, subxt::Error> {
let seed_query = runtime::storage().randomness().author_vrf();
let randomness_query = runtime::storage()
.randomness()
.author_vrf_history(block_number);

self.client
.storage()
.at_latest()
.await?
.fetch(&seed_query)
.fetch(&randomness_query)
.await
.map(|opt_hash| opt_hash.map(|hash| *hash.as_fixed_bytes()))
}
Expand Down

0 comments on commit 2fd86d5

Please sign in to comment.