Skip to content
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

benchmark parachain-system #75

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion pallets/parachain-system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sp-state-machine = { git = "https://github.com/paritytech/substrate", default-fe
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
sp-trie = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
sp-version = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", default-features = false, optional = true, branch = "master" }

# Polkadot
polkadot-parachain = { git = "https://github.com/paritytech/polkadot", default-features = false, features = [ "wasm-api" ], branch = "master" }
Expand Down Expand Up @@ -70,7 +71,9 @@ std = [
]

runtime-benchmarks = [
"sp-runtime/runtime-benchmarks"
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]

try-runtime = ["frame-support/try-runtime"]
124 changes: 124 additions & 0 deletions pallets/parachain-system/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Benchmarking setup for pallet-collator-selection

#![allow(unused)]

use super::*;

use crate::{Pallet, PendingUpwardMessages, PendingValidationCode, ValidationData};
use cumulus_primitives_core::relay_chain::v2::HeadData;
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_support::{
assert_ok,
codec::Decode,
traits::{Currency, EnsureOrigin, Get},
};
use frame_system::{EnsureNone, EnsureRoot, EnsureSigned, EventRecord, RawOrigin};
use sp_core::H256;
use sp_std::{collections::btree_set::BTreeSet, prelude::*};
use sp_trie::StorageProof;

const SEED: u32 = 0;

benchmarks! {
set_validation_data_no_messages {
// root and proof are obtained via `RelayStateSproofBuilder::default().into_state_root_and_proof()`
let root = [
87, 247, 132, 202, 60, 197, 129, 248, 29, 30, 75, 209, 87, 82, 217, 27, 193, 91, 133, 158, 57,
219, 5, 125, 31, 120, 73, 108, 83, 161, 122, 141,
];
let proof = vec![
vec![
0, 0, 32, 0, 0, 0, 16, 0, 8, 0, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0, 6, 0,
0, 0, 6, 0, 0, 0
],
vec![
63, 32, 6, 222, 61, 138, 84, 210, 126, 68, 169, 213, 206, 24, 150, 24, 242, 45, 180, 180, 157,
149, 50, 13, 144, 33, 153, 76, 133, 15, 37, 184, 227, 133, 122, 95, 83, 148, 165, 203, 236, 87,
253, 11, 60, 82, 36, 95, 199, 120, 54, 22, 228, 227, 110, 231, 204, 83, 94, 179, 154, 8, 200,
180, 89, 235,
],
vec![
127, 0, 12, 182, 243, 110, 2, 122, 187, 32, 145, 207, 181, 17, 10, 181, 8, 127, 6, 21, 91, 60, 217,
168, 201, 229, 233, 162, 63, 213, 220, 19, 165, 237, 32, 0, 0, 0, 0, 0, 0, 0, 0,
],
vec![
128, 3, 0, 128, 242, 238, 200, 199, 170, 3, 233, 7, 253, 11, 161, 204, 28, 109, 207, 110, 116, 240,
105, 181, 38, 64, 118, 111, 3, 222, 55, 184, 183, 81, 181, 61, 128, 30, 47, 17, 234, 172, 118, 214,
115, 102, 113, 126, 126, 111, 149, 151, 252, 216, 113, 234, 7, 17, 48, 53, 220, 0, 90, 170, 247, 242,
1, 69, 8,
],
];

let data = ParachainInherentData {
validation_data: PersistedValidationData {
parent_head: vec![].into(),
relay_parent_number: 1,
relay_parent_storage_root: H256::from_slice(&root),
max_pov_size: Default::default(),
},
relay_chain_state: StorageProof::new(proof),
downward_messages: vec![],
horizontal_messages: BTreeMap::new(),
};
}: { assert_ok!(<Pallet<T>>::set_validation_data(EnsureNone::successful_origin(), data)); }
verify {
assert_eq!(<ValidationData<T>>::get().expect("validation data must exist").relay_parent_number, 1);
}

sudo_send_upward_message {
}: { assert_ok!(<Pallet<T>>::sudo_send_upward_message(EnsureRoot::successful_origin(), vec![])); }
verify {
assert_eq!(<PendingUpwardMessages<T>>::get().len(), 1);
}

authorize_upgrade {
let hash = T::Hashing::hash(vec![0u8; 32].as_slice());
}: { assert_ok!(<Pallet<T>>::authorize_upgrade(EnsureRoot::successful_origin(), hash.clone())); }
verify {
assert_eq!(<AuthorizedUpgrade<T>>::get(), Some(hash));
}

enact_authorized_upgrade {
let code = vec![0u8; 32];
let hash = T::Hashing::hash(code.as_slice());
<ValidationData<T>>::put(PersistedValidationData {
parent_head: vec![].into(),
relay_parent_number: Default::default(),
max_pov_size: Default::default(),
relay_parent_storage_root: Default::default(),
});
<HostConfiguration<T>>::put(AbridgedHostConfiguration {
max_code_size: 2 * 1024 * 1024,
max_head_data_size: 1024 * 1024,
max_upward_queue_count: 8,
max_upward_queue_size: 1024,
max_upward_message_size: 256,
max_upward_message_num_per_candidate: 5,
hrmp_max_message_num_per_candidate: 5,
validation_upgrade_cooldown: 6,
validation_upgrade_delay: 6,
});
assert_ok!(<Pallet<T>>::authorize_upgrade(EnsureRoot::successful_origin(), hash));
}: { assert_ok!(<Pallet<T>>::enact_authorized_upgrade(EnsureRoot::successful_origin(), code.clone())); }
verify {
assert_eq!(<AuthorizedUpgrade<T>>::get(), None);
assert_eq!(<PendingValidationCode<T>>::get(), code);
}
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test,);
20 changes: 14 additions & 6 deletions pallets/parachain-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,17 @@ use sp_std::{cmp, collections::btree_map::BTreeMap, prelude::*};

mod migration;
mod relay_state_snapshot;
pub mod weights;
#[macro_use]
pub mod validate_block;
#[cfg(test)]
mod tests;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
#[cfg(test)]
mod mock;

/// Register the `validate_block` function that is used by parachains to validate blocks on a
/// validator.
///
Expand Down Expand Up @@ -136,6 +142,7 @@ impl CheckAssociatedRelayNumber for AnyRelayNumber {
#[frame_support::pallet]
pub mod pallet {
use super::*;
pub use crate::weights::WeightInfo;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

Expand Down Expand Up @@ -177,6 +184,9 @@ pub mod pallet {

/// Something that can check the associated relay parent block number.
type CheckAssociatedRelayNumber: CheckAssociatedRelayNumber;

/// The weight information of this pallet.
type WeightInfo: WeightInfo;
}

#[pallet::hooks]
Expand Down Expand Up @@ -336,8 +346,7 @@ pub mod pallet {
/// As a side effect, this function upgrades the current validation function
/// if the appropriate time has come.
#[pallet::call_index(0)]
#[pallet::weight((0, DispatchClass::Mandatory))]
// TODO: This weight should be corrected.
#[pallet::weight((T::WeightInfo::set_validation_data_no_messages(), DispatchClass::Mandatory))]
pub fn set_validation_data(
origin: OriginFor<T>,
data: ParachainInherentData,
Expand Down Expand Up @@ -428,12 +437,11 @@ pub mod pallet {
horizontal_messages,
vfp.relay_parent_number,
);

Ok(PostDispatchInfo { actual_weight: Some(total_weight), pays_fee: Pays::No })
}

#[pallet::call_index(1)]
#[pallet::weight((1_000, DispatchClass::Operational))]
#[pallet::weight((T::WeightInfo::sudo_send_upward_message(), DispatchClass::Operational))]
pub fn sudo_send_upward_message(
origin: OriginFor<T>,
message: UpwardMessage,
Expand All @@ -444,7 +452,7 @@ pub mod pallet {
}

#[pallet::call_index(2)]
#[pallet::weight((1_000_000, DispatchClass::Operational))]
#[pallet::weight((T::WeightInfo::authorize_upgrade(), DispatchClass::Operational))]
pub fn authorize_upgrade(origin: OriginFor<T>, code_hash: T::Hash) -> DispatchResult {
ensure_root(origin)?;

Expand All @@ -455,7 +463,7 @@ pub mod pallet {
}

#[pallet::call_index(3)]
#[pallet::weight(1_000_000)]
#[pallet::weight(T::WeightInfo::enact_authorized_upgrade())]
pub fn enact_authorized_upgrade(
_: OriginFor<T>,
code: Vec<u8>,
Expand Down
92 changes: 92 additions & 0 deletions pallets/parachain-system/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#![allow(unused)]

use super::*;

use codec::Encode;
use cumulus_primitives_core::{
relay_chain::BlockNumber as RelayBlockNumber, AbridgedHrmpChannel, InboundDownwardMessage,
InboundHrmpMessage, PersistedValidationData,
};
use frame_support::{
assert_ok,
dispatch::UnfilteredDispatchable,
inherent::{InherentData, ProvideInherent},
parameter_types,
traits::{OnFinalize, OnInitialize},
weights::Weight,
};
use frame_system::RawOrigin;
use relay_chain::v2::HrmpChannelId;
use sp_core::H256;
use sp_runtime::traits::{BlakeTwo256, IdentityLookup};
use sp_version::RuntimeVersion;

use crate as parachain_system;

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
ParachainSystem: parachain_system::{Pallet, Call, Config, Storage, Inherent, Event<T>, ValidateUnsigned},
}
);

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub Version: RuntimeVersion = RuntimeVersion {
spec_name: sp_version::create_runtime_str!("test"),
impl_name: sp_version::create_runtime_str!("system-test"),
authoring_version: 1,
spec_version: 1,
impl_version: 1,
apis: sp_version::create_apis_vec!([]),
transaction_version: 1,
state_version: 1,
};
pub const ParachainId: ParaId = ParaId::new(200);
pub const ReservedXcmpWeight: Weight = 0;
pub const ReservedDmpWeight: Weight = 0;
}
impl frame_system::Config for Test {
type Origin = Origin;
type Call = Call;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = sp_runtime::generic::Header<u64, BlakeTwo256>;
type Event = Event;
type BlockHashCount = BlockHashCount;
type BlockLength = ();
type BlockWeights = ();
type Version = Version;
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type DbWeight = ();
type BaseCallFilter = frame_support::traits::Everything;
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ParachainSetCode<Self>;
type MaxConsumers = frame_support::traits::ConstU32<16>;
}
impl Config for Test {
type Event = Event;
type OnSystemEvent = ();
type SelfParaId = ParachainId;
type OutboundXcmpMessageSource = ();
type DmpMessageHandler = ();
type ReservedDmpWeight = ReservedDmpWeight;
type XcmpMessageHandler = ();
type ReservedXcmpWeight = ReservedXcmpWeight;
type CheckAssociatedRelayNumber = RelayNumberStrictlyIncreases;
}
Loading