#![cfg(feature = "runtime-benchmarks")]
use bp_messages::{
source_chain::FromBridgedChainMessagesDeliveryProof,
target_chain::FromBridgedChainMessagesProof, MessagePayload,
};
use bp_polkadot_core::parachains::ParaHash;
use bp_runtime::{AccountIdOf, Chain, HashOf, Parachain};
use codec::Encode;
use frame_support::weights::Weight;
use pallet_bridge_messages::{
benchmarking::{MessageDeliveryProofParams, MessageProofParams},
messages_generation::{
encode_all_messages, encode_lane_data, prepare_message_delivery_storage_proof,
prepare_messages_storage_proof,
},
BridgedChainOf, LaneIdOf, ThisChainOf,
};
use sp_runtime::traits::{Header, Zero};
use sp_std::prelude::*;
use xcm::latest::prelude::*;
fn prepare_inbound_message<LaneId>(
params: &MessageProofParams<LaneId>,
successful_dispatch_message_generator: impl Fn(usize) -> MessagePayload,
) -> MessagePayload {
let expected_size = params.proof_params.db_size.unwrap_or(0) as usize;
if !params.is_successful_dispatch_expected {
return vec![0u8; expected_size]
}
let msg = successful_dispatch_message_generator(expected_size);
assert!(
msg.len() >= expected_size,
"msg.len(): {} does not match expected_size: {}",
expected_size,
msg.len()
);
msg
}
pub fn prepare_message_proof_from_grandpa_chain<R, FI, MI>(
params: MessageProofParams<LaneIdOf<R, MI>>,
message_generator: impl Fn(usize) -> MessagePayload,
) -> (FromBridgedChainMessagesProof<HashOf<BridgedChainOf<R, MI>>, LaneIdOf<R, MI>>, Weight)
where
R: pallet_bridge_grandpa::Config<FI, BridgedChain = BridgedChainOf<R, MI>>
+ pallet_bridge_messages::Config<
MI,
BridgedHeaderChain = pallet_bridge_grandpa::Pallet<R, FI>,
>,
FI: 'static,
MI: 'static,
{
let (state_root, storage_proof) = prepare_messages_storage_proof::<
BridgedChainOf<R, MI>,
ThisChainOf<R, MI>,
LaneIdOf<R, MI>,
>(
params.lane,
params.message_nonces.clone(),
params.outbound_lane_data.clone(),
params.proof_params,
|_| prepare_inbound_message(¶ms, &message_generator),
encode_all_messages,
encode_lane_data,
false,
false,
);
let (_, bridged_header_hash) = insert_header_to_grandpa_pallet::<R, FI>(state_root);
(
FromBridgedChainMessagesProof {
bridged_header_hash,
storage_proof,
lane: params.lane,
nonces_start: *params.message_nonces.start(),
nonces_end: *params.message_nonces.end(),
},
Weight::MAX / 1000,
)
}
pub fn prepare_message_proof_from_parachain<R, PI, MI>(
params: MessageProofParams<LaneIdOf<R, MI>>,
message_generator: impl Fn(usize) -> MessagePayload,
) -> (FromBridgedChainMessagesProof<HashOf<BridgedChainOf<R, MI>>, LaneIdOf<R, MI>>, Weight)
where
R: pallet_bridge_parachains::Config<PI> + pallet_bridge_messages::Config<MI>,
PI: 'static,
MI: 'static,
BridgedChainOf<R, MI>: Chain<Hash = ParaHash> + Parachain,
{
let (state_root, storage_proof) = prepare_messages_storage_proof::<
BridgedChainOf<R, MI>,
ThisChainOf<R, MI>,
LaneIdOf<R, MI>,
>(
params.lane,
params.message_nonces.clone(),
params.outbound_lane_data.clone(),
params.proof_params,
|_| prepare_inbound_message(¶ms, &message_generator),
encode_all_messages,
encode_lane_data,
false,
false,
);
let (_, bridged_header_hash) =
insert_header_to_parachains_pallet::<R, PI, BridgedChainOf<R, MI>>(state_root);
(
FromBridgedChainMessagesProof {
bridged_header_hash,
storage_proof,
lane: params.lane,
nonces_start: *params.message_nonces.start(),
nonces_end: *params.message_nonces.end(),
},
Weight::MAX / 1000,
)
}
pub fn prepare_message_delivery_proof_from_grandpa_chain<R, FI, MI>(
params: MessageDeliveryProofParams<AccountIdOf<ThisChainOf<R, MI>>, LaneIdOf<R, MI>>,
) -> FromBridgedChainMessagesDeliveryProof<HashOf<BridgedChainOf<R, MI>>, LaneIdOf<R, MI>>
where
R: pallet_bridge_grandpa::Config<FI, BridgedChain = BridgedChainOf<R, MI>>
+ pallet_bridge_messages::Config<
MI,
BridgedHeaderChain = pallet_bridge_grandpa::Pallet<R, FI>,
>,
FI: 'static,
MI: 'static,
{
let lane = params.lane;
let (state_root, storage_proof) = prepare_message_delivery_storage_proof::<
BridgedChainOf<R, MI>,
ThisChainOf<R, MI>,
LaneIdOf<R, MI>,
>(params.lane, params.inbound_lane_data, params.proof_params);
let (_, bridged_header_hash) = insert_header_to_grandpa_pallet::<R, FI>(state_root);
FromBridgedChainMessagesDeliveryProof {
bridged_header_hash: bridged_header_hash.into(),
storage_proof,
lane,
}
}
pub fn prepare_message_delivery_proof_from_parachain<R, PI, MI>(
params: MessageDeliveryProofParams<AccountIdOf<ThisChainOf<R, MI>>, LaneIdOf<R, MI>>,
) -> FromBridgedChainMessagesDeliveryProof<HashOf<BridgedChainOf<R, MI>>, LaneIdOf<R, MI>>
where
R: pallet_bridge_parachains::Config<PI> + pallet_bridge_messages::Config<MI>,
PI: 'static,
MI: 'static,
BridgedChainOf<R, MI>: Chain<Hash = ParaHash> + Parachain,
{
let lane = params.lane;
let (state_root, storage_proof) = prepare_message_delivery_storage_proof::<
BridgedChainOf<R, MI>,
ThisChainOf<R, MI>,
LaneIdOf<R, MI>,
>(params.lane, params.inbound_lane_data, params.proof_params);
let (_, bridged_header_hash) =
insert_header_to_parachains_pallet::<R, PI, BridgedChainOf<R, MI>>(state_root);
FromBridgedChainMessagesDeliveryProof {
bridged_header_hash: bridged_header_hash.into(),
storage_proof,
lane,
}
}
pub(crate) fn insert_header_to_grandpa_pallet<R, GI>(
state_root: bp_runtime::HashOf<R::BridgedChain>,
) -> (bp_runtime::BlockNumberOf<R::BridgedChain>, bp_runtime::HashOf<R::BridgedChain>)
where
R: pallet_bridge_grandpa::Config<GI>,
GI: 'static,
R::BridgedChain: bp_runtime::Chain,
{
let bridged_block_number = Zero::zero();
let bridged_header = bp_runtime::HeaderOf::<R::BridgedChain>::new(
bridged_block_number,
Default::default(),
state_root,
Default::default(),
Default::default(),
);
let bridged_header_hash = bridged_header.hash();
pallet_bridge_grandpa::initialize_for_benchmarks::<R, GI>(bridged_header);
(bridged_block_number, bridged_header_hash)
}
pub(crate) fn insert_header_to_parachains_pallet<R, PI, PC>(
state_root: bp_runtime::HashOf<PC>,
) -> (bp_runtime::BlockNumberOf<PC>, bp_runtime::HashOf<PC>)
where
R: pallet_bridge_parachains::Config<PI>,
PI: 'static,
PC: Chain<Hash = ParaHash> + Parachain,
{
let bridged_block_number = Zero::zero();
let bridged_header = bp_runtime::HeaderOf::<PC>::new(
bridged_block_number,
Default::default(),
state_root,
Default::default(),
Default::default(),
);
let bridged_header_hash = bridged_header.hash();
pallet_bridge_parachains::initialize_for_benchmarks::<R, PI, PC>(bridged_header);
(bridged_block_number, bridged_header_hash)
}
pub fn generate_xcm_builder_bridge_message_sample(
destination: InteriorLocation,
) -> impl Fn(usize) -> MessagePayload {
move |expected_message_size| -> MessagePayload {
let location = xcm::VersionedInteriorLocation::from(destination.clone());
let location_encoded_size = location.encoded_size();
let xcm_size = expected_message_size.saturating_sub(location_encoded_size);
let xcm_data_size = xcm_size.saturating_sub(
Instruction::<()>::ExpectPallet {
index: 0,
name: vec![],
module_name: vec![],
crate_major: 0,
min_crate_minor: 0,
}
.encoded_size(),
);
log::trace!(
target: "runtime::bridge-benchmarks",
"generate_xcm_builder_bridge_message_sample with expected_message_size: {}, location_encoded_size: {}, xcm_size: {}, xcm_data_size: {}",
expected_message_size, location_encoded_size, xcm_size, xcm_data_size,
);
let xcm = xcm::VersionedXcm::<()>::from(Xcm(vec![Instruction::<()>::ExpectPallet {
index: 0,
name: vec![42; xcm_data_size],
module_name: vec![],
crate_major: 0,
min_crate_minor: 0,
}]));
(location, xcm).encode().encode()
}
}