Skip to content

Commit 7bd91e8

Browse files
kianenigmagpestanabkchrggwpez
authored
Update Polkadot ideal staking rate (#26)
* Update Polkadot inflation * fmr * add test to dispatch all proposals * Update relay/polkadot/src/lib.rs Co-authored-by: Gonçalo Pestana <[email protected]> * Bump spec versions * Update changelog and make it release * Bump Kusama `transaction_version` The new society is at the same index as the old one, but has different `calls`. * Update `xcm-builder` * Update relay/polkadot/src/lib.rs Co-authored-by: Oliver Tale-Yazdi <[email protected]> * Be optimistic!! * Upgrade staging xcm to get the fix for the metadata * Add a test * Adds custom paras scheduler migration This is a backport from polkadot-sdk master that includes some fixes. * Also copy the migration over to Kusama * Update CHANGELOG --------- Co-authored-by: Gonçalo Pestana <[email protected]> Co-authored-by: Bastian Köcher <[email protected]> Co-authored-by: Bastian Köcher <[email protected]> Co-authored-by: Oliver Tale-Yazdi <[email protected]>
1 parent 1dc04eb commit 7bd91e8

File tree

20 files changed

+604
-52
lines changed

20 files changed

+604
-52
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ Changelog for the runtimes governed by the Polkadot Fellowship.
44

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

7-
## [Unreleased]
7+
## [1.0.0] 22.10.2023
88

99
### Changed
1010

11+
- Update Polkadot ideal staking rate ([polkadot-fellows/runtimes#26](https://github.com/polkadot-fellows/runtimes/pull/26))
1112
- Treasury deprecate `propose_spend` dispatchable ([paritytech/substrate#14538](https://github.com/paritytech/substrate/pull/14538))
1213
- Use benchmarked weights for `XCM` ([paritytech/polkadot#7077](https://github.com/paritytech/polkadot/pull/7077))
1314
- Put HRMP Channel Management on General Admin Track ([paritytech/polkadot#7477](https://github.com/paritytech/polkadot/pull/7477))

Cargo.lock

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

relay/kusama/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ runtime-common = { package = "polkadot-runtime-common", default-features = false
109109
runtime-parachains = { package = "polkadot-runtime-parachains", default-features = false , version = "2.0.0" }
110110
primitives = { package = "polkadot-primitives", default-features = false , version = "2.0.0" }
111111

112-
xcm = { package = "staging-xcm", default-features = false , version = "2.0.0" }
112+
xcm = { package = "staging-xcm", default-features = false , version = "2.0.1" }
113113
xcm-executor = { package = "staging-xcm-executor", default-features = false , version = "2.0.0" }
114-
xcm-builder = { package = "staging-xcm-builder", default-features = false , version = "2.0.0" }
114+
xcm-builder = { package = "staging-xcm-builder", default-features = false , version = "2.0.1" }
115115

116116
[dev-dependencies]
117117
tiny-keccak = { version = "2.0.2", features = ["keccak"] }

relay/kusama/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ mod weights;
109109
// Voter bag threshold definitions.
110110
mod bag_thresholds;
111111

112+
mod paras_scheduler_migration;
113+
112114
// Historical information of society finances.
113115
mod past_payouts;
114116

@@ -137,10 +139,10 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
137139
spec_name: create_runtime_str!("kusama"),
138140
impl_name: create_runtime_str!("parity-kusama"),
139141
authoring_version: 2,
140-
spec_version: 9430,
142+
spec_version: 1_000_000,
141143
impl_version: 0,
142144
apis: RUNTIME_API_VERSIONS,
143-
transaction_version: 23,
145+
transaction_version: 24,
144146
state_version: 1,
145147
};
146148

@@ -1734,7 +1736,7 @@ pub mod migrations {
17341736
>,
17351737
pallet_im_online::migration::v1::Migration<Runtime>,
17361738
parachains_configuration::migration::v7::MigrateToV7<Runtime>,
1737-
parachains_scheduler::migration::v1::MigrateToV1<Runtime>,
1739+
paras_scheduler_migration::v1::MigrateToV1<Runtime>,
17381740
parachains_configuration::migration::v8::MigrateToV8<Runtime>,
17391741

17401742
// Unlock/unreserve balances from Gov v1 pallets that hold them
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
//! A copy of the migration found in the polkadot sdk repo.
2+
//!
3+
//! It is copied as the version of the migration found in the crate used by this runtime is broken.
4+
5+
use frame_support::{
6+
migrations::VersionedMigration, pallet_prelude::ValueQuery, storage_alias,
7+
traits::OnRuntimeUpgrade, weights::Weight,
8+
};
9+
use frame_system::pallet_prelude::BlockNumberFor;
10+
use parity_scale_codec::{Decode, Encode};
11+
use primitives::{
12+
v5::{Assignment, ParasEntry},
13+
CoreIndex, CoreOccupied, GroupIndex, Id as ParaId,
14+
};
15+
use runtime_parachains::scheduler::*;
16+
use scale_info::TypeInfo;
17+
use sp_core::{Get, RuntimeDebug};
18+
use sp_std::{
19+
collections::{btree_map::BTreeMap, vec_deque::VecDeque},
20+
prelude::*,
21+
};
22+
23+
const LOG_TARGET: &str = "runtime::parachains::scheduler";
24+
25+
mod v0 {
26+
use super::*;
27+
28+
use primitives::{CollatorId, Id};
29+
30+
#[storage_alias]
31+
pub(super) type Scheduled<T: Config> = StorageValue<Pallet<T>, Vec<CoreAssignment>, ValueQuery>;
32+
33+
#[derive(Clone, Encode, Decode)]
34+
#[cfg_attr(feature = "std", derive(PartialEq))]
35+
pub struct ParathreadClaim(pub Id, pub CollatorId);
36+
37+
#[derive(Clone, Encode, Decode)]
38+
#[cfg_attr(feature = "std", derive(PartialEq))]
39+
pub struct ParathreadEntry {
40+
/// The claim.
41+
pub claim: ParathreadClaim,
42+
/// Number of retries.
43+
pub retries: u32,
44+
}
45+
46+
/// What is occupying a specific availability core.
47+
#[derive(Clone, Encode, Decode)]
48+
#[cfg_attr(feature = "std", derive(PartialEq))]
49+
pub enum CoreOccupied {
50+
/// A parathread.
51+
Parathread(ParathreadEntry),
52+
/// A parachain.
53+
Parachain,
54+
}
55+
56+
/// The actual type isn't important, as we only delete the key in the state.
57+
#[storage_alias]
58+
pub(crate) type AvailabilityCores<T: Config> =
59+
StorageValue<Pallet<T>, Vec<Option<CoreOccupied>>, ValueQuery>;
60+
61+
/// The actual type isn't important, as we only delete the key in the state.
62+
#[storage_alias]
63+
pub(super) type ParathreadQueue<T: Config> = StorageValue<Pallet<T>, (), ValueQuery>;
64+
65+
#[storage_alias]
66+
pub(super) type ParathreadClaimIndex<T: Config> = StorageValue<Pallet<T>, (), ValueQuery>;
67+
68+
/// The assignment type.
69+
#[derive(Clone, Encode, Decode, TypeInfo, RuntimeDebug)]
70+
#[cfg_attr(feature = "std", derive(PartialEq))]
71+
pub enum AssignmentKind {
72+
/// A parachain.
73+
Parachain,
74+
/// A parathread.
75+
Parathread(CollatorId, u32),
76+
}
77+
78+
/// How a free core is scheduled to be assigned.
79+
#[derive(Clone, Encode, Decode, TypeInfo, RuntimeDebug)]
80+
#[cfg_attr(feature = "std", derive(PartialEq))]
81+
pub struct CoreAssignment {
82+
/// The core that is assigned.
83+
pub core: CoreIndex,
84+
/// The unique ID of the para that is assigned to the core.
85+
pub para_id: ParaId,
86+
/// The kind of the assignment.
87+
pub kind: AssignmentKind,
88+
/// The index of the validator group assigned to the core.
89+
pub group_idx: GroupIndex,
90+
}
91+
}
92+
93+
pub mod v1 {
94+
use super::*;
95+
96+
#[storage_alias]
97+
pub(crate) type AvailabilityCores<T: Config> =
98+
StorageValue<Pallet<T>, Vec<CoreOccupied<BlockNumberFor<T>>>, ValueQuery>;
99+
100+
#[storage_alias]
101+
pub(crate) type ClaimQueue<T: Config> = StorageValue<
102+
Pallet<T>,
103+
BTreeMap<CoreIndex, VecDeque<Option<ParasEntry<BlockNumberFor<T>>>>>,
104+
ValueQuery,
105+
>;
106+
107+
#[allow(deprecated)]
108+
pub type MigrateToV1<T> = VersionedMigration<
109+
0,
110+
1,
111+
UncheckedMigrateToV1<T>,
112+
Pallet<T>,
113+
<T as frame_system::Config>::DbWeight,
114+
>;
115+
116+
#[deprecated(note = "Use MigrateToV1 instead")]
117+
pub struct UncheckedMigrateToV1<T>(sp_std::marker::PhantomData<T>);
118+
#[allow(deprecated)]
119+
impl<T: Config> OnRuntimeUpgrade for UncheckedMigrateToV1<T> {
120+
fn on_runtime_upgrade() -> Weight {
121+
let weight_consumed = migrate_to_v1::<T>();
122+
123+
log::info!(target: LOG_TARGET, "Migrating para scheduler storage to v1");
124+
125+
weight_consumed
126+
}
127+
128+
#[cfg(feature = "try-runtime")]
129+
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> {
130+
let n: u32 = v0::Scheduled::<T>::get().len() as u32 +
131+
v0::AvailabilityCores::<T>::get().iter().filter(|c| c.is_some()).count() as u32;
132+
133+
log::info!(
134+
target: LOG_TARGET,
135+
"Number of scheduled and waiting for availability before: {n}",
136+
);
137+
138+
Ok(n.encode())
139+
}
140+
141+
#[cfg(feature = "try-runtime")]
142+
fn post_upgrade(state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
143+
log::info!(target: LOG_TARGET, "Running post_upgrade()");
144+
145+
frame_support::ensure!(
146+
v0::Scheduled::<T>::get().is_empty(),
147+
"Scheduled should be empty after the migration"
148+
);
149+
150+
let expected_len = u32::decode(&mut &state[..]).unwrap();
151+
let availability_cores_waiting = AvailabilityCores::<T>::get()
152+
.iter()
153+
.filter(|c| !matches!(c, CoreOccupied::Free))
154+
.count();
155+
156+
frame_support::ensure!(
157+
ClaimQueue::<T>::get().iter().map(|la_vec| la_vec.1.len()).sum::<usize>() as u32 +
158+
availability_cores_waiting as u32 ==
159+
expected_len,
160+
"ClaimQueue and AvailabilityCores should have the correct length",
161+
);
162+
163+
Ok(())
164+
}
165+
}
166+
}
167+
168+
pub fn migrate_to_v1<T: Config>() -> Weight {
169+
let mut weight: Weight = Weight::zero();
170+
171+
v0::ParathreadQueue::<T>::kill();
172+
v0::ParathreadClaimIndex::<T>::kill();
173+
174+
let now = <frame_system::Pallet<T>>::block_number();
175+
let scheduled = v0::Scheduled::<T>::take();
176+
let sched_len = scheduled.len() as u64;
177+
for core_assignment in scheduled {
178+
let core_idx = core_assignment.core;
179+
let assignment = Assignment::new(core_assignment.para_id);
180+
let pe = ParasEntry::new(assignment, now);
181+
182+
v1::ClaimQueue::<T>::mutate(|la| {
183+
la.entry(core_idx).or_default().push_back(Some(pe));
184+
});
185+
}
186+
187+
let parachains = runtime_parachains::paras::Pallet::<T>::parachains();
188+
let availability_cores = v0::AvailabilityCores::<T>::take();
189+
let mut new_availability_cores = Vec::new();
190+
191+
for (core_index, core) in availability_cores.into_iter().enumerate() {
192+
let new_core = if let Some(core) = core {
193+
match core {
194+
v0::CoreOccupied::Parachain => CoreOccupied::Paras(ParasEntry::new(
195+
Assignment::new(parachains[core_index]),
196+
now,
197+
)),
198+
v0::CoreOccupied::Parathread(entry) =>
199+
CoreOccupied::Paras(ParasEntry::new(Assignment::new(entry.claim.0), now)),
200+
}
201+
} else {
202+
CoreOccupied::Free
203+
};
204+
205+
new_availability_cores.push(new_core);
206+
}
207+
208+
v1::AvailabilityCores::<T>::set(new_availability_cores);
209+
210+
// 2x as once for Scheduled and once for Claimqueue
211+
weight = weight.saturating_add(T::DbWeight::get().reads_writes(2 * sched_len, 2 * sched_len));
212+
// reading parachains + availability_cores, writing AvailabilityCores
213+
weight = weight.saturating_add(T::DbWeight::get().reads_writes(2, 1));
214+
// 2x kill
215+
weight = weight.saturating_add(T::DbWeight::get().writes(2));
216+
217+
weight
218+
}

relay/polkadot/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pallet-scheduler = { default-features = false , version = "24.0.0" }
7171
pallet-session = { default-features = false , version = "23.0.0" }
7272
frame-support = { default-features = false , version = "23.0.0" }
7373
pallet-staking = { default-features = false , version = "23.0.0" }
74+
pallet-staking-reward-fn = { default-features = false, version = "14.0.0" }
7475
pallet-staking-reward-curve = { version = "8.0.0" }
7576
pallet-staking-runtime-api = { default-features = false , version = "9.0.0" }
7677
frame-system = { default-features = false , version = "23.0.0" }
@@ -99,9 +100,9 @@ runtime-common = { package = "polkadot-runtime-common", default-features = false
99100
runtime-parachains = { package = "polkadot-runtime-parachains", default-features = false , version = "2.0.0" }
100101
primitives = { package = "polkadot-primitives", default-features = false , version = "2.0.0" }
101102

102-
xcm = { package = "staging-xcm", default-features = false , version = "2.0.0" }
103+
xcm = { package = "staging-xcm", default-features = false , version = "2.0.1" }
103104
xcm-executor = { package = "staging-xcm-executor", default-features = false , version = "2.0.0" }
104-
xcm-builder = { package = "staging-xcm-builder", default-features = false , version = "2.0.0" }
105+
xcm-builder = { package = "staging-xcm-builder", default-features = false , version = "2.0.1" }
105106

106107
[dev-dependencies]
107108
hex-literal = "0.4.1"
@@ -171,6 +172,7 @@ std = [
171172
"pallet-session-benchmarking?/std",
172173
"pallet-session/std",
173174
"pallet-staking-runtime-api/std",
175+
"pallet-staking-reward-fn/std",
174176
"pallet-staking/std",
175177
"pallet-timestamp/std",
176178
"pallet-tips/std",

0 commit comments

Comments
 (0)