Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Companion for Polkadot#7563 #2956

Merged
merged 6 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions parachains/runtimes/assets/asset-hub-kusama/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ fn test_asset_xcm_trader() {
(asset_multilocation, asset_amount_needed + asset_amount_extra).into();

let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };

// Lets buy_weight and make sure buy_weight does not return an error
let unused_assets = trader.buy_weight(bought, asset.into()).expect("Expected Ok");
let unused_assets = trader.buy_weight(bought, asset.into(), &ctx).expect("Expected Ok");
// Check whether a correct amount of unused assets is returned
assert_ok!(
unused_assets.ensure_contains(&(asset_multilocation, asset_amount_extra).into())
Expand Down Expand Up @@ -163,6 +164,7 @@ fn test_asset_xcm_trader_with_refund() {
));

let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };

// Set Alice as block author, who will receive fees
RuntimeHelper::<Runtime>::run_to_block(2, Some(AccountId::from(ALICE)));
Expand All @@ -178,11 +180,11 @@ fn test_asset_xcm_trader_with_refund() {
let asset: MultiAsset = (asset_multilocation, amount_bought).into();

// Make sure buy_weight does not return an error
assert_ok!(trader.buy_weight(bought, asset.clone().into()));
assert_ok!(trader.buy_weight(bought, asset.clone().into(), &ctx));

// Make sure again buy_weight does return an error
// This assert relies on the fact, that we use `TakeFirstAssetTrader` in `WeightTrader` tuple chain, which cannot be called twice
assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::TooExpensive);
assert_noop!(trader.buy_weight(bought, asset.into(), &ctx), XcmError::TooExpensive);

// We actually use half of the weight
let weight_used = bought / 2;
Expand All @@ -191,7 +193,7 @@ fn test_asset_xcm_trader_with_refund() {
let amount_refunded = WeightToFee::weight_to_fee(&(bought - weight_used));

assert_eq!(
trader.refund_weight(bought - weight_used),
trader.refund_weight(bought - weight_used, &ctx),
Some((asset_multilocation, amount_refunded).into())
);

Expand Down Expand Up @@ -233,6 +235,7 @@ fn test_asset_xcm_trader_refund_not_possible_since_amount_less_than_ed() {
));

let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };

// Set Alice as block author, who will receive fees
RuntimeHelper::<Runtime>::run_to_block(2, Some(AccountId::from(ALICE)));
Expand All @@ -252,7 +255,7 @@ fn test_asset_xcm_trader_refund_not_possible_since_amount_less_than_ed() {
let asset: MultiAsset = (asset_multilocation, amount_bought).into();

// Buy weight should return an error
assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::TooExpensive);
assert_noop!(trader.buy_weight(bought, asset.into(), &ctx), XcmError::TooExpensive);

// not credited since the ED is higher than this value
assert_eq!(Assets::balance(1, AccountId::from(ALICE)), 0);
Expand Down Expand Up @@ -284,6 +287,7 @@ fn test_that_buying_ed_refund_does_not_refund() {
));

let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };

// Set Alice as block author, who will receive fees
RuntimeHelper::<Runtime>::run_to_block(2, Some(AccountId::from(ALICE)));
Expand All @@ -303,17 +307,17 @@ fn test_that_buying_ed_refund_does_not_refund() {
// We know we will have to buy at least ED, so lets make sure first it will
// fail with a payment of less than ED
let asset: MultiAsset = (asset_multilocation, amount_bought).into();
assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::TooExpensive);
assert_noop!(trader.buy_weight(bought, asset.into(), &ctx), XcmError::TooExpensive);

// Now lets buy ED at least
let asset: MultiAsset = (asset_multilocation, ExistentialDeposit::get()).into();

// Buy weight should work
assert_ok!(trader.buy_weight(bought, asset.into()));
assert_ok!(trader.buy_weight(bought, asset.into(), &ctx));

// Should return None. We have a specific check making sure we dont go below ED for
// drop payment
assert_eq!(trader.refund_weight(bought), None);
assert_eq!(trader.refund_weight(bought, &ctx), None);

// Drop trader
drop(trader);
Expand Down Expand Up @@ -356,6 +360,7 @@ fn test_asset_xcm_trader_not_possible_for_non_sufficient_assets() {
));

