-
Notifications
You must be signed in to change notification settings - Fork 834
/
msg_server_test.go
271 lines (241 loc) · 8.67 KB
/
msg_server_test.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright Tharsis Labs Ltd.(Evmos)
// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE)
package keeper_test
import (
"testing"
"time"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/evmos/evmos/v18/testutil"
"github.com/evmos/evmos/v18/testutil/integration/evmos/network"
utiltx "github.com/evmos/evmos/v18/testutil/tx"
"github.com/evmos/evmos/v18/utils"
"github.com/evmos/evmos/v18/x/staking/keeper"
vestingtypes "github.com/evmos/evmos/v18/x/vesting/types"
"github.com/stretchr/testify/require"
)
func TestMsgDelegate(t *testing.T) {
var (
ctx sdk.Context
nw *network.UnitTestNetwork
defaultDelCoin = sdk.NewCoin(utils.BaseDenom, math.NewInt(1e18))
delegatorAddr, _ = utiltx.NewAccAddressAndKey()
funderAddr, _ = utiltx.NewAccAddressAndKey()
)
testCases := []struct { //nolint:dupl
name string
setup func() sdk.Coin
expErr bool
errMsg string
}{
{
name: "can delegate from a common EthAccount",
setup: func() sdk.Coin {
// Send some funds to delegator account
err := testutil.FundAccountWithBaseDenom(ctx, nw.App.BankKeeper, delegatorAddr, defaultDelCoin.Amount.Int64())
require.NoError(t, err)
return defaultDelCoin
},
expErr: false,
},
{
name: "can delegate free coins from a ClawbackVestingAccount",
setup: func() sdk.Coin {
err := setupClawbackVestingAccount(ctx, nw, delegatorAddr, funderAddr, testutil.TestVestingSchedule.TotalVestingCoins.Add(defaultDelCoin))
require.NoError(t, err)
return defaultDelCoin
},
expErr: false,
},
{
name: "cannot delegate unvested coins from a ClawbackVestingAccount",
setup: func() sdk.Coin {
err := setupClawbackVestingAccount(ctx, nw, delegatorAddr, funderAddr, testutil.TestVestingSchedule.TotalVestingCoins)
require.NoError(t, err)
return defaultDelCoin
},
expErr: true,
errMsg: "cannot delegate unvested coins",
},
{
name: "can delegate locked vested coins from a ClawbackVestingAccount",
setup: func() sdk.Coin {
err := setupClawbackVestingAccount(ctx, nw, delegatorAddr, funderAddr, testutil.TestVestingSchedule.TotalVestingCoins)
require.NoError(t, err)
// after first vesting period and before lockup
// some vested tokens, but still all locked
cliffDuration := time.Duration(testutil.TestVestingSchedule.CliffPeriodLength)
require.NoError(t, nw.NextBlockAfter(cliffDuration*time.Second))
ctx = nw.GetContext()
acc := nw.App.AccountKeeper.GetAccount(ctx, delegatorAddr)
vestAcc, ok := acc.(*vestingtypes.ClawbackVestingAccount)
require.True(t, ok)
// check that locked vested is > 0
lockedVested := vestAcc.GetLockedUpVestedCoins(ctx.BlockTime())
require.True(t, lockedVested.IsAllGT(sdk.NewCoins()))
// returned delegation coins are the locked vested coins
return lockedVested[0]
},
expErr: false,
},
{
name: "can delegate unlocked vested coins from a ClawbackVestingAccount",
setup: func() sdk.Coin {
err := setupClawbackVestingAccount(ctx, nw, delegatorAddr, funderAddr, testutil.TestVestingSchedule.TotalVestingCoins)
require.NoError(t, err)
// Between first and second lockup periods
// vested coins are unlocked
lockDuration := time.Duration(testutil.TestVestingSchedule.LockupPeriodLength)
require.NoError(t, nw.NextBlockAfter(lockDuration*time.Second))
ctx = nw.GetContext()
acc := nw.App.AccountKeeper.GetAccount(ctx, delegatorAddr)
vestAcc, ok := acc.(*vestingtypes.ClawbackVestingAccount)
require.True(t, ok)
unlockedVested := vestAcc.GetUnlockedVestedCoins(ctx.BlockTime())
require.True(t, unlockedVested.IsAllGT(sdk.NewCoins()))
// returned delegation coins are the locked vested coins
return unlockedVested[0]
},
expErr: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
nw = network.NewUnitTestNetwork()
ctx = nw.GetContext()
delCoin := tc.setup()
srv := keeper.NewMsgServerImpl(&nw.App.StakingKeeper)
res, err := srv.Delegate(ctx, &types.MsgDelegate{
DelegatorAddress: delegatorAddr.String(),
ValidatorAddress: nw.GetValidators()[0].OperatorAddress,
Amount: delCoin,
})
if tc.expErr {
require.Error(t, err)
require.Contains(t, err.Error(), tc.errMsg)
} else {
require.NoError(t, err)
require.NotNil(t, res)
}
})
}
}
func TestMsgCreateValidator(t *testing.T) {
var (
ctx sdk.Context
nw *network.UnitTestNetwork
defaultDelCoin = sdk.NewCoin(utils.BaseDenom, math.NewInt(1e18))
validatorAddr, _ = utiltx.NewAccAddressAndKey()
funderAddr, _ = utiltx.NewAccAddressAndKey()
)
testCases := []struct { //nolint:dupl
name string
setup func() sdk.Coin
expErr bool
errMsg string
}{
{
name: "can create a validator using a common EthAccount",
setup: func() sdk.Coin {
// Send some funds to delegator account
err := testutil.FundAccountWithBaseDenom(ctx, nw.App.BankKeeper, validatorAddr, defaultDelCoin.Amount.Int64())
require.NoError(t, err)
return defaultDelCoin
},
expErr: false,
},
{
name: "can create a validator using a ClawbackVestingAccount and free tokens in self delegation",
setup: func() sdk.Coin {
err := setupClawbackVestingAccount(ctx, nw, validatorAddr, funderAddr, testutil.TestVestingSchedule.TotalVestingCoins.Add(defaultDelCoin))
require.NoError(t, err)
return defaultDelCoin
},
expErr: false,
},
{
name: "cannot create a validator using a ClawbackVestingAccount and unvested tokens in self delegation",
setup: func() sdk.Coin {
err := setupClawbackVestingAccount(ctx, nw, validatorAddr, funderAddr, testutil.TestVestingSchedule.TotalVestingCoins)
require.NoError(t, err)
return defaultDelCoin
},
expErr: true,
errMsg: "cannot delegate unvested coins",
},
{
name: "can create a validator using a ClawbackVestingAccount and locked vested coins in self delegation",
setup: func() sdk.Coin {
err := setupClawbackVestingAccount(ctx, nw, validatorAddr, funderAddr, testutil.TestVestingSchedule.TotalVestingCoins)
require.NoError(t, err)
// after first vesting period and before lockup
// some vested tokens, but still all locked
cliffDuration := time.Duration(testutil.TestVestingSchedule.CliffPeriodLength)
require.NoError(t, nw.NextBlockAfter(cliffDuration*time.Second))
ctx = nw.GetContext()
acc := nw.App.AccountKeeper.GetAccount(ctx, validatorAddr)
vestAcc, ok := acc.(*vestingtypes.ClawbackVestingAccount)
require.True(t, ok)
// check that locked vested is > 0
lockedVested := vestAcc.GetLockedUpVestedCoins(ctx.BlockTime())
require.True(t, lockedVested.IsAllGT(sdk.NewCoins()))
// returned delegation coins are the locked vested coins
return lockedVested[0]
},
expErr: false,
},
{
name: "can create a validator using a ClawbackVestingAccount and unlocked vested coins in self delegation",
setup: func() sdk.Coin {
err := setupClawbackVestingAccount(ctx, nw, validatorAddr, funderAddr, testutil.TestVestingSchedule.TotalVestingCoins)
require.NoError(t, err)
// Between first and second lockup periods
// vested coins are unlocked
lockDuration := time.Duration(testutil.TestVestingSchedule.LockupPeriodLength)
require.NoError(t, nw.NextBlockAfter(lockDuration*time.Second))
ctx = nw.GetContext()
acc := nw.App.AccountKeeper.GetAccount(ctx, validatorAddr)
vestAcc, ok := acc.(*vestingtypes.ClawbackVestingAccount)
require.True(t, ok)
unlockedVested := vestAcc.GetUnlockedVestedCoins(ctx.BlockTime())
require.True(t, unlockedVested.IsAllGT(sdk.NewCoins()))
// returned delegation coins are the locked vested coins
return unlockedVested[0]
},
expErr: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
nw = network.NewUnitTestNetwork()
ctx = nw.GetContext()
coinToSelfBond := tc.setup()
pubKey := ed25519.GenPrivKey().PubKey()
commissions := types.NewCommissionRates(
sdk.NewDecWithPrec(5, 2),
sdk.NewDecWithPrec(2, 1),
sdk.NewDecWithPrec(5, 2),
)
msg, err := types.NewMsgCreateValidator(
sdk.ValAddress(validatorAddr),
pubKey,
coinToSelfBond,
types.NewDescription("T", "E", "S", "T", "Z"),
commissions,
sdk.OneInt(),
)
require.NoError(t, err)
srv := keeper.NewMsgServerImpl(&nw.App.StakingKeeper)
res, err := srv.CreateValidator(ctx, msg)
if tc.expErr {
require.Error(t, err)
require.Contains(t, err.Error(), tc.errMsg)
} else {
require.NoError(t, err)
require.NotNil(t, res)
}
})
}
}