-
Notifications
You must be signed in to change notification settings - Fork 138
/
ibc_module_test.go
404 lines (366 loc) · 13.8 KB
/
ibc_module_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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package consumer_test
import (
"testing"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
conntypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
sdk "github.com/cosmos/cosmos-sdk/types"
testkeeper "github.com/cosmos/interchain-security/v6/testutil/keeper"
"github.com/cosmos/interchain-security/v6/x/ccv/consumer"
consumerkeeper "github.com/cosmos/interchain-security/v6/x/ccv/consumer/keeper"
ccv "github.com/cosmos/interchain-security/v6/x/ccv/types"
)
// TestOnChanOpenInit validates the consumer's OnChanOpenInit implementation against the spec.
// Additional validation for VerifyProviderChain can be found in it's unit test.
//
// See: https://github.com/cosmos/ibc/blob/main/spec/app/ics-028-cross-chain-validation/methods.md#ccv-ccf-coinit1
// Spec tag: [CCV-CCF-COINIT.1]
func TestOnChanOpenInit(t *testing.T) {
// Params for the OnChanOpenInit method
type params struct {
ctx sdk.Context
order channeltypes.Order
connectionHops []string
portID string
channelID string
chanCap *capabilitytypes.Capability
counterparty channeltypes.Counterparty
version string
}
testCases := []struct {
name string
// Test-case specific function that mutates method parameters and setups expected mock calls
setup func(*consumerkeeper.Keeper, *params, testkeeper.MockedKeepers)
expPass bool
}{
{
"success", func(keeper *consumerkeeper.Keeper, params *params, mocks testkeeper.MockedKeepers) {
gomock.InOrder(
mocks.MockScopedKeeper.EXPECT().ClaimCapability(
params.ctx, params.chanCap, host.ChannelCapabilityPath(
params.portID, params.channelID)).Return(nil).Times(1),
mocks.MockConnectionKeeper.EXPECT().GetConnection(
params.ctx, "connectionIDToProvider").Return(
conntypes.ConnectionEnd{ClientId: "clientIDToProvider"}, true).Times(1),
)
}, true,
},
{
"should succeed when IBC module version isn't provided", func(keeper *consumerkeeper.Keeper, params *params, mocks testkeeper.MockedKeepers) {
params.version = ""
gomock.InOrder(
mocks.MockScopedKeeper.EXPECT().ClaimCapability(
params.ctx, params.chanCap, host.ChannelCapabilityPath(
params.portID, params.channelID)).Return(nil).Times(1),
mocks.MockConnectionKeeper.EXPECT().GetConnection(
params.ctx, "connectionIDToProvider").Return(
conntypes.ConnectionEnd{ClientId: "clientIDToProvider"}, true).Times(1),
)
}, true,
},
{
"invalid non-empty IBC module version",
func(keeper *consumerkeeper.Keeper, params *params, mocks testkeeper.MockedKeepers) {
params.version = "2"
}, false,
},
{
"invalid: channel to provider already established",
func(keeper *consumerkeeper.Keeper, params *params, mocks testkeeper.MockedKeepers) {
keeper.SetProviderChannel(params.ctx, "existingProviderChanID")
}, false,
},
{
"invalid: UNORDERED channel",
func(keeper *consumerkeeper.Keeper, params *params, mocks testkeeper.MockedKeepers) {
params.order = channeltypes.UNORDERED
}, false,
},
{
"invalid port ID, not CCV port",
func(keeper *consumerkeeper.Keeper, params *params, mocks testkeeper.MockedKeepers) {
params.portID = "someDingusPortID"
}, false,
},
{
"invalid version",
func(keeper *consumerkeeper.Keeper, params *params, mocks testkeeper.MockedKeepers) {
params.version = "someDingusVer"
}, false,
},
{
"invalid counterparty port ID",
func(keeper *consumerkeeper.Keeper, params *params, mocks testkeeper.MockedKeepers) {
params.counterparty.PortId = "someOtherDingusPortID"
}, false,
},
{
"invalid clientID to provider",
func(keeper *consumerkeeper.Keeper, params *params, mocks testkeeper.MockedKeepers) {
gomock.InOrder(
mocks.MockScopedKeeper.EXPECT().ClaimCapability(
params.ctx, params.chanCap, host.ChannelCapabilityPath(
params.portID, params.channelID)).Return(nil).Times(1),
mocks.MockConnectionKeeper.EXPECT().GetConnection(
params.ctx, "connectionIDToProvider").Return(
conntypes.ConnectionEnd{ClientId: "unexpectedClientID"}, true).Times(1), // unexpected clientID
)
}, false,
},
}
for _, tc := range testCases {
// Common setup
keeperParams := testkeeper.NewInMemKeeperParams(t)
consumerKeeper, ctx, ctrl, mocks := testkeeper.GetConsumerKeeperAndCtx(
t, keeperParams)
consumerModule := consumer.NewAppModule(consumerKeeper, *keeperParams.ParamsSubspace)
consumerKeeper.SetPort(ctx, ccv.ConsumerPortID)
consumerKeeper.SetProviderClientID(ctx, "clientIDToProvider")
// Instantiate valid params as default. Individual test cases mutate these as needed.
params := params{
ctx: ctx,
order: channeltypes.ORDERED,
connectionHops: []string{"connectionIDToProvider"},
portID: ccv.ConsumerPortID,
channelID: "consumerChannelID",
chanCap: &capabilitytypes.Capability{},
counterparty: channeltypes.NewCounterparty(ccv.ProviderPortID, "providerChannelID"),
version: ccv.Version,
}
tc.setup(&consumerKeeper, ¶ms, mocks)
version, err := consumerModule.OnChanOpenInit(
params.ctx,
params.order,
params.connectionHops,
params.portID,
params.channelID,
params.chanCap,
params.counterparty,
params.version,
)
if tc.expPass {
// assert correct version
require.Equal(t, ccv.Version, version)
require.NoError(t, err)
} else {
require.Error(t, err)
// assert version string is empty
require.Empty(t, version)
}
// Confirm there are no unexpected external keeper calls
ctrl.Finish()
}
}
// TestOnChanOpenTry validates the consumer's OnChanOpenTry implementation against the spec.
//
// See: https://github.com/cosmos/ibc/blob/main/spec/app/ics-028-cross-chain-validation/methods.md#ccv-ccf-cotry1
// Spec tag: [CCV-CCF-COTRY.1]
func TestOnChanOpenTry(t *testing.T) {
keeperParams := testkeeper.NewInMemKeeperParams(t)
consumerKeeper, ctx, ctrl, _ := testkeeper.GetConsumerKeeperAndCtx(t, keeperParams)
// No external keeper methods should be called
defer ctrl.Finish()
consumerModule := consumer.NewAppModule(consumerKeeper, *keeperParams.ParamsSubspace)
// OnOpenTry must error even with correct arguments
_, err := consumerModule.OnChanOpenTry(
ctx,
channeltypes.ORDERED,
[]string{"connection-1"},
ccv.ConsumerPortID,
"channel-1",
nil,
channeltypes.NewCounterparty(ccv.ProviderPortID, "channel-1"),
ccv.Version,
)
require.Error(t, err, "OnChanOpenTry callback must error on consumer chain")
}
// TestOnChanOpenAck validates the consumer's OnChanOpenAck implementation against the spec.
//
// See: https://github.com/cosmos/ibc/blob/main/spec/app/ics-028-cross-chain-validation/methods.md#ccv-ccf-coack1
// Spec tag: [CCV-CCF-COACK.1]
func TestOnChanOpenAck(t *testing.T) {
// Params for the OnChanOpenAck method
type params struct {
ctx sdk.Context
portID string
channelID string
counterpartyChannelID string
counterpartyMetadata string
}
testCases := []struct {
name string
// Test-case specific function that mutates method parameters and setups expected mock calls
setup func(*consumerkeeper.Keeper, *params, testkeeper.MockedKeepers)
expPass bool
}{
{
"success - empty transferChannelID",
func(keeper *consumerkeeper.Keeper, params *params, mocks testkeeper.MockedKeepers) {
// Expected msg
distrTransferMsg := channeltypes.NewMsgChannelOpenInit(
transfertypes.PortID,
transfertypes.Version,
channeltypes.UNORDERED,
[]string{"connectionID"},
transfertypes.PortID,
"", // signer unused
)
transferChannelID := ""
keeper.SetDistributionTransmissionChannel(params.ctx, transferChannelID)
// Expected mock calls
gomock.InOrder(
mocks.MockChannelKeeper.EXPECT().GetChannel(
params.ctx, transfertypes.PortID, transferChannelID).Return(channeltypes.Channel{}, false).Times(1),
mocks.MockChannelKeeper.EXPECT().GetChannel(
params.ctx, params.portID, params.channelID).Return(channeltypes.Channel{
ConnectionHops: []string{"connectionID"},
}, true).Times(1),
mocks.MockIBCCoreKeeper.EXPECT().ChannelOpenInit(
params.ctx, distrTransferMsg).Return(
&channeltypes.MsgChannelOpenInitResponse{}, nil,
).Times(1),
)
},
true,
},
{
"invalid: provider channel already established",
func(keeper *consumerkeeper.Keeper, params *params, mocks testkeeper.MockedKeepers) {
keeper.SetProviderChannel(params.ctx, "existingProviderChannelID")
}, false,
},
{
"invalid: cannot unmarshal ack metadata ",
func(keeper *consumerkeeper.Keeper, params *params, mocks testkeeper.MockedKeepers) {
params.counterpartyMetadata = "bunkData"
}, false,
},
{
"invalid: mismatched serialized version",
func(keeper *consumerkeeper.Keeper, params *params, mocks testkeeper.MockedKeepers) {
md := ccv.HandshakeMetadata{
ProviderFeePoolAddr: "", // dummy address used
Version: "bunkVersion",
}
metadataBz, err := md.Marshal()
require.NoError(t, err)
params.counterpartyMetadata = string(metadataBz)
}, false,
},
}
for _, tc := range testCases {
// Common setup
keeperParams := testkeeper.NewInMemKeeperParams(t)
consumerKeeper, ctx, ctrl, mocks := testkeeper.GetConsumerKeeperAndCtx(
t, keeperParams)
consumerModule := consumer.NewAppModule(consumerKeeper, *keeperParams.ParamsSubspace)
// Instantiate valid params as default. Individual test cases mutate these as needed.
params := params{
ctx: ctx,
portID: ccv.ConsumerPortID,
channelID: "consumerCCVChannelID",
counterpartyChannelID: "providerCCVChannelID",
}
metadata := ccv.HandshakeMetadata{
ProviderFeePoolAddr: "someAcct",
Version: ccv.Version,
}
metadataBz, err := metadata.Marshal()
require.NoError(t, err)
params.counterpartyMetadata = string(metadataBz)
tc.setup(&consumerKeeper, ¶ms, mocks)
err = consumerModule.OnChanOpenAck(
params.ctx,
params.portID,
params.channelID,
params.counterpartyChannelID,
params.counterpartyMetadata,
)
if tc.expPass {
require.NoError(t, err)
// Confirm address of the distribution module account (on provider) was persisted on consumer
distModuleAcct := consumerKeeper.GetProviderFeePoolAddrStr(ctx)
require.Equal(t, "someAcct", distModuleAcct)
} else {
require.Error(t, err)
}
// Confirm there are no unexpected external keeper calls
ctrl.Finish()
}
}
// TestOnChanOpenConfirm validates the consumer's OnChanOpenConfirm implementation against the spec.
//
// See: https://github.com/cosmos/ibc/blob/main/spec/app/ics-028-cross-chain-validation/methods.md#ccv-ccf-coconfirm1
// Spec tag: [CCV-CCF-COCONFIRM.1]
func TestOnChanOpenConfirm(t *testing.T) {
keeperParams := testkeeper.NewInMemKeeperParams(t)
consumerKeeper, ctx, ctrl, _ := testkeeper.GetConsumerKeeperAndCtx(t, keeperParams)
defer ctrl.Finish()
consumerModule := consumer.NewAppModule(consumerKeeper, *keeperParams.ParamsSubspace)
err := consumerModule.OnChanOpenConfirm(ctx, ccv.ConsumerPortID, "channel-1")
require.Error(t, err, "OnChanOpenConfirm callback must error on consumer chain")
}
// TestOnChanCloseInit validates the consumer's OnChanCloseInit implementation against the spec.
//
// See: https://github.com/cosmos/ibc/blob/main/spec/app/ics-028-cross-chain-validation/methods.md#ccv-ccf-ccinit1
// Spec tag: [CCV-CCF-CCINIT.1]
func TestOnChanCloseInit(t *testing.T) {
testCases := []struct {
name string
channelToClose string
establishedProviderExists bool
expPass bool
}{
{
name: "No established provider channel, error returned disallowing closing of channel",
channelToClose: "someChannelID",
establishedProviderExists: false,
expPass: false,
},
{
name: "Provider channel is established, User CANNOT close established provider channel",
channelToClose: "provider",
establishedProviderExists: true,
expPass: false,
},
{
name: "User CAN close duplicate channel that is NOT established provider",
channelToClose: "someChannelID",
establishedProviderExists: true,
expPass: true,
},
}
for _, tc := range testCases {
keeperParams := testkeeper.NewInMemKeeperParams(t)
consumerKeeper, ctx, ctrl, _ := testkeeper.GetConsumerKeeperAndCtx(t, keeperParams)
consumerModule := consumer.NewAppModule(consumerKeeper, *keeperParams.ParamsSubspace)
if tc.establishedProviderExists {
consumerKeeper.SetProviderChannel(ctx, "provider")
}
err := consumerModule.OnChanCloseInit(ctx, "portID", tc.channelToClose)
if tc.expPass {
require.NoError(t, err)
} else {
require.Error(t, err)
}
ctrl.Finish()
}
}
// TestOnChanCloseConfirm validates the consumer's OnChanCloseConfirm implementation against the spec.
//
// See: https://github.com/cosmos/ibc/blob/main/spec/app/ics-028-cross-chain-validation/methods.md#ccv-pcf-ccconfirm1// Spec tag: [CCV-CCF-CCINIT.1]
// Spec tag: [CCV-PCF-CCCONFIRM.1]
func TestOnChanCloseConfirm(t *testing.T) {
keeperParams := testkeeper.NewInMemKeeperParams(t)
consumerKeeper, ctx, ctrl, _ := testkeeper.GetConsumerKeeperAndCtx(t, keeperParams)
// No external keeper methods should be called
defer ctrl.Finish()
consumerModule := consumer.NewAppModule(consumerKeeper, *keeperParams.ParamsSubspace)
// Nothing happens, no error returned
err := consumerModule.OnChanCloseConfirm(ctx, "portID", "channelID")
require.NoError(t, err)
}