Skip to content

Commit 4f04bea

Browse files
authored
refactor!: create math go sub module (#11788)
1 parent 597ab54 commit 4f04bea

37 files changed

+626
-522
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
8686

8787
### API Breaking Changes
8888

89+
* (types) [#11788](https://github.com/cosmos/cosmos-sdk/pull/11788) The `Int` and `Uint` types have been moved to their own dedicated module, `math`. Aliases are kept in the SDK's root `types` package, however, it is encouraged to utilize the new `math` module. As a result, the `Int#ToDec` API has been removed.
8990
* (grpc) [\#11642](https://github.com/cosmos/cosmos-sdk/pull/11642) The `RegisterTendermintService` method in the `tmservice` package now requires a `abciQueryFn` query function parameter.
9091
* [\#11496](https://github.com/cosmos/cosmos-sdk/pull/11496) Refactor abstractions for snapshot and pruning; snapshot intervals eventually pruned; unit tests.
9192
* (types) [\#11689](https://github.com/cosmos/cosmos-sdk/pull/11689) Make `Coins#Sub` and `Coins#SafeSub` consistent with `Coins#Add`.

contrib/images/simd-dlv/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ FROM golang:1.18-alpine AS build
22

33
RUN apk add build-base git linux-headers libc-dev
44
RUN go install github.com/go-delve/delve/cmd/dlv@latest
5+
56
WORKDIR /work
67
COPY go.mod go.sum /work/
78
COPY db/go.mod db/go.sum /work/db/
9+
COPY errors/go.mod errors/go.sum /work/errors/
10+
COPY math/go.mod math/go.sum /work/math/
11+
812
RUN go mod download
913
COPY ./ /work
1014
RUN LEDGER_ENABLED=false make COSMOS_BUILD_OPTIONS="debug,nostrip" clean build

contrib/images/simd-env/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
FROM golang:1.18-alpine AS build
2+
23
RUN apk add build-base git linux-headers
4+
35
WORKDIR /work
46
COPY go.mod go.sum /work/
57
COPY db/go.mod db/go.sum /work/db/
68
COPY errors/go.mod errors/go.sum /work/errors/
9+
COPY math/go.mod math/go.sum /work/math/
710

811
RUN go mod download
912
COPY ./ /work

errors/CHANGELOG.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,14 @@ Change log entries are to be added to the Unreleased section under the
1515
appropriate stanza (see below). Each entry should ideally include a tag and
1616
the Github issue reference in the following format:
1717
18-
* (<tag>) \#<issue-number> message
19-
20-
The issue numbers will later be link-ified during the release process so you do
21-
not have to worry about including a link manually, but you can if you wish.
18+
* (<tag>) [#<issue-number>] Changelog message.
2219
2320
Types of changes (Stanzas):
2421
2522
"Features" for new features.
2623
"Improvements" for changes in existing functionality.
2724
"Deprecated" for soon-to-be removed features.
2825
"Bug Fixes" for any bug fixes.
29-
"Client Breaking" for breaking Protobuf, gRPC and REST routes used by end-users.
30-
"CLI Breaking" for breaking CLI commands.
3126
"API Breaking" for breaking exported APIs used by developers building on SDK.
3227
Ref: https://keepachangelog.com/en/1.0.0/
3328
-->

go.mod

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/cosmos/cosmos-sdk/api v0.1.0
1616
github.com/cosmos/cosmos-sdk/db v1.0.0-beta.1
1717
github.com/cosmos/cosmos-sdk/errors v1.0.0-beta.5
18+
github.com/cosmos/cosmos-sdk/math v0.0.0-00010101000000-000000000000
1819
github.com/cosmos/go-bip39 v1.0.0
1920
github.com/cosmos/iavl v0.18.0
2021
github.com/cosmos/ledger-cosmos-go v0.11.1
@@ -149,14 +150,16 @@ require (
149150
nhooyr.io/websocket v1.8.6 // indirect
150151
)
151152

152-
replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
153+
replace (
154+
github.com/99designs/keyring => github.com/cosmos/keyring v1.1.7-0.20210622111912-ef00f8ac3d76
153155

154-
replace github.com/99designs/keyring => github.com/cosmos/keyring v1.1.7-0.20210622111912-ef00f8ac3d76
156+
github.com/cosmos/cosmos-sdk/db => ./db
157+
github.com/cosmos/cosmos-sdk/math => ./math
155158

156-
// Fix upstream GHSA-h395-qcrw-5vmq vulnerability.
157-
// TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409
158-
replace github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.7.0
159-
160-
replace github.com/cosmos/cosmos-sdk/db => ./db
159+
// Fix upstream GHSA-h395-qcrw-5vmq vulnerability.
160+
// TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409
161+
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.7.0
162+
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
163+
)
161164

162165
retract v0.43.0

math/CHANGELOG.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!--
2+
Guiding Principles:
3+
4+
Changelogs are for humans, not machines.
5+
There should be an entry for every single version.
6+
The same types of changes should be grouped.
7+
Versions and sections should be linkable.
8+
The latest version comes first.
9+
The release date of each version is displayed.
10+
Mention whether you follow Semantic Versioning.
11+
12+
Usage:
13+
14+
Change log entries are to be added to the Unreleased section under the
15+
appropriate stanza (see below). Each entry should ideally include a tag and
16+
the Github issue reference in the following format:
17+
18+
* (<tag>) [#<issue-number>] Changelog message.
19+
20+
Types of changes (Stanzas):
21+
22+
"Features" for new features.
23+
"Improvements" for changes in existing functionality.
24+
"Deprecated" for soon-to-be removed features.
25+
"Bug Fixes" for any bug fixes.
26+
"API Breaking" for breaking exported APIs used by developers building on SDK.
27+
Ref: https://keepachangelog.com/en/1.0.0/
28+
-->
29+
30+
# Changelog
31+
32+
## [Unreleased]

math/doc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
Package math implements custom Cosmos SDK math types used for arithmetic
3+
operations. Signed and unsigned integer types utilize Golang's standard library
4+
big integers types, having a maximum bit length of 256 bits.
5+
*/
6+
package math

math/go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/cosmos/cosmos-sdk/math
2+
3+
go 1.18
4+
5+
require github.com/stretchr/testify v1.7.0
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.0 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
11+
)

math/go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
7+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

types/int.go renamed to math/int.go

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
package types
1+
package math
22

33
import (
44
"encoding"
55
"encoding/json"
66
"fmt"
7-
"testing"
8-
97
"math/big"
8+
"testing"
109
)
1110

12-
const maxBitLen = 256
11+
// MaxBitLen defines the maximum bit length supported bit Int and Uint types.
12+
const MaxBitLen = 256
1313

1414
func newIntegerFromString(s string) (*big.Int, bool) {
1515
return new(big.Int).SetString(s, 0)
@@ -60,14 +60,14 @@ func unmarshalText(i *big.Int, text string) error {
6060
return err
6161
}
6262

63-
if i.BitLen() > maxBitLen {
63+
if i.BitLen() > MaxBitLen {
6464
return fmt.Errorf("integer out of range: %s", text)
6565
}
6666

6767
return nil
6868
}
6969

70-
var _ CustomProtobufType = (*Int)(nil)
70+
var _ customProtobufType = (*Int)(nil)
7171

7272
// Int wraps big.Int with a 257 bit range bound
7373
// Checks overflow, underflow and division by zero
@@ -108,7 +108,7 @@ func NewIntFromBigInt(i *big.Int) Int {
108108
return Int{}
109109
}
110110

111-
if i.BitLen() > maxBitLen {
111+
if i.BitLen() > MaxBitLen {
112112
panic("NewIntFromBigInt() out of bound")
113113
}
114114
return Int{i}
@@ -121,7 +121,7 @@ func NewIntFromString(s string) (res Int, ok bool) {
121121
return
122122
}
123123
// Check overflow
124-
if i.BitLen() > maxBitLen {
124+
if i.BitLen() > MaxBitLen {
125125
ok = false
126126
return
127127
}
@@ -139,7 +139,7 @@ func NewIntWithDecimal(n int64, dec int) Int {
139139
i.Mul(big.NewInt(n), exp)
140140

141141
// Check overflow
142-
if i.BitLen() > maxBitLen {
142+
if i.BitLen() > MaxBitLen {
143143
panic("NewIntWithDecimal() out of bound")
144144
}
145145
return Int{i}
@@ -151,11 +151,6 @@ func ZeroInt() Int { return Int{big.NewInt(0)} }
151151
// OneInt returns Int value with one
152152
func OneInt() Int { return Int{big.NewInt(1)} }
153153

154-
// ToDec converts Int to Dec
155-
func (i Int) ToDec() Dec {
156-
return NewDecFromInt(i)
157-
}
158-
159154
// Int64 converts Int to int64
160155
// Panics if the value is out of range
161156
func (i Int) Int64() int64 {
@@ -234,7 +229,7 @@ func (i Int) LTE(i2 Int) bool {
234229
func (i Int) Add(i2 Int) (res Int) {
235230
res = Int{add(i.i, i2.i)}
236231
// Check overflow
237-
if res.i.BitLen() > maxBitLen {
232+
if res.i.BitLen() > MaxBitLen {
238233
panic("Int overflow")
239234
}
240235
return
@@ -249,7 +244,7 @@ func (i Int) AddRaw(i2 int64) Int {
249244
func (i Int) Sub(i2 Int) (res Int) {
250245
res = Int{sub(i.i, i2.i)}
251246
// Check overflow
252-
if res.i.BitLen() > maxBitLen {
247+
if res.i.BitLen() > MaxBitLen {
253248
panic("Int overflow")
254249
}
255250
return
@@ -263,12 +258,12 @@ func (i Int) SubRaw(i2 int64) Int {
263258
// Mul multiples two Ints
264259
func (i Int) Mul(i2 Int) (res Int) {
265260
// Check overflow
266-
if i.i.BitLen()+i2.i.BitLen()-1 > maxBitLen {
261+
if i.i.BitLen()+i2.i.BitLen()-1 > MaxBitLen {
267262
panic("Int overflow")
268263
}
269264
res = Int{mul(i.i, i2.i)}
270265
// Check overflow if sign of both are same
271-
if res.i.BitLen() > maxBitLen {
266+
if res.i.BitLen() > MaxBitLen {
272267
panic("Int overflow")
273268
}
274269
return
@@ -416,8 +411,8 @@ func (i *Int) Unmarshal(data []byte) error {
416411
return err
417412
}
418413

419-
if i.i.BitLen() > maxBitLen {
420-
return fmt.Errorf("integer out of range; got: %d, max: %d", i.i.BitLen(), maxBitLen)
414+
if i.i.BitLen() > MaxBitLen {
415+
return fmt.Errorf("integer out of range; got: %d, max: %d", i.i.BitLen(), MaxBitLen)
421416
}
422417

423418
return nil
@@ -437,7 +432,3 @@ func (i *Int) UnmarshalAmino(bz []byte) error { return i.Unmarshal(bz) }
437432
func IntEq(t *testing.T, exp, got Int) (*testing.T, bool, string, string, string) {
438433
return t, exp.Equal(got), "expected:\t%v\ngot:\t\t%v", exp.String(), got.String()
439434
}
440-
441-
func (ip IntProto) String() string {
442-
return ip.Int.String()
443-
}

0 commit comments

Comments
 (0)