Skip to content

Commit

Permalink
Update from bkontur running command 'fmt'
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Nov 7, 2024
1 parent 94dce1a commit edd9c5c
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 80 deletions.
6 changes: 3 additions & 3 deletions bridges/modules/xcm-bridge-hub-router/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

#![cfg(feature = "runtime-benchmarks")]

use crate::{BridgeState, ResolveBridgeId, Bridges, Call, MINIMAL_DELIVERY_FEE_FACTOR};
use crate::{BridgeState, Bridges, Call, ResolveBridgeId, MINIMAL_DELIVERY_FEE_FACTOR};
use frame_benchmarking::{benchmarks_instance_pallet, BenchmarkError, BenchmarkResult};
use frame_support::traits::{UnfilteredDispatchable, EnsureOriginWithArg, Hooks};
use sp_runtime::{Saturating, traits::Zero};
use frame_support::traits::{EnsureOriginWithArg, Hooks, UnfilteredDispatchable};
use sp_runtime::{traits::Zero, Saturating};
use xcm::prelude::*;

/// Pallet we're benchmarking here.
Expand Down
38 changes: 24 additions & 14 deletions bridges/modules/xcm-bridge-hub-router/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ impl<T: Config<I>, I: 'static> bp_xcm_bridge_hub::LocalXcmChannelManager<BridgeI
}
}

/// Adapter implementation for `ExporterFor` that allows exporting message size fee and/or dynamic fees based on the `BridgeId` resolved by the `T::BridgeIdResolver` resolver, if and only if the `E` exporter supports bridging.
/// This adapter acts as an `ExporterFor`, for example, for the `SovereignPaidRemoteExporter`, enabling it to compute message and/or dynamic fees using a fee factor.
/// Adapter implementation for `ExporterFor` that allows exporting message size fee and/or dynamic
/// fees based on the `BridgeId` resolved by the `T::BridgeIdResolver` resolver, if and only if the
/// `E` exporter supports bridging. This adapter acts as an `ExporterFor`, for example, for the
/// `SovereignPaidRemoteExporter`, enabling it to compute message and/or dynamic fees using a fee
/// factor.
pub struct ViaRemoteBridgeHubExporter<T, I, E, BNF, BHLF>(PhantomData<(T, I, E, BNF, BHLF)>);
impl<T: Config<I>, I: 'static, E, BridgedNetworkIdFilter, BridgeHubLocationFilter> ExporterFor
for ViaRemoteBridgeHubExporter<T, I, E, BridgedNetworkIdFilter, BridgeHubLocationFilter>
Expand Down Expand Up @@ -188,41 +191,49 @@ where
}
}

/// Adapter implementation for `SendXcm` that allows adding a message size fee and/or dynamic fees based on the `BridgeId` resolved by the `T::BridgeIdResolver` resolver,
/// if and only if `E` supports routing.
/// This adapter can be used, for example, as a wrapper over `UnpaidLocalExporter`, enabling it to compute message and/or dynamic fees using a fee factor.
/// Adapter implementation for `SendXcm` that allows adding a message size fee and/or dynamic fees
/// based on the `BridgeId` resolved by the `T::BridgeIdResolver` resolver, if and only if `E`
/// supports routing. This adapter can be used, for example, as a wrapper over
/// `UnpaidLocalExporter`, enabling it to compute message and/or dynamic fees using a fee factor.
pub struct ViaLocalBridgeHubExporter<T, I, E>(PhantomData<(T, I, E)>);
impl<T: Config<I>, I: 'static, E: SendXcm> SendXcm for ViaLocalBridgeHubExporter<T, I, E> {
type Ticket = E::Ticket;

