chore: use math.Int instead of math.Int and apply linting (#12702)

* linting round

* fix changelog

* revert docs changes

* Update CHANGELOG.md

* revert an accidental change to security.md

* Update simapp/integration/client/grpc/tmservice/service_test.go

Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
Jacob Gadikian
2022-07-26 00:24:47 +02:00
committed by GitHub
co-authored by Julien Robert
parent e7bd66841d
commit 5decd22cd3
84 changed files with 310 additions and 263 deletions
+3 -2
View File
@@ -3,6 +3,7 @@ package staking_test
import (
"testing"
"cosmossdk.io/math"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
@@ -65,7 +66,7 @@ func TestStakingMsgs(t *testing.T) {
// create validator
description := types.NewDescription("foo_moniker", "", "", "", "")
createValidatorMsg, err := types.NewMsgCreateValidator(
sdk.ValAddress(addr1), valKey.PubKey(), bondCoin, description, commissionRates, sdk.OneInt(),
sdk.ValAddress(addr1), valKey.PubKey(), bondCoin, description, commissionRates, math.OneInt(),
)
require.NoError(t, err)
@@ -81,7 +82,7 @@ func TestStakingMsgs(t *testing.T) {
validator := checkValidator(t, app, sdk.ValAddress(addr1), true)
require.Equal(t, sdk.ValAddress(addr1).String(), validator.OperatorAddress)
require.Equal(t, types.Bonded, validator.Status)
require.True(sdk.IntEq(t, bondTokens, validator.BondedTokens()))
require.True(math.IntEq(t, bondTokens, validator.BondedTokens()))
header = tmproto.Header{Height: app.LastBlockHeight() + 1}
app.BeginBlock(abci.RequestBeginBlock{Header: header})
+5 -3
View File
@@ -1,11 +1,13 @@
package staking_test
import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"math/big"
"testing"
"cosmossdk.io/math"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec"
@@ -58,7 +60,7 @@ func getBaseSimappWithCustomKeeper(t *testing.T) (*codec.LegacyAmino, *simapp.Si
}
// generateAddresses generates numAddrs of normal AccAddrs and ValAddrs
func generateAddresses(app *simapp.SimApp, ctx sdk.Context, numAddrs int, accAmount sdk.Int) ([]sdk.AccAddress, []sdk.ValAddress) {
func generateAddresses(app *simapp.SimApp, ctx sdk.Context, numAddrs int, accAmount math.Int) ([]sdk.AccAddress, []sdk.ValAddress) {
addrDels := simapp.AddTestAddrsIncremental(app, ctx, numAddrs, accAmount)
addrVals := simtestutil.ConvertAddrsToValAddrs(addrDels)
+2 -1
View File
@@ -3,6 +3,7 @@ package staking_test
import (
"testing"
"cosmossdk.io/math"
"github.com/stretchr/testify/assert"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
@@ -16,7 +17,7 @@ func TestValidateGenesis(t *testing.T) {
genValidators1 := make([]types.Validator, 1, 5)
pk := ed25519.GenPrivKey().PubKey()
genValidators1[0] = teststaking.NewValidator(t, sdk.ValAddress(pk.Address()), pk)
genValidators1[0].Tokens = sdk.OneInt()
genValidators1[0].Tokens = math.OneInt()
genValidators1[0].DelegatorShares = sdk.OneDec()
tests := []struct {
+3 -2
View File
@@ -1,11 +1,12 @@
package keeper_test
import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"math/big"
"testing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec"
+1 -1
View File
@@ -183,7 +183,7 @@ func (k Keeper) IterateUnbondingDelegations(ctx sdk.Context, fn func(index int64
// GetDelegatorUnbonding returns the total amount a delegator has unbonding.
func (k Keeper) GetDelegatorUnbonding(ctx sdk.Context, delegator sdk.AccAddress) math.Int {
unbonding := sdk.ZeroInt()
unbonding := math.ZeroInt()
k.IterateDelegatorUnbondingDelegations(ctx, delegator, func(ubd types.UnbondingDelegation) bool {
for _, entry := range ubd.Entries {
unbonding = unbonding.Add(entry.Balance)
+11 -10
View File
@@ -4,6 +4,7 @@ import (
"testing"
"time"
"cosmossdk.io/math"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -33,7 +34,7 @@ func TestDelegation(t *testing.T) {
valAddrs := simtestutil.ConvertAddrsToValAddrs(addrDels)
// construct the validators
amts := []sdk.Int{sdk.NewInt(9), sdk.NewInt(8), sdk.NewInt(7)}
amts := []math.Int{sdk.NewInt(9), sdk.NewInt(8), sdk.NewInt(7)}
var validators [3]types.Validator
for i, amt := range amts {
validators[i] = teststaking.NewValidator(t, valAddrs[i], PKs[i])
@@ -257,7 +258,7 @@ func TestUnbondingDelegationsMaxEntries(t *testing.T) {
require.Equal(t, startTokens, issuedShares.RoundInt())
validator = keeper.TestingUpdateValidator(app.StakingKeeper, ctx, validator, true)
require.True(sdk.IntEq(t, startTokens, validator.BondedTokens()))
require.True(math.IntEq(t, startTokens, validator.BondedTokens()))
require.True(t, validator.IsBonded())
delegation := types.NewDelegation(addrDels[0], addrVals[0], issuedShares)
@@ -278,8 +279,8 @@ func TestUnbondingDelegationsMaxEntries(t *testing.T) {
newBonded := app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount
newNotBonded := app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount
require.True(sdk.IntEq(t, newBonded, oldBonded.SubRaw(int64(maxEntries))))
require.True(sdk.IntEq(t, newNotBonded, oldNotBonded.AddRaw(int64(maxEntries))))
require.True(math.IntEq(t, newBonded, oldBonded.SubRaw(int64(maxEntries))))
require.True(math.IntEq(t, newNotBonded, oldNotBonded.AddRaw(int64(maxEntries))))
oldBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount
oldNotBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount
@@ -291,8 +292,8 @@ func TestUnbondingDelegationsMaxEntries(t *testing.T) {
newBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount
newNotBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount
require.True(sdk.IntEq(t, newBonded, oldBonded))
require.True(sdk.IntEq(t, newNotBonded, oldNotBonded))
require.True(math.IntEq(t, newBonded, oldBonded))
require.True(math.IntEq(t, newNotBonded, oldNotBonded))
// mature unbonding delegations
ctx = ctx.WithBlockTime(completionTime)
@@ -301,8 +302,8 @@ func TestUnbondingDelegationsMaxEntries(t *testing.T) {
newBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount
newNotBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount
require.True(sdk.IntEq(t, newBonded, oldBonded))
require.True(sdk.IntEq(t, newNotBonded, oldNotBonded.SubRaw(int64(maxEntries))))
require.True(math.IntEq(t, newBonded, oldBonded))
require.True(math.IntEq(t, newNotBonded, oldNotBonded.SubRaw(int64(maxEntries))))
oldNotBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount
@@ -312,8 +313,8 @@ func TestUnbondingDelegationsMaxEntries(t *testing.T) {
newBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount
newNotBonded = app.BankKeeper.GetBalance(ctx, app.StakingKeeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount
require.True(sdk.IntEq(t, newBonded, oldBonded.SubRaw(1)))
require.True(sdk.IntEq(t, newNotBonded, oldNotBonded.AddRaw(1)))
require.True(math.IntEq(t, newBonded, oldBonded.SubRaw(1)))
require.True(math.IntEq(t, newNotBonded, oldNotBonded.AddRaw(1)))
}
//// test undelegating self delegation from a validator pushing it below MinSelfDelegation
+3 -2
View File
@@ -3,6 +3,7 @@ package keeper
import (
"fmt"
"cosmossdk.io/math"
abci "github.com/tendermint/tendermint/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -15,8 +16,8 @@ import (
// data. Finally, it updates the bonded validators.
// Returns final validator set after applying all declaration and delegations
func (k Keeper) InitGenesis(ctx sdk.Context, data *types.GenesisState) (res []abci.ValidatorUpdate) {
bondedTokens := sdk.ZeroInt()
notBondedTokens := sdk.ZeroInt()
bondedTokens := math.ZeroInt()
notBondedTokens := math.ZeroInt()
// We need to pretend to be "n blocks before genesis", where "n" is the
// validator update delay, so that e.g. slashing periods are correctly
+2 -1
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"testing"
"cosmossdk.io/math"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
@@ -171,7 +172,7 @@ func TestInitGenesisLargeValidatorSet(t *testing.T) {
var err error
bondedPoolAmt := sdk.ZeroInt()
bondedPoolAmt := math.ZeroInt()
for i := range validators {
validators[i], err = types.NewValidator(
sdk.ValAddress(addrs[i]),
+2 -1
View File
@@ -3,9 +3,10 @@ package keeper_test
import (
gocontext "context"
"fmt"
"testing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"testing"
"github.com/stretchr/testify/require"
+2 -1
View File
@@ -3,6 +3,7 @@ package keeper_test
import (
"testing"
"cosmossdk.io/math"
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
@@ -14,7 +15,7 @@ import (
)
// IsValSetSorted reports whether valset is sorted.
func IsValSetSorted(data []types.Validator, powerReduction sdk.Int) bool {
func IsValSetSorted(data []types.Validator, powerReduction math.Int) bool {
n := len(data)
for i := n - 1; i > 0; i-- {
if types.ValidatorsByVotingPower(data).Less(i, i-1, powerReduction) {
+3 -2
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
@@ -46,8 +47,8 @@ func AllInvariants(k *Keeper) sdk.Invariant {
// reflects the tokens actively bonded and not bonded
func ModuleAccountInvariants(k *Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
bonded := sdk.ZeroInt()
notBonded := sdk.ZeroInt()
bonded := math.ZeroInt()
notBonded := math.ZeroInt()
bondedPool := k.GetBondedPool(ctx)
notBondedPool := k.GetNotBondedPool(ctx)
bondDenom := k.BondDenom(ctx)
+4 -3
View File
@@ -1,9 +1,10 @@
package keeper
import (
"cosmossdk.io/math"
"fmt"
"cosmossdk.io/math"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/tendermint/tendermint/libs/log"
@@ -47,7 +48,7 @@ func NewKeeper(
// ensure that authority is a valid AccAddress
if _, err := sdk.AccAddressFromBech32(authority); err != nil {
panic(fmt.Sprintf("authority is not a valid acc address"))
panic(("authority is not a valid acc address"))
}
return &Keeper{
@@ -80,7 +81,7 @@ func (k Keeper) GetLastTotalPower(ctx sdk.Context) math.Int {
bz := store.Get(types.LastTotalPowerKey)
if bz == nil {
return sdk.ZeroInt()
return math.ZeroInt()
}
ip := sdk.IntProto{}
+2 -2
View File
@@ -3,8 +3,8 @@ package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/exported"
"github.com/cosmos/cosmos-sdk/x/staking/migrations/v2"
"github.com/cosmos/cosmos-sdk/x/staking/migrations/v3"
v2 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v2"
v3 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v3"
v4 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v4"
)
+2 -1
View File
@@ -2,10 +2,11 @@ package keeper
import (
"context"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"strconv"
"time"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
+3 -2
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"testing"
"cosmossdk.io/math"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
@@ -25,7 +26,7 @@ func TestNewQuerier(t *testing.T) {
addrVal1, _ := sdk.ValAddress(addrs[0]), sdk.ValAddress(addrs[1])
// Create Validators
amts := []sdk.Int{sdk.NewInt(9), sdk.NewInt(8)}
amts := []math.Int{sdk.NewInt(9), sdk.NewInt(8)}
var validators [2]types.Validator
for i, amt := range amts {
validators[i] = teststaking.NewValidator(t, sdk.ValAddress(addrs[i]), PKs[i])
@@ -144,7 +145,7 @@ func TestQueryValidators(t *testing.T) {
addrs := simapp.AddTestAddrs(app, ctx, 500, app.StakingKeeper.TokensFromConsensusPower(ctx, 10000))
// Create Validators
amts := []sdk.Int{sdk.NewInt(8), sdk.NewInt(7)}
amts := []math.Int{sdk.NewInt(8), sdk.NewInt(7)}
status := []types.BondStatus{types.Unbonded, types.Unbonding}
var validators [2]types.Validator
for i, amt := range amts {
+5 -5
View File
@@ -106,7 +106,7 @@ func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, infractionHeigh
// cannot decrease balance below zero
tokensToBurn := sdk.MinInt(remainingSlashAmount, validator.Tokens)
tokensToBurn = sdk.MaxInt(tokensToBurn, sdk.ZeroInt()) // defensive.
tokensToBurn = sdk.MaxInt(tokensToBurn, math.ZeroInt()) // defensive.
// we need to calculate the *effective* slash fraction for distribution
if validator.Tokens.IsPositive() {
@@ -170,8 +170,8 @@ func (k Keeper) SlashUnbondingDelegation(ctx sdk.Context, unbondingDelegation ty
infractionHeight int64, slashFactor sdk.Dec,
) (totalSlashAmount math.Int) {
now := ctx.BlockHeader().Time
totalSlashAmount = sdk.ZeroInt()
burnedAmount := sdk.ZeroInt()
totalSlashAmount = math.ZeroInt()
burnedAmount := math.ZeroInt()
// perform slashing on all entries within the unbonding delegation
for i, entry := range unbondingDelegation.Entries {
@@ -224,8 +224,8 @@ func (k Keeper) SlashRedelegation(ctx sdk.Context, srcValidator types.Validator,
infractionHeight int64, slashFactor sdk.Dec,
) (totalSlashAmount math.Int) {
now := ctx.BlockHeader().Time
totalSlashAmount = sdk.ZeroInt()
bondedBurnedAmount, notBondedBurnedAmount := sdk.ZeroInt(), sdk.ZeroInt()
totalSlashAmount = math.ZeroInt()
bondedBurnedAmount, notBondedBurnedAmount := math.ZeroInt(), math.ZeroInt()
// perform slashing on all entries within the redelegation
for _, entry := range redelegation.Entries {
+14 -13
View File
@@ -4,6 +4,7 @@ import (
"testing"
"time"
"cosmossdk.io/math"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
@@ -422,10 +423,10 @@ func TestSlashWithRedelegation(t *testing.T) {
// burn bonded tokens from only from delegations
bondedPoolBalance := app.BankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount
require.True(sdk.IntEq(t, oldBonded.Sub(burnAmount), bondedPoolBalance))
require.True(math.IntEq(t, oldBonded.Sub(burnAmount), bondedPoolBalance))
notBondedPoolBalance := app.BankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), bondDenom).Amount
require.True(sdk.IntEq(t, oldNotBonded, notBondedPoolBalance))
require.True(math.IntEq(t, oldNotBonded, notBondedPoolBalance))
oldBonded = app.BankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount
// read updating redelegation
@@ -454,14 +455,14 @@ func TestSlashWithRedelegation(t *testing.T) {
// seven bonded tokens burned
bondedPoolBalance = app.BankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount
require.True(sdk.IntEq(t, oldBonded.Sub(burnAmount), bondedPoolBalance))
require.True(sdk.IntEq(t, oldNotBonded, notBondedPoolBalance))
require.True(math.IntEq(t, oldBonded.Sub(burnAmount), bondedPoolBalance))
require.True(math.IntEq(t, oldNotBonded, notBondedPoolBalance))
bondedPoolBalance = app.BankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount
require.True(sdk.IntEq(t, oldBonded.Sub(burnAmount), bondedPoolBalance))
require.True(math.IntEq(t, oldBonded.Sub(burnAmount), bondedPoolBalance))
notBondedPoolBalance = app.BankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), bondDenom).Amount
require.True(sdk.IntEq(t, oldNotBonded, notBondedPoolBalance))
require.True(math.IntEq(t, oldNotBonded, notBondedPoolBalance))
oldBonded = app.BankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount
// read updating redelegation
@@ -489,9 +490,9 @@ func TestSlashWithRedelegation(t *testing.T) {
notBondedPool = app.StakingKeeper.GetNotBondedPool(ctx)
bondedPoolBalance = app.BankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount
require.True(sdk.IntEq(t, oldBonded.Sub(burnAmount), bondedPoolBalance))
require.True(math.IntEq(t, oldBonded.Sub(burnAmount), bondedPoolBalance))
notBondedPoolBalance = app.BankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), bondDenom).Amount
require.True(sdk.IntEq(t, oldNotBonded, notBondedPoolBalance))
require.True(math.IntEq(t, oldNotBonded, notBondedPoolBalance))
oldBonded = app.BankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount
// read updating redelegation
@@ -519,9 +520,9 @@ func TestSlashWithRedelegation(t *testing.T) {
notBondedPool = app.StakingKeeper.GetNotBondedPool(ctx)
bondedPoolBalance = app.BankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount
require.True(sdk.IntEq(t, oldBonded, bondedPoolBalance))
require.True(math.IntEq(t, oldBonded, bondedPoolBalance))
notBondedPoolBalance = app.BankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), bondDenom).Amount
require.True(sdk.IntEq(t, oldNotBonded, notBondedPoolBalance))
require.True(math.IntEq(t, oldNotBonded, notBondedPoolBalance))
// read updating redelegation
rd, found = app.StakingKeeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1])
@@ -587,10 +588,10 @@ func TestSlashBoth(t *testing.T) {
notBondedPool = app.StakingKeeper.GetNotBondedPool(ctx)
bondedPoolBalance := app.BankKeeper.GetBalance(ctx, bondedPool.GetAddress(), bondDenom).Amount
require.True(sdk.IntEq(t, oldBonded.Sub(burnedBondAmount), bondedPoolBalance))
require.True(math.IntEq(t, oldBonded.Sub(burnedBondAmount), bondedPoolBalance))
notBondedPoolBalance := app.BankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), bondDenom).Amount
require.True(sdk.IntEq(t, oldNotBonded.Sub(burnedNotBondedAmount), notBondedPoolBalance))
require.True(math.IntEq(t, oldNotBonded.Sub(burnedNotBondedAmount), notBondedPoolBalance))
// read updating redelegation
rdA, found = app.StakingKeeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1])
@@ -608,7 +609,7 @@ func TestSlashAmount(t *testing.T) {
consAddr := sdk.ConsAddress(PKs[0].Address())
fraction := sdk.NewDecWithPrec(5, 1)
burnedCoins := app.StakingKeeper.Slash(ctx, consAddr, ctx.BlockHeight(), 10, fraction)
require.True(t, burnedCoins.GT(sdk.ZeroInt()))
require.True(t, burnedCoins.GT(math.ZeroInt()))
// test the case where the validator was not found, which should return no coins
_, addrVals := generateAddresses(app, ctx, 100)
+3 -2
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"sort"
"cosmossdk.io/math"
gogotypes "github.com/gogo/protobuf/types"
abci "github.com/tendermint/tendermint/abci/types"
@@ -109,8 +110,8 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx sdk.Context) (updates []ab
params := k.GetParams(ctx)
maxValidators := params.MaxValidators
powerReduction := k.PowerReduction(ctx)
totalPower := sdk.ZeroInt()
amtFromBondedToNotBonded, amtFromNotBondedToBonded := sdk.ZeroInt(), sdk.ZeroInt()
totalPower := math.ZeroInt()
amtFromBondedToNotBonded, amtFromNotBondedToBonded := math.ZeroInt(), math.ZeroInt()
// Retrieve the last validator set.
// The persistent set is updated later in this function.
+6 -5
View File
@@ -5,6 +5,7 @@ import (
"testing"
"time"
"cosmossdk.io/math"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
@@ -259,7 +260,7 @@ func TestValidatorBasics(t *testing.T) {
for i, power := range powers {
validators[i] = teststaking.NewValidator(t, addrVals[i], PKs[i])
validators[i].Status = types.Unbonded
validators[i].Tokens = sdk.ZeroInt()
validators[i].Tokens = math.ZeroInt()
tokens := app.StakingKeeper.TokensFromConsensusPower(ctx, power)
validators[i], _ = validators[i].AddTokensFromDel(tokens)
@@ -296,7 +297,7 @@ func TestValidatorBasics(t *testing.T) {
require.Equal(t, 1, len(resVals))
assert.True(ValEq(t, validators[0], resVals[0]))
assert.Equal(t, types.Bonded, validators[0].Status)
assert.True(sdk.IntEq(t, app.StakingKeeper.TokensFromConsensusPower(ctx, 9), validators[0].BondedTokens()))
assert.True(math.IntEq(t, app.StakingKeeper.TokensFromConsensusPower(ctx, 9), validators[0].BondedTokens()))
// modify a records, save, and retrieve
validators[0].Status = types.Bonded
@@ -341,7 +342,7 @@ func TestValidatorBasics(t *testing.T) {
"attempting to remove a validator which still contains tokens",
func() { app.StakingKeeper.RemoveValidator(ctx, validators[1].GetOperator()) })
validators[1].Tokens = sdk.ZeroInt() // ...remove all tokens
validators[1].Tokens = math.ZeroInt() // ...remove all tokens
app.StakingKeeper.SetValidator(ctx, validators[1]) // ...set the validator
app.StakingKeeper.RemoveValidator(ctx, validators[1].GetOperator()) // Now it can be removed.
_, found = app.StakingKeeper.GetValidator(ctx, addrVals[1])
@@ -353,7 +354,7 @@ func TestGetValidatorSortingUnmixed(t *testing.T) {
app, ctx, addrs, _ := bootstrapValidatorTest(t, 1000, 20)
// initialize some validators into the state
amts := []sdk.Int{
amts := []math.Int{
sdk.NewIntFromUint64(0),
app.StakingKeeper.PowerReduction(ctx).MulRaw(100),
app.StakingKeeper.PowerReduction(ctx),
@@ -447,7 +448,7 @@ func TestGetValidatorSortingMixed(t *testing.T) {
app.StakingKeeper.SetParams(ctx, params)
// initialize some validators into the state
amts := []sdk.Int{
amts := []math.Int{
sdk.NewIntFromUint64(0),
app.StakingKeeper.PowerReduction(ctx).MulRaw(100),
app.StakingKeeper.PowerReduction(ctx),
+1 -1
View File
@@ -7,7 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/address"
v042auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v042"
v043distribution "github.com/cosmos/cosmos-sdk/x/distribution/migrations/v043"
"github.com/cosmos/cosmos-sdk/x/staking/migrations/v1"
v1 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v1"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
+3 -4
View File
@@ -4,11 +4,12 @@ import (
"context"
"encoding/json"
"fmt"
"math/rand"
"sort"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/staking/exported"
"math/rand"
"sort"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
@@ -160,11 +161,9 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
m := keeper.NewMigrator(am.keeper, am.legacySubspace)
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err))
}
if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil {
panic(fmt.Sprintf("failed to migrate x/%s from version 2 to 3: %v", types.ModuleName, err))
}
if err := cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4); err != nil {
panic(fmt.Sprintf("failed to migrate x/%s from version 3 to 4: %v", types.ModuleName, err))
+5 -4
View File
@@ -5,6 +5,7 @@ import (
"testing"
"time"
"cosmossdk.io/math"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/codec"
@@ -39,12 +40,12 @@ func TestDecodeStore(t *testing.T) {
val, err := types.NewValidator(valAddr1, delPk1, types.NewDescription("test", "test", "test", "test", "test"))
require.NoError(t, err)
del := types.NewDelegation(delAddr1, valAddr1, sdk.OneDec())
ubd := types.NewUnbondingDelegation(delAddr1, valAddr1, 15, bondTime, sdk.OneInt())
red := types.NewRedelegation(delAddr1, valAddr1, valAddr1, 12, bondTime, sdk.OneInt(), sdk.OneDec())
ubd := types.NewUnbondingDelegation(delAddr1, valAddr1, 15, bondTime, math.OneInt())
red := types.NewRedelegation(delAddr1, valAddr1, valAddr1, 12, bondTime, math.OneInt(), sdk.OneDec())
kvPairs := kv.Pairs{
Pairs: []kv.Pair{
{Key: types.LastTotalPowerKey, Value: cdc.MustMarshal(&sdk.IntProto{Int: sdk.OneInt()})},
{Key: types.LastTotalPowerKey, Value: cdc.MustMarshal(&sdk.IntProto{Int: math.OneInt()})},
{Key: types.GetValidatorKey(valAddr1), Value: cdc.MustMarshal(&val)},
{Key: types.LastValidatorPowerKey, Value: valAddr1.Bytes()},
{Key: types.GetDelegationKey(delAddr1, valAddr1), Value: cdc.MustMarshal(&del)},
@@ -58,7 +59,7 @@ func TestDecodeStore(t *testing.T) {
name string
expectedLog string
}{
{"LastTotalPower", fmt.Sprintf("%v\n%v", sdk.OneInt(), sdk.OneInt())},
{"LastTotalPower", fmt.Sprintf("%v\n%v", math.OneInt(), math.OneInt())},
{"Validator", fmt.Sprintf("%v\n%v", val, val)},
{"LastValidatorPower/ValidatorsByConsAddr/ValidatorsByPowerIndex", fmt.Sprintf("%v\n%v", valAddr1, valAddr1)},
{"Delegation", fmt.Sprintf("%v\n%v", del, del)},
+2 -1
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"math/rand"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
@@ -159,7 +160,7 @@ func SimulateMsgCreateValidator(ak types.AccountKeeper, bk types.BankKeeper, k *
simtypes.RandomDecAmount(r, maxCommission),
)
msg, err := types.NewMsgCreateValidator(address, simAccount.ConsKey.PubKey(), selfDelegation, description, commission, sdk.OneInt())
msg, err := types.NewMsgCreateValidator(address, simAccount.ConsKey.PubKey(), selfDelegation, description, commission, math.OneInt())
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to create CreateValidator message"), nil, err
}
+1 -1
View File
@@ -13,7 +13,7 @@ Pool is used for tracking bonded and not-bonded token supply of the bond denomin
LastTotalPower tracks the total amounts of bonded tokens recorded during the previous end block.
Store entries prefixed with "Last" must remain unchanged until EndBlock.
* LastTotalPower: `0x12 -> ProtocolBuffer(sdk.Int)`
* LastTotalPower: `0x12 -> ProtocolBuffer(math.Int)`
## Params
+2 -2
View File
@@ -51,7 +51,7 @@ func (sh *Helper) CreateValidatorWithValPower(addr sdk.ValAddress, pk cryptotype
// CreateValidatorMsg returns a message used to create validator in this service.
func (sh *Helper) CreateValidatorMsg(addr sdk.ValAddress, pk cryptotypes.PubKey, stakeAmount math.Int) *stakingtypes.MsgCreateValidator {
coin := sdk.NewCoin(sh.Denom, stakeAmount)
msg, err := stakingtypes.NewMsgCreateValidator(addr, pk, coin, stakingtypes.Description{}, sh.Commission, sdk.OneInt())
msg, err := stakingtypes.NewMsgCreateValidator(addr, pk, coin, stakingtypes.Description{}, sh.Commission, math.OneInt())
require.NoError(sh.t, err)
return msg
}
@@ -62,7 +62,7 @@ func (sh *Helper) CreateValidatorWithMsg(ctx context.Context, msg *stakingtypes.
}
func (sh *Helper) createValidator(addr sdk.ValAddress, pk cryptotypes.PubKey, coin sdk.Coin, ok bool) {
msg, err := stakingtypes.NewMsgCreateValidator(addr, pk, coin, stakingtypes.Description{}, sh.Commission, sdk.OneInt())
msg, err := stakingtypes.NewMsgCreateValidator(addr, pk, coin, stakingtypes.Description{}, sh.Commission, math.OneInt())
require.NoError(sh.t, err)
res, err := sh.msgSrvr.CreateValidator(sdk.WrapSDKContext(sh.Ctx), msg)
if ok {
+2 -1
View File
@@ -7,6 +7,7 @@ import (
"testing"
"time"
"cosmossdk.io/math"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
@@ -26,7 +27,7 @@ var (
func TestGetValidatorPowerRank(t *testing.T) {
valAddr1 := sdk.ValAddress(keysAddr1)
val1 := newValidator(t, valAddr1, keysPK1)
val1.Tokens = sdk.ZeroInt()
val1.Tokens = math.ZeroInt()
val2, val3, val4 := val1, val1, val1
val2.Tokens = sdk.TokensFromConsensusPower(1, sdk.DefaultPowerReduction)
val3.Tokens = sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction)
+21 -19
View File
@@ -1,11 +1,13 @@
package types_test
import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"testing"
"time"
"cosmossdk.io/math"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/codec"
@@ -40,7 +42,7 @@ func TestMsgDecode(t *testing.T) {
// now let's try to serialize the whole message
commission1 := types.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
msg, err := types.NewMsgCreateValidator(valAddr1, pk1, coinPos, types.Description{}, commission1, sdk.OneInt())
msg, err := types.NewMsgCreateValidator(valAddr1, pk1, coinPos, types.Description{}, commission1, math.OneInt())
require.NoError(t, err)
msgSerialized, err := cdc.MarshalInterface(msg)
require.NoError(t, err)
@@ -62,22 +64,22 @@ func TestMsgCreateValidator(t *testing.T) {
tests := []struct {
name, moniker, identity, website, securityContact, details string
CommissionRates types.CommissionRates
minSelfDelegation sdk.Int
minSelfDelegation math.Int
validatorAddr sdk.ValAddress
pubkey cryptotypes.PubKey
bond sdk.Coin
expectPass bool
}{
{"basic good", "a", "b", "c", "d", "e", commission1, sdk.OneInt(), valAddr1, pk1, coinPos, true},
{"partial description", "", "", "c", "", "", commission1, sdk.OneInt(), valAddr1, pk1, coinPos, true},
{"empty description", "", "", "", "", "", commission2, sdk.OneInt(), valAddr1, pk1, coinPos, false},
{"empty address", "a", "b", "c", "d", "e", commission2, sdk.OneInt(), emptyAddr, pk1, coinPos, false},
{"empty pubkey", "a", "b", "c", "d", "e", commission1, sdk.OneInt(), valAddr1, emptyPubkey, coinPos, false},
{"empty bond", "a", "b", "c", "d", "e", commission2, sdk.OneInt(), valAddr1, pk1, coinZero, false},
{"nil bond", "a", "b", "c", "d", "e", commission2, sdk.OneInt(), valAddr1, pk1, sdk.Coin{}, false},
{"zero min self delegation", "a", "b", "c", "d", "e", commission1, sdk.ZeroInt(), valAddr1, pk1, coinPos, false},
{"basic good", "a", "b", "c", "d", "e", commission1, math.OneInt(), valAddr1, pk1, coinPos, true},
{"partial description", "", "", "c", "", "", commission1, math.OneInt(), valAddr1, pk1, coinPos, true},
{"empty description", "", "", "", "", "", commission2, math.OneInt(), valAddr1, pk1, coinPos, false},
{"empty address", "a", "b", "c", "d", "e", commission2, math.OneInt(), emptyAddr, pk1, coinPos, false},
{"empty pubkey", "a", "b", "c", "d", "e", commission1, math.OneInt(), valAddr1, emptyPubkey, coinPos, false},
{"empty bond", "a", "b", "c", "d", "e", commission2, math.OneInt(), valAddr1, pk1, coinZero, false},
{"nil bond", "a", "b", "c", "d", "e", commission2, math.OneInt(), valAddr1, pk1, sdk.Coin{}, false},
{"zero min self delegation", "a", "b", "c", "d", "e", commission1, math.ZeroInt(), valAddr1, pk1, coinPos, false},
{"negative min self delegation", "a", "b", "c", "d", "e", commission1, sdk.NewInt(-1), valAddr1, pk1, coinPos, false},
{"delegation less than min self delegation", "a", "b", "c", "d", "e", commission1, coinPos.Amount.Add(sdk.OneInt()), valAddr1, pk1, coinPos, false},
{"delegation less than min self delegation", "a", "b", "c", "d", "e", commission1, coinPos.Amount.Add(math.OneInt()), valAddr1, pk1, coinPos, false},
}
for _, tc := range tests {
@@ -98,13 +100,13 @@ func TestMsgEditValidator(t *testing.T) {
name, moniker, identity, website, securityContact, details string
validatorAddr sdk.ValAddress
expectPass bool
minSelfDelegation sdk.Int
minSelfDelegation math.Int
}{
{"basic good", "a", "b", "c", "d", "e", valAddr1, true, sdk.OneInt()},
{"partial description", "", "", "c", "", "", valAddr1, true, sdk.OneInt()},
{"empty description", "", "", "", "", "", valAddr1, false, sdk.OneInt()},
{"empty address", "a", "b", "c", "d", "e", emptyAddr, false, sdk.OneInt()},
{"nil int", "a", "b", "c", "d", "e", emptyAddr, false, sdk.Int{}},
{"basic good", "a", "b", "c", "d", "e", valAddr1, true, math.OneInt()},
{"partial description", "", "", "c", "", "", valAddr1, true, math.OneInt()},
{"empty description", "", "", "", "", "", valAddr1, false, math.OneInt()},
{"empty address", "a", "b", "c", "d", "e", emptyAddr, false, math.OneInt()},
{"nil int", "a", "b", "c", "d", "e", emptyAddr, false, math.Int{}},
}
for _, tc := range tests {
+2 -2
View File
@@ -14,13 +14,13 @@ var (
var _ paramtypes.ParamSet = (*Params)(nil)
// ParamTable for staking module
// NOTE: Deprecated now params can be accesed on key `0x51` on the staking store.
// Deprecated: now params can be accesed on key `0x51` on the staking store.
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// Implements params.ParamSet
// NOTE: Deprecated.
// Deprecated.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyUnbondingTime, &p.UnbondingTime, validateUnbondingTime),
+4 -4
View File
@@ -51,13 +51,13 @@ func NewValidator(operator sdk.ValAddress, pubKey cryptotypes.PubKey, descriptio
ConsensusPubkey: pkAny,
Jailed: false,
Status: Unbonded,
Tokens: sdk.ZeroInt(),
Tokens: math.ZeroInt(),
DelegatorShares: sdk.ZeroDec(),
Description: description,
UnbondingHeight: int64(0),
UnbondingTime: time.Unix(0, 0).UTC(),
Commission: NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
MinSelfDelegation: sdk.OneInt(),
MinSelfDelegation: math.OneInt(),
}, nil
}
@@ -352,7 +352,7 @@ func (v Validator) BondedTokens() math.Int {
return v.Tokens
}
return sdk.ZeroInt()
return math.ZeroInt()
}
// ConsensusPower gets the consensus-engine power. Aa reduction of 10^6 from
@@ -424,7 +424,7 @@ func (v Validator) RemoveDelShares(delShares sdk.Dec) (Validator, math.Int) {
if remainingShares.IsZero() {
// last delegation share gets any trimmings
issuedTokens = v.Tokens
v.Tokens = sdk.ZeroInt()
v.Tokens = math.ZeroInt()
} else {
// leave excess tokens in the validator
// however fully use all the delegator shares
+7 -6
View File
@@ -5,6 +5,7 @@ import (
"sort"
"testing"
"cosmossdk.io/math"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tmtypes "github.com/tendermint/tendermint/types"
@@ -105,7 +106,7 @@ func TestAddTokensValidatorBonded(t *testing.T) {
validator, delShares := validator.AddTokensFromDel(sdk.NewInt(10))
assert.True(sdk.DecEq(t, sdk.NewDec(10), delShares))
assert.True(sdk.IntEq(t, sdk.NewInt(10), validator.BondedTokens()))
assert.True(math.IntEq(t, sdk.NewInt(10), validator.BondedTokens()))
assert.True(sdk.DecEq(t, sdk.NewDec(10), validator.DelegatorShares))
}
@@ -116,7 +117,7 @@ func TestAddTokensValidatorUnbonding(t *testing.T) {
assert.True(sdk.DecEq(t, sdk.NewDec(10), delShares))
assert.Equal(t, types.Unbonding, validator.Status)
assert.True(sdk.IntEq(t, sdk.NewInt(10), validator.Tokens))
assert.True(math.IntEq(t, sdk.NewInt(10), validator.Tokens))
assert.True(sdk.DecEq(t, sdk.NewDec(10), validator.DelegatorShares))
}
@@ -127,7 +128,7 @@ func TestAddTokensValidatorUnbonded(t *testing.T) {
assert.True(sdk.DecEq(t, sdk.NewDec(10), delShares))
assert.Equal(t, types.Unbonded, validator.Status)
assert.True(sdk.IntEq(t, sdk.NewInt(10), validator.Tokens))
assert.True(math.IntEq(t, sdk.NewInt(10), validator.Tokens))
assert.True(sdk.DecEq(t, sdk.NewDec(10), validator.DelegatorShares))
}
@@ -151,7 +152,7 @@ func TestRemoveDelShares(t *testing.T) {
validator := mkValidator(5102, sdk.NewDec(115))
_, tokens := validator.RemoveDelShares(sdk.NewDec(29))
require.True(sdk.IntEq(t, sdk.NewInt(1286), tokens))
require.True(math.IntEq(t, sdk.NewInt(1286), tokens))
}
func TestAddTokensFromDel(t *testing.T) {
@@ -160,12 +161,12 @@ func TestAddTokensFromDel(t *testing.T) {
validator, shares := validator.AddTokensFromDel(sdk.NewInt(6))
require.True(sdk.DecEq(t, sdk.NewDec(6), shares))
require.True(sdk.DecEq(t, sdk.NewDec(6), validator.DelegatorShares))
require.True(sdk.IntEq(t, sdk.NewInt(6), validator.Tokens))
require.True(math.IntEq(t, sdk.NewInt(6), validator.Tokens))
validator, shares = validator.AddTokensFromDel(sdk.NewInt(3))
require.True(sdk.DecEq(t, sdk.NewDec(3), shares))
require.True(sdk.DecEq(t, sdk.NewDec(9), validator.DelegatorShares))
require.True(sdk.IntEq(t, sdk.NewInt(9), validator.Tokens))
require.True(math.IntEq(t, sdk.NewInt(9), validator.Tokens))
}
func TestUpdateStatus(t *testing.T) {