chore: use sdkmath in misc packages (#14606)

Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com>
Co-authored-by: Marko <marbar3778@yahoo.com>
This commit is contained in:
Aleksandr Bezobchuk 2023-01-16 07:48:32 -05:00 committed by GitHub
parent ae1c29b8f8
commit f2b6013cee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 67 additions and 64 deletions

View File

@ -5,6 +5,8 @@ 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"
@ -12,8 +14,6 @@ import (
"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"
@ -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

View File

@ -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"
@ -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))),
},
}

View File

@ -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"
@ -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"
@ -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)
@ -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) {

View File

@ -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"
@ -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},

View File

@ -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
}

View File

@ -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"
@ -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"
@ -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)
@ -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)
@ -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))),
},
}
@ -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))

View File

@ -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"
@ -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
@ -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
@ -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
}
@ -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
@ -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 {

View File

@ -5,6 +5,8 @@ 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"
@ -12,9 +14,6 @@ import (
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"
@ -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,
@ -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()))
}

View File

@ -6,7 +6,9 @@ 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"
@ -14,9 +16,6 @@ import (
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"
@ -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
@ -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,
},

View File

@ -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
@ -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),

View File

@ -7,7 +7,8 @@ import (
"time"
"unsafe"
"cosmossdk.io/math"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@ -41,19 +42,19 @@ func RandStringOfLength(r *rand.Rand, n int) string {
}
// RandPositiveInt get a rand positive math.Int
func RandPositiveInt(r *rand.Rand, max math.Int) (math.Int, error) {
if !max.GTE(math.OneInt()) {
return math.Int{}, errors.New("max too small")
func RandPositiveInt(r *rand.Rand, max sdkmath.Int) (sdkmath.Int, error) {
if !max.GTE(sdkmath.OneInt()) {
return sdkmath.Int{}, errors.New("max too small")
}
max = max.Sub(math.OneInt())
max = max.Sub(sdkmath.OneInt())
return sdk.NewIntFromBigInt(new(big.Int).Rand(r, max.BigInt())).Add(math.OneInt()), nil
return sdkmath.NewIntFromBigInt(new(big.Int).Rand(r, max.BigInt())).Add(sdkmath.OneInt()), nil
}
// RandomAmount generates a random amount
// Note: The range of RandomAmount includes max, and is, in fact, biased to return max as well as 0.
func RandomAmount(r *rand.Rand, max math.Int) math.Int {
func RandomAmount(r *rand.Rand, max sdkmath.Int) sdkmath.Int {
randInt := big.NewInt(0)
switch r.Intn(10) {
@ -65,12 +66,12 @@ func RandomAmount(r *rand.Rand, max math.Int) math.Int {
randInt = big.NewInt(0).Rand(r, max.BigInt()) // up to max - 1
}
return sdk.NewIntFromBigInt(randInt)
return sdkmath.NewIntFromBigInt(randInt)
}
// RandomDecAmount generates a random decimal amount
// Note: The range of RandomDecAmount includes max, and is, in fact, biased to return max as well as 0.
func RandomDecAmount(r *rand.Rand, max sdk.Dec) math.LegacyDec {
func RandomDecAmount(r *rand.Rand, max sdkmath.LegacyDec) sdkmath.LegacyDec {
randInt := big.NewInt(0)
switch r.Intn(10) {
@ -82,7 +83,7 @@ func RandomDecAmount(r *rand.Rand, max sdk.Dec) math.LegacyDec {
randInt = big.NewInt(0).Rand(r, max.BigInt())
}
return sdk.NewDecFromBigIntWithPrec(randInt, sdk.Precision)
return sdkmath.LegacyNewDecFromBigIntWithPrec(randInt, sdkmath.LegacyPrecision)
}
// RandTimestamp generates a random timestamp

View File

@ -1,5 +1,9 @@
package types
import (
sdkmath "cosmossdk.io/math"
)
// Delay, in blocks, between when validator updates are returned to the
// consensus-engine and when they are applied. For example, if
// ValidatorUpdateDelay is set to X, and if a validator set update is
@ -17,15 +21,15 @@ var (
DefaultBondDenom = "stake"
// DefaultPowerReduction is the default amount of staking tokens required for 1 unit of consensus-engine power
DefaultPowerReduction = NewIntFromUint64(1000000)
DefaultPowerReduction = sdkmath.NewIntFromUint64(1000000)
)
// TokensToConsensusPower - convert input tokens to potential consensus-engine power
func TokensToConsensusPower(tokens Int, powerReduction Int) int64 {
func TokensToConsensusPower(tokens sdkmath.Int, powerReduction sdkmath.Int) int64 {
return (tokens.Quo(powerReduction)).Int64()
}
// TokensFromConsensusPower - convert input power to tokens
func TokensFromConsensusPower(power int64, powerReduction Int) Int {
return NewInt(power).Mul(powerReduction)
func TokensFromConsensusPower(power int64, powerReduction sdkmath.Int) sdkmath.Int {
return sdkmath.NewInt(power).Mul(powerReduction)
}