fn validate(destination: &mut Option<Location>, message: &mut Option<Xcm<()>>) -> SendResult<Self::Ticket> {
fn validate(
destination: &mut Option<Location>,
message: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket> {
let dest_clone = destination.clone().ok_or(SendError::MissingArgument)?;
let message_size = message.as_ref().map_or(0, |message|message.encoded_size()) as _;
let message_size = message.as_ref().map_or(0, |message| message.encoded_size()) as _;

match E::validate(destination, message) {
Ok((ticket, mut fees)) => {
// calculate message size fees (if configured)
let maybe_message_size_fees = Pallet::<T, I>::calculate_message_size_fees(|| message_size);
let maybe_message_size_fees =
Pallet::<T, I>::calculate_message_size_fees(|| message_size);
if let Some(message_size_fees) = maybe_message_size_fees {
fees.push(message_size_fees);
}

// Here, we have the actual result fees covering bridge fees, so now we need to check/apply
// the congestion and dynamic_fees features (if possible).
// Here, we have the actual result fees covering bridge fees, so now we need to
// check/apply the congestion and dynamic_fees features (if possible).
if let Some(bridge_id) = T::BridgeIdResolver::resolve_for_dest(&dest_clone) {
if let Some(bridge_state) = Bridges::<T, I>::get(bridge_id) {
let mut dynamic_fees = sp_std::vec::Vec::with_capacity(fees.len());
for fee in fees.into_inner() {
dynamic_fees.push(Pallet::<T, I>::calculate_dynamic_fees_for_asset(&bridge_state, fee));
dynamic_fees.push(Pallet::<T, I>::calculate_dynamic_fees_for_asset(
&bridge_state,
fee,
));
}
fees = Assets::from(dynamic_fees);
}
}

// return original ticket with possibly extended fees
Ok((ticket, fees))
}
e => e
},
e => e,
}
}

Expand All @@ -231,7 +242,6 @@ impl<T: Config<I>, I: 'static, E: SendXcm> SendXcm for ViaLocalBridgeHubExporter
}
}


/// Implementation of `ResolveBridgeId` returning `bp_xcm_bridge_hub::BridgeId` based on the
/// configured `UniversalLocation` and remote universal location.
pub struct EnsureIsRemoteBridgeIdResolver<UniversalLocation>(PhantomData<UniversalLocation>);
Expand Down
34 changes: 24 additions & 10 deletions bridges/modules/xcm-bridge-hub-router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,51 @@
// 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/>.

//! A pallet that can be used as an alternative in the XCM router configuration — see the `SendXcm` implementation for details.
//! A pallet that can be used as an alternative in the XCM router configuration — see the `SendXcm`
//! implementation for details.
//!
//! ## Features
//!
//! This pallet offers several optional features to customize functionality:
//!
//! ### Message Size Fee
//! An optional fee based on `T::FeeAsset` and `T::ByteFee`. If `T::FeeAsset` is not specified, this fee is not calculated.
//! An optional fee based on `T::FeeAsset` and `T::ByteFee`. If `T::FeeAsset` is not specified, this
//! fee is not calculated.
//!
//! ### Dynamic Fees and Congestion
//!
//! This pallet supports storing the congestion status of bridge outbound queues. The fee increases exponentially if the queue between this chain and a sibling or child bridge hub becomes congested. All other bridge hub queues provide backpressure mechanisms, so if any of these queues are congested, it will eventually lead to increased queuing on this chain.
//! This pallet supports storing the congestion status of bridge outbound queues. The fee increases
//! exponentially if the queue between this chain and a sibling or child bridge hub becomes
//! congested. All other bridge hub queues provide backpressure mechanisms, so if any of these
//! queues are congested, it will eventually lead to increased queuing on this chain.
//!
//! There are two methods for storing congestion status:
//! 1. A dedicated extrinsic `report_bridge_status`, which relies on `T::BridgeHubOrigin`. This allows the message exporter to send, for example, an XCM `Transact`.
//! 1. A dedicated extrinsic `report_bridge_status`, which relies on `T::BridgeHubOrigin`. This
//! allows the message exporter to send, for example, an XCM `Transact`.
//! 2. An implementation of `bp_xcm_bridge_hub::LocalXcmChannelManager`.
//!
//! ## Usage
//!
//! This pallet provides several implementations, such as `ViaLocalBridgeHubExporter` and `ViaRemoteBridgeHubExporter`, which can expose or access these features.
//! This pallet provides several implementations, such as `ViaLocalBridgeHubExporter` and
//! `ViaRemoteBridgeHubExporter`, which can expose or access these features.
//!
//! This router can be used in two main scenarios, depending on where the router and message exporter (e.g., `pallet_xcm_bridge_hub` or another pallet with an `ExportXcm` implementation) are deployed:
//! This router can be used in two main scenarios, depending on where the router and message
//! exporter (e.g., `pallet_xcm_bridge_hub` or another pallet with an `ExportXcm` implementation)
//! are deployed:
//!
//! ### On the Same Chain as the Message Exporter
//! In this setup, the router directly calls an `ExportXcm` implementation. In this case, `ViaLocalBridgeHubExporter` can be used as a wrapper with `T::ToBridgeHubSender`.
//! In this setup, the router directly calls an `ExportXcm` implementation. In this case,
//! `ViaLocalBridgeHubExporter` can be used as a wrapper with `T::ToBridgeHubSender`.
//!
//! ### On a Different Chain than the Message Exporter
//! In this setup, we need to provide a `SendXcm` implementation for `T::ToBridgeHubSender`, which sends `ExportMessage`. For example, `SovereignPaidRemoteExporter` can be used with `ViaRemoteBridgeHubExporter`.
//! In this setup, we need to provide a `SendXcm` implementation for `T::ToBridgeHubSender`, which
//! sends `ExportMessage`. For example, `SovereignPaidRemoteExporter` can be used with
//! `ViaRemoteBridgeHubExporter`.
//!
//! **Note on Terminology**: When we refer to the bridge hub, we mean the chain that has the `pallet-bridge-messages` with an `ExportXcm` implementation deployed, such as `pallet-xcm-bridge-hub`. Depending on the deployment setup, `T::ToBridgeHubSender` can be configured accordingly—see `T::ToBridgeHubSender` for additional documentation.
//! **Note on Terminology**: When we refer to the bridge hub, we mean the chain that has the
//! `pallet-bridge-messages` with an `ExportXcm` implementation deployed, such as
//! `pallet-xcm-bridge-hub`. Depending on the deployment setup, `T::ToBridgeHubSender` can be
//! configured accordingly—see `T::ToBridgeHubSender` for additional documentation.