let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };

// Set Alice as block author, who will receive fees
RuntimeHelper::<Runtime>::run_to_block(2, Some(AccountId::from(ALICE)));
Expand All @@ -371,7 +376,7 @@ fn test_asset_xcm_trader_not_possible_for_non_sufficient_assets() {
let asset: MultiAsset = (asset_multilocation, asset_amount_needed).into();

// Make sure again buy_weight does return an error
assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::TooExpensive);
assert_noop!(trader.buy_weight(bought, asset.into(), &ctx), XcmError::TooExpensive);

// Drop trader
drop(trader);
Expand Down
23 changes: 14 additions & 9 deletions parachains/runtimes/assets/asset-hub-polkadot/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ fn test_asset_xcm_trader() {
(asset_multilocation, asset_amount_needed + asset_amount_extra).into();

let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };

// Lets buy_weight and make sure buy_weight does not return an error
let unused_assets = trader.buy_weight(bought, asset.into()).expect("Expected Ok");
let unused_assets = trader.buy_weight(bought, asset.into(), &ctx).expect("Expected Ok");
// Check whether a correct amount of unused assets is returned
assert_ok!(
unused_assets.ensure_contains(&(asset_multilocation, asset_amount_extra).into())
Expand Down Expand Up @@ -167,6 +168,7 @@ fn test_asset_xcm_trader_with_refund() {
));

let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };

// Set Alice as block author, who will receive fees
RuntimeHelper::<Runtime>::run_to_block(2, Some(AccountId::from(ALICE)));
Expand All @@ -185,11 +187,11 @@ fn test_asset_xcm_trader_with_refund() {
let asset: MultiAsset = (asset_multilocation, amount_bought).into();

// Make sure buy_weight does not return an error
assert_ok!(trader.buy_weight(bought, asset.clone().into()));
assert_ok!(trader.buy_weight(bought, asset.clone().into(), &ctx));

// Make sure again buy_weight does return an error
// This assert relies on the fact, that we use `TakeFirstAssetTrader` in `WeightTrader` tuple chain, which cannot be called twice
assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::TooExpensive);
assert_noop!(trader.buy_weight(bought, asset.into(), &ctx), XcmError::TooExpensive);

// We actually use half of the weight
let weight_used = bought / 2;
Expand All @@ -198,7 +200,7 @@ fn test_asset_xcm_trader_with_refund() {
let amount_refunded = WeightToFee::weight_to_fee(&(bought - weight_used));

assert_eq!(
trader.refund_weight(bought - weight_used),
trader.refund_weight(bought - weight_used, &ctx),
Some((asset_multilocation, amount_refunded).into())
);

Expand Down Expand Up @@ -240,6 +242,7 @@ fn test_asset_xcm_trader_refund_not_possible_since_amount_less_than_ed() {
));

let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };

// Set Alice as block author, who will receive fees
RuntimeHelper::<Runtime>::run_to_block(2, Some(AccountId::from(ALICE)));
Expand All @@ -262,7 +265,7 @@ fn test_asset_xcm_trader_refund_not_possible_since_amount_less_than_ed() {
let asset: MultiAsset = (asset_multilocation, amount_bought).into();

// Buy weight should return an error
assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::TooExpensive);
assert_noop!(trader.buy_weight(bought, asset.into(), &ctx), XcmError::TooExpensive);

// not credited since the ED is higher than this value
assert_eq!(Assets::balance(1, AccountId::from(ALICE)), 0);
Expand Down Expand Up @@ -294,6 +297,7 @@ fn test_that_buying_ed_refund_does_not_refund() {
));

let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };

// Set Alice as block author, who will receive fees
RuntimeHelper::<Runtime>::run_to_block(2, Some(AccountId::from(ALICE)));
Expand All @@ -313,17 +317,17 @@ fn test_that_buying_ed_refund_does_not_refund() {
// We know we will have to buy at least ED, so lets make sure first it will
// fail with a payment of less than ED
let asset: MultiAsset = (asset_multilocation, amount_bought).into();
assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::TooExpensive);
assert_noop!(trader.buy_weight(bought, asset.into(), &ctx), XcmError::TooExpensive);

