-
Notifications
You must be signed in to change notification settings - Fork 845
/
vesting.go
176 lines (152 loc) · 5.53 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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"
evmtypes "github.com/tharsis/ethermint/x/evm/types"
vestingtypes "github.com/tharsis/evmos/v4/x/vesting/types"
)
// EthVestingTransactionDecorator validates if clawback vesting accounts are
// permitted to perform Ethereum Tx.
type EthVestingTransactionDecorator struct {
ak evmtypes.AccountKeeper
}
func NewEthVestingTransactionDecorator(ak evmtypes.AccountKeeper) EthVestingTransactionDecorator {
return EthVestingTransactionDecorator{
ak: ak,
}
}
// AnteHandle validates that a clawback vesting account has surpassed the
// vesting cliff and lockup period.
//
// This AnteHandler decorator will fail if:
// - the message is not a MsgEthereumTx
// - sender account cannot be found
// - sender account is not a ClawbackvestingAccount
// - blocktime is before surpassing vesting cliff end (with zero vested coins) AND
// - blocktime is before surpassing all lockup periods (with non-zero locked coins)
func (vtd EthVestingTransactionDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
for _, msg := range tx.GetMsgs() {
msgEthTx, ok := msg.(*evmtypes.MsgEthereumTx)
if !ok {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest,
"invalid message type %T, expected %T", msg, (*evmtypes.MsgEthereumTx)(nil),
)
}
acc := vtd.ak.GetAccount(ctx, msgEthTx.GetFrom())
if acc == nil {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress,
"account %s does not exist", acc)
}
// Check that this decorator only applies to clawback vesting accounts
clawbackAccount, isClawback := acc.(*vestingtypes.ClawbackVestingAccount)
if !isClawback {
return next(ctx, tx, simulate)
}
// Error if vesting cliff has not passed (with zero vested coins). This
// rule does not apply for existing clawback accounts that receive a new
// grant while there are already vested coins on the account.
vested := clawbackAccount.GetVestedCoins(ctx.BlockTime())
if len(vested) == 0 {
return ctx, sdkerrors.Wrapf(vestingtypes.ErrInsufficientVestedCoins,
"cannot perform Ethereum tx with clawback vesting account, that has no vested coins: %s", vested,
)
}
// Error if account has locked coins (before surpassing all lockup periods)
islocked := clawbackAccount.HasLockedCoins(ctx.BlockTime())
if islocked {
return ctx, sdkerrors.Wrapf(vestingtypes.ErrVestingLockup,
"cannot perform Ethereum tx with clawback vesting account, that has locked coins: %s", vested,
)
}
}
return next(ctx, tx, simulate)
}
// 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 sdkerrors.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 sdkerrors.Wrapf(
sdkerrors.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 sdkerrors.Wrap(
vestingtypes.ErrInsufficientVestedCoins,
"account has no vested coins",
)
}
vested := coins.AmountOf(bondDenom)
if vested.LT(delegateMsg.Amount.Amount) {
return sdkerrors.Wrapf(
vestingtypes.ErrInsufficientVestedCoins,
"cannot delegate unvested coins. coins vested < delegation amount (%s < %s)",
vested, delegateMsg.Amount.Amount,
)
}
}
return nil
}