mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-18 02:31:03 +00:00
Squashed 'bridges/' content from commit 89a76998f
git-subtree-dir: bridges git-subtree-split: 89a76998f93c8219e9b1f785dcce73d4891e7068
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
[package]
|
||||
name = "bridge-runtime-common"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
homepage = "https://substrate.dev"
|
||||
repository = "https://github.com/paritytech/parity-bridges-common/"
|
||||
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
||||
|
||||
[dependencies]
|
||||
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
|
||||
ed25519-dalek = { version = "1.0", default-features = false, optional = true }
|
||||
hash-db = { version = "0.15.2", default-features = false }
|
||||
|
||||
# Bridge dependencies
|
||||
|
||||
bp-message-dispatch = { path = "../../primitives/message-dispatch", default-features = false }
|
||||
bp-messages = { path = "../../primitives/messages", default-features = false }
|
||||
bp-runtime = { path = "../../primitives/runtime", default-features = false }
|
||||
pallet-bridge-dispatch = { path = "../../modules/dispatch", default-features = false }
|
||||
pallet-bridge-grandpa = { path = "../../modules/grandpa", default-features = false }
|
||||
pallet-bridge-messages = { path = "../../modules/messages", default-features = false }
|
||||
|
||||
# Substrate dependencies
|
||||
|
||||
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false, optional = true }
|
||||
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"bp-message-dispatch/std",
|
||||
"bp-messages/std",
|
||||
"bp-runtime/std",
|
||||
"codec/std",
|
||||
"frame-support/std",
|
||||
"hash-db/std",
|
||||
"pallet-bridge-dispatch/std",
|
||||
"pallet-bridge-grandpa/std",
|
||||
"pallet-bridge-messages/std",
|
||||
"sp-core/std",
|
||||
"sp-runtime/std",
|
||||
"sp-state-machine/std",
|
||||
"sp-std/std",
|
||||
"sp-trie/std",
|
||||
]
|
||||
runtime-benchmarks = [
|
||||
"ed25519-dalek/u64_backend",
|
||||
"pallet-bridge-grandpa/runtime-benchmarks",
|
||||
"pallet-bridge-messages/runtime-benchmarks",
|
||||
"sp-state-machine",
|
||||
]
|
||||
@@ -0,0 +1,176 @@
|
||||
# Helpers for Messages Module Integration
|
||||
|
||||
The [`messages`](./src/messages.rs) module of this crate contains a bunch of helpers for integrating
|
||||
messages module into your runtime. Basic prerequisites of these helpers are:
|
||||
- we're going to bridge Substrate-based chain with another Substrate-based chain;
|
||||
- both chains have [messages module](../../modules/messages/README.md), Substrate bridge
|
||||
module and the [call dispatch module](../../modules/dispatch/README.md);
|
||||
- all message lanes are identical and may be used to transfer the same messages;
|
||||
- the messages sent over the bridge are dispatched using
|
||||
[call dispatch module](../../modules/dispatch/README.md);
|
||||
- the messages are `pallet_bridge_dispatch::MessagePayload` structures, where `call` field is
|
||||
encoded `Call` of the target chain. This means that the `Call` is opaque to the
|
||||
[messages module](../../modules/messages/README.md) instance at the source chain.
|
||||
It is pre-encoded by the message submitter;
|
||||
- all proofs in the [messages module](../../modules/messages/README.md) transactions are
|
||||
based on the storage proofs from the bridged chain: storage proof of the outbound message (value
|
||||
from the `pallet_bridge_messages::Store::MessagePayload` map), storage proof of the outbound lane
|
||||
state (value from the `pallet_bridge_messages::Store::OutboundLanes` map) and storage proof of the
|
||||
inbound lane state (value from the `pallet_bridge_messages::Store::InboundLanes` map);
|
||||
- storage proofs are built at the finalized headers of the corresponding chain. So all message lane
|
||||
transactions with proofs are verifying storage proofs against finalized chain headers from
|
||||
Substrate bridge module.
|
||||
|
||||
**IMPORTANT NOTE**: after reading this document, you may refer to our test runtimes
|
||||
([rialto_messages.rs](../millau/runtime/src/rialto_messages.rs) and/or
|
||||
[millau_messages.rs](../rialto/runtime/src/millau_messages.rs)) to see how to use these helpers.
|
||||
|
||||
## Contents
|
||||
- [`MessageBridge` Trait](#messagebridge-trait)
|
||||
- [`ChainWithMessages` Trait ](#ChainWithMessages-trait)
|
||||
- [Helpers for the Source Chain](#helpers-for-the-source-chain)
|
||||
- [Helpers for the Target Chain](#helpers-for-the-target-chain)
|
||||
|
||||
## `MessageBridge` Trait
|
||||
|
||||
The essence of your integration will be a struct that implements a `MessageBridge` trait. It has
|
||||
single method (`MessageBridge::bridged_balance_to_this_balance`), used to convert from bridged chain
|
||||
tokens into this chain tokens. The bridge also requires two associated types to be specified -
|
||||
`ThisChain` and `BridgedChain`.
|
||||
|
||||
Worth to say that if you're going to use hardcoded constant (conversion rate) in the
|
||||
`MessageBridge::bridged_balance_to_this_balance` method (or in any other method of
|
||||
`ThisChainWithMessages` or `BridgedChainWithMessages` traits), then you should take a
|
||||
look at the
|
||||
[messages parameters functionality](../../modules/messages/README.md#Non-Essential-Functionality).
|
||||
They allow pallet owner to update constants more frequently than runtime upgrade happens.
|
||||
|
||||
## `ChainWithMessages` Trait
|
||||
|
||||
The trait is quite simple and can easily be implemented - you just need to specify types used at the
|
||||
corresponding chain. There is single exception, though (it may be changed in the future):
|
||||
|
||||
- `ChainWithMessages::MessagesInstance`: this is used to compute runtime storage keys. There
|
||||
may be several instances of messages pallet, included in the Runtime. Every instance stores
|
||||
messages and these messages stored under different keys. When we are verifying storage proofs from
|
||||
the bridged chain, we should know which instance we're talking to. This is fine, but there's
|
||||
significant inconvenience with that - this chain runtime must have the same messages pallet
|
||||
instance. This does not necessarily mean that we should use the same instance on both chains -
|
||||
this instance may be used to bridge with another chain/instance, or may not be used at all.
|
||||
|
||||
## `ThisChainWithMessages` Trait
|
||||
|
||||
This trait represents this chain from bridge point of view. Let's review every method of this trait:
|
||||
|
||||
- `ThisChainWithMessages::is_outbound_lane_enabled`: is used to check whether given lane accepts
|
||||
outbound messages.
|
||||
|
||||
- `ThisChainWithMessages::maximal_pending_messages_at_outbound_lane`: you should return maximal
|
||||
number of pending (undelivered) messages from this function. Returning small values would require
|
||||
relayers to operate faster and could make message sending logic more complicated. On the other
|
||||
hand, returning large values could lead to chain state growth.
|
||||
|
||||
- `ThisChainWithMessages::estimate_delivery_confirmation_transaction`: you'll need to return
|
||||
estimated size and dispatch weight of the delivery confirmation transaction (that happens on
|
||||
this chain) from this function.
|
||||
|
||||
- `ThisChainWithMessages::transaction_payment`: you'll need to return fee that the submitter
|
||||
must pay for given transaction on this chain. Normally, you would use transaction payment pallet
|
||||
for this. However, if your chain has non-zero fee multiplier set, this would mean that the
|
||||
payment will be computed using current value of this multiplier. But since this transaction
|
||||
will be submitted in the future, you may want to choose other value instead. Otherwise,
|
||||
non-altruistic relayer may choose not to submit this transaction until number of transactions
|
||||
will decrease.
|
||||
|
||||
## `BridgedChainWithMessages` Trait
|
||||
|
||||
This trait represents this chain from bridge point of view. Let's review every method of this trait:
|
||||
|
||||
- `BridgedChainWithMessages::maximal_extrinsic_size`: you will need to return the maximal
|
||||
extrinsic size of the target chain from this function.
|
||||
|
||||
- `MessageBridge::message_weight_limits`: you'll need to return a range of
|
||||
dispatch weights that the outbound message may take at the target chain. Please keep in mind that
|
||||
our helpers assume that the message is an encoded call of the target chain. But we never decode
|
||||
this call at the source chain. So you can't simply get dispatch weight from pre-dispatch
|
||||
information. Instead there are two options to prepare this range: if you know which calls are to
|
||||
be sent over your bridge, then you may just return weight ranges for these particular calls.
|
||||
Otherwise, if you're going to accept all kinds of calls, you may just return range `[0; maximal
|
||||
incoming message dispatch weight]`. If you choose the latter, then you shall remember that the
|
||||
delivery transaction itself has some weight, so you can't accept messages with weight equal to
|
||||
maximal weight of extrinsic at the target chain. In our test chains, we reject all messages that
|
||||
have declared dispatch weight larger than 50% of the maximal bridged extrinsic weight.
|
||||
|
||||
- `MessageBridge::estimate_delivery_transaction`: you will need to return estimated dispatch weight and
|
||||
size of the delivery transaction that delivers a given message to the target chain.
|
||||
|
||||
- `MessageBridge::transaction_payment`: you'll need to return fee that the submitter
|
||||
must pay for given transaction on bridged chain. The best case is when you have the same conversion
|
||||
formula on both chains - then you may just reuse the `ThisChainWithMessages::transaction_payment`
|
||||
implementation. Otherwise, you'll need to hardcode this formula into your runtime.
|
||||
|
||||
## Helpers for the Source Chain
|
||||
|
||||
The helpers for the Source Chain reside in the `source` submodule of the
|
||||
[`messages`](./src/messages.rs) module. The structs are: `FromThisChainMessagePayload`,
|
||||
`FromBridgedChainMessagesDeliveryProof`, `FromThisChainMessageVerifier`. And the helper functions
|
||||
are: `maximal_message_size`, `verify_chain_message`, `verify_messages_delivery_proof` and
|
||||
`estimate_message_dispatch_and_delivery_fee`.
|
||||
|
||||
`FromThisChainMessagePayload` is a message that the sender sends through our bridge. It is the
|
||||
`pallet_bridge_dispatch::MessagePayload`, where `call` field is encoded target chain call. So
|
||||
at this chain we don't see internals of this call - we just know its size.
|
||||
|
||||
`FromThisChainMessageVerifier` is an implementation of `bp_messages::LaneMessageVerifier`. It
|
||||
has following checks in its `verify_message` method:
|
||||
|
||||
1. it'll verify that the used outbound lane is enabled in our runtime;
|
||||
|
||||
1. it'll reject messages if there are too many undelivered outbound messages at this lane. The
|
||||
sender need to wait while relayers will do their work before sending the message again;
|
||||
|
||||
1. it'll reject a message if it has the wrong dispatch origin declared. Like if the submitter is not
|
||||
the root of this chain, but it tries to dispatch the message at the target chain using
|
||||
`pallet_bridge_dispatch::CallOrigin::SourceRoot` origin. Or he has provided wrong signature
|
||||
in the `pallet_bridge_dispatch::CallOrigin::TargetAccount` origin;
|
||||
|
||||
1. it'll reject a message if the delivery and dispatch fee that the submitter wants to pay is lesser
|
||||
than the fee that is computed using the `estimate_message_dispatch_and_delivery_fee` function.
|
||||
|
||||
`estimate_message_dispatch_and_delivery_fee` returns a minimal fee that the submitter needs to pay
|
||||
for sending a given message. The fee includes: payment for the delivery transaction at the target
|
||||
chain, payment for delivery confirmation transaction on this chain, payment for `Call` dispatch at
|
||||
the target chain and relayer interest.
|
||||
|
||||
`FromBridgedChainMessagesDeliveryProof` holds the lane identifier and the storage proof of this
|
||||
inbound lane state at the bridged chain. This also holds the hash of the target chain header, that
|
||||
was used to generate this storage proof. The proof is verified by the
|
||||
`verify_messages_delivery_proof`, which simply checks that the target chain header is finalized
|
||||
(using Substrate bridge module) and then reads the inbound lane state from the proof.
|
||||
|
||||
`verify_chain_message` function checks that the message may be delivered to the bridged chain. There
|
||||
are two main checks:
|
||||
|
||||
1. that the message size is less than or equal to the `2/3` of maximal extrinsic size at the target
|
||||
chain. We leave `1/3` for signed extras and for the storage proof overhead;
|
||||
|
||||
1. that the message dispatch weight is less than or equal to the `1/2` of maximal normal extrinsic
|
||||
weight at the target chain. We leave `1/2` for the delivery transaction overhead.
|
||||
|
||||
## Helpers for the Target Chain
|
||||
|
||||
The helpers for the target chain reside in the `target` submodule of the
|
||||
[`messages`](./src/messages.rs) module. The structs are: `FromBridgedChainMessagePayload`,
|
||||
`FromBridgedChainMessagesProof`, `FromBridgedChainMessagesProof`. And the helper functions are:
|
||||
`maximal_incoming_message_dispatch_weight`, `maximal_incoming_message_size` and
|
||||
`verify_messages_proof`.
|
||||
|
||||
`FromBridgedChainMessagePayload` corresponds to the `FromThisChainMessagePayload` at the bridged
|
||||
chain. We expect that messages with this payload are stored in the `OutboundMessages` storage map of
|
||||
the [messages module](../../modules/messages/README.md). This map is used to build
|
||||
`FromBridgedChainMessagesProof`. The proof holds the lane id, range of message nonces included in
|
||||
the proof, storage proof of `OutboundMessages` entries and the hash of bridged chain header that has
|
||||
been used to build the proof. Additionally, there's storage proof may contain the proof of outbound
|
||||
lane state. It may be required to prune `relayers` entries at this chain (see
|
||||
[messages module documentation](../../modules/messages/README.md#What-about-other-Constants-in-the-Messages-Module-Configuration-Trait)
|
||||
for details). This proof is verified by the `verify_messages_proof` function.
|
||||
@@ -0,0 +1,22 @@
|
||||
// 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/>.
|
||||
|
||||
//! Common types/functions that may be used by runtimes of all bridged chains.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
pub mod messages;
|
||||
pub mod messages_benchmarking;
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,226 @@
|
||||
// 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 messages module, based on
|
||||
//! `bridge_runtime_common::messages` implementation.
|
||||
|
||||
#![cfg(feature = "runtime-benchmarks")]
|
||||
|
||||
use crate::messages::{
|
||||
source::FromBridgedChainMessagesDeliveryProof, target::FromBridgedChainMessagesProof, AccountIdOf, BalanceOf,
|
||||
BridgedChain, HashOf, MessageBridge, ThisChain,
|
||||
};
|
||||
|
||||
use bp_messages::{LaneId, MessageData, MessageKey, MessagePayload};
|
||||
use codec::Encode;
|
||||
use ed25519_dalek::{PublicKey, SecretKey, Signer, KEYPAIR_LENGTH, SECRET_KEY_LENGTH};
|
||||
use frame_support::weights::Weight;
|
||||
use pallet_bridge_messages::benchmarking::{MessageDeliveryProofParams, MessageProofParams, ProofSize};
|
||||
use sp_core::Hasher;
|
||||
use sp_runtime::traits::Header;
|
||||
use sp_std::prelude::*;
|
||||
use sp_trie::{record_all_keys, trie_types::TrieDBMut, Layout, MemoryDB, Recorder, TrieMut};
|
||||
|
||||
/// Generate ed25519 signature to be used in `pallet_brdige_call_dispatch::CallOrigin::TargetAccount`.
|
||||
///
|
||||
/// Returns public key of the signer and the signature itself.
|
||||
pub fn ed25519_sign(target_call: &impl Encode, source_account_id: &impl Encode) -> ([u8; 32], [u8; 64]) {
|
||||
// key from the repo example (https://docs.rs/ed25519-dalek/1.0.1/ed25519_dalek/struct.SecretKey.html)
|
||||
let target_secret = SecretKey::from_bytes(&[
|
||||
157, 097, 177, 157, 239, 253, 090, 096, 186, 132, 074, 244, 146, 236, 044, 196, 068, 073, 197, 105, 123, 050,
|
||||
105, 025, 112, 059, 172, 003, 028, 174, 127, 096,
|
||||
])
|
||||
.expect("harcoded key is valid");
|
||||
let target_public: PublicKey = (&target_secret).into();
|
||||
|
||||
let mut target_pair_bytes = [0u8; KEYPAIR_LENGTH];
|
||||
target_pair_bytes[..SECRET_KEY_LENGTH].copy_from_slice(&target_secret.to_bytes());
|
||||
target_pair_bytes[SECRET_KEY_LENGTH..].copy_from_slice(&target_public.to_bytes());
|
||||
let target_pair = ed25519_dalek::Keypair::from_bytes(&target_pair_bytes).expect("hardcoded pair is valid");
|
||||
|
||||
let mut signature_message = Vec::new();
|
||||
target_call.encode_to(&mut signature_message);
|
||||
source_account_id.encode_to(&mut signature_message);
|
||||
let target_origin_signature = target_pair
|
||||
.try_sign(&signature_message)
|
||||
.expect("Ed25519 try_sign should not fail in benchmarks");
|
||||
|
||||
(target_public.to_bytes(), target_origin_signature.to_bytes())
|
||||
}
|
||||
|
||||
/// Prepare proof of messages for the `receive_messages_proof` call.
|
||||
pub fn prepare_message_proof<B, H, R, FI, MM, ML, MH>(
|
||||
params: MessageProofParams,
|
||||
make_bridged_message_storage_key: MM,
|
||||
make_bridged_outbound_lane_data_key: ML,
|
||||
make_bridged_header: MH,
|
||||
message_dispatch_weight: Weight,
|
||||
message_payload: MessagePayload,
|
||||
) -> (FromBridgedChainMessagesProof<HashOf<BridgedChain<B>>>, Weight)
|
||||
where
|
||||
B: MessageBridge,
|
||||
H: Hasher,
|
||||
R: pallet_bridge_grandpa::Config<FI>,
|
||||
FI: 'static,
|
||||
<R::BridgedChain as bp_runtime::Chain>::Hash: Into<HashOf<BridgedChain<B>>>,
|
||||
MM: Fn(MessageKey) -> Vec<u8>,
|
||||
ML: Fn(LaneId) -> Vec<u8>,
|
||||
MH: Fn(H::Out) -> <R::BridgedChain as bp_runtime::Chain>::Header,
|
||||
{
|
||||
// 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 = TrieDBMut::<H>::new(&mut mdb, &mut root);
|
||||
|
||||
// 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 = make_bridged_message_storage_key(message_key);
|
||||
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(outbound_lane_data) = params.outbound_lane_data {
|
||||
let storage_key = make_bridged_outbound_lane_data_key(params.lane);
|
||||
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::<H::Out>::new();
|
||||
record_all_keys::<Layout<H>, _>(&mdb, &root, &mut proof_recorder)
|
||||
.map_err(|_| "record_all_keys has failed")
|
||||
.expect("record_all_keys should not fail in benchmarks");
|
||||
let storage_proof = proof_recorder.drain().into_iter().map(|n| n.data.to_vec()).collect();
|
||||
|
||||
// prepare Bridged chain header and insert it into the Substrate pallet
|
||||
let bridged_header = make_bridged_header(root);
|
||||
let bridged_header_hash = bridged_header.hash();
|
||||
pallet_bridge_grandpa::initialize_for_benchmarks::<R, FI>(bridged_header);
|
||||
|
||||
(
|
||||
FromBridgedChainMessagesProof {
|
||||
bridged_header_hash: bridged_header_hash.into(),
|
||||
storage_proof,
|
||||
lane: params.lane,
|
||||
nonces_start: *params.message_nonces.start(),
|
||||
nonces_end: *params.message_nonces.end(),
|
||||
},
|
||||
message_dispatch_weight
|
||||
.checked_mul(message_count)
|
||||
.expect("too many messages requested by benchmark"),
|
||||
)
|
||||
}
|
||||
|
||||
/// Prepare proof of messages delivery for the `receive_messages_delivery_proof` call.
|
||||
pub fn prepare_message_delivery_proof<B, H, R, FI, ML, MH>(
|
||||
params: MessageDeliveryProofParams<AccountIdOf<ThisChain<B>>>,
|
||||
make_bridged_inbound_lane_data_key: ML,
|
||||
make_bridged_header: MH,
|
||||
) -> FromBridgedChainMessagesDeliveryProof<HashOf<BridgedChain<B>>>
|
||||
where
|
||||
B: MessageBridge,
|
||||
H: Hasher,
|
||||
R: pallet_bridge_grandpa::Config<FI>,
|
||||
FI: 'static,
|
||||
<R::BridgedChain as bp_runtime::Chain>::Hash: Into<HashOf<BridgedChain<B>>>,
|
||||
ML: Fn(LaneId) -> Vec<u8>,
|
||||
MH: Fn(H::Out) -> <R::BridgedChain as bp_runtime::Chain>::Header,
|
||||
{
|
||||
// prepare Bridged chain storage with inbound lane state
|
||||
let storage_key = make_bridged_inbound_lane_data_key(params.lane);
|
||||
let mut root = Default::default();
|
||||
let mut mdb = MemoryDB::default();
|
||||
{
|
||||
let mut trie = TrieDBMut::<H>::new(&mut mdb, &mut root);
|
||||
trie.insert(&storage_key, ¶ms.inbound_lane_data.encode())
|
||||
.map_err(|_| "TrieMut::insert has failed")
|
||||
.expect("TrieMut::insert should not fail in benchmarks");
|
||||
}
|
||||
root = grow_trie(root, &mut mdb, params.size);
|
||||
|
||||
// generate storage proof to be delivered to This chain
|
||||
let mut proof_recorder = Recorder::<H::Out>::new();
|
||||
record_all_keys::<Layout<H>, _>(&mdb, &root, &mut proof_recorder)
|
||||
.map_err(|_| "record_all_keys has failed")
|
||||
.expect("record_all_keys should not fail in benchmarks");
|
||||
let storage_proof = proof_recorder.drain().into_iter().map(|n| n.data.to_vec()).collect();
|
||||
|
||||
// prepare Bridged chain header and insert it into the Substrate pallet
|
||||
let bridged_header = make_bridged_header(root);
|
||||
let bridged_header_hash = bridged_header.hash();
|
||||
pallet_bridge_grandpa::initialize_for_benchmarks::<R, FI>(bridged_header);
|
||||
|
||||
FromBridgedChainMessagesDeliveryProof {
|
||||
bridged_header_hash: bridged_header_hash.into(),
|
||||
storage_proof,
|
||||
lane: params.lane,
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
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),
|
||||
};
|
||||
|
||||
let mut key_index = 0;
|
||||
loop {
|
||||
// generate storage proof to be delivered to This chain
|
||||
let mut proof_recorder = Recorder::<H::Out>::new();
|
||||
record_all_keys::<Layout<H>, _>(mdb, &root, &mut proof_recorder)
|
||||
.map_err(|_| "record_all_keys has failed")
|
||||
.expect("record_all_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 = TrieDBMut::<H>::from_existing(mdb, &mut root)
|
||||
.map_err(|_| "TrieDBMut::from_existing has failed")
|
||||
.expect("TrieDBMut::from_existing should not fail in benchmarks");
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user