Skip to content

Commit

Permalink
refactor!: create math go sub module (#11788)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored Apr 28, 2022
1 parent 597ab54 commit 4f04bea
Show file tree
Hide file tree
Showing 37 changed files with 626 additions and 522 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### API Breaking Changes

* (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.
* (grpc) [\#11642](https://github.com/cosmos/cosmos-sdk/pull/11642) The `RegisterTendermintService` method in the `tmservice` package now requires a `abciQueryFn` query function parameter.
* [\#11496](https://github.com/cosmos/cosmos-sdk/pull/11496) Refactor abstractions for snapshot and pruning; snapshot intervals eventually pruned; unit tests.
* (types) [\#11689](https://github.com/cosmos/cosmos-sdk/pull/11689) Make `Coins#Sub` and `Coins#SafeSub` consistent with `Coins#Add`.
Expand Down
4 changes: 4 additions & 0 deletions contrib/images/simd-dlv/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ FROM golang:1.18-alpine AS build

RUN apk add build-base git linux-headers libc-dev
RUN go install github.com/go-delve/delve/cmd/dlv@latest

WORKDIR /work
COPY go.mod go.sum /work/
COPY db/go.mod db/go.sum /work/db/
COPY errors/go.mod errors/go.sum /work/errors/
COPY math/go.mod math/go.sum /work/math/

RUN go mod download
COPY ./ /work
RUN LEDGER_ENABLED=false make COSMOS_BUILD_OPTIONS="debug,nostrip" clean build
Expand Down
3 changes: 3 additions & 0 deletions contrib/images/simd-env/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
FROM golang:1.18-alpine AS build

RUN apk add build-base git linux-headers

WORKDIR /work
COPY go.mod go.sum /work/
COPY db/go.mod db/go.sum /work/db/
COPY errors/go.mod errors/go.sum /work/errors/
COPY math/go.mod math/go.sum /work/math/

RUN go mod download
COPY ./ /work
Expand Down
7 changes: 1 addition & 6 deletions errors/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,14 @@ Change log entries are to be added to the Unreleased section under the
appropriate stanza (see below). Each entry should ideally include a tag and
the Github issue reference in the following format:
* (<tag>) \#<issue-number> message
The issue numbers will later be link-ified during the release process so you do
not have to worry about including a link manually, but you can if you wish.
* (<tag>) [#<issue-number>] Changelog message.
Types of changes (Stanzas):
"Features" for new features.
"Improvements" for changes in existing functionality.
"Deprecated" for soon-to-be removed features.
"Bug Fixes" for any bug fixes.
"Client Breaking" for breaking Protobuf, gRPC and REST routes used by end-users.
"CLI Breaking" for breaking CLI commands.
"API Breaking" for breaking exported APIs used by developers building on SDK.
Ref: https://keepachangelog.com/en/1.0.0/
-->
Expand Down
17 changes: 10 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/cosmos/cosmos-sdk/api v0.1.0
github.com/cosmos/cosmos-sdk/db v1.0.0-beta.1
github.com/cosmos/cosmos-sdk/errors v1.0.0-beta.5
github.com/cosmos/cosmos-sdk/math v0.0.0-00010101000000-000000000000
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/iavl v0.18.0
github.com/cosmos/ledger-cosmos-go v0.11.1
Expand Down Expand Up @@ -149,14 +150,16 @@ require (
nhooyr.io/websocket v1.8.6 // indirect
)

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

replace github.com/99designs/keyring => github.com/cosmos/keyring v1.1.7-0.20210622111912-ef00f8ac3d76
github.com/cosmos/cosmos-sdk/db => ./db
github.com/cosmos/cosmos-sdk/math => ./math

// Fix upstream GHSA-h395-qcrw-5vmq vulnerability.
// TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409
replace github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.7.0

replace github.com/cosmos/cosmos-sdk/db => ./db
// Fix upstream GHSA-h395-qcrw-5vmq vulnerability.
// TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.7.0
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
)

retract v0.43.0
32 changes: 32 additions & 0 deletions math/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!--
Guiding Principles:
Changelogs are for humans, not machines.
There should be an entry for every single version.
The same types of changes should be grouped.
Versions and sections should be linkable.
The latest version comes first.
The release date of each version is displayed.
Mention whether you follow Semantic Versioning.
Usage:
Change log entries are to be added to the Unreleased section under the
appropriate stanza (see below). Each entry should ideally include a tag and
the Github issue reference in the following format:
* (<tag>) [#<issue-number>] Changelog message.
Types of changes (Stanzas):
"Features" for new features.
"Improvements" for changes in existing functionality.
"Deprecated" for soon-to-be removed features.
"Bug Fixes" for any bug fixes.
"API Breaking" for breaking exported APIs used by developers building on SDK.
Ref: https://keepachangelog.com/en/1.0.0/
-->

# Changelog

## [Unreleased]
6 changes: 6 additions & 0 deletions math/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
Package math implements custom Cosmos SDK math types used for arithmetic
operations. Signed and unsigned integer types utilize Golang's standard library
big integers types, having a maximum bit length of 256 bits.
*/
package math
11 changes: 11 additions & 0 deletions math/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/cosmos/cosmos-sdk/math

go 1.18

require github.com/stretchr/testify v1.7.0

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
11 changes: 11 additions & 0 deletions math/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
39 changes: 15 additions & 24 deletions types/int.go → math/int.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package types
package math

import (
"encoding"
"encoding/json"
"fmt"
"testing"

"math/big"
"testing"
)

const maxBitLen = 256
// MaxBitLen defines the maximum bit length supported bit Int and Uint types.
const MaxBitLen = 256

func newIntegerFromString(s string) (*big.Int, bool) {
return new(big.Int).SetString(s, 0)
Expand Down Expand Up @@ -60,14 +60,14 @@ func unmarshalText(i *big.Int, text string) error {
return err
}

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

return nil
}

var _ CustomProtobufType = (*Int)(nil)
var _ customProtobufType = (*Int)(nil)

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

if i.BitLen() > maxBitLen {
if i.BitLen() > MaxBitLen {
panic("NewIntFromBigInt() out of bound")
}
return Int{i}
Expand All @@ -121,7 +121,7 @@ func NewIntFromString(s string) (res Int, ok bool) {
return
}
// Check overflow
if i.BitLen() > maxBitLen {
if i.BitLen() > MaxBitLen {
ok = false
return
}
Expand All @@ -139,7 +139,7 @@ func NewIntWithDecimal(n int64, dec int) Int {
i.Mul(big.NewInt(n), exp)

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

// ToDec converts Int to Dec
func (i Int) ToDec() Dec {
return NewDecFromInt(i)
}

// Int64 converts Int to int64
// Panics if the value is out of range
func (i Int) Int64() int64 {
Expand Down Expand Up @@ -234,7 +229,7 @@ func (i Int) LTE(i2 Int) bool {
func (i Int) Add(i2 Int) (res Int) {
res = Int{add(i.i, i2.i)}
// Check overflow
if res.i.BitLen() > maxBitLen {
if res.i.BitLen() > MaxBitLen {
panic("Int overflow")
}
return
Expand All @@ -249,7 +244,7 @@ func (i Int) AddRaw(i2 int64) Int {
func (i Int) Sub(i2 Int) (res Int) {
res = Int{sub(i.i, i2.i)}
// Check overflow
if res.i.BitLen() > maxBitLen {
if res.i.BitLen() > MaxBitLen {
panic("Int overflow")
}
return
Expand All @@ -263,12 +258,12 @@ func (i Int) SubRaw(i2 int64) Int {
// Mul multiples two Ints
func (i Int) Mul(i2 Int) (res Int) {
// Check overflow
if i.i.BitLen()+i2.i.BitLen()-1 > maxBitLen {
if i.i.BitLen()+i2.i.BitLen()-1 > MaxBitLen {
panic("Int overflow")
}
res = Int{mul(i.i, i2.i)}
// Check overflow if sign of both are same
if res.i.BitLen() > maxBitLen {
if res.i.BitLen() > MaxBitLen {
panic("Int overflow")
}
return
Expand Down Expand Up @@ -416,8 +411,8 @@ func (i *Int) Unmarshal(data []byte) error {
return err
}

if i.i.BitLen() > maxBitLen {
return fmt.Errorf("integer out of range; got: %d, max: %d", i.i.BitLen(), maxBitLen)
if i.i.BitLen() > MaxBitLen {
return fmt.Errorf("integer out of range; got: %d, max: %d", i.i.BitLen(), MaxBitLen)
}

return nil
Expand All @@ -437,7 +432,3 @@ func (i *Int) UnmarshalAmino(bz []byte) error { return i.Unmarshal(bz) }
func IntEq(t *testing.T, exp, got Int) (*testing.T, bool, string, string, string) {
return t, exp.Equal(got), "expected:\t%v\ngot:\t\t%v", exp.String(), got.String()
}

func (ip IntProto) String() string {
return ip.Int.String()
}
2 changes: 1 addition & 1 deletion types/int_internal_test.go → math/int_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package math

import (
"math/big"
Expand Down
Loading

0 comments on commit 4f04bea

Please sign in to comment.