From c7ebeb31a71f23d531b7d70908f781976c1b9cb4 Mon Sep 17 00:00:00 2001 From: Shawn <44221603+smarshall-spitzbart@users.noreply.github.com> Date: Tue, 19 Sep 2023 12:28:57 -0700 Subject: [PATCH] finish test --- x/ccv/provider/keeper/migration.go | 21 +++++ x/ccv/provider/keeper/migration_test.go | 103 +++++++++++++++++++++++- 2 files changed, 123 insertions(+), 1 deletion(-) diff --git a/x/ccv/provider/keeper/migration.go b/x/ccv/provider/keeper/migration.go index 24669bb3c7..815e496494 100644 --- a/x/ccv/provider/keeper/migration.go +++ b/x/ccv/provider/keeper/migration.go @@ -34,6 +34,7 @@ func (k Keeper) MigrateQueuedPackets(ctx sdktypes.Context) error { for _, data := range vscmData { k.HandleVSCMaturedPacket(ctx, consumer.ChainId, data) } + k.DeleteThrottledPacketDataForConsumer(ctx, consumer.ChainId) } return nil @@ -85,6 +86,26 @@ func (k Keeper) GetAllThrottledPacketData(ctx sdktypes.Context, consumerChainID return slashData, vscMaturedData } +// Note this method is copy/pasted from throttle v1 code. +func (k Keeper) DeleteThrottledPacketDataForConsumer(ctx sdktypes.Context, consumerChainID string) { + store := ctx.KVStore(k.storeKey) + iteratorPrefix := providertypes.ChainIdWithLenKey(providertypes.ThrottledPacketDataBytePrefix, consumerChainID) + iterator := sdktypes.KVStorePrefixIterator(store, iteratorPrefix) + defer iterator.Close() + + keysToDel := [][]byte{} + for ; iterator.Valid(); iterator.Next() { + keysToDel = append(keysToDel, iterator.Key()) + } + // Delete data for this consumer + for _, key := range keysToDel { + store.Delete(key) + } + + // Delete size of data queue for this consumer + store.Delete(providertypes.ThrottledPacketDataSizeKey(consumerChainID)) +} + // Note this method is adapted from throttle v1 code. func (k Keeper) QueueThrottledPacketDataOnlyForTesting( ctx sdktypes.Context, consumerChainID string, ibcSeqNum uint64, packetData interface{}, diff --git a/x/ccv/provider/keeper/migration_test.go b/x/ccv/provider/keeper/migration_test.go index 4a7c3d695d..764f2f1b73 100644 --- a/x/ccv/provider/keeper/migration_test.go +++ b/x/ccv/provider/keeper/migration_test.go @@ -2,14 +2,115 @@ package keeper_test import ( "testing" + "time" testutil "github.com/cosmos/interchain-security/v3/testutil/keeper" + "github.com/stretchr/testify/require" ) func TestMigrate2To3(t *testing.T) { consumerKeeper, ctx, ctrl, _ := testutil.GetProviderKeeperAndCtx(t, testutil.NewInMemKeeperParams(t)) defer ctrl.Finish() - // Set some data in old format + // Set consumer client ids to mock consumers being connected to provider + consumerKeeper.SetConsumerClientId(ctx, "chain-1", "client-1") + consumerKeeper.SetConsumerClientId(ctx, "chain-2", "client-2") + consumerKeeper.SetConsumerClientId(ctx, "chain-3", "client-3") + // Queue some data for chain-1 + consumerKeeper.QueueThrottledPacketDataOnlyForTesting( + ctx, "chain-1", 66, testutil.GetNewSlashPacketData()) + consumerKeeper.QueueThrottledPacketDataOnlyForTesting( + ctx, "chain-1", 67, testutil.GetNewVSCMaturedPacketData()) + consumerKeeper.QueueThrottledPacketDataOnlyForTesting( + ctx, "chain-1", 68, testutil.GetNewSlashPacketData()) + consumerKeeper.QueueThrottledPacketDataOnlyForTesting( + ctx, "chain-1", 69, testutil.GetNewVSCMaturedPacketData()) + + // Queue some data for chain-2 + consumerKeeper.QueueThrottledPacketDataOnlyForTesting( + ctx, "chain-2", 789, testutil.GetNewVSCMaturedPacketData()) + consumerKeeper.QueueThrottledPacketDataOnlyForTesting( + ctx, "chain-2", 790, testutil.GetNewSlashPacketData()) + consumerKeeper.QueueThrottledPacketDataOnlyForTesting( + ctx, "chain-2", 791, testutil.GetNewVSCMaturedPacketData()) + + // Queue some data for chain-3 + consumerKeeper.QueueThrottledPacketDataOnlyForTesting( + ctx, "chain-3", 123, testutil.GetNewSlashPacketData()) + consumerKeeper.QueueThrottledPacketDataOnlyForTesting( + ctx, "chain-3", 124, testutil.GetNewVSCMaturedPacketData()) + consumerKeeper.QueueThrottledPacketDataOnlyForTesting( + ctx, "chain-3", 125, testutil.GetNewVSCMaturedPacketData()) + + // Confirm getter methods return expected values + slash1, vscm1 := consumerKeeper.GetAllThrottledPacketData(ctx, "chain-1") + require.Len(t, slash1, 2) + require.Len(t, vscm1, 2) + + slash2, vscm2 := consumerKeeper.GetAllThrottledPacketData(ctx, "chain-2") + require.Len(t, slash2, 1) + require.Len(t, vscm2, 2) + + slash3, vscm3 := consumerKeeper.GetAllThrottledPacketData(ctx, "chain-3") + require.Len(t, slash3, 1) + require.Len(t, vscm3, 2) + + // Set vsc send timestamp for every queued vsc matured packet, + // as a way to assert that the vsc matured packets are handled in the migration. + // + // That is, timestamp should exist before a vsc matured packet is handled, + // and deleted after handling. + for _, data := range vscm1 { + consumerKeeper.SetVscSendTimestamp(ctx, "chain-1", data.ValsetUpdateId, time.Now()) + } + for _, data := range vscm2 { + consumerKeeper.SetVscSendTimestamp(ctx, "chain-2", data.ValsetUpdateId, time.Now()) + } + for _, data := range vscm3 { + consumerKeeper.SetVscSendTimestamp(ctx, "chain-3", data.ValsetUpdateId, time.Now()) + } + + // Confirm timestamps are set + for _, data := range vscm1 { + _, found := consumerKeeper.GetVscSendTimestamp(ctx, "chain-1", data.ValsetUpdateId) + require.True(t, found) + } + for _, data := range vscm2 { + _, found := consumerKeeper.GetVscSendTimestamp(ctx, "chain-2", data.ValsetUpdateId) + require.True(t, found) + } + for _, data := range vscm3 { + _, found := consumerKeeper.GetVscSendTimestamp(ctx, "chain-3", data.ValsetUpdateId) + require.True(t, found) + } + + // Run migration + err := consumerKeeper.MigrateQueuedPackets(ctx) + require.NoError(t, err) + + // Confirm throttled data is now deleted + slash1, vscm1 = consumerKeeper.GetAllThrottledPacketData(ctx, "chain-1") + require.Empty(t, slash1) + require.Empty(t, vscm1) + slash2, vscm2 = consumerKeeper.GetAllThrottledPacketData(ctx, "chain-2") + require.Empty(t, slash2) + require.Empty(t, vscm2) + slash3, vscm3 = consumerKeeper.GetAllThrottledPacketData(ctx, "chain-3") + require.Empty(t, slash3) + require.Empty(t, vscm3) + + // Confirm unbonding op indexes are deleted, meaning vsc matured packets were handled + for _, data := range vscm1 { + _, found := consumerKeeper.GetVscSendTimestamp(ctx, "chain-1", data.ValsetUpdateId) + require.False(t, found) + } + for _, data := range vscm2 { + _, found := consumerKeeper.GetVscSendTimestamp(ctx, "chain-2", data.ValsetUpdateId) + require.False(t, found) + } + for _, data := range vscm3 { + _, found := consumerKeeper.GetVscSendTimestamp(ctx, "chain-3", data.ValsetUpdateId) + require.False(t, found) + } }