Skip to content

Commit

Permalink
Fix benchmarks for BH
Browse files Browse the repository at this point in the history
  • Loading branch information
bkontur committed Nov 7, 2024
1 parent 1c2dedc commit d329dec
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 190 deletions.
26 changes: 26 additions & 0 deletions bridges/modules/xcm-bridge-hub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,32 @@ pub mod pallet {
}
}

#[cfg(feature = "runtime-benchmarks")]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Open bridge for lane.
pub fn open_bridge_for_benchmarks(
lane_id: LaneIdOf<T, I>,
bridge_origin_relative_location: Location,
bridge_destination_universal_location: InteriorLocation,
create_lanes: bool,
maybe_notify: Option<Receiver>,
) -> Result<Box<BridgeLocations>, sp_runtime::DispatchError> {
let locations = Pallet::<T, I>::bridge_locations(
bridge_origin_relative_location.into(),
bridge_destination_universal_location.into(),
)?;

Pallet::<T, I>::do_open_bridge(
locations.clone(),
lane_id,
create_lanes,
maybe_notify,
)?;

Ok(locations)
}
}

#[cfg(any(test, feature = "try-runtime", feature = "std"))]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Ensure the correctness of the state of this pallet.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,55 +235,6 @@ mod tests {
}
}

#[cfg(feature = "runtime-benchmarks")]
pub(crate) fn open_bridge_for_benchmarks<R, XBHI, C>(
with: pallet_xcm_bridge_hub::LaneIdOf<R, XBHI>,
sibling_para_id: u32,
) -> InteriorLocation
where
R: pallet_xcm_bridge_hub::Config<XBHI>,
XBHI: 'static,
C: xcm_executor::traits::ConvertLocation<
bp_runtime::AccountIdOf<pallet_xcm_bridge_hub::ThisChainOf<R, XBHI>>,
>,
{
use pallet_xcm_bridge_hub::{Bridge, BridgeId, BridgeState};
use sp_runtime::traits::Zero;
use xcm::{latest::ROCOCO_GENESIS_HASH, VersionedInteriorLocation};

// insert bridge metadata
let lane_id = with;
let sibling_parachain = Location::new(1, [Parachain(sibling_para_id)]);
let universal_source =
[GlobalConsensus(ByGenesis(ROCOCO_GENESIS_HASH)), Parachain(sibling_para_id)].into();
let universal_destination =
[GlobalConsensus(RococoBulletinGlobalConsensusNetwork::get()), Parachain(2075)].into();
let bridge_id = BridgeId::new(&universal_source, &universal_destination);

// insert only bridge metadata, because the benchmarks create lanes
pallet_xcm_bridge_hub::Bridges::<R, XBHI>::insert(
bridge_id,
Bridge {
bridge_origin_relative_location: alloc::boxed::Box::new(
sibling_parachain.clone().into(),
),
bridge_origin_universal_location: alloc::boxed::Box::new(
VersionedInteriorLocation::from(universal_source.clone()),
),
bridge_destination_universal_location: alloc::boxed::Box::new(
VersionedInteriorLocation::from(universal_destination),
),
state: BridgeState::Opened,
bridge_owner_account: C::convert_location(&sibling_parachain).expect("valid AccountId"),
deposit: Zero::zero(),
lane_id,
},
);
pallet_xcm_bridge_hub::LaneToBridge::<R, XBHI>::insert(lane_id, bridge_id);

universal_source
}

/// Contains the migration for the PeopleRococo<>RococoBulletin bridge.
pub mod migration {
use super::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,55 +193,6 @@ impl pallet_xcm_bridge_hub::Config<XcmOverBridgeHubWestendInstance> for Runtime
>;
}

