-
Notifications
You must be signed in to change notification settings - Fork 11
/
comission.go
82 lines (70 loc) · 2.51 KB
/
comission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package ante
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/authz"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
var minCommission = sdk.NewDecWithPrec(0, 2) // 0%
// ValidatorCommissionDecorator validates that the validator commission is always
// greater or equal than the min commission rate
type ValidatorCommissionDecorator struct {
cdc codec.BinaryCodec
}
// NewValidatorCommissionDecorator creates a new NewValidatorCommissionDecorator
func NewValidatorCommissionDecorator(cdc codec.BinaryCodec) ValidatorCommissionDecorator {
return ValidatorCommissionDecorator{
cdc: cdc,
}
}
// AnteHandle checks if the tx contains a staking create validator or edit validator.
// It errors if the the commission rate is below the min threshold.
func (vcd ValidatorCommissionDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
for _, msg := range tx.GetMsgs() {
switch msg := msg.(type) {
case *authz.MsgExec:
// Check for bypassing authorization
if err := vcd.validateAuthz(ctx, msg); err != nil {
return ctx, err
}
default:
if err := vcd.validateMsg(ctx, msg); err != nil {
return ctx, err
}
}
}
return next(ctx, tx, simulate)
}
// validateAuthz validates the authorization internal message
func (vcd ValidatorCommissionDecorator) validateAuthz(ctx sdk.Context, execMsg *authz.MsgExec) error {
for _, v := range execMsg.Msgs {
var innerMsg sdk.Msg
err := vcd.cdc.UnpackAny(v, &innerMsg)
if err != nil {
return sdkerrors.Wrap(err, "cannot unmarshal authz exec msgs")
}
if err := vcd.validateMsg(ctx, innerMsg); err != nil {
return err
}
}
return nil
}
// validateMsg checks that the commission rate is over 5% for create and edit validator msgs
func (vcd ValidatorCommissionDecorator) validateMsg(_ sdk.Context, msg sdk.Msg) error {
switch msg := msg.(type) {
case *stakingtypes.MsgCreateValidator:
if msg.Commission.Rate.LT(minCommission) {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest,
"validator commission %s be lower than minimum of %s", msg.Commission.Rate, minCommission)
}
case *stakingtypes.MsgEditValidator:
if msg.CommissionRate != nil && msg.CommissionRate.LT(minCommission) {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest,
"validator commission %s be lower than minimum of %s", msg.CommissionRate, minCommission)
}
}
return nil
}