paras: count upgrade delay from inclusion (#7486)

* paras: count upgrade delay from inclusion

* fix warning

* rename check cause block number field

* rename inclusion_parent -> included_at
This commit is contained in:
Chris Sosnin
2023-07-19 15:06:58 +03:00
committed by GitHub
parent 3bbb336ea7
commit c63b557e50
3 changed files with 178 additions and 18 deletions
+33 -16
View File
@@ -367,17 +367,21 @@ impl TypeInfo for ParaKind {
/// This enum describes a reason why a particular PVF pre-checking vote was initiated. When the
/// PVF vote in question is concluded, this enum indicates what changes should be performed.
#[derive(Encode, Decode, TypeInfo)]
enum PvfCheckCause<BlockNumber> {
#[derive(Debug, Encode, Decode, TypeInfo)]
pub(crate) enum PvfCheckCause<BlockNumber> {
/// PVF vote was initiated by the initial onboarding process of the given para.
Onboarding(ParaId),
/// PVF vote was initiated by signalling of an upgrade by the given para.
Upgrade {
/// The ID of the parachain that initiated or is waiting for the conclusion of pre-checking.
id: ParaId,
/// The relay-chain block number that was used as the relay-parent for the parablock that
/// initiated the upgrade.
relay_parent_number: BlockNumber,
/// The relay-chain block number of **inclusion** of candidate that that initiated the upgrade.
///
/// It's important to count upgrade enactment delay from the inclusion of this candidate instead
/// of its relay parent -- in order to keep PVF available in case of chain reversions.
///
/// See https://github.com/paritytech/polkadot/issues/4601 for detailed explanation.
included_at: BlockNumber,
},
}
@@ -400,7 +404,7 @@ enum PvfCheckOutcome {
/// This struct describes the current state of an in-progress PVF pre-checking vote.
#[derive(Encode, Decode, TypeInfo)]
struct PvfCheckActiveVoteState<BlockNumber> {
pub(crate) struct PvfCheckActiveVoteState<BlockNumber> {
// The two following vectors have their length equal to the number of validators in the active
// set. They start with all zeroes. A 1 is set at an index when the validator at the that index
// makes a vote. Once a 1 is set for either of the vectors, that validator cannot vote anymore.
@@ -466,6 +470,11 @@ impl<BlockNumber> PvfCheckActiveVoteState<BlockNumber> {
None
}
}
#[cfg(test)]
pub(crate) fn causes(&self) -> &[PvfCheckCause<BlockNumber>] {
self.causes.as_slice()
}
}
pub trait WeightInfo {
@@ -1473,9 +1482,8 @@ impl<T: Config> Pallet<T> {
PvfCheckCause::Onboarding(id) => {
weight += Self::proceed_with_onboarding(*id, sessions_observed);
},
PvfCheckCause::Upgrade { id, relay_parent_number } => {
weight +=
Self::proceed_with_upgrade(*id, code_hash, now, *relay_parent_number, cfg);
PvfCheckCause::Upgrade { id, included_at } => {
weight += Self::proceed_with_upgrade(*id, code_hash, now, *included_at, cfg);
},
}
}
@@ -1519,10 +1527,10 @@ impl<T: Config> Pallet<T> {
// against the new validation code.
//
// Here we are trying to choose the block number that will have `validation_upgrade_delay`
// blocks from the relay-parent of the block that schedule code upgrade but no less than
// `minimum_validation_upgrade_delay`. We want this delay out of caution so that when
// the last vote for pre-checking comes the parachain will have some time until the upgrade
// finally takes place.
// blocks from the relay-parent of inclusion of the the block that scheduled code upgrade
// but no less than `minimum_validation_upgrade_delay`. We want this delay out of caution
// so that when the last vote for pre-checking comes the parachain will have some time until
// the upgrade finally takes place.
let expected_at = cmp::max(
relay_parent_number + cfg.validation_upgrade_delay,
now + cfg.minimum_validation_upgrade_delay,
@@ -1765,10 +1773,12 @@ impl<T: Config> Pallet<T> {
///
/// The new code should not be equal to the current one, otherwise the upgrade will be aborted.
/// If there is already a scheduled code upgrade for the para, this is a no-op.
///
/// Inclusion block number specifies relay parent which enacted candidate initiating the upgrade.
pub(crate) fn schedule_code_upgrade(
id: ParaId,
new_code: ValidationCode,
relay_parent_number: BlockNumberFor<T>,
inclusion_block_number: BlockNumberFor<T>,
cfg: &configuration::HostConfiguration<BlockNumberFor<T>>,
) -> Weight {
let mut weight = T::DbWeight::get().reads(1);
@@ -1810,7 +1820,7 @@ impl<T: Config> Pallet<T> {
UpgradeRestrictionSignal::<T>::insert(&id, UpgradeRestriction::Present);
weight += T::DbWeight::get().reads_writes(1, 1);
let next_possible_upgrade_at = relay_parent_number + cfg.validation_upgrade_cooldown;
let next_possible_upgrade_at = inclusion_block_number + cfg.validation_upgrade_cooldown;
UpgradeCooldowns::<T>::mutate(|upgrade_cooldowns| {
let insert_idx = upgrade_cooldowns
.binary_search_by_key(&next_possible_upgrade_at, |&(_, b)| b)
@@ -1819,7 +1829,7 @@ impl<T: Config> Pallet<T> {
});
weight += Self::kick_off_pvf_check(
PvfCheckCause::Upgrade { id, relay_parent_number },
PvfCheckCause::Upgrade { id, included_at: inclusion_block_number },
code_hash,
new_code,
cfg,
@@ -2120,6 +2130,13 @@ impl<T: Config> Pallet<T> {
Heads::<T>::insert(&id, &genesis_data.genesis_head);
}
#[cfg(test)]
pub(crate) fn active_vote_state(
code_hash: &ValidationCodeHash,
) -> Option<PvfCheckActiveVoteState<BlockNumberFor<T>>> {
PvfActiveVoteMap::<T>::get(code_hash)
}
}
/// An overlay over the `Parachains` storage entry that provides a convenient interface for adding