mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-01 11:17:56 +00:00
move "common" code of messages pallet benchmarks helpers to the common library (#1281)
This commit is contained in:
committed by
Bastian Köcher
parent
c4837d2368
commit
fd816b60e3
@@ -828,7 +828,7 @@ impl_runtime_apis! {
|
||||
let mut batches = Vec::<BenchmarkBatch>::new();
|
||||
let params = (&config, &whitelist);
|
||||
|
||||
use bp_runtime::messages::DispatchFeePayment;
|
||||
use bridge_runtime_common::messages_benchmarking::{prepare_message_delivery_proof, prepare_message_proof, prepare_outbound_message};
|
||||
use bridge_runtime_common::messages;
|
||||
use pallet_bridge_messages::benchmarking::{
|
||||
Pallet as MessagesBench,
|
||||
@@ -836,9 +836,8 @@ impl_runtime_apis! {
|
||||
MessageDeliveryProofParams,
|
||||
MessageParams,
|
||||
MessageProofParams,
|
||||
ProofSize as MessagesProofSize,
|
||||
};
|
||||
use rialto_messages::{ToRialtoMessagePayload, WithRialtoMessageBridge};
|
||||
use rialto_messages::WithRialtoMessageBridge;
|
||||
|
||||
impl MessagesConfig<WithRialtoMessagesInstance> for Runtime {
|
||||
fn maximal_message_size() -> u32 {
|
||||
@@ -863,120 +862,24 @@ impl_runtime_apis! {
|
||||
fn prepare_outbound_message(
|
||||
params: MessageParams<Self::AccountId>,
|
||||
) -> (rialto_messages::ToRialtoMessagePayload, Balance) {
|
||||
let message_payload = vec![0; params.size as usize];
|
||||
let dispatch_origin = bp_message_dispatch::CallOrigin::SourceAccount(
|
||||
params.sender_account,
|
||||
);
|
||||
|
||||
let message = ToRialtoMessagePayload {
|
||||
spec_version: 0,
|
||||
weight: params.size as _,
|
||||
origin: dispatch_origin,
|
||||
call: message_payload,
|
||||
dispatch_fee_payment: DispatchFeePayment::AtSourceChain,
|
||||
};
|
||||
(message, pallet_bridge_messages::benchmarking::MESSAGE_FEE.into())
|
||||
prepare_outbound_message::<WithRialtoMessageBridge>(params)
|
||||
}
|
||||
|
||||
fn prepare_message_proof(
|
||||
params: MessageProofParams,
|
||||
) -> (rialto_messages::FromRialtoMessagesProof, Weight) {
|
||||
use bp_messages::{MessageKey, storage_keys};
|
||||
use bridge_runtime_common::{
|
||||
messages::MessageBridge,
|
||||
messages_benchmarking::{ed25519_sign, prepare_message_proof},
|
||||
};
|
||||
use codec::Encode;
|
||||
use frame_support::weights::GetDispatchInfo;
|
||||
use rialto_messages::WithRialtoMessageBridge;
|
||||
use sp_runtime::traits::{Header, IdentifyAccount};
|
||||
|
||||
let remark = match params.size {
|
||||
MessagesProofSize::Minimal(ref size) => vec![0u8; *size as _],
|
||||
_ => vec![],
|
||||
};
|
||||
let call = Call::System(SystemCall::remark { remark });
|
||||
let call_weight = call.get_dispatch_info().weight;
|
||||
|
||||
let rialto_account_id: bp_rialto::AccountId = Default::default();
|
||||
let (millau_raw_public, millau_raw_signature) = ed25519_sign(
|
||||
&call,
|
||||
&rialto_account_id,
|
||||
VERSION.spec_version,
|
||||
bp_runtime::RIALTO_CHAIN_ID,
|
||||
bp_runtime::MILLAU_CHAIN_ID,
|
||||
);
|
||||
let millau_public = MultiSigner::Ed25519(sp_core::ed25519::Public::from_raw(millau_raw_public));
|
||||
let millau_signature = MultiSignature::Ed25519(sp_core::ed25519::Signature::from_raw(
|
||||
millau_raw_signature,
|
||||
));
|
||||
|
||||
if params.dispatch_fee_payment == DispatchFeePayment::AtTargetChain {
|
||||
Self::endow_account(&millau_public.clone().into_account());
|
||||
}
|
||||
|
||||
let make_rialto_message_key = |message_key: MessageKey| storage_keys::message_key(
|
||||
<WithRialtoMessageBridge as MessageBridge>::BRIDGED_MESSAGES_PALLET_NAME,
|
||||
&message_key.lane_id, message_key.nonce,
|
||||
).0;
|
||||
let make_rialto_outbound_lane_data_key = |lane_id| storage_keys::outbound_lane_data_key(
|
||||
<WithRialtoMessageBridge as MessageBridge>::BRIDGED_MESSAGES_PALLET_NAME,
|
||||
&lane_id,
|
||||
).0;
|
||||
|
||||
let make_rialto_header = |state_root| bp_rialto::Header::new(
|
||||
0,
|
||||
Default::default(),
|
||||
state_root,
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
);
|
||||
|
||||
let dispatch_fee_payment = params.dispatch_fee_payment.clone();
|
||||
prepare_message_proof::<WithRialtoMessageBridge, bp_rialto::Hasher, Runtime, (), _, _, _>(
|
||||
prepare_message_proof::<Runtime, (), (), WithRialtoMessageBridge, bp_rialto::Header, bp_rialto::Hasher>(
|
||||
params,
|
||||
make_rialto_message_key,
|
||||
make_rialto_outbound_lane_data_key,
|
||||
make_rialto_header,
|
||||
call_weight,
|
||||
bp_message_dispatch::MessagePayload {
|
||||
spec_version: VERSION.spec_version,
|
||||
weight: call_weight,
|
||||
origin: bp_message_dispatch::CallOrigin::<
|
||||
bp_rialto::AccountId,
|
||||
MultiSigner,
|
||||
Signature,
|
||||
>::TargetAccount(
|
||||
rialto_account_id,
|
||||
millau_public,
|
||||
millau_signature,
|
||||
),
|
||||
dispatch_fee_payment,
|
||||
call: call.encode(),
|
||||
}.encode(),
|
||||
&VERSION,
|
||||
Balance::MAX / 100,
|
||||
)
|
||||
}
|
||||
|
||||
fn prepare_message_delivery_proof(
|
||||
params: MessageDeliveryProofParams<Self::AccountId>,
|
||||
) -> rialto_messages::ToRialtoMessagesDeliveryProof {
|
||||
use bridge_runtime_common::messages_benchmarking::prepare_message_delivery_proof;
|
||||
use rialto_messages::WithRialtoMessageBridge;
|
||||
use sp_runtime::traits::Header;
|
||||
|
||||
prepare_message_delivery_proof::<WithRialtoMessageBridge, bp_rialto::Hasher, Runtime, (), _, _>(
|
||||
prepare_message_delivery_proof::<Runtime, (), WithRialtoMessageBridge, bp_rialto::Header, bp_rialto::Hasher>(
|
||||
params,
|
||||
|lane_id| bp_messages::storage_keys::inbound_lane_data_key(
|
||||
<WithRialtoMessageBridge as MessageBridge>::BRIDGED_MESSAGES_PALLET_NAME,
|
||||
&lane_id,
|
||||
).0,
|
||||
|state_root| bp_rialto::Header::new(
|
||||
0,
|
||||
Default::default(),
|
||||
state_root,
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -25,12 +25,15 @@ pallet-bridge-messages = { path = "../../modules/messages", default-features = f
|
||||
# Substrate dependencies
|
||||
|
||||
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
|
||||
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true }
|
||||
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true }
|
||||
pallet-transaction-payment = { 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 }
|
||||
sp-version = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
@@ -54,7 +57,10 @@ std = [
|
||||
]
|
||||
runtime-benchmarks = [
|
||||
"ed25519-dalek/u64_backend",
|
||||
"frame-system",
|
||||
"pallet-balances",
|
||||
"pallet-bridge-grandpa/runtime-benchmarks",
|
||||
"pallet-bridge-messages/runtime-benchmarks",
|
||||
"sp-state-machine",
|
||||
"sp-version",
|
||||
]
|
||||
|
||||
@@ -176,7 +176,7 @@ pub type BalanceOf<C> = <C as ChainWithMessages>::Balance;
|
||||
pub type CallOf<C> = <C as ThisChainWithMessages>::Call;
|
||||
|
||||
/// Raw storage proof type (just raw trie nodes).
|
||||
type RawStorageProof = Vec<Vec<u8>>;
|
||||
pub type RawStorageProof = Vec<Vec<u8>>;
|
||||
|
||||
/// Compute fee of transaction at runtime where regular transaction payment pallet is being used.
|
||||
///
|
||||
|
||||
@@ -20,28 +20,279 @@
|
||||
#![cfg(feature = "runtime-benchmarks")]
|
||||
|
||||
use crate::messages::{
|
||||
source::FromBridgedChainMessagesDeliveryProof, target::FromBridgedChainMessagesProof,
|
||||
AccountIdOf, BalanceOf, BridgedChain, HashOf, MessageBridge, ThisChain,
|
||||
source::{FromBridgedChainMessagesDeliveryProof, FromThisChainMessagePayload},
|
||||
target::FromBridgedChainMessagesProof,
|
||||
AccountIdOf, BalanceOf, BridgedChain, CallOf, HashOf, MessageBridge, RawStorageProof,
|
||||
SignatureOf, SignerOf, ThisChain,
|
||||
};
|
||||
|
||||
use bp_messages::{LaneId, MessageData, MessageKey, MessagePayload};
|
||||
use bp_runtime::ChainId;
|
||||
use bp_messages::{storage_keys, MessageData, MessageKey, MessagePayload};
|
||||
use bp_runtime::{messages::DispatchFeePayment, ChainId};
|
||||
use codec::Encode;
|
||||
use ed25519_dalek::{PublicKey, SecretKey, Signer, KEYPAIR_LENGTH, SECRET_KEY_LENGTH};
|
||||
use frame_support::weights::Weight;
|
||||
use frame_support::{
|
||||
traits::Currency,
|
||||
weights::{GetDispatchInfo, Weight},
|
||||
};
|
||||
use pallet_bridge_messages::benchmarking::{
|
||||
MessageDeliveryProofParams, MessageProofParams, ProofSize,
|
||||
MessageDeliveryProofParams, MessageParams, MessageProofParams, ProofSize,
|
||||
};
|
||||
use sp_core::Hasher;
|
||||
use sp_runtime::traits::Header;
|
||||
use sp_std::prelude::*;
|
||||
use sp_runtime::traits::{Header, IdentifyAccount, MaybeSerializeDeserialize, Zero};
|
||||
use sp_std::{fmt::Debug, prelude::*};
|
||||
use sp_trie::{record_all_keys, trie_types::TrieDBMut, Layout, MemoryDB, Recorder, TrieMut};
|
||||
use sp_version::RuntimeVersion;
|
||||
|
||||
/// Prepare outbound message for the `send_message` call.
|
||||
pub fn prepare_outbound_message<B>(
|
||||
params: MessageParams<AccountIdOf<ThisChain<B>>>,
|
||||
) -> (FromThisChainMessagePayload<B>, BalanceOf<ThisChain<B>>)
|
||||
where
|
||||
B: MessageBridge,
|
||||
BalanceOf<ThisChain<B>>: From<u64>,
|
||||
{
|
||||
let message_payload = vec![0; params.size as usize];
|
||||
let dispatch_origin = bp_message_dispatch::CallOrigin::SourceAccount(params.sender_account);
|
||||
|
||||
let message = FromThisChainMessagePayload::<B> {
|
||||
spec_version: 0,
|
||||
weight: params.size as _,
|
||||
origin: dispatch_origin,
|
||||
call: message_payload,
|
||||
dispatch_fee_payment: DispatchFeePayment::AtSourceChain,
|
||||
};
|
||||
(message, pallet_bridge_messages::benchmarking::MESSAGE_FEE.into())
|
||||
}
|
||||
|
||||
/// 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_message_proof<R, BI, FI, B, BH, BHH>(
|
||||
params: MessageProofParams,
|
||||
version: &RuntimeVersion,
|
||||
endow_amount: BalanceOf<ThisChain<B>>,
|
||||
) -> (FromBridgedChainMessagesProof<HashOf<BridgedChain<B>>>, Weight)
|
||||
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>,
|
||||
B: MessageBridge,
|
||||
BI: 'static,
|
||||
FI: 'static,
|
||||
BH: Header<Hash = HashOf<BridgedChain<B>>>,
|
||||
BHH: Hasher<Out = HashOf<BridgedChain<B>>>,
|
||||
AccountIdOf<BridgedChain<B>>: From<[u8; 32]>,
|
||||
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>>>,
|
||||
{
|
||||
// we'll be dispatching the same call at This chain
|
||||
let remark = match params.size {
|
||||
ProofSize::Minimal(ref size) => vec![0u8; *size as _],
|
||||
_ => vec![],
|
||||
};
|
||||
let call: CallOf<ThisChain<B>> = frame_system::Call::remark { remark }.into();
|
||||
let call_weight = call.get_dispatch_info().weight;
|
||||
|
||||
// message payload needs to be signed, because we use `TargetAccount` call origin
|
||||
// (which is 'heaviest' to verify)
|
||||
let bridged_account_id: AccountIdOf<BridgedChain<B>> = [0u8; 32].into();
|
||||
let (this_raw_public, this_raw_signature) = ed25519_sign(
|
||||
&call,
|
||||
&bridged_account_id,
|
||||
version.spec_version,
|
||||
B::BRIDGED_CHAIN_ID,
|
||||
B::THIS_CHAIN_ID,
|
||||
);
|
||||
let this_public: SignerOf<ThisChain<B>> =
|
||||
sp_core::ed25519::Public::from_raw(this_raw_public).into();
|
||||
let this_signature: SignatureOf<ThisChain<B>> =
|
||||
sp_core::ed25519::Signature::from_raw(this_raw_signature).into();
|
||||
|
||||
// if dispatch fee is paid at this chain, endow relayer account
|
||||
if params.dispatch_fee_payment == DispatchFeePayment::AtTargetChain {
|
||||
pallet_balances::Pallet::<R, BI>::make_free_balance_be(
|
||||
&this_public.clone().into_account(),
|
||||
endow_amount,
|
||||
);
|
||||
}
|
||||
|
||||
// prepare message payload that is stored in the Bridged chain storage
|
||||
let message_payload = bp_message_dispatch::MessagePayload {
|
||||
spec_version: version.spec_version,
|
||||
weight: call_weight,
|
||||
origin: bp_message_dispatch::CallOrigin::<
|
||||
AccountIdOf<BridgedChain<B>>,
|
||||
SignerOf<ThisChain<B>>,
|
||||
SignatureOf<ThisChain<B>>,
|
||||
>::TargetAccount(bridged_account_id, this_public, this_signature),
|
||||
dispatch_fee_payment: params.dispatch_fee_payment.clone(),
|
||||
call: call.encode(),
|
||||
}
|
||||
.encode();
|
||||
|
||||
// finally - prepare storage proof and update environment
|
||||
let (state_root, storage_proof) =
|
||||
prepare_messages_storage_proof::<B, BHH>(¶ms, message_payload);
|
||||
let bridged_header_hash = insert_bridged_chain_header::<R, FI, B, BH>(state_root);
|
||||
|
||||
(
|
||||
FromBridgedChainMessagesProof {
|
||||
bridged_header_hash,
|
||||
storage_proof,
|
||||
lane: params.lane,
|
||||
nonces_start: *params.message_nonces.start(),
|
||||
nonces_end: *params.message_nonces.end(),
|
||||
},
|
||||
call_weight
|
||||
.checked_mul(
|
||||
params.message_nonces.end().saturating_sub(*params.message_nonces.start()) + 1,
|
||||
)
|
||||
.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<R, FI, B, BH, BHH>(
|
||||
params: MessageDeliveryProofParams<AccountIdOf<ThisChain<B>>>,
|
||||
) -> FromBridgedChainMessagesDeliveryProof<HashOf<BridgedChain<B>>>
|
||||
where
|
||||
R: pallet_bridge_grandpa::Config<FI>,
|
||||
R::BridgedChain: bp_runtime::Chain<Header = BH>,
|
||||
FI: 'static,
|
||||
B: MessageBridge,
|
||||
BH: Header<Hash = HashOf<BridgedChain<B>>>,
|
||||
BHH: Hasher<Out = HashOf<BridgedChain<B>>>,
|
||||
HashOf<BridgedChain<B>>: Copy + Default,
|
||||
{
|
||||
// prepare Bridged chain storage with inbound lane state
|
||||
let storage_key =
|
||||
storage_keys::inbound_lane_data_key(B::BRIDGED_MESSAGES_PALLET_NAME, ¶ms.lane).0;
|
||||
let mut root = Default::default();
|
||||
let mut mdb = MemoryDB::default();
|
||||
{
|
||||
let mut trie = TrieDBMut::<BHH>::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::<BHH::Out>::new();
|
||||
record_all_keys::<Layout<BHH>, _>(&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();
|
||||
|
||||
// finally insert header with given state root to our storage
|
||||
let bridged_header_hash = insert_bridged_chain_header::<R, FI, B, BH>(root);
|
||||
|
||||
FromBridgedChainMessagesDeliveryProof {
|
||||
bridged_header_hash: bridged_header_hash.into(),
|
||||
storage_proof,
|
||||
lane: params.lane,
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 = TrieDBMut::<BHH>::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 = 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, ¶ms.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::<BHH::Out>::new();
|
||||
record_all_keys::<Layout<BHH>, _>(&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();
|
||||
|
||||
(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>>
|
||||
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,
|
||||
{
|
||||
let bridged_header = BH::new(
|
||||
Zero::zero(),
|
||||
Default::default(),
|
||||
state_root,
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
);
|
||||
let bridged_header_hash = bridged_header.hash();
|
||||
pallet_bridge_grandpa::initialize_for_benchmarks::<R, FI>(bridged_header);
|
||||
bridged_header_hash
|
||||
}
|
||||
|
||||
/// 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(
|
||||
fn ed25519_sign(
|
||||
target_call: &impl Encode,
|
||||
source_account_id: &impl Encode,
|
||||
target_spec_version: u32,
|
||||
@@ -76,131 +327,6 @@ pub fn ed25519_sign(
|
||||
(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 {
|
||||
|
||||
@@ -41,6 +41,7 @@ const SEED: u32 = 0;
|
||||
pub struct Pallet<T: Config<I>, I: 'static>(crate::Pallet<T, I>);
|
||||
|
||||
/// Proof size requirements.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum ProofSize {
|
||||
/// The proof is expected to be minimal. If value size may be changed, then it is expected to
|
||||
/// have given size.
|
||||
@@ -54,6 +55,7 @@ pub enum ProofSize {
|
||||
}
|
||||
|
||||
/// Benchmark-specific message parameters.
|
||||
#[derive(Debug)]
|
||||
pub struct MessageParams<ThisAccountId> {
|
||||
/// Size of the message payload.
|
||||
pub size: u32,
|
||||
@@ -62,6 +64,7 @@ pub struct MessageParams<ThisAccountId> {
|
||||
}
|
||||
|
||||
/// Benchmark-specific message proof parameters.
|
||||
#[derive(Debug)]
|
||||
pub struct MessageProofParams {
|
||||
/// Id of the lane.
|
||||
pub lane: LaneId,
|
||||
@@ -76,6 +79,7 @@ pub struct MessageProofParams {
|
||||
}
|
||||
|
||||
/// Benchmark-specific message delivery proof parameters.
|
||||
#[derive(Debug)]
|
||||
pub struct MessageDeliveryProofParams<ThisChainAccountId> {
|
||||
/// Id of the lane.
|
||||
pub lane: LaneId,
|
||||
|
||||
Reference in New Issue
Block a user