// Now lets buy ED at least
let asset: MultiAsset = (asset_multilocation, ExistentialDeposit::get()).into();

// Buy weight should work
assert_ok!(trader.buy_weight(bought, asset.into()));
assert_ok!(trader.buy_weight(bought, asset.into(), &ctx));

// Should return None. We have a specific check making sure we dont go below ED for
// drop payment
assert_eq!(trader.refund_weight(bought), None);
assert_eq!(trader.refund_weight(bought, &ctx), None);

// Drop trader
drop(trader);
Expand Down Expand Up @@ -366,6 +370,7 @@ fn test_asset_xcm_trader_not_possible_for_non_sufficient_assets() {
));

let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };

// Set Alice as block author, who will receive fees
RuntimeHelper::<Runtime>::run_to_block(2, Some(AccountId::from(ALICE)));
Expand All @@ -384,7 +389,7 @@ fn test_asset_xcm_trader_not_possible_for_non_sufficient_assets() {
let asset: MultiAsset = (asset_multilocation, asset_amount_needed).into();

// Make sure again buy_weight does return an error
assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::TooExpensive);
assert_noop!(trader.buy_weight(bought, asset.into(), &ctx), XcmError::TooExpensive);

// Drop trader
drop(trader);
Expand Down
23 changes: 14 additions & 9 deletions parachains/runtimes/assets/asset-hub-westend/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ fn test_asset_xcm_trader() {
(asset_multilocation, asset_amount_needed + asset_amount_extra).into();

let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };

// Lets buy_weight and make sure buy_weight does not return an error
let unused_assets = trader.buy_weight(bought, asset.into()).expect("Expected Ok");
let unused_assets = trader.buy_weight(bought, asset.into(), &ctx).expect("Expected Ok");
// Check whether a correct amount of unused assets is returned
assert_ok!(
unused_assets.ensure_contains(&(asset_multilocation, asset_amount_extra).into())
Expand Down Expand Up @@ -174,6 +175,7 @@ fn test_asset_xcm_trader_with_refund() {
));

let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };

// Set Alice as block author, who will receive fees
RuntimeHelper::<Runtime>::run_to_block(2, Some(AccountId::from(ALICE)));
Expand All @@ -188,11 +190,11 @@ fn test_asset_xcm_trader_with_refund() {
let asset: MultiAsset = (asset_multilocation, amount_bought).into();

// Make sure buy_weight does not return an error
assert_ok!(trader.buy_weight(bought, asset.clone().into()));
assert_ok!(trader.buy_weight(bought, asset.clone().into(), &ctx));

// Make sure again buy_weight does return an error
// This assert relies on the fact, that we use `TakeFirstAssetTrader` in `WeightTrader` tuple chain, which cannot be called twice
assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::TooExpensive);
assert_noop!(trader.buy_weight(bought, asset.into(), &ctx), XcmError::TooExpensive);

// We actually use half of the weight
let weight_used = bought / 2;
Expand All @@ -201,7 +203,7 @@ fn test_asset_xcm_trader_with_refund() {
let amount_refunded = WeightToFee::weight_to_fee(&(bought - weight_used));

assert_eq!(
trader.refund_weight(bought - weight_used),
trader.refund_weight(bought - weight_used, &ctx),
Some((asset_multilocation, amount_refunded).into())
);

Expand Down Expand Up @@ -243,6 +245,7 @@ fn test_asset_xcm_trader_refund_not_possible_since_amount_less_than_ed() {
));

let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };

// Set Alice as block author, who will receive fees
RuntimeHelper::<Runtime>::run_to_block(2, Some(AccountId::from(ALICE)));
Expand All @@ -262,7 +265,7 @@ fn test_asset_xcm_trader_refund_not_possible_since_amount_less_than_ed() {
let asset: MultiAsset = (asset_multilocation, amount_bought).into();

// Buy weight should return an error
assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::TooExpensive);
assert_noop!(trader.buy_weight(bought, asset.into(), &ctx), XcmError::TooExpensive);

// not credited since the ED is higher than this value
assert_eq!(Assets::balance(1, AccountId::from(ALICE)), 0);
Expand Down Expand Up @@ -294,6 +297,7 @@ fn test_that_buying_ed_refund_does_not_refund() {
));

