cosmos-sdk/math
Matt Kocubinski 4db99fc1a5
fix(math): Dec marshal shouldn't have side effects (#15506)
## Description

Ref: https://github.com/cosmos/cosmos-sdk/issues/15170
Included in: #15515 

Required to resolve a discrepancy between the sign mode being implemented in #15170 and the existing legacy one.

When calling tx builder [SetMsgs](4a6a1e3cb8/x/auth/tx/builder.go (L208)) messages are coerced to an any type in [NewAnyWithValue](4a6a1e3cb8/codec/types/any.go (L61)). This marshals a message as proto bytes. If a message has cosmos.Dec typed fields, and they are set to nil, the will be set to 0 during seriazliation in the [Dec.Marshal](88909d6f2b/math/dec.go (L801)) implementation.

This side effect *changes* the output of a JSON marshal before and after the call to Dec.Marshal, as shown below.

```go
rates := &stakingtypes.CommissionRates{}
MarshalJson(rates)
// {"rate":"0","max_rate":"0","max_change_rate":"0"}

MarshalProto(rates)

MarshalJson(rates)
//{"rate":"0.000000000000000000","max_rate":"0.000000000000000000","max_change_rate":"0.000000000000000000"}
```

An integration is test is also committed asserting the (now fixed) behavior above.  Its asserts should be changed once a release of math is made and `/tests/go.mod` is updated.



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

* [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
* [ ] added `!` to the type prefix if API or client breaking change
* [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
* [ ] provided a link to the relevant issue or specification
* [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/docs/building-modules)
* [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
* [ ] added a changelog entry to `CHANGELOG.md`
* [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
* [ ] updated the relevant documentation or specification
* [ ] reviewed "Files changed" and left comments if necessary
* [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

* [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
* [ ] confirmed `!` in the type prefix if API or client breaking change
* [ ] confirmed all author checklist items have been addressed 
* [ ] reviewed state machine logic
* [ ] reviewed API design and naming
* [ ] reviewed documentation is accurate
* [ ] reviewed tests and test coverage
* [ ] manually tested (if applicable)
2023-03-22 19:48:36 +00:00
..
testdata test(math): added integer file to fix test (#15490) 2023-03-20 22:55:34 +00:00
unsafe feat: add rand funcs to math (#15043) 2023-02-18 18:39:46 +00:00
CHANGELOG.md fix(math): Dec marshal shouldn't have side effects (#15506) 2023-03-22 19:48:36 +00:00
dec_internal_test.go test: Added test cases for precisionMultiplier (#14576) 2023-01-12 06:41:47 +00:00
dec_test.go test(math): fix FormatDec test (#15215) 2023-02-28 14:57:42 +00:00
dec.go fix(math): Dec marshal shouldn't have side effects (#15506) 2023-03-22 19:48:36 +00:00
doc.go refactor!: create math go sub module (#11788) 2022-04-28 14:05:21 -04:00
fuzz_test.go fix: math: add LegacyNewDecFromStr fuzzers + remove unnecessary error wrapping (#14252) 2022-12-11 09:02:51 +00:00
go.mod chore: bump cometbft to v0.37.0-rc3 (#15220) 2023-03-01 16:30:41 +01:00
go.sum build(deps): Bump github.com/stretchr/testify from 1.8.1 to 1.8.2 (#15172) 2023-02-27 19:49:52 +00:00
int_internal_test.go refactor!: create math go sub module (#11788) 2022-04-28 14:05:21 -04:00
int_test.go test(math): added integer file to fix test (#15490) 2023-03-20 22:55:34 +00:00
int.go perf: math: optimize and test FormatInt + simplify LegacyNewDecFromStr (#14010) 2022-11-29 21:00:47 +00:00
max_min_test.go feat: math: add generics versions of Max, Min to cater to all numeric types (#14166) 2022-12-25 07:26:59 +00:00
max_min.go feat: math: add generics versions of Max, Min to cater to all numeric types (#14166) 2022-12-25 07:26:59 +00:00
proto.go refactor: migrate to cosmos/gogoproto (#13070) 2022-09-08 17:27:48 +00:00
sonar-project.properties ci: make sonarcloud more quiet (#14585) 2023-01-12 10:24:56 +00:00
uint_internal_test.go chore: gofumpt (#11839) 2022-05-19 10:55:27 +02:00
uint_test.go feat: add uint IsNil method (#13381) 2022-09-25 09:19:50 +00:00
uint.go feat: add uint IsNil method (#13381) 2022-09-25 09:19:50 +00:00