Skip to content

Commit

Permalink
chore: use sdkmath in misc packages (cosmos#14606)
Browse files Browse the repository at this point in the history
Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com>
Co-authored-by: Marko <marbar3778@yahoo.com>
  • Loading branch information
3 people committed Jan 16, 2023
1 parent ae1c29b commit f2b6013
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 64 deletions.
6 changes: 3 additions & 3 deletions baseapp/block_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"math"
"testing"

"cosmossdk.io/depinject"
sdkmath "cosmossdk.io/math"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"cosmossdk.io/depinject"

baseapptestutil "github.com/cosmos/cosmos-sdk/baseapp/testutil"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestBaseApp_BlockGas(t *testing.T) {
ctx := bapp.NewContext(false, tmproto.Header{})

// tx fee
feeCoin := sdk.NewCoin("atom", sdk.NewInt(150))
feeCoin := sdk.NewCoin("atom", sdkmath.NewInt(150))
feeAmount := sdk.NewCoins(feeCoin)

// test account and fund
Expand Down
3 changes: 2 additions & 1 deletion baseapp/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
"cosmossdk.io/core/appconfig"
"cosmossdk.io/depinject"
sdkmath "cosmossdk.io/math"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"
Expand Down Expand Up @@ -78,7 +79,7 @@ func GenesisStateWithSingleValidator(t *testing.T, codec codec.Codec, builder *r
balances := []banktypes.Balance{
{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000))),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(100000000000000))),
},
}

Expand Down
7 changes: 4 additions & 3 deletions client/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"testing"

"cosmossdk.io/depinject"
sdkmath "cosmossdk.io/math"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
Expand All @@ -13,7 +15,6 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"

"cosmossdk.io/depinject"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -66,7 +67,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0)
balance := types.Balance{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(s.genesisAccountBalance))),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(s.genesisAccountBalance))),
}

genesisState, err := sims.GenesisStateWithValSet(cdc, app.DefaultGenesis(), valSet, []authtypes.GenesisAccount{acc}, balance)
Expand Down Expand Up @@ -124,7 +125,7 @@ func (s *IntegrationTestSuite) TestGRPCQuery() {
)
s.Require().NoError(err)
bal := res.GetBalance()
s.Equal(sdk.NewCoin(denom, sdk.NewInt(s.genesisAccountBalance)), *bal)
s.Equal(sdk.NewCoin(denom, sdkmath.NewInt(s.genesisAccountBalance)), *bal)
}

func TestIntegrationTestSuite(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions client/tx/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"strings"
"testing"

"cosmossdk.io/depinject"
sdkmath "cosmossdk.io/math"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"

"cosmossdk.io/depinject"

"github.com/cosmos/cosmos-sdk/client"
clienttestutil "github.com/cosmos/cosmos-sdk/client/testutil"
"github.com/cosmos/cosmos-sdk/client/tx"
Expand Down Expand Up @@ -388,7 +388,7 @@ func TestPreprocessHook(t *testing.T) {

coin := sdk.Coin{
Denom: "atom",
Amount: sdk.NewInt(20),
Amount: sdkmath.NewInt(20),
}
newTip := &txtypes.Tip{
Amount: sdk.Coins{coin},
Expand Down
4 changes: 2 additions & 2 deletions fuzz/tests/types_dec_setstring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"strings"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkmath "cosmossdk.io/math"
)

func FuzzTypesDecSetString(f *testing.F) {
f.Fuzz(func(t *testing.T, b []byte) {
dec, err := sdk.NewDecFromStr(string(b))
dec, err := sdkmath.LegacyNewDecFromStr(string(b))
if err != nil {
return
}
Expand Down
13 changes: 6 additions & 7 deletions simapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"testing"

sdkmath "cosmossdk.io/math"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
Expand All @@ -14,8 +15,6 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"

"cosmossdk.io/math"

bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand Down Expand Up @@ -69,7 +68,7 @@ func NewSimappWithCustomOptions(t *testing.T, isCheckTx bool, options SetupOptio
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0)
balance := banktypes.Balance{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000))),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(100000000000000))),
}

app := NewSimApp(options.Logger, options.DB, nil, true, options.AppOpts)
Expand Down Expand Up @@ -112,7 +111,7 @@ func Setup(t *testing.T, isCheckTx bool) *SimApp {
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0)
balance := banktypes.Balance{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000))),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(100000000000000))),
}

app := SetupWithGenesisValSet(t, valSet, []authtypes.GenesisAccount{acc}, balance)
Expand Down Expand Up @@ -174,7 +173,7 @@ func GenesisStateWithSingleValidator(t *testing.T, app *SimApp) GenesisState {
balances := []banktypes.Balance{
{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000))),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(100000000000000))),
},
}