let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };

// Set Alice as block author, who will receive fees
RuntimeHelper::<Runtime>::run_to_block(2, Some(AccountId::from(ALICE)));
Expand All @@ -312,17 +316,17 @@ fn test_that_buying_ed_refund_does_not_refund() {
// We know we will have to buy at least ED, so lets make sure first it will
// fail with a payment of less than ED
let asset: MultiAsset = (asset_multilocation, amount_bought).into();
assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::TooExpensive);
assert_noop!(trader.buy_weight(bought, asset.into(), &ctx), XcmError::TooExpensive);

// Now lets buy ED at least
let asset: MultiAsset = (asset_multilocation, ExistentialDeposit::get()).into();

// Buy weight should work
assert_ok!(trader.buy_weight(bought, asset.into()));
assert_ok!(trader.buy_weight(bought, asset.into(), &ctx));

// Should return None. We have a specific check making sure we dont go below ED for
// drop payment
assert_eq!(trader.refund_weight(bought), None);
assert_eq!(trader.refund_weight(bought, &ctx), None);

// Drop trader
drop(trader);
Expand Down Expand Up @@ -365,6 +369,7 @@ fn test_asset_xcm_trader_not_possible_for_non_sufficient_assets() {
));

let mut trader = <XcmConfig as xcm_executor::Config>::Trader::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };

// Set Alice as block author, who will receive fees
RuntimeHelper::<Runtime>::run_to_block(2, Some(AccountId::from(ALICE)));
Expand All @@ -380,7 +385,7 @@ fn test_asset_xcm_trader_not_possible_for_non_sufficient_assets() {
let asset: MultiAsset = (asset_multilocation, asset_amount_needed).into();

// Make sure again buy_weight does return an error
assert_noop!(trader.buy_weight(bought, asset.into()), XcmError::TooExpensive);
assert_noop!(trader.buy_weight(bought, asset.into(), &ctx), XcmError::TooExpensive);

// Drop trader
drop(trader);
Expand Down
12 changes: 7 additions & 5 deletions primitives/utility/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ impl<
&mut self,
weight: Weight,
payment: xcm_executor::Assets,
context: &XcmContext,
) -> Result<xcm_executor::Assets, XcmError> {
log::trace!(target: "xcm::weight", "TakeFirstAssetTrader::buy_weight weight: {:?}, payment: {:?}", weight, payment);
log::trace!(target: "xcm::weight", "TakeFirstAssetTrader::buy_weight weight: {:?}, payment: {:?}, context: {:?}", weight, payment, context);

// Make sure we dont enter twice
if self.0.is_some() {
Expand Down Expand Up @@ -196,8 +197,8 @@ impl<
Ok(unused)
}

fn refund_weight(&mut self, weight: Weight) -> Option<MultiAsset> {
log::trace!(target: "xcm::weight", "TakeFirstAssetTrader::refund_weight weight: {:?}", weight);
fn refund_weight(&mut self, weight: Weight, context: &XcmContext) -> Option<MultiAsset> {
log::trace!(target: "xcm::weight", "TakeFirstAssetTrader::refund_weight weight: {:?}, context: {:?}", weight, context);
if let Some(AssetTraderRefunder {
mut weight_outstanding,
outstanding_concrete_asset: MultiAsset { id, fun },
Expand Down Expand Up @@ -526,16 +527,17 @@ mod tests {
FeeChargerAssetsHandleRefund,
>;
let mut trader = <Trader as WeightTrader>::new();
let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None };

// prepare test data
let asset: MultiAsset = (Here, AMOUNT).into();
let payment = Assets::from(asset);
let weight_to_buy = Weight::from_parts(1_000, 1_000);

// lets do first call (success)
assert_ok!(trader.buy_weight(weight_to_buy, payment.clone()));
assert_ok!(trader.buy_weight(weight_to_buy, payment.clone(), &ctx));

// lets do second call (error)
assert_eq!(trader.buy_weight(weight_to_buy, payment), Err(XcmError::NotWithdrawable));
assert_eq!(trader.buy_weight(weight_to_buy, payment, &ctx), Err(XcmError::NotWithdrawable));
}
}