Reintroduce header chain trait (#1622)

* reintroduce header chain trait

* renive BridgedChainWithMessages::maximal_extrinsic_size
This commit is contained in:
Svyatoslav Nikolsky
2022-11-11 15:31:01 +03:00
committed by Bastian Köcher
parent 3e00c5c022
commit 6dcecf4425
19 changed files with 648 additions and 831 deletions
@@ -49,36 +49,6 @@ macro_rules! assert_chain_types(
}
);
/// Macro that ensures that the bridge configuration and chain primitives crates are sharing
/// the same types (hash, account id, ...).
#[macro_export]
macro_rules! assert_bridge_types(
( bridge: $bridge:path, this_chain: $this:path, bridged_chain: $bridged:path ) => {
{
// if one of this asserts fail, then all chains, bridged with this chain and bridge relays are now broken
//
// `frame_support::weights::Weight` is used here directly, because all chains we know are using this
// primitive (may be changed in the future)
use $crate::messages::{
AccountIdOf, BalanceOf, BridgedChain, HashOf, SignatureOf, SignerOf, ThisChain,
};
use static_assertions::assert_type_eq_all;
assert_type_eq_all!(HashOf<ThisChain<$bridge>>, bp_runtime::HashOf<$this>);
assert_type_eq_all!(AccountIdOf<ThisChain<$bridge>>, bp_runtime::AccountIdOf<$this>);
assert_type_eq_all!(SignerOf<ThisChain<$bridge>>, bp_runtime::AccountPublicOf<$this>);
assert_type_eq_all!(SignatureOf<ThisChain<$bridge>>, bp_runtime::SignatureOf<$this>);
assert_type_eq_all!(BalanceOf<ThisChain<$bridge>>, bp_runtime::BalanceOf<$this>);
assert_type_eq_all!(HashOf<BridgedChain<$bridge>>, bp_runtime::HashOf<$bridged>);
assert_type_eq_all!(AccountIdOf<BridgedChain<$bridge>>, bp_runtime::AccountIdOf<$bridged>);
assert_type_eq_all!(SignerOf<BridgedChain<$bridge>>, bp_runtime::AccountPublicOf<$bridged>);
assert_type_eq_all!(SignatureOf<BridgedChain<$bridge>>, bp_runtime::SignatureOf<$bridged>);
assert_type_eq_all!(BalanceOf<BridgedChain<$bridge>>, bp_runtime::BalanceOf<$bridged>);
}
}
);
/// Macro that ensures that the bridge GRANDPA pallet is configured properly to bridge with given
/// chain.
#[macro_export]
@@ -145,7 +115,6 @@ macro_rules! assert_complete_bridge_types(
bridged_chain: $bridged:path,
) => {
$crate::assert_chain_types!(runtime: $r, this_chain: $this);
$crate::assert_bridge_types!(bridge: $bridge, this_chain: $this, bridged_chain: $bridged);
$crate::assert_bridge_grandpa_pallet_types!(
runtime: $r,
with_bridged_chain_grandpa_instance: $gi,
+2
View File
@@ -28,6 +28,8 @@ pub mod messages_benchmarking;
pub mod messages_extension;
pub mod parachains_benchmarking;
mod messages_generation;
#[cfg(feature = "integrity-test")]
pub mod integrity;
File diff suppressed because it is too large Load Diff
@@ -19,14 +19,18 @@
#![cfg(feature = "runtime-benchmarks")]
use crate::messages::{
source::{FromBridgedChainMessagesDeliveryProof, FromThisChainMessagePayload},
target::FromBridgedChainMessagesProof,
AccountIdOf, BalanceOf, BridgedChain, CallOf, HashOf, MessageBridge, RawStorageProof,
SignatureOf, SignerOf, ThisChain,
use crate::{
messages::{
source::{FromBridgedChainMessagesDeliveryProof, FromThisChainMessagePayload},
target::FromBridgedChainMessagesProof,
AccountIdOf, BalanceOf, BridgedChain, CallOf, HashOf, MessageBridge, ThisChain,
},
messages_generation::{
encode_all_messages, encode_lane_data, grow_trie, prepare_messages_storage_proof,
},
};
use bp_messages::{storage_keys, MessageData, MessageKey, MessagePayload};
use bp_messages::storage_keys;
use bp_runtime::{record_all_trie_keys, StorageProofSize};
use codec::Encode;
use frame_support::{dispatch::GetDispatchInfo, weights::Weight};
@@ -34,7 +38,7 @@ use pallet_bridge_messages::benchmarking::{
MessageDeliveryProofParams, MessageParams, MessageProofParams,
};
use sp_core::Hasher;
use sp_runtime::traits::{Header, IdentifyAccount, MaybeSerializeDeserialize, Zero};
use sp_runtime::traits::{Header, MaybeSerializeDeserialize, Zero};
use sp_std::{fmt::Debug, prelude::*};
use sp_trie::{trie_types::TrieDBMutBuilderV1, LayoutV1, MemoryDB, Recorder, TrieMut};
@@ -71,10 +75,6 @@ where
BalanceOf<ThisChain<B>>: Debug + MaybeSerializeDeserialize,
CallOf<ThisChain<B>>: From<frame_system::Call<R>> + GetDispatchInfo,
HashOf<BridgedChain<B>>: Copy + Default,
SignatureOf<ThisChain<B>>: From<sp_core::ed25519::Signature>,
SignerOf<ThisChain<B>>: Clone
+ From<sp_core::ed25519::Public>
+ IdentifyAccount<AccountId = AccountIdOf<ThisChain<B>>>,
{
let message_payload = match params.size {
StorageProofSize::Minimal(ref size) => vec![0u8; *size as _],
@@ -82,8 +82,15 @@ where
};
// finally - prepare storage proof and update environment
let (state_root, storage_proof) =
prepare_messages_storage_proof::<B, BHH>(&params, message_payload);
let (state_root, storage_proof) = prepare_messages_storage_proof::<B>(
params.lane,
params.message_nonces.clone(),
params.outbound_lane_data,
params.size,
message_payload,
encode_all_messages,
encode_lane_data,
);
let (_, bridged_header_hash) = insert_header_to_grandpa_pallet::<R, FI>(state_root);
(
@@ -141,69 +148,6 @@ where
}
}
/// Prepare storage proof of given messages.
///
/// Returns state trie root and nodes with prepared messages.
fn prepare_messages_storage_proof<B, BHH>(
params: &MessageProofParams,
message_payload: MessagePayload,
) -> (HashOf<BridgedChain<B>>, RawStorageProof)
where
B: MessageBridge,
BHH: Hasher<Out = HashOf<BridgedChain<B>>>,
HashOf<BridgedChain<B>>: Copy + Default,
{
// prepare Bridged chain storage with messages and (optionally) outbound lane state
let message_count =
params.message_nonces.end().saturating_sub(*params.message_nonces.start()) + 1;
let mut storage_keys = Vec::with_capacity(message_count as usize + 1);
let mut root = Default::default();
let mut mdb = MemoryDB::default();
{
let mut trie = TrieDBMutBuilderV1::<BHH>::new(&mut mdb, &mut root).build();
// insert messages
for nonce in params.message_nonces.clone() {
let message_key = MessageKey { lane_id: params.lane, nonce };
let message_data = MessageData {
fee: BalanceOf::<BridgedChain<B>>::from(0),
payload: message_payload.clone(),
};
let storage_key = storage_keys::message_key(
B::BRIDGED_MESSAGES_PALLET_NAME,
&message_key.lane_id,
message_key.nonce,
)
.0;
trie.insert(&storage_key, &message_data.encode())
.map_err(|_| "TrieMut::insert has failed")
.expect("TrieMut::insert should not fail in benchmarks");
storage_keys.push(storage_key);
}
// insert outbound lane state
if let Some(ref outbound_lane_data) = params.outbound_lane_data {
let storage_key =
storage_keys::outbound_lane_data_key(B::BRIDGED_MESSAGES_PALLET_NAME, &params.lane)
.0;
trie.insert(&storage_key, &outbound_lane_data.encode())
.map_err(|_| "TrieMut::insert has failed")
.expect("TrieMut::insert should not fail in benchmarks");
storage_keys.push(storage_key);
}
}
root = grow_trie(root, &mut mdb, params.size);
// generate storage proof to be delivered to This chain
let mut proof_recorder = Recorder::<LayoutV1<BHH>>::new();
record_all_trie_keys::<LayoutV1<BHH>, _>(&mdb, &root, &mut proof_recorder)
.map_err(|_| "record_all_trie_keys has failed")
.expect("record_all_trie_keys should not fail in benchmarks");
let storage_proof = proof_recorder.drain().into_iter().map(|n| n.data.to_vec()).collect();
(root, storage_proof)
}
/// Insert header to the bridge GRANDPA pallet.
pub(crate) fn insert_header_to_grandpa_pallet<R, GI>(
state_root: bp_runtime::HashOf<R::BridgedChain>,
@@ -225,38 +169,3 @@ where
pallet_bridge_grandpa::initialize_for_benchmarks::<R, GI>(bridged_header);
(bridged_block_number, bridged_header_hash)
}
/// Populate trie with dummy keys+values until trie has at least given size.
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 {
StorageProofSize::Minimal(_) => return root,
StorageProofSize::HasLargeLeaf(size) => (1, size, size),
StorageProofSize::HasExtraNodes(size) => (8, 1, size),
};
let mut key_index = 0;
loop {
// generate storage proof to be delivered to This chain
let mut proof_recorder = Recorder::<LayoutV1<H>>::new();
record_all_trie_keys::<LayoutV1<H>, _>(mdb, &root, &mut proof_recorder)
.map_err(|_| "record_all_trie_keys has failed")
.expect("record_all_trie_keys should not fail in benchmarks");
let size: usize = proof_recorder.drain().into_iter().map(|n| n.data.len()).sum();
if size > minimal_trie_size as _ {
return root
}
let mut trie = TrieDBMutBuilderV1::<H>::from_existing(mdb, &mut root).build();
for _ in 0..iterations {
trie.insert(&key_index.encode(), &vec![42u8; leaf_size as _])
.map_err(|_| "TrieMut::insert has failed")
.expect("TrieMut::insert should not fail in benchmarks");
key_index += 1;
}
trie.commit();
}
}
@@ -0,0 +1,154 @@
// Copyright 2019-2022 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/>.
//! Helpers for generating message storage proofs, that are used by tests and by benchmarks.
#![cfg(any(feature = "runtime-benchmarks", test))]
use crate::messages::{BalanceOf, BridgedChain, HashOf, HasherOf, MessageBridge, RawStorageProof};
use bp_messages::{
storage_keys, LaneId, MessageData, MessageKey, MessageNonce, MessagePayload, OutboundLaneData,
};
use bp_runtime::{record_all_trie_keys, StorageProofSize};
use codec::Encode;
use sp_core::Hasher;
use sp_runtime::traits::Zero;
use sp_std::{ops::RangeInclusive, prelude::*};
use sp_trie::{trie_types::TrieDBMutBuilderV1, LayoutV1, MemoryDB, Recorder, TrieMut};
/// Simple and correct message data encode function.
pub(crate) fn encode_all_messages<B: Encode>(
_: MessageNonce,
m: &MessageData<B>,
) -> Option<Vec<u8>> {
Some(m.encode())
}
/// Simple and correct outbound lane data encode function.
pub(crate) fn encode_lane_data(d: &OutboundLaneData) -> Vec<u8> {
d.encode()
}
/// Prepare storage proof of given messages.
///
/// Returns state trie root and nodes with prepared messages.
pub(crate) fn prepare_messages_storage_proof<B>(
lane: LaneId,
message_nonces: RangeInclusive<MessageNonce>,
outbound_lane_data: Option<OutboundLaneData>,
size: StorageProofSize,
message_payload: MessagePayload,
encode_message: impl Fn(MessageNonce, &MessageData<BalanceOf<BridgedChain<B>>>) -> Option<Vec<u8>>,
encode_outbound_lane_data: impl Fn(&OutboundLaneData) -> Vec<u8>,
) -> (HashOf<BridgedChain<B>>, RawStorageProof)
where
B: MessageBridge,
HashOf<BridgedChain<B>>: Copy + Default,
{
// prepare Bridged chain storage with messages and (optionally) outbound lane state
let message_count = message_nonces.end().saturating_sub(*message_nonces.start()) + 1;
let mut storage_keys = Vec::with_capacity(message_count as usize + 1);
let mut root = Default::default();
let mut mdb = MemoryDB::default();
{
let mut trie =
TrieDBMutBuilderV1::<HasherOf<BridgedChain<B>>>::new(&mut mdb, &mut root).build();
// insert messages
for nonce in message_nonces {
let message_key = MessageKey { lane_id: lane, nonce };
let message_data = MessageData {
fee: BalanceOf::<BridgedChain<B>>::zero(),
payload: message_payload.clone(),
};
let message_data = match encode_message(nonce, &message_data) {
Some(message_data) => message_data,
None => continue,
};
let storage_key = storage_keys::message_key(
B::BRIDGED_MESSAGES_PALLET_NAME,
&message_key.lane_id,
message_key.nonce,
)
.0;
trie.insert(&storage_key, &message_data)
.map_err(|_| "TrieMut::insert has failed")
.expect("TrieMut::insert should not fail in benchmarks");
storage_keys.push(storage_key);
}
// insert outbound lane state
if let Some(outbound_lane_data) = outbound_lane_data.as_ref().map(encode_outbound_lane_data)
{
let storage_key =
storage_keys::outbound_lane_data_key(B::BRIDGED_MESSAGES_PALLET_NAME, &lane).0;
trie.insert(&storage_key, &outbound_lane_data)
.map_err(|_| "TrieMut::insert has failed")
.expect("TrieMut::insert should not fail in benchmarks");
storage_keys.push(storage_key);
}
}
root = grow_trie(root, &mut mdb, size);
// generate storage proof to be delivered to This chain
let mut proof_recorder = Recorder::<LayoutV1<HasherOf<BridgedChain<B>>>>::new();
record_all_trie_keys::<LayoutV1<HasherOf<BridgedChain<B>>>, _>(
&mdb,
&root,
&mut proof_recorder,
)
.map_err(|_| "record_all_trie_keys has failed")
.expect("record_all_trie_keys should not fail in benchmarks");
let storage_proof = proof_recorder.drain().into_iter().map(|n| n.data.to_vec()).collect();
(root, storage_proof)
}
/// Populate trie with dummy keys+values until trie has at least given size.
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 {
StorageProofSize::Minimal(_) => return root,
StorageProofSize::HasLargeLeaf(size) => (1, size, size),
StorageProofSize::HasExtraNodes(size) => (8, 1, size),
};
let mut key_index = 0;
loop {
// generate storage proof to be delivered to This chain
let mut proof_recorder = Recorder::<LayoutV1<H>>::new();
record_all_trie_keys::<LayoutV1<H>, _>(mdb, &root, &mut proof_recorder)
.map_err(|_| "record_all_trie_keys has failed")
.expect("record_all_trie_keys should not fail in benchmarks");
let size: usize = proof_recorder.drain().into_iter().map(|n| n.data.len()).sum();
if size > minimal_trie_size as _ {
return root
}
let mut trie = TrieDBMutBuilderV1::<H>::from_existing(mdb, &mut root).build();
for _ in 0..iterations {
trie.insert(&key_index.encode(), &vec![42u8; leaf_size as _])
.map_err(|_| "TrieMut::insert has failed")
.expect("TrieMut::insert should not fail in benchmarks");
key_index += 1;
}
trie.commit();
}
}
@@ -18,7 +18,9 @@
#![cfg(feature = "runtime-benchmarks")]
use crate::messages_benchmarking::{grow_trie, insert_header_to_grandpa_pallet};
use crate::{
messages_benchmarking::insert_header_to_grandpa_pallet, messages_generation::grow_trie,
};
use bp_parachains::parachain_head_storage_key_at_source;
use bp_polkadot_core::parachains::{ParaHash, ParaHead, ParaHeadsProof, ParaId};