Expand All @@ -187,11 +186,11 @@ func GenesisStateWithSingleValidator(t *testing.T, app *SimApp) GenesisState {

// AddTestAddrsIncremental constructs and returns accNum amount of accounts with an
// initial balance of accAmt in random order
func AddTestAddrsIncremental(app *SimApp, ctx sdk.Context, accNum int, accAmt math.Int) []sdk.AccAddress {
func AddTestAddrsIncremental(app *SimApp, ctx sdk.Context, accNum int, accAmt sdkmath.Int) []sdk.AccAddress {
return addTestAddrs(app, ctx, accNum, accAmt, simtestutil.CreateIncrementalAccounts)
}

func addTestAddrs(app *SimApp, ctx sdk.Context, accNum int, accAmt math.Int, strategy simtestutil.GenerateAccountStrategy) []sdk.AccAddress {
func addTestAddrs(app *SimApp, ctx sdk.Context, accNum int, accAmt sdkmath.Int, strategy simtestutil.GenerateAccountStrategy) []sdk.AccAddress {
testAddrs := strategy(accNum)

initCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), accAmt))
Expand Down
25 changes: 12 additions & 13 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,16 @@ import (
"testing"
"time"

"cosmossdk.io/depinject"
sdkmath "cosmossdk.io/math"
dbm "github.com/cosmos/cosmos-db"
"github.com/spf13/cobra"
tmlog "github.com/tendermint/tendermint/libs/log"
tmrand "github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/node"
tmclient "github.com/tendermint/tendermint/rpc/client"
"google.golang.org/grpc"

"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/testutil/configurator"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
tmlog "github.com/tendermint/tendermint/libs/log"

"cosmossdk.io/depinject"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
Expand All @@ -43,6 +40,8 @@ import (
servertypes "github.com/cosmos/cosmos-sdk/server/types"
pruningtypes "github.com/cosmos/cosmos-sdk/store/pruning/types"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/configurator"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
_ "github.com/cosmos/cosmos-sdk/x/auth" // import auth as a blank
Expand Down Expand Up @@ -90,9 +89,9 @@ type Config struct {
Mnemonics []string // custom user-provided validator operator mnemonics
BondDenom string // the staking bond denomination
MinGasPrices string // the minimum gas prices each validator will accept
AccountTokens math.Int // the amount of unique validator tokens (e.g. 1000node0)
StakingTokens math.Int // the amount of tokens each validator has available to stake
BondedTokens math.Int // the amount of tokens each validator stakes
AccountTokens sdkmath.Int // the amount of unique validator tokens (e.g. 1000node0)
StakingTokens sdkmath.Int // the amount of tokens each validator has available to stake
BondedTokens sdkmath.Int // the amount of tokens each validator stakes
PruningStrategy string // the pruning strategy each validator will have
EnableTMLogging bool // enable Tendermint logging to STDOUT
CleanupDir bool // remove base temporary directory during cleanup
Expand Down Expand Up @@ -481,7 +480,7 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
genBalances = append(genBalances, banktypes.Balance{Address: addr.String(), Coins: balances.Sort()})
genAccounts = append(genAccounts, authtypes.NewBaseAccount(addr, nil, 0, 0))

commission, err := sdk.NewDecFromStr("0.5")
commission, err := sdkmath.LegacyNewDecFromStr("0.5")
if err != nil {
return nil, err
}
Expand All @@ -491,8 +490,8 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
valPubKeys[i],
sdk.NewCoin(cfg.BondDenom, cfg.BondedTokens),
stakingtypes.NewDescription(nodeDirName, "", "", "", ""),
stakingtypes.NewCommissionRates(commission, math.LegacyOneDec(), math.LegacyOneDec()),
math.OneInt(),
stakingtypes.NewCommissionRates(commission, sdkmath.LegacyOneDec(), sdkmath.LegacyOneDec()),
sdkmath.OneInt(),
)
if err != nil {
return nil, err
Expand All @@ -504,7 +503,7 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
}

memo := fmt.Sprintf("%s@%s:%s", nodeIDs[i], p2pURL.Hostname(), p2pURL.Port())
fee := sdk.NewCoins(sdk.NewCoin(fmt.Sprintf("%stoken", nodeDirName), sdk.NewInt(0)))
fee := sdk.NewCoins(sdk.NewCoin(fmt.Sprintf("%stoken", nodeDirName), sdkmath.NewInt(0)))
txBuilder := cfg.TxConfig.NewTxBuilder()
err = txBuilder.SetMsgs(createValMsg)
if err != nil {
Expand Down
15 changes: 7 additions & 8 deletions testutil/sims/app_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import (
"fmt"
"time"

"cosmossdk.io/depinject"
sdkmath "cosmossdk.io/math"
dbm "github.com/cosmos/cosmos-db"
abci "github.com/tendermint/tendermint/abci/types"
tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"

"cosmossdk.io/depinject"
"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -84,7 +83,7 @@ type StartupConfig struct {
func DefaultStartUpConfig() StartupConfig {
priv := secp256k1.GenPrivKey()
ba := authtypes.NewBaseAccount(priv.PubKey().Address().Bytes(), priv.PubKey(), 0, 0)
ga := GenesisAccount{ba, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000)))}
ga := GenesisAccount{ba, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(100000000000000)))}
return StartupConfig{
ValidatorSet: CreateRandomValidatorSet,
AtGenesis: false,
Expand Down Expand Up @@ -216,15 +215,15 @@ func GenesisStateWithValSet(
Jailed: false,
Status: stakingtypes.Bonded,
Tokens: bondAmt,
DelegatorShares: math.LegacyOneDec(),
DelegatorShares: sdkmath.LegacyOneDec(),
Description: stakingtypes.Description{},
UnbondingHeight: int64(0),
UnbondingTime: time.Unix(0, 0).UTC(),
Commission: stakingtypes.NewCommission(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec()),
MinSelfDelegation: math.ZeroInt(),
Commission: stakingtypes.NewCommission(sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec()),
MinSelfDelegation: sdkmath.ZeroInt(),
}
validators = append(validators, validator)
delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), math.LegacyOneDec()))
delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdkmath.LegacyOneDec()))

}

