Parachains pallet benchmarks (#1436)

* added parachains pallet benchmarks

* deduplicate insertion of bridged header

* pruning weight

* fixes

* fix compilation
This commit is contained in:
Svyatoslav Nikolsky
2022-06-07 10:50:55 +03:00
committed by Bastian Köcher
parent 690a929cf6
commit b870fe74f8
20 changed files with 565 additions and 74 deletions
+1
View File
@@ -21,6 +21,7 @@
pub mod messages;
pub mod messages_api;
pub mod messages_benchmarking;
pub mod parachains_benchmarking;
#[cfg(feature = "integrity-test")]
pub mod integrity;
@@ -27,10 +27,11 @@ use crate::messages::{
};
use bp_messages::{storage_keys, MessageData, MessageKey, MessagePayload};
use bp_runtime::StorageProofSize;
use codec::Encode;
use frame_support::weights::{GetDispatchInfo, Weight};
use pallet_bridge_messages::benchmarking::{
MessageDeliveryProofParams, MessageParams, MessageProofParams, ProofSize,
MessageDeliveryProofParams, MessageParams, MessageProofParams,
};
use sp_core::Hasher;
use sp_runtime::traits::{Header, IdentifyAccount, MaybeSerializeDeserialize, Zero};
@@ -59,7 +60,7 @@ where
R: frame_system::Config<AccountId = AccountIdOf<ThisChain<B>>>
+ pallet_balances::Config<BI, Balance = BalanceOf<ThisChain<B>>>
+ pallet_bridge_grandpa::Config<FI>,
R::BridgedChain: bp_runtime::Chain<Header = BH>,
R::BridgedChain: bp_runtime::Chain<Hash = HashOf<BridgedChain<B>>, Header = BH>,
B: MessageBridge,
BI: 'static,
FI: 'static,
@@ -76,14 +77,14 @@ where
+ IdentifyAccount<AccountId = AccountIdOf<ThisChain<B>>>,
{
let message_payload = match params.size {
ProofSize::Minimal(ref size) => vec![0u8; *size as _],
StorageProofSize::Minimal(ref size) => vec![0u8; *size as _],
_ => vec![],
};
// finally - prepare storage proof and update environment
let (state_root, storage_proof) =
prepare_messages_storage_proof::<B, BHH>(&params, message_payload);
let bridged_header_hash = insert_bridged_chain_header::<R, FI, B, BH>(state_root);
let bridged_header_hash = insert_header_to_grandpa_pallet::<R, FI>(state_root);
(
FromBridgedChainMessagesProof {
@@ -103,7 +104,7 @@ pub fn prepare_message_delivery_proof<R, FI, B, BH, BHH>(
) -> FromBridgedChainMessagesDeliveryProof<HashOf<BridgedChain<B>>>
where
R: pallet_bridge_grandpa::Config<FI>,
R::BridgedChain: bp_runtime::Chain<Header = BH>,
R::BridgedChain: bp_runtime::Chain<Hash = HashOf<BridgedChain<B>>, Header = BH>,
FI: 'static,
B: MessageBridge,
BH: Header<Hash = HashOf<BridgedChain<B>>>,
@@ -131,7 +132,7 @@ where
let storage_proof = proof_recorder.drain().into_iter().map(|n| n.data.to_vec()).collect();
// finally insert header with given state root to our storage
let bridged_header_hash = insert_bridged_chain_header::<R, FI, B, BH>(root);
let bridged_header_hash = insert_header_to_grandpa_pallet::<R, FI>(root);
FromBridgedChainMessagesDeliveryProof {
bridged_header_hash: bridged_header_hash.into(),
@@ -203,19 +204,16 @@ where
(root, storage_proof)
}
/// Insert Bridged chain header with given state root into storage of GRANDPA pallet at This chain.
fn insert_bridged_chain_header<R, FI, B, BH>(
state_root: HashOf<BridgedChain<B>>,
) -> HashOf<BridgedChain<B>>
/// Insert header to the bridge GRANDPA pallet.
pub(crate) fn insert_header_to_grandpa_pallet<R, GI>(
state_root: bp_runtime::HashOf<R::BridgedChain>,
) -> bp_runtime::HashOf<R::BridgedChain>
where
R: pallet_bridge_grandpa::Config<FI>,
R::BridgedChain: bp_runtime::Chain<Header = BH>,
FI: 'static,
B: MessageBridge,
BH: Header<Hash = HashOf<BridgedChain<B>>>,
HashOf<BridgedChain<B>>: Default,
R: pallet_bridge_grandpa::Config<GI>,
GI: 'static,
R::BridgedChain: bp_runtime::Chain,
{
let bridged_header = BH::new(
let bridged_header = bp_runtime::HeaderOf::<R::BridgedChain>::new(
Zero::zero(),
Default::default(),
state_root,
@@ -223,16 +221,20 @@ where
Default::default(),
);
let bridged_header_hash = bridged_header.hash();
pallet_bridge_grandpa::initialize_for_benchmarks::<R, FI>(bridged_header);
pallet_bridge_grandpa::initialize_for_benchmarks::<R, GI>(bridged_header);
bridged_header_hash
}
/// Populate trie with dummy keys+values until trie has at least given size.
fn grow_trie<H: Hasher>(mut root: H::Out, mdb: &mut MemoryDB<H>, trie_size: ProofSize) -> H::Out {
pub fn grow_trie<H: Hasher>(
mut root: H::Out,
mdb: &mut MemoryDB<H>,
trie_size: StorageProofSize,
) -> H::Out {
let (iterations, leaf_size, minimal_trie_size) = match trie_size {
ProofSize::Minimal(_) => return root,
ProofSize::HasLargeLeaf(size) => (1, size, size),
ProofSize::HasExtraNodes(size) => (8, 1, size),
StorageProofSize::Minimal(_) => return root,
StorageProofSize::HasLargeLeaf(size) => (1, size, size),
StorageProofSize::HasExtraNodes(size) => (8, 1, size),
};
let mut key_index = 0;
@@ -0,0 +1,80 @@
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of Parity Bridges Common.
// Parity Bridges Common 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.
// Parity Bridges Common 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 Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
//! Everything required to run benchmarks of parachains finality module.
#![cfg(feature = "runtime-benchmarks")]
use crate::messages_benchmarking::{grow_trie, insert_header_to_grandpa_pallet};
use bp_parachains::parachain_head_storage_key_at_source;
use bp_polkadot_core::parachains::{ParaHead, ParaHeadsProof, ParaId};
use bp_runtime::StorageProofSize;
use codec::Encode;
use frame_support::traits::Get;
use pallet_bridge_parachains::{RelayBlockHash, RelayBlockHasher};
use sp_std::prelude::*;
use sp_trie::{record_all_keys, trie_types::TrieDBMutV1, LayoutV1, MemoryDB, Recorder, TrieMut};
/// Prepare proof of messages for the `receive_messages_proof` call.
///
/// In addition to returning valid messages proof, environment is prepared to verify this message
/// proof.
pub fn prepare_parachain_heads_proof<R, PI>(
parachains: &[ParaId],
parachain_head_size: u32,
size: StorageProofSize,
) -> (RelayBlockHash, ParaHeadsProof)
where
R: pallet_bridge_parachains::Config<PI>
+ pallet_bridge_grandpa::Config<R::BridgesGrandpaPalletInstance>,
PI: 'static,
<R as pallet_bridge_grandpa::Config<R::BridgesGrandpaPalletInstance>>::BridgedChain:
bp_runtime::Chain<Hash = RelayBlockHash>,
{
let parachain_head = ParaHead(vec![0u8; parachain_head_size as usize]);
// insert all heads to the trie
let mut storage_keys = Vec::with_capacity(parachains.len());
let mut state_root = Default::default();
let mut mdb = MemoryDB::default();
{
let mut trie = TrieDBMutV1::<RelayBlockHasher>::new(&mut mdb, &mut state_root);
// insert parachain heads
for parachain in parachains {
let storage_key =
parachain_head_storage_key_at_source(R::ParasPalletName::get(), *parachain);
trie.insert(&storage_key.0, &parachain_head.encode())
.map_err(|_| "TrieMut::insert has failed")
.expect("TrieMut::insert should not fail in benchmarks");
storage_keys.push(storage_key);
}
}
state_root = grow_trie(state_root, &mut mdb, size);
// generate heads storage proof
let mut proof_recorder = Recorder::<RelayBlockHash>::new();
record_all_keys::<LayoutV1<RelayBlockHasher>, _>(&mdb, &state_root, &mut proof_recorder)
.map_err(|_| "record_all_keys has failed")
.expect("record_all_keys should not fail in benchmarks");
let proof = proof_recorder.drain().into_iter().map(|n| n.data.to_vec()).collect();
let relay_block_hash =
insert_header_to_grandpa_pallet::<R, R::BridgesGrandpaPalletInstance>(state_root);
(relay_block_hash, ParaHeadsProof(proof))
}