referrerpolicy=no-referrer-when-downgrade

bridge_runtime_common/
parachains_benchmarking.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
3
4// Parity Bridges Common is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Bridges Common is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Bridges Common.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Everything required to run benchmarks of parachains finality module.
18
19#![cfg(feature = "runtime-benchmarks")]
20
21use crate::messages_benchmarking::insert_header_to_grandpa_pallet;
22
23use bp_parachains::{
24	parachain_head_storage_key_at_source, RelayBlockHash, RelayBlockHasher, RelayBlockNumber,
25};
26use bp_polkadot_core::parachains::{ParaHash, ParaHead, ParaHeadsProof, ParaId};
27use bp_runtime::{grow_storage_value, record_all_trie_keys, Chain, UnverifiedStorageProofParams};
28use codec::Encode;
29use frame_support::traits::Get;
30use sp_std::prelude::*;
31use sp_trie::{trie_types::TrieDBMutBuilderV1, LayoutV1, MemoryDB, TrieMut};
32
33/// Prepare proof of messages for the `receive_messages_proof` call.
34///
35/// In addition to returning valid messages proof, environment is prepared to verify this message
36/// proof.
37pub fn prepare_parachain_heads_proof<R, PI>(
38	parachains: &[ParaId],
39	parachain_head_size: u32,
40	proof_params: UnverifiedStorageProofParams,
41) -> (RelayBlockNumber, RelayBlockHash, ParaHeadsProof, Vec<(ParaId, ParaHash)>)
42where
43	R: pallet_bridge_parachains::Config<PI>
44		+ pallet_bridge_grandpa::Config<R::BridgesGrandpaPalletInstance>,
45	PI: 'static,
46	<R as pallet_bridge_grandpa::Config<R::BridgesGrandpaPalletInstance>>::BridgedChain:
47		Chain<BlockNumber = RelayBlockNumber, Hash = RelayBlockHash>,
48{
49	let parachain_head = ParaHead(vec![0u8; parachain_head_size as usize]);
50
51	// insert all heads to the trie
52	let mut parachain_heads = Vec::with_capacity(parachains.len());
53	let mut storage_keys = Vec::with_capacity(parachains.len());
54	let mut state_root = Default::default();
55	let mut mdb = MemoryDB::default();
56	{
57		let mut trie =
58			TrieDBMutBuilderV1::<RelayBlockHasher>::new(&mut mdb, &mut state_root).build();
59
60		// insert parachain heads
61		for (i, parachain) in parachains.into_iter().enumerate() {
62			let storage_key =
63				parachain_head_storage_key_at_source(R::ParasPalletName::get(), *parachain);
64			let leaf_data = if i == 0 {
65				grow_storage_value(parachain_head.encode(), &proof_params)
66			} else {
67				parachain_head.encode()
68			};
69			trie.insert(&storage_key.0, &leaf_data)
70				.map_err(|_| "TrieMut::insert has failed")
71				.expect("TrieMut::insert should not fail in benchmarks");
72			storage_keys.push(storage_key);
73			parachain_heads.push((*parachain, parachain_head.hash()))
74		}
75	}
76
77	// generate heads storage proof
78	let proof = record_all_trie_keys::<LayoutV1<RelayBlockHasher>, _>(&mdb, &state_root)
79		.map_err(|_| "record_all_trie_keys has failed")
80		.expect("record_all_trie_keys should not fail in benchmarks");
81
82	let (relay_block_number, relay_block_hash) =
83		insert_header_to_grandpa_pallet::<R, R::BridgesGrandpaPalletInstance>(state_root);
84
85	(relay_block_number, relay_block_hash, ParaHeadsProof { storage_proof: proof }, parachain_heads)
86}