Skip to content

Commit

Permalink
Fix barrier (#1076)
Browse files Browse the repository at this point in the history
  • Loading branch information
hqwangningbo authored Nov 13, 2023
1 parent 648bd04 commit 8997f6c
Showing 1 changed file with 66 additions and 14 deletions.
80 changes: 66 additions & 14 deletions runtime/bifrost-kusama/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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};
Expand Down Expand Up @@ -255,6 +254,7 @@ pub type LocationToAccountId = (
SiblingParachainConvertsVia<Sibling, AccountId>,
// Straight up local `AccountId32` origins just alias directly to `AccountId`.
AccountId32Aliases<RelayNetwork, AccountId>,
// TODO: Generate remote accounts according to polkadot standards
// Derives a private `Account32` by hashing `("multiloc", received multilocation)`
Account32Hash<RelayNetwork, AccountId>,
);
Expand Down Expand Up @@ -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<T>(PhantomData<T>);
impl<T: Contains<MultiLocation>> ShouldExecute for AllowTopLevelPaidExecutionDescendOriginFirst<T> {
fn should_execute<Call>(
origin: &MultiLocation,
message: &mut [Instruction<Call>],
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.
Expand All @@ -322,21 +379,16 @@ impl<InnerBarrier: ShouldExecute> ShouldExecute for TrailingSetTopicAsId<InnerBa
}

pub type Barrier = TrailingSetTopicAsId<(
// Weight that is paid for may be consumed.
TakeWeightCredit,
// Expected responses are OK.
AllowKnownQueryResponses<PolkadotXcm>,
// 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<Everything>,
// Subscriptions for version tracking are OK.
AllowSubscriptionsFrom<Everything>,
),
UniversalLocation,
ConstU32<8>,
>,
// If the message is one that immediately attemps to pay for execution, then allow it.
AllowTopLevelPaidExecutionFrom<Everything>,
// Subscriptions for version tracking are OK.
AllowSubscriptionsFrom<Everything>,
// Barrier allowing a top level paid message with DescendOrigin instruction
AllowTopLevelPaidExecutionDescendOriginFirst<Everything>,
)>;

pub type BifrostAssetTransactor = MultiCurrencyAdapter<
Expand Down

0 comments on commit 8997f6c

Please sign in to comment.