forked from cosmos/interchain-security
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_assignment.go
341 lines (287 loc) · 10.7 KB
/
key_assignment.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package integration
import (
"github.com/cosmos/ibc-go/v8/testing/mock"
"cosmossdk.io/math"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
tmencoding "github.com/cometbft/cometbft/crypto/encoding"
tmprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
providerkeeper "github.com/cosmos/interchain-security/v6/x/ccv/provider/keeper"
"github.com/cosmos/interchain-security/v6/x/ccv/provider/types"
ccv "github.com/cosmos/interchain-security/v6/x/ccv/types"
)
// TestKeyAssignment tests key assignments relayed from the provider chain to the consumer chain at different times in the protocol lifecycle.
// @Long Description@
// Each test scenario sets up a provider chain and then assigns a key for a validator.
// However, the assignment comes at different times in the protocol lifecycle.
// The test covers the following scenarios:
// * successfully assign the key before the CCV channel initialization is complete, then check that a VSCPacket is indeed queued
// * successfully assign the key after the CCV channel initialization is complete
// * successfully assign the key during an same epoch where the validator power changes
// * get an error when assigning the same key twice in the same block by different validators
// * get an error when assigning the same key twice in the same block by the same validator
// * successfully assign two different keys in the same block by one validator
// * get an error when assigning the same key twice in different blocks by different validators
// * get an error when assigning the same key twice in different blocks by the same validator
// For each scenario where the key assignment does not produce an error,
// the test also checks that VSCPackets are relayed to the consumer chain and that the clients on
// the provider and consumer chain can be updated.
// TODO: Remove panics when unexpected error occurs.
func (s *CCVTestSuite) TestKeyAssignment() {
testCases := []struct {
name string
assignFunc func(*providerkeeper.Keeper) error
expError bool
expPacketCount int
}{
{
"assignment during channel init", func(pk *providerkeeper.Keeper) error {
// key assignment
validator, consumerKey := generateNewConsumerKey(s, 0)
err := pk.AssignConsumerKey(s.providerCtx(), s.getFirstBundle().ConsumerId, validator, consumerKey)
if err != nil {
return err
}
// check that a VSCPacket is queued
s.nextEpoch()
pendingPackets := pk.GetPendingVSCPackets(s.providerCtx(), s.getFirstBundle().ConsumerId)
s.Require().Len(pendingPackets, 1)
// establish CCV channel
s.SetupCCVChannel(s.path)
return nil
}, false, 2,
},
{
"assignment after channel init", func(pk *providerkeeper.Keeper) error {
// establish CCV channel
s.SetupCCVChannel(s.path)
// key assignment
validator, consumerKey := generateNewConsumerKey(s, 0)
err := pk.AssignConsumerKey(s.providerCtx(), s.getFirstBundle().ConsumerId, validator, consumerKey)
if err != nil {
return err
}
s.nextEpoch()
return nil
}, false, 2,
},
{
"assignment with power change", func(pk *providerkeeper.Keeper) error {
// establish CCV channel
s.SetupCCVChannel(s.path)
// key assignment
validator, consumerKey := generateNewConsumerKey(s, 0)
err := pk.AssignConsumerKey(s.providerCtx(), s.getFirstBundle().ConsumerId, validator, consumerKey)
if err != nil {
return err
}
// Bond some tokens on provider to change validator powers
bondAmt := math.NewInt(1000000)
delAddr := s.providerChain.SenderAccount.GetAddress()
delegate(s, delAddr, bondAmt)
s.nextEpoch()
return nil
}, false, 2,
},
{
"double same-key assignment in same block by different vals", func(pk *providerkeeper.Keeper) error {
// establish CCV channel
s.SetupCCVChannel(s.path)
// key assignment
validator, consumerKey := generateNewConsumerKey(s, 0)
err := pk.AssignConsumerKey(s.providerCtx(), s.getFirstBundle().ConsumerId, validator, consumerKey)
if err != nil {
panic(err)
}
// same key assignment, but different validator
validator2, _ := generateNewConsumerKey(s, 1)
err = pk.AssignConsumerKey(s.providerCtx(), s.getFirstBundle().ConsumerId, validator2, consumerKey)
if err != nil {
return err
}
s.nextEpoch()
return nil
}, true, 2,
},
{
"double same-key assignment in same block by same val", func(pk *providerkeeper.Keeper) error {
// establish CCV channel
s.SetupCCVChannel(s.path)
// key assignment
validator, consumerKey := generateNewConsumerKey(s, 0)
err := pk.AssignConsumerKey(s.providerCtx(), s.getFirstBundle().ConsumerId, validator, consumerKey)
if err != nil {
panic(err)
}
// same key assignment, but different validator
err = pk.AssignConsumerKey(s.providerCtx(), s.getFirstBundle().ConsumerId, validator, consumerKey)
if err != nil {
return err
}
s.nextEpoch()
return nil
}, true, 2,
},
{
"double key assignment in same block by same val", func(pk *providerkeeper.Keeper) error {
// establish CCV channel
s.SetupCCVChannel(s.path)
// key assignment
validator, consumerKey := generateNewConsumerKey(s, 0)
err := pk.AssignConsumerKey(s.providerCtx(), s.getFirstBundle().ConsumerId, validator, consumerKey)
if err != nil {
return err
}
// same key assignment
validator, consumerKey = generateNewConsumerKey(s, 0)
err = pk.AssignConsumerKey(s.providerCtx(), s.getFirstBundle().ConsumerId, validator, consumerKey)
if err != nil {
return err
}
s.nextEpoch()
return nil
}, false, 2,
},
{
"double same-key assignment in different blocks by different vals", func(pk *providerkeeper.Keeper) error {
// establish CCV channel
s.SetupCCVChannel(s.path)
// key assignment
validator, consumerKey := generateNewConsumerKey(s, 0)
err := pk.AssignConsumerKey(s.providerCtx(), s.getFirstBundle().ConsumerId, validator, consumerKey)
if err != nil {
panic(err)
}
s.nextEpoch()
// same key assignment
validator2, _ := generateNewConsumerKey(s, 1)
err = pk.AssignConsumerKey(s.providerCtx(), s.getFirstBundle().ConsumerId, validator2, consumerKey)
if err != nil {
return err
}
// check that the key was not assigned to the second validator
valConsAddr2, getConsAddrErr := validator2.GetConsAddr() // make sure we don't override err, which we are saving for below
s.Require().NoError(getConsAddrErr)
actualConsumerKey2, found := pk.GetValidatorConsumerPubKey(s.providerCtx(), s.consumerChain.ChainID, types.NewProviderConsAddress(valConsAddr2))
s.Require().True(found)
// the key for the second validator should *not* be the one we just assigned to the first validator
s.Require().NotEqual(consumerKey, actualConsumerKey2)
s.nextEpoch()
return nil
}, true, 2,
},
{
"double same-key assignment in different blocks by same val", func(pk *providerkeeper.Keeper) error {
// establish CCV channel
s.SetupCCVChannel(s.path)
// key assignment
validator, consumerKey := generateNewConsumerKey(s, 0)
err := pk.AssignConsumerKey(s.providerCtx(), s.getFirstBundle().ConsumerId, validator, consumerKey)
if err != nil {
return err
}
s.nextEpoch()
// same key assignment
err = pk.AssignConsumerKey(s.providerCtx(), s.getFirstBundle().ConsumerId, validator, consumerKey)
if err != nil {
return err
}
s.nextEpoch()
return nil
}, true, 2,
},
{
"double key assignment in different blocks by same val", func(pk *providerkeeper.Keeper) error {
// establish CCV channel
s.SetupCCVChannel(s.path)
// key assignment
validator, consumerKey := generateNewConsumerKey(s, 0)
err := pk.AssignConsumerKey(s.providerCtx(), s.getFirstBundle().ConsumerId, validator, consumerKey)
if err != nil {
return err
}
s.nextEpoch()
// same key assignment
validator, consumerKey = generateNewConsumerKey(s, 0)
err = pk.AssignConsumerKey(s.providerCtx(), s.getFirstBundle().ConsumerId, validator, consumerKey)
if err != nil {
return err
}
s.nextEpoch()
return nil
}, false, 3,
},
// TODO: this test should pass if we manage to change the client update mode to sequential
// {
// "key assignment for all validators in the same block", func(pk *providerkeeper.Keeper) error {
// // establish CCV channel
// s.SetupCCVChannel(s.path)
// // key assignment
// for i, _ := range s.providerChain.Vals.Validators {
// validator, consumerKey := generateNewConsumerKey(s, i)
// err := pk.AssignConsumerKey(s.providerCtx(), s.consumerChain.ChainID, validator, consumerKey)
// if err != nil {
// return err
// }
// }
// // vscPakcketData := pk.GetPendingPackets(s.providerCtx(), s.consumerChain.ChainID)
// // s.Require().Len(vscPakcketData, 1)
// // s.Require().Len(vscPakcketData[0].ValidatorUpdates, 2*len(s.providerChain.Vals.Validators))
// s.providerChain.NextBlock()
// return nil
// }, false, 2,
// },
}
for i, tc := range testCases {
providerKeeper := s.providerApp.GetProviderKeeper()
err := tc.assignFunc(&providerKeeper)
if tc.expError {
s.Require().Error(err, "test: "+tc.name)
} else {
s.Require().NoError(err, "test: "+tc.name)
}
if !tc.expError {
// Bond some tokens on provider to change validator powers
bondAmt := math.NewInt(1000000)
delAddr := s.providerChain.SenderAccount.GetAddress()
delegate(s, delAddr, bondAmt)
// Send CCV packet to consumer
// s.providerChain.NextBlock()
s.nextEpoch()
// Relay all VSC packets from provider to consumer
relayAllCommittedPackets(
s,
s.providerChain,
s.path,
ccv.ProviderPortID,
s.path.EndpointB.ChannelID,
tc.expPacketCount,
"test: "+tc.name,
)
// update clients
err := s.path.EndpointA.UpdateClient()
s.Require().NoError(err)
err = s.path.EndpointB.UpdateClient()
s.Require().NoError(err)
}
if i+1 < len(testCases) {
// reset suite to reset provider client
s.SetupTest()
}
}
}
// generateNewConsumerKey generate new consumer key for the validator with valIndex
func generateNewConsumerKey(s *CCVTestSuite, valIndex int) (stakingtypes.Validator, tmprotocrypto.PublicKey) {
// get validator
s.Require().Less(valIndex, len(s.providerChain.Vals.Validators))
_, valAddr := s.getValByIdx(valIndex)
validator := s.getVal(s.providerCtx(), valAddr)
// generate new PrivValidator
privVal := mock.NewPV()
tmPubKey, err := privVal.GetPubKey()
s.Require().NoError(err)
pubKey, err := tmencoding.PubKeyToProto(tmPubKey)
s.Require().NoError(err)
// add Signer to the consumer chain
s.consumerChain.Signers[tmPubKey.Address().String()] = privVal
return validator, pubKey
}