-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Parachains: Use relay chain slot for velocity measurement #6825
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
Changes from 21 commits
2cea63d
3086345
ad5b1af
2305334
a023784
b69df30
ee45c93
665039b
720bd03
73ba8d1
bd1acea
6b8ce29
db26437
74992c2
7a7aa52
d78df61
8f03a9f
ce0a28c
31a91d3
a2d5936
ef2a785
257f15b
03b8056
40de356
d67e56b
d7a6e55
1756059
e4f8c8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,6 @@ | |
| //! block velocity. | ||
| //! | ||
| //! The velocity `V` refers to the rate of block processing by the relay chain. | ||
|
|
||
| use super::{pallet, Aura}; | ||
| use core::{marker::PhantomData, num::NonZeroU32}; | ||
| use cumulus_pallet_parachain_system::{ | ||
|
|
@@ -54,8 +53,23 @@ where | |
| let velocity = V.max(1); | ||
| let relay_chain_slot = state_proof.read_slot().expect("failed to read relay chain slot"); | ||
|
|
||
| let (slot, authored) = | ||
| pallet::SlotInfo::<T>::get().expect("slot info is inserted on block initialization"); | ||
| let (relay_chain_slot, authored_in_relay) = match pallet::RelaySlotInfo::<T>::get() { | ||
| Some((slot, authored)) if slot == relay_chain_slot => (slot, authored), | ||
| Some((slot, _)) if slot < relay_chain_slot => (relay_chain_slot, 0), | ||
| Some((slot, _)) => { | ||
| panic!("Slot moved backwards: stored_slot={slot:?}, relay_chain_slot={relay_chain_slot:?}") | ||
| }, | ||
| None => (relay_chain_slot, 0), | ||
| }; | ||
|
|
||
| // We need to allow one additional block to be built to fill the unincluded segment. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really get this comment. |
||
| if authored_in_relay > velocity { | ||
| panic!("authored blocks limit is reached for the slot: relay_chain_slot={relay_chain_slot:?}, authored={authored_in_relay:?}, velocity={velocity:?}"); | ||
| } | ||
|
|
||
| pallet::RelaySlotInfo::<T>::put((relay_chain_slot, authored_in_relay + 1)); | ||
|
|
||
| let para_slot = pallet_aura::CurrentSlot::<T>::get(); | ||
|
|
||
| // Convert relay chain timestamp. | ||
| let relay_chain_timestamp = | ||
|
|
@@ -67,19 +81,16 @@ where | |
|
|
||
| // Check that we are not too far in the future. Since we expect `V` parachain blocks | ||
| // during the relay chain slot, we can allow for `V` parachain slots into the future. | ||
| if *slot > *para_slot_from_relay + u64::from(velocity) { | ||
| if *para_slot > *para_slot_from_relay + u64::from(velocity) { | ||
| panic!( | ||
| "Parachain slot is too far in the future: parachain_slot: {:?}, derived_from_relay_slot: {:?} velocity: {:?}", | ||
| slot, | ||
| "Parachain slot is too far in the future: parachain_slot={:?}, derived_from_relay_slot={:?} velocity={:?}, relay_chain_slot={:?}", | ||
| para_slot, | ||
| para_slot_from_relay, | ||
| velocity | ||
| velocity, | ||
| relay_chain_slot | ||
| ); | ||
| } | ||
|
|
||
| // We need to allow authoring multiple blocks in the same slot. | ||
| if slot != para_slot_from_relay && authored > velocity { | ||
| panic!("authored blocks limit is reached for the slot") | ||
| } | ||
| let weight = T::DbWeight::get().reads(1); | ||
|
|
||
| ( | ||
|
|
@@ -110,7 +121,7 @@ impl< | |
| /// is more recent than the included block itself. | ||
| pub fn can_build_upon(included_hash: T::Hash, new_slot: Slot) -> bool { | ||
| let velocity = V.max(1); | ||
| let (last_slot, authored_so_far) = match pallet::SlotInfo::<T>::get() { | ||
| let (last_slot, authored_so_far) = match pallet::RelaySlotInfo::<T>::get() { | ||
bkchr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| None => return true, | ||
| Some(x) => x, | ||
| }; | ||
|
|
@@ -123,11 +134,8 @@ impl< | |
| return false | ||
| } | ||
|
|
||
| // TODO: This logic needs to be adjusted. | ||
| // It checks that we have not authored more than `V + 1` blocks in the slot. | ||
| // As a slot however, we take the parachain slot here. Velocity should | ||
| // be measured in relation to the relay chain slot. | ||
| // https://github.com/paritytech/polkadot-sdk/issues/3967 | ||
| // Check that we have not authored more than `V + 1` parachain blocks in the current relay | ||
| // chain slot. | ||
| if last_slot == new_slot { | ||
| authored_so_far < velocity + 1 | ||
| } else { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,9 @@ use sp_consensus_aura::{digests::CompatibleDigestItem, Slot}; | |
| use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; | ||
|
|
||
| pub mod consensus_hook; | ||
| pub mod migration; | ||
| mod test; | ||
|
|
||
| pub use consensus_hook::FixedVelocityConsensusHook; | ||
|
|
||
| type Aura<T> = pallet_aura::Pallet<T>; | ||
|
|
@@ -57,6 +60,7 @@ pub mod pallet { | |
| pub trait Config: pallet_aura::Config + frame_system::Config {} | ||
|
|
||
| #[pallet::pallet] | ||
| #[pallet::storage_version(migration::STORAGE_VERSION)] | ||
| pub struct Pallet<T>(_); | ||
|
|
||
| #[pallet::hooks] | ||
|
|
@@ -70,20 +74,7 @@ pub mod pallet { | |
| // Fetch the authorities once to get them into the storage proof of the PoV. | ||
| Authorities::<T>::get(); | ||
|
|
||
| let new_slot = pallet_aura::CurrentSlot::<T>::get(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it feels a bit weird that the pallet now has a |
||
|
|
||
| let (new_slot, authored) = match SlotInfo::<T>::get() { | ||
| Some((slot, authored)) if slot == new_slot => (slot, authored + 1), | ||
| Some((slot, _)) if slot < new_slot => (new_slot, 1), | ||
| Some(..) => { | ||
| panic!("slot moved backwards") | ||
| }, | ||
| None => (new_slot, 1), | ||
| }; | ||
|
|
||
| SlotInfo::<T>::put((new_slot, authored)); | ||
|
|
||
| T::DbWeight::get().reads_writes(4, 2) | ||
| T::DbWeight::get().reads_writes(1, 0) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -99,11 +90,12 @@ pub mod pallet { | |
| ValueQuery, | ||
| >; | ||
|
|
||
| /// Current slot paired with a number of authored blocks. | ||
| /// Current relay chain slot paired with a number of authored blocks. | ||
| /// | ||
| /// Updated on each block initialization. | ||
| /// This is updated in [`FixedVelocityConsensusHook::on_state_proof`] with the current relay | ||
| /// chain slot as provided by the relay chain state proof. | ||
| #[pallet::storage] | ||
| pub(crate) type SlotInfo<T: Config> = StorageValue<_, (Slot, u32), OptionQuery>; | ||
| pub(crate) type RelaySlotInfo<T: Config> = StorageValue<_, (Slot, u32), OptionQuery>; | ||
|
|
||
| #[pallet::genesis_config] | ||
| #[derive(frame_support::DefaultNoBound)] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // Copyright (C) Parity Technologies (UK) Ltd. | ||
| // This file is part of Cumulus. | ||
|
|
||
| // Cumulus is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Cumulus is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Cumulus. If not, see <http://www.gnu.org/licenses/>. | ||
| extern crate alloc; | ||
|
|
||
| use crate::{Config, Pallet}; | ||
| #[cfg(feature = "try-runtime")] | ||
| use alloc::vec::Vec; | ||
| use frame_support::{migrations::VersionedMigration, pallet_prelude::StorageVersion}; | ||
|
|
||
| /// The in-code storage version. | ||
| pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); | ||
|
|
||
| mod v0 { | ||
| use super::*; | ||
| use frame_support::{pallet_prelude::OptionQuery, storage_alias}; | ||
| use sp_consensus_aura::Slot; | ||
|
|
||
| /// Current slot paired with a number of authored blocks. | ||
| /// | ||
| /// Updated on each block initialization. | ||
| #[storage_alias] | ||
| pub(super) type SlotInfo<T: Config> = StorageValue<Pallet<T>, (Slot, u32), OptionQuery>; | ||
| } | ||
| mod v1 { | ||
| use super::*; | ||
| use frame_support::{pallet_prelude::*, traits::UncheckedOnRuntimeUpgrade}; | ||
|
|
||
| pub struct UncheckedMigrationToV1<T: Config>(PhantomData<T>); | ||
|
|
||
| impl<T: Config> UncheckedOnRuntimeUpgrade for UncheckedMigrationToV1<T> { | ||
| fn on_runtime_upgrade() -> Weight { | ||
| let mut weight: Weight = Weight::zero(); | ||
| weight += migrate::<T>(); | ||
| weight | ||
| } | ||
|
|
||
| #[cfg(feature = "try-runtime")] | ||
| fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> { | ||
| Ok(Vec::new()) | ||
| } | ||
| #[cfg(feature = "try-runtime")] | ||
| fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> { | ||
| ensure!(!v0::SlotInfo::<T>::exists(), "SlotInfo should not exist"); | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| pub fn migrate<T: Config>() -> Weight { | ||
| v0::SlotInfo::<T>::kill(); | ||
skunert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| T::DbWeight::get().writes(1) | ||
| } | ||
| } | ||
|
|
||
| /// Migrate `V0` to `V1`. | ||
| pub type MigrateV0ToV1<T> = VersionedMigration< | ||
| 0, | ||
| 1, | ||
| v1::UncheckedMigrationToV1<T>, | ||
| Pallet<T>, | ||
| <T as frame_system::Config>::DbWeight, | ||
| >; | ||
Uh oh!
There was an error while loading. Please reload this page.