Skip to content

Commit

Permalink
Removed pallet::getter usage from Polkadot Runtime pallets (#3660)
Browse files Browse the repository at this point in the history
Part of #3326 

@kianenigma @ggwpez 

polkadot address: 12poSUQPtcF1HUPQGY3zZu2P8emuW9YnsPduA4XG3oCEfJVp

---------

Signed-off-by: Matteo Muraca <[email protected]>
Co-authored-by: ordian <[email protected]>
  • Loading branch information
muraca and ordian authored Apr 10, 2024
1 parent d96a975 commit 92e1425
Show file tree
Hide file tree
Showing 58 changed files with 834 additions and 820 deletions.
3 changes: 0 additions & 3 deletions polkadot/roadmap/implementers-guide/src/runtime/disputes.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,6 @@ This is currently a `no op`.

* `is_frozen()`: Load the value of `Frozen` from storage. Return true if `Some` and false if `None`.

* `last_valid_block()`: Load the value of `Frozen` from storage and return. None indicates that all blocks in the chain
are potentially valid.

* `revert_and_freeze(BlockNumber)`:
1. If `is_frozen()` return.
1. Set `Frozen` to `Some(BlockNumber)` to indicate a rollback to the block number.
Expand Down
4 changes: 2 additions & 2 deletions polkadot/roadmap/implementers-guide/src/runtime/scheduler.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ Actions:
[`HostConfiguration`](../types/runtime.md#host-configuration))
1. Fetch `Shared::ActiveValidators` as AV.
1. Determine the number of cores & validator groups as `n_cores`. This is the maximum of
1. `Paras::parachains().len() + configuration.parathread_cores`
1. `paras::Parachains::<T>::get().len() + configuration.parathread_cores`
1. `n_validators / max_validators_per_core` if `configuration.max_validators_per_core` is `Some` and non-zero.
1. Resize `AvailabilityCores` to have length `n_cores` with all `None` entries.
1. Compute new validator groups by shuffling using a secure randomness beacon
Expand Down Expand Up @@ -261,7 +261,7 @@ No finalization routine runs for this module.
- Fails if any on-demand claim on the same parachain is currently indexed.
- Fails if the queue length is >= `config.scheduling_lookahead * config.parathread_cores`.
- The core used for the on-demand claim is the `next_core` field of the `ParathreadQueue` (on-demand queue) and adding
`Paras::parachains().len()` to it.
`paras::Parachains::<T>::get().len()` to it.
- `next_core` is then updated by adding 1 and taking it modulo `config.parathread_cores`.
- The claim is then added to the claim index.
- `free_cores(Vec<(CoreIndex, FreedReason)>)`: indicate previously-occupied cores which are to be considered returned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ Sessions: map SessionIndex => Option<SessionInfo>,

## Routines

* `earliest_stored_session() -> SessionIndex`: Yields the earliest session for which we have information stored.
* `session_info(session: SessionIndex) -> Option<SessionInfo>`: Yields the session info for the given session, if
* `EarliestStoredSession::<T>::get() -> SessionIndex`: Yields the earliest session for which we have information stored.
* `Sessions::<T>::get(session: SessionIndex) -> Option<SessionInfo>`: Yields the session info for the given session, if
stored.
8 changes: 4 additions & 4 deletions polkadot/runtime/common/src/assigned_slots/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub mod v1 {
const MAX_PERMANENT_SLOTS: u32 = 100;
const MAX_TEMPORARY_SLOTS: u32 = 100;

<MaxPermanentSlots<T>>::put(MAX_PERMANENT_SLOTS);
<MaxTemporarySlots<T>>::put(MAX_TEMPORARY_SLOTS);
MaxPermanentSlots::<T>::put(MAX_PERMANENT_SLOTS);
MaxTemporarySlots::<T>::put(MAX_TEMPORARY_SLOTS);
// Return the weight consumed by the migration.
T::DbWeight::get().reads_writes(1, 3)
} else {
Expand All @@ -53,8 +53,8 @@ pub mod v1 {
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
let on_chain_version = Pallet::<T>::on_chain_storage_version();
ensure!(on_chain_version == 1, "assigned_slots::MigrateToV1 needs to be run");
assert_eq!(<MaxPermanentSlots<T>>::get(), 100);
assert_eq!(<MaxTemporarySlots<T>>::get(), 100);
assert_eq!(MaxPermanentSlots::<T>::get(), 100);
assert_eq!(MaxTemporarySlots::<T>::get(), 100);
Ok(())
}
}
Expand Down
82 changes: 40 additions & 42 deletions polkadot/runtime/common/src/assigned_slots/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,15 @@ pub mod pallet {

/// Assigned permanent slots, with their start lease period, and duration.
#[pallet::storage]
#[pallet::getter(fn permanent_slots)]
pub type PermanentSlots<T: Config> =
StorageMap<_, Twox64Concat, ParaId, (LeasePeriodOf<T>, LeasePeriodOf<T>), OptionQuery>;

/// Number of assigned (and active) permanent slots.
#[pallet::storage]
#[pallet::getter(fn permanent_slot_count)]
pub type PermanentSlotCount<T: Config> = StorageValue<_, u32, ValueQuery>;

/// Assigned temporary slots.
#[pallet::storage]
#[pallet::getter(fn temporary_slots)]
pub type TemporarySlots<T: Config> = StorageMap<
_,
Twox64Concat,
Expand All @@ -170,12 +167,10 @@ pub mod pallet {

/// Number of assigned temporary slots.
#[pallet::storage]
#[pallet::getter(fn temporary_slot_count)]
pub type TemporarySlotCount<T: Config> = StorageValue<_, u32, ValueQuery>;

/// Number of active temporary slots in current slot lease period.
#[pallet::storage]
#[pallet::getter(fn active_temporary_slot_count)]
pub type ActiveTemporarySlotCount<T: Config> = StorageValue<_, u32, ValueQuery>;

/// The max number of temporary slots that can be assigned.
Expand All @@ -197,8 +192,8 @@ pub mod pallet {
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
<MaxPermanentSlots<T>>::put(&self.max_permanent_slots);
<MaxTemporarySlots<T>>::put(&self.max_temporary_slots);
MaxPermanentSlots::<T>::put(&self.max_permanent_slots);
MaxTemporarySlots::<T>::put(&self.max_temporary_slots);
}
}

Expand Down Expand Up @@ -306,7 +301,7 @@ pub mod pallet {
LeasePeriodOf::<T>::from(T::PermanentSlotLeasePeriodLength::get()),
),
);
<PermanentSlotCount<T>>::mutate(|count| count.saturating_inc());
PermanentSlotCount::<T>::mutate(|count| count.saturating_inc());

Self::deposit_event(Event::<T>::PermanentSlotAssigned(id));
Ok(())
Expand Down Expand Up @@ -364,7 +359,7 @@ pub mod pallet {
};

if lease_period_start == SlotLeasePeriodStart::Current &&
Self::active_temporary_slot_count() < T::MaxTemporarySlotPerLeasePeriod::get()
ActiveTemporarySlotCount::<T>::get() < T::MaxTemporarySlotPerLeasePeriod::get()
{
// Try to allocate slot directly
match Self::configure_slot_lease(
Expand Down Expand Up @@ -394,7 +389,7 @@ pub mod pallet {
}

TemporarySlots::<T>::insert(id, temp_slot);
<TemporarySlotCount<T>>::mutate(|count| count.saturating_inc());
TemporarySlotCount::<T>::mutate(|count| count.saturating_inc());

Self::deposit_event(Event::<T>::TemporarySlotAssigned(id));

Expand All @@ -420,12 +415,12 @@ pub mod pallet {

if PermanentSlots::<T>::contains_key(id) {
PermanentSlots::<T>::remove(id);
<PermanentSlotCount<T>>::mutate(|count| *count = count.saturating_sub(One::one()));
PermanentSlotCount::<T>::mutate(|count| *count = count.saturating_sub(One::one()));
} else if TemporarySlots::<T>::contains_key(id) {
TemporarySlots::<T>::remove(id);
<TemporarySlotCount<T>>::mutate(|count| *count = count.saturating_sub(One::one()));
TemporarySlotCount::<T>::mutate(|count| *count = count.saturating_sub(One::one()));
if is_parachain {
<ActiveTemporarySlotCount<T>>::mutate(|active_count| {
ActiveTemporarySlotCount::<T>::mutate(|active_count| {
*active_count = active_count.saturating_sub(One::one())
});
}
Expand Down Expand Up @@ -456,7 +451,7 @@ pub mod pallet {
pub fn set_max_permanent_slots(origin: OriginFor<T>, slots: u32) -> DispatchResult {
ensure_root(origin)?;

<MaxPermanentSlots<T>>::put(slots);
MaxPermanentSlots::<T>::put(slots);

Self::deposit_event(Event::<T>::MaxPermanentSlotsChanged { slots });
Ok(())
Expand All @@ -468,7 +463,7 @@ pub mod pallet {
pub fn set_max_temporary_slots(origin: OriginFor<T>, slots: u32) -> DispatchResult {
ensure_root(origin)?;

<MaxTemporarySlots<T>>::put(slots);
MaxTemporarySlots::<T>::put(slots);

Self::deposit_event(Event::<T>::MaxTemporarySlotsChanged { slots });
Ok(())
Expand Down Expand Up @@ -965,7 +960,7 @@ mod tests {
RuntimeOrigin::root(),
ParaId::from(2_u32),
));
assert_eq!(AssignedSlots::permanent_slot_count(), 2);
assert_eq!(assigned_slots::PermanentSlotCount::<Test>::get(), 2);

assert_noop!(
AssignedSlots::assign_perm_parachain_slot(
Expand All @@ -989,8 +984,8 @@ mod tests {
dummy_validation_code(),
));

assert_eq!(AssignedSlots::permanent_slot_count(), 0);
assert_eq!(AssignedSlots::permanent_slots(ParaId::from(1_u32)), None);
assert_eq!(assigned_slots::PermanentSlotCount::<Test>::get(), 0);
assert_eq!(assigned_slots::PermanentSlots::<Test>::get(ParaId::from(1_u32)), None);

assert_ok!(AssignedSlots::assign_perm_parachain_slot(
RuntimeOrigin::root(),
Expand All @@ -1004,9 +999,12 @@ mod tests {

assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(1_u32)), true);

assert_eq!(AssignedSlots::permanent_slot_count(), 1);
assert_eq!(assigned_slots::PermanentSlotCount::<Test>::get(), 1);
assert_eq!(AssignedSlots::has_permanent_slot(ParaId::from(1_u32)), true);
assert_eq!(AssignedSlots::permanent_slots(ParaId::from(1_u32)), Some((0, 3)));
assert_eq!(
assigned_slots::PermanentSlots::<Test>::get(ParaId::from(1_u32)),
Some((0, 3))
);

assert_eq!(Slots::already_leased(ParaId::from(1_u32), 0, 2), true);

Expand Down Expand Up @@ -1138,7 +1136,7 @@ mod tests {
));
}

assert_eq!(AssignedSlots::temporary_slot_count(), 6);
assert_eq!(assigned_slots::TemporarySlotCount::<Test>::get(), 6);

// Attempt to assign one more temp slot
assert_ok!(TestRegistrar::<Test>::register(
Expand Down Expand Up @@ -1170,30 +1168,30 @@ mod tests {
dummy_validation_code(),
));

assert_eq!(AssignedSlots::temporary_slots(ParaId::from(1_u32)), None);
assert_eq!(assigned_slots::TemporarySlots::<Test>::get(ParaId::from(1_u32)), None);

assert_ok!(AssignedSlots::assign_temp_parachain_slot(
RuntimeOrigin::root(),
ParaId::from(1_u32),
SlotLeasePeriodStart::Current
));
assert_eq!(AssignedSlots::temporary_slot_count(), 1);
assert_eq!(AssignedSlots::active_temporary_slot_count(), 1);
assert_eq!(assigned_slots::TemporarySlotCount::<Test>::get(), 1);
assert_eq!(assigned_slots::ActiveTemporarySlotCount::<Test>::get(), 1);

// Block 1-5
// Para is a lease holding parachain for TemporarySlotLeasePeriodLength * LeasePeriod
// blocks
while block < 6 {
println!("block #{}", block);
println!("lease period #{}", AssignedSlots::current_lease_period_index());
println!("lease {:?}", Slots::lease(ParaId::from(1_u32)));
println!("lease {:?}", slots::Leases::<Test>::get(ParaId::from(1_u32)));

assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(1_u32)), true);

assert_eq!(AssignedSlots::has_temporary_slot(ParaId::from(1_u32)), true);
assert_eq!(AssignedSlots::active_temporary_slot_count(), 1);
assert_eq!(assigned_slots::ActiveTemporarySlotCount::<Test>::get(), 1);
assert_eq!(
AssignedSlots::temporary_slots(ParaId::from(1_u32)),
assigned_slots::TemporarySlots::<Test>::get(ParaId::from(1_u32)),
Some(ParachainTemporarySlot {
manager: 1,
period_begin: 0,
Expand All @@ -1212,23 +1210,23 @@ mod tests {
// Block 6
println!("block #{}", block);
println!("lease period #{}", AssignedSlots::current_lease_period_index());
println!("lease {:?}", Slots::lease(ParaId::from(1_u32)));
println!("lease {:?}", slots::Leases::<Test>::get(ParaId::from(1_u32)));

// Para lease ended, downgraded back to on-demand parachain
assert_eq!(TestRegistrar::<Test>::is_parathread(ParaId::from(1_u32)), true);
assert_eq!(Slots::already_leased(ParaId::from(1_u32), 0, 3), false);
assert_eq!(AssignedSlots::active_temporary_slot_count(), 0);
assert_eq!(assigned_slots::ActiveTemporarySlotCount::<Test>::get(), 0);

// Block 12
// Para should get a turn after TemporarySlotLeasePeriodLength * LeasePeriod blocks
run_to_block(12);
println!("block #{}", block);
println!("lease period #{}", AssignedSlots::current_lease_period_index());
println!("lease {:?}", Slots::lease(ParaId::from(1_u32)));
println!("lease {:?}", slots::Leases::<Test>::get(ParaId::from(1_u32)));

assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(1_u32)), true);
assert_eq!(Slots::already_leased(ParaId::from(1_u32), 4, 5), true);
assert_eq!(AssignedSlots::active_temporary_slot_count(), 1);
assert_eq!(assigned_slots::ActiveTemporarySlotCount::<Test>::get(), 1);
});
}

Expand Down Expand Up @@ -1270,7 +1268,7 @@ mod tests {
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(3_u32)), false);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(4_u32)), false);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(5_u32)), false);
assert_eq!(AssignedSlots::active_temporary_slot_count(), 2);
assert_eq!(assigned_slots::ActiveTemporarySlotCount::<Test>::get(), 2);
}

// Block 6-11, Period 2-3
Expand All @@ -1282,7 +1280,7 @@ mod tests {
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(3_u32)), true);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(4_u32)), false);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(5_u32)), false);
assert_eq!(AssignedSlots::active_temporary_slot_count(), 2);
assert_eq!(assigned_slots::ActiveTemporarySlotCount::<Test>::get(), 2);
}

