From 2c095ba1d3a484b1349baf950fad5415c732d849 Mon Sep 17 00:00:00 2001 From: Chris Sosnin Date: Fri, 25 Nov 2022 17:05:41 +0400 Subject: [PATCH] paras: adjust weights for force upgrade --- .../src/weights/runtime_parachains_paras.rs | 3 ++ runtime/parachains/src/paras/benchmarking.rs | 14 ++++++++- .../src/paras/benchmarking/pvf_check.rs | 13 ++++++--- runtime/parachains/src/paras/mod.rs | 29 ++++++++++++++++--- .../src/weights/runtime_parachains_paras.rs | 3 ++ .../src/weights/runtime_parachains_paras.rs | 3 ++ .../src/weights/runtime_parachains_paras.rs | 3 ++ 7 files changed, 59 insertions(+), 9 deletions(-) diff --git a/runtime/kusama/src/weights/runtime_parachains_paras.rs b/runtime/kusama/src/weights/runtime_parachains_paras.rs index 4b42562c8566..12006878cb22 100644 --- a/runtime/kusama/src/weights/runtime_parachains_paras.rs +++ b/runtime/kusama/src/weights/runtime_parachains_paras.rs @@ -176,4 +176,7 @@ impl runtime_parachains::paras::WeightInfo for WeightIn .saturating_add(T::DbWeight::get().reads(5 as u64)) .saturating_add(T::DbWeight::get().writes(304 as u64)) } + fn force_schedule_code_upgrade_pvf_checking_enabled(_c: u32) -> Weight { + Weight::MAX + } } diff --git a/runtime/parachains/src/paras/benchmarking.rs b/runtime/parachains/src/paras/benchmarking.rs index 9970937f4558..ae1292a95591 100644 --- a/runtime/parachains/src/paras/benchmarking.rs +++ b/runtime/parachains/src/paras/benchmarking.rs @@ -23,7 +23,7 @@ use sp_runtime::traits::{One, Saturating}; mod pvf_check; -use self::pvf_check::{VoteCause, VoteOutcome}; +use self::pvf_check::{enable_pvf_checking, VoteCause, VoteOutcome}; // 2 ^ 10, because binary search time complexity is O(log(2, n)) and n = 1024 gives us a big and // round number. @@ -109,6 +109,18 @@ benchmarks! { verify { assert_last_event::(Event::CodeUpgradeScheduled(para_id).into()); } + force_schedule_code_upgrade_pvf_checking_enabled { + let c in 1 .. MAX_CODE_SIZE; + let new_code = ValidationCode(vec![0; c as usize]); + let para_id = ParaId::from(c as u32); + let block = T::BlockNumber::from(c); + + enable_pvf_checking::(); + generate_disordered_upgrades::(); + }: _(RawOrigin::Root, para_id, new_code, block) + verify { + assert_last_event::(Event::CodeUpgradeScheduled(para_id).into()); + } force_note_new_head { let s in 1 .. MAX_HEAD_DATA_SIZE; let para_id = ParaId::from(1000); diff --git a/runtime/parachains/src/paras/benchmarking/pvf_check.rs b/runtime/parachains/src/paras/benchmarking/pvf_check.rs index e1400ac4297d..5dfa13893287 100644 --- a/runtime/parachains/src/paras/benchmarking/pvf_check.rs +++ b/runtime/parachains/src/paras/benchmarking/pvf_check.rs @@ -32,6 +32,13 @@ fn old_validation_code() -> ValidationCode { ValidationCode(vec![1]) } +/// Enables pvf-checking in the configuration pallet. +pub fn enable_pvf_checking() { + let mut config = configuration::Pallet::::config(); + config.pvf_checking_enabled = true; + configuration::Pallet::::force_set_active_config(config); +} + /// Prepares the PVF check statement and the validator signature to pass into /// `include_pvf_check_statement` during benchmarking phase. /// @@ -109,9 +116,7 @@ where .collect::>(); // 1. Make sure PVF pre-checking is enabled in the config. - let mut config = configuration::Pallet::::config(); - config.pvf_checking_enabled = true; - configuration::Pallet::::force_set_active_config(config.clone()); + enable_pvf_checking::(); // 2. initialize a new session with deterministic validator set. ParasShared::::set_active_validators_ascending(validators.clone()); @@ -122,7 +127,7 @@ where /// /// The subject of the vote (i.e. validation code) and the cause (upgrade/onboarding) is specified /// by the test setup. -fn initialize_pvf_active_vote(vote_cause: VoteCause) +pub fn initialize_pvf_active_vote(vote_cause: VoteCause) where T: Config + shared::Config, { diff --git a/runtime/parachains/src/paras/mod.rs b/runtime/parachains/src/paras/mod.rs index 3c5744b96546..4f5fdaf13844 100644 --- a/runtime/parachains/src/paras/mod.rs +++ b/runtime/parachains/src/paras/mod.rs @@ -467,9 +467,12 @@ impl PvfCheckActiveVoteState { pub trait WeightInfo { fn force_set_current_code(c: u32) -> Weight; fn force_set_current_head(s: u32) -> Weight; - fn force_schedule_code_upgrade(c: u32) -> Weight; fn force_note_new_head(s: u32) -> Weight; fn force_queue_action() -> Weight; + + fn force_schedule_code_upgrade(c: u32) -> Weight; + fn force_schedule_code_upgrade_pvf_checking_enabled(c: u32) -> Weight; + fn add_trusted_validation_code(c: u32) -> Weight; fn poke_unused_validation_code() -> Weight; @@ -491,6 +494,9 @@ impl WeightInfo for TestWeightInfo { fn force_schedule_code_upgrade(_c: u32) -> Weight { Weight::MAX } + fn force_schedule_code_upgrade_pvf_checking_enabled(_c: u32) -> Weight { + Weight::MAX + } fn force_note_new_head(_s: u32) -> Weight { Weight::MAX } @@ -827,18 +833,33 @@ pub mod pallet { } /// Schedule an upgrade as if it was scheduled in the given relay parent block. - #[pallet::weight(::WeightInfo::force_schedule_code_upgrade(new_code.0.len() as u32))] + #[pallet::weight( + ::WeightInfo::force_schedule_code_upgrade(new_code.0.len() as u32).max( + ::WeightInfo::force_schedule_code_upgrade_pvf_checking_enabled( + new_code.0.len() as u32 + ) + ) + )] pub fn force_schedule_code_upgrade( origin: OriginFor, para: ParaId, new_code: ValidationCode, relay_parent_number: T::BlockNumber, - ) -> DispatchResult { + ) -> DispatchResultWithPostInfo { ensure_root(origin)?; let config = configuration::Pallet::::config(); + let new_code_len = new_code.0.len() as u32; Self::schedule_code_upgrade(para, new_code, relay_parent_number, &config); Self::deposit_event(Event::CodeUpgradeScheduled(para)); - Ok(()) + + let actual_weight = if config.pvf_checking_enabled { + ::WeightInfo::force_schedule_code_upgrade_pvf_checking_enabled( + new_code_len, + ) + } else { + ::WeightInfo::force_schedule_code_upgrade(new_code_len) + }; + Ok(Some(actual_weight).into()) } /// Note a new block head for para within the context of the current block. diff --git a/runtime/polkadot/src/weights/runtime_parachains_paras.rs b/runtime/polkadot/src/weights/runtime_parachains_paras.rs index bf81061f31ef..ea342ede300a 100644 --- a/runtime/polkadot/src/weights/runtime_parachains_paras.rs +++ b/runtime/polkadot/src/weights/runtime_parachains_paras.rs @@ -182,4 +182,7 @@ impl runtime_parachains::paras::WeightInfo for WeightIn .saturating_add(T::DbWeight::get().reads(6 as u64)) .saturating_add(T::DbWeight::get().writes(304 as u64)) } + fn force_schedule_code_upgrade_pvf_checking_enabled(_c: u32) -> Weight { + Weight::MAX + } } diff --git a/runtime/rococo/src/weights/runtime_parachains_paras.rs b/runtime/rococo/src/weights/runtime_parachains_paras.rs index ebc8ea4c6909..f2508a759a46 100644 --- a/runtime/rococo/src/weights/runtime_parachains_paras.rs +++ b/runtime/rococo/src/weights/runtime_parachains_paras.rs @@ -182,4 +182,7 @@ impl runtime_parachains::paras::WeightInfo for WeightIn .saturating_add(T::DbWeight::get().reads(6 as u64)) .saturating_add(T::DbWeight::get().writes(304 as u64)) } + fn force_schedule_code_upgrade_pvf_checking_enabled(_c: u32) -> Weight { + Weight::MAX + } } diff --git a/runtime/westend/src/weights/runtime_parachains_paras.rs b/runtime/westend/src/weights/runtime_parachains_paras.rs index 62de4f872ba9..4009971fdf96 100644 --- a/runtime/westend/src/weights/runtime_parachains_paras.rs +++ b/runtime/westend/src/weights/runtime_parachains_paras.rs @@ -176,4 +176,7 @@ impl runtime_parachains::paras::WeightInfo for WeightIn .saturating_add(T::DbWeight::get().reads(5 as u64)) .saturating_add(T::DbWeight::get().writes(304 as u64)) } + fn force_schedule_code_upgrade_pvf_checking_enabled(_c: u32) -> Weight { + Weight::MAX + } }