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

Conversation

ghzlatarev
Copy link
Contributor

@ghzlatarev ghzlatarev commented Apr 24, 2023

Description

  • This was not going to happen with current values but just in case for future additions we protect against accidental overflows.
  • The division is impossible to underflow because the dividend will be saturated to the max value of u128. And u128::Max can be divided by the second smallest u128 number.
  • A divide by zero is possible but we can assert that WEIGHT_PER_SECOND is never 0 with an integration test.

Before we can approve this PR for merge, please make sure that all the following items have been checked off:

  • Connected to an issue with discussion and accepted design using zenhub "Connect issue" button below
  • Added one label out of the L- group to this PR
  • Added one or more labels from the A- and C- groups to this PR
  • Explicitly labelled A-calamari, A-dolphin and/or A-manta if your changes are meant for/impact either of these (CI depends on it)
  • Re-reviewed Files changed in the Github PR explorer.

Situational Notes:

  • If adding functionality, write unit tests!
  • If importing a new pallet, choose a proper module index for it, and allow it in BaseFilter. Ensure every extrinsic works from front-end. If there's corresponding tool, ensure both work for each other.
  • If needed, update our Javascript/Typescript APIs. These APIs are officially used by exchanges or community developers.
  • If modifying existing runtime storage items, make sure to implement storage migrations for the runtime and test them with try-runtime. This includes migrations inherited from upstream changes, and you can search the diffs for modifications of #[pallet::storage] items to check for any.

Signed-off-by: Georgi Zlatarev <georgi.zlatarev@manta.network>
Signed-off-by: Georgi Zlatarev <georgi.zlatarev@manta.network>
@ghzlatarev ghzlatarev self-assigned this Apr 24, 2023
@ghzlatarev ghzlatarev added C-enhancement Category: An issue proposing an enhancement or a PR with one A-manta Area: Issues and PRs related to the Manta Runtime A-calamari Area: Issues and PRs related to the Calamari Runtime A-xcm Area: Issues and PRs related to Cross-Consensus Messaging (XCM) L-changed Log: Issues and PRs related to changes labels Apr 24, 2023
@ghzlatarev ghzlatarev marked this pull request as ready for review April 24, 2023 11:04
Signed-off-by: Georgi Zlatarev <georgi.zlatarev@manta.network>
@github-actions
Copy link

github-actions bot commented Apr 24, 2023

⚠️ Congestion test: 1-day congestion cost (calamari) is NOT above target_daily_congestion_cost_kma

Copy link
Contributor

@ferrell-code ferrell-code left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one comment fine as is

primitives/manta/src/xcm.rs Show resolved Hide resolved
primitives/manta/src/xcm.rs Show resolved Hide resolved
Copy link
Contributor

@Garandor Garandor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this division can't overflow, so it's fine as is

@ferrell-code
Copy link
Contributor

ferrell-code commented Apr 25, 2023

Good point, why does saturating_div even exist?

@Garandor
Copy link
Contributor

Garandor commented Apr 25, 2023

Good point, why does saturating_div even exist?

Dividing by a float [0,1) increases the value so it can overflow in the positive direction which is handled by saturating...
on signed types it can also overflow in the negative direction in the same way.
Also, division-by-zero is a panic condition.
Georgi uses a u128 type and checks for nonzero of the divisor in the integration test, so for these inputs saturating_div and / have identical semantics

@ghzlatarev ghzlatarev merged commit fe65fcc into manta Apr 26, 2023
@ghzlatarev ghzlatarev deleted the ghzlatarev/xcm-weights-safe-math branch April 26, 2023 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-calamari Area: Issues and PRs related to the Calamari Runtime A-manta Area: Issues and PRs related to the Manta Runtime A-xcm Area: Issues and PRs related to Cross-Consensus Messaging (XCM) C-enhancement Category: An issue proposing an enhancement or a PR with one L-changed Log: Issues and PRs related to changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants