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