rippled icon indicating copy to clipboard operation
rippled copied to clipboard

Change the return type of mulDiv to std::optional

Open ckeshava opened this issue 3 years ago • 8 comments

  • Previously, mulDiv had std::pair<bool, uint64_t> as the output type.
  • This PR changes the return type to std::optional
  • The existing unit test cases pass with this change.
  • This PR attempts to fix Issue 3495 (https://github.com/XRPLF/rippled/issues/3495)

ckeshava avatar Jul 19 '22 20:07 ckeshava

I had to use std::numeric_limits<std::uint64_t>::max() in a few places to return a default value (in case of an overflow). Do I need to store this value in a variable for better readability? Or is it okay to use this verbose expression?

ckeshava avatar Jul 19 '22 20:07 ckeshava

Nice, thanks @chennakeshava1998 , but I don't think it is necessary to leave the old code commented out.

greg7mdp avatar Jul 20 '22 19:07 greg7mdp

@greg7mdp Sorry my bad. I forgot to remove it. Completed now 👍

ckeshava avatar Jul 20 '22 20:07 ckeshava

Almost there!

Done :) I have also removed extra import statements and refactored one more if-else condition into a value_or statement.

Do I need to clean up the commits? (I can do a force push with a single commit)

ckeshava avatar Jul 21 '22 19:07 ckeshava

Nice! Looks very clean now.

Do I need to clean up the commits? (I can do a force push with a single commit)

Yes, I think it would be better.

Make sure to run the unittests again with the latest version, if you haven't already.

greg7mdp avatar Jul 21 '22 19:07 greg7mdp

I have executed the unit tests, I didn't observe any failures. 👍

ckeshava avatar Jul 21 '22 20:07 ckeshava

I approved the PR, but I don't have write access to the repo, so it doesn't count.

greg7mdp avatar Jul 21 '22 21:07 greg7mdp

Okat, thanks for helping me out greg!

ckeshava avatar Jul 21 '22 21:07 ckeshava

@ckeshava could you merge develop into your branch, please?

intelliot avatar Mar 14 '23 15:03 intelliot

Hello @intelliot , Sure, I'd love to merge it and clean up the commit. Is this a priority issue? I completed this code change as a part of my summer internship last year.

I do not have C++ toolchain required to install the dependencies and unit-test this change. Can I do this after 2-3 weeks?

ckeshava avatar Mar 14 '23 16:03 ckeshava

No hurry - thanks!

intelliot avatar Apr 03 '23 05:04 intelliot

@ckeshava Could you rebase this against the current develop? I tried to do so and got multiple merge conflicts. And I don't want to try to guess the correct conflict resolutions.

I need it rebased because we've recently moved to conan for a build tool, and your PR won't build with Conan without a rebase.

HowardHinnant avatar Apr 04 '23 18:04 HowardHinnant

@HowardHinnant Will do 👍

ckeshava avatar Apr 04 '23 19:04 ckeshava

@HowardHinnant I have merged the latest develop into this feature branch.

ckeshava avatar Apr 20 '23 23:04 ckeshava

you can download the clang-format patch from https://github.com/XRPLF/rippled/actions/runs/4759912341?pr=4243 (under Artifacts) and apply it with git apply <patch file name>

intelliot avatar Apr 21 '23 16:04 intelliot

thanks a lot elliot, I was searching for exactly that :))

ckeshava avatar Apr 21 '23 17:04 ckeshava

Hello, I have completed the code changes for this PR. @HowardHinnant @greg7mdp

ckeshava avatar Apr 25 '23 22:04 ckeshava

I have requested changes to the git merge which place these commits at the top.

HowardHinnant avatar May 30 '23 18:05 HowardHinnant

I'll work on your suggestions, sorry I forgot about it.

ckeshava avatar May 30 '23 19:05 ckeshava

Hello @HowardHinnant , Thank you so much for your detailed help with reorganizing the git commits, it was really helpful. Sorry for the delay in updating this PR, I got occupied with another PR.

Is there a better alternative than force-pushing the changes? After rebasing, the remote and local branches were so different that I couldn't think of a better alternative.

ckeshava avatar Jun 05 '23 23:06 ckeshava

Hello @HowardHinnant , Thank you so much for your detailed help with reorganizing the git commits, it was really helpful. Sorry for the delay in updating this PR, I got occupied with another PR.

Is there a better alternative than force-pushing the changes? After rebasing, the remote and local branches were so different that I couldn't think of a better alternative.

I think force pushing is the only option here. Thanks for this work. It is on my to-do list to review.

HowardHinnant avatar Jun 08 '23 02:06 HowardHinnant

CI caught a format issue. Instructions on how to fix it are in the job log.

thejohnfreeman avatar Jun 14 '23 23:06 thejohnfreeman

okay, I've fixed that. let me know if I need to squash or clean-up the commit log

ckeshava avatar Jun 14 '23 23:06 ckeshava

It will be squashed when it is merged. Now that there are at least two approvals and all checks pass, you can add the "Passed" label if you think this PR is ready to merge.

thejohnfreeman avatar Jun 15 '23 03:06 thejohnfreeman

It will be squashed when it is merged. Now that there are at least two approvals and all checks pass, you can add the "Passed" label if you think this PR is ready to merge.

Okay, done. thanks

ckeshava avatar Jun 15 '23 17:06 ckeshava

Suggested commit message:

refactor: change the return type of mulDiv to std::optional (#4243)

- Previously, mulDiv had `std::pair<bool, uint64_t>` as the output type.
  - This is an error-prone interface as it is easy to ignore when
    overflow occurs.
- Using a return type of `std::optional` should decrease the likelihood
  of ignoring overflow.
  - It also allows for the use of optional::value_or() as a way to
    explicitly recover from overflow.
- Include limits.h header file preprocessing directive in order to
  satisfy gcc's numeric_limits incomplete_type requirement.

Fix #3495

---------

Co-authored-by: John Freeman <[email protected]>

intelliot avatar Jul 05 '23 18:07 intelliot

For future reference: there is no need to force push. Instead, just confirm (or write a comment with) the desired commit message. A new commit message is set when the PR is Squash and merged.

Also, for future PRs, please sign your commits.

intelliot avatar Jul 06 '23 03:07 intelliot

okay, I'll keep that in mind 👍

ckeshava avatar Jul 06 '23 15:07 ckeshava

Following up on an old question: @HowardHinnant @ckeshava please never force-push in a PR. If you end up in a situation where you don't want to bother resolving conflicts, and you just want to keep your changes, then use this:

git merge upstream/develop --strategy ours

This creates a merge commit with two parents, but just takes your changes wherever there is a conflict.

You should not rebase either. That is what creates the conditions (lost history) that encourages force-push.

thejohnfreeman avatar Jul 10 '23 14:07 thejohnfreeman

Okay, thanks for the tip, I'll keep it in mind.

ckeshava avatar Jul 10 '23 17:07 ckeshava