From 06d26fdf1b56fea9b9b14bde4c3f839d50b2506d Mon Sep 17 00:00:00 2001 From: Georgi Zlatarev Date: Mon, 24 Apr 2023 12:22:19 +0300 Subject: [PATCH 1/3] use safe math for xcm weights computaitons Signed-off-by: Georgi Zlatarev --- primitives/manta/src/xcm.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/primitives/manta/src/xcm.rs b/primitives/manta/src/xcm.rs index c471ff9af..ef1b9ae92 100644 --- a/primitives/manta/src/xcm.rs +++ b/primitives/manta/src/xcm.rs @@ -180,7 +180,10 @@ where XcmError::TooExpensive })?; - let amount = units_per_second * (weight as u128) / (WEIGHT_PER_SECOND as u128); + let amount = (units_per_second.saturating_mul(weight as u128)) + .checked_div(WEIGHT_PER_SECOND as u128) + .ok_or(XcmError::TooExpensive)?; + // we don't need to proceed if amount is zero. // This is very useful in tests. if amount.is_zero() { @@ -248,8 +251,9 @@ where fn refund_weight(&mut self, weight: Weight) -> Option { if let Some((id, prev_amount, units_per_second)) = &mut self.refund_cache { let weight = weight.min(self.weight); - self.weight -= weight; - let amount = *units_per_second * (weight as u128) / (WEIGHT_PER_SECOND as u128); + self.weight = self.weight.saturating_sub(weight); + let amount = ((*units_per_second).saturating_mul(weight as u128)) + .checked_div(WEIGHT_PER_SECOND as u128)?; *prev_amount = prev_amount.saturating_sub(amount); Some(MultiAsset { fun: Fungibility::Fungible(amount), From c2f43dc7793d76756347ae748708e9f45cf6e1df Mon Sep 17 00:00:00 2001 From: Georgi Zlatarev Date: Mon, 24 Apr 2023 13:55:49 +0300 Subject: [PATCH 2/3] No need for checked_div Signed-off-by: Georgi Zlatarev --- primitives/manta/src/xcm.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/primitives/manta/src/xcm.rs b/primitives/manta/src/xcm.rs index ef1b9ae92..fbe9d7cd5 100644 --- a/primitives/manta/src/xcm.rs +++ b/primitives/manta/src/xcm.rs @@ -180,9 +180,8 @@ where XcmError::TooExpensive })?; - let amount = (units_per_second.saturating_mul(weight as u128)) - .checked_div(WEIGHT_PER_SECOND as u128) - .ok_or(XcmError::TooExpensive)?; + let amount = + (units_per_second.saturating_mul(weight as u128)) / (WEIGHT_PER_SECOND as u128); // we don't need to proceed if amount is zero. // This is very useful in tests. @@ -252,8 +251,8 @@ where if let Some((id, prev_amount, units_per_second)) = &mut self.refund_cache { let weight = weight.min(self.weight); self.weight = self.weight.saturating_sub(weight); - let amount = ((*units_per_second).saturating_mul(weight as u128)) - .checked_div(WEIGHT_PER_SECOND as u128)?; + let amount = + ((*units_per_second).saturating_mul(weight as u128)) / (WEIGHT_PER_SECOND as u128); *prev_amount = prev_amount.saturating_sub(amount); Some(MultiAsset { fun: Fungibility::Fungible(amount), From 6b2227d7d532b7882ade2ce67a92bbc6752639da Mon Sep 17 00:00:00 2001 From: Georgi Zlatarev Date: Mon, 24 Apr 2023 14:15:08 +0300 Subject: [PATCH 3/3] Assert weight_per_econd constant is not 0 Signed-off-by: Georgi Zlatarev --- primitives/manta/src/constants.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/primitives/manta/src/constants.rs b/primitives/manta/src/constants.rs index 2836e5e8d..669eb352e 100644 --- a/primitives/manta/src/constants.rs +++ b/primitives/manta/src/constants.rs @@ -148,6 +148,7 @@ mod constants_tests { }; assert_eq!(WEIGHT_PER_SECOND, IMPORTED_WEIGHT_PER_SECOND); + assert_ne!(WEIGHT_PER_SECOND, 0); assert_eq!(WEIGHT_PER_MILLIS, IMPORTED_WEIGHT_PER_MILLIS); assert_eq!(WEIGHT_PER_MICROS, IMPORTED_WEIGHT_PER_MICROS); assert_eq!(WEIGHT_PER_NANOS, IMPORTED_WEIGHT_PER_NANOS);