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

fix(math): NewUintFromBigInt argument mutation #18214

Merged
merged 3 commits into from
Oct 23, 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 math/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Ref: https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.j

### Bug Fixes

* [#18214](https://github.com/cosmos/cosmos-sdk/pull/18214) Ensure that modifying the argument to `NewUIntFromBigInt` doesn't mutate the returned value.
* [#18211](https://github.com/cosmos/cosmos-sdk/pull/18211) RelativePow now returns 1 when 0^0, before it was returning the scale factor.
* [#17725](https://github.com/cosmos/cosmos-sdk/pull/17725) Fix state break in ApproxRoot. This has been present since math/v1.0.1. It changed the rounding behavior at precision end in an intermediary division from banker's to truncation. The truncation occurs from binary right shift in the case of square roots. The change is now reverted back to banker's rounding universally for any root.

Expand Down
2 changes: 1 addition & 1 deletion math/uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func checkNewUint(i *big.Int) (Uint, error) {
if err := UintOverflow(i); err != nil {
return Uint{}, err
}
return Uint{i}, nil
return Uint{new(big.Int).Set(i)}, nil
}

// RelativePow raises x to the power of n, where x (and the result, z) are scaled by factor b
Expand Down
10 changes: 10 additions & 0 deletions math/uint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ func (s *uintTestSuite) TestParseUint() {
}
}

func (s *uintTestSuite) TestNewUintFromBigInt() {
r := big.NewInt(42)
i := sdkmath.NewUintFromBigInt(r)
s.Require().Equal(r, i.BigInt())

// modify r and ensure i doesn't change
r = r.SetInt64(100)
s.Require().NotEqual(r, i.BigInt())
}

func randuint() sdkmath.Uint {
return sdkmath.NewUint(rand.Uint64())
}
Expand Down