Skip to content

Commit

Permalink
finish test
Browse files Browse the repository at this point in the history
  • Loading branch information
shaspitz committed Sep 19, 2023
1 parent 56ff6f2 commit c7ebeb3
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
21 changes: 21 additions & 0 deletions x/ccv/provider/keeper/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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{},
Expand Down
103 changes: 102 additions & 1 deletion x/ccv/provider/keeper/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit c7ebeb3

Please sign in to comment.