#![cfg_attr(not(feature = "std"), no_std)]

Expand Down Expand Up @@ -829,7 +844,6 @@ mod tests {
Pallet::<TestRuntime, ()>::update_bridge_status(bridge_id, false);
assert!(get_bridge_state_for::<TestRuntime, ()>(&dest).is_none());


// update as is_congested=true
Pallet::<TestRuntime, ()>::update_bridge_status(bridge_id, true);
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion bridges/modules/xcm-bridge-hub-router/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,4 @@ impl crate::benchmarking::Config<()> for TestRuntime {
fn ensure_bridged_target_destination() -> Result<Location, frame_benchmarking::BenchmarkError> {
Ok(Location::new(2, [GlobalConsensus(BridgedNetworkId::get())]))
}
}
}
69 changes: 39 additions & 30 deletions bridges/modules/xcm-bridge-hub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,9 @@ pub mod pallet {
/// The states after this call: bridge is `Opened`, outbound lane is `Opened`, inbound lane
/// is `Opened`.
///
/// Optional `maybe_notify` holds data about the `bridge_origin_relative_location` where notifications can be sent to
/// handle congestion. The notification contains an encoded tuple `(BridgeId, bool)`, where `bool` is the `is_congested` flag.
/// Optional `maybe_notify` holds data about the `bridge_origin_relative_location` where
/// notifications can be sent to handle congestion. The notification contains an encoded
/// tuple `(BridgeId, bool)`, where `bool` is the `is_congested` flag.
#[pallet::call_index(0)]
#[pallet::weight(Weight::zero())] // TODO:(bridges-v2) - https://github.com/paritytech/parity-bridges-common/issues/3046 - add benchmarks impl FAIL-CI
pub fn open_bridge(
Expand Down Expand Up @@ -472,13 +473,14 @@ pub mod pallet {
bridge_destination_universal_location: Box<VersionedInteriorLocation>,
maybe_notify: Option<Receiver>,
) -> DispatchResult {
let locations = Self::bridge_locations_from_origin(origin, bridge_destination_universal_location)?;
let locations =
Self::bridge_locations_from_origin(origin, bridge_destination_universal_location)?;
Bridges::<T, I>::try_mutate_exists(locations.bridge_id(), |bridge| match bridge {
Some(b) => {
b.maybe_notify = maybe_notify;

Self::deposit_event(Event::<T, I>::NotificationReceiverUpdated {
bridge_id: * locations.bridge_id(),
bridge_id: *locations.bridge_id(),
maybe_notify: b.maybe_notify.clone(),
});
Ok(())
Expand Down Expand Up @@ -856,7 +858,7 @@ pub mod pallet {
bridge_id: BridgeId,
/// Updated notification receiver.
maybe_notify: Option<Receiver>,
}
},
}

#[pallet::error]
Expand Down Expand Up @@ -1225,7 +1227,7 @@ mod tests {
state: BridgeState::Opened,
deposit: expected_deposit.clone(),
lane_id,
maybe_notify: maybe_notify,
maybe_notify,
}),
);
assert_eq!(
Expand Down Expand Up @@ -1514,51 +1516,58 @@ mod tests {
let origin = OpenBridgeOrigin::parent_relay_chain_origin();
let locations = XcmOverBridge::bridge_locations_from_origin(
origin.clone(),
Box::new(
VersionedInteriorLocation::from(bridged_asset_hub_universal_location())
),
).unwrap();
Box::new(VersionedInteriorLocation::from(bridged_asset_hub_universal_location())),
)
.unwrap();

// open the bridge
assert_ok!(XcmOverBridge::open_bridge(
origin.clone(),
Box::new(locations.bridge_destination_universal_location().clone().into()),
Some(Receiver::new(13, 15)),
));
origin.clone(),
Box::new(locations.bridge_destination_universal_location().clone().into()),
Some(Receiver::new(13, 15)),
));
assert_eq!(
Bridges::<TestRuntime, ()>::get(locations.bridge_id()).map(|b| b.maybe_notify).unwrap(),
Bridges::<TestRuntime, ()>::get(locations.bridge_id())
.map(|b| b.maybe_notify)
.unwrap(),
Some(Receiver::new(13, 15))
);

// update the notification receiver to `None`
assert_ok!(XcmOverBridge::update_notification_receiver(
origin.clone(),
Box::new(locations.bridge_destination_universal_location().clone().into()),
None,
));
origin.clone(),
Box::new(locations.bridge_destination_universal_location().clone().into()),
None,
));
assert_eq!(
Bridges::<TestRuntime, ()>::get(locations.bridge_id()).map(|b| b.maybe_notify).unwrap(),
Bridges::<TestRuntime, ()>::get(locations.bridge_id())
.map(|b| b.maybe_notify)
.unwrap(),
None,
);