#[cfg(feature = "runtime-benchmarks")]
pub(crate) fn open_bridge_for_benchmarks<R, XBHI, C>(
with: pallet_xcm_bridge_hub::LaneIdOf<R, XBHI>,
sibling_para_id: u32,
) -> InteriorLocation
where
R: pallet_xcm_bridge_hub::Config<XBHI>,
XBHI: 'static,
C: xcm_executor::traits::ConvertLocation<
bp_runtime::AccountIdOf<pallet_xcm_bridge_hub::ThisChainOf<R, XBHI>>,
>,
{
use pallet_xcm_bridge_hub::{Bridge, BridgeId, BridgeState};
use sp_runtime::traits::Zero;
use xcm::{latest::ROCOCO_GENESIS_HASH, VersionedInteriorLocation};

// insert bridge metadata
let lane_id = with;
let sibling_parachain = Location::new(1, [Parachain(sibling_para_id)]);
let universal_source =
[GlobalConsensus(ByGenesis(ROCOCO_GENESIS_HASH)), Parachain(sibling_para_id)].into();
let universal_destination =
[GlobalConsensus(ByGenesis(WESTEND_GENESIS_HASH)), Parachain(2075)].into();
let bridge_id = BridgeId::new(&universal_source, &universal_destination);

// insert only bridge metadata, because the benchmarks create lanes
pallet_xcm_bridge_hub::Bridges::<R, XBHI>::insert(
bridge_id,
Bridge {
bridge_origin_relative_location: alloc::boxed::Box::new(
sibling_parachain.clone().into(),
),
bridge_origin_universal_location: alloc::boxed::Box::new(
VersionedInteriorLocation::from(universal_source.clone()),
),
bridge_destination_universal_location: alloc::boxed::Box::new(
VersionedInteriorLocation::from(universal_destination),
),
state: BridgeState::Opened,
bridge_owner_account: C::convert_location(&sibling_parachain).expect("valid AccountId"),
deposit: Zero::zero(),
lane_id,
},
);
pallet_xcm_bridge_hub::LaneToBridge::<R, XBHI>::insert(lane_id, bridge_id);

universal_source
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1278,14 +1278,12 @@ impl_runtime_apis! {

// open bridge
let bridge_destination_universal_location: InteriorLocation = [GlobalConsensus(NetworkId::ByGenesis(WESTEND_GENESIS_HASH)), Parachain(8765)].into();
let locations = XcmOverBridgeHubWestend::bridge_locations(
let _ = XcmOverBridgeHubWestend::open_bridge_for_benchmarks(
bp_messages::LegacyLaneId([1, 2, 3, 4]),
sibling_parachain_location.clone(),
bridge_destination_universal_location.clone(),
)?;
XcmOverBridgeHubWestend::do_open_bridge(
locations,
bp_messages::LegacyLaneId([1, 2, 3, 4]),
true,
None,
).map_err(|e| {
log::error!(
"Failed to `XcmOverBridgeHubWestend::open_bridge`({:?}, {:?})`, error: {:?}",
Expand Down Expand Up @@ -1354,26 +1352,32 @@ impl_runtime_apis! {
use cumulus_primitives_core::XcmpMessageSource;
assert!(XcmpQueue::take_outbound_messages(usize::MAX).is_empty());
ParachainSystem::open_outbound_hrmp_channel_for_benchmarks_or_tests(42.into());
let universal_source = bridge_to_westend_config::open_bridge_for_benchmarks::<
Runtime,
bridge_to_westend_config::XcmOverBridgeHubWestendInstance,
xcm_config::LocationToAccountId,
>(params.lane, 42);
let bridge_locations = XcmOverBridgeHubWestend::open_bridge_for_benchmarks(
params.lane,
Location::new(1, [Parachain(42)]),
[GlobalConsensus(bridge_to_westend_config::WestendGlobalConsensusNetwork::get()), Parachain(2075)].into(),
// do not create lanes, because they are already created `params.lane`
false,
None,
).expect("valid bridge opened");
prepare_message_proof_from_parachain::<
Runtime,
bridge_common_config::BridgeGrandpaWestendInstance,
bridge_to_westend_config::WithBridgeHubWestendMessagesInstance,
>(params, generate_xcm_builder_bridge_message_sample(universal_source))
>(params, generate_xcm_builder_bridge_message_sample(bridge_locations.bridge_origin_universal_location().clone()))
}

fn prepare_message_delivery_proof(
params: MessageDeliveryProofParams<AccountId, LaneIdOf<Runtime, bridge_to_westend_config::WithBridgeHubWestendMessagesInstance>>,
) -> bridge_to_westend_config::ToWestendBridgeHubMessagesDeliveryProof<bridge_to_westend_config::WithBridgeHubWestendMessagesInstance> {
let _ = bridge_to_westend_config::open_bridge_for_benchmarks::<
Runtime,
bridge_to_westend_config::XcmOverBridgeHubWestendInstance,
xcm_config::LocationToAccountId,
>(params.lane, 42);
let _ = XcmOverBridgeHubWestend::open_bridge_for_benchmarks(
params.lane,
Location::new(1, [Parachain(42)]),
[GlobalConsensus(bridge_to_westend_config::WestendGlobalConsensusNetwork::get()), Parachain(2075)].into(),
// do not create lanes, because they are already created `params.lane`
false,
None,
);
prepare_message_delivery_proof_from_parachain::<
Runtime,
bridge_common_config::BridgeGrandpaWestendInstance,
Expand All @@ -1399,26 +1403,32 @@ impl_runtime_apis! {
use cumulus_primitives_core::XcmpMessageSource;
assert!(XcmpQueue::take_outbound_messages(usize::MAX).is_empty());
ParachainSystem::open_outbound_hrmp_channel_for_benchmarks_or_tests(42.into());
let universal_source = bridge_to_bulletin_config::open_bridge_for_benchmarks::<
Runtime,
bridge_to_bulletin_config::XcmOverPolkadotBulletinInstance,
xcm_config::LocationToAccountId,
>(params.lane, 42);
let bridge_locations = XcmOverPolkadotBulletin::open_bridge_for_benchmarks(
params.lane,
Location::new(1, [Parachain(42)]),
[GlobalConsensus(bridge_to_bulletin_config::RococoBulletinGlobalConsensusNetwork::get())].into(),
// do not create lanes, because they are already created `params.lane`
false,
None,
).expect("valid bridge opened");
prepare_message_proof_from_grandpa_chain::<
Runtime,
bridge_common_config::BridgeGrandpaRococoBulletinInstance,
bridge_to_bulletin_config::WithRococoBulletinMessagesInstance,
>(params, generate_xcm_builder_bridge_message_sample(universal_source))
>(params, generate_xcm_builder_bridge_message_sample(bridge_locations.bridge_origin_universal_location().clone()))
}

fn prepare_message_delivery_proof(
params: MessageDeliveryProofParams<AccountId, LaneIdOf<Runtime, bridge_to_bulletin_config::WithRococoBulletinMessagesInstance>>,
) -> bridge_to_bulletin_config::ToRococoBulletinMessagesDeliveryProof<bridge_to_bulletin_config::WithRococoBulletinMessagesInstance> {
let _ = bridge_to_bulletin_config::open_bridge_for_benchmarks::<
Runtime,
bridge_to_bulletin_config::XcmOverPolkadotBulletinInstance,
xcm_config::LocationToAccountId,
>(params.lane, 42);
let _ = XcmOverPolkadotBulletin::open_bridge_for_benchmarks(
params.lane,
Location::new(1, [Parachain(42)]),
[GlobalConsensus(bridge_to_bulletin_config::RococoBulletinGlobalConsensusNetwork::get())].into(),
// do not create lanes, because they are already created `params.lane`
false,
None,
).expect("valid bridge opened");
prepare_message_delivery_proof_from_grandpa_chain::<
Runtime,
bridge_common_config::BridgeGrandpaRococoBulletinInstance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,55 +222,6 @@ impl pallet_xcm_bridge_hub::Config<XcmOverBridgeHubRococoInstance> for Runtime {
>;
}

#[cfg(feature = "runtime-benchmarks")]
pub(crate) fn open_bridge_for_benchmarks<R, XBHI, C>(
with: pallet_xcm_bridge_hub::LaneIdOf<R, XBHI>,
sibling_para_id: u32,
) -> InteriorLocation
where
R: pallet_xcm_bridge_hub::Config<XBHI>,
XBHI: 'static,
C: xcm_executor::traits::ConvertLocation<
bp_runtime::AccountIdOf<pallet_xcm_bridge_hub::ThisChainOf<R, XBHI>>,
>,
{
use pallet_xcm_bridge_hub::{Bridge, BridgeId, BridgeState};
use sp_runtime::traits::Zero;
use xcm::{latest::WESTEND_GENESIS_HASH, VersionedInteriorLocation};

// insert bridge metadata
let lane_id = with;
let sibling_parachain = Location::new(1, [Parachain(sibling_para_id)]);
let universal_source =
[GlobalConsensus(ByGenesis(WESTEND_GENESIS_HASH)), Parachain(sibling_para_id)].into();
let universal_destination =
[GlobalConsensus(ByGenesis(ROCOCO_GENESIS_HASH)), Parachain(2075)].into();
let bridge_id = BridgeId::new(&universal_source, &universal_destination);

// insert only bridge metadata, because the benchmarks create lanes
pallet_xcm_bridge_hub::Bridges::<R, XBHI>::insert(
bridge_id,
Bridge {
bridge_origin_relative_location: alloc::boxed::Box::new(
sibling_parachain.clone().into(),
),
bridge_origin_universal_location: alloc::boxed::Box::new(
VersionedInteriorLocation::from(universal_source.clone()),
),
bridge_destination_universal_location: alloc::boxed::Box::new(
VersionedInteriorLocation::from(universal_destination),
),
state: BridgeState::Opened,
bridge_owner_account: C::convert_location(&sibling_parachain).expect("valid AccountId"),
deposit: Zero::zero(),
lane_id,
},
);
pallet_xcm_bridge_hub::LaneToBridge::<R, XBHI>::insert(lane_id, bridge_id);

universal_source
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1163,14 +1163,12 @@ impl_runtime_apis! {

// open bridge
let bridge_destination_universal_location: InteriorLocation = [GlobalConsensus(ByGenesis(ROCOCO_GENESIS_HASH)), Parachain(8765)].into();
let locations = XcmOverBridgeHubRococo::bridge_locations(
let _ = XcmOverBridgeHubRococo::open_bridge_for_benchmarks(
bp_messages::LegacyLaneId([1, 2, 3, 4]),
sibling_parachain_location.clone(),
bridge_destination_universal_location.clone(),
)?;
XcmOverBridgeHubRococo::do_open_bridge(
locations,
bp_messages::LegacyLaneId([1, 2, 3, 4]),
true,
None,
).map_err(|e| {
log::error!(
"Failed to `XcmOverBridgeHubRococo::open_bridge`({:?}, {:?})`, error: {:?}",
Expand Down Expand Up @@ -1234,26 +1232,32 @@ impl_runtime_apis! {
use cumulus_primitives_core::XcmpMessageSource;
assert!(XcmpQueue::take_outbound_messages(usize::MAX).is_empty());
ParachainSystem::open_outbound_hrmp_channel_for_benchmarks_or_tests(42.into());
let universal_source = bridge_to_rococo_config::open_bridge_for_benchmarks::<
Runtime,
bridge_to_rococo_config::XcmOverBridgeHubRococoInstance,
xcm_config::LocationToAccountId,
>(params.lane, 42);
let bridge_locations = XcmOverBridgeHubRococo::open_bridge_for_benchmarks(
params.lane,
Location::new(1, [Parachain(42)]),
[GlobalConsensus(bridge_to_rococo_config::RococoGlobalConsensusNetwork::get()), Parachain(2075)].into(),
// do not create lanes, because they are already created `params.lane`
false,
None,
).expect("valid bridge opened");
prepare_message_proof_from_parachain::<
Runtime,
bridge_to_rococo_config::BridgeGrandpaRococoInstance,
bridge_to_rococo_config::WithBridgeHubRococoMessagesInstance,
>(params, generate_xcm_builder_bridge_message_sample(universal_source))
>(params, generate_xcm_builder_bridge_message_sample(bridge_locations.bridge_origin_universal_location().clone()))
}

fn prepare_message_delivery_proof(
params: MessageDeliveryProofParams<AccountId, LaneIdOf<Runtime, bridge_to_rococo_config::WithBridgeHubRococoMessagesInstance>>,
) -> bridge_to_rococo_config::ToRococoBridgeHubMessagesDeliveryProof<bridge_to_rococo_config::WithBridgeHubRococoMessagesInstance> {
let _ = bridge_to_rococo_config::open_bridge_for_benchmarks::<
Runtime,
bridge_to_rococo_config::XcmOverBridgeHubRococoInstance,
xcm_config::LocationToAccountId,
>(params.lane, 42);
let _ = XcmOverBridgeHubRococo::open_bridge_for_benchmarks(
params.lane,
Location::new(1, [Parachain(42)]),
[GlobalConsensus(bridge_to_rococo_config::RococoGlobalConsensusNetwork::get()), Parachain(2075)].into(),
// do not create lanes, because they are already created `params.lane`
false,
None,
);
prepare_message_delivery_proof_from_parachain::<
Runtime,
bridge_to_rococo_config::BridgeGrandpaRococoInstance,
Expand Down

0 comments on commit d329dec

Please sign in to comment.