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

Add ensure-ops family methods #12967

Merged
merged 43 commits into from
Dec 28, 2022

Conversation

lemunozm
Copy link
Contributor

This PR adds the following ensure-ops methods for safe arithmetic operations:

  • ensure_add()
  • ensure_sub()
  • ensure_mul()
  • ensure_div()
  • ensure_add_assign()
  • ensure_sub_assign()
  • ensure_mul_assign()
  • ensure_div-assign()
  • ensure_from_rational() for FixedPointNumber
  • ensure_mul_int() for FixedPointNumber
  • ensure_div_int() for FixedPointNumber
  • ensure_from()
  • ensure_into()

Checks before accepting this PR:

  • Is sp_runtime::traits the correct place for this?
  • In order to use any of these traits, the user should import, for example, sp_runtime::traits::ensure::EnsureAdd. If this is too long, it can be shortcutted to sp_runtime::traits::EnsureAdd

This PR does't not break any current API.

Fixes #12754

@cla-bot-2021
Copy link

cla-bot-2021 bot commented Dec 19, 2022

User @lemunozm, please sign the CLA here.

@ggwpez
Copy link
Member

ggwpez commented Dec 19, 2022

Is sp_runtime::traits the correct place for this?

IMO rather sp_primitives::arithmetic::traits. Second opinion?

In order to use any of these traits, the user should import, for example, sp_runtime::traits::ensure::EnsureAdd. If this is too long, it can be shortcutted to sp_runtime::traits::EnsureAdd

You can also create super-traits like Ensure: EnsureAdd + EnsureSub … and the same for EnsureAssign to make it a bit less verbose, in the case that multiple are needed.

@ggwpez ggwpez added A0-please_review Pull request needs code review. B7-runtimenoteworthy C1-low PR touches the given topic and has a low impact on builders. D5-nicetohaveaudit ⚠️ PR contains trivial changes to logic that should be properly reviewed. labels Dec 19, 2022
@lemunozm
Copy link
Contributor Author

IMO rather sp_primitives::arithmetic::traits. Second opinion?

That should be theoretically the best place, but the methods return ArithmeticError that is defined in the runtime. I suppose we don't want to have an arithmetic -> runtime dependency for this.

You can also create super-traits like Ensure: EnsureAdd + EnsureSub … and the same for EnsureAssign to make it a bit less verbose, in the case that multiple are needed.

Sure! I like the idea

@bkchr
Copy link
Member

bkchr commented Dec 19, 2022

Can we not move all of this into sp-arithmetic? Maybe we could move the ArithmeticError to sp-arithmetic as well?

@lemunozm
Copy link
Contributor Author

It could be a possibility 👍🏻 . Makes sense to have this error there. If the runtime reexports the error, it should not be a breaking change.

If you agree, I can do this refactor.

@bkchr
Copy link
Member

bkchr commented Dec 19, 2022

Sounds good 👍

@lemunozm lemunozm force-pushed the lm-ensure-methods branch 2 times, most recently from 47df1eb to 2bc3f02 Compare December 20, 2022 08:36
@lemunozm
Copy link
Contributor Author

I've finally reexported the ensure module to make it more accessible. Now you can import this as sp_arithmetics::traits::Ensure. Also, I've reexported the traits to sp-runtime following the same pattern as the CheckedOps traits. Please, give me double-check if this is what you expect.

primitives/arithmetic/src/traits.rs Outdated Show resolved Hide resolved
primitives/arithmetic/src/traits.rs Outdated Show resolved Hide resolved
primitives/arithmetic/src/traits.rs Outdated Show resolved Hide resolved
primitives/arithmetic/src/traits.rs Outdated Show resolved Hide resolved
primitives/arithmetic/src/traits.rs Outdated Show resolved Hide resolved
primitives/arithmetic/src/traits.rs Show resolved Hide resolved
primitives/arithmetic/src/traits.rs Outdated Show resolved Hide resolved
primitives/arithmetic/src/traits.rs Outdated Show resolved Hide resolved
primitives/arithmetic/src/traits.rs Show resolved Hide resolved
@lemunozm lemunozm requested a review from ggwpez December 21, 2022 17:12
@ggwpez ggwpez requested a review from bkchr December 21, 2022 17:57
Copy link
Contributor

