Skip to content

Commit

Permalink
refactor(staking, slashing, signing, misc): remove aliases from types…
Browse files Browse the repository at this point in the history
…/math.go (#15902)
  • Loading branch information
samricotta authored Apr 25, 2023
1 parent 91872ab commit e771568
Show file tree
Hide file tree
Showing 50 changed files with 374 additions and 356 deletions.
2 changes: 1 addition & 1 deletion server/grpc/gogoreflection/fix_registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func getExtension(extID int32, m proto.Message) *gogoproto.ExtensionDesc {
}

// check into proto registry
for id, desc := range proto.RegisteredExtensions(m) { //nolint:staticcheck // kept for backwards compatibility
for id, desc := range proto.RegisteredExtensions(m) { //nolint:staticcheck // keep for backward compatibility
if id == extID {
return &gogoproto.ExtensionDesc{
ExtendedType: desc.ExtendedType,
Expand Down
6 changes: 4 additions & 2 deletions types/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package types_test
import (
"testing"

sdkmath "cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -44,7 +46,7 @@ func BenchmarkUintMarshal(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, value := range values {
u := sdk.NewUint(value)
u := sdkmath.NewUint(value)
n, err := u.MarshalTo(scratch[:])
if err != nil {
b.Fatal(err)
Expand All @@ -69,7 +71,7 @@ func BenchmarkIntMarshal(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, value := range values {
in := sdk.NewInt(value)
in := sdkmath.NewInt(value)
n, err := in.MarshalTo(scratch[:])
if err != nil {
b.Fatal(err)
Expand Down
154 changes: 77 additions & 77 deletions types/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ func TestCoinTestSuite(t *testing.T) {
}

func (s *coinTestSuite) SetupSuite() {
zero := sdk.NewInt(0)
zero := math.NewInt(0)
one := math.OneInt()
two := sdk.NewInt(2)
four := sdk.NewInt(4)
two := math.NewInt(2)
four := math.NewInt(4)

s.ca0, s.ca1, s.ca2, s.ca4 = sdk.NewCoin(testDenom1, zero), sdk.NewCoin(testDenom1, one), sdk.NewCoin(testDenom1, two), sdk.NewCoin(testDenom1, four)
s.cm0, s.cm1, s.cm2, s.cm4 = sdk.NewCoin(testDenom2, zero), sdk.NewCoin(testDenom2, one), sdk.NewCoin(testDenom2, two), sdk.NewCoin(testDenom2, four)
Expand All @@ -44,15 +44,15 @@ func (s *coinTestSuite) SetupSuite() {

func (s *coinTestSuite) TestCoin() {
s.Require().Panics(func() { sdk.NewInt64Coin(testDenom1, -1) })
s.Require().Panics(func() { sdk.NewCoin(testDenom1, sdk.NewInt(-1)) })
s.Require().Equal(sdk.NewInt(10), sdk.NewInt64Coin(strings.ToUpper(testDenom1), 10).Amount)
s.Require().Equal(sdk.NewInt(10), sdk.NewCoin(strings.ToUpper(testDenom1), sdk.NewInt(10)).Amount)
s.Require().Equal(sdk.NewInt(5), sdk.NewInt64Coin(testDenom1, 5).Amount)
s.Require().Equal(sdk.NewInt(5), sdk.NewCoin(testDenom1, sdk.NewInt(5)).Amount)
s.Require().Panics(func() { sdk.NewCoin(testDenom1, math.NewInt(-1)) })
s.Require().Equal(math.NewInt(10), sdk.NewInt64Coin(strings.ToUpper(testDenom1), 10).Amount)
s.Require().Equal(math.NewInt(10), sdk.NewCoin(strings.ToUpper(testDenom1), math.NewInt(10)).Amount)
s.Require().Equal(math.NewInt(5), sdk.NewInt64Coin(testDenom1, 5).Amount)
s.Require().Equal(math.NewInt(5), sdk.NewCoin(testDenom1, math.NewInt(5)).Amount)
}

func (s *coinTestSuite) TestCoin_String() {
coin := sdk.NewCoin(testDenom1, sdk.NewInt(10))
coin := sdk.NewCoin(testDenom1, math.NewInt(10))
s.Require().Equal(fmt.Sprintf("10%s", testDenom1), coin.String())
}

Expand Down Expand Up @@ -86,8 +86,8 @@ func (s *coinTestSuite) TestCoinIsValid() {
coin sdk.Coin
expectPass bool
}{
{sdk.Coin{testDenom1, sdk.NewInt(-1)}, false},
{sdk.Coin{testDenom1, sdk.NewInt(0)}, true},
{sdk.Coin{testDenom1, math.NewInt(-1)}, false},
{sdk.Coin{testDenom1, math.NewInt(0)}, true},
{sdk.Coin{testDenom1, math.OneInt()}, true},
{sdk.Coin{"Atom", math.OneInt()}, true},
{sdk.Coin{"ATOM", math.OneInt()}, true},
Expand All @@ -114,11 +114,11 @@ func (s *coinTestSuite) TestCustomValidation() {
coin sdk.Coin
expectPass bool
}{
{sdk.Coin{"🙂", sdk.NewInt(1)}, true},
{sdk.Coin{"🙁", sdk.NewInt(1)}, true},
{sdk.Coin{"🌶", sdk.NewInt(1)}, false}, // outside the unicode range listed above
{sdk.Coin{"asdf", sdk.NewInt(1)}, false},
{sdk.Coin{"", sdk.NewInt(1)}, false},
{sdk.Coin{"🙂", math.NewInt(1)}, true},
{sdk.Coin{"🙁", math.NewInt(1)}, true},
{sdk.Coin{"🌶", math.NewInt(1)}, false}, // outside the unicode range listed above
{sdk.Coin{"asdf", math.NewInt(1)}, false},
{sdk.Coin{"", math.NewInt(1)}, false},
}

for i, tc := range cases {
Expand All @@ -133,8 +133,8 @@ func (s *coinTestSuite) TestCoinsDenoms() {
testOutput []string
expectPass bool
}{
{sdk.NewCoins(sdk.Coin{"ATOM", sdk.NewInt(1)}, sdk.Coin{"JUNO", sdk.NewInt(1)}, sdk.Coin{"OSMO", sdk.NewInt(1)}, sdk.Coin{"RAT", sdk.NewInt(1)}), []string{"ATOM", "JUNO", "OSMO", "RAT"}, true},
{sdk.NewCoins(sdk.Coin{"ATOM", sdk.NewInt(1)}, sdk.Coin{"JUNO", sdk.NewInt(1)}), []string{"ATOM"}, false},
{sdk.NewCoins(sdk.Coin{"ATOM", math.NewInt(1)}, sdk.Coin{"JUNO", math.NewInt(1)}, sdk.Coin{"OSMO", math.NewInt(1)}, sdk.Coin{"RAT", math.NewInt(1)}), []string{"ATOM", "JUNO", "OSMO", "RAT"}, true},
{sdk.NewCoins(sdk.Coin{"ATOM", math.NewInt(1)}, sdk.Coin{"JUNO", math.NewInt(1)}), []string{"ATOM"}, false},
}

for i, tc := range cases {
Expand Down Expand Up @@ -183,8 +183,8 @@ func (s *coinTestSuite) TestAddCoinAmount() {
amount math.Int
expected sdk.Coin
}{
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt(1), sdk.NewInt64Coin(testDenom1, 2)},
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt(0), sdk.NewInt64Coin(testDenom1, 1)},
{sdk.NewInt64Coin(testDenom1, 1), math.NewInt(1), sdk.NewInt64Coin(testDenom1, 2)},
{sdk.NewInt64Coin(testDenom1, 1), math.NewInt(0), sdk.NewInt64Coin(testDenom1, 1)},
}
for i, tc := range cases {
res := tc.coin.AddAmount(tc.amount)
Expand Down Expand Up @@ -232,11 +232,11 @@ func (s *coinTestSuite) TestSubCoinAmount() {
expected sdk.Coin
shouldPanic bool
}{
{sdk.NewInt64Coin(testDenom1, 2), sdk.NewInt(1), sdk.NewInt64Coin(testDenom1, 1), false},
{sdk.NewInt64Coin(testDenom1, 10), sdk.NewInt(1), sdk.NewInt64Coin(testDenom1, 9), false},
{sdk.NewInt64Coin(testDenom1, 5), sdk.NewInt(3), sdk.NewInt64Coin(testDenom1, 2), false},
{sdk.NewInt64Coin(testDenom1, 5), sdk.NewInt(0), sdk.NewInt64Coin(testDenom1, 5), false},
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt(5), sdk.Coin{}, true},
{sdk.NewInt64Coin(testDenom1, 2), math.NewInt(1), sdk.NewInt64Coin(testDenom1, 1), false},
{sdk.NewInt64Coin(testDenom1, 10), math.NewInt(1), sdk.NewInt64Coin(testDenom1, 9), false},
{sdk.NewInt64Coin(testDenom1, 5), math.NewInt(3), sdk.NewInt64Coin(testDenom1, 2), false},
{sdk.NewInt64Coin(testDenom1, 5), math.NewInt(0), sdk.NewInt64Coin(testDenom1, 5), false},
{sdk.NewInt64Coin(testDenom1, 1), math.NewInt(5), sdk.Coin{}, true},
}

for i, tc := range cases {
Expand All @@ -256,9 +256,9 @@ func (s *coinTestSuite) TestMulIntCoins() {
expected sdk.Coins
shouldPanic bool
}{
{sdk.Coins{s.ca2}, sdk.NewInt(0), sdk.Coins{s.ca0}, true},
{sdk.Coins{s.ca2}, sdk.NewInt(2), sdk.Coins{s.ca4}, false},
{sdk.Coins{s.ca1, s.cm2}, sdk.NewInt(2), sdk.Coins{s.ca2, s.cm4}, false},
{sdk.Coins{s.ca2}, math.NewInt(0), sdk.Coins{s.ca0}, true},
{sdk.Coins{s.ca2}, math.NewInt(2), sdk.Coins{s.ca4}, false},
{sdk.Coins{s.ca1, s.cm2}, math.NewInt(2), sdk.Coins{s.ca2, s.cm4}, false},
}

assert := s.Assert()
Expand All @@ -282,10 +282,10 @@ func (s *coinTestSuite) TestQuoIntCoins() {
isValid bool
shouldPanic bool
}{
{sdk.Coins{s.ca2, s.ca1}, sdk.NewInt(0), sdk.Coins{s.ca0, s.ca0}, true, true},
{sdk.Coins{s.ca2}, sdk.NewInt(4), sdk.Coins{s.ca0}, false, false},
{sdk.Coins{s.ca2, s.cm4}, sdk.NewInt(2), sdk.Coins{s.ca1, s.cm2}, true, false},
{sdk.Coins{s.ca4}, sdk.NewInt(2), sdk.Coins{s.ca2}, true, false},
{sdk.Coins{s.ca2, s.ca1}, math.NewInt(0), sdk.Coins{s.ca0, s.ca0}, true, true},
{sdk.Coins{s.ca2}, math.NewInt(4), sdk.Coins{s.ca0}, false, false},
{sdk.Coins{s.ca2, s.cm4}, math.NewInt(2), sdk.Coins{s.ca1, s.cm2}, true, false},
{sdk.Coins{s.ca4}, math.NewInt(2), sdk.Coins{s.ca2}, true, false},
}

assert := s.Assert()
Expand Down Expand Up @@ -410,10 +410,10 @@ func (s *coinTestSuite) TestFilteredZeroCoins() {
name: "all greater than zero",
input: sdk.Coins{
{"testa", math.OneInt()},
{"testb", sdk.NewInt(2)},
{"testc", sdk.NewInt(3)},
{"testd", sdk.NewInt(4)},
{"teste", sdk.NewInt(5)},
{"testb", math.NewInt(2)},
{"testc", math.NewInt(3)},
{"testd", math.NewInt(4)},
{"teste", math.NewInt(5)},
},
original: "1testa,2testb,3testc,4testd,5teste",
expected: "1testa,2testb,3testc,4testd,5teste",
Expand All @@ -422,22 +422,22 @@ func (s *coinTestSuite) TestFilteredZeroCoins() {
name: "zero coin in middle",
input: sdk.Coins{
{"testa", math.OneInt()},
{"testb", sdk.NewInt(2)},
{"testc", sdk.NewInt(0)},
{"testd", sdk.NewInt(4)},
{"teste", sdk.NewInt(5)},
{"testb", math.NewInt(2)},
{"testc", math.NewInt(0)},
{"testd", math.NewInt(4)},
{"teste", math.NewInt(5)},
},
original: "1testa,2testb,0testc,4testd,5teste",
expected: "1testa,2testb,4testd,5teste",
},
{
name: "zero coin end (unordered)",
input: sdk.Coins{
{"teste", sdk.NewInt(5)},
{"testc", sdk.NewInt(3)},
{"teste", math.NewInt(5)},
{"testc", math.NewInt(3)},
{"testa", math.OneInt()},
{"testd", sdk.NewInt(4)},
{"testb", sdk.NewInt(0)},
{"testd", math.NewInt(4)},
{"testb", math.NewInt(0)},
},
original: "5teste,3testc,1testa,4testd,0testb",
expected: "1testa,3testc,4testd,5teste",
Expand Down Expand Up @@ -584,21 +584,21 @@ func (s *coinTestSuite) TestAddCoins() {
// are correctly coalesced. Please see issue https://github.com/cosmos/cosmos-sdk/issues/13234
func TestCoinsAddCoalescesDuplicateDenominations(t *testing.T) {
A := sdk.Coins{
{"den", sdk.NewInt(2)},
{"den", sdk.NewInt(3)},
{"den", math.NewInt(2)},
{"den", math.NewInt(3)},
}
B := sdk.Coins{
{"den", sdk.NewInt(3)},
{"den", sdk.NewInt(2)},
{"den", sdk.NewInt(1)},
{"den", math.NewInt(3)},
{"den", math.NewInt(2)},
{"den", math.NewInt(1)},
}

A = A.Sort()
B = B.Sort()
got := A.Add(B...)

want := sdk.Coins{
{"den", sdk.NewInt(11)},
{"den", math.NewInt(11)},
}

if !got.Equal(want) {
Expand Down Expand Up @@ -758,7 +758,7 @@ func (s *coinTestSuite) TestCoins_Validate() {
"IBC denominations (ADR 001)",
sdk.Coins{
{"ibc/7F1D3FCF4AE79E1554D670D1AD949A9BA4E4A3C76C63093E17E446A46061A7A2", math.OneInt()},
{"ibc/876563AAAACF739EB061C67CDB5EDF2B7C9FD4AA9D876450CC21210807C2820A", sdk.NewInt(2)},
{"ibc/876563AAAACF739EB061C67CDB5EDF2B7C9FD4AA9D876450CC21210807C2820A", math.NewInt(2)},
},
true,
},
Expand Down Expand Up @@ -820,15 +820,15 @@ func (s *coinTestSuite) TestCoins_Validate() {
"non-positive amount (1)",
sdk.Coins{
{"gas", math.OneInt()},
{"tree", sdk.NewInt(0)},
{"tree", math.NewInt(0)},
{"mineral", math.OneInt()},
},
false,
},
{
"non-positive amount (2)",
sdk.Coins{
{"gas", sdk.NewInt(-1)},
{"gas", math.NewInt(-1)},
{"tree", math.OneInt()},
{"mineral", math.OneInt()},
},
Expand Down Expand Up @@ -875,7 +875,7 @@ func (s *coinTestSuite) TestCoins_Validate() {

func (s *coinTestSuite) TestMinMax() {
one := math.OneInt()
two := sdk.NewInt(2)
two := math.NewInt(2)

cases := []struct {
name string
Expand Down Expand Up @@ -907,7 +907,7 @@ func (s *coinTestSuite) TestMinMax() {

func (s *coinTestSuite) TestCoinsGT() {
one := math.OneInt()
two := sdk.NewInt(2)
two := math.NewInt(2)

s.Require().False(sdk.Coins{}.IsAllGT(sdk.Coins{}))
s.Require().True(sdk.Coins{{testDenom1, one}}.IsAllGT(sdk.Coins{}))
Expand All @@ -919,7 +919,7 @@ func (s *coinTestSuite) TestCoinsGT() {

func (s *coinTestSuite) TestCoinsLT() {
one := math.OneInt()
two := sdk.NewInt(2)
two := math.NewInt(2)

s.Require().False(sdk.Coins{}.IsAllLT(sdk.Coins{}))
s.Require().False(sdk.Coins{{testDenom1, one}}.IsAllLT(sdk.Coins{}))
Expand All @@ -934,7 +934,7 @@ func (s *coinTestSuite) TestCoinsLT() {

func (s *coinTestSuite) TestCoinsLTE() {
one := math.OneInt()
two := sdk.NewInt(2)
two := math.NewInt(2)

s.Require().True(sdk.Coins{}.IsAllLTE(sdk.Coins{}))
s.Require().False(sdk.Coins{{testDenom1, one}}.IsAllLTE(sdk.Coins{}))
Expand All @@ -957,22 +957,22 @@ func (s *coinTestSuite) TestParseCoins() {
}{
{"", true, nil},
{"0stake", true, sdk.Coins{}}, // remove zero coins
{"0stake,1foo,99bar", true, sdk.Coins{{"bar", sdk.NewInt(99)}, {"foo", one}}}, // remove zero coins
{"0stake,1foo,99bar", true, sdk.Coins{{"bar", math.NewInt(99)}, {"foo", one}}}, // remove zero coins
{"1foo", true, sdk.Coins{{"foo", one}}},
{"10btc,1atom,20btc", false, nil},
{"10bar", true, sdk.Coins{{"bar", sdk.NewInt(10)}}},
{"99bar,1foo", true, sdk.Coins{{"bar", sdk.NewInt(99)}, {"foo", one}}},
{"98 bar , 1 foo ", true, sdk.Coins{{"bar", sdk.NewInt(98)}, {"foo", one}}},
{" 55\t \t bling\n", true, sdk.Coins{{"bling", sdk.NewInt(55)}}},
{"2foo, 97 bar", true, sdk.Coins{{"bar", sdk.NewInt(97)}, {"foo", sdk.NewInt(2)}}},
{"5 mycoin,", false, nil}, // no empty coins in a list
{"2 3foo, 97 bar", false, nil}, // 3foo is invalid coin name
{"11me coin, 12you coin", false, nil}, // no spaces in coin names
{"1.2btc", true, sdk.Coins{{"btc", sdk.NewInt(1)}}}, // amount can be decimal, will get truncated
{"5foo:bar", true, sdk.Coins{{"foo:bar", sdk.NewInt(5)}}},
{"10atom10", true, sdk.Coins{{"atom10", sdk.NewInt(10)}}},
{"200transfer/channelToA/uatom", true, sdk.Coins{{"transfer/channelToA/uatom", sdk.NewInt(200)}}},
{"50ibc/7F1D3FCF4AE79E1554D670D1AD949A9BA4E4A3C76C63093E17E446A46061A7A2", true, sdk.Coins{{"ibc/7F1D3FCF4AE79E1554D670D1AD949A9BA4E4A3C76C63093E17E446A46061A7A2", sdk.NewInt(50)}}},
{"10bar", true, sdk.Coins{{"bar", math.NewInt(10)}}},
{"99bar,1foo", true, sdk.Coins{{"bar", math.NewInt(99)}, {"foo", one}}},
{"98 bar , 1 foo ", true, sdk.Coins{{"bar", math.NewInt(98)}, {"foo", one}}},
{" 55\t \t bling\n", true, sdk.Coins{{"bling", math.NewInt(55)}}},
{"2foo, 97 bar", true, sdk.Coins{{"bar", math.NewInt(97)}, {"foo", math.NewInt(2)}}},
{"5 mycoin,", false, nil}, // no empty coins in a list
{"2 3foo, 97 bar", false, nil}, // 3foo is invalid coin name
{"11me coin, 12you coin", false, nil}, // no spaces in coin names
{"1.2btc", true, sdk.Coins{{"btc", math.NewInt(1)}}}, // amount can be decimal, will get truncated
{"5foo:bar", true, sdk.Coins{{"foo:bar", math.NewInt(5)}}},
{"10atom10", true, sdk.Coins{{"atom10", math.NewInt(10)}}},
{"200transfer/channelToA/uatom", true, sdk.Coins{{"transfer/channelToA/uatom", math.NewInt(200)}}},
{"50ibc/7F1D3FCF4AE79E1554D670D1AD949A9BA4E4A3C76C63093E17E446A46061A7A2", true, sdk.Coins{{"ibc/7F1D3FCF4AE79E1554D670D1AD949A9BA4E4A3C76C63093E17E446A46061A7A2", math.NewInt(50)}}},
{"120000000000000000000000000000000000000000000000000000000000000000000000000000btc", false, nil},
}

Expand Down Expand Up @@ -1085,9 +1085,9 @@ func (s *coinTestSuite) TestSearch() {

s.Run("AmountOf", func() {
for i, tc := range amountOfCases {
require.Equal(sdk.NewInt(tc.amountOfGAS), tc.coins.AmountOf("gas"), i)
require.Equal(sdk.NewInt(tc.amountOfMINERAL), tc.coins.AmountOf("mineral"), i)
require.Equal(sdk.NewInt(tc.amountOfTREE), tc.coins.AmountOf("tree"), i)
require.Equal(math.NewInt(tc.amountOfGAS), tc.coins.AmountOf("gas"), i)
require.Equal(math.NewInt(tc.amountOfMINERAL), tc.coins.AmountOf("mineral"), i)
require.Equal(math.NewInt(tc.amountOfTREE), tc.coins.AmountOf("tree"), i)
}
require.Panics(func() { amountOfCases[0].coins.AmountOf("10Invalid") })
})
Expand Down Expand Up @@ -1119,7 +1119,7 @@ func (s *coinTestSuite) TestSearch() {

func (s *coinTestSuite) TestCoinsIsAnyGTE() {
one := math.OneInt()
two := sdk.NewInt(2)
two := math.NewInt(2)

s.Require().False(sdk.Coins{}.IsAnyGTE(sdk.Coins{}))
s.Require().False(sdk.Coins{{testDenom1, one}}.IsAnyGTE(sdk.Coins{}))
Expand All @@ -1139,7 +1139,7 @@ func (s *coinTestSuite) TestCoinsIsAnyGTE() {

func (s *coinTestSuite) TestCoinsIsAllGT() {
one := math.OneInt()
two := sdk.NewInt(2)
two := math.NewInt(2)

s.Require().False(sdk.Coins{}.IsAllGT(sdk.Coins{}))
s.Require().True(sdk.Coins{{testDenom1, one}}.IsAllGT(sdk.Coins{}))
Expand All @@ -1159,7 +1159,7 @@ func (s *coinTestSuite) TestCoinsIsAllGT() {

func (s *coinTestSuite) TestCoinsIsAllGTE() {
one := math.OneInt()
two := sdk.NewInt(2)
two := math.NewInt(2)

s.Require().True(sdk.Coins{}.IsAllGTE(sdk.Coins{}))
s.Require().True(sdk.Coins{{testDenom1, one}}.IsAllGTE(sdk.Coins{}))
Expand Down
Loading

0 comments on commit e771568

Please sign in to comment.