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(math)!: remove aliases in math #16040

Merged
merged 6 commits into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
* The signature of `VerifySignature` has been changed to accept a `x/tx/signing.HandlerMap` and other structs from `x/tx` as arguments.
* The signature of `NewTxConfigWithTextual` has been deprecated and its signature changed to accept a `SignModeOptions`.
* (x/genutil) [#15999](https://github.com/cosmos/cosmos-sdk/pull/15999) Genutil now takes the `GenesisTxHanlder` interface instead of deliverTx. The interface is implemented on baseapp
* (types/math) [#16040](https://github.com/cosmos/cosmos-sdk/pull/16040) Remove unused aliases in math.go
* Remove aliases Uint, ZeroDec, OneDec, SmallestDec, NewDec, NewDecWithPrec, NewDecFromBigInt, NewDecFromInt, NewDecFromIntWithPrec, NewDecFromStr, MustNewDecFromStr, MaxSortableDec, ValidSortableDec, SortableDecBytes, DecsEqual, MinDec, MaxDec, DecEq, DecApproxEq, Precision, DecimalPrecisionsBits, NewIntFromBigInt, OneInt, NewInt, ZeroInt, IntEq, NewIntFromString, NewUint, NewIntFromUint64, MaxInt, MinInt, Uint
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved

### Client Breaking Changes

Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/gov/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"fmt"

"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/testutil"

"github.com/cosmos/gogoproto/proto"
Expand Down Expand Up @@ -373,7 +374,7 @@ func (s *E2ETestSuite) TestNewCmdCancelProposal() {
s.Require().NoError(err)
remainingAmount := v1.DefaultMinDepositTokens.Mul(
v1.DefaultProposalCancelRatio.Mul(sdk.MustNewDecFromStr("100")).TruncateInt(),
).Quo(sdk.NewIntFromUint64(100))
).Quo(math.NewIntFromUint64(100))

// new balance = old balance + remaining amount from proposal deposit - txFee (cancel proposal)
txFee := sdk.NewInt(10)
Expand Down
7 changes: 4 additions & 3 deletions tests/integration/aminojson/aminojson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
stakingapi "cosmossdk.io/api/cosmos/staking/v1beta1"
txv1beta1 "cosmossdk.io/api/cosmos/tx/v1beta1"
vestingapi "cosmossdk.io/api/cosmos/vesting/v1beta1"
"cosmossdk.io/math"
"cosmossdk.io/x/evidence"
feegrantmodule "cosmossdk.io/x/feegrant/module"
"cosmossdk.io/x/tx/signing/aminojson"
Expand Down Expand Up @@ -200,7 +201,7 @@ func TestAminoJSON_LegacyParity(t *testing.T) {
genericAuthPulsar := newAny(t, &authzapi.GenericAuthorization{Msg: "foo"})
pubkeyAny, _ := codectypes.NewAnyWithValue(&secp256k1types.PubKey{Key: []byte("foo")})
pubkeyAnyPulsar := newAny(t, &secp256k1.PubKey{Key: []byte("foo")})
dec10bz, _ := types.NewDec(10).Marshal()
dec10bz, _ := math.LegacyNewDec(10).Marshal()
int123bz, _ := types.NewInt(123).Marshal()

cases := map[string]struct {
Expand Down Expand Up @@ -251,7 +252,7 @@ func TestAminoJSON_LegacyParity(t *testing.T) {
pulsar: &distapi.DelegatorStartingInfo{},
},
"distribution/delegator_starting_info/non_zero_dec": {
gogo: &disttypes.DelegatorStartingInfo{Stake: types.NewDec(10)},
gogo: &disttypes.DelegatorStartingInfo{Stake: math.LegacyNewDec(10)},
pulsar: &distapi.DelegatorStartingInfo{Stake: "10.000000000000000000"},
protoUnmarshalFails: true,
},
Expand Down Expand Up @@ -331,7 +332,7 @@ func TestAminoJSON_LegacyParity(t *testing.T) {
"slashing/params/dec": {
gogo: &slashingtypes.Params{
DowntimeJailDuration: 1e9 + 7,
MinSignedPerWindow: types.NewDec(10),
MinSignedPerWindow: math.LegacyNewDec(10),
},
pulsar: &slashingapi.Params{
DowntimeJailDuration: &durationpb.Duration{Seconds: 1, Nanos: 7},
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/distribution/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func TestGRPCParams(t *testing.T) {
malleate: func() {
params = types.Params{
CommunityTax: sdk.NewDecWithPrec(3, 1),
BaseProposerReward: sdk.ZeroDec(),
BonusProposerReward: sdk.ZeroDec(),
BaseProposerReward: math.LegacyZeroDec(),
BonusProposerReward: math.LegacyZeroDec(),
WithdrawAddrEnabled: true,
}

Expand Down
20 changes: 10 additions & 10 deletions tests/integration/distribution/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,8 @@ func TestMsgUpdateParams(t *testing.T) {
Params: distrtypes.Params{
CommunityTax: sdk.NewDecWithPrec(2, 0),
WithdrawAddrEnabled: withdrawAddrEnabled,
BaseProposerReward: sdk.ZeroDec(),
BonusProposerReward: sdk.ZeroDec(),
BaseProposerReward: math.LegacyZeroDec(),
BonusProposerReward: math.LegacyZeroDec(),
},
},
expErr: true,
Expand All @@ -670,8 +670,8 @@ func TestMsgUpdateParams(t *testing.T) {
Params: distrtypes.Params{
CommunityTax: sdk.NewDecWithPrec(2, 0),
WithdrawAddrEnabled: withdrawAddrEnabled,
BaseProposerReward: sdk.ZeroDec(),
BonusProposerReward: sdk.ZeroDec(),
BaseProposerReward: math.LegacyZeroDec(),
BonusProposerReward: math.LegacyZeroDec(),
},
},
expErr: true,
Expand All @@ -684,8 +684,8 @@ func TestMsgUpdateParams(t *testing.T) {
Params: distrtypes.Params{
CommunityTax: sdk.NewDecWithPrec(-2, 1),
WithdrawAddrEnabled: withdrawAddrEnabled,
BaseProposerReward: sdk.ZeroDec(),
BonusProposerReward: sdk.ZeroDec(),
BaseProposerReward: math.LegacyZeroDec(),
BonusProposerReward: math.LegacyZeroDec(),
},
},
expErr: true,
Expand All @@ -698,7 +698,7 @@ func TestMsgUpdateParams(t *testing.T) {
Params: distrtypes.Params{
CommunityTax: communityTax,
BaseProposerReward: sdk.NewDecWithPrec(1, 2),
BonusProposerReward: sdk.ZeroDec(),
BonusProposerReward: math.LegacyZeroDec(),
WithdrawAddrEnabled: withdrawAddrEnabled,
},
},
Expand All @@ -711,7 +711,7 @@ func TestMsgUpdateParams(t *testing.T) {
Authority: f.distrKeeper.GetAuthority(),
Params: distrtypes.Params{
CommunityTax: communityTax,
BaseProposerReward: sdk.ZeroDec(),
BaseProposerReward: math.LegacyZeroDec(),
BonusProposerReward: sdk.NewDecWithPrec(1, 2),
WithdrawAddrEnabled: withdrawAddrEnabled,
},
Expand All @@ -725,8 +725,8 @@ func TestMsgUpdateParams(t *testing.T) {
Authority: f.distrKeeper.GetAuthority(),
Params: distrtypes.Params{
CommunityTax: communityTax,
BaseProposerReward: sdk.ZeroDec(),
BonusProposerReward: sdk.ZeroDec(),
BaseProposerReward: math.LegacyZeroDec(),
BonusProposerReward: math.LegacyZeroDec(),
WithdrawAddrEnabled: withdrawAddrEnabled,
},
},
Expand Down
9 changes: 5 additions & 4 deletions tests/integration/gov/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"testing"

"cosmossdk.io/math"
"gotest.tools/v3/assert"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -255,10 +256,10 @@ func TestLegacyGRPCQueryTally(t *testing.T) {
}

func v1TallyToV1Beta1Tally(t v1.TallyResult) v1beta1.TallyResult {
yes, _ := sdk.NewIntFromString(t.YesCount)
no, _ := sdk.NewIntFromString(t.NoCount)
noWithVeto, _ := sdk.NewIntFromString(t.NoWithVetoCount)
abstain, _ := sdk.NewIntFromString(t.AbstainCount)
yes, _ := math.NewIntFromString(t.YesCount)
no, _ := math.NewIntFromString(t.NoCount)
noWithVeto, _ := math.NewIntFromString(t.NoWithVetoCount)
abstain, _ := math.NewIntFromString(t.AbstainCount)
return v1beta1.TallyResult{
Yes: yes,
No: no,
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/staking/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"cosmossdk.io/log"
"cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"
"gotest.tools/v3/assert"

Expand Down Expand Up @@ -45,7 +46,7 @@ type fixture struct {
}

func init() {
sdk.DefaultPowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil))
sdk.DefaultPowerReduction = math.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil))
}

// intended to be used with require/assert: require.True(ValEq(...))
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/staking/keeper/unbonding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func doUnbondingDelegation(
notBondedAmt1 := bankKeeper.GetBalance(ctx, stakingKeeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount

var err error
undelegateAmount := sdk.NewDec(1)
undelegateAmount := math.LegacyNewDec(1)
completionTime, undelegatedAmount, err := stakingKeeper.Undelegate(ctx, addrDels[0], addrVals[0], undelegateAmount)
assert.NilError(t, err)
assert.Assert(t, undelegateAmount.Equal(math.LegacyNewDecFromInt(undelegatedAmount)))
Expand Down Expand Up @@ -128,13 +128,13 @@ func doRedelegation(
hookCalled *bool,
) (completionTime time.Time) {
var err error
completionTime, err = stakingKeeper.BeginRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1], sdk.NewDec(1))
completionTime, err = stakingKeeper.BeginRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1], math.LegacyNewDec(1))
assert.NilError(t, err)

// Check that the redelegation happened- we look up the entry and see that it has the correct number of shares
redelegations := stakingKeeper.GetRedelegationsFromSrcValidator(ctx, addrVals[0])
assert.Equal(t, 1, len(redelegations))
assert.DeepEqual(t, sdk.NewDec(1), redelegations[0].Entries[0].SharesDst)
assert.DeepEqual(t, math.LegacyNewDec(1), redelegations[0].Entries[0].SharesDst)

// check that our hook was called
assert.Assert(t, *hookCalled)
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/staking/keeper/validator_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -58,7 +59,7 @@ func BenchmarkGetValidatorDelegations(b *testing.B) {
delegator := sdk.AccAddress(fmt.Sprintf("address%d", i))
banktestutil.FundAccount(f.sdkCtx, f.bankKeeper, delegator,
sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(int64(i)))))
NewDel := types.NewDelegation(delegator, val, sdk.NewDec(int64(i)))
NewDel := types.NewDelegation(delegator, val, math.LegacyNewDec(int64(i)))
f.stakingKeeper.SetDelegation(f.sdkCtx, NewDel)
}
}
Expand Down Expand Up @@ -91,7 +92,7 @@ func BenchmarkGetValidatorDelegationsLegacy(b *testing.B) {
delegator := sdk.AccAddress(fmt.Sprintf("address%d", i))
banktestutil.FundAccount(f.sdkCtx, f.bankKeeper, delegator,
sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(int64(i)))))
NewDel := types.NewDelegation(delegator, val, sdk.NewDec(int64(i)))
NewDel := types.NewDelegation(delegator, val, math.LegacyNewDec(int64(i)))
f.stakingKeeper.SetDelegation(f.sdkCtx, NewDel)
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/staking/keeper/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestGetValidatorSortingUnmixed(t *testing.T) {

// initialize some validators into the state
amts := []math.Int{
sdk.NewIntFromUint64(0),
math.NewIntFromUint64(0),
f.stakingKeeper.PowerReduction(f.sdkCtx).MulRaw(100),
f.stakingKeeper.PowerReduction(f.sdkCtx),
f.stakingKeeper.PowerReduction(f.sdkCtx).MulRaw(400),
Expand Down Expand Up @@ -245,7 +245,7 @@ func TestGetValidatorSortingMixed(t *testing.T) {

// initialize some validators into the state
amts := []math.Int{
sdk.NewIntFromUint64(0),
math.NewIntFromUint64(0),
f.stakingKeeper.PowerReduction(f.sdkCtx).MulRaw(100),
f.stakingKeeper.PowerReduction(f.sdkCtx),
f.stakingKeeper.PowerReduction(f.sdkCtx).MulRaw(400),
Expand Down
3 changes: 2 additions & 1 deletion testutil/testnet/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"time"

"cosmossdk.io/math"
cmttypes "github.com/cometbft/cometbft/types"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -107,7 +108,7 @@ func (b *GenesisBuilder) GenTx(privVal secp256k1.PrivKey, val cmttypes.GenesisVa
MaxRate: sdk.MustNewDecFromStr("0.2"),
MaxChangeRate: sdk.MustNewDecFromStr("0.01"),
},
sdk.OneInt(),
math.OneInt(),
)
if err != nil {
panic(err)
Expand Down
7 changes: 4 additions & 3 deletions types/dec_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"cosmossdk.io/errors"
"cosmossdk.io/math"
)

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -328,7 +329,7 @@ func (coins DecCoins) Intersect(coinsB DecCoins) DecCoins {
for i, coin := range coins {
minCoin := DecCoin{
Denom: coin.Denom,
Amount: MinDec(coin.Amount, coinsB.AmountOf(coin.Denom)),
Amount: math.LegacyMinDec(coin.Amount, coinsB.AmountOf(coin.Denom)),
}
res[i] = minCoin
}
Expand Down Expand Up @@ -456,14 +457,14 @@ func (coins DecCoins) AmountOf(denom string) Dec {

switch len(coins) {
case 0:
return ZeroDec()
return math.LegacyZeroDec()

case 1:
coin := coins[0]
if coin.Denom == denom {
return coin.Amount
}
return ZeroDec()
return math.LegacyZeroDec()

default:
midIdx := len(coins) / 2 // 2:1, 3:1, 4:2
Expand Down
18 changes: 9 additions & 9 deletions types/dec_coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,34 +377,34 @@ func (s *decCoinTestSuite) TestParseDecCoins() {
{"", nil, false},
{"4stake", sdk.DecCoins{sdk.NewDecCoinFromDec("stake", sdk.NewDecFromInt(math.NewInt(4)))}, false},
{"5.5atom,4stake", sdk.DecCoins{
sdk.NewDecCoinFromDec("atom", sdk.NewDecWithPrec(5500000000000000000, sdk.Precision)),
sdk.NewDecCoinFromDec("atom", sdk.NewDecWithPrec(5500000000000000000, math.LegacyPrecision)),
sdk.NewDecCoinFromDec("stake", math.LegacyNewDec(4)),
}, false},
{"0.0stake", sdk.DecCoins{}, false}, // remove zero coins
{"10.0btc,1.0atom,20.0btc", nil, true},
{
"0.004STAKE",
sdk.DecCoins{sdk.NewDecCoinFromDec("STAKE", sdk.NewDecWithPrec(4000000000000000, sdk.Precision))},
sdk.DecCoins{sdk.NewDecCoinFromDec("STAKE", sdk.NewDecWithPrec(4000000000000000, math.LegacyPrecision))},
false,
},
{
"0.004stake",
sdk.DecCoins{sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(4000000000000000, sdk.Precision))},
sdk.DecCoins{sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(4000000000000000, math.LegacyPrecision))},
false,
},
{
"5.04atom,0.004stake",
sdk.DecCoins{
sdk.NewDecCoinFromDec("atom", sdk.NewDecWithPrec(5040000000000000000, sdk.Precision)),
sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(4000000000000000, sdk.Precision)),
sdk.NewDecCoinFromDec("atom", sdk.NewDecWithPrec(5040000000000000000, math.LegacyPrecision)),
sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(4000000000000000, math.LegacyPrecision)),
},
false,
},
{
"0.0stake,0.004stake,5.04atom", // remove zero coins
sdk.DecCoins{
sdk.NewDecCoinFromDec("atom", sdk.NewDecWithPrec(5040000000000000000, sdk.Precision)),
sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(4000000000000000, sdk.Precision)),
sdk.NewDecCoinFromDec("atom", sdk.NewDecWithPrec(5040000000000000000, math.LegacyPrecision)),
sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(4000000000000000, math.LegacyPrecision)),
},
false,
},
Expand All @@ -429,8 +429,8 @@ func (s *decCoinTestSuite) TestDecCoinsString() {
{sdk.DecCoins{}, ""},
{
sdk.DecCoins{
sdk.NewDecCoinFromDec("atom", sdk.NewDecWithPrec(5040000000000000000, sdk.Precision)),
sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(4000000000000000, sdk.Precision)),
sdk.NewDecCoinFromDec("atom", sdk.NewDecWithPrec(5040000000000000000, math.LegacyPrecision)),
sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(4000000000000000, math.LegacyPrecision)),
},
"5.040000000000000000atom,0.004000000000000000stake",
},
Expand Down
6 changes: 4 additions & 2 deletions types/denom.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package types

import (
"fmt"

"cosmossdk.io/math"
)

// denomUnits contains a mapping of denomination mapped to their respective unit
Expand Down Expand Up @@ -34,12 +36,12 @@ func RegisterDenom(denom string, unit Dec) error {
// is returned if the denomination is registered.
func GetDenomUnit(denom string) (Dec, bool) {
if err := ValidateDenom(denom); err != nil {
return ZeroDec(), false
return math.LegacyZeroDec(), false
}

unit, ok := denomUnits[denom]
if !ok {
return ZeroDec(), false
return math.LegacyZeroDec(), false
}

return unit, true
Expand Down
Loading