forked from cosmos/interchain-security
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expired_client.go
294 lines (239 loc) · 12.7 KB
/
expired_client.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
package integration
import (
"time"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
ibctesting "github.com/cosmos/ibc-go/v7/testing"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
abci "github.com/cometbft/cometbft/abci/types"
ccv "github.com/cosmos/interchain-security/v4/x/ccv/types"
)
// TestVSCPacketSendWithExpiredClient tests queueing of VSCPackets when the consumer client is expired.
// While the consumer client is expired (or inactive for some reason) all packets will be queued and
// and cleared once the consumer client is established.
func (s *CCVTestSuite) TestVSCPacketSendExpiredClient() {
providerKeeper := s.providerApp.GetProviderKeeper()
s.SetupCCVChannel(s.path)
expireClient(s, Consumer)
// bond some tokens on provider to change validator powers
bondAmt := sdk.NewInt(1000000)
delAddr := s.providerChain.SenderAccount.GetAddress()
delegate(s, delAddr, bondAmt)
// try to send CCV packet to consumer
s.nextEpoch()
// check that the packet was added to the list of pending VSC packets
packets := providerKeeper.GetPendingVSCPackets(s.providerCtx(), s.consumerChain.ChainID)
s.Require().NotEmpty(packets, "no pending VSC packets found")
s.Require().Equal(1, len(packets), "unexpected number of pending VSC packets")
// try again to send CCV packet to consumer
s.nextEpoch()
// check that the packet is still in the list of pending VSC packets
packets = providerKeeper.GetPendingVSCPackets(s.providerCtx(), s.consumerChain.ChainID)
s.Require().NotEmpty(packets, "no pending VSC packets found")
s.Require().Equal(1, len(packets), "unexpected number of pending VSC packets")
// bond more tokens on provider to change validator powers
delegate(s, delAddr, bondAmt)
// try again to send CCV packets to consumer
s.nextEpoch()
// check that the packets are still in the list of pending VSC packets
packets = providerKeeper.GetPendingVSCPackets(s.providerCtx(), s.consumerChain.ChainID)
s.Require().NotEmpty(packets, "no pending VSC packets found")
s.Require().Equal(2, len(packets), "unexpected number of pending VSC packets")
// upgrade expired client to the consumer
upgradeExpiredClient(s, Consumer)
// go to next epoch
s.nextEpoch()
// check that the packets are not in the list of pending VSC packets
packets = providerKeeper.GetPendingVSCPackets(s.providerCtx(), s.consumerChain.ChainID)
s.Require().Empty(packets, "unexpected pending VSC packets found")
// check that validator updates work
// - bond more tokens on provider to change validator powers
delegate(s, delAddr, bondAmt)
// - send CCV packet to consumer
s.nextEpoch()
// - relay all VSC packet from provider to consumer
relayAllCommittedPackets(s, s.providerChain, s.path, ccv.ProviderPortID, s.path.EndpointB.ChannelID, 3)
// - increment time so that the unbonding period ends on the consumer
incrementTimeByUnbondingPeriod(s, Consumer)
// - relay all VSCMatured packet from consumer to provider
relayAllCommittedPackets(s, s.consumerChain, s.path, ccv.ConsumerPortID, s.path.EndpointA.ChannelID, 3)
}
// TestConsumerPacketSendExpiredClient tests the consumer sending packets when the provider client is expired.
// While the provider client is expired all packets will be queued and and cleared once the provider client is upgraded.
func (s *CCVTestSuite) TestConsumerPacketSendExpiredClient() {
providerKeeper := s.providerApp.GetProviderKeeper()
consumerKeeper := s.consumerApp.GetConsumerKeeper()
s.SetupCCVChannel(s.path)
// 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.nextEpoch()
// bond more tokens on provider to change validator powers
delegate(s, delAddr, bondAmt)
// send CCV packets to consumer
s.nextEpoch()
// check that the packets are not in the list of pending VSC packets
providerPackets := providerKeeper.GetPendingVSCPackets(s.providerCtx(), s.consumerChain.ChainID)
s.Require().Empty(providerPackets, "pending VSC packets found")
// relay all VSC packet from provider to consumer
relayAllCommittedPackets(s, s.providerChain, s.path, ccv.ProviderPortID, s.path.EndpointB.ChannelID, 2)
// expire client to provider
expireClient(s, Provider)
// check that the client to the consumer is active
checkClientExpired(s, Consumer, false)
// increment time so that the unbonding period ends on the consumer;
// do not try to update the client to the provider since it's expired
consumerUnbondingPeriod := s.consumerApp.GetConsumerKeeper().GetUnbondingPeriod(s.consumerCtx())
incrementTimeWithoutUpdate(s, consumerUnbondingPeriod+time.Hour, Provider)
// check that the packets were added to the list of pending data packets
consumerPackets := consumerKeeper.GetPendingPackets(s.consumerCtx())
s.Require().NotEmpty(consumerPackets)
s.Require().Len(consumerPackets, 2, "unexpected number of pending data packets")
// try to send slash packet for downtime infraction
addr := ed25519.GenPrivKey().PubKey().Address()
val := abci.Validator{Address: addr, Power: 1}
consumerKeeper.QueueSlashPacket(s.consumerCtx(), val, 2, stakingtypes.Infraction_INFRACTION_DOWNTIME)
// try to send slash packet for the same downtime infraction
consumerKeeper.QueueSlashPacket(s.consumerCtx(), val, 3, stakingtypes.Infraction_INFRACTION_DOWNTIME)
// try to send slash packet for the double-sign infraction
consumerKeeper.QueueSlashPacket(s.consumerCtx(), val, 3, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN)
// check that the packets were added to the list of pending data packets
consumerPackets = consumerKeeper.GetPendingPackets(s.consumerCtx())
s.Require().NotEmpty(consumerPackets)
// At this point we expect 4 packets, two vsc matured packets and two trailing slash packets
s.Require().Len(consumerPackets, 4, "unexpected number of pending data packets")
// upgrade expired client to the consumer
upgradeExpiredClient(s, Provider)
// go to next block to trigger SendPendingPackets
s.consumerChain.NextBlock()
// Check that the leading vsc matured packets were sent and no longer pending
consumerPackets = consumerKeeper.GetPendingPackets(s.consumerCtx())
s.Require().Len(consumerPackets, 2, "unexpected number of pending data packets")
// relay committed packets from consumer to provider, first slash packet should be committed
relayAllCommittedPackets(s, s.consumerChain, s.path, ccv.ConsumerPortID, s.path.EndpointA.ChannelID, 3) // two vsc matured + one slash
// First slash has been acked, now only the second slash packet should remain as pending
consumerPackets = consumerKeeper.GetPendingPackets(s.consumerCtx())
s.Require().Len(consumerPackets, 1, "unexpected number of pending data packets")
// go to next block to trigger SendPendingPackets
s.consumerChain.NextBlock()
// relay committed packets from consumer to provider, only second slash packet should be committed
relayAllCommittedPackets(s, s.consumerChain, s.path, ccv.ConsumerPortID, s.path.EndpointA.ChannelID, 1) // one slash
consumerPackets = consumerKeeper.GetPendingPackets(s.consumerCtx())
s.Require().Empty(consumerPackets, "pending data packets found")
// check that everything works
// - bond more tokens on provider to change validator powers
delegate(s, delAddr, bondAmt)
// - send CCV packet to consumer
s.nextEpoch()
// - relay 1 VSC packet from provider to consumer
relayAllCommittedPackets(s, s.providerChain, s.path, ccv.ProviderPortID, s.path.EndpointB.ChannelID, 1)
// - increment time so that the unbonding period ends on the provider
incrementTimeByUnbondingPeriod(s, Consumer)
// - relay 1 VSCMatured packet from consumer to provider
relayAllCommittedPackets(s, s.consumerChain, s.path, ccv.ConsumerPortID, s.path.EndpointA.ChannelID, 1)
}
// expireClient expires the client to the `clientTo` chain
func expireClient(s *CCVTestSuite, clientTo ChainType) {
var hostEndpoint *ibctesting.Endpoint
var hostChain *ibctesting.TestChain
if clientTo == Consumer {
hostEndpoint = s.path.EndpointB
hostChain = s.providerChain
} else {
hostEndpoint = s.path.EndpointA
hostChain = s.consumerChain
}
cs, ok := hostChain.App.GetIBCKeeper().ClientKeeper.GetClientState(hostChain.GetContext(), hostEndpoint.ClientID)
s.Require().True(ok)
trustingPeriod := cs.(*ibctm.ClientState).TrustingPeriod
// increment time without updating the `clientTo` client
incrementTimeWithoutUpdate(s, trustingPeriod+time.Hour, clientTo)
// check that the client is not active
checkClientExpired(s, clientTo, true)
}
// checkClientIsExpired checks whether the client to `clientTo` is expired
func checkClientExpired(s *CCVTestSuite, clientTo ChainType, expectedExpired bool) {
var hostEndpoint *ibctesting.Endpoint
var hostChain *ibctesting.TestChain
if clientTo == Consumer {
hostEndpoint = s.path.EndpointB
hostChain = s.providerChain
} else {
hostEndpoint = s.path.EndpointA
hostChain = s.consumerChain
}
// check that the client to the consumer is not active
cs, ok := hostChain.App.GetIBCKeeper().ClientKeeper.GetClientState(hostChain.GetContext(), hostEndpoint.ClientID)
s.Require().True(ok)
clientStore := hostChain.App.GetIBCKeeper().ClientKeeper.ClientStore(hostChain.GetContext(), hostEndpoint.ClientID)
status := cs.Status(hostChain.GetContext(), clientStore, hostChain.App.AppCodec())
if expectedExpired {
s.Require().NotEqual(ibcexported.Active, status, "client is active")
} else {
s.Require().Equal(ibcexported.Active, status, "client is not active")
}
}
// upgradeExpiredClient upgrades an expired client to `clientTo`
func upgradeExpiredClient(s *CCVTestSuite, clientTo ChainType) {
subjectPath := s.path
substitutePath := ibctesting.NewPath(s.consumerChain, s.providerChain)
var subject, subjectCounterparty string
var hostNewEndpoint, targetNewEndpoint *ibctesting.Endpoint
var hostChain *ibctesting.TestChain
var targetChain *ibctesting.TestChain
if clientTo == Consumer {
subject = subjectPath.EndpointB.ClientID // provider endpoint client
subjectCounterparty = subjectPath.EndpointA.ClientID // consumer endpoint client
hostNewEndpoint = substitutePath.EndpointB
targetNewEndpoint = substitutePath.EndpointA
hostChain = s.providerChain
targetChain = s.consumerChain
} else {
subject = subjectPath.EndpointA.ClientID // consumer endpoint client
subjectCounterparty = subjectPath.EndpointB.ClientID // provider endpoint client
hostNewEndpoint = substitutePath.EndpointA
targetNewEndpoint = substitutePath.EndpointB
hostChain = s.consumerChain
targetChain = s.providerChain
}
subjectClientState := hostChain.GetClientState(subject)
// create substitute client with same unbonding period
hostTmConfig, ok := hostNewEndpoint.ClientConfig.(*ibctesting.TendermintConfig)
s.Require().True(ok)
hostTmConfig.UnbondingPeriod = subjectClientState.(*ibctm.ClientState).UnbondingPeriod
hostTmConfig.TrustingPeriod = subjectClientState.(*ibctm.ClientState).TrustingPeriod
targetTmConfig, ok := targetNewEndpoint.ClientConfig.(*ibctesting.TendermintConfig)
s.Require().True(ok)
subjectCounterpartyCS := targetChain.GetClientState(subjectCounterparty)
targetTmConfig.UnbondingPeriod = subjectCounterpartyCS.(*ibctm.ClientState).UnbondingPeriod
targetTmConfig.TrustingPeriod = subjectCounterpartyCS.(*ibctm.ClientState).TrustingPeriod
s.coordinator.SetupClients(substitutePath)
substitute := hostNewEndpoint.ClientID
// update substitute twice
err := hostNewEndpoint.UpdateClient()
s.Require().NoError(err)
err = hostNewEndpoint.UpdateClient()
s.Require().NoError(err)
substituteClientState := hostChain.GetClientState(substitute)
tmClientState, ok := subjectClientState.(*ibctm.ClientState)
s.Require().True(ok)
tmClientState.AllowUpdateAfterMisbehaviour = true
tmClientState.AllowUpdateAfterExpiry = true
tmClientState.FrozenHeight = tmClientState.LatestHeight
hostChain.App.GetIBCKeeper().ClientKeeper.SetClientState(hostChain.GetContext(), subject, tmClientState)
tmClientState, ok = substituteClientState.(*ibctm.ClientState)
s.Require().True(ok)
tmClientState.AllowUpdateAfterMisbehaviour = true
tmClientState.AllowUpdateAfterExpiry = true
hostChain.App.GetIBCKeeper().ClientKeeper.SetClientState(hostChain.GetContext(), substitute, tmClientState)
content := clienttypes.NewClientUpdateProposal(ibctesting.Title, ibctesting.Description, subject, substitute)
updateProp, ok := content.(*clienttypes.ClientUpdateProposal)
s.Require().True(ok)
err = hostChain.App.GetIBCKeeper().ClientKeeper.ClientUpdateProposal(hostChain.GetContext(), updateProp)
s.Require().NoError(err)
}