-
Notifications
You must be signed in to change notification settings - Fork 2
/
vesting.go
113 lines (97 loc) · 3.2 KB
/
vesting.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
104
105
106
107
108
109
110
111
112
113
package cosmos
import (
errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/authz"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
evmtypes "github.com/cvn-network/cvn/v2/x/evm/types"
vestingtypes "github.com/cvn-network/cvn/v2/x/vesting/types"
)
// TODO: remove once Cosmos SDK is upgraded to v0.46
// VestingDelegationDecorator validates delegation of vested coins
type VestingDelegationDecorator struct {
ak evmtypes.AccountKeeper
sk vestingtypes.StakingKeeper
cdc codec.BinaryCodec
}
// NewVestingDelegationDecorator creates a new VestingDelegationDecorator
func NewVestingDelegationDecorator(ak evmtypes.AccountKeeper, sk vestingtypes.StakingKeeper, cdc codec.BinaryCodec) VestingDelegationDecorator {
return VestingDelegationDecorator{
ak: ak,
sk: sk,
cdc: cdc,
}
}
// AnteHandle checks if the tx contains a staking delegation.
// It errors if the coins are still locked or the bond amount is greater than
// the coins already vested
func (vdd VestingDelegationDecorator) 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 := vdd.validateAuthz(ctx, msg); err != nil {
return ctx, err
}
default:
if err := vdd.validateMsg(ctx, msg); err != nil {
return ctx, err
}
}
}
return next(ctx, tx, simulate)
}
// validateAuthz validates the authorization internal message
func (vdd VestingDelegationDecorator) validateAuthz(ctx sdk.Context, execMsg *authz.MsgExec) error {
for _, v := range execMsg.Msgs {
var innerMsg sdk.Msg
if err := vdd.cdc.UnpackAny(v, &innerMsg); err != nil {
return errorsmod.Wrap(err, "cannot unmarshal authz exec msgs")
}
if err := vdd.validateMsg(ctx, innerMsg); err != nil {
return err
}
}
return nil
}
// validateMsg checks that the only vested coins can be delegated
func (vdd VestingDelegationDecorator) validateMsg(ctx sdk.Context, msg sdk.Msg) error {
delegateMsg, ok := msg.(*stakingtypes.MsgDelegate)
if !ok {
return nil
}
for _, addr := range msg.GetSigners() {
acc := vdd.ak.GetAccount(ctx, addr)
if acc == nil {
return errorsmod.Wrapf(
errortypes.ErrUnknownAddress,
"account %s does not exist", addr,
)
}
clawbackAccount, isClawback := acc.(*vestingtypes.ClawbackVestingAccount)
if !isClawback {
// continue to next decorator as this logic only applies to vesting
return nil
}
// error if bond amount is > vested coins
bondDenom := vdd.sk.BondDenom(ctx)
coins := clawbackAccount.GetVestedOnly(ctx.BlockTime())
if coins == nil || coins.Empty() {
return errorsmod.Wrap(
vestingtypes.ErrInsufficientVestedCoins,
"account has no vested coins",
)
}
vested := coins.AmountOf(bondDenom)
if vested.LT(delegateMsg.Amount.Amount) {
return errorsmod.Wrapf(
vestingtypes.ErrInsufficientVestedCoins,
"cannot delegate unvested coins. coins vested < delegation amount (%s < %s)",
vested, delegateMsg.Amount.Amount,
)
}
}
return nil
}