From 4e3f27325a9137a9f8572432177465faba26ba24 Mon Sep 17 00:00:00 2001 From: atheesh Date: Fri, 30 Jun 2023 16:14:29 +0530 Subject: [PATCH 1/4] chore(x/staking): audit changes --- x/staking/client/cli/query.go | 8 ++--- x/staking/keeper/abci.go | 2 +- x/staking/keeper/alias_functions.go | 14 ++++----- x/staking/keeper/delegation.go | 8 ++--- x/staking/keeper/grpc_query.go | 20 ++++++------ x/staking/keeper/historical_info.go | 8 ++--- x/staking/keeper/keeper.go | 6 ++-- x/staking/keeper/migrations.go | 2 +- x/staking/keeper/msg_server.go | 5 +-- x/staking/keeper/params.go | 2 +- x/staking/keeper/pool.go | 4 +-- x/staking/keeper/power_reduction.go | 4 +-- x/staking/keeper/query_utils.go | 10 +++--- x/staking/keeper/slash.go | 2 +- x/staking/keeper/test_common.go | 4 +-- x/staking/keeper/unbonding.go | 9 ++++-- x/staking/keeper/validator.go | 43 +++++++++++++------------- x/staking/types/msg.go | 48 ----------------------------- 18 files changed, 78 insertions(+), 121 deletions(-) diff --git a/x/staking/client/cli/query.go b/x/staking/client/cli/query.go index 1747b7c54c7a..4007c5b5ae41 100644 --- a/x/staking/client/cli/query.go +++ b/x/staking/client/cli/query.go @@ -134,13 +134,13 @@ $ %s query staking validators return cmd } -// GetCmdQueryValidatorUnbondingDelegations implements the query all unbonding delegatations from a validator command. +// GetCmdQueryValidatorUnbondingDelegations implements the query all unbonding delegations from a validator command. func GetCmdQueryValidatorUnbondingDelegations() *cobra.Command { bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix() cmd := &cobra.Command{ Use: "unbonding-delegations-from [validator-addr]", - Short: "Query all unbonding delegatations from a validator", + Short: "Query all unbonding delegations from a validator", Long: strings.TrimSpace( fmt.Sprintf(`Query delegations that are unbonding _from_ a validator. @@ -188,14 +188,14 @@ $ %s query staking unbonding-delegations-from %s1gghjut3ccd8ay0zduzj64hwre2fxs9l return cmd } -// GetCmdQueryValidatorRedelegations implements the query all redelegatations +// GetCmdQueryValidatorRedelegations implements the query all redelegations // from a validator command. func GetCmdQueryValidatorRedelegations() *cobra.Command { bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix() cmd := &cobra.Command{ Use: "redelegations-from [validator-addr]", - Short: "Query all outgoing redelegatations from a validator", + Short: "Query all outgoing redelegations from a validator", Long: strings.TrimSpace( fmt.Sprintf(`Query delegations that are redelegating _from_ a validator. diff --git a/x/staking/keeper/abci.go b/x/staking/keeper/abci.go index 6a21b2b02300..aa32e8cccc05 100644 --- a/x/staking/keeper/abci.go +++ b/x/staking/keeper/abci.go @@ -17,7 +17,7 @@ func (k *Keeper) BeginBlocker(ctx context.Context) error { return k.TrackHistoricalInfo(ctx) } -// Called every block, update validator set +// EndBlocker called at every block, update validator set func (k *Keeper) EndBlocker(ctx context.Context) ([]abci.ValidatorUpdate, error) { defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker) return k.BlockValidatorUpdates(ctx) diff --git a/x/staking/keeper/alias_functions.go b/x/staking/keeper/alias_functions.go index 0047b92192c0..4379043cc6d6 100644 --- a/x/staking/keeper/alias_functions.go +++ b/x/staking/keeper/alias_functions.go @@ -11,7 +11,7 @@ import ( // Validator Set -// iterate through the validator set and perform the provided function +// IterateValidators iterates through the validator set and perform the provided function func (k Keeper) IterateValidators(ctx context.Context, fn func(index int64, validator types.ValidatorI) (stop bool)) error { store := k.storeService.OpenKVStore(ctx) iterator, err := store.Iterator(types.ValidatorsKey, storetypes.PrefixEndBytes(types.ValidatorsKey)) @@ -38,7 +38,7 @@ func (k Keeper) IterateValidators(ctx context.Context, fn func(index int64, vali return nil } -// iterate through the bonded validator set and perform the provided function +// IterateBondedValidatorsByPower iterates through the bonded validator set and perform the provided function func (k Keeper) IterateBondedValidatorsByPower(ctx context.Context, fn func(index int64, validator types.ValidatorI) (stop bool)) error { store := k.storeService.OpenKVStore(ctx) maxValidators, err := k.MaxValidators(ctx) @@ -69,7 +69,7 @@ func (k Keeper) IterateBondedValidatorsByPower(ctx context.Context, fn func(inde return nil } -// iterate through the active validator set and perform the provided function +// IterateLastValidators iterates through the active validator set and perform the provided function func (k Keeper) IterateLastValidators(ctx context.Context, fn func(index int64, validator types.ValidatorI) (stop bool)) error { iterator, err := k.LastValidatorsIterator(ctx) if err != nil { @@ -108,12 +108,12 @@ func (k Keeper) ValidatorByConsAddr(ctx context.Context, addr sdk.ConsAddress) ( // Delegation Set -// Returns self as it is both a validatorset and delegationset +// GetValidatorSet returns self as it is both a validatorset and delegationset func (k Keeper) GetValidatorSet() types.ValidatorSet { return k } -// Delegation get the delegation interface for a particular set of delegator and validator addresses +// Delegation gets the delegation interface for a particular set of delegator and validator addresses func (k Keeper) Delegation(ctx context.Context, addrDel sdk.AccAddress, addrVal sdk.ValAddress) (types.DelegationI, error) { bond, err := k.GetDelegation(ctx, addrDel, addrVal) if err != nil { @@ -123,7 +123,7 @@ func (k Keeper) Delegation(ctx context.Context, addrDel sdk.AccAddress, addrVal return bond, nil } -// iterate through all of the delegations from a delegator +// IterateDelegations iterates through all of the delegations from a delegator func (k Keeper) IterateDelegations(ctx context.Context, delAddr sdk.AccAddress, fn func(index int64, del types.DelegationI) (stop bool), ) error { @@ -151,7 +151,7 @@ func (k Keeper) IterateDelegations(ctx context.Context, delAddr sdk.AccAddress, return nil } -// return all delegations used during genesis dump +// GetAllSDKDelegations returns all delegations used during genesis dump // TODO: remove this func, change all usage for iterate functionality func (k Keeper) GetAllSDKDelegations(ctx context.Context) (delegations []types.Delegation, err error) { store := k.storeService.OpenKVStore(ctx) diff --git a/x/staking/keeper/delegation.go b/x/staking/keeper/delegation.go index 06ad400a4f44..b783c6c41a58 100644 --- a/x/staking/keeper/delegation.go +++ b/x/staking/keeper/delegation.go @@ -352,7 +352,7 @@ func (k Keeper) IterateDelegatorRedelegations(ctx context.Context, delegator sdk return nil } -// HasMaxUnbondingDelegationEntries - check if unbonding delegation has maximum number of entries. +// HasMaxUnbondingDelegationEntries checks if unbonding delegation has maximum number of entries. func (k Keeper) HasMaxUnbondingDelegationEntries(ctx context.Context, delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress) (bool, error) { ubd, err := k.GetUnbondingDelegation(ctx, delegatorAddr, validatorAddr) if err != nil && !errors.Is(err, types.ErrNoUnbondingDelegation) { @@ -616,7 +616,7 @@ func (k Keeper) HasReceivingRedelegation(ctx context.Context, delAddr sdk.AccAdd return iterator.Valid(), nil } -// HasMaxRedelegationEntries checks if redelegation has maximum number of entries. +// HasMaxRedelegationEntries checks if the redelegation entries reached maximum limit. func (k Keeper) HasMaxRedelegationEntries(ctx context.Context, delegatorAddr sdk.AccAddress, validatorSrcAddr, validatorDstAddr sdk.ValAddress) (bool, error) { red, err := k.GetRedelegation(ctx, delegatorAddr, validatorSrcAddr, validatorDstAddr) if err != nil { @@ -634,7 +634,7 @@ func (k Keeper) HasMaxRedelegationEntries(ctx context.Context, delegatorAddr sdk return len(red.Entries) >= int(maxEntries), nil } -// SetRedelegation set a redelegation and associated index. +// SetRedelegation sets a redelegation and associated index. func (k Keeper) SetRedelegation(ctx context.Context, red types.Redelegation) error { delegatorAddress, err := k.authKeeper.AddressCodec().StringToBytes(red.DelegatorAddress) if err != nil { @@ -1319,7 +1319,7 @@ func (k Keeper) CompleteRedelegation( } // ValidateUnbondAmount validates that a given unbond or redelegation amount is -// valied based on upon the converted shares. If the amount is valid, the total +// valid based on upon the converted shares. If the amount is valid, the total // amount of respective shares is returned, otherwise an error is returned. func (k Keeper) ValidateUnbondAmount( ctx context.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress, amt math.Int, diff --git a/x/staking/keeper/grpc_query.go b/x/staking/keeper/grpc_query.go index 13bb3bc3f9f1..5d5906e25b82 100644 --- a/x/staking/keeper/grpc_query.go +++ b/x/staking/keeper/grpc_query.go @@ -133,7 +133,7 @@ func (k Querier) ValidatorDelegations(ctx context.Context, req *types.QueryValid pageRes = pageResponse } - delResponses, err := DelegationsToDelegationResponses(ctx, k.Keeper, dels) + delResponses, err := delegationsToDelegationResponses(ctx, k.Keeper, dels) if err != nil { return nil, status.Error(codes.Internal, err.Error()) } @@ -234,7 +234,7 @@ func (k Querier) Delegation(ctx context.Context, req *types.QueryDelegationReque req.DelegatorAddr, req.ValidatorAddr) } - delResponse, err := DelegationToDelegationResponse(ctx, k.Keeper, delegation) + delResponse, err := delegationToDelegationResponse(ctx, k.Keeper, delegation) if err != nil { return nil, status.Error(codes.Internal, err.Error()) } @@ -242,7 +242,7 @@ func (k Querier) Delegation(ctx context.Context, req *types.QueryDelegationReque return &types.QueryDelegationResponse{DelegationResponse: &delResponse}, nil } -// UnbondingDelegation queries unbonding info for give validator delegator pair +// UnbondingDelegation queries unbonding info for given validator delegator pair func (k Querier) UnbondingDelegation(ctx context.Context, req *types.QueryUnbondingDelegationRequest) (*types.QueryUnbondingDelegationResponse, error) { if req == nil { return nil, status.Errorf(codes.InvalidArgument, "empty request") @@ -276,7 +276,7 @@ func (k Querier) UnbondingDelegation(ctx context.Context, req *types.QueryUnbond return &types.QueryUnbondingDelegationResponse{Unbond: unbond}, nil } -// DelegatorDelegations queries all delegations of a give delegator address +// DelegatorDelegations queries all delegations of a given delegator address func (k Querier) DelegatorDelegations(ctx context.Context, req *types.QueryDelegatorDelegationsRequest) (*types.QueryDelegatorDelegationsResponse, error) { if req == nil { return nil, status.Errorf(codes.InvalidArgument, "empty request") @@ -306,7 +306,7 @@ func (k Querier) DelegatorDelegations(ctx context.Context, req *types.QueryDeleg return nil, status.Error(codes.Internal, err.Error()) } - delegationResps, err := DelegationsToDelegationResponses(ctx, k.Keeper, delegations) + delegationResps, err := delegationsToDelegationResponses(ctx, k.Keeper, delegations) if err != nil { return nil, status.Error(codes.Internal, err.Error()) } @@ -420,7 +420,7 @@ func (k Querier) Redelegations(ctx context.Context, req *types.QueryRedelegation if err != nil { return nil, status.Error(codes.Internal, err.Error()) } - redelResponses, err := RedelegationsToRedelegationResponses(ctx, k.Keeper, redels) + redelResponses, err := redelegationsToRedelegationResponses(ctx, k.Keeper, redels) if err != nil { return nil, status.Error(codes.Internal, err.Error()) } @@ -564,7 +564,7 @@ func queryAllRedelegations(store storetypes.KVStore, k Querier, req *types.Query // util -func DelegationToDelegationResponse(ctx context.Context, k *Keeper, del types.Delegation) (types.DelegationResponse, error) { +func delegationToDelegationResponse(ctx context.Context, k *Keeper, del types.Delegation) (types.DelegationResponse, error) { val, err := k.GetValidator(ctx, del.GetValidatorAddr()) if err != nil { return types.DelegationResponse{}, err @@ -588,11 +588,11 @@ func DelegationToDelegationResponse(ctx context.Context, k *Keeper, del types.De ), nil } -func DelegationsToDelegationResponses(ctx context.Context, k *Keeper, delegations types.Delegations) (types.DelegationResponses, error) { +func delegationsToDelegationResponses(ctx context.Context, k *Keeper, delegations types.Delegations) (types.DelegationResponses, error) { resp := make(types.DelegationResponses, len(delegations)) for i, del := range delegations { - delResp, err := DelegationToDelegationResponse(ctx, k, del) + delResp, err := delegationToDelegationResponse(ctx, k, del) if err != nil { return nil, err } @@ -603,7 +603,7 @@ func DelegationsToDelegationResponses(ctx context.Context, k *Keeper, delegation return resp, nil } -func RedelegationsToRedelegationResponses(ctx context.Context, k *Keeper, redels types.Redelegations) (types.RedelegationResponses, error) { +func redelegationsToRedelegationResponses(ctx context.Context, k *Keeper, redels types.Redelegations) (types.RedelegationResponses, error) { resp := make(types.RedelegationResponses, len(redels)) for i, redel := range redels { diff --git a/x/staking/keeper/historical_info.go b/x/staking/keeper/historical_info.go index eb9707d103a2..4cc9cded97d7 100644 --- a/x/staking/keeper/historical_info.go +++ b/x/staking/keeper/historical_info.go @@ -46,11 +46,9 @@ func (k Keeper) DeleteHistoricalInfo(ctx context.Context, height int64) error { return store.Delete(key) } -// IterateHistoricalInfo provides an interator over all stored HistoricalInfo -// -// objects. For each HistoricalInfo object, cb will be called. If the cb returns -// -// true, the iterator will close and stop. +// IterateHistoricalInfo provides an iterator over all stored HistoricalInfo +// objects. For each HistoricalInfo object, cb will be called. If the cb returns +// true, the iterator will break and close. func (k Keeper) IterateHistoricalInfo(ctx context.Context, cb func(types.HistoricalInfo) bool) error { store := k.storeService.OpenKVStore(ctx) iterator, err := store.Iterator(types.HistoricalInfoKey, storetypes.PrefixEndBytes(types.HistoricalInfoKey)) diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index 3fff14d4df66..35c4804916f3 100644 --- a/x/staking/keeper/keeper.go +++ b/x/staking/keeper/keeper.go @@ -79,7 +79,7 @@ func (k *Keeper) Hooks() types.StakingHooks { return k.hooks } -// SetHooks Set the validator hooks. In contrast to other receivers, this method must take a pointer due to nature +// SetHooks sets 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 { @@ -89,7 +89,7 @@ func (k *Keeper) SetHooks(sh types.StakingHooks) { k.hooks = sh } -// GetLastTotalPower Load the last total validator power. +// GetLastTotalPower loads the last total validator power. func (k Keeper) GetLastTotalPower(ctx context.Context) (math.Int, error) { store := k.storeService.OpenKVStore(ctx) bz, err := store.Get(types.LastTotalPowerKey) @@ -110,7 +110,7 @@ func (k Keeper) GetLastTotalPower(ctx context.Context) (math.Int, error) { return ip.Int, nil } -// SetLastTotalPower Set the last total validator power. +// SetLastTotalPower sets the last total validator power. func (k Keeper) SetLastTotalPower(ctx context.Context, power math.Int) error { store := k.storeService.OpenKVStore(ctx) bz, err := k.cdc.Marshal(&sdk.IntProto{Int: power}) diff --git a/x/staking/keeper/migrations.go b/x/staking/keeper/migrations.go index 0e0e8560784e..e94848b13397 100644 --- a/x/staking/keeper/migrations.go +++ b/x/staking/keeper/migrations.go @@ -16,7 +16,7 @@ type Migrator struct { legacySubspace exported.Subspace } -// NewMigrator returns a new Migrator. +// NewMigrator returns a new Migrator instance. func NewMigrator(keeper *Keeper, legacySubspace exported.Subspace) Migrator { return Migrator{ keeper: keeper, diff --git a/x/staking/keeper/msg_server.go b/x/staking/keeper/msg_server.go index f00be4a663f3..54cce6831109 100644 --- a/x/staking/keeper/msg_server.go +++ b/x/staking/keeper/msg_server.go @@ -23,7 +23,7 @@ type msgServer struct { *Keeper } -// NewMsgServerImpl returns an implementation of the bank MsgServer interface +// NewMsgServerImpl returns an implementation of the staking MsgServer interface // for the provided Keeper. func NewMsgServerImpl(keeper *Keeper) types.MsgServer { return &msgServer{Keeper: keeper} @@ -301,7 +301,7 @@ func (k msgServer) Delegate(ctx context.Context, msg *types.MsgDelegate) (*types return &types.MsgDelegateResponse{}, nil } -// BeginRedelegate defines a method for performing a redelegation of coins from a delegator and source validator to a destination validator +// BeginRedelegate defines a method for performing a redelegation of coins from a source validator to a destination validator of given delegator func (k msgServer) BeginRedelegate(ctx context.Context, msg *types.MsgBeginRedelegate) (*types.MsgBeginRedelegateResponse, error) { valSrcAddr, err := sdk.ValAddressFromBech32(msg.ValidatorSrcAddress) if err != nil { @@ -576,6 +576,7 @@ func (k msgServer) CancelUnbondingDelegation(ctx context.Context, msg *types.Msg return &types.MsgCancelUnbondingDelegationResponse{}, nil } +// UpdateParams defines a method to perform updation of params exist in x/staking module. func (k msgServer) UpdateParams(ctx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) { if k.authority != msg.Authority { return nil, errorsmod.Wrapf(types.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, msg.Authority) diff --git a/x/staking/keeper/params.go b/x/staking/keeper/params.go index e099c0aa0293..83e29708470c 100644 --- a/x/staking/keeper/params.go +++ b/x/staking/keeper/params.go @@ -67,7 +67,7 @@ func (k Keeper) SetParams(ctx context.Context, params types.Params) error { return store.Set(types.ParamsKey, bz) } -// GetParams sets the x/staking module parameters. +// GetParams gets the x/staking module parameters. func (k Keeper) GetParams(ctx context.Context) (params types.Params, err error) { store := k.storeService.OpenKVStore(ctx) bz, err := store.Get(types.ParamsKey) diff --git a/x/staking/keeper/pool.go b/x/staking/keeper/pool.go index b8df38c2cd2e..dc81409ff6a7 100644 --- a/x/staking/keeper/pool.go +++ b/x/staking/keeper/pool.go @@ -41,7 +41,7 @@ func (k Keeper) notBondedTokensToBonded(ctx context.Context, tokens math.Int) er return k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.NotBondedPoolName, types.BondedPoolName, coins) } -// burnBondedTokens removes coins from the bonded pool module account +// burnBondedTokens burns coins from the bonded pool module account func (k Keeper) burnBondedTokens(ctx context.Context, amt math.Int) error { if !amt.IsPositive() { // skip as no coins need to be burned @@ -58,7 +58,7 @@ func (k Keeper) burnBondedTokens(ctx context.Context, amt math.Int) error { return k.bankKeeper.BurnCoins(ctx, types.BondedPoolName, coins) } -// burnNotBondedTokens removes coins from the not bonded pool module account +// burnNotBondedTokens burns coins from the not bonded pool module account func (k Keeper) burnNotBondedTokens(ctx context.Context, amt math.Int) error { if !amt.IsPositive() { // skip as no coins need to be burned diff --git a/x/staking/keeper/power_reduction.go b/x/staking/keeper/power_reduction.go index b35917c58c92..7eaad0cbe4f6 100644 --- a/x/staking/keeper/power_reduction.go +++ b/x/staking/keeper/power_reduction.go @@ -8,12 +8,12 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -// TokensToConsensusPower - convert input tokens to potential consensus-engine power +// TokensToConsensusPower converts input tokens to potential consensus-engine power func (k Keeper) TokensToConsensusPower(ctx context.Context, tokens math.Int) int64 { return sdk.TokensToConsensusPower(tokens, k.PowerReduction(ctx)) } -// TokensFromConsensusPower - convert input power to tokens +// TokensFromConsensusPower converts input power to tokens func (k Keeper) TokensFromConsensusPower(ctx context.Context, power int64) math.Int { return sdk.TokensFromConsensusPower(power, k.PowerReduction(ctx)) } diff --git a/x/staking/keeper/query_utils.go b/x/staking/keeper/query_utils.go index 75b1616dcc76..c8298c001e20 100644 --- a/x/staking/keeper/query_utils.go +++ b/x/staking/keeper/query_utils.go @@ -9,7 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/types" ) -// Return all validators that a delegator is bonded to. If maxRetrieve is supplied, the respective amount will be returned. +// GetDelegatorValidators returns all validators that a delegator is bonded to. If maxRetrieve is supplied, the respective amount will be returned. func (k Keeper) GetDelegatorValidators( ctx context.Context, delegatorAddr sdk.AccAddress, maxRetrieve uint32, ) (types.Validators, error) { @@ -39,7 +39,7 @@ func (k Keeper) GetDelegatorValidators( return validators[:i], nil // trim } -// return a validator that a delegator is bonded to +// GetDelegatorValidator returns a validator that a delegator is bonded to func (k Keeper) GetDelegatorValidator( ctx context.Context, delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress, ) (validator types.Validator, err error) { @@ -51,7 +51,7 @@ func (k Keeper) GetDelegatorValidator( return k.GetValidator(ctx, delegation.GetValidatorAddr()) } -// return all delegations for a delegator +// GetAllDelegatorDelegations returns all delegations of a delegator func (k Keeper) GetAllDelegatorDelegations(ctx context.Context, delegator sdk.AccAddress) ([]types.Delegation, error) { delegations := make([]types.Delegation, 0) @@ -76,7 +76,7 @@ func (k Keeper) GetAllDelegatorDelegations(ctx context.Context, delegator sdk.Ac return delegations, nil } -// return all unbonding-delegations for a delegator +// GetAllUnbondingDelegations returns all unbonding-delegations of a delegator func (k Keeper) GetAllUnbondingDelegations(ctx context.Context, delegator sdk.AccAddress) ([]types.UnbondingDelegation, error) { unbondingDelegations := make([]types.UnbondingDelegation, 0) @@ -101,7 +101,7 @@ func (k Keeper) GetAllUnbondingDelegations(ctx context.Context, delegator sdk.Ac return unbondingDelegations, nil } -// return all redelegations for a delegator +// GetAllRedelegations returns all redelegations of a delegator func (k Keeper) GetAllRedelegations( ctx context.Context, delegator sdk.AccAddress, srcValAddress, dstValAddress sdk.ValAddress, ) ([]types.Redelegation, error) { diff --git a/x/staking/keeper/slash.go b/x/staking/keeper/slash.go index 163456821cac..fc98a44585b8 100644 --- a/x/staking/keeper/slash.go +++ b/x/staking/keeper/slash.go @@ -31,7 +31,7 @@ import ( // CONTRACT: // // Infraction was committed at the current height or at a past height, -// not at a height in the future +// but not at a height in the future func (k Keeper) Slash(ctx context.Context, consAddr sdk.ConsAddress, infractionHeight, power int64, slashFactor math.LegacyDec) (math.Int, error) { logger := k.Logger(ctx) sdkCtx := sdk.UnwrapSDKContext(ctx) diff --git a/x/staking/keeper/test_common.go b/x/staking/keeper/test_common.go index 854751c8d404..812c0b562bf3 100644 --- a/x/staking/keeper/test_common.go +++ b/x/staking/keeper/test_common.go @@ -10,7 +10,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/types" ) -// does a certain by-power index record exist +// ValidatorByPowerIndexExists does a certain by-power index record exist func ValidatorByPowerIndexExists(ctx context.Context, keeper *Keeper, power []byte) bool { store := keeper.storeService.OpenKVStore(ctx) has, err := store.Has(power) @@ -20,7 +20,7 @@ func ValidatorByPowerIndexExists(ctx context.Context, keeper *Keeper, power []by return has } -// update validator for testing +// TestingUpdateValidator updates a validator for testing func TestingUpdateValidator(keeper *Keeper, ctx sdk.Context, validator types.Validator, apply bool) types.Validator { err := keeper.SetValidator(ctx, validator) if err != nil { diff --git a/x/staking/keeper/unbonding.go b/x/staking/keeper/unbonding.go index 2b96aebdf9a0..a0fb55de8c46 100644 --- a/x/staking/keeper/unbonding.go +++ b/x/staking/keeper/unbonding.go @@ -41,6 +41,8 @@ func (k Keeper) DeleteUnbondingIndex(ctx context.Context, id uint64) error { return store.Delete(types.GetUnbondingIndexKey(id)) } +// GetUnbondingType returns the enum type of unbonding which is any of +// {UnbondingDelegation | Redelegation | ValidatorUnbonding} func (k Keeper) GetUnbondingType(ctx context.Context, id uint64) (unbondingType types.UnbondingType, err error) { store := k.storeService.OpenKVStore(ctx) @@ -56,6 +58,8 @@ func (k Keeper) GetUnbondingType(ctx context.Context, id uint64) (unbondingType return types.UnbondingType(binary.BigEndian.Uint64(bz)), nil } +// SetUnbondingType sets the enum type of unbonding which is any of +// {UnbondingDelegation | Redelegation | ValidatorUnbonding} func (k Keeper) SetUnbondingType(ctx context.Context, id uint64, unbondingType types.UnbondingType) error { store := k.storeService.OpenKVStore(ctx) @@ -159,8 +163,9 @@ func (k Keeper) GetValidatorByUnbondingID(ctx context.Context, id uint64) (val t return val, nil } -// SetUnbondingDelegationByUnbondingID sets an index to look up an UnbondingDelegation by the unbondingID of an UnbondingDelegationEntry that it contains -// Note, it does not set the unbonding delegation itself, use SetUnbondingDelegation(ctx, ubd) for that +// SetUnbondingDelegationByUnbondingID sets an index to look up an UnbondingDelegation +// by the unbondingID of an UnbondingDelegationEntry that it contains Note, it does not +// set the unbonding delegation itself, use SetUnbondingDelegation(ctx, ubd) for that func (k Keeper) SetUnbondingDelegationByUnbondingID(ctx context.Context, ubd types.UnbondingDelegation, id uint64) error { store := k.storeService.OpenKVStore(ctx) delAddr, err := k.authKeeper.AddressCodec().StringToBytes(ubd.DelegatorAddress) diff --git a/x/staking/keeper/validator.go b/x/staking/keeper/validator.go index 23ab4319ac17..e92c49047157 100644 --- a/x/staking/keeper/validator.go +++ b/x/staking/keeper/validator.go @@ -17,7 +17,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/types" ) -// get a single validator +// GetValidator gets a single validator func (k Keeper) GetValidator(ctx context.Context, addr sdk.ValAddress) (validator types.Validator, err error) { store := k.storeService.OpenKVStore(ctx) value, err := store.Get(types.GetValidatorKey(addr)) @@ -41,7 +41,7 @@ func (k Keeper) mustGetValidator(ctx context.Context, addr sdk.ValAddress) types return validator } -// get a single validator by consensus address +// GetValidatorByConsAddr gets a single validator by consensus address func (k Keeper) GetValidatorByConsAddr(ctx context.Context, consAddr sdk.ConsAddress) (validator types.Validator, err error) { store := k.storeService.OpenKVStore(ctx) opAddr, err := store.Get(types.GetValidatorByConsAddrKey(consAddr)) @@ -65,14 +65,14 @@ func (k Keeper) mustGetValidatorByConsAddr(ctx context.Context, consAddr sdk.Con return validator } -// set the main record holding validator details +// SetValidator sets the main record holding validator details func (k Keeper) SetValidator(ctx context.Context, validator types.Validator) error { store := k.storeService.OpenKVStore(ctx) bz := types.MustMarshalValidator(k.cdc, &validator) return store.Set(types.GetValidatorKey(validator.GetOperator()), bz) } -// validator index +// SetValidatorByConsAddr sets a validator by conesensus address func (k Keeper) SetValidatorByConsAddr(ctx context.Context, validator types.Validator) error { consPk, err := validator.GetConsAddr() if err != nil { @@ -82,7 +82,7 @@ func (k Keeper) SetValidatorByConsAddr(ctx context.Context, validator types.Vali return store.Set(types.GetValidatorByConsAddrKey(consPk), validator.GetOperator()) } -// validator index +// SetValidatorByPowerIndex sets a validator by power index func (k Keeper) SetValidatorByPowerIndex(ctx context.Context, validator types.Validator) error { // jailed validators are not kept in the power index if validator.Jailed { @@ -93,19 +93,19 @@ func (k Keeper) SetValidatorByPowerIndex(ctx context.Context, validator types.Va return store.Set(types.GetValidatorsByPowerIndexKey(validator, k.PowerReduction(ctx)), validator.GetOperator()) } -// validator index +// DeleteValidatorByPowerIndex deletes a record by power index func (k Keeper) DeleteValidatorByPowerIndex(ctx context.Context, validator types.Validator) error { store := k.storeService.OpenKVStore(ctx) return store.Delete(types.GetValidatorsByPowerIndexKey(validator, k.PowerReduction(ctx))) } -// validator index +// SetNewValidatorByPowerIndex adds new entry by power index func (k Keeper) SetNewValidatorByPowerIndex(ctx context.Context, validator types.Validator) error { store := k.storeService.OpenKVStore(ctx) return store.Set(types.GetValidatorsByPowerIndexKey(validator, k.PowerReduction(ctx)), validator.GetOperator()) } -// Update the tokens of an existing validator, update the validators power index key +// AddValidatorTokensAndShares updates the tokens of an existing validator, updates the validators power index key func (k Keeper) AddValidatorTokensAndShares(ctx context.Context, validator types.Validator, tokensToAdd math.Int, ) (valOut types.Validator, addedShares math.LegacyDec, err error) { @@ -124,7 +124,7 @@ func (k Keeper) AddValidatorTokensAndShares(ctx context.Context, validator types return validator, addedShares, err } -// Update the tokens of an existing validator, update the validators power index key +// RemoveValidatorTokensAndShares updates the tokens of an existing validator, updates the validators power index key func (k Keeper) RemoveValidatorTokensAndShares(ctx context.Context, validator types.Validator, sharesToRemove math.LegacyDec, ) (valOut types.Validator, removedTokens math.Int, err error) { @@ -142,7 +142,7 @@ func (k Keeper) RemoveValidatorTokensAndShares(ctx context.Context, validator ty return validator, removedTokens, err } -// Update the tokens of an existing validator, update the validators power index key +// RemoveValidatorTokens updates the tokens of an existing validator, updates the validators power index key func (k Keeper) RemoveValidatorTokens(ctx context.Context, validator types.Validator, tokensToRemove math.Int, ) (types.Validator, error) { @@ -190,7 +190,7 @@ func (k Keeper) UpdateValidatorCommission(ctx context.Context, return commission, nil } -// remove the validator record and associated indexes +// RemoveValidator removes the validator record and associated indexes // except for the bonded validator index which is only handled in ApplyAndReturnTendermintUpdates func (k Keeper) RemoveValidator(ctx context.Context, address sdk.ValAddress) error { // first retrieve the old validator record @@ -235,7 +235,7 @@ func (k Keeper) RemoveValidator(ctx context.Context, address sdk.ValAddress) err // get groups of validators -// get the set of all validators with no limits, used during genesis dump +// GetAllValidators gets the set of all validators with no limits, used during genesis dump func (k Keeper) GetAllValidators(ctx context.Context) (validators []types.Validator, err error) { store := k.storeService.OpenKVStore(ctx) @@ -256,7 +256,7 @@ func (k Keeper) GetAllValidators(ctx context.Context) (validators []types.Valida return validators, nil } -// return a given amount of all the validators +// GetValidators returns a given amount of all the validators func (k Keeper) GetValidators(ctx context.Context, maxRetrieve uint32) (validators []types.Validator, err error) { store := k.storeService.OpenKVStore(ctx) validators = make([]types.Validator, maxRetrieve) @@ -279,7 +279,7 @@ func (k Keeper) GetValidators(ctx context.Context, maxRetrieve uint32) (validato return validators[:i], nil // trim if the array length < maxRetrieve } -// get the current group of bonded validators sorted by power-rank +// GetBondedValidatorsByPower gets the current group of bonded validators sorted by power-rank func (k Keeper) GetBondedValidatorsByPower(ctx context.Context) ([]types.Validator, error) { maxValidators, err := k.MaxValidators(ctx) if err != nil { @@ -307,7 +307,7 @@ func (k Keeper) GetBondedValidatorsByPower(ctx context.Context) ([]types.Validat return validators[:i], nil // trim } -// returns an iterator for the current validator power store +// ValidatorsPowerStoreIterator returns an iterator for the current validator power store func (k Keeper) ValidatorsPowerStoreIterator(ctx context.Context) (corestore.Iterator, error) { store := k.storeService.OpenKVStore(ctx) return store.ReverseIterator(types.ValidatorsByPowerIndexKey, storetypes.PrefixEndBytes(types.ValidatorsByPowerIndexKey)) @@ -315,7 +315,7 @@ func (k Keeper) ValidatorsPowerStoreIterator(ctx context.Context) (corestore.Ite // Last Validator Index -// Load the last validator power. +// GetLastValidatorPower loads the last validator power. // Returns zero if the operator was not a validator last block. func (k Keeper) GetLastValidatorPower(ctx context.Context, operator sdk.ValAddress) (power int64, err error) { store := k.storeService.OpenKVStore(ctx) @@ -337,7 +337,7 @@ func (k Keeper) GetLastValidatorPower(ctx context.Context, operator sdk.ValAddre return intV.GetValue(), nil } -// Set the last validator power. +// SetLastValidatorPower sets the last validator power. func (k Keeper) SetLastValidatorPower(ctx context.Context, operator sdk.ValAddress, power int64) error { store := k.storeService.OpenKVStore(ctx) bz, err := k.cdc.Marshal(&gogotypes.Int64Value{Value: power}) @@ -347,19 +347,19 @@ func (k Keeper) SetLastValidatorPower(ctx context.Context, operator sdk.ValAddre return store.Set(types.GetLastValidatorPowerKey(operator), bz) } -// Delete the last validator power. +// DeleteLastValidatorPower deletes the last validator power. func (k Keeper) DeleteLastValidatorPower(ctx context.Context, operator sdk.ValAddress) error { store := k.storeService.OpenKVStore(ctx) return store.Delete(types.GetLastValidatorPowerKey(operator)) } -// returns an iterator for the consensus validators in the last block +// lastValidatorsIterator returns an iterator for the consensus validators in the last block func (k Keeper) LastValidatorsIterator(ctx context.Context) (corestore.Iterator, error) { store := k.storeService.OpenKVStore(ctx) return store.Iterator(types.LastValidatorPowerKey, storetypes.PrefixEndBytes(types.LastValidatorPowerKey)) } -// Iterate over last validator powers. +// IterateLastValidatorPowers iterates over last validator powers. func (k Keeper) IterateLastValidatorPowers(ctx context.Context, handler func(operator sdk.ValAddress, power int64) (stop bool)) error { iter, err := k.LastValidatorsIterator(ctx) if err != nil { @@ -382,7 +382,7 @@ func (k Keeper) IterateLastValidatorPowers(ctx context.Context, handler func(ope return nil } -// get the group of the bonded validators +// GetLastValidators gets the group of the bonded validators func (k Keeper) GetLastValidators(ctx context.Context) (validators []types.Validator, err error) { store := k.storeService.OpenKVStore(ctx) @@ -591,6 +591,7 @@ func (k Keeper) UnbondAllMatureValidators(ctx context.Context) error { return nil } +// IsValidatorJailed checks and returns boolean of a validator status jailed or not. func (k Keeper) IsValidatorJailed(ctx context.Context, addr sdk.ConsAddress) (bool, error) { v, err := k.GetValidatorByConsAddr(ctx, addr) if err != nil { diff --git a/x/staking/types/msg.go b/x/staking/types/msg.go index 89012d1b5605..09744a69d23f 100644 --- a/x/staking/types/msg.go +++ b/x/staking/types/msg.go @@ -44,18 +44,6 @@ func NewMsgCreateValidator( }, nil } -// GetSigners implements the sdk.Msg interface. It returns the address(es) that -// must sign over msg.GetSignBytes(). -// If the validator address is not same as delegator's, then the validator must -// sign the msg as well. -func (msg MsgCreateValidator) GetSigners() []sdk.AccAddress { - valAddr, _ := sdk.ValAddressFromBech32(msg.ValidatorAddress) - - valAccAddr := sdk.AccAddress(valAddr) - - return []sdk.AccAddress{valAccAddr} -} - // Validate validates the MsgCreateValidator sdk msg. func (msg MsgCreateValidator) Validate() error { // note that unmarshaling from bech32 ensures both non-empty and valid @@ -114,12 +102,6 @@ func NewMsgEditValidator(valAddr sdk.ValAddress, description Description, newRat } } -// GetSigners implements the sdk.Msg interface. -func (msg MsgEditValidator) GetSigners() []sdk.AccAddress { - valAddr, _ := sdk.ValAddressFromBech32(msg.ValidatorAddress) - return []sdk.AccAddress{sdk.AccAddress(valAddr)} -} - // NewMsgDelegate creates a new MsgDelegate instance. func NewMsgDelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount sdk.Coin) *MsgDelegate { return &MsgDelegate{ @@ -129,12 +111,6 @@ func NewMsgDelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount sdk.C } } -// GetSigners implements the sdk.Msg interface. -func (msg MsgDelegate) GetSigners() []sdk.AccAddress { - delegator, _ := sdk.AccAddressFromBech32(msg.DelegatorAddress) - return []sdk.AccAddress{delegator} -} - // NewMsgBeginRedelegate creates a new MsgBeginRedelegate instance. func NewMsgBeginRedelegate( delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress, amount sdk.Coin, @@ -147,12 +123,6 @@ func NewMsgBeginRedelegate( } } -// GetSigners implements the sdk.Msg interface -func (msg MsgBeginRedelegate) GetSigners() []sdk.AccAddress { - delegator, _ := sdk.AccAddressFromBech32(msg.DelegatorAddress) - return []sdk.AccAddress{delegator} -} - // NewMsgUndelegate creates a new MsgUndelegate instance. func NewMsgUndelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount sdk.Coin) *MsgUndelegate { return &MsgUndelegate{ @@ -162,12 +132,6 @@ func NewMsgUndelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount sdk } } -// GetSigners implements the sdk.Msg interface. -func (msg MsgUndelegate) GetSigners() []sdk.AccAddress { - delegator, _ := sdk.AccAddressFromBech32(msg.DelegatorAddress) - return []sdk.AccAddress{delegator} -} - // NewMsgCancelUnbondingDelegation creates a new MsgCancelUnbondingDelegation instance. func NewMsgCancelUnbondingDelegation(delAddr sdk.AccAddress, valAddr sdk.ValAddress, creationHeight int64, amount sdk.Coin) *MsgCancelUnbondingDelegation { return &MsgCancelUnbondingDelegation{ @@ -177,15 +141,3 @@ func NewMsgCancelUnbondingDelegation(delAddr sdk.AccAddress, valAddr sdk.ValAddr CreationHeight: creationHeight, } } - -// GetSigners implements the sdk.Msg interface. -func (msg MsgCancelUnbondingDelegation) GetSigners() []sdk.AccAddress { - delegator, _ := sdk.AccAddressFromBech32(msg.DelegatorAddress) - return []sdk.AccAddress{delegator} -} - -// GetSigners returns the expected signers for a MsgUpdateParams message -func (m MsgUpdateParams) GetSigners() []sdk.AccAddress { - addr, _ := sdk.AccAddressFromBech32(m.Authority) - return []sdk.AccAddress{addr} -} From a37d05e4aba680336675c3d97f9c67dc300f8ae9 Mon Sep 17 00:00:00 2001 From: atheesh Date: Fri, 30 Jun 2023 16:39:25 +0530 Subject: [PATCH 2/4] nit --- proto/cosmos/staking/v1beta1/genesis.proto | 1 + 1 file changed, 1 insertion(+) diff --git a/proto/cosmos/staking/v1beta1/genesis.proto b/proto/cosmos/staking/v1beta1/genesis.proto index 8b278ff91af7..6003be0eec25 100644 --- a/proto/cosmos/staking/v1beta1/genesis.proto +++ b/proto/cosmos/staking/v1beta1/genesis.proto @@ -37,6 +37,7 @@ message GenesisState { // redelegations defines the redelegations active at genesis. repeated Redelegation redelegations = 7 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true]; + // exported defines a bool to identify whether the chain dealing with exported or initialized genesis. bool exported = 8; } From e25afdc1ff34459c2e5cd1318df5326ee5e75be5 Mon Sep 17 00:00:00 2001 From: atheesh Date: Mon, 3 Jul 2023 11:27:28 +0530 Subject: [PATCH 3/4] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4311819bcca1..b6c13b1db168 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * remove `Keeper`: `IterateValidatorHistoricalRewards`, `GetValidatorHistoricalRewards`, `SetValidatorHistoricalRewards`, `DeleteValidatorHistoricalRewards`, `DeleteValidatorHistoricalReward`, `DeleteAllValidatorHistoricalRewards` * (x/auth) [#16621](https://github.com/cosmos/cosmos-sdk/pull/16621), [#16768](https://github.com/cosmos/cosmos-sdk/pull/16768) Pass address codecs to auth new keeper constructor. * (x/auth/vesting) [#16741](https://github.com/cosmos/cosmos-sdk/pull/16741) Vesting account constructor now return an error with the result of their validate function. +* (x/staking) [#16795](https://github.com/cosmos/cosmos-sdk/pull/16795) `DelegationToDelegationResponse`, `DelegationsToDelegationResponses`, `RedelegationsToRedelegationResponses` are no longer exported. ### Bug Fixes From 87c19d169b530568194c7fa67af11bb40d1bf9a3 Mon Sep 17 00:00:00 2001 From: atheeshp <59333759+atheeshp@users.noreply.github.com> Date: Mon, 3 Jul 2023 06:48:37 +0000 Subject: [PATCH 4/4] proto-gen --- api/cosmos/staking/v1beta1/genesis.pulsar.go | 3 ++- x/staking/types/genesis.pb.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/api/cosmos/staking/v1beta1/genesis.pulsar.go b/api/cosmos/staking/v1beta1/genesis.pulsar.go index 38efa2056c46..4120d7c3d326 100644 --- a/api/cosmos/staking/v1beta1/genesis.pulsar.go +++ b/api/cosmos/staking/v1beta1/genesis.pulsar.go @@ -1763,7 +1763,8 @@ type GenesisState struct { UnbondingDelegations []*UnbondingDelegation `protobuf:"bytes,6,rep,name=unbonding_delegations,json=unbondingDelegations,proto3" json:"unbonding_delegations,omitempty"` // redelegations defines the redelegations active at genesis. Redelegations []*Redelegation `protobuf:"bytes,7,rep,name=redelegations,proto3" json:"redelegations,omitempty"` - Exported bool `protobuf:"varint,8,opt,name=exported,proto3" json:"exported,omitempty"` + // exported defines a bool to identify whether the chain dealing with exported or initialized genesis. + Exported bool `protobuf:"varint,8,opt,name=exported,proto3" json:"exported,omitempty"` } func (x *GenesisState) Reset() { diff --git a/x/staking/types/genesis.pb.go b/x/staking/types/genesis.pb.go index fc9b8d48a237..92b215e14edd 100644 --- a/x/staking/types/genesis.pb.go +++ b/x/staking/types/genesis.pb.go @@ -44,7 +44,8 @@ type GenesisState struct { UnbondingDelegations []UnbondingDelegation `protobuf:"bytes,6,rep,name=unbonding_delegations,json=unbondingDelegations,proto3" json:"unbonding_delegations"` // redelegations defines the redelegations active at genesis. Redelegations []Redelegation `protobuf:"bytes,7,rep,name=redelegations,proto3" json:"redelegations"` - Exported bool `protobuf:"varint,8,opt,name=exported,proto3" json:"exported,omitempty"` + // exported defines a bool to identify whether the chain dealing with exported or initialized genesis. + Exported bool `protobuf:"varint,8,opt,name=exported,proto3" json:"exported,omitempty"` } func (m *GenesisState) Reset() { *m = GenesisState{} }