Expand Down
11 changes: 5 additions & 6 deletions tools/rosetta/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import (
"fmt"
"reflect"

"cosmossdk.io/math"
sdkmath "cosmossdk.io/math"
crgerrs "cosmossdk.io/tools/rosetta/lib/errors"
crgtypes "cosmossdk.io/tools/rosetta/lib/types"
"github.com/btcsuite/btcd/btcec/v2"
rosettatypes "github.com/coinbase/rosetta-sdk-go/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
tmcoretypes "github.com/tendermint/tendermint/rpc/core/types"
tmtypes "github.com/tendermint/tendermint/types"

crgerrs "cosmossdk.io/tools/rosetta/lib/errors"
crgtypes "cosmossdk.io/tools/rosetta/lib/types"

sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -404,7 +403,7 @@ func sdkEventToBalanceOperations(status string, event abci.Event) (operations []
// Amounts converts []sdk.Coin to rosetta amounts
func (c converter) Amounts(ownedCoins []sdk.Coin, availableCoins sdk.Coins) []*rosettatypes.Amount {
amounts := make([]*rosettatypes.Amount, len(availableCoins))
ownedCoinsMap := make(map[string]math.Int, len(availableCoins))
ownedCoinsMap := make(map[string]sdkmath.Int, len(availableCoins))

for _, ownedCoin := range ownedCoins {
ownedCoinsMap[ownedCoin.Denom] = ownedCoin.Amount
Expand All @@ -414,7 +413,7 @@ func (c converter) Amounts(ownedCoins []sdk.Coin, availableCoins sdk.Coins) []*r
value, owned := ownedCoinsMap[coin.Denom]
if !owned {
amounts[i] = &rosettatypes.Amount{
Value: sdk.NewInt(0).String(),
Value: sdkmath.NewInt(0).String(),
Currency: &rosettatypes.Currency{
Symbol: coin.Denom,
},
Expand Down
8 changes: 4 additions & 4 deletions tools/rosetta/lib/internal/service/construction.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"strconv"
"strings"

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

sdkmath "cosmossdk.io/math"
"cosmossdk.io/tools/rosetta/lib/errors"
"github.com/coinbase/rosetta-sdk-go/types"

"cosmossdk.io/tools/rosetta/lib/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// ConstructionCombine Combine creates a network-specific transaction from an unsigned transaction
Expand Down Expand Up @@ -96,7 +96,7 @@ func (on OnlineNetwork) ConstructionMetadata(ctx context.Context, request *types
if gasLimit == 0 { // gas_limit is unset. skip fee suggestion
return response, nil
}
gas := sdk.NewIntFromUint64(uint64(gasLimit))
gas := sdkmath.NewIntFromUint64(uint64(gasLimit))

suggestedFee := types.Amount{
Value: strconv.FormatInt(price.Amount.MulInt64(gas.Int64()).Ceil().TruncateInt64(), 10),
Expand Down
Loading

0 comments on commit f2b6013

Please sign in to comment.