From b17c2d902e372bf0fe1e9fc327591101e133c294 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Wed, 14 Dec 2022 17:09:32 -0600 Subject: [PATCH] refactor: x/staking 0.47 audit (#14303) --- x/staking/keeper/historical_info_test.go | 3 ++- x/staking/keeper/keeper.go | 17 +++++++++-------- x/staking/keeper/keeper_test.go | 16 ++++++++-------- x/staking/keeper/msg_server.go | 6 ++---- x/staking/keeper/params.go | 2 +- x/staking/keeper/unbonding.go | 4 ++-- x/staking/keeper/val_state_change.go | 5 +++-- x/staking/migrations/v4/migrations_test.go | 3 ++- x/staking/types/keys.go | 4 ++-- x/staking/types/msg.go | 4 ++-- 10 files changed, 33 insertions(+), 31 deletions(-) diff --git a/x/staking/keeper/historical_info_test.go b/x/staking/keeper/historical_info_test.go index c10b10973e..aa9efd5497 100644 --- a/x/staking/keeper/historical_info_test.go +++ b/x/staking/keeper/historical_info_test.go @@ -1,9 +1,10 @@ package keeper_test import ( - "cosmossdk.io/math" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/x/staking/testutil" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index fb61ea58b8..952a48dc7d 100644 --- a/x/staking/keeper/keeper.go +++ b/x/staking/keeper/keeper.go @@ -3,13 +3,13 @@ package keeper import ( "fmt" - "cosmossdk.io/math" - - storetypes "github.com/cosmos/cosmos-sdk/store/types" abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" + "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/codec" + storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -49,7 +49,7 @@ func NewKeeper( // ensure that authority is a valid AccAddress if _, err := sdk.AccAddressFromBech32(authority); err != nil { - panic(("authority is not a valid acc address")) + panic("authority is not a valid acc address") } return &Keeper{ @@ -68,16 +68,17 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { } // Hooks gets the hooks for staking *Keeper { -func (keeper *Keeper) Hooks() types.StakingHooks { - if keeper.hooks == nil { +func (k *Keeper) Hooks() types.StakingHooks { + if k.hooks == nil { // return a no-op implementation if no hooks are set return types.MultiStakingHooks{} } - return keeper.hooks + return k.hooks } -// SetHooks Set the validator hooks +// SetHooks Set the validator hooks. In contrast to other receivers, this method must take a pointer due to nature +// of the hooks interface and SDK start up sequence. func (k *Keeper) SetHooks(sh types.StakingHooks) { if k.hooks != nil { panic("cannot set validator hooks twice") diff --git a/x/staking/keeper/keeper_test.go b/x/staking/keeper/keeper_test.go index 09509d289b..a114c6a2bc 100644 --- a/x/staking/keeper/keeper_test.go +++ b/x/staking/keeper/keeper_test.go @@ -3,23 +3,23 @@ package keeper_test import ( "testing" - "cosmossdk.io/math" - "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/testutil" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/golang/mock/gomock" "github.com/stretchr/testify/suite" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" tmtime "github.com/tendermint/tendermint/types/time" + "cosmossdk.io/math" + + "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/testutil" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" + sdk "github.com/cosmos/cosmos-sdk/types" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - - simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" - moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" stakingtestutil "github.com/cosmos/cosmos-sdk/x/staking/testutil" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) var ( diff --git a/x/staking/keeper/msg_server.go b/x/staking/keeper/msg_server.go index bbd9205247..5d38a6045f 100644 --- a/x/staking/keeper/msg_server.go +++ b/x/staking/keeper/msg_server.go @@ -5,17 +5,15 @@ import ( "strconv" "time" - govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" - + "github.com/armon/go-metrics" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "github.com/armon/go-metrics" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" "github.com/cosmos/cosmos-sdk/x/staking/types" ) diff --git a/x/staking/keeper/params.go b/x/staking/keeper/params.go index 99e30d26ce..3e1bb08391 100644 --- a/x/staking/keeper/params.go +++ b/x/staking/keeper/params.go @@ -9,7 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/types" ) -// UnbondingTime +// UnbondingTime - The time duration for unbonding func (k Keeper) UnbondingTime(ctx sdk.Context) time.Duration { return k.GetParams(ctx).UnbondingTime } diff --git a/x/staking/keeper/unbonding.go b/x/staking/keeper/unbonding.go index 275b4fda70..122ca4ef83 100644 --- a/x/staking/keeper/unbonding.go +++ b/x/staking/keeper/unbonding.go @@ -8,7 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/types" ) -// Increments and returns a unique ID for an unbonding operation +// IncrementUnbondingID increments and returns a unique ID for an unbonding operation func (k Keeper) IncrementUnbondingID(ctx sdk.Context) (unbondingID uint64) { store := ctx.KVStore(k.storeKey) bz := store.Get(types.UnbondingIDKey) @@ -27,7 +27,7 @@ func (k Keeper) IncrementUnbondingID(ctx sdk.Context) (unbondingID uint64) { return unbondingID } -// Remove a mapping from UnbondingId to unbonding operation +// DeleteUnbondingIndex removes a mapping from UnbondingId to unbonding operation func (k Keeper) DeleteUnbondingIndex(ctx sdk.Context, id uint64) { store := ctx.KVStore(k.storeKey) store.Delete(types.GetUnbondingIndexKey(id)) diff --git a/x/staking/keeper/val_state_change.go b/x/staking/keeper/val_state_change.go index b45eb4ba8c..3049512e98 100644 --- a/x/staking/keeper/val_state_change.go +++ b/x/staking/keeper/val_state_change.go @@ -5,10 +5,11 @@ import ( "fmt" "sort" - "cosmossdk.io/math" gogotypes "github.com/cosmos/gogoproto/types" abci "github.com/tendermint/tendermint/abci/types" + "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -309,7 +310,7 @@ func (k Keeper) bondValidator(ctx sdk.Context, validator types.Validator) (types return validator, err } -// perform all the store operations for when a validator begins unbonding +// BeginUnbondingValidator performs all the store operations for when a validator begins unbonding func (k Keeper) BeginUnbondingValidator(ctx sdk.Context, validator types.Validator) (types.Validator, error) { params := k.GetParams(ctx) diff --git a/x/staking/migrations/v4/migrations_test.go b/x/staking/migrations/v4/migrations_test.go index 1efbd4dbf7..1b1ff95f01 100644 --- a/x/staking/migrations/v4/migrations_test.go +++ b/x/staking/migrations/v4/migrations_test.go @@ -4,6 +4,8 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" + "github.com/cosmos/cosmos-sdk/codec" storetypes "github.com/cosmos/cosmos-sdk/store/types" "github.com/cosmos/cosmos-sdk/testutil" @@ -14,7 +16,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking" v4 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v4" "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/stretchr/testify/require" ) type mockSubspace struct { diff --git a/x/staking/types/keys.go b/x/staking/types/keys.go index 7b76bc2d91..6fede0ef1a 100644 --- a/x/staking/types/keys.go +++ b/x/staking/types/keys.go @@ -66,14 +66,14 @@ const ( UnbondingType_ValidatorUnbonding ) -// Returns a key for an index containing the type of unbonding operations +// GetUnbondingTypeKey returns a key for an index containing the type of unbonding operations func GetUnbondingTypeKey(id uint64) []byte { bz := make([]byte, 8) binary.BigEndian.PutUint64(bz, id) return append(UnbondingTypeKey, bz...) } -// Returns a key for the index for looking up UnbondingDelegations by the UnbondingDelegationEntries they contain +// GetUnbondingIndexKey returns a key for the index for looking up UnbondingDelegations by the UnbondingDelegationEntries they contain func GetUnbondingIndexKey(id uint64) []byte { bz := make([]byte, 8) binary.BigEndian.PutUint64(bz, id) diff --git a/x/staking/types/msg.go b/x/staking/types/msg.go index e72e68a039..a8539bd218 100644 --- a/x/staking/types/msg.go +++ b/x/staking/types/msg.go @@ -403,8 +403,8 @@ func (msg MsgCancelUnbondingDelegation) ValidateBasic() error { // GetSignBytes returns the raw bytes for a MsgUpdateParams message that // the expected signer needs to sign. -func (m MsgUpdateParams) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&m) +func (m *MsgUpdateParams) GetSignBytes() []byte { + bz := ModuleCdc.MustMarshalJSON(m) return sdk.MustSortJSON(bz) }