Skip to content

Commit

Permalink
migrate sequence to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaSripal committed Dec 17, 2024
1 parent 7cd750a commit 6a206fc
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 8 deletions.
24 changes: 21 additions & 3 deletions modules/core/04-channel/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v9/modules/core/05-port/types"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
hostv2 "github.com/cosmos/ibc-go/v9/modules/core/24-host/v2"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
)

Expand Down Expand Up @@ -138,9 +139,10 @@ func (k *Keeper) SetNextChannelSequence(ctx context.Context, sequence uint64) {
}

// GetNextSequenceSend gets a channel's next send sequence from the store
// NOTE: portID is now ignored in key to use same key as v2. However this API remains unchanged
func (k *Keeper) GetNextSequenceSend(ctx context.Context, portID, channelID string) (uint64, bool) {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get(host.NextSequenceSendKey(portID, channelID))
bz, err := store.Get(hostv2.NextSequenceSendKey(channelID))
if err != nil {
panic(err)
}
Expand All @@ -152,10 +154,11 @@ func (k *Keeper) GetNextSequenceSend(ctx context.Context, portID, channelID stri
}

// SetNextSequenceSend sets a channel's next send sequence to the store
// NOTE: portID is now ignored in key to use same key as v2. However this API remains unchanged
func (k *Keeper) SetNextSequenceSend(ctx context.Context, portID, channelID string, sequence uint64) {
store := k.storeService.OpenKVStore(ctx)
bz := sdk.Uint64ToBigEndian(sequence)
if err := store.Set(host.NextSequenceSendKey(portID, channelID), bz); err != nil {
if err := store.Set(hostv2.NextSequenceSendKey(channelID), bz); err != nil {
panic(err)
}
}
Expand Down Expand Up @@ -336,7 +339,7 @@ func (k *Keeper) IteratePacketSequence(ctx context.Context, iterator db.Iterator
}

// GetAllPacketSendSeqs returns all stored next send sequences.
func (k *Keeper) GetAllPacketSendSeqs(ctx context.Context) (seqs []types.PacketSequence) {
func (k *Keeper) GetAllPacketSendSeqsV1(ctx context.Context) (seqs []types.PacketSequence) {
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
iterator := storetypes.KVStorePrefixIterator(store, []byte(host.KeyNextSeqSendPrefix))
k.IteratePacketSequence(ctx, iterator, func(portID, channelID string, nextSendSeq uint64) bool {
Expand All @@ -347,6 +350,21 @@ func (k *Keeper) GetAllPacketSendSeqs(ctx context.Context) (seqs []types.PacketS
return seqs
}

func (k *Keeper) GetAllPacketSendSeqs(ctx context.Context) (seqs []types.PacketSequence) {
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
iterator := storetypes.KVStorePrefixIterator(store, []byte(hostv2.NextSequenceSendKeyPrefix))
defer sdk.LogDeferred(k.Logger(ctx), func() error { return iterator.Close() })
for ; iterator.Valid(); iterator.Next() {
channelID := string(iterator.Key()[len(hostv2.NextSequenceSendKeyPrefix):])

sequence := sdk.BigEndianToUint64(iterator.Value())

ps := types.NewPacketSequence("", channelID, sequence)
seqs = append(seqs, ps)
}
return seqs
}

// GetAllPacketRecvSeqs returns all stored next recv sequences.
func (k *Keeper) GetAllPacketRecvSeqs(ctx context.Context) (seqs []types.PacketSequence) {
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
Expand Down
7 changes: 6 additions & 1 deletion modules/core/04-channel/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,14 @@ func (suite *KeeperTestSuite) TestGetAllSequences() {
suite.Len(recvSeqs, 2)
suite.Len(ackSeqs, 2)

suite.Equal(expSeqs, sendSeqs)
suite.Equal(expSeqs, recvSeqs)
suite.Equal(expSeqs, ackSeqs)

expSendSeqs := []types.PacketSequence{}
for _, es := range expSeqs {
expSendSeqs = append(expSendSeqs, types.NewPacketSequence("", es.ChannelId, es.Sequence))
}
suite.Equal(expSendSeqs, sendSeqs)
}

// TestGetAllPacketState creates a set of acks, packet commitments, and receipts on two different
Expand Down
15 changes: 15 additions & 0 deletions modules/core/04-channel/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package keeper

import (
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"

channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
)

// Migrator is a struct for handling in-place store migrations.
Expand All @@ -23,3 +27,14 @@ func (m Migrator) MigrateParams(ctx sdk.Context) error {
m.keeper.Logger(ctx).Info("successfully migrated ibc channel params")
return nil
}

// MigrateNextSequenceSend migrates the nextSequenceSend storage from the v1 to v2 format
func (m Migrator) MigrateNextSequenceSend(ctx sdk.Context) error {
store := runtime.KVStoreAdapter(m.keeper.storeService.OpenKVStore(ctx))
iterator := storetypes.KVStorePrefixIterator(store, []byte(host.KeyNextSeqSendPrefix))
m.keeper.IteratePacketSequence(ctx, iterator, func(portID, channelID string, nextSendSeq uint64) bool {
m.keeper.SetNextSequenceSend(ctx, portID, channelID, nextSendSeq)
return false
})
return nil
}
39 changes: 39 additions & 0 deletions modules/core/04-channel/keeper/migrations_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package keeper_test

import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/ibc-go/v9/modules/core/04-channel/keeper"
"github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"

Check failure on line 7 in modules/core/04-channel/keeper/migrations_test.go

View workflow job for this annotation

GitHub Actions / lint

ST1019: package "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types" is being imported more than once (stylecheck)

Check failure on line 7 in modules/core/04-channel/keeper/migrations_test.go

View workflow job for this annotation

GitHub Actions / lint

ST1019: package "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types" is being imported more than once (stylecheck)
channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
)

// TestMigrateDefaultParams tests the migration for the channel params
Expand Down Expand Up @@ -32,3 +37,37 @@ func (suite *KeeperTestSuite) TestMigrateDefaultParams() {
})
}
}

// TestMigrateNextSequenceSend will test the migration from the v1 NextSeqSend keys
// to the v2 format.
func (suite *KeeperTestSuite) TestMigrateNextSequenceSend() {
seq1 := types.NewPacketSequence("transfer", "channel-0", 1)
seq2 := types.NewPacketSequence("mock", "channel-2", 2)
seq3 := types.NewPacketSequence("ica", "channel-4", 3)

expSeqs := []types.PacketSequence{seq1, seq2, seq3}

store := suite.chainA.GetContext().KVStore(suite.chainA.GetSimApp().GetKey(exported.StoreKey))
for _, es := range expSeqs {
bz := sdk.Uint64ToBigEndian(es.Sequence)
store.Set(host.NextSequenceSendKey(es.PortId, es.ChannelId), bz)
}

k := suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper

ctx := suite.chainA.GetContext()
seqs := k.GetAllPacketSendSeqs(ctx)
suite.Require().Equal([]types.PacketSequence(nil), seqs, "sequences already exist in correct key format")

migrator := keeper.NewMigrator(k)

migrator.MigrateNextSequenceSend(ctx)

Check failure on line 64 in modules/core/04-channel/keeper/migrations_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `migrator.MigrateNextSequenceSend` is not checked (errcheck)

Check failure on line 64 in modules/core/04-channel/keeper/migrations_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `migrator.MigrateNextSequenceSend` is not checked (errcheck)

expV2Seqs := []types.PacketSequence{}
for _, es := range expSeqs {
expV2Seqs = append(expV2Seqs, types.NewPacketSequence("", es.ChannelId, es.Sequence))
}

seqs = k.GetAllPacketSendSeqs(ctx)
suite.Require().Equal(expV2Seqs, seqs, "new sequence keys not stored correctly")
}
3 changes: 2 additions & 1 deletion modules/core/04-channel/keeper/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
hostv2 "github.com/cosmos/ibc-go/v9/modules/core/24-host/v2"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
ibctesting "github.com/cosmos/ibc-go/v9/testing"
"github.com/cosmos/ibc-go/v9/testing/mock"
Expand Down Expand Up @@ -2249,7 +2250,7 @@ func (suite *KeeperTestSuite) TestStartFlush() {
func() {
// Delete next sequence send key from store
store := suite.chainB.GetContext().KVStore(suite.chainB.GetSimApp().GetKey(exported.StoreKey))
store.Delete(host.NextSequenceSendKey(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID))
store.Delete(hostv2.NextSequenceSendKey(path.EndpointB.ChannelID))
},
types.ErrSequenceSendNotFound,
},
Expand Down
8 changes: 5 additions & 3 deletions modules/core/24-host/v2/packet_keys.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package v2

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
NextSequenceSendKeyPrefix = "nextSequenceSendV2/"
)

// PacketCommitmentPrefixKey returns the store key prefix under which packet commitments for a particular channel are stored.
// channelID must be a generated identifier, not provided externally so key collisions are not possible.
func PacketCommitmentPrefixKey(channelID string) []byte {
Expand Down Expand Up @@ -44,5 +46,5 @@ func PacketAcknowledgementKey(channelID string, sequence uint64) []byte {

// NextSequenceSendKey returns the store key for the next sequence send of a given channelID.
func NextSequenceSendKey(channelID string) []byte {
return []byte(fmt.Sprintf("nextSequenceSend/%s", channelID))
return append([]byte(NextSequenceSendKeyPrefix), []byte(channelID)...)
}

0 comments on commit 6a206fc

Please sign in to comment.