Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
AllowUnpaidExecutionFromSnowBridgeWithFeeChecked
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Mar 7, 2024
1 parent 04a339e commit c18f771
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -809,3 +809,60 @@ fn transact_from_ethereum_to_penpal_fee_paid_on_ethereum() {
);
});
}

#[test]
fn transact_from_ethereum_to_penpal_fee_paid_on_ethereum_fail_for_insufficient_fee() {
BridgeHubRococo::fund_para_sovereign(PenpalA::para_id().into(), INITIAL_FUND);

BridgeHubRococo::execute_with(|| {
type RuntimeEvent = <BridgeHubRococo as Chain>::RuntimeEvent;
type Runtime = <BridgeHubRococo as Chain>::Runtime;

let agent_id = snowbridge_pallet_system::agent_id_of::<Runtime>(&Location::new(
1,
[Parachain(PenpalA::para_id().into())],
))
.unwrap();
snowbridge_pallet_system::Agents::<Runtime>::insert(agent_id, ());
let channel_id: ChannelId = PenpalA::para_id().into();
snowbridge_pallet_system::Channels::<Runtime>::insert(
channel_id,
Channel { agent_id, para_id: PenpalA::para_id() },
);

let message_id: H256 = [1; 32].into();
let message = VersionedMessage::V1(MessageV1 {
chain_id: CHAIN_ID,
command: Command::Transact {
sender: hex!("90A987B944Cb1dCcE5564e5FDeCD7a54D3de27Fe").into(),
fee: INSUFFICIENT_XCM_FEE,
weight_at_most: XCM_WEIGHT,
origin_kind: OriginKind::SovereignAccount,
payload: hex!("00071468656c6c6f").to_vec(),
fee_mode: TransactFeeMode::OnEthereum,
},
});
// Convert the message to XCM
let (xcm, _) = EthereumInboundQueue::do_convert(message_id, message).unwrap();
// Send the XCM
let _ = EthereumInboundQueue::send_xcm(xcm, PenpalA::para_id().into()).unwrap();

assert_expected_events!(
BridgeHubRococo,
vec![
RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::XcmpMessageSent { .. }) => {},
]
);
});

PenpalA::execute_with(|| {
type RuntimeEvent = <PenpalA as Chain>::RuntimeEvent;
// Check xcm execution fails on PenPal
assert_expected_events!(
PenpalA,
vec![
RuntimeEvent::MessageQueue(pallet_message_queue::Event::ProcessingFailed{ .. }) => {},
]
);
});
}
57 changes: 46 additions & 11 deletions cumulus/parachains/runtimes/testing/penpal/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ use super::{
use codec::Encode;
use core::marker::PhantomData;
use frame_support::{
parameter_types,
ensure, parameter_types,
traits::{
fungibles::{self, Balanced, Credit},
ConstU32, Contains, ContainsPair, Equals, Everything, Get, Nothing,
ConstU32, Contains, ContainsPair, Equals, Everything, Get, Nothing, ProcessMessageError,
},
weights::Weight,
};
Expand All @@ -45,21 +45,21 @@ use polkadot_parachain_primitives::primitives::Sibling;
use polkadot_runtime_common::impls::ToAuthor;
use sp_io::hashing::blake2_256;
use sp_runtime::traits::Zero;
use sp_std::collections::btree_set::BTreeSet;
use sp_std::{collections::btree_set::BTreeSet, ops::ControlFlow};
use testnet_parachains_constants::rococo::snowbridge::EthereumNetwork;
use xcm::latest::prelude::*;
use xcm_builder::{
AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses,
AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom, AsPrefixedGeneralIndex,
ConvertedConcreteId, EnsureXcmOrigin, FixedWeightBounds, FrameTransactionalProcessor,
FungibleAdapter, FungiblesAdapter, IsConcrete, LocalMint, NativeAsset, NoChecking,
ParentAsSuperuser, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative,
SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32,
SovereignSignedViaLocation, StartsWith, TakeWeightCredit, TrailingSetTopicAsId,
UsingComponents, WithComputedOrigin, WithUniqueTopic,
ConvertedConcreteId, CreateMatcher, EnsureXcmOrigin, FixedWeightBounds,
FrameTransactionalProcessor, FungibleAdapter, FungiblesAdapter, IsConcrete, LocalMint,
MatchXcm, NativeAsset, NoChecking, ParentAsSuperuser, ParentIsPreset, RelayChainAsNative,
SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation, StartsWith, TakeWeightCredit,
TrailingSetTopicAsId, UsingComponents, WithComputedOrigin, WithUniqueTopic,
};
use xcm_executor::{
traits::{ConvertLocation, JustTry},
traits::{ConvertLocation, JustTry, Properties, ShouldExecute, WeightTrader},
XcmExecutor,
};

Expand Down Expand Up @@ -200,12 +200,47 @@ impl Contains<Location> for CommonGoodAssetsParachain {
}
}

pub struct AllowUnpaidExecutionFromSnowBridgeWithFeeChecked<T>(PhantomData<T>);
impl<T: Contains<Location>> ShouldExecute for AllowUnpaidExecutionFromSnowBridgeWithFeeChecked<T> {
fn should_execute<RuntimeCall>(
origin: &Location,
instructions: &mut [Instruction<RuntimeCall>],
max_weight: Weight,
_properties: &mut Properties,
) -> Result<(), ProcessMessageError> {
log::trace!(
target: "xcm::barriers",
"AllowUnpaidExecutionFromSnowBridgeWithFeeChecked origin: {:?}, instructions: {:?}, max_weight: {:?}, properties: {:?}",
origin, instructions, max_weight, _properties,
);
ensure!(T::contains(origin), ProcessMessageError::Unsupported);
let mut fee_assets = xcm::prelude::Assets::default();
instructions.matcher().match_next_inst_while(
|_| true,
|inst| match inst {
BurnAsset(assets) => {
fee_assets = assets.clone();
Ok(ControlFlow::Break(()))
},

_ => Ok(ControlFlow::Continue(())),
},
)?;
let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };
trader
.buy_weight(max_weight, fee_assets.into(), &ctx)
.map_err(|_| ProcessMessageError::Unsupported)?;
Ok(())
}
}

pub type Barrier = TrailingSetTopicAsId<(
TakeWeightCredit,
// Expected responses are OK.
AllowKnownQueryResponses<PolkadotXcm>,
// Allow from BridgeHub
AllowExplicitUnpaidExecutionFrom<Equals<SiblingBridgeHub>>,
AllowUnpaidExecutionFromSnowBridgeWithFeeChecked<Equals<SiblingBridgeHub>>,
// Allow XCMs with some computed origins to pass through.
WithComputedOrigin<
(
Expand Down

0 comments on commit c18f771

Please sign in to comment.