Skip to content

Commit

Permalink
refactor: change byte prefixing scheme for consumer (#2148)
Browse files Browse the repository at this point in the history
* update consumer keys prefixing scheme

* make changes consistent

* mark PendingDataPackets key as v1

* apply review suggestions

* fix import
  • Loading branch information
mpoke committed Sep 17, 2024
1 parent e196a63 commit 66e209c
Show file tree
Hide file tree
Showing 6 changed files with 337 additions and 176 deletions.
32 changes: 14 additions & 18 deletions x/ccv/consumer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,13 @@ func (k Keeper) GetProviderClientID(ctx sdk.Context) (string, bool) {
// SetProviderChannel sets the channelID for the channel to the provider.
func (k Keeper) SetProviderChannel(ctx sdk.Context, channelID string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.ProviderChannelKey(), []byte(channelID))
store.Set(types.ProviderChannelIDKey(), []byte(channelID))
}

// GetProviderChannel gets the channelID for the channel to the provider.
func (k Keeper) GetProviderChannel(ctx sdk.Context) (string, bool) {
store := ctx.KVStore(k.storeKey)
channelIdBytes := store.Get(types.ProviderChannelKey())
channelIdBytes := store.Get(types.ProviderChannelIDKey())
if len(channelIdBytes) == 0 {
return "", false
}
Expand All @@ -260,7 +260,7 @@ func (k Keeper) GetProviderChannel(ctx sdk.Context) (string, bool) {
// DeleteProviderChannel deletes the channelID for the channel to the provider.
func (k Keeper) DeleteProviderChannel(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.ProviderChannelKey())
store.Delete(types.ProviderChannelIDKey())
}

// SetPendingChanges sets the pending validator set change packet that haven't been flushed to ABCI
Expand Down Expand Up @@ -360,7 +360,7 @@ func (k Keeper) GetLastStandaloneValidators(ctx sdk.Context) ([]stakingtypes.Val
// i.e., the slice contains the IDs of the matured VSCPackets.
func (k Keeper) GetElapsedPacketMaturityTimes(ctx sdk.Context) (maturingVSCPackets []types.MaturingVSCPacket) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PacketMaturityTimeBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, types.PacketMaturityTimeKeyPrefix())

defer iterator.Close()

Expand Down Expand Up @@ -392,7 +392,7 @@ func (k Keeper) GetElapsedPacketMaturityTimes(ctx sdk.Context) (maturingVSCPacke
// If two entries have the same maturityTime, then they are ordered by vscID.
func (k Keeper) GetAllPacketMaturityTimes(ctx sdk.Context) (maturingVSCPackets []types.MaturingVSCPacket) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PacketMaturityTimeBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, types.PacketMaturityTimeKeyPrefix())

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -493,7 +493,7 @@ func (k Keeper) DeleteHeightValsetUpdateID(ctx sdk.Context, height uint64) {
// Thus, the returned array is in ascending order of heights.
func (k Keeper) GetAllHeightToValsetUpdateIDs(ctx sdk.Context) (heightToValsetUpdateIDs []types.HeightToValsetUpdateID) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.HeightValsetUpdateIDBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, types.HeightValsetUpdateIDKeyPrefix())

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -535,7 +535,7 @@ func (k Keeper) DeleteOutstandingDowntime(ctx sdk.Context, address sdk.ConsAddre
// Thus, the returned array is in ascending order of consAddresses.
func (k Keeper) GetAllOutstandingDowntimes(ctx sdk.Context) (downtimes []types.OutstandingDowntime) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.OutstandingDowntimeBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, types.OutstandingDowntimeKeyPrefix())

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -584,7 +584,7 @@ func (k Keeper) DeleteCCValidator(ctx sdk.Context, addr []byte) {
// Thus, the returned array is in ascending order of addresses.
func (k Keeper) GetAllCCValidator(ctx sdk.Context) (validators []types.CrossChainValidator) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.CrossChainValidatorBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, types.CrossChainValidatorKeyPrefix())

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -612,7 +612,7 @@ func (k Keeper) getAndIncrementPendingPacketsIdx(ctx sdk.Context) (toReturn uint
// DeleteHeadOfPendingPackets deletes the head of the pending packets queue.
func (k Keeper) DeleteHeadOfPendingPackets(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PendingDataPacketsBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, types.PendingDataPacketsV1KeyPrefix())
defer iterator.Close()
if !iterator.Valid() {
return
Expand Down Expand Up @@ -644,9 +644,7 @@ type ConsumerPacketDataWithIdx struct {
func (k Keeper) GetAllPendingPacketsWithIdx(ctx sdk.Context) []ConsumerPacketDataWithIdx {
packets := []ConsumerPacketDataWithIdx{}
store := ctx.KVStore(k.storeKey)
// Note: PendingDataPacketsBytePrefix is the correct prefix, NOT PendingDataPacketsByteKey.
// See consistency with PendingDataPacketsKey().
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PendingDataPacketsBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, types.PendingDataPacketsV1KeyPrefix())
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var packet ccv.ConsumerPacketData
Expand All @@ -658,7 +656,7 @@ func (k Keeper) GetAllPendingPacketsWithIdx(ctx sdk.Context) []ConsumerPacketDat
}
packetWithIdx := ConsumerPacketDataWithIdx{
ConsumerPacketData: packet,
// index stored in key after prefix, see PendingDataPacketsKey()
// index stored in key after prefix, see PendingDataPacketsV1Key()
Idx: sdk.BigEndianToUint64(iterator.Key()[1:]),
}
packets = append(packets, packetWithIdx)
Expand All @@ -670,15 +668,13 @@ func (k Keeper) GetAllPendingPacketsWithIdx(ctx sdk.Context) []ConsumerPacketDat
func (k Keeper) DeletePendingDataPackets(ctx sdk.Context, idxs ...uint64) {
store := ctx.KVStore(k.storeKey)
for _, idx := range idxs {
store.Delete(types.PendingDataPacketsKey(idx))
store.Delete(types.PendingDataPacketsV1Key(idx))
}
}

func (k Keeper) DeleteAllPendingDataPackets(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
// Note: PendingDataPacketsBytePrefix is the correct prefix, NOT PendingDataPacketsByteKey.
// See consistency with PendingDataPacketsKey().
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PendingDataPacketsBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, types.PendingDataPacketsV1KeyPrefix())
keysToDel := [][]byte{}
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand All @@ -692,7 +688,7 @@ func (k Keeper) DeleteAllPendingDataPackets(ctx sdk.Context) {
// AppendPendingPacket enqueues the given data packet to the end of the pending data packets queue
func (k Keeper) AppendPendingPacket(ctx sdk.Context, packetType ccv.ConsumerPacketDataType, data ccv.ExportedIsConsumerPacketData_Data) {
idx := k.getAndIncrementPendingPacketsIdx(ctx) // for FIFO queue
key := types.PendingDataPacketsKey(idx)
key := types.PendingDataPacketsV1Key(idx)
store := ctx.KVStore(k.storeKey)
cpd := ccv.NewConsumerPacketData(packetType, data)
bz, err := cpd.Marshal()
Expand Down
10 changes: 5 additions & 5 deletions x/ccv/consumer/migrations/v2/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
func MigrateConsumerPacketData(ctx sdk.Context, store storetypes.KVStore) error {
// retrieve old data an deserialize
var oldData consumertypes.ConsumerPacketDataList
bz := store.Get([]byte{consumertypes.PendingDataPacketsBytePrefix})
bz := store.Get(consumertypes.PendingDataPacketsV1KeyPrefix())
if bz == nil {
ctx.Logger().Info("no pending data packets to migrate")
return nil
Expand All @@ -32,8 +32,8 @@ func MigrateConsumerPacketData(ctx sdk.Context, store storetypes.KVStore) error
// index incrementation happens in getAndIncrementPendingPacketsIdx
// the loop operations are equivalent to consumerkeeper.AppendPendingPacket()
for _, data := range oldData.List {
idx := getAndIncrementPendingPacketsIdx(ctx, store)
key := consumertypes.PendingDataPacketsKey(idx)
idx := getAndIncrementPendingPacketsIdx(store)
key := consumertypes.PendingDataPacketsV1Key(idx)
cpd := ccvtypes.NewConsumerPacketData(data.Type, data.Data)
bz, err := cpd.Marshal()
if err != nil {
Expand All @@ -43,12 +43,12 @@ func MigrateConsumerPacketData(ctx sdk.Context, store storetypes.KVStore) error
store.Set(key, bz)
}

store.Delete([]byte{consumertypes.PendingDataPacketsBytePrefix})
store.Delete(consumertypes.PendingDataPacketsV1KeyPrefix())
return nil
}

// getAndIncrementPendingPacketsIdx returns the current pending packets index and increments it.
func getAndIncrementPendingPacketsIdx(ctx sdk.Context, store storetypes.KVStore) (toReturn uint64) {
func getAndIncrementPendingPacketsIdx(store storetypes.KVStore) (toReturn uint64) {
bz := store.Get(consumertypes.PendingPacketsIndexKey())
if bz != nil {
toReturn = sdk.BigEndianToUint64(bz)
Expand Down
2 changes: 1 addition & 1 deletion x/ccv/consumer/migrations/v2/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ func setPendingPackets(ctx sdk.Context, store storetypes.KVStore, packets consum
if err != nil {
panic(fmt.Errorf("failed to marshal ConsumerPacketDataList: %w", err))
}
store.Set([]byte{consumertypes.PendingDataPacketsBytePrefix}, bz)
store.Set(consumertypes.PendingDataPacketsV1KeyPrefix(), bz)
}
Loading

0 comments on commit 66e209c

Please sign in to comment.