Skip to content

Commit

Permalink
Add test for OnTimeoutPacket.
Browse files Browse the repository at this point in the history
  • Loading branch information
insumity committed Jul 31, 2023
1 parent 7484dbc commit f8f281c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
9 changes: 7 additions & 2 deletions testutil/keeper/unit_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,18 @@ func GetNewVSCMaturedPacketData() types.VSCMaturedPacketData {
// SetupForStoppingConsumerChain registers expected mock calls and corresponding state setup
// which asserts that a consumer chain was properly stopped from StopConsumerChain().
func SetupForStoppingConsumerChain(t *testing.T, ctx sdk.Context,
providerKeeper *providerkeeper.Keeper, mocks MockedKeepers,
providerKeeper *providerkeeper.Keeper, mocks MockedKeepers, closeChan bool,
) {
t.Helper()
expectations := GetMocksForCreateConsumerClient(ctx, &mocks,
"chainID", clienttypes.NewHeight(4, 5))
expectations = append(expectations, GetMocksForSetConsumerChain(ctx, &mocks, "chainID")...)
expectations = append(expectations, GetMocksForStopConsumerChain(ctx, &mocks)...)

// StopConsumerChain receives a `closeChan` parameter. If the `closeChan` argument is true, then
// we register additional expected mock calls that capture the fact that the underlying channel is closed.
if closeChan {
expectations = append(expectations, GetMocksForStopConsumerChain(ctx, &mocks)...)
}

gomock.InOrder(expectations...)

Expand Down
51 changes: 51 additions & 0 deletions x/ccv/provider/ibc_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,54 @@ func TestUnmarshalConsumerPacket(t *testing.T) {
require.Equal(t, tc.expectedPacketData, actualConsumerPacketData)
}
}

// TestOnTimeoutPacket tests the provider's OnTimeoutPacket method against the spec.
//
// See: https://github.com/cosmos/ibc/blob/main/spec/app/ics-028-cross-chain-validation/methods.md#ccv-pcf-tovsc1
// Spec tag: [CCV-PCF-TOVSC.1]
func TestOnTimeoutPacket(t *testing.T) {
testCases := []struct {
name string
setup func(*providerkeeper.Keeper, sdk.Context, testkeeper.MockedKeepers)
packet channeltypes.Packet
expPass bool
}{
{
name: "invalid channel state",
setup: func(*providerkeeper.Keeper, sdk.Context, testkeeper.MockedKeepers) {
// Nothing to set up because `OnTimeoutPacket` would not attempt to stop
// the underlying consumer chain due to the invalid channel in the packet.
},
packet: channeltypes.Packet{SourceChannel: "invalid channel"},
expPass: false,
},
{
name: "stop consumer chain",
setup: func(providerKeeper *providerkeeper.Keeper, ctx sdk.Context, mocks testkeeper.MockedKeepers) {
testkeeper.SetupForStoppingConsumerChain(t, ctx, providerKeeper, mocks, false)
},
packet: channeltypes.Packet{SourceChannel: "channelID"},
expPass: true,
},
}

for _, tc := range testCases {
keeperParams := testkeeper.NewInMemKeeperParams(t)
providerKeeper, ctx, ctrl, mocks := testkeeper.GetProviderKeeperAndCtx(
t, keeperParams)
providerKeeper.SetParams(ctx, providertypes.DefaultParams())

tc.setup(&providerKeeper, ctx, mocks)

providerModule := provider.NewAppModule(&providerKeeper, *keeperParams.ParamsSubspace)

err := providerModule.OnTimeoutPacket(ctx, tc.packet, nil)
if tc.expPass {
require.NoError(t, err)
} else {
require.Error(t, err)
}

ctrl.Finish()
}
}
6 changes: 3 additions & 3 deletions x/ccv/provider/keeper/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func TestHandleConsumerRemovalProposal(t *testing.T) {
// Note: when expAppendProp is false, no mocks are setup,
// meaning no external keeper methods are allowed to be called.
if tc.expAppendProp {
testkeeper.SetupForStoppingConsumerChain(t, ctx, &providerKeeper, mocks)
testkeeper.SetupForStoppingConsumerChain(t, ctx, &providerKeeper, mocks, true)
}

tc.setupMocks(ctx, providerKeeper, tc.prop.ChainId)
Expand Down Expand Up @@ -526,7 +526,7 @@ func TestStopConsumerChain(t *testing.T) {
{
description: "valid stop of consumer chain, throttle related queues are cleaned",
setup: func(ctx sdk.Context, providerKeeper *providerkeeper.Keeper, mocks testkeeper.MockedKeepers) {
testkeeper.SetupForStoppingConsumerChain(t, ctx, providerKeeper, mocks)
testkeeper.SetupForStoppingConsumerChain(t, ctx, providerKeeper, mocks, true)

providerKeeper.QueueGlobalSlashEntry(ctx, providertypes.NewGlobalSlashEntry(
ctx.BlockTime(), "chainID", 1, cryptoutil.NewCryptoIdentityFromIntSeed(90).ProviderConsAddress()))
Expand All @@ -545,7 +545,7 @@ func TestStopConsumerChain(t *testing.T) {
{
description: "valid stop of consumer chain, all mock calls hit",
setup: func(ctx sdk.Context, providerKeeper *providerkeeper.Keeper, mocks testkeeper.MockedKeepers) {
testkeeper.SetupForStoppingConsumerChain(t, ctx, providerKeeper, mocks)
testkeeper.SetupForStoppingConsumerChain(t, ctx, providerKeeper, mocks, true)
},
expErr: false,
},
Expand Down
2 changes: 1 addition & 1 deletion x/ccv/provider/proposal_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestProviderProposalHandler(t *testing.T) {
)...)

case tc.expValidConsumerRemoval:
testkeeper.SetupForStoppingConsumerChain(t, ctx, &providerKeeper, mocks)
testkeeper.SetupForStoppingConsumerChain(t, ctx, &providerKeeper, mocks, true)

case tc.expValidEquivocation:
providerKeeper.SetSlashLog(ctx, providertypes.NewProviderConsAddress(equivocation.GetConsensusAddress()))
Expand Down

0 comments on commit f8f281c

Please sign in to comment.