Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Polkadot ideal staking rate #26

Merged
merged 17 commits into from Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -4,10 +4,11 @@ Changelog for the runtimes governed by the Polkadot Fellowship.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]
## [1.0.0] 22.10.2023

### Changed

- Update Polkadot ideal staking rate ([polkadot-fellows/runtimes#26](https://github.com/polkadot-fellows/runtimes/pull/26))
- Treasury deprecate `propose_spend` dispatchable ([paritytech/substrate#14538](https://github.com/paritytech/substrate/pull/14538))
- Use benchmarked weights for `XCM` ([paritytech/polkadot#7077](https://github.com/paritytech/polkadot/pull/7077))
- Put HRMP Channel Management on General Admin Track ([paritytech/polkadot#7477](https://github.com/paritytech/polkadot/pull/7477))
Expand Down
17 changes: 9 additions & 8 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions relay/kusama/Cargo.toml
Expand Up @@ -109,9 +109,9 @@ runtime-common = { package = "polkadot-runtime-common", default-features = false
runtime-parachains = { package = "polkadot-runtime-parachains", default-features = false , version = "2.0.0" }
primitives = { package = "polkadot-primitives", default-features = false , version = "2.0.0" }

xcm = { package = "staging-xcm", default-features = false , version = "2.0.0" }
xcm = { package = "staging-xcm", default-features = false , version = "2.0.1" }
xcm-executor = { package = "staging-xcm-executor", default-features = false , version = "2.0.0" }
xcm-builder = { package = "staging-xcm-builder", default-features = false , version = "2.0.0" }
xcm-builder = { package = "staging-xcm-builder", default-features = false , version = "2.0.1" }

[dev-dependencies]
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
Expand Down
8 changes: 5 additions & 3 deletions relay/kusama/src/lib.rs
Expand Up @@ -109,6 +109,8 @@ mod weights;
// Voter bag threshold definitions.
mod bag_thresholds;

mod paras_scheduler_migration;

// Historical information of society finances.
mod past_payouts;

Expand Down Expand Up @@ -137,10 +139,10 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("kusama"),
impl_name: create_runtime_str!("parity-kusama"),
authoring_version: 2,
spec_version: 9430,
spec_version: 1_000_000,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 23,
transaction_version: 24,
state_version: 1,
};

Expand Down Expand Up @@ -1734,7 +1736,7 @@ pub mod migrations {
>,
pallet_im_online::migration::v1::Migration<Runtime>,
parachains_configuration::migration::v7::MigrateToV7<Runtime>,
parachains_scheduler::migration::v1::MigrateToV1<Runtime>,
paras_scheduler_migration::v1::MigrateToV1<Runtime>,
parachains_configuration::migration::v8::MigrateToV8<Runtime>,

// Unlock/unreserve balances from Gov v1 pallets that hold them
Expand Down
218 changes: 218 additions & 0 deletions relay/kusama/src/paras_scheduler_migration.rs
@@ -0,0 +1,218 @@
//! A copy of the migration found in the polkadot sdk repo.
//!
//! It is copied as the version of the migration found in the crate used by this runtime is broken.

use frame_support::{
migrations::VersionedMigration, pallet_prelude::ValueQuery, storage_alias,
traits::OnRuntimeUpgrade, weights::Weight,
};
use frame_system::pallet_prelude::BlockNumberFor;
use parity_scale_codec::{Decode, Encode};
use primitives::{
v5::{Assignment, ParasEntry},
CoreIndex, CoreOccupied, GroupIndex, Id as ParaId,
};
use runtime_parachains::scheduler::*;
use scale_info::TypeInfo;
use sp_core::{Get, RuntimeDebug};
use sp_std::{
collections::{btree_map::BTreeMap, vec_deque::VecDeque},
prelude::*,
};

const LOG_TARGET: &str = "runtime::parachains::scheduler";

mod v0 {
use super::*;

use primitives::{CollatorId, Id};

#[storage_alias]
pub(super) type Scheduled<T: Config> = StorageValue<Pallet<T>, Vec<CoreAssignment>, ValueQuery>;

#[derive(Clone, Encode, Decode)]
#[cfg_attr(feature = "std", derive(PartialEq))]
pub struct ParathreadClaim(pub Id, pub CollatorId);

#[derive(Clone, Encode, Decode)]
#[cfg_attr(feature = "std", derive(PartialEq))]
pub struct ParathreadEntry {
/// The claim.
pub claim: ParathreadClaim,
/// Number of retries.
pub retries: u32,
}

/// What is occupying a specific availability core.
#[derive(Clone, Encode, Decode)]
#[cfg_attr(feature = "std", derive(PartialEq))]
pub enum CoreOccupied {
/// A parathread.
Parathread(ParathreadEntry),
/// A parachain.
Parachain,
}

/// The actual type isn't important, as we only delete the key in the state.
#[storage_alias]
pub(crate) type AvailabilityCores<T: Config> =
StorageValue<Pallet<T>, Vec<Option<CoreOccupied>>, ValueQuery>;

/// The actual type isn't important, as we only delete the key in the state.
#[storage_alias]
pub(super) type ParathreadQueue<T: Config> = StorageValue<Pallet<T>, (), ValueQuery>;

#[storage_alias]
pub(super) type ParathreadClaimIndex<T: Config> = StorageValue<Pallet<T>, (), ValueQuery>;

/// The assignment type.
#[derive(Clone, Encode, Decode, TypeInfo, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(PartialEq))]
pub enum AssignmentKind {
/// A parachain.
Parachain,
/// A parathread.
Parathread(CollatorId, u32),
}

/// How a free core is scheduled to be assigned.
#[derive(Clone, Encode, Decode, TypeInfo, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(PartialEq))]
pub struct CoreAssignment {
/// The core that is assigned.
pub core: CoreIndex,
/// The unique ID of the para that is assigned to the core.
pub para_id: ParaId,
/// The kind of the assignment.
pub kind: AssignmentKind,
/// The index of the validator group assigned to the core.
pub group_idx: GroupIndex,
}
}

pub mod v1 {
use super::*;

#[storage_alias]
pub(crate) type AvailabilityCores<T: Config> =
StorageValue<Pallet<T>, Vec<CoreOccupied<BlockNumberFor<T>>>, ValueQuery>;

#[storage_alias]
pub(crate) type ClaimQueue<T: Config> = StorageValue<
Pallet<T>,
BTreeMap<CoreIndex, VecDeque<Option<ParasEntry<BlockNumberFor<T>>>>>,
ValueQuery,
>;

#[allow(deprecated)]
pub type MigrateToV1<T> = VersionedMigration<
0,
1,
UncheckedMigrateToV1<T>,
Pallet<T>,
<T as frame_system::Config>::DbWeight,
>;

#[deprecated(note = "Use MigrateToV1 instead")]
pub struct UncheckedMigrateToV1<T>(sp_std::marker::PhantomData<T>);
#[allow(deprecated)]
impl<T: Config> OnRuntimeUpgrade for UncheckedMigrateToV1<T> {
fn on_runtime_upgrade() -> Weight {
let weight_consumed = migrate_to_v1::<T>();

log::info!(target: LOG_TARGET, "Migrating para scheduler storage to v1");

weight_consumed
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> {
let n: u32 = v0::Scheduled::<T>::get().len() as u32 +
v0::AvailabilityCores::<T>::get().iter().filter(|c| c.is_some()).count() as u32;

log::info!(
target: LOG_TARGET,
"Number of scheduled and waiting for availability before: {n}",
);

Ok(n.encode())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
log::info!(target: LOG_TARGET, "Running post_upgrade()");

frame_support::ensure!(
v0::Scheduled::<T>::get().is_empty(),
"Scheduled should be empty after the migration"
);

let expected_len = u32::decode(&mut &state[..]).unwrap();
let availability_cores_waiting = AvailabilityCores::<T>::get()
.iter()
.filter(|c| !matches!(c, CoreOccupied::Free))
.count();

frame_support::ensure!(
ClaimQueue::<T>::get().iter().map(|la_vec| la_vec.1.len()).sum::<usize>() as u32 +
availability_cores_waiting as u32 ==
expected_len,
"ClaimQueue and AvailabilityCores should have the correct length",
);

Ok(())
}
}
}

pub fn migrate_to_v1<T: Config>() -> Weight {
let mut weight: Weight = Weight::zero();

v0::ParathreadQueue::<T>::kill();
v0::ParathreadClaimIndex::<T>::kill();

let now = <frame_system::Pallet<T>>::block_number();
let scheduled = v0::Scheduled::<T>::take();
let sched_len = scheduled.len() as u64;
for core_assignment in scheduled {
let core_idx = core_assignment.core;
let assignment = Assignment::new(core_assignment.para_id);
let pe = ParasEntry::new(assignment, now);

v1::ClaimQueue::<T>::mutate(|la| {
la.entry(core_idx).or_default().push_back(Some(pe));
});
}

let parachains = runtime_parachains::paras::Pallet::<T>::parachains();
let availability_cores = v0::AvailabilityCores::<T>::take();
let mut new_availability_cores = Vec::new();

for (core_index, core) in availability_cores.into_iter().enumerate() {
let new_core = if let Some(core) = core {
match core {
v0::CoreOccupied::Parachain => CoreOccupied::Paras(ParasEntry::new(
Assignment::new(parachains[core_index]),
now,
)),
v0::CoreOccupied::Parathread(entry) =>
CoreOccupied::Paras(ParasEntry::new(Assignment::new(entry.claim.0), now)),
}
} else {
CoreOccupied::Free
};

new_availability_cores.push(new_core);
}

v1::AvailabilityCores::<T>::set(new_availability_cores);

// 2x as once for Scheduled and once for Claimqueue
weight = weight.saturating_add(T::DbWeight::get().reads_writes(2 * sched_len, 2 * sched_len));
// reading parachains + availability_cores, writing AvailabilityCores
weight = weight.saturating_add(T::DbWeight::get().reads_writes(2, 1));
// 2x kill
weight = weight.saturating_add(T::DbWeight::get().writes(2));

weight
}
6 changes: 4 additions & 2 deletions relay/polkadot/Cargo.toml
Expand Up @@ -71,6 +71,7 @@ pallet-scheduler = { default-features = false , version = "24.0.0" }
pallet-session = { default-features = false , version = "23.0.0" }
frame-support = { default-features = false , version = "23.0.0" }
pallet-staking = { default-features = false , version = "23.0.0" }
pallet-staking-reward-fn = { default-features = false, version = "14.0.0" }
pallet-staking-reward-curve = { version = "8.0.0" }
pallet-staking-runtime-api = { default-features = false , version = "9.0.0" }
frame-system = { default-features = false , version = "23.0.0" }
Expand Down Expand Up @@ -99,9 +100,9 @@ runtime-common = { package = "polkadot-runtime-common", default-features = false
runtime-parachains = { package = "polkadot-runtime-parachains", default-features = false , version = "2.0.0" }
primitives = { package = "polkadot-primitives", default-features = false , version = "2.0.0" }

xcm = { package = "staging-xcm", default-features = false , version = "2.0.0" }
xcm = { package = "staging-xcm", default-features = false , version = "2.0.1" }
xcm-executor = { package = "staging-xcm-executor", default-features = false , version = "2.0.0" }
xcm-builder = { package = "staging-xcm-builder", default-features = false , version = "2.0.0" }
xcm-builder = { package = "staging-xcm-builder", default-features = false , version = "2.0.1" }

[dev-dependencies]
hex-literal = "0.4.1"
Expand Down Expand Up @@ -171,6 +172,7 @@ std = [
"pallet-session-benchmarking?/std",
"pallet-session/std",
"pallet-staking-runtime-api/std",
"pallet-staking-reward-fn/std",
"pallet-staking/std",
"pallet-timestamp/std",
"pallet-tips/std",
Expand Down