From 74907638f3720617c0a533cdf97c15acaea32cc0 Mon Sep 17 00:00:00 2001 From: Patricio Napoli Date: Wed, 5 Jul 2023 15:53:07 -0300 Subject: [PATCH 1/6] added XcmContext to WeightTrader trait --- runtime/test-runtime/src/xcm_config.rs | 2 +- xcm/xcm-builder/src/tests/weight.rs | 14 ++++++++++---- xcm/xcm-builder/src/weight.rs | 8 ++++---- xcm/xcm-executor/src/lib.rs | 4 ++-- xcm/xcm-executor/src/traits/weight.rs | 12 ++++++------ 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/runtime/test-runtime/src/xcm_config.rs b/runtime/test-runtime/src/xcm_config.rs index 45e7956d45ba..d7cc3eae5cf5 100644 --- a/runtime/test-runtime/src/xcm_config.rs +++ b/runtime/test-runtime/src/xcm_config.rs @@ -80,7 +80,7 @@ impl WeightTrader for DummyWeightTrader { DummyWeightTrader } - fn buy_weight(&mut self, _weight: Weight, _payment: Assets) -> Result { + fn buy_weight(&mut self, _ctx: &XcmContext, _weight: Weight, _payment: Assets) -> Result { Ok(Assets::default()) } } diff --git a/xcm/xcm-builder/src/tests/weight.rs b/xcm/xcm-builder/src/tests/weight.rs index 99ef029196ff..f8d3241b2239 100644 --- a/xcm/xcm-builder/src/tests/weight.rs +++ b/xcm/xcm-builder/src/tests/weight.rs @@ -24,25 +24,31 @@ fn fixed_rate_of_fungible_should_work() { } let mut trader = FixedRateOfFungible::::new(); + let ctx = XcmContext { + origin: None, + message_id: XcmHash::default(), + topic: None, + }; + // supplies 100 unit of asset, 80 still remains after purchasing weight assert_eq!( trader - .buy_weight(Weight::from_parts(10, 10), fungible_multi_asset(Here.into(), 100).into()), + .buy_weight(&ctx, Weight::from_parts(10, 10), fungible_multi_asset(Here.into(), 100).into()), Ok(fungible_multi_asset(Here.into(), 80).into()), ); // should have nothing left, as 5 + 5 = 10, and we supplied 10 units of asset. assert_eq!( - trader.buy_weight(Weight::from_parts(5, 5), fungible_multi_asset(Here.into(), 10).into()), + trader.buy_weight(&ctx, Weight::from_parts(5, 5), fungible_multi_asset(Here.into(), 10).into()), Ok(vec![].into()), ); // should have 5 left, as there are no proof size components assert_eq!( - trader.buy_weight(Weight::from_parts(5, 0), fungible_multi_asset(Here.into(), 10).into()), + trader.buy_weight(&ctx, Weight::from_parts(5, 0), fungible_multi_asset(Here.into(), 10).into()), Ok(fungible_multi_asset(Here.into(), 5).into()), ); // not enough to purchase the combined weights assert_err!( - trader.buy_weight(Weight::from_parts(5, 5), fungible_multi_asset(Here.into(), 5).into()), + trader.buy_weight(&ctx, Weight::from_parts(5, 5), fungible_multi_asset(Here.into(), 5).into()), XcmError::TooExpensive, ); } diff --git a/xcm/xcm-builder/src/weight.rs b/xcm/xcm-builder/src/weight.rs index 1473775eccd8..96183ba5af55 100644 --- a/xcm/xcm-builder/src/weight.rs +++ b/xcm/xcm-builder/src/weight.rs @@ -140,7 +140,7 @@ impl, R: TakeRevenue> WeightTrader for FixedRateOf Self(Weight::zero(), 0, PhantomData) } - fn buy_weight(&mut self, weight: Weight, payment: Assets) -> Result { + fn buy_weight(&mut self, _ctx: &XcmContext, weight: Weight, payment: Assets) -> Result { log::trace!( target: "xcm::weight", "FixedRateOfFungible::buy_weight weight: {:?}, payment: {:?}", @@ -160,7 +160,7 @@ impl, R: TakeRevenue> WeightTrader for FixedRateOf Ok(unused) } - fn refund_weight(&mut self, weight: Weight) -> Option { + fn refund_weight(&mut self, _ctx: &XcmContext, weight: Weight) -> Option { log::trace!(target: "xcm::weight", "FixedRateOfFungible::refund_weight weight: {:?}", weight); let (id, units_per_second, units_per_mb) = T::get(); let weight = weight.min(self.0); @@ -210,7 +210,7 @@ impl< Self(Weight::zero(), Zero::zero(), PhantomData) } - fn buy_weight(&mut self, weight: Weight, payment: Assets) -> Result { + fn buy_weight(&mut self, _ctx: &XcmContext, weight: Weight, payment: Assets) -> Result { log::trace!(target: "xcm::weight", "UsingComponents::buy_weight weight: {:?}, payment: {:?}", weight, payment); let amount = WeightToFee::weight_to_fee(&weight); let u128_amount: u128 = amount.try_into().map_err(|_| XcmError::Overflow)?; @@ -221,7 +221,7 @@ impl< Ok(unused) } - fn refund_weight(&mut self, weight: Weight) -> Option { + fn refund_weight(&mut self, _ctx: &XcmContext, weight: Weight) -> Option { log::trace!(target: "xcm::weight", "UsingComponents::refund_weight weight: {:?}", weight); let weight = weight.min(self.0); let amount = WeightToFee::weight_to_fee(&weight); diff --git a/xcm/xcm-executor/src/lib.rs b/xcm/xcm-executor/src/lib.rs index 050d73837085..a9002422512d 100644 --- a/xcm/xcm-executor/src/lib.rs +++ b/xcm/xcm-executor/src/lib.rs @@ -457,7 +457,7 @@ impl XcmExecutor { let current_surplus = self.total_surplus.saturating_sub(self.total_refunded); if current_surplus.any_gt(Weight::zero()) { self.total_refunded.saturating_accrue(current_surplus); - if let Some(w) = self.trader.refund_weight(current_surplus) { + if let Some(w) = self.trader.refund_weight(&self.context, current_surplus) { self.subsume_asset(w)?; } } @@ -689,7 +689,7 @@ impl XcmExecutor { // pay for `weight` using up to `fees` of the holding register. let max_fee = self.holding.try_take(fees.into()).map_err(|_| XcmError::NotHoldingFees)?; - let unspent = self.trader.buy_weight(weight, max_fee)?; + let unspent = self.trader.buy_weight(&self.context, weight, max_fee)?; self.subsume_assets(unspent)?; } Ok(()) diff --git a/xcm/xcm-executor/src/traits/weight.rs b/xcm/xcm-executor/src/traits/weight.rs index 033614d4bf61..4f5bf7d2e99d 100644 --- a/xcm/xcm-executor/src/traits/weight.rs +++ b/xcm/xcm-executor/src/traits/weight.rs @@ -49,13 +49,13 @@ pub trait WeightTrader: Sized { /// Purchase execution weight credit in return for up to a given `payment`. If less of the /// payment is required then the surplus is returned. If the `payment` cannot be used to pay /// for the `weight`, then an error is returned. - fn buy_weight(&mut self, weight: Weight, payment: Assets) -> Result; + fn buy_weight(&mut self, ctx: &XcmContext, weight: Weight, payment: Assets) -> Result; /// Attempt a refund of `weight` into some asset. The caller does not guarantee that the weight was /// purchased using `buy_weight`. /// /// Default implementation refunds nothing. - fn refund_weight(&mut self, _weight: Weight) -> Option { + fn refund_weight(&mut self, _ctx: &XcmContext, _weight: Weight) -> Option { None } } @@ -66,11 +66,11 @@ impl WeightTrader for Tuple { for_tuples!( ( #( Tuple::new() ),* ) ) } - fn buy_weight(&mut self, weight: Weight, payment: Assets) -> Result { + fn buy_weight(&mut self, ctx: &XcmContext, weight: Weight, payment: Assets) -> Result { let mut too_expensive_error_found = false; let mut last_error = None; for_tuples!( #( - match Tuple.buy_weight(weight, payment.clone()) { + match Tuple.buy_weight(ctx, weight, payment.clone()) { Ok(assets) => return Ok(assets), Err(e) => { if let XcmError::TooExpensive = e { @@ -92,9 +92,9 @@ impl WeightTrader for Tuple { }) } - fn refund_weight(&mut self, weight: Weight) -> Option { + fn refund_weight(&mut self, ctx: &XcmContext, weight: Weight) -> Option { for_tuples!( #( - if let Some(asset) = Tuple.refund_weight(weight) { + if let Some(asset) = Tuple.refund_weight(ctx, weight) { return Some(asset); } )* ); From 63a10d1a9c99074b2328d57c0cce60c5cdb4be35 Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Sun, 30 Jul 2023 08:41:44 +0100 Subject: [PATCH 2/6] cargo fmt --- runtime/test-runtime/src/xcm_config.rs | 7 +++++- xcm/xcm-builder/src/tests/weight.rs | 31 +++++++++++++++++--------- xcm/xcm-builder/src/weight.rs | 14 ++++++++++-- xcm/xcm-executor/src/traits/weight.rs | 14 ++++++++++-- 4 files changed, 51 insertions(+), 15 deletions(-) diff --git a/runtime/test-runtime/src/xcm_config.rs b/runtime/test-runtime/src/xcm_config.rs index d7cc3eae5cf5..acbd10a87cf9 100644 --- a/runtime/test-runtime/src/xcm_config.rs +++ b/runtime/test-runtime/src/xcm_config.rs @@ -80,7 +80,12 @@ impl WeightTrader for DummyWeightTrader { DummyWeightTrader } - fn buy_weight(&mut self, _ctx: &XcmContext, _weight: Weight, _payment: Assets) -> Result { + fn buy_weight( + &mut self, + _ctx: &XcmContext, + _weight: Weight, + _payment: Assets, + ) -> Result { Ok(Assets::default()) } } diff --git a/xcm/xcm-builder/src/tests/weight.rs b/xcm/xcm-builder/src/tests/weight.rs index f8d3241b2239..6e9ee1ae04c3 100644 --- a/xcm/xcm-builder/src/tests/weight.rs +++ b/xcm/xcm-builder/src/tests/weight.rs @@ -24,31 +24,42 @@ fn fixed_rate_of_fungible_should_work() { } let mut trader = FixedRateOfFungible::::new(); - let ctx = XcmContext { - origin: None, - message_id: XcmHash::default(), - topic: None, - }; + let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None }; // supplies 100 unit of asset, 80 still remains after purchasing weight assert_eq!( - trader - .buy_weight(&ctx, Weight::from_parts(10, 10), fungible_multi_asset(Here.into(), 100).into()), + trader.buy_weight( + &ctx, + Weight::from_parts(10, 10), + fungible_multi_asset(Here.into(), 100).into() + ), Ok(fungible_multi_asset(Here.into(), 80).into()), ); // should have nothing left, as 5 + 5 = 10, and we supplied 10 units of asset. assert_eq!( - trader.buy_weight(&ctx, Weight::from_parts(5, 5), fungible_multi_asset(Here.into(), 10).into()), + trader.buy_weight( + &ctx, + Weight::from_parts(5, 5), + fungible_multi_asset(Here.into(), 10).into() + ), Ok(vec![].into()), ); // should have 5 left, as there are no proof size components assert_eq!( - trader.buy_weight(&ctx, Weight::from_parts(5, 0), fungible_multi_asset(Here.into(), 10).into()), + trader.buy_weight( + &ctx, + Weight::from_parts(5, 0), + fungible_multi_asset(Here.into(), 10).into() + ), Ok(fungible_multi_asset(Here.into(), 5).into()), ); // not enough to purchase the combined weights assert_err!( - trader.buy_weight(&ctx, Weight::from_parts(5, 5), fungible_multi_asset(Here.into(), 5).into()), + trader.buy_weight( + &ctx, + Weight::from_parts(5, 5), + fungible_multi_asset(Here.into(), 5).into() + ), XcmError::TooExpensive, ); } diff --git a/xcm/xcm-builder/src/weight.rs b/xcm/xcm-builder/src/weight.rs index 96183ba5af55..989f94321531 100644 --- a/xcm/xcm-builder/src/weight.rs +++ b/xcm/xcm-builder/src/weight.rs @@ -140,7 +140,12 @@ impl, R: TakeRevenue> WeightTrader for FixedRateOf Self(Weight::zero(), 0, PhantomData) } - fn buy_weight(&mut self, _ctx: &XcmContext, weight: Weight, payment: Assets) -> Result { + fn buy_weight( + &mut self, + _ctx: &XcmContext, + weight: Weight, + payment: Assets, + ) -> Result { log::trace!( target: "xcm::weight", "FixedRateOfFungible::buy_weight weight: {:?}, payment: {:?}", @@ -210,7 +215,12 @@ impl< Self(Weight::zero(), Zero::zero(), PhantomData) } - fn buy_weight(&mut self, _ctx: &XcmContext, weight: Weight, payment: Assets) -> Result { + fn buy_weight( + &mut self, + _ctx: &XcmContext, + weight: Weight, + payment: Assets, + ) -> Result { log::trace!(target: "xcm::weight", "UsingComponents::buy_weight weight: {:?}, payment: {:?}", weight, payment); let amount = WeightToFee::weight_to_fee(&weight); let u128_amount: u128 = amount.try_into().map_err(|_| XcmError::Overflow)?; diff --git a/xcm/xcm-executor/src/traits/weight.rs b/xcm/xcm-executor/src/traits/weight.rs index 4f5bf7d2e99d..758cfea4e1cf 100644 --- a/xcm/xcm-executor/src/traits/weight.rs +++ b/xcm/xcm-executor/src/traits/weight.rs @@ -49,7 +49,12 @@ pub trait WeightTrader: Sized { /// Purchase execution weight credit in return for up to a given `payment`. If less of the /// payment is required then the surplus is returned. If the `payment` cannot be used to pay /// for the `weight`, then an error is returned. - fn buy_weight(&mut self, ctx: &XcmContext, weight: Weight, payment: Assets) -> Result; + fn buy_weight( + &mut self, + ctx: &XcmContext, + weight: Weight, + payment: Assets, + ) -> Result; /// Attempt a refund of `weight` into some asset. The caller does not guarantee that the weight was /// purchased using `buy_weight`. @@ -66,7 +71,12 @@ impl WeightTrader for Tuple { for_tuples!( ( #( Tuple::new() ),* ) ) } - fn buy_weight(&mut self, ctx: &XcmContext, weight: Weight, payment: Assets) -> Result { + fn buy_weight( + &mut self, + ctx: &XcmContext, + weight: Weight, + payment: Assets, + ) -> Result { let mut too_expensive_error_found = false; let mut last_error = None; for_tuples!( #( From 14451363727016e2a5fb9a9e3e152b5d8ae166e8 Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Sun, 30 Jul 2023 09:46:19 +0100 Subject: [PATCH 3/6] make xcm context optional --- xcm/xcm-builder/src/weight.rs | 8 ++++---- xcm/xcm-executor/src/lib.rs | 4 ++-- xcm/xcm-executor/src/traits/weight.rs | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/xcm/xcm-builder/src/weight.rs b/xcm/xcm-builder/src/weight.rs index 989f94321531..de7fb6daafa9 100644 --- a/xcm/xcm-builder/src/weight.rs +++ b/xcm/xcm-builder/src/weight.rs @@ -142,7 +142,7 @@ impl, R: TakeRevenue> WeightTrader for FixedRateOf fn buy_weight( &mut self, - _ctx: &XcmContext, + _ctx: Option<&XcmContext>, weight: Weight, payment: Assets, ) -> Result { @@ -165,7 +165,7 @@ impl, R: TakeRevenue> WeightTrader for FixedRateOf Ok(unused) } - fn refund_weight(&mut self, _ctx: &XcmContext, weight: Weight) -> Option { + fn refund_weight(&mut self, _ctx: Option<&XcmContext>, weight: Weight) -> Option { log::trace!(target: "xcm::weight", "FixedRateOfFungible::refund_weight weight: {:?}", weight); let (id, units_per_second, units_per_mb) = T::get(); let weight = weight.min(self.0); @@ -217,7 +217,7 @@ impl< fn buy_weight( &mut self, - _ctx: &XcmContext, + _ctx: Option<&XcmContext>, weight: Weight, payment: Assets, ) -> Result { @@ -231,7 +231,7 @@ impl< Ok(unused) } - fn refund_weight(&mut self, _ctx: &XcmContext, weight: Weight) -> Option { + fn refund_weight(&mut self, _ctx: Option<&XcmContext>, weight: Weight) -> Option { log::trace!(target: "xcm::weight", "UsingComponents::refund_weight weight: {:?}", weight); let weight = weight.min(self.0); let amount = WeightToFee::weight_to_fee(&weight); diff --git a/xcm/xcm-executor/src/lib.rs b/xcm/xcm-executor/src/lib.rs index a9002422512d..24f38cc00e0c 100644 --- a/xcm/xcm-executor/src/lib.rs +++ b/xcm/xcm-executor/src/lib.rs @@ -457,7 +457,7 @@ impl XcmExecutor { let current_surplus = self.total_surplus.saturating_sub(self.total_refunded); if current_surplus.any_gt(Weight::zero()) { self.total_refunded.saturating_accrue(current_surplus); - if let Some(w) = self.trader.refund_weight(&self.context, current_surplus) { + if let Some(w) = self.trader.refund_weight(Some(&self.context), current_surplus) { self.subsume_asset(w)?; } } @@ -689,7 +689,7 @@ impl XcmExecutor { // pay for `weight` using up to `fees` of the holding register. let max_fee = self.holding.try_take(fees.into()).map_err(|_| XcmError::NotHoldingFees)?; - let unspent = self.trader.buy_weight(&self.context, weight, max_fee)?; + let unspent = self.trader.buy_weight(Some(&self.context), weight, max_fee)?; self.subsume_assets(unspent)?; } Ok(()) diff --git a/xcm/xcm-executor/src/traits/weight.rs b/xcm/xcm-executor/src/traits/weight.rs index 758cfea4e1cf..273d728db560 100644 --- a/xcm/xcm-executor/src/traits/weight.rs +++ b/xcm/xcm-executor/src/traits/weight.rs @@ -51,7 +51,7 @@ pub trait WeightTrader: Sized { /// for the `weight`, then an error is returned. fn buy_weight( &mut self, - ctx: &XcmContext, + ctx: Option<&XcmContext>, weight: Weight, payment: Assets, ) -> Result; @@ -60,7 +60,7 @@ pub trait WeightTrader: Sized { /// purchased using `buy_weight`. /// /// Default implementation refunds nothing. - fn refund_weight(&mut self, _ctx: &XcmContext, _weight: Weight) -> Option { + fn refund_weight(&mut self, _ctx: Option<&XcmContext>, _weight: Weight) -> Option { None } } @@ -73,7 +73,7 @@ impl WeightTrader for Tuple { fn buy_weight( &mut self, - ctx: &XcmContext, + ctx: Option<&XcmContext>, weight: Weight, payment: Assets, ) -> Result { @@ -102,7 +102,7 @@ impl WeightTrader for Tuple { }) } - fn refund_weight(&mut self, ctx: &XcmContext, weight: Weight) -> Option { + fn refund_weight(&mut self, ctx: Option<&XcmContext>, weight: Weight) -> Option { for_tuples!( #( if let Some(asset) = Tuple.refund_weight(ctx, weight) { return Some(asset); From ba030dfd1d243a3d4fdd04cb0f9e31330756813d Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Mon, 31 Jul 2023 11:46:59 +0100 Subject: [PATCH 4/6] make compile --- runtime/test-runtime/src/xcm_config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/test-runtime/src/xcm_config.rs b/runtime/test-runtime/src/xcm_config.rs index acbd10a87cf9..c6891c349d64 100644 --- a/runtime/test-runtime/src/xcm_config.rs +++ b/runtime/test-runtime/src/xcm_config.rs @@ -82,7 +82,7 @@ impl WeightTrader for DummyWeightTrader { fn buy_weight( &mut self, - _ctx: &XcmContext, + _ctx: Option<&XcmContext>, _weight: Weight, _payment: Assets, ) -> Result { From 138fb931c30698b67ed01cbbf56887540f92c7a5 Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Mon, 31 Jul 2023 13:38:27 +0100 Subject: [PATCH 5/6] fix compile --- xcm/xcm-builder/src/tests/weight.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/xcm/xcm-builder/src/tests/weight.rs b/xcm/xcm-builder/src/tests/weight.rs index 6e9ee1ae04c3..ebbb79f1ec00 100644 --- a/xcm/xcm-builder/src/tests/weight.rs +++ b/xcm/xcm-builder/src/tests/weight.rs @@ -29,7 +29,7 @@ fn fixed_rate_of_fungible_should_work() { // supplies 100 unit of asset, 80 still remains after purchasing weight assert_eq!( trader.buy_weight( - &ctx, + Some(&ctx), Weight::from_parts(10, 10), fungible_multi_asset(Here.into(), 100).into() ), @@ -38,7 +38,7 @@ fn fixed_rate_of_fungible_should_work() { // should have nothing left, as 5 + 5 = 10, and we supplied 10 units of asset. assert_eq!( trader.buy_weight( - &ctx, + Some(&ctx), Weight::from_parts(5, 5), fungible_multi_asset(Here.into(), 10).into() ), @@ -47,7 +47,7 @@ fn fixed_rate_of_fungible_should_work() { // should have 5 left, as there are no proof size components assert_eq!( trader.buy_weight( - &ctx, + Some(&ctx), Weight::from_parts(5, 0), fungible_multi_asset(Here.into(), 10).into() ), @@ -56,7 +56,7 @@ fn fixed_rate_of_fungible_should_work() { // not enough to purchase the combined weights assert_err!( trader.buy_weight( - &ctx, + Some(&ctx), Weight::from_parts(5, 5), fungible_multi_asset(Here.into(), 5).into() ), @@ -168,33 +168,33 @@ fn weight_trader_tuple_should_work() { let mut traders = Traders::new(); // trader one buys weight assert_eq!( - traders.buy_weight(Weight::from_parts(5, 5), fungible_multi_asset(Here.into(), 10).into()), + traders.buy_weight(None, Weight::from_parts(5, 5), fungible_multi_asset(Here.into(), 10).into()), Ok(vec![].into()), ); // trader one refunds assert_eq!( - traders.refund_weight(Weight::from_parts(2, 2)), + traders.refund_weight(None, Weight::from_parts(2, 2)), Some(fungible_multi_asset(Here.into(), 4)) ); let mut traders = Traders::new(); // trader one failed; trader two buys weight assert_eq!( - traders.buy_weight(Weight::from_parts(5, 5), fungible_multi_asset(para_1, 10).into()), + traders.buy_weight(None, Weight::from_parts(5, 5), fungible_multi_asset(para_1, 10).into()), Ok(vec![].into()), ); // trader two refunds assert_eq!( - traders.refund_weight(Weight::from_parts(2, 2)), + traders.refund_weight(None, Weight::from_parts(2, 2)), Some(fungible_multi_asset(para_1, 4)) ); let mut traders = Traders::new(); // all traders fails assert_err!( - traders.buy_weight(Weight::from_parts(5, 5), fungible_multi_asset(para_2, 10).into()), + traders.buy_weight(None, Weight::from_parts(5, 5), fungible_multi_asset(para_2, 10).into()), XcmError::TooExpensive, ); // and no refund - assert_eq!(traders.refund_weight(Weight::from_parts(2, 2)), None); + assert_eq!(traders.refund_weight(None, Weight::from_parts(2, 2)), None); } From 6d8532b2f758f9a559c40f22d70dbdd8ac6c4c00 Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Mon, 31 Jul 2023 14:02:39 +0200 Subject: [PATCH 6/6] `XcmContext` to `buy_weight / refund_weight` --- runtime/test-runtime/src/xcm_config.rs | 2 +- xcm/xcm-builder/src/tests/weight.rs | 34 +++++++++++++++----------- xcm/xcm-builder/src/weight.rs | 18 +++++++------- xcm/xcm-executor/src/lib.rs | 4 +-- xcm/xcm-executor/src/traits/weight.rs | 12 ++++----- 5 files changed, 38 insertions(+), 32 deletions(-) diff --git a/runtime/test-runtime/src/xcm_config.rs b/runtime/test-runtime/src/xcm_config.rs index c6891c349d64..21ce8c877dc3 100644 --- a/runtime/test-runtime/src/xcm_config.rs +++ b/runtime/test-runtime/src/xcm_config.rs @@ -82,9 +82,9 @@ impl WeightTrader for DummyWeightTrader { fn buy_weight( &mut self, - _ctx: Option<&XcmContext>, _weight: Weight, _payment: Assets, + _context: &XcmContext, ) -> Result { Ok(Assets::default()) } diff --git a/xcm/xcm-builder/src/tests/weight.rs b/xcm/xcm-builder/src/tests/weight.rs index ebbb79f1ec00..a2fb265413f5 100644 --- a/xcm/xcm-builder/src/tests/weight.rs +++ b/xcm/xcm-builder/src/tests/weight.rs @@ -29,36 +29,36 @@ fn fixed_rate_of_fungible_should_work() { // supplies 100 unit of asset, 80 still remains after purchasing weight assert_eq!( trader.buy_weight( - Some(&ctx), Weight::from_parts(10, 10), - fungible_multi_asset(Here.into(), 100).into() + fungible_multi_asset(Here.into(), 100).into(), + &ctx, ), Ok(fungible_multi_asset(Here.into(), 80).into()), ); // should have nothing left, as 5 + 5 = 10, and we supplied 10 units of asset. assert_eq!( trader.buy_weight( - Some(&ctx), Weight::from_parts(5, 5), - fungible_multi_asset(Here.into(), 10).into() + fungible_multi_asset(Here.into(), 10).into(), + &ctx, ), Ok(vec![].into()), ); // should have 5 left, as there are no proof size components assert_eq!( trader.buy_weight( - Some(&ctx), Weight::from_parts(5, 0), - fungible_multi_asset(Here.into(), 10).into() + fungible_multi_asset(Here.into(), 10).into(), + &ctx, ), Ok(fungible_multi_asset(Here.into(), 5).into()), ); // not enough to purchase the combined weights assert_err!( trader.buy_weight( - Some(&ctx), Weight::from_parts(5, 5), - fungible_multi_asset(Here.into(), 5).into() + fungible_multi_asset(Here.into(), 5).into(), + &ctx, ), XcmError::TooExpensive, ); @@ -166,35 +166,41 @@ fn weight_trader_tuple_should_work() { ); let mut traders = Traders::new(); + let ctx = XcmContext { origin: None, message_id: XcmHash::default(), topic: None }; + // trader one buys weight assert_eq!( - traders.buy_weight(None, Weight::from_parts(5, 5), fungible_multi_asset(Here.into(), 10).into()), + traders.buy_weight( + Weight::from_parts(5, 5), + fungible_multi_asset(Here.into(), 10).into(), + &ctx + ), Ok(vec![].into()), ); // trader one refunds assert_eq!( - traders.refund_weight(None, Weight::from_parts(2, 2)), + traders.refund_weight(Weight::from_parts(2, 2), &ctx), Some(fungible_multi_asset(Here.into(), 4)) ); let mut traders = Traders::new(); // trader one failed; trader two buys weight assert_eq!( - traders.buy_weight(None, Weight::from_parts(5, 5), fungible_multi_asset(para_1, 10).into()), + traders.buy_weight(Weight::from_parts(5, 5), fungible_multi_asset(para_1, 10).into(), &ctx), Ok(vec![].into()), ); // trader two refunds assert_eq!( - traders.refund_weight(None, Weight::from_parts(2, 2)), + traders.refund_weight(Weight::from_parts(2, 2), &ctx), Some(fungible_multi_asset(para_1, 4)) ); let mut traders = Traders::new(); // all traders fails assert_err!( - traders.buy_weight(None, Weight::from_parts(5, 5), fungible_multi_asset(para_2, 10).into()), + traders.buy_weight(Weight::from_parts(5, 5), fungible_multi_asset(para_2, 10).into(), &ctx), XcmError::TooExpensive, ); // and no refund - assert_eq!(traders.refund_weight(None, Weight::from_parts(2, 2)), None); + assert_eq!(traders.refund_weight(Weight::from_parts(2, 2), &ctx), None); } diff --git a/xcm/xcm-builder/src/weight.rs b/xcm/xcm-builder/src/weight.rs index de7fb6daafa9..73cba6cb557b 100644 --- a/xcm/xcm-builder/src/weight.rs +++ b/xcm/xcm-builder/src/weight.rs @@ -142,14 +142,14 @@ impl, R: TakeRevenue> WeightTrader for FixedRateOf fn buy_weight( &mut self, - _ctx: Option<&XcmContext>, weight: Weight, payment: Assets, + context: &XcmContext, ) -> Result { log::trace!( target: "xcm::weight", - "FixedRateOfFungible::buy_weight weight: {:?}, payment: {:?}", - weight, payment, + "FixedRateOfFungible::buy_weight weight: {:?}, payment: {:?}, context: {:?}", + weight, payment, context, ); let (id, units_per_second, units_per_mb) = T::get(); let amount = (units_per_second * (weight.ref_time() as u128) / @@ -165,8 +165,8 @@ impl, R: TakeRevenue> WeightTrader for FixedRateOf Ok(unused) } - fn refund_weight(&mut self, _ctx: Option<&XcmContext>, weight: Weight) -> Option { - log::trace!(target: "xcm::weight", "FixedRateOfFungible::refund_weight weight: {:?}", weight); + fn refund_weight(&mut self, weight: Weight, context: &XcmContext) -> Option { + log::trace!(target: "xcm::weight", "FixedRateOfFungible::refund_weight weight: {:?}, context: {:?}", weight, context); let (id, units_per_second, units_per_mb) = T::get(); let weight = weight.min(self.0); let amount = (units_per_second * (weight.ref_time() as u128) / @@ -217,11 +217,11 @@ impl< fn buy_weight( &mut self, - _ctx: Option<&XcmContext>, weight: Weight, payment: Assets, + context: &XcmContext, ) -> Result { - log::trace!(target: "xcm::weight", "UsingComponents::buy_weight weight: {:?}, payment: {:?}", weight, payment); + log::trace!(target: "xcm::weight", "UsingComponents::buy_weight weight: {:?}, payment: {:?}, context: {:?}", weight, payment, context); let amount = WeightToFee::weight_to_fee(&weight); let u128_amount: u128 = amount.try_into().map_err(|_| XcmError::Overflow)?; let required = (Concrete(AssetId::get()), u128_amount).into(); @@ -231,8 +231,8 @@ impl< Ok(unused) } - fn refund_weight(&mut self, _ctx: Option<&XcmContext>, weight: Weight) -> Option { - log::trace!(target: "xcm::weight", "UsingComponents::refund_weight weight: {:?}", weight); + fn refund_weight(&mut self, weight: Weight, context: &XcmContext) -> Option { + log::trace!(target: "xcm::weight", "UsingComponents::refund_weight weight: {:?}, context: {:?}", weight, context); let weight = weight.min(self.0); let amount = WeightToFee::weight_to_fee(&weight); self.0 -= weight; diff --git a/xcm/xcm-executor/src/lib.rs b/xcm/xcm-executor/src/lib.rs index 24f38cc00e0c..57ddc4322923 100644 --- a/xcm/xcm-executor/src/lib.rs +++ b/xcm/xcm-executor/src/lib.rs @@ -457,7 +457,7 @@ impl XcmExecutor { let current_surplus = self.total_surplus.saturating_sub(self.total_refunded); if current_surplus.any_gt(Weight::zero()) { self.total_refunded.saturating_accrue(current_surplus); - if let Some(w) = self.trader.refund_weight(Some(&self.context), current_surplus) { + if let Some(w) = self.trader.refund_weight(current_surplus, &self.context) { self.subsume_asset(w)?; } } @@ -689,7 +689,7 @@ impl XcmExecutor { // pay for `weight` using up to `fees` of the holding register. let max_fee = self.holding.try_take(fees.into()).map_err(|_| XcmError::NotHoldingFees)?; - let unspent = self.trader.buy_weight(Some(&self.context), weight, max_fee)?; + let unspent = self.trader.buy_weight(weight, max_fee, &self.context)?; self.subsume_assets(unspent)?; } Ok(()) diff --git a/xcm/xcm-executor/src/traits/weight.rs b/xcm/xcm-executor/src/traits/weight.rs index 273d728db560..06e6b5f55bce 100644 --- a/xcm/xcm-executor/src/traits/weight.rs +++ b/xcm/xcm-executor/src/traits/weight.rs @@ -51,16 +51,16 @@ pub trait WeightTrader: Sized { /// for the `weight`, then an error is returned. fn buy_weight( &mut self, - ctx: Option<&XcmContext>, weight: Weight, payment: Assets, + context: &XcmContext, ) -> Result; /// Attempt a refund of `weight` into some asset. The caller does not guarantee that the weight was /// purchased using `buy_weight`. /// /// Default implementation refunds nothing. - fn refund_weight(&mut self, _ctx: Option<&XcmContext>, _weight: Weight) -> Option { + fn refund_weight(&mut self, _weight: Weight, _context: &XcmContext) -> Option { None } } @@ -73,14 +73,14 @@ impl WeightTrader for Tuple { fn buy_weight( &mut self, - ctx: Option<&XcmContext>, weight: Weight, payment: Assets, + context: &XcmContext, ) -> Result { let mut too_expensive_error_found = false; let mut last_error = None; for_tuples!( #( - match Tuple.buy_weight(ctx, weight, payment.clone()) { + match Tuple.buy_weight(weight, payment.clone(), context) { Ok(assets) => return Ok(assets), Err(e) => { if let XcmError::TooExpensive = e { @@ -102,9 +102,9 @@ impl WeightTrader for Tuple { }) } - fn refund_weight(&mut self, ctx: Option<&XcmContext>, weight: Weight) -> Option { + fn refund_weight(&mut self, weight: Weight, context: &XcmContext) -> Option { for_tuples!( #( - if let Some(asset) = Tuple.refund_weight(ctx, weight) { + if let Some(asset) = Tuple.refund_weight(weight, context) { return Some(asset); } )* );