From 8ad87ba8f861f8020752d52063b017423c80553f Mon Sep 17 00:00:00 2001 From: ron Date: Wed, 26 Jun 2024 12:45:24 +0800 Subject: [PATCH] Use a const fee instead --- bridges/snowbridge/Cargo.lock | 2 ++ .../pallets/inbound-queue/src/lib.rs | 30 ++++--------------- .../pallets/inbound-queue/src/mock.rs | 24 ++------------- .../snowbridge/runtime/test-common/src/lib.rs | 6 +--- .../src/bridge_to_ethereum_config.rs | 2 ++ 5 files changed, 12 insertions(+), 52 deletions(-) diff --git a/bridges/snowbridge/Cargo.lock b/bridges/snowbridge/Cargo.lock index 620f87a1dd56..3cb15b781ddb 100644 --- a/bridges/snowbridge/Cargo.lock +++ b/bridges/snowbridge/Cargo.lock @@ -3073,6 +3073,7 @@ name = "pallet-migrations" version = "1.0.0" dependencies = [ "docify", + "frame-benchmarking", "frame-support", "frame-system", "impl-trait-for-tuples", @@ -4748,6 +4749,7 @@ dependencies = [ "snowbridge-core", "snowbridge-pallet-ethereum-client", "snowbridge-pallet-ethereum-client-fixtures", + "snowbridge-pallet-inbound-queue", "snowbridge-pallet-outbound-queue", "snowbridge-pallet-system", "sp-core", diff --git a/bridges/snowbridge/pallets/inbound-queue/src/lib.rs b/bridges/snowbridge/pallets/inbound-queue/src/lib.rs index b5d243ace29c..34fd69c1d671 100644 --- a/bridges/snowbridge/pallets/inbound-queue/src/lib.rs +++ b/bridges/snowbridge/pallets/inbound-queue/src/lib.rs @@ -52,8 +52,8 @@ use sp_core::H160; use sp_runtime::traits::Zero; use sp_std::vec; use xcm::prelude::{ - send_xcm, validate_send, Junction::*, Location, SendError as XcmpSendError, SendXcm, Xcm, - XcmContext, XcmHash, *, + send_xcm, Junction::*, Location, SendError as XcmpSendError, SendXcm, Xcm, XcmContext, XcmHash, + *, }; use xcm_executor::traits::TransactAsset; @@ -143,7 +143,7 @@ pub mod pallet { type AssetTransactor: TransactAsset; /// The most expensive xcm here only used to estimate send cost - type MaxSendCostXcm: Get>; + type MaxSendCost: Get>; } #[pallet::hooks] @@ -362,33 +362,13 @@ pub mod pallet { })?; Ok(()) } - - pub fn calculate_send_cost( - para_id: ParaId, - xcm: Xcm<()>, - ) -> Result, Error> { - let dest = Location::new(1, [Parachain(para_id.into())]); - let (_, assets) = validate_send::(dest, xcm).map_err(Error::::from)?; - ensure!(assets.len() == 1, Error::::Send(SendError::Fees)); - let fee = assets.get(0).unwrap(); - let cost: u128 = match (*fee).fun { - Fungible(amount) => Some(amount), - _ => None, - } - .ok_or(Error::::Send(SendError::Fees))?; - Ok(cost.saturated_into::>()) - } } /// API for accessing the delivery cost of a message impl Get> for Pallet { fn get() -> BalanceOf { - // Cost here based on MaxMessagePayloadSize(the worst case) - let delivery_cost = Self::calculate_delivery_cost(T::MaxMessageSize::get()); - // Cost here based on MaxSendCostXcm(the worst case) - let send_cost = Self::calculate_send_cost(1000_u32.into(), T::MaxSendCostXcm::get()) - .unwrap_or_default(); - delivery_cost.saturating_add(send_cost) + Self::calculate_delivery_cost(T::MaxMessageSize::get()) + .saturating_add(T::MaxSendCost::get()) } } } diff --git a/bridges/snowbridge/pallets/inbound-queue/src/mock.rs b/bridges/snowbridge/pallets/inbound-queue/src/mock.rs index 77ba83bd9b47..df2f65fb34d6 100644 --- a/bridges/snowbridge/pallets/inbound-queue/src/mock.rs +++ b/bridges/snowbridge/pallets/inbound-queue/src/mock.rs @@ -176,27 +176,7 @@ parameter_types! { multiplier: FixedU128::from_rational(1, 1), }; pub DefaultFee: Asset = (Parent, 1_000_000_000).into(); - pub MaxSendCostXcm: Xcm<()> = vec![ - ReceiveTeleportedAsset(DefaultFee::get().into()), - BuyExecution { fees: DefaultFee::get(), weight_limit: Unlimited }, - DescendOrigin(PalletInstance(80).into()), - UniversalOrigin(GlobalConsensus(Ethereum { chain_id: 1 })), - ReserveAssetDeposited(DefaultFee::get().into()), - ClearOrigin, - DepositReserveAsset { - assets: Definite(DefaultFee::get().into()), - dest: Location::new(1, [Parachain(1000)]), - xcm: vec![ - // Buy execution on target. - BuyExecution { fees: DefaultFee::get(), weight_limit: Unlimited }, - // Deposit asset to beneficiary. - DepositAsset { assets: Definite(DefaultFee::get().into()), beneficiary: Location::default() }, - // Forward message id to destination parachain. - SetTopic([0; 32]), - ] - .into(), - }, - ].into(); + pub MaxSendCost: u128 = 1; } pub const DOT: u128 = 10_000_000_000; @@ -270,7 +250,7 @@ impl inbound_queue::Config for Test { type LengthToFee = IdentityFee; type MaxMessageSize = ConstU32<1024>; type AssetTransactor = SuccessfulTransactor; - type MaxSendCostXcm = MaxSendCostXcm; + type MaxSendCost = MaxSendCost; } pub fn last_events(n: usize) -> Vec { diff --git a/bridges/snowbridge/runtime/test-common/src/lib.rs b/bridges/snowbridge/runtime/test-common/src/lib.rs index 579f4f9ce4d0..0e2753d61599 100644 --- a/bridges/snowbridge/runtime/test-common/src/lib.rs +++ b/bridges/snowbridge/runtime/test-common/src/lib.rs @@ -595,11 +595,7 @@ where ::MaxMessageSize::get(), ); let inbound_send_cost = - >::calculate_send_cost( - 1000.into(), - ::MaxSendCostXcm::get(), - ) - .unwrap(); + ::MaxSendCost::get(); let outbound_delivery_cost = >::calculate_local_fee(); diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_to_ethereum_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_to_ethereum_config.rs index 53a536655408..96aae0e50eb9 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_to_ethereum_config.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_to_ethereum_config.rs @@ -63,6 +63,7 @@ parameter_types! { rewards: Rewards { local: 1 * UNITS, remote: meth(1) }, multiplier: FixedU128::from_rational(1, 1), }; + pub MaxSendCost: Balance = UNITS / 10; } impl snowbridge_pallet_inbound_queue::Config for Runtime { @@ -90,6 +91,7 @@ impl snowbridge_pallet_inbound_queue::Config for Runtime { type WeightInfo = crate::weights::snowbridge_pallet_inbound_queue::WeightInfo; type PricingParameters = EthereumSystem; type AssetTransactor = ::AssetTransactor; + type MaxSendCost = MaxSendCost; } impl snowbridge_pallet_outbound_queue::Config for Runtime {