diff --git a/runtime/bifrost-kusama/src/xcm_config.rs b/runtime/bifrost-kusama/src/xcm_config.rs index b581d3789..0226d0fb4 100644 --- a/runtime/bifrost-kusama/src/xcm_config.rs +++ b/runtime/bifrost-kusama/src/xcm_config.rs @@ -21,7 +21,7 @@ use bifrost_asset_registry::{AssetIdMaps, FixedRateOfAsset}; use codec::{Decode, Encode}; pub use cumulus_primitives_core::ParaId; use frame_support::{ - parameter_types, + ensure, parameter_types, sp_runtime::traits::{CheckedConversion, Convert}, traits::Get, }; @@ -34,7 +34,6 @@ pub use xcm_builder::{ FixedWeightBounds, IsConcrete, ParentAsSuperuser, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeRevenue, TakeWeightCredit, - WithComputedOrigin, }; use xcm_executor::traits::{MatchesFungible, ShouldExecute}; pub use xcm_interface::traits::{parachains, XcmBaseWeight}; @@ -255,6 +254,7 @@ pub type LocationToAccountId = ( SiblingParachainConvertsVia, // Straight up local `AccountId32` origins just alias directly to `AccountId`. AccountId32Aliases, + // TODO: Generate remote accounts according to polkadot standards // Derives a private `Account32` by hashing `("multiloc", received multilocation)` Account32Hash, ); @@ -296,6 +296,63 @@ match_types! { }; } +/// Barrier allowing a top level paid message with DescendOrigin instruction +pub const DEFAULT_PROOF_SIZE: u64 = 64 * 1024; +pub const DEFAULT_REF_TIMR: u64 = 10_000_000_000; +pub struct AllowTopLevelPaidExecutionDescendOriginFirst(PhantomData); +impl> ShouldExecute for AllowTopLevelPaidExecutionDescendOriginFirst { + fn should_execute( + origin: &MultiLocation, + message: &mut [Instruction], + max_weight: Weight, + _weight_credit: &mut Weight, + ) -> Result<(), ProcessMessageError> { + log::trace!( + target: "xcm::barriers", + "AllowTopLevelPaidExecutionDescendOriginFirst origin: + {:?}, message: {:?}, max_weight: {:?}, weight_credit: {:?}", + origin, message, max_weight, _weight_credit, + ); + ensure!(T::contains(origin), ProcessMessageError::Unsupported); + let mut iter = message.iter_mut(); + // Make sure the first instruction is DescendOrigin + iter.next() + .filter(|instruction| matches!(instruction, DescendOrigin(_))) + .ok_or(ProcessMessageError::Unsupported)?; + + // Then WithdrawAsset + iter.next() + .filter(|instruction| matches!(instruction, WithdrawAsset(_))) + .ok_or(ProcessMessageError::Unsupported)?; + + // Then BuyExecution + let i = iter.next().ok_or(ProcessMessageError::Unsupported)?; + match i { + BuyExecution { weight_limit: Limited(ref mut weight), .. } => { + if weight.all_gte(max_weight) { + weight.set_ref_time(max_weight.ref_time()); + weight.set_proof_size(max_weight.proof_size()); + }; + }, + BuyExecution { ref mut weight_limit, .. } if weight_limit == &Unlimited => { + *weight_limit = Limited(max_weight); + }, + _ => {}, + }; + + // Then Transact + let i = iter.next().ok_or(ProcessMessageError::Unsupported)?; + match i { + Transact { ref mut require_weight_at_most, .. } => { + let weight = Weight::from_parts(DEFAULT_REF_TIMR, DEFAULT_PROOF_SIZE); + *require_weight_at_most = weight; + Ok(()) + }, + _ => Err(ProcessMessageError::Unsupported), + } + } +} + /// Sets the message ID to `t` using a `SetTopic(t)` in the last position if present. /// /// Requires some inner barrier to pass on the rest of the message. @@ -322,21 +379,16 @@ impl ShouldExecute for TrailingSetTopicAsId, - // Allow XCMs with some computed origins to pass through. - WithComputedOrigin< - ( - // If the message is one that immediately attemps to pay for execution, then - // allow it. - AllowTopLevelPaidExecutionFrom, - // Subscriptions for version tracking are OK. - AllowSubscriptionsFrom, - ), - UniversalLocation, - ConstU32<8>, - >, + // If the message is one that immediately attemps to pay for execution, then allow it. + AllowTopLevelPaidExecutionFrom, + // Subscriptions for version tracking are OK. + AllowSubscriptionsFrom, + // Barrier allowing a top level paid message with DescendOrigin instruction + AllowTopLevelPaidExecutionDescendOriginFirst, )>; pub type BifrostAssetTransactor = MultiCurrencyAdapter<