// update the notification receiver to `Some(..)`
assert_ok!(XcmOverBridge::update_notification_receiver(
origin.clone(),
Box::new(locations.bridge_destination_universal_location().clone().into()),
Some(Receiver::new(29, 43)),
));
origin.clone(),
Box::new(locations.bridge_destination_universal_location().clone().into()),
Some(Receiver::new(29, 43)),
));
assert_eq!(
Bridges::<TestRuntime, ()>::get(locations.bridge_id()).map(|b| b.maybe_notify).unwrap(),
Bridges::<TestRuntime, ()>::get(locations.bridge_id())
.map(|b| b.maybe_notify)
.unwrap(),
Some(Receiver::new(29, 43))
);
// update the notification receiver to `Some(..)`
assert_ok!(XcmOverBridge::update_notification_receiver(
origin.clone(),
Box::new(locations.bridge_destination_universal_location().clone().into()),
Some(Receiver::new(29, 79)),
));
origin.clone(),
Box::new(locations.bridge_destination_universal_location().clone().into()),
Some(Receiver::new(29, 79)),
));
assert_eq!(
Bridges::<TestRuntime, ()>::get(locations.bridge_id()).map(|b| b.maybe_notify).unwrap(),
Bridges::<TestRuntime, ()>::get(locations.bridge_id())
.map(|b| b.maybe_notify)
.unwrap(),
Some(Receiver::new(29, 79))
);
});
Expand Down
16 changes: 13 additions & 3 deletions bridges/modules/xcm-bridge-hub/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! A module that is responsible for migration of storage.

