-
Notifications
You must be signed in to change notification settings - Fork 39
/
ante.go
103 lines (85 loc) · 2.81 KB
/
ante.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
91
92
93
94
95
96
97
98
99
100
101
102
103
package ante
import (
"fmt"
errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authz "github.com/cosmos/cosmos-sdk/x/authz"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
dyncommkeeper "github.com/classic-terra/core/v3/x/dyncomm/keeper"
)
// DyncommDecorator checks for EditValidator and rejects
// edits that do not conform with dyncomm
type DyncommDecorator struct {
dyncommKeeper dyncommkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
cdc codec.BinaryCodec
}
func NewDyncommDecorator(cdc codec.BinaryCodec, dk dyncommkeeper.Keeper, sk *stakingkeeper.Keeper) DyncommDecorator {
return DyncommDecorator{
dyncommKeeper: dk,
stakingKeeper: sk,
cdc: cdc,
}
}
func (dd DyncommDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
if simulate {
return next(ctx, tx, simulate)
}
msgs := tx.GetMsgs()
err := dd.FilterMsgsAndProcessMsgs(ctx, msgs...)
if err != nil {
return ctx, err
}
return next(ctx, tx, simulate)
}
func (dd DyncommDecorator) FilterMsgsAndProcessMsgs(ctx sdk.Context, msgs ...sdk.Msg) (err error) {
for _, msg := range msgs {
switch msg := msg.(type) {
case *stakingtypes.MsgEditValidator:
err = dd.ProcessEditValidator(ctx, msg)
case *authz.MsgExec:
messages, msgerr := msg.GetMessages()
if msgerr == nil {
err = dd.FilterMsgsAndProcessMsgs(ctx, messages...)
}
case *channeltypes.MsgRecvPacket:
var data icatypes.InterchainAccountPacketData
err = icatypes.ModuleCdc.UnmarshalJSON(msg.Packet.GetData(), &data)
if err != nil {
continue
}
if data.Type != icatypes.EXECUTE_TX {
continue
}
messages, msgerr := icatypes.DeserializeCosmosTx(dd.cdc, data.Data)
if msgerr == nil {
err = dd.FilterMsgsAndProcessMsgs(ctx, messages...)
}
default:
continue
}
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrUnauthorized, err.Error())
}
}
return nil
}
func (dd DyncommDecorator) ProcessEditValidator(ctx sdk.Context, msg sdk.Msg) (err error) {
msgEditValidator := msg.(*stakingtypes.MsgEditValidator)
// no update of CommissionRate provided
if msgEditValidator.CommissionRate == nil {
return nil
}
operator := msgEditValidator.ValidatorAddress
newIntendedRate := msgEditValidator.CommissionRate
dynMinRate := dd.dyncommKeeper.GetDynCommissionRate(ctx, operator)
if newIntendedRate.LT(dynMinRate) {
return fmt.Errorf("commission for %s must be at least %f", operator, dynMinRate.MustFloat64())
}
return nil
}