Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use safe-math in xcm buy_weight impl #1084

Merged
merged 3 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions primitives/manta/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 6 additions & 3 deletions primitives/manta/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ 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)) / (WEIGHT_PER_SECOND as u128);
ferrell-code marked this conversation as resolved.
Show resolved Hide resolved

// we don't need to proceed if amount is zero.
// This is very useful in tests.
if amount.is_zero() {
Expand Down Expand Up @@ -248,8 +250,9 @@ where
fn refund_weight(&mut self, weight: Weight) -> Option<MultiAsset> {
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)) / (WEIGHT_PER_SECOND as u128);
ferrell-code marked this conversation as resolved.
Show resolved Hide resolved
*prev_amount = prev_amount.saturating_sub(amount);
Some(MultiAsset {
fun: Fungibility::Fungible(amount),
Expand Down