Skip to content

Commit 6e70d25

Browse files
authored
feat: Add decimal support to group module (#10035)
<!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #9895 Add decimal support to group module based on a streamlined version of https://github.com/regen-network/regen-ledger/tree/3265a868bf834fb946da1facb81122e13e348538/types/math --- ### 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... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/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)
1 parent 58673f8 commit 6e70d25

File tree

5 files changed

+391
-7
lines changed

5 files changed

+391
-7
lines changed

types/errors/errors.go

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const RootCodespace = "sdk"
1313
// UndefinedCodespace when we explicitly declare no codespace
1414
const UndefinedCodespace = "undefined"
1515

16+
// mathCodespace is the codespace for all errors defined in math package
17+
const mathCodespace = "math"
18+
1619
var (
1720
// errInternal should never be exposed, but we reserve this code for non-specified errors
1821
errInternal = Register(UndefinedCodespace, 1, "internal")
@@ -147,6 +150,9 @@ var (
147150

148151
// ErrAppConfig defines an error occurred if min-gas-prices field in BaseConfig is empty.
149152
ErrAppConfig = Register(RootCodespace, 40, "error in app.toml")
153+
154+
// ErrInvalidDecString defines an error for an invalid decimal string
155+
ErrInvalidDecString = Register(mathCodespace, 41, "invalid decimal string")
150156
)
151157

152158
// Register returns an error instance that should be used as the base for

x/group/go.mod

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ go 1.17
33
module github.com/cosmos/cosmos-sdk/x/group
44

55
require (
6+
github.com/cockroachdb/apd/v2 v2.0.2
67
github.com/cosmos/cosmos-sdk v0.44.0
78
github.com/gogo/protobuf v1.3.3
89
github.com/regen-network/cosmos-proto v0.3.1
10+
github.com/stretchr/testify v1.7.0
911
google.golang.org/grpc v1.40.0
1012
google.golang.org/protobuf v1.27.1
13+
pgregory.net/rapid v0.4.7
1114
)
1215

1316
require (
@@ -45,6 +48,7 @@ require (
4548
github.com/pelletier/go-toml v1.9.3 // indirect
4649
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
4750
github.com/pkg/errors v0.9.1 // indirect
51+
github.com/pmezard/go-difflib v1.0.0 // indirect
4852
github.com/prometheus/client_golang v1.11.0 // indirect
4953
github.com/prometheus/client_model v0.2.0 // indirect
5054
github.com/prometheus/common v0.30.0 // indirect
@@ -63,13 +67,14 @@ require (
6367
github.com/tendermint/tendermint v0.34.13 // indirect
6468
github.com/tendermint/tm-db v0.6.4 // indirect
6569
go.etcd.io/bbolt v1.3.5 // indirect
66-
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
70+
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
6771
golang.org/x/net v0.0.0-20210903162142-ad29c8ab022f // indirect
6872
golang.org/x/sys v0.0.0-20210903071746-97244b99971b // indirect
6973
golang.org/x/text v0.3.6 // indirect
7074
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
7175
gopkg.in/ini.v1 v1.62.0 // indirect
7276
gopkg.in/yaml.v2 v2.4.0 // indirect
77+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
7378
)
7479

7580
replace google.golang.org/grpc => google.golang.org/grpc v1.33.2

x/group/go.sum

+11-6
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6D
123123
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=
124124
github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec/go.mod h1:jMjuTZXRI4dUb/I5gc9Hdhagfvm9+RyrPryS/auMzxE=
125125
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
126+
github.com/cockroachdb/apd/v2 v2.0.2 h1:weh8u7Cneje73dDh+2tEVLUvyBc89iwepWCD8b8034E=
127+
github.com/cockroachdb/apd/v2 v2.0.2/go.mod h1:DDxRlzC2lo3/vSlmSoS7JkqbbrARPuFOGr0B9pvN3Gw=
126128
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
127129
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
128130
github.com/coinbase/rosetta-sdk-go v0.6.10 h1:rgHD/nHjxLh0lMEdfGDqpTtlvtSBwULqrrZ2qPdNaCM=
@@ -149,7 +151,7 @@ github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY
149151
github.com/cosmos/iavl v0.15.0-rc3.0.20201009144442-230e9bdf52cd/go.mod h1:3xOIaNNX19p0QrX0VqWa6voPRoJRGGYtny+DH8NEPvE=
150152
github.com/cosmos/iavl v0.15.0-rc5/go.mod h1:WqoPL9yPTQ85QBMT45OOUzPxG/U/JcJoN7uMjgxke/I=
151153
github.com/cosmos/iavl v0.15.3/go.mod h1:OLjQiAQ4fGD2KDZooyJG9yz+p2ao2IAYSbke8mVvSA4=
152-
github.com/cosmos/iavl v0.16.0 h1:ICIOB8xysirTX27GmVAaoeSpeozzgSu9d49w36xkVJA=
154+
github.com/cosmos/iavl v0.17.1 h1:b/Cl8h1PRMvsu24+TYNlKchIu7W6tmxIBGe6E9u2Ybw=
153155
github.com/cosmos/ledger-cosmos-go v0.11.1 h1:9JIYsGnXP613pb2vPjFeMMjBI5lEDsEaF6oYorTy6J4=
154156
github.com/cosmos/ledger-go v0.9.2 h1:Nnao/dLwaVTk1Q5U9THldpUMMXU94BOTWPddSmVB6pI=
155157
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
@@ -403,8 +405,9 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFB
403405
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
404406
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
405407
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
406-
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
407408
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
409+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
410+
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
408411
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
409412
github.com/libp2p/go-buffer-pool v0.0.2 h1:QNK2iAFa8gjAe1SPz6mHSMuCcjs+X1wlHzeOSqcmlfs=
410413
github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoRZd1Vi32+RXyFM=
@@ -464,13 +467,13 @@ github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:v
464467
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
465468
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
466469
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
467-
github.com/onsi/ginkgo v1.14.0 h1:2mOpI4JVVPBN+WQRa0WKH2eXR+Ey+uK4n7Zj0aYpIQA=
468470
github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
471+
github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc=
469472
github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
470473
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
471474
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
472-
github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE=
473475
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
476+
github.com/onsi/gomega v1.13.0 h1:7lLHu94wT9Ij0o6EWWclhu0aOh32VxhkwEJvzuWPeak=
474477
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
475478
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
476479
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
@@ -708,8 +711,8 @@ golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899/go.mod h1:LzIPMQfyMNhhGPh
708711
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
709712
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
710713
golang.org/x/crypto v0.0.0-20201117144127-c1f2f97bffc9/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
711-
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=
712-
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
714+
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a h1:kr2P4QFmQr29mSLA43kwrOcgcReGTfbE9N577tCTuBc=
715+
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
713716
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
714717
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
715718
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -1101,6 +1104,8 @@ honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt
11011104
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
11021105
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
11031106
nhooyr.io/websocket v1.8.6 h1:s+C3xAMLwGmlI31Nyn/eAehUlZPwfYZu2JXM621Q5/k=
1107+
pgregory.net/rapid v0.4.7 h1:MTNRktPuv5FNqOO151TM9mDTa+XHcX6ypYeISDVD14g=
1108+
pgregory.net/rapid v0.4.7/go.mod h1:UYpPVyjFHzYBGHIxLFoupi8vwk6rXNzRY9OMvVxFIOU=
11041109
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
11051110
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
11061111
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

x/group/internal/math/dec.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Package math provides helper functions for doing mathematical calculations and parsing for the ecocredit module.
2+
package math
3+
4+
import (
5+
"fmt"
6+
7+
"github.com/cockroachdb/apd/v2"
8+
9+
"github.com/cosmos/cosmos-sdk/types/errors"
10+
)
11+
12+
// Dec is a wrapper struct around apd.Decimal that does no mutation of apd.Decimal's when performing
13+
// arithmetic, instead creating a new apd.Decimal for every operation ensuring usage is safe.
14+
//
15+
// Using apd.Decimal directly can be unsafe because apd operations mutate the underlying Decimal,
16+
// but when copying the big.Int structure can be shared between Decimal instances causing corruption.
17+
// This was originally discovered in regen0-network/mainnet#15.
18+
type Dec struct {
19+
dec apd.Decimal
20+
}
21+
22+
func NewDecFromString(s string) (Dec, error) {
23+
d, _, err := apd.NewFromString(s)
24+
if err != nil {
25+
return Dec{}, errors.ErrInvalidDecString.Wrap(err.Error())
26+
}
27+
return Dec{*d}, nil
28+
}
29+
30+
func NewDecFromInt64(x int64) Dec {
31+
var res Dec
32+
res.dec.SetInt64(x)
33+
return res
34+
}
35+
36+
// Add returns a new Dec with value `x+y` without mutating any argument and error if
37+
// there is an overflow.
38+
func (x Dec) Add(y Dec) (Dec, error) {
39+
var z Dec
40+
_, err := apd.BaseContext.Add(&z.dec, &x.dec, &y.dec)
41+
return z, errors.Wrap(err, "decimal addition error")
42+
}
43+
44+
// Sub returns a new Dec with value `x-y` without mutating any argument and error if
45+
// there is an overflow.
46+
func (x Dec) Sub(y Dec) (Dec, error) {
47+
var z Dec
48+
_, err := apd.BaseContext.Sub(&z.dec, &x.dec, &y.dec)
49+
return z, errors.Wrap(err, "decimal subtraction error")
50+
}
51+
52+
func (x Dec) Int64() (int64, error) {
53+
return x.dec.Int64()
54+
}
55+
56+
func (x Dec) Cmp(y Dec) int {
57+
return x.dec.Cmp(&y.dec)
58+
}
59+
60+
func (x Dec) IsEqual(y Dec) bool {
61+
return x.dec.Cmp(&y.dec) == 0
62+
}
63+
64+
func (x Dec) IsNegative() bool {
65+
return x.dec.Negative && !x.dec.IsZero()
66+
}
67+
68+
// Add adds x and y
69+
func Add(x Dec, y Dec) (Dec, error) {
70+
return x.Add(y)
71+
}
72+
73+
// SubNonNegative subtracts the value of y from x and returns the result with
74+
// arbitrary precision. Returns an error if the result is negative.
75+
func SubNonNegative(x Dec, y Dec) (Dec, error) {
76+
z, err := x.Sub(y)
77+
if err != nil {
78+
return Dec{}, err
79+
}
80+
81+
if z.IsNegative() {
82+
return z, fmt.Errorf("result negative during non-negative subtraction")
83+
}
84+
85+
return z, nil
86+
}

0 commit comments

Comments
 (0)