forked from strangelove-ventures/poa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommission_limit.go
90 lines (72 loc) · 2.58 KB
/
commission_limit.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
83
84
85
86
87
88
89
90
package poaante
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"cosmossdk.io/math"
"github.com/strangelove-ventures/poa"
)
// CommissionLimitDecorator limits commission rates for validators between 2 ranges.
// if both ranges are the same, the rate change must only be the value both.
type CommissionLimitDecorator struct {
// if true, gentxs are also required to be within the commission limit on network start.
DoGenTxRateValidation bool
// the validator's set commission rate.
RateFloor math.LegacyDec
RateCeil math.LegacyDec
}
func NewCommissionLimitDecorator(doGenTxRateValidation bool, rateFloor, rateCiel math.LegacyDec) CommissionLimitDecorator {
if rateFloor.GT(rateCiel) {
panic(fmt.Sprintf("NewCommissionLimitDecorator: rateFloor %v is greater than rateCiel %v", rateFloor, rateCiel))
}
return CommissionLimitDecorator{
DoGenTxRateValidation: doGenTxRateValidation,
RateFloor: rateFloor,
RateCeil: rateCiel,
}
}
// AnteHandle performs an AnteHandler check that returns an error if the tx contains a message that is not within the commission limit.
func (mcl CommissionLimitDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
if !mcl.DoGenTxRateValidation && ctx.BlockHeight() <= 1 {
return next(ctx, tx, simulate)
}
err := mcl.hasInvalidCommissionRange(tx.GetMsgs())
if err != nil {
return ctx, err
}
return next(ctx, tx, simulate)
}
func (mcl CommissionLimitDecorator) hasInvalidCommissionRange(msgs []sdk.Msg) error {
for _, msg := range msgs {
// authz nested message check (recursive)
if execMsg, ok := msg.(*authz.MsgExec); ok {
msgs, err := execMsg.GetMessages()
if err != nil {
return err
}
err = mcl.hasInvalidCommissionRange(msgs)
if err != nil {
return err
}
}
switch msg := msg.(type) {
// Create Validator POA wrapper
case *poa.MsgCreateValidator:
return rateCheck(msg.Commission.Rate, mcl.RateFloor, mcl.RateCeil)
// Editing the validator through staking (no POA edit)
case *stakingtypes.MsgEditValidator:
return rateCheck(*msg.CommissionRate, mcl.RateFloor, mcl.RateCeil)
}
}
return nil
}
func rateCheck(source math.LegacyDec, low math.LegacyDec, high math.LegacyDec) error {
if low.Equal(high) && !source.Equal(low) {
return fmt.Errorf("rate %v is not equal to %v", source, low)
}
if source.GT(high) || source.LT(low) {
return fmt.Errorf("rate %v is not between %v and %v", source, low, high)
}
return nil
}