forked from cosmos/interchain-security
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_assignment.go
259 lines (220 loc) · 7.22 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
package integration
import (
"github.com/cosmos/ibc-go/v7/testing/mock"
sdk "github.com/cosmos/cosmos-sdk/types"
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/v4/x/ccv/provider/keeper"
ccv "github.com/cosmos/interchain-security/v4/x/ccv/types"
)
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.consumerChain.ChainID, validator, consumerKey)
if err != nil {
return err
}
// check that a VSCPacket is queued
s.nextEpoch()
pendingPackets := pk.GetPendingVSCPackets(s.providerCtx(), s.consumerChain.ChainID)
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.consumerChain.ChainID, 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.consumerChain.ChainID, validator, consumerKey)
if err != nil {
return err
}
// Bond some tokens on provider to change validator powers
bondAmt := sdk.NewInt(1000000)
delAddr := s.providerChain.SenderAccount.GetAddress()
delegate(s, delAddr, bondAmt)
s.nextEpoch()
return nil
}, false, 2,
},
{
"double same-key assignment in same block", 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.consumerChain.ChainID, validator, consumerKey)
if err != nil {
return err
}
// same key assignment
err = pk.AssignConsumerKey(s.providerCtx(), s.consumerChain.ChainID, validator, consumerKey)
if err != nil {
return err
}
s.nextEpoch()
return nil
}, true, 2,
},
{
"double key assignment in same block", 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.consumerChain.ChainID, validator, consumerKey)
if err != nil {
return err
}
// same key assignment
validator, consumerKey = generateNewConsumerKey(s, 0)
err = pk.AssignConsumerKey(s.providerCtx(), s.consumerChain.ChainID, validator, consumerKey)
if err != nil {
return err
}
s.nextEpoch()
return nil
}, false, 2,
},
{
"double same-key assignment in different blocks", 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.consumerChain.ChainID, validator, consumerKey)
if err != nil {
return err
}
s.nextEpoch()
// same key assignment
err = pk.AssignConsumerKey(s.providerCtx(), s.consumerChain.ChainID, validator, consumerKey)
if err != nil {
return err
}
s.nextEpoch()
return nil
}, true, 2,
},
{
"double key assignment in different blocks", 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.consumerChain.ChainID, validator, consumerKey)
if err != nil {
return err
}
s.nextEpoch()
// same key assignment
validator, consumerKey = generateNewConsumerKey(s, 0)
err = pk.AssignConsumerKey(s.providerCtx(), s.consumerChain.ChainID, 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 := sdk.NewInt(1000000)
delAddr := s.providerChain.SenderAccount.GetAddress()
delegate(s, delAddr, bondAmt)
// Send CCV packet to consumer
s.providerChain.NextBlock()
// 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
}