// Block 12-17, Period 4-5
Expand All @@ -1294,7 +1292,7 @@ mod tests {
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(3_u32)), false);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(4_u32)), true);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(5_u32)), true);
assert_eq!(AssignedSlots::active_temporary_slot_count(), 2);
assert_eq!(assigned_slots::ActiveTemporarySlotCount::<Test>::get(), 2);
}

// Block 18-23, Period 6-7
Expand All @@ -1306,7 +1304,7 @@ mod tests {
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(3_u32)), false);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(4_u32)), false);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(5_u32)), false);
assert_eq!(AssignedSlots::active_temporary_slot_count(), 2);
assert_eq!(assigned_slots::ActiveTemporarySlotCount::<Test>::get(), 2);
}

// Block 24-29, Period 8-9
Expand All @@ -1318,7 +1316,7 @@ mod tests {
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(3_u32)), true);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(4_u32)), false);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(5_u32)), false);
assert_eq!(AssignedSlots::active_temporary_slot_count(), 2);
assert_eq!(assigned_slots::ActiveTemporarySlotCount::<Test>::get(), 2);
}

// Block 30-35, Period 10-11
Expand All @@ -1330,7 +1328,7 @@ mod tests {
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(3_u32)), false);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(4_u32)), true);
assert_eq!(TestRegistrar::<Test>::is_parachain(ParaId::from(5_u32)), true);
assert_eq!(AssignedSlots::active_temporary_slot_count(), 2);
assert_eq!(assigned_slots::ActiveTemporarySlotCount::<Test>::get(), 2);
}
});
}
Expand Down Expand Up @@ -1386,9 +1384,9 @@ mod tests {
ParaId::from(1_u32),
));

