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

refactor: Move sdk.Dec to math package #12634

Merged
merged 10 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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 go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ replace (
cosmossdk.io/api => ./api
cosmossdk.io/core => ./core
cosmossdk.io/depinject => ./depinject
cosmossdk.io/math => ./math
github.com/cosmos/cosmos-sdk/db => ./db
github.com/cosmos/cosmos-sdk/store/tools/ics23 => ./store/tools/ics23

Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
contrib.go.opencensus.io/exporter/stackdriver v0.13.4/go.mod h1:aXENhDJ1Y4lIg4EUaVTwzvYETVNZk10Pu26tevFKLUc=
cosmossdk.io/errors v1.0.0-beta.7 h1:gypHW76pTQGVnHKo6QBkb4yFOJjC+sUGRc5Al3Odj1w=
cosmossdk.io/errors v1.0.0-beta.7/go.mod h1:mz6FQMJRku4bY7aqS/Gwfcmr/ue91roMEKAmDUDpBfE=
cosmossdk.io/math v1.0.0-beta.2 h1:17hSVc9ne1c31IaLDfjRojtN+y4Rd2N8H/6Fht2sBzw=
cosmossdk.io/math v1.0.0-beta.2/go.mod h1:u/MXvf8wbUbCsAEyQSSYXXMsczAsFX48e2D6JI86T4o=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU=
filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
Expand Down
2 changes: 2 additions & 0 deletions math/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ Ref: https://keepachangelog.com/en/1.0.0/
# Changelog

## [Unreleased]

* [#12634](https://github.com/cosmos/cosmos-sdk/pull/12634) Move `sdk.Dec` to math package.
Copy link
Member

Choose a reason for hiding this comment

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

Can we add this same entry in the root changelog as well?

8 changes: 1 addition & 7 deletions types/decimal.go → math/dec.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package math

import (
"encoding/json"
Expand All @@ -10,8 +10,6 @@ import (
"testing"
)

var _ CustomProtobufType = (*Dec)(nil)

// NOTE: never use new(Dec) or else we will panic unmarshalling into the
// nil embedded big.Int
type Dec struct {
Expand Down Expand Up @@ -853,10 +851,6 @@ func (d *Dec) Size() int {
func (d Dec) MarshalAmino() ([]byte, error) { return d.Marshal() }
func (d *Dec) UnmarshalAmino(bz []byte) error { return d.Unmarshal(bz) }

func (dp DecProto) String() string {
return dp.Dec.String()
}

// helpers

// test if two decimal arrays are equal
Expand Down
11 changes: 6 additions & 5 deletions types/decimal_internal_test.go → math/dec_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types
package math

import (
"encoding/json"
"math/big"
"testing"

Expand All @@ -23,20 +24,20 @@ func (s *decimalInternalTestSuite) TestPrecisionMultiplier() {

func (s *decimalInternalTestSuite) TestZeroDeserializationJSON() {
d := Dec{new(big.Int)}
err := cdc.UnmarshalJSON([]byte(`"0"`), &d)
err := json.Unmarshal([]byte(`"0"`), &d)
s.Require().Nil(err)
err = cdc.UnmarshalJSON([]byte(`"{}"`), &d)
err = json.Unmarshal([]byte(`"{}"`), &d)
s.Require().NotNil(err)
}

func (s *decimalInternalTestSuite) TestSerializationGocodecJSON() {
d := MustNewDecFromStr("0.333")

bz, err := cdc.MarshalJSON(d)
bz, err := json.Marshal(d)
s.Require().NoError(err)

d2 := Dec{new(big.Int)}
err = cdc.UnmarshalJSON(bz, &d2)
err = json.Unmarshal(bz, &d2)
s.Require().NoError(err)
s.Require().True(d.Equal(d2), "original: %v, unmarshalled: %v", d, d2)
}
Expand Down
Loading