use crate::{Config, Pallet, LOG_TARGET, Receiver};
use crate::{Config, Pallet, Receiver, LOG_TARGET};
use frame_support::{
traits::{Get, OnRuntimeUpgrade, StorageVersion},
weights::Weight,
Expand Down Expand Up @@ -59,7 +59,15 @@ impl<
BridgedUniversalLocation: Get<InteriorLocation>,
MaybeNotifyRelativeLocation: Get<Option<Receiver>>,
> OnRuntimeUpgrade
for OpenBridgeForLane<T, I, Lane, CreateLane, SourceRelativeLocation, BridgedUniversalLocation, MaybeNotifyRelativeLocation>
for OpenBridgeForLane<
T,
I,
Lane,
CreateLane,
SourceRelativeLocation,
BridgedUniversalLocation,
MaybeNotifyRelativeLocation,
>
{
fn on_runtime_upgrade() -> Weight {
let bridge_origin_relative_location = SourceRelativeLocation::get();
Expand Down Expand Up @@ -108,7 +116,9 @@ impl<
return T::DbWeight::get().reads(2)
}

if let Err(e) = Pallet::<T, I>::do_open_bridge(locations, lane_id, create_lane, maybe_notify) {
if let Err(e) =
Pallet::<T, I>::do_open_bridge(locations, lane_id, create_lane, maybe_notify)
{
log::error!(target: LOG_TARGET, "OpenBridgeForLane - do_open_bridge failed with error: {e:?}");
T::DbWeight::get().reads(6)
} else {
Expand Down
11 changes: 4 additions & 7 deletions bridges/modules/xcm-bridge-hub/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,12 @@ impl pallet_xcm_bridge_hub_router::Config<()> for TestRuntime {
pub type XcmOverBridgeByExportXcmRouterInstance = pallet_xcm_bridge_hub_router::Instance2;
#[derive_impl(pallet_xcm_bridge_hub_router::config_preludes::TestDefaultConfig)]
impl pallet_xcm_bridge_hub_router::Config<XcmOverBridgeByExportXcmRouterInstance> for TestRuntime {
// We use `UnpaidLocalExporter` with `ViaLocalBridgeHubExporter` here to test and ensure that `pallet_xcm_bridge_hub_router` can
// trigger directly `pallet_xcm_bridge_hub` as exporter.
// We use `UnpaidLocalExporter` with `ViaLocalBridgeHubExporter` here to test and ensure that
// `pallet_xcm_bridge_hub_router` can trigger directly `pallet_xcm_bridge_hub` as exporter.
type ToBridgeHubSender = pallet_xcm_bridge_hub_router::impls::ViaLocalBridgeHubExporter<
TestRuntime,
XcmOverBridgeByExportXcmRouterInstance,
UnpaidLocalExporter<XcmOverBridge, UniversalLocation>
UnpaidLocalExporter<XcmOverBridge, UniversalLocation>,
>;

type BridgeIdResolver =
Expand Down Expand Up @@ -550,10 +550,7 @@ impl Convert<Vec<u8>, Xcm<()>> for ReportBridgeStatusXcmProvider {
fn convert(encoded_call: Vec<u8>) -> Xcm<()> {
Xcm(vec![
UnpaidExecution { weight_limit: Unlimited, check_origin: None },
Transact {
origin_kind: OriginKind::Xcm,
call: encoded_call.into(),
},
Transact { origin_kind: OriginKind::Xcm, call: encoded_call.into() },
ExpectTransactStatus(MaybeErrorCode::Success),
])
}
Expand Down
3 changes: 2 additions & 1 deletion bridges/primitives/xcm-bridge-hub/src/call_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ pub enum XcmBridgeHubCall {
open_bridge {
/// Universal `InteriorLocation` from the bridged consensus.
bridge_destination_universal_location: Box<VersionedInteriorLocation>,
/// Optional `maybe_notify` holds data about the `bridge_origin_relative_location` where notifications can be sent to handle congestion.
/// Optional `maybe_notify` holds data about the `bridge_origin_relative_location` where
/// notifications can be sent to handle congestion.
maybe_notify: Option<Receiver>,
},
/// `pallet_xcm_bridge_hub::Call::close_bridge`
Expand Down
11 changes: 10 additions & 1 deletion bridges/primitives/xcm-bridge-hub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,16 @@ pub struct Bridge<ThisChain: Chain, LaneId: LaneIdType> {

/// Receiver metadata.
#[derive(
CloneNoBound, Decode, Encode, Eq, PartialEqNoBound, TypeInfo, MaxEncodedLen, RuntimeDebugNoBound, Serialize, Deserialize,
CloneNoBound,
Decode,
Encode,
Eq,
PartialEqNoBound,
TypeInfo,
MaxEncodedLen,
RuntimeDebugNoBound,
Serialize,
Deserialize,
)]
pub struct Receiver {
/// Pallet index.
Expand Down
Loading

0 comments on commit edd9c5c

Please sign in to comment.