mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-17 20:35:41 +00:00
c8fda4f1b6
* paras: `include_pvf_check_statement` rt bench Resolves #4933 This PR adds a benchmark for the `include_pvf_check_statement` dispatchable. This is a necessary step to make it work without modifications. That enables us to proceed with testing on Versi. This introduces 5 new benchmarks. Those measure performance of the `include_pvf_check_statement` under 2 different conditions: 1. regular vote submission. That's the common case. 2. submission of the last vote. That happens only once and leads to a heavy finalization stage. There are 2 different types of finalization (one for onboarding, one for upgrading) and there are two outcomes: accepted and rejected. Those 4 are similar but I decided to cover them all and assign the maximum of all 4. This is to avoid a situation when one of those paths becomes more heavier than others and opens up an attack venue. The regular vote submission weight is drastically different from the submission last vote weight. That's why in case during runtime finalization was not executed the weight consumed value will be lowered down to the regular vote submission. The finalization weight is proportional to the number of "causes", i.e. the events that caused the PVF pre-checking vote in the first place, and here we assume that the maximum number of causes is 100. Theoretically, there is nothing that prevents an adversary to register/upgrade to more than 100 parachains. In that case, the consumed weight will be lower than the actual time consumed by the finalization process. That can enable a DoS vector. However, practically, it is not very possible. Right now it is very expensive to call `schedule_para_initialize` because it requires a very large lock up of funds. Moreover, finalizing a vote with 100 causes leads to around 31ms time spent. Finalizing more will require more time. However, finalizing with 200 causes will cause ≈62ms delay. This is not that bad since even though we had a full block and the adversary tried to finalize 200 causes it won't be able to even exceed the operational extrinsic boundary of 250ms and even if so it won't make big difference. That said, this should be addressed later on, esp. when we enable parathreads, which will make creating causes easier. One of potential solutions will be shifting the logic of finalization into `on_initialize`/`on_finalize`. Another is to create a maximum number of causes and then reject upgrades or onboardings if that was reached. * cargo run --quiet --profile=production --features=runtime-benchmarks -- benchmark --chain=polkadot-dev --steps=50 --repeat=20 --pallet=runtime_parachains::paras --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --output=./runtime/polkadot/src/weights/runtime_parachains_paras.rs * cargo run --quiet --profile=production --features=runtime-benchmarks -- benchmark --chain=kusama-dev --steps=50 --repeat=20 --pallet=runtime_parachains::paras --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --output=./runtime/kusama/src/weights/runtime_parachains_paras.rs * cargo run --quiet --profile=production --features=runtime-benchmarks -- benchmark --chain=westend-dev --steps=50 --repeat=20 --pallet=runtime_parachains::paras --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --output=./runtime/westend/src/weights/runtime_parachains_paras.rs * cargo run --quiet --profile=production --features runtime-benchmarks -- benchmark --chain=rococo-dev --steps=50 --repeat=20 --pallet=runtime_parachains::paras --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --output=./runtime/rococo/src/weights/runtime_parachains_paras.rs * Fix import error Co-authored-by: Parity Bot <admin@parity.io> Co-authored-by: Robert Klotzner <robert.klotzner@gmx.at> Co-authored-by: Lldenaurois <Ljdenaurois@gmail.com>
194 lines
6.4 KiB
Rust
194 lines
6.4 KiB
Rust
// Copyright 2021 Parity Technologies (UK) Ltd.
|
|
// This file is part of Polkadot.
|
|
|
|
// Polkadot 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.
|
|
|
|
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
use super::*;
|
|
use crate::configuration::HostConfiguration;
|
|
use frame_benchmarking::benchmarks;
|
|
use frame_system::RawOrigin;
|
|
use primitives::v2::{HeadData, Id as ParaId, ValidationCode, MAX_CODE_SIZE, MAX_HEAD_DATA_SIZE};
|
|
use sp_runtime::traits::{One, Saturating};
|
|
|
|
mod pvf_check;
|
|
|
|
use self::pvf_check::{VoteCause, VoteOutcome};
|
|
|
|
// 2 ^ 10, because binary search time complexity is O(log(2, n)) and n = 1024 gives us a big and
|
|
// round number.
|
|
// Due to the limited number of parachains, the number of pruning, upcoming upgrades and cooldowns
|
|
// shouldn't exceed this number.
|
|
const SAMPLE_SIZE: u32 = 1024;
|
|
|
|
fn assert_last_event<T: Config>(generic_event: <T as Config>::Event) {
|
|
let events = frame_system::Pallet::<T>::events();
|
|
let system_event: <T as frame_system::Config>::Event = generic_event.into();
|
|
// compare to the last event record
|
|
let frame_system::EventRecord { event, .. } = &events[events.len() - 1];
|
|
assert_eq!(event, &system_event);
|
|
}
|
|
|
|
fn generate_disordered_pruning<T: Config>() {
|
|
let mut needs_pruning = Vec::new();
|
|
|
|
for i in 0..SAMPLE_SIZE {
|
|
let id = ParaId::from(i);
|
|
let block_number = T::BlockNumber::from(1000u32);
|
|
needs_pruning.push((id, block_number));
|
|
}
|
|
|
|
<Pallet<T> as Store>::PastCodePruning::put(needs_pruning);
|
|
}
|
|
|
|
pub(crate) fn generate_disordered_upgrades<T: Config>() {
|
|
let mut upgrades = Vec::new();
|
|
let mut cooldowns = Vec::new();
|
|
|
|
for i in 0..SAMPLE_SIZE {
|
|
let id = ParaId::from(i);
|
|
let block_number = T::BlockNumber::from(1000u32);
|
|
upgrades.push((id, block_number));
|
|
cooldowns.push((id, block_number));
|
|
}
|
|
|
|
<Pallet<T> as Store>::UpcomingUpgrades::put(upgrades);
|
|
<Pallet<T> as Store>::UpgradeCooldowns::put(cooldowns);
|
|
}
|
|
|
|
fn generate_disordered_actions_queue<T: Config>() {
|
|
let mut queue = Vec::new();
|
|
let next_session = shared::Pallet::<T>::session_index().saturating_add(One::one());
|
|
|
|
for _ in 0..SAMPLE_SIZE {
|
|
let id = ParaId::from(1000);
|
|
queue.push(id);
|
|
}
|
|
|
|
<Pallet<T> as Store>::ActionsQueue::mutate(next_session, |v| {
|
|
*v = queue;
|
|
});
|
|
}
|
|
|
|
benchmarks! {
|
|
force_set_current_code {
|
|
let c in 1 .. MAX_CODE_SIZE;
|
|
let new_code = ValidationCode(vec![0; c as usize]);
|
|
let para_id = ParaId::from(c as u32);
|
|
<Pallet<T> as Store>::CurrentCodeHash::insert(¶_id, new_code.hash());
|
|
generate_disordered_pruning::<T>();
|
|
}: _(RawOrigin::Root, para_id, new_code)
|
|
verify {
|
|
assert_last_event::<T>(Event::CurrentCodeUpdated(para_id).into());
|
|
}
|
|
force_set_current_head {
|
|
let s in 1 .. MAX_HEAD_DATA_SIZE;
|
|
let new_head = HeadData(vec![0; s as usize]);
|
|
let para_id = ParaId::from(1000);
|
|
}: _(RawOrigin::Root, para_id, new_head)
|
|
verify {
|
|
assert_last_event::<T>(Event::CurrentHeadUpdated(para_id).into());
|
|
}
|
|
force_schedule_code_upgrade {
|
|
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);
|
|
generate_disordered_upgrades::<T>();
|
|
}: _(RawOrigin::Root, para_id, new_code, block)
|
|
verify {
|
|
assert_last_event::<T>(Event::CodeUpgradeScheduled(para_id).into());
|
|
}
|
|
force_note_new_head {
|
|
let s in 1 .. MAX_HEAD_DATA_SIZE;
|
|
let para_id = ParaId::from(1000);
|
|
let new_head = HeadData(vec![0; s as usize]);
|
|
let old_code_hash = ValidationCode(vec![0]).hash();
|
|
<Pallet<T> as Store>::CurrentCodeHash::insert(¶_id, old_code_hash);
|
|
// schedule an expired code upgrade for this `para_id` so that force_note_new_head would use
|
|
// the worst possible code path
|
|
let expired = frame_system::Pallet::<T>::block_number().saturating_sub(One::one());
|
|
let config = HostConfiguration::<T::BlockNumber>::default();
|
|
generate_disordered_pruning::<T>();
|
|
Pallet::<T>::schedule_code_upgrade(para_id, ValidationCode(vec![0]), expired, &config);
|
|
}: _(RawOrigin::Root, para_id, new_head)
|
|
verify {
|
|
assert_last_event::<T>(Event::NewHeadNoted(para_id).into());
|
|
}
|
|
force_queue_action {
|
|
let para_id = ParaId::from(1000);
|
|
generate_disordered_actions_queue::<T>();
|
|
}: _(RawOrigin::Root, para_id)
|
|
verify {
|
|
let next_session = crate::shared::Pallet::<T>::session_index().saturating_add(One::one());
|
|
assert_last_event::<T>(Event::ActionQueued(para_id, next_session).into());
|
|
}
|
|
|
|
add_trusted_validation_code {
|
|
let c in 1 .. MAX_CODE_SIZE;
|
|
let new_code = ValidationCode(vec![0; c as usize]);
|
|
}: _(RawOrigin::Root, new_code)
|
|
|
|
poke_unused_validation_code {
|
|
let code_hash = [0; 32].into();
|
|
}: _(RawOrigin::Root, code_hash)
|
|
|
|
include_pvf_check_statement {
|
|
let (stmt, signature) = pvf_check::prepare_inclusion_bench::<T>();
|
|
}: {
|
|
let _ = Pallet::<T>::include_pvf_check_statement(RawOrigin::None.into(), stmt, signature);
|
|
}
|
|
|
|
include_pvf_check_statement_finalize_upgrade_accept {
|
|
let (stmt, signature) = pvf_check::prepare_finalization_bench::<T>(
|
|
VoteCause::Upgrade,
|
|
VoteOutcome::Accept,
|
|
);
|
|
}: {
|
|
let _ = Pallet::<T>::include_pvf_check_statement(RawOrigin::None.into(), stmt, signature);
|
|
}
|
|
|
|
include_pvf_check_statement_finalize_upgrade_reject {
|
|
let (stmt, signature) = pvf_check::prepare_finalization_bench::<T>(
|
|
VoteCause::Upgrade,
|
|
VoteOutcome::Reject,
|
|
);
|
|
}: {
|
|
let _ = Pallet::<T>::include_pvf_check_statement(RawOrigin::None.into(), stmt, signature);
|
|
}
|
|
|
|
include_pvf_check_statement_finalize_onboarding_accept {
|
|
let (stmt, signature) = pvf_check::prepare_finalization_bench::<T>(
|
|
VoteCause::Onboarding,
|
|
VoteOutcome::Accept,
|
|
);
|
|
}: {
|
|
let _ = Pallet::<T>::include_pvf_check_statement(RawOrigin::None.into(), stmt, signature);
|
|
}
|
|
|
|
include_pvf_check_statement_finalize_onboarding_reject {
|
|
let (stmt, signature) = pvf_check::prepare_finalization_bench::<T>(
|
|
VoteCause::Onboarding,
|
|
VoteOutcome::Reject,
|
|
);
|
|
}: {
|
|
let _ = Pallet::<T>::include_pvf_check_statement(RawOrigin::None.into(), stmt, signature);
|
|
}
|
|
|
|
impl_benchmark_test_suite!(
|
|
Pallet,
|
|
crate::mock::new_test_ext(Default::default()),
|
|
crate::mock::Test
|
|
);
|
|
}
|