Skip to content

Commit

Permalink
chore(xcm-executor): test that leftover fees are also trapped
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoaguirre committed Aug 23, 2024
1 parent 95747eb commit 1f82808
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
5 changes: 5 additions & 0 deletions polkadot/xcm/xcm-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,11 @@ impl<Config: config::Config> XcmExecutor<Config> {
self.process(xcm)
}

#[cfg(any(test, feature = "runtime-benchmarks"))]
pub fn bench_post_process(self, xcm_weight: Weight) -> Outcome {
self.post_process(xcm_weight)
}

fn process(&mut self, xcm: Xcm<Config::RuntimeCall>) -> Result<(), ExecutorError> {
tracing::trace!(
target: "xcm::process",
Expand Down
13 changes: 7 additions & 6 deletions polkadot/xcm/xcm-executor/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ use crate::{
pub fn instantiate_executor(
origin: impl Into<Location>,
message: Xcm<<XcmConfig as Config>::RuntimeCall>,
) -> XcmExecutor<XcmConfig> {
) -> (XcmExecutor<XcmConfig>, Weight) {
let mut vm =
XcmExecutor::<XcmConfig>::new(origin, message.using_encoded(sp_io::hashing::blake2_256));
vm.message_weight = XcmExecutor::<XcmConfig>::prepare(message.clone()).unwrap().weight_of();
vm
let weight = XcmExecutor::<XcmConfig>::prepare(message.clone()).unwrap().weight_of();
vm.message_weight = weight;
(vm, weight)
}

parameter_types! {
Expand Down Expand Up @@ -200,15 +201,15 @@ impl WeightTrader for TestTrader {
}

/// Account where all dropped assets are deposited.
pub const VOID_ACCOUNT: [u8; 32] = [255; 32];
pub const TRAPPED_ASSETS: [u8; 32] = [255; 32];

/// Test asset trap that moves all dropped assets to the `VOID_ACCOUNT` account.
/// Test asset trap that moves all dropped assets to the `TRAPPED_ASSETS` account.
pub struct TestAssetTrap;
impl DropAssets for TestAssetTrap {
fn drop_assets(_origin: &Location, assets: AssetsInHolding, _context: &XcmContext) -> Weight {
ASSETS.with(|a| {
a.borrow_mut()
.entry(VOID_ACCOUNT.into())
.entry(TRAPPED_ASSETS.into())
.or_insert(AssetsInHolding::new())
.subsume_assets(assets)
});
Expand Down
16 changes: 10 additions & 6 deletions polkadot/xcm/xcm-executor/src/tests/pay_fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn works_for_execution_fees() {
.deposit_asset(All, RECIPIENT.clone())
.build();

let mut vm = instantiate_executor(SENDER, xcm.clone());
let (mut vm, weight) = instantiate_executor(SENDER, xcm.clone());

// Program runs successfully.
assert!(vm.bench_process(xcm).is_ok());
Expand All @@ -57,6 +57,10 @@ fn works_for_execution_fees() {
// The recipient received all the assets in the holding register, so `100` that
// were withdrawn, minus the `10` that were destinated for fee payment.
assert_eq!(asset_list(RECIPIENT), [(Here, 90u128).into()]);

// Leftover fees get trapped.
assert!(vm.bench_post_process(weight).ensure_complete().is_ok());
assert_eq!(asset_list(TRAPPED_ASSETS), [(Here, 6u128).into()])
}

// This tests the new functionality provided by `PayFees`, being able to pay for
Expand All @@ -82,7 +86,7 @@ fn works_for_delivery_fees() {
.deposit_asset(All, RECIPIENT.clone())
.build();

let mut vm = instantiate_executor(SENDER.clone(), xcm.clone());
let (mut vm, _) = instantiate_executor(SENDER.clone(), xcm.clone());

// Program runs successfully.
assert!(vm.bench_process(xcm).is_ok());
Expand Down Expand Up @@ -135,7 +139,7 @@ fn buy_execution_works_as_before() {
.deposit_asset(All, RECIPIENT.clone())
.build();

let mut vm = instantiate_executor(SENDER, xcm.clone());
let (mut vm, _) = instantiate_executor(SENDER, xcm.clone());

// Program runs successfully.
assert!(vm.bench_process(xcm).is_ok());
Expand Down Expand Up @@ -165,7 +169,7 @@ fn fees_can_be_refunded() {
.deposit_asset(All, SENDER.clone())
.build();

let mut vm = instantiate_executor(SENDER.clone(), xcm.clone());
let (mut vm, _) = instantiate_executor(SENDER.clone(), xcm.clone());

// Program runs successfully.
assert!(vm.bench_process(xcm).is_ok());
Expand Down Expand Up @@ -197,7 +201,7 @@ fn putting_all_assets_in_pay_fees() {
.deposit_asset(All, RECIPIENT.clone())
.build();

let mut vm = instantiate_executor(SENDER.clone(), xcm.clone());
let (mut vm, _) = instantiate_executor(SENDER.clone(), xcm.clone());

// Program runs successfully.
assert!(vm.bench_process(xcm).is_ok());
Expand Down Expand Up @@ -234,7 +238,7 @@ fn refunding_too_early() {
.report_error(query_response_info)
.build();

let mut vm = instantiate_executor(SENDER.clone(), xcm.clone());
let (mut vm, _) = instantiate_executor(SENDER.clone(), xcm.clone());

// Program fails to run.
assert!(vm.bench_process(xcm).is_err());
Expand Down

0 comments on commit 1f82808

Please sign in to comment.