Skip to content

Commit

Permalink
x/staking v0.43 Audit updates (#9267)
Browse files Browse the repository at this point in the history
* update staking specs

* use gosimple code in x/staking/client/testutil/suite.go

* small fixes

* add godoc to methods in msg_server and grpc_query

* changes to godoc and small fixes

* remove unnecessary lines

* remove mentions of serviceMsg and avoid having slash after Msg
  • Loading branch information
likhita-809 authored May 10, 2021
1 parent 9038dfe commit e96839d
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 68 deletions.
16 changes: 8 additions & 8 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -6182,9 +6182,9 @@ AuthorizationType defines the type of staking module authorization type
| Name | Number | Description |
| ---- | ------ | ----------- |
| AUTHORIZATION_TYPE_UNSPECIFIED | 0 | AUTHORIZATION_TYPE_UNSPECIFIED specifies an unknown authorization type |
| AUTHORIZATION_TYPE_DELEGATE | 1 | AUTHORIZATION_TYPE_DELEGATE defines an authorization type for Msg/Delegate |
| AUTHORIZATION_TYPE_UNDELEGATE | 2 | AUTHORIZATION_TYPE_UNDELEGATE defines an authorization type for Msg/Undelegate |
| AUTHORIZATION_TYPE_REDELEGATE | 3 | AUTHORIZATION_TYPE_REDELEGATE defines an authorization type for Msg/BeginRedelegate |
| AUTHORIZATION_TYPE_DELEGATE | 1 | AUTHORIZATION_TYPE_DELEGATE defines an authorization type for MsgDelegate |
| AUTHORIZATION_TYPE_UNDELEGATE | 2 | AUTHORIZATION_TYPE_UNDELEGATE defines an authorization type for MsgUndelegate |
| AUTHORIZATION_TYPE_REDELEGATE | 3 | AUTHORIZATION_TYPE_REDELEGATE defines an authorization type for MsgBeginRedelegate |


<!-- end enums -->
Expand Down Expand Up @@ -7167,7 +7167,7 @@ of coins from a delegator and source validator to a destination validator.
<a name="cosmos.staking.v1beta1.MsgBeginRedelegateResponse"></a>

### MsgBeginRedelegateResponse
MsgBeginRedelegateResponse defines the Msg/BeginRedelegate response type.
MsgBeginRedelegateResponse defines the MsgBeginRedelegate response type.


| Field | Type | Label | Description |
Expand Down Expand Up @@ -7203,7 +7203,7 @@ MsgCreateValidator defines a SDK message for creating a new validator.
<a name="cosmos.staking.v1beta1.MsgCreateValidatorResponse"></a>

### MsgCreateValidatorResponse
MsgCreateValidatorResponse defines the Msg/CreateValidator response type.
MsgCreateValidatorResponse defines the MsgCreateValidator response type.



Expand Down Expand Up @@ -7231,7 +7231,7 @@ from a delegator to a validator.
<a name="cosmos.staking.v1beta1.MsgDelegateResponse"></a>

### MsgDelegateResponse
MsgDelegateResponse defines the Msg/Delegate response type.
MsgDelegateResponse defines the MsgDelegate response type.



Expand Down Expand Up @@ -7259,7 +7259,7 @@ MsgEditValidator defines a SDK message for editing an existing validator.
<a name="cosmos.staking.v1beta1.MsgEditValidatorResponse"></a>

### MsgEditValidatorResponse
MsgEditValidatorResponse defines the Msg/EditValidator response type.
MsgEditValidatorResponse defines the MsgEditValidator response type.



Expand Down Expand Up @@ -7287,7 +7287,7 @@ delegate and a validator.
<a name="cosmos.staking.v1beta1.MsgUndelegateResponse"></a>

### MsgUndelegateResponse
MsgUndelegateResponse defines the Msg/Undelegate response type.
MsgUndelegateResponse defines the MsgUndelegate response type.


| Field | Type | Label | Description |
Expand Down
1 change: 0 additions & 1 deletion x/staking/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,6 @@ $ %s query staking historical-info 5

params := &types.QueryHistoricalInfoRequest{Height: height}
res, err := queryClient.HistoricalInfo(cmd.Context(), params)

if err != nil {
return err
}
Expand Down
14 changes: 7 additions & 7 deletions x/staking/client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryValidator() {
},
{
"happy case",
[]string{fmt.Sprintf("%s", val.ValAddress), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
[]string{val.ValAddress.String(), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
false,
},
}
Expand Down Expand Up @@ -415,7 +415,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryDelegations() {
}
}

func (s *IntegrationTestSuite) TestGetCmdQueryDelegationsTo() {
func (s *IntegrationTestSuite) TestGetCmdQueryValidatorDelegations() {
val := s.network.Validators[0]

testCases := []struct {
Expand Down Expand Up @@ -755,7 +755,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryRedelegation() {
}
}

func (s *IntegrationTestSuite) TestGetCmdQueryRedelegationsFrom() {
func (s *IntegrationTestSuite) TestGetCmdQueryValidatorRedelegations() {
val := s.network.Validators[0]
val2 := s.network.Validators[1]

Expand Down Expand Up @@ -925,7 +925,7 @@ not_bonded_tokens: "0"`, cli.DefaultTokens.Mul(sdk.NewInt(2)).String()),
}
}

func (s *IntegrationTestSuite) TestNewCmdEditValidator() {
func (s *IntegrationTestSuite) TestNewEditValidatorCmd() {
val := s.network.Validators[0]

details := "bio"
Expand Down Expand Up @@ -1041,7 +1041,7 @@ func (s *IntegrationTestSuite) TestNewCmdEditValidator() {
}
}

func (s *IntegrationTestSuite) TestNewCmdDelegate() {
func (s *IntegrationTestSuite) TestNewDelegateCmd() {
val := s.network.Validators[0]

info, _, err := val.ClientCtx.Keyring.NewMnemonic("NewAccount", keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
Expand Down Expand Up @@ -1123,7 +1123,7 @@ func (s *IntegrationTestSuite) TestNewCmdDelegate() {
}
}

func (s *IntegrationTestSuite) TestNewCmdRedelegate() {
func (s *IntegrationTestSuite) TestNewRedelegateCmd() {
val := s.network.Validators[0]
val2 := s.network.Validators[1]

Expand Down Expand Up @@ -1209,7 +1209,7 @@ func (s *IntegrationTestSuite) TestNewCmdRedelegate() {
}
}

func (s *IntegrationTestSuite) TestNewCmdUnbond() {
func (s *IntegrationTestSuite) TestNewUnbondCmd() {
val := s.network.Validators[0]

testCases := []struct {
Expand Down
5 changes: 4 additions & 1 deletion x/staking/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ func TestUndelegateFromUnbondingValidator(t *testing.T) {
selfDelegation := types.NewDelegation(addrVals[0].Bytes(), addrVals[0], issuedShares)
app.StakingKeeper.SetDelegation(ctx, selfDelegation)

// add bonded tokens to pool for delegations
bondedPool := app.StakingKeeper.GetBondedPool(ctx)
require.NoError(t, simapp.FundModuleAccount(app, ctx, bondedPool.GetName(), delCoins))
app.AccountKeeper.SetModuleAccount(ctx, bondedPool)
Expand Down Expand Up @@ -470,6 +471,7 @@ func TestUndelegateFromUnbondedValidator(t *testing.T) {
selfDelegation := types.NewDelegation(val0AccAddr, addrVals[0], issuedShares)
app.StakingKeeper.SetDelegation(ctx, selfDelegation)

// add bonded tokens to pool for delegations
bondedPool := app.StakingKeeper.GetBondedPool(ctx)
require.NoError(t, simapp.FundModuleAccount(app, ctx, bondedPool.GetName(), delCoins))
app.AccountKeeper.SetModuleAccount(ctx, bondedPool)
Expand Down Expand Up @@ -518,7 +520,7 @@ func TestUndelegateFromUnbondedValidator(t *testing.T) {
_, err = app.StakingKeeper.Undelegate(ctx, addrDels[1], addrVals[0], remainingTokens.ToDec())
require.NoError(t, err)

// now validator should now be deleted from state
// now validator should be deleted from state
validator, found = app.StakingKeeper.GetValidator(ctx, addrVals[0])
require.False(t, found, "%v", validator)
}
Expand Down Expand Up @@ -556,6 +558,7 @@ func TestUnbondingAllDelegationFromValidator(t *testing.T) {
validator, issuedShares = validator.AddTokensFromDel(delTokens)
require.Equal(t, delTokens, issuedShares.RoundInt())

// add bonded tokens to pool for delegations
bondedPool := app.StakingKeeper.GetBondedPool(ctx)
require.NoError(t, simapp.FundModuleAccount(app, ctx, bondedPool.GetName(), delCoins))
app.AccountKeeper.SetModuleAccount(ctx, bondedPool)
Expand Down
4 changes: 3 additions & 1 deletion x/staking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (k Querier) Validators(c context.Context, req *types.QueryValidatorsRequest
return &types.QueryValidatorsResponse{Validators: validators, Pagination: pageRes}, nil
}

// Validator queries validator info for given validator addr
// Validator queries validator info for given validator address
func (k Querier) Validator(c context.Context, req *types.QueryValidatorRequest) (*types.QueryValidatorResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
Expand Down Expand Up @@ -380,6 +380,7 @@ func (k Querier) HistoricalInfo(c context.Context, req *types.QueryHistoricalInf
return &types.QueryHistoricalInfoResponse{Hist: &hi}, nil
}

// Redelegations queries redelegations of given address
func (k Querier) Redelegations(c context.Context, req *types.QueryRedelegationsRequest) (*types.QueryRedelegationsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
Expand Down Expand Up @@ -410,6 +411,7 @@ func (k Querier) Redelegations(c context.Context, req *types.QueryRedelegationsR
return &types.QueryRedelegationsResponse{RedelegationResponses: redelResponses, Pagination: pageRes}, nil
}

// DelegatorValidators queries all validators info for given delegator address
func (k Querier) DelegatorValidators(c context.Context, req *types.QueryDelegatorValidatorsRequest) (*types.QueryDelegatorValidatorsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
Expand Down
4 changes: 2 additions & 2 deletions x/staking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryValidators() {
}
}

func (suite *KeeperTestSuite) TestGRPCValidator() {
func (suite *KeeperTestSuite) TestGRPCQueryValidator() {
app, ctx, queryClient, vals := suite.app, suite.ctx, suite.queryClient, suite.vals
validator, found := app.StakingKeeper.GetValidator(ctx, vals[0].GetOperator())
suite.True(found)
Expand Down Expand Up @@ -586,7 +586,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryHistoricalInfo() {
}
}

func (suite *KeeperTestSuite) TestGRPCQueryRedelegation() {
func (suite *KeeperTestSuite) TestGRPCQueryRedelegations() {
app, ctx, queryClient, addrs, vals := suite.app, suite.ctx, suite.queryClient, suite.addrs, suite.vals

addrAcc, addrAcc1 := addrs[0], addrs[1]
Expand Down
5 changes: 5 additions & 0 deletions x/staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func NewMsgServerImpl(keeper Keeper) types.MsgServer {

var _ types.MsgServer = msgServer{}

// CreateValidator defines a method for creating a new validator
func (k msgServer) CreateValidator(goCtx context.Context, msg *types.MsgCreateValidator) (*types.MsgCreateValidatorResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

Expand Down Expand Up @@ -119,6 +120,7 @@ func (k msgServer) CreateValidator(goCtx context.Context, msg *types.MsgCreateVa
return &types.MsgCreateValidatorResponse{}, nil
}

// EditValidator defines a method for editing an existing validator
func (k msgServer) EditValidator(goCtx context.Context, msg *types.MsgEditValidator) (*types.MsgEditValidatorResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress)
Expand Down Expand Up @@ -181,6 +183,7 @@ func (k msgServer) EditValidator(goCtx context.Context, msg *types.MsgEditValida
return &types.MsgEditValidatorResponse{}, nil
}

// Delegate defines a method for performing a delegation of coins from a delegator to a validator
func (k msgServer) Delegate(goCtx context.Context, msg *types.MsgDelegate) (*types.MsgDelegateResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
valAddr, valErr := sdk.ValAddressFromBech32(msg.ValidatorAddress)
Expand Down Expand Up @@ -237,6 +240,7 @@ func (k msgServer) Delegate(goCtx context.Context, msg *types.MsgDelegate) (*typ
return &types.MsgDelegateResponse{}, nil
}

// BeginRedelegate defines a method for performing a redelegation of coins from a delegator and source validator to a destination validator
func (k msgServer) BeginRedelegate(goCtx context.Context, msg *types.MsgBeginRedelegate) (*types.MsgBeginRedelegateResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
valSrcAddr, err := sdk.ValAddressFromBech32(msg.ValidatorSrcAddress)
Expand Down Expand Up @@ -302,6 +306,7 @@ func (k msgServer) BeginRedelegate(goCtx context.Context, msg *types.MsgBeginRed
}, nil
}

// Undelegate defines a method for performing an undelegation from a delegate and a validator
func (k msgServer) Undelegate(goCtx context.Context, msg *types.MsgUndelegate) (*types.MsgUndelegateResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

Expand Down
18 changes: 9 additions & 9 deletions x/staking/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ const (
historicalEntries = "historical_entries"
)

// GenUnbondingTime randomized UnbondingTime
func GenUnbondingTime(r *rand.Rand) (ubdTime time.Duration) {
// genUnbondingTime returns randomized UnbondingTime
func genUnbondingTime(r *rand.Rand) (ubdTime time.Duration) {
return time.Duration(simulation.RandIntBetween(r, 60, 60*60*24*3*2)) * time.Second
}

// GenMaxValidators randomized MaxValidators
func GenMaxValidators(r *rand.Rand) (maxValidators uint32) {
// genMaxValidators returns randomized MaxValidators
func genMaxValidators(r *rand.Rand) (maxValidators uint32) {
return uint32(r.Intn(250) + 1)
}

// GetHistEntries randomized HistoricalEntries between 0-100.
func GetHistEntries(r *rand.Rand) uint32 {
// getHistEntries returns randomized HistoricalEntries between 0-100.
func getHistEntries(r *rand.Rand) uint32 {
return uint32(r.Intn(int(types.DefaultHistoricalEntries + 1)))
}

Expand All @@ -47,17 +47,17 @@ func RandomizedGenState(simState *module.SimulationState) {

simState.AppParams.GetOrGenerate(
simState.Cdc, unbondingTime, &unbondTime, simState.Rand,
func(r *rand.Rand) { unbondTime = GenUnbondingTime(r) },
func(r *rand.Rand) { unbondTime = genUnbondingTime(r) },
)

simState.AppParams.GetOrGenerate(
simState.Cdc, maxValidators, &maxVals, simState.Rand,
func(r *rand.Rand) { maxVals = GenMaxValidators(r) },
func(r *rand.Rand) { maxVals = genMaxValidators(r) },
)

simState.AppParams.GetOrGenerate(
simState.Cdc, historicalEntries, &histEntries, simState.Rand,
func(r *rand.Rand) { histEntries = GetHistEntries(r) },
func(r *rand.Rand) { histEntries = getHistEntries(r) },
)

// NOTE: the slashing module need to be defined after the staking module on the
Expand Down
2 changes: 1 addition & 1 deletion x/staking/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestRandomizedGenState(t *testing.T) {
require.Equal(t, "1", stakingGenesis.Validators[2].MinSelfDelegation.String())
}

// TestRandomizedGenState tests abnormal scenarios of applying RandomizedGenState.
// TestRandomizedGenState1 tests abnormal scenarios of applying RandomizedGenState.
func TestRandomizedGenState1(t *testing.T) {
interfaceRegistry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(interfaceRegistry)
Expand Down
9 changes: 4 additions & 5 deletions x/staking/simulation/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (
"fmt"
"math/rand"

"github.com/cosmos/cosmos-sdk/x/simulation"

simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)

Expand All @@ -18,17 +17,17 @@ func ParamChanges(r *rand.Rand) []simtypes.ParamChange {
return []simtypes.ParamChange{
simulation.NewSimParamChange(types.ModuleName, string(types.KeyMaxValidators),
func(r *rand.Rand) string {
return fmt.Sprintf("%d", GenMaxValidators(r))
return fmt.Sprintf("%d", genMaxValidators(r))
},
),
simulation.NewSimParamChange(types.ModuleName, string(types.KeyUnbondingTime),
func(r *rand.Rand) string {
return fmt.Sprintf("\"%d\"", GenUnbondingTime(r))
return fmt.Sprintf("\"%d\"", genUnbondingTime(r))
},
),
simulation.NewSimParamChange(types.ModuleName, string(types.KeyHistoricalEntries),
func(r *rand.Rand) string {
return fmt.Sprintf("%d", GetHistEntries(r))
return fmt.Sprintf("%d", getHistEntries(r))
},
),
}
Expand Down
4 changes: 4 additions & 0 deletions x/staking/spec/01_state.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ order: 1

# State

## Pool

Pool is used for tracking bonded and not-bonded token supply of the bond denomination.

## LastTotalPower

LastTotalPower tracks the total amounts of bonded tokens recorded during the previous end block.
Expand Down
Loading

0 comments on commit e96839d

Please sign in to comment.