@melekes melekes left a comment

Choose a reason for hiding this comment

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

👍 (code-wise; don't know enough to reason about business logic)

lemunozm and others added 8 commits December 27, 2022 12:02
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: Bastian Köcher <git@kchr.de>
@lemunozm lemunozm requested a review from bkchr December 27, 2022 11:05
primitives/arithmetic/src/traits.rs Outdated Show resolved Hide resolved
primitives/arithmetic/src/traits.rs Outdated Show resolved Hide resolved
@bkchr
Copy link
Member

bkchr commented Dec 28, 2022

bot merge

@paritytech-processbot
Copy link

Error: Statuses failed for 9ba9307

@bkchr
Copy link
Member

bkchr commented Dec 28, 2022

/cmd queue -c fmt $ 1

@command-bot
Copy link

command-bot bot commented Dec 28, 2022

@bkchr https://gitlab.parity.io/parity/mirrors/substrate/-/jobs/2204938 was started for your command "$PIPELINE_SCRIPTS_DIR/fmt.sh" 1. Check out https://gitlab.parity.io/parity/mirrors/substrate/-/pipelines?page=1&scope=all&username=group_605_bot to know what else is being executed currently.

Comment /cmd cancel 149-39fefd7e-7a0f-4853-8780-ffbf40fbd59b to cancel this command or /cmd cancel to cancel all commands in this pull request.

@command-bot
Copy link

command-bot bot commented Dec 28, 2022

@bkchr Command "$PIPELINE_SCRIPTS_DIR/fmt.sh" 1 has finished. Result: https://gitlab.parity.io/parity/mirrors/substrate/-/jobs/2204938 has finished. If any artifacts were generated, you can download them from https://gitlab.parity.io/parity/mirrors/substrate/-/jobs/2204938/artifacts/download.

@bkchr
Copy link
Member

bkchr commented Dec 28, 2022

bot merge

@paritytech-processbot paritytech-processbot bot merged commit 043a60b into paritytech:master Dec 28, 2022
@lemunozm lemunozm deleted the lm-ensure-methods branch December 28, 2022 12:52
@lemunozm lemunozm mentioned this pull request Jan 2, 2023
@Polkadot-Forum
Copy link

This pull request has been mentioned on Polkadot Forum. There might be relevant details there:

https://forum.polkadot.network/t/polkadot-release-analysis-v0-9-37/1736/1

ltfschoen pushed a commit to ltfschoen/substrate that referenced this pull request Feb 22, 2023
* add ensure-ops family methods

* fix cargo doc

* add EnsureOp and EnsureOpAssign meta traits

* move ensure module and ArithmeticError to sp-arithmetic

* fix doc examples

* reexport ensure module content

* ensure mod private

* reexport to sp-runtime

* fix doc example

* remove into(). in doc examples, minor doc changes

* remove return value from assign methods

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* cargo fmt

* Apply suggestions from code review

* ".git/.scripts/fmt.sh" 1

Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: command-bot <>
ark0f pushed a commit to gear-tech/substrate that referenced this pull request Feb 27, 2023
* add ensure-ops family methods

* fix cargo doc

* add EnsureOp and EnsureOpAssign meta traits

* move ensure module and ArithmeticError to sp-arithmetic

* fix doc examples

* reexport ensure module content

* ensure mod private

* reexport to sp-runtime

* fix doc example

* remove into(). in doc examples, minor doc changes

* remove return value from assign methods

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/arithmetic/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* cargo fmt

* Apply suggestions from code review

* ".git/.scripts/fmt.sh" 1

Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: command-bot <>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. C1-low PR touches the given topic and has a low impact on builders. D5-nicetohaveaudit ⚠️ PR contains trivial changes to logic that should be properly reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ensure_ops methods (as checked_ops but returning ArithmeticError instead)
5 participants