Skip to content
13 changes: 13 additions & 0 deletions prdoc/pr_10460.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: Remove pallet::getter usage from sassafras pallet

doc:
- audience: Runtime Dev
description: |
This PR removes all pallet::getter occurrences from pallet-sassafras.

crates:
- name: pallet-sassafras
bump: minor
2 changes: 1 addition & 1 deletion substrate/frame/sassafras/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ mod benchmarks {
#[block]
{
Pallet::<T>::should_end_epoch(BlockNumberFor::<T>::from(3u32));
let next_authorities = Pallet::<T>::next_authorities();
let next_authorities = NextAuthorities::<T>::get();
// Using a different set of authorities triggers the recomputation of ring verifier.
Pallet::<T>::enact_epoch_change(Default::default(), next_authorities);
}
Expand Down
87 changes: 66 additions & 21 deletions substrate/frame/sassafras/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,47 +156,39 @@ pub mod pallet {

/// Current epoch index.
#[pallet::storage]
#[pallet::getter(fn epoch_index)]
pub type EpochIndex<T> = StorageValue<_, u64, ValueQuery>;

/// Current epoch authorities.
#[pallet::storage]
#[pallet::getter(fn authorities)]
pub type Authorities<T: Config> = StorageValue<_, AuthoritiesVec<T>, ValueQuery>;

/// Next epoch authorities.
#[pallet::storage]
#[pallet::getter(fn next_authorities)]
pub type NextAuthorities<T: Config> = StorageValue<_, AuthoritiesVec<T>, ValueQuery>;

/// First block slot number.
///
/// As the slots may not be zero-based, we record the slot value for the fist block.
/// This allows to always compute relative indices for epochs and slots.
#[pallet::storage]
#[pallet::getter(fn genesis_slot)]
pub type GenesisSlot<T> = StorageValue<_, Slot, ValueQuery>;

/// Current block slot number.
#[pallet::storage]
#[pallet::getter(fn current_slot)]
pub type CurrentSlot<T> = StorageValue<_, Slot, ValueQuery>;

/// Current epoch randomness.
#[pallet::storage]
#[pallet::getter(fn randomness)]
pub type CurrentRandomness<T> = StorageValue<_, Randomness, ValueQuery>;

/// Next epoch randomness.
#[pallet::storage]
#[pallet::getter(fn next_randomness)]
pub type NextRandomness<T> = StorageValue<_, Randomness, ValueQuery>;

/// Randomness accumulator.
///
/// Excluded the first imported block, its value is updated on block finalization.
#[pallet::storage]
#[pallet::getter(fn randomness_accumulator)]
pub(crate) type RandomnessAccumulator<T> = StorageValue<_, Randomness, ValueQuery>;

/// Per slot randomness used to feed the randomness accumulator.
Expand All @@ -207,12 +199,10 @@ pub mod pallet {

/// The configuration for the current epoch.
#[pallet::storage]
#[pallet::getter(fn config)]
pub type EpochConfig<T> = StorageValue<_, EpochConfiguration, ValueQuery>;

/// The configuration for the next epoch.
#[pallet::storage]
#[pallet::getter(fn next_config)]
pub type NextEpochConfig<T> = StorageValue<_, EpochConfiguration>;

/// Pending epoch configuration change that will be set as `NextEpochConfig` when the next
Expand Down Expand Up @@ -270,7 +260,6 @@ pub mod pallet {
///
/// In practice: Updatable Universal Reference String and the seed.
#[pallet::storage]
#[pallet::getter(fn ring_context)]
pub type RingContext<T: Config> = StorageValue<_, vrf::RingContext>;

/// Ring verifier data for the current epoch.
Expand Down Expand Up @@ -392,10 +381,11 @@ pub mod pallet {
return Err("Ring verifier key not initialized".into())
};

let next_authorities = Self::next_authorities();
let next_authorities = NextAuthorities::<T>::get();

// Compute tickets threshold
let next_config = Self::next_config().unwrap_or_else(|| Self::config());
let next_config =
NextEpochConfig::<T>::get().unwrap_or_else(|| EpochConfig::<T>::get());
let ticket_threshold = sp_consensus_sassafras::ticket_id_threshold(
next_config.redundancy_factor,
epoch_length as u32,
Expand Down Expand Up @@ -746,7 +736,7 @@ impl<T: Config> Pallet<T> {
// Deposit a log as this is the first block in first epoch.
let next_epoch = NextEpochDescriptor {
randomness: next_randomness,
authorities: Self::next_authorities().into_inner(),
authorities: NextAuthorities::<T>::get().into_inner(),
config: None,
};
Self::deposit_next_epoch_descriptor_digest(next_epoch);
Expand All @@ -759,9 +749,9 @@ impl<T: Config> Pallet<T> {
index,
start: Self::epoch_start(index),
length: T::EpochLength::get(),
authorities: Self::authorities().into_inner(),
randomness: Self::randomness(),
config: Self::config(),
authorities: Authorities::<T>::get().into_inner(),
randomness: CurrentRandomness::<T>::get(),
config: EpochConfig::<T>::get(),
}
}

Expand All @@ -772,9 +762,9 @@ impl<T: Config> Pallet<T> {
index,
start: Self::epoch_start(index),
length: T::EpochLength::get(),
authorities: Self::next_authorities().into_inner(),
randomness: Self::next_randomness(),
config: Self::next_config().unwrap_or_else(|| Self::config()),
authorities: NextAuthorities::<T>::get().into_inner(),
randomness: NextRandomness::<T>::get(),
config: NextEpochConfig::<T>::get().unwrap_or_else(|| EpochConfig::<T>::get()),
}
}

Expand Down Expand Up @@ -1015,6 +1005,61 @@ impl<T: Config> Pallet<T> {
pub fn epoch_length() -> u32 {
T::EpochLength::get()
}

/// Get current epoch index.
pub fn epoch_index() -> u64 {
EpochIndex::<T>::get()
}

/// Get current epoch authorities.
pub fn authorities() -> Vec<AuthorityId> {
Authorities::<T>::get().into_inner()
}

/// Get next epoch authorities.
pub fn next_authorities() -> Vec<AuthorityId> {
NextAuthorities::<T>::get().into_inner()
}

/// Get genesis slot.
pub fn genesis_slot() -> Slot {
GenesisSlot::<T>::get()
}

/// Get current slot.
pub fn current_slot() -> Slot {
CurrentSlot::<T>::get()
}

/// Get current epoch randomness.
pub fn randomness() -> Randomness {
CurrentRandomness::<T>::get()
}

/// Get next epoch randomness.
pub fn next_randomness() -> Randomness {
NextRandomness::<T>::get()
}

/// Get randomness accumulator.
pub fn randomness_accumulator() -> Randomness {
RandomnessAccumulator::<T>::get()
}

/// Get current epoch configuration.
pub fn config() -> EpochConfiguration {
EpochConfig::<T>::get()
}

/// Get next epoch configuration.
pub fn next_config() -> Option<EpochConfiguration> {
NextEpochConfig::<T>::get()
}

/// Get ring context.
pub fn ring_context() -> Option<vrf::RingContext> {
RingContext::<T>::get()
}
}

/// Trigger an epoch change, if any should take place.
Expand Down Expand Up @@ -1049,7 +1094,7 @@ pub struct EpochChangeInternalTrigger;
impl EpochChangeTrigger for EpochChangeInternalTrigger {
fn trigger<T: Config>(block_num: BlockNumberFor<T>) -> Weight {
if Pallet::<T>::should_end_epoch(block_num) {
let authorities = Pallet::<T>::next_authorities();
let authorities = NextAuthorities::<T>::get();
let next_authorities = authorities.clone();
let len = next_authorities.len() as u32;
Pallet::<T>::enact_epoch_change(authorities, next_authorities);
Expand Down
18 changes: 9 additions & 9 deletions substrate/frame/sassafras/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ fn make_ticket_with_prover(
log::debug!("attempt: {}", attempt);

// Values are referring to the next epoch
let epoch = Sassafras::epoch_index() + 1;
let randomness = Sassafras::next_randomness();
let epoch = EpochIndex::<Test>::get() + 1;
let randomness = NextRandomness::<Test>::get();

// Make a dummy ephemeral public that hopefully is unique within one test instance.
// In the tests, the values within the erased public are just used to compare
Expand All @@ -162,9 +162,9 @@ pub fn make_prover(pair: &AuthorityPair) -> RingProver {
let public = pair.public();
let mut prover_idx = None;

let ring_ctx = Sassafras::ring_context().unwrap();
let ring_ctx = RingContext::<Test>::get().unwrap();

let pks: Vec<sp_core::bandersnatch::Public> = Sassafras::authorities()
let pks: Vec<sp_core::bandersnatch::Public> = Authorities::<Test>::get()
.iter()
.enumerate()
.map(|(idx, auth)| {
Expand Down Expand Up @@ -195,8 +195,8 @@ pub fn make_tickets(attempts: u32, pair: &AuthorityPair) -> Vec<TicketEnvelope>

pub fn make_ticket_body(attempt_idx: u32, pair: &AuthorityPair) -> (TicketId, TicketBody) {
// Values are referring to the next epoch
let epoch = Sassafras::epoch_index() + 1;
let randomness = Sassafras::next_randomness();
let epoch = EpochIndex::<Test>::get() + 1;
let randomness = NextRandomness::<Test>::get();

let ticket_id_input = vrf::ticket_id_input(&randomness, attempt_idx, epoch);
let ticket_id_pre_output = pair.as_inner_ref().vrf_pre_output(&ticket_id_input);
Expand Down Expand Up @@ -275,8 +275,8 @@ pub fn persist_next_epoch_tickets(tickets: &[(TicketId, TicketBody)]) {
}

fn slot_claim_vrf_signature(slot: Slot, pair: &AuthorityPair) -> VrfSignature {
let mut epoch = Sassafras::epoch_index();
let mut randomness = Sassafras::randomness();
let mut epoch = EpochIndex::<Test>::get();
let mut randomness = CurrentRandomness::<Test>::get();

// Check if epoch is going to change on initialization.
let epoch_start = Sassafras::current_epoch_start();
Expand Down Expand Up @@ -341,7 +341,7 @@ pub fn go_to_block(number: u64, slot: Slot, pair: &AuthorityPair) -> Digest {
/// Progress the pallet state up to the given block `number`.
/// Slots will grow linearly accordingly to blocks.
pub fn progress_to_block(number: u64, pair: &AuthorityPair) -> Option<Digest> {
let mut slot = Sassafras::current_slot() + 1;
let mut slot = CurrentSlot::<Test>::get() + 1;
let mut digest = None;
for i in System::block_number() + 1..=number {
let dig = go_to_block(i, slot, pair);
Expand Down
Loading
Loading