Skip to content

Commit

Permalink
min commission rate check on create validator (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
antstalepresh authored and ValarDragon committed Aug 3, 2021
1 parent 791522e commit c034987
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
4 changes: 0 additions & 4 deletions x/staking/keeper/alias_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,7 @@ func (k Keeper) ValidatorByConsAddr(ctx sdk.Context, addr sdk.ConsAddress) types
return val
}

<<<<<<< HEAD
// _______________________________________________________________________
=======
// ______________________________________________________________________
>>>>>>> Fix silly lints
// Delegation Set

// Returns self as it is both a validatorset and delegationset
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 @@ -71,6 +71,11 @@ func (k msgServer) CreateValidator(goCtx context.Context, msg *types.MsgCreateVa
if err != nil {
return nil, err
}

if msg.Commission.Rate.LT(k.MinCommissionRate(ctx)) {
return nil, sdkerrors.Wrapf(types.ErrCommissionLTMinRate, "cannot set validator commission to less than minimum rate of %s", k.MinCommissionRate(ctx))
}

commission := types.NewCommissionWithTime(
msg.Commission.Rate, msg.Commission.MaxRate,
msg.Commission.MaxChangeRate, ctx.BlockHeader().Time,
Expand Down
42 changes: 42 additions & 0 deletions x/staking/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package keeper_test

import (
"testing"

"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

func TestCreateValidatorWithLessThanMinCommission(t *testing.T) {
PKS := simapp.CreateTestPubKeys(1)
valConsPk1 := PKS[0]

app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})

addrs := simapp.AddTestAddrs(app, ctx, 3, sdk.NewInt(1234))

// set min commission rate to non-zero
params := app.StakingKeeper.GetParams(ctx)
params.MinCommissionRate = sdk.NewDecWithPrec(1, 2)
app.StakingKeeper.SetParams(ctx, params)

// create validator with 0% commission
msg, err := stakingtypes.NewMsgCreateValidator(
sdk.ValAddress(addrs[0]),
valConsPk1,
sdk.NewInt64Coin(sdk.DefaultBondDenom, 100),
stakingtypes.Description{},
stakingtypes.NewCommissionRates(sdk.NewDec(0), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)),
sdk.OneInt())
require.NoError(t, err)

sh := staking.NewHandler(app.StakingKeeper)
_, err = sh(ctx, msg)
require.Error(t, err)
}
1 change: 1 addition & 0 deletions x/staking/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ var (
ErrInvalidHistoricalInfo = sdkerrors.Register(ModuleName, 45, "invalid historical info")
ErrNoHistoricalInfo = sdkerrors.Register(ModuleName, 46, "no historical info found")
ErrEmptyValidatorPubKey = sdkerrors.Register(ModuleName, 47, "empty validator public key")
ErrCommissionLTMinRate = sdkerrors.Register(ModuleName, 48, "commission cannot be less than min rate")
)

0 comments on commit c034987

Please sign in to comment.