Skip to content

Commit

Permalink
add mini commission limit for the validator (#128)
Browse files Browse the repository at this point in the history
* add mini commission limit for the validator

* update app
  • Loading branch information
EG-easy committed Feb 8, 2022
1 parent b9bf136 commit 08b6a2f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
77 changes: 77 additions & 0 deletions app/ante_handler.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package app

import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/authz"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
channelkeeper "github.com/cosmos/ibc-go/v2/modules/core/04-channel/keeper"
ibcante "github.com/cosmos/ibc-go/v2/modules/core/ante"
)
Expand All @@ -14,6 +17,79 @@ type HandlerOptions struct {
ante.HandlerOptions

IBCChannelkeeper channelkeeper.Keeper
Cdc codec.BinaryCodec
}

type MinCommissionDecorator struct {
cdc codec.BinaryCodec
}

func NewMinCommissionDecorator(cdc codec.BinaryCodec) MinCommissionDecorator {
return MinCommissionDecorator{cdc}
}

func (min MinCommissionDecorator) AnteHandle(
ctx sdk.Context, tx sdk.Tx,
simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
msgs := tx.GetMsgs()
minCommissionRate := sdk.NewDecWithPrec(5, 2)

validMsg := func(m sdk.Msg) error {
switch msg := m.(type) {
case *stakingtypes.MsgCreateValidator:
// prevent new validators joining the set with
// commission set below 5%
c := msg.Commission
if c.Rate.LT(minCommissionRate) {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "commission can't be lower than 5%")
}
case *stakingtypes.MsgEditValidator:
// if commission rate is nil, it means only
// other fields are affected - skip
if msg.CommissionRate == nil {
break
}
if msg.CommissionRate.LT(minCommissionRate) {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "commission can't be lower than 5%")
}
}

return nil
}

validAuthz := func(execMsg *authz.MsgExec) error {
for _, v := range execMsg.Msgs {
var innerMsg sdk.Msg
err := min.cdc.UnpackAny(v, &innerMsg)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "cannot unmarshal authz exec msgs")
}

err = validMsg(innerMsg)
if err != nil {
return err
}
}

return nil
}

for _, m := range msgs {
if msg, ok := m.(*authz.MsgExec); ok {
if err := validAuthz(msg); err != nil {
return ctx, err
}
continue
}

// validate normal msgs
err = validMsg(m)
if err != nil {
return ctx, err
}
}

return next(ctx, tx, simulate)
}

func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
Expand All @@ -34,6 +110,7 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {

anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(),
NewMinCommissionDecorator(options.Cdc),
ante.NewRejectExtensionOptionsDecorator(),
ante.NewMempoolFeeDecorator(),
ante.NewValidateBasicDecorator(),
Expand Down
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ func NewNibiruApp(
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
},
IBCChannelkeeper: app.IBCKeeper.ChannelKeeper,
Cdc: appCodec,
},
)
if err != nil {
Expand Down

0 comments on commit 08b6a2f

Please sign in to comment.