assert_eq!(AssignedSlots::permanent_slot_count(), 0);
assert_eq!(assigned_slots::PermanentSlotCount::<Test>::get(), 0);
assert_eq!(AssignedSlots::has_permanent_slot(ParaId::from(1_u32)), false);
assert_eq!(AssignedSlots::permanent_slots(ParaId::from(1_u32)), None);
assert_eq!(assigned_slots::PermanentSlots::<Test>::get(ParaId::from(1_u32)), None);

assert_eq!(Slots::already_leased(ParaId::from(1_u32), 0, 2), false);
});
Expand Down Expand Up @@ -1419,10 +1417,10 @@ mod tests {
ParaId::from(1_u32),
));

assert_eq!(AssignedSlots::temporary_slot_count(), 0);
assert_eq!(AssignedSlots::active_temporary_slot_count(), 0);
assert_eq!(assigned_slots::TemporarySlotCount::<Test>::get(), 0);
assert_eq!(assigned_slots::ActiveTemporarySlotCount::<Test>::get(), 0);
assert_eq!(AssignedSlots::has_temporary_slot(ParaId::from(1_u32)), false);
assert_eq!(AssignedSlots::temporary_slots(ParaId::from(1_u32)), None);
assert_eq!(assigned_slots::TemporarySlots::<Test>::get(ParaId::from(1_u32)), None);

assert_eq!(Slots::already_leased(ParaId::from(1_u32), 0, 1), false);
});
Expand Down
Loading

0 comments on commit 92e1425

Please sign in to comment.