-
Notifications
You must be signed in to change notification settings - Fork 740
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
Parachains: Use relay chain slot for velocity measurement #6825
base: master
Are you sure you want to change the base?
Changes from all commits
2cea63d
3086345
ad5b1af
2305334
a023784
b69df30
ee45c93
665039b
720bd03
73ba8d1
bd1acea
6b8ce29
db26437
74992c2
7a7aa52
d78df61
8f03a9f
ce0a28c
31a91d3
a2d5936
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 |
---|---|---|
|
@@ -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(); | ||
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(); | ||
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. By just killing this item and not setting 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. Hmm I don't agree. Collators could in theory build up to |
||
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, | ||
>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think can this be affected by #64, which leads to reading corrupted data here.
if block N updates the runtime, the code is overwritten immediately. The migration is run at the beginning of block N+1. Therefore, when we'll call
can_build_upon
at the end of block N, we'll use the new code with the old stateThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will not read corrupted state, as
RelaySlotInfo
doesn't exist yet.