// 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 . //! 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( parachains: &[ParaId], parachain_head_size: u32, size: StorageProofSize, ) -> (RelayBlockHash, ParaHeadsProof) where R: pallet_bridge_parachains::Config + pallet_bridge_grandpa::Config, PI: 'static, >::BridgedChain: bp_runtime::Chain, { 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::::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, ¶chain_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::::new(); record_all_keys::, _>(&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::(state_root); (relay_block_hash, ParaHeadsProof(proof)) }