Skip to content

Commit

Permalink
wrapper type
Browse files Browse the repository at this point in the history
  • Loading branch information
shaspitz committed Jul 17, 2023
1 parent ffb6208 commit dca0dc1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
31 changes: 28 additions & 3 deletions x/ccv/consumer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,9 +606,29 @@ func (k Keeper) getAndIncrementPendingPacketsIdx(ctx sdk.Context) (toReturn uint
return toReturn
}

// GetPendingPackets returns ALL the pending CCV packets from the store
// GetPendingPackets returns ALL the pending CCV packets from the store without indexes.
func (k Keeper) GetPendingPackets(ctx sdk.Context) []ccv.ConsumerPacketData {
var packets []ccv.ConsumerPacketData
ppWithIndexes := k.GetAllPendingPacketsWithIdx(ctx)
var ppList []ccv.ConsumerPacketData
for _, ppWithIndex := range ppWithIndexes {
ppList = append(ppList, ppWithIndex.ConsumerPacketData)
}
return ppList
}

// ConsumerPacketDataWithIdx is a wrapper around ConsumerPacketData
// that also stores the index of the packet in the pending packets queue.
//
// Note this type is a shim to avoid changing the schema of ConsumerPacketData and breaking the wire.
type ConsumerPacketDataWithIdx struct {
ccv.ConsumerPacketData // Struct embedding
Idx uint64
}

// GetAllPendingPacketsWithIdx returns ALL pending consumer packet data from the store
// with indexes relevant to the pending packets queue.
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().
Expand All @@ -622,7 +642,12 @@ func (k Keeper) GetPendingPackets(ctx sdk.Context) []ccv.ConsumerPacketData {
// An error here would indicate something is very wrong,
panic(fmt.Errorf("failed to unmarshal pending data packet: %w", err))
}
packets = append(packets, packet)
packetWithIdx := ConsumerPacketDataWithIdx{
ConsumerPacketData: packet,
// index stored in key after prefix, see PendingDataPacketsKey()
Idx: sdk.BigEndianToUint64(iterator.Key()[1:]),
}
packets = append(packets, packetWithIdx)
}
return packets
}
Expand Down
5 changes: 5 additions & 0 deletions x/ccv/consumer/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,16 +398,21 @@ func TestPendingPackets(t *testing.T) {
consumerKeeper.DeletePendingDataPackets(ctx, 5)
storedPacketData = consumerKeeper.GetPendingPackets(ctx)
require.Equal(t, packetData[:len(packetData)-1], storedPacketData)
pendingPacketsWithIdx := consumerKeeper.GetAllPendingPacketsWithIdx(ctx)
require.Equal(t, uint64(4), pendingPacketsWithIdx[len(pendingPacketsWithIdx)-1].Idx) // final element should have idx 4

// Delete packet with idx 0 (first index)
consumerKeeper.DeletePendingDataPackets(ctx, 0)
storedPacketData = consumerKeeper.GetPendingPackets(ctx)
require.Equal(t, packetData[1:len(packetData)-1], storedPacketData)
pendingPacketsWithIdx = consumerKeeper.GetAllPendingPacketsWithIdx(ctx)
require.Equal(t, uint64(1), pendingPacketsWithIdx[0].Idx) // first element should have idx 1

// Delete all packets
consumerKeeper.DeleteAllPendingDataPackets(ctx)
storedPacketData = consumerKeeper.GetPendingPackets(ctx)
require.Empty(t, storedPacketData)
require.Empty(t, consumerKeeper.GetAllPendingPacketsWithIdx(ctx))
}

// TestVerifyProviderChain tests the VerifyProviderChain method for the consumer keeper
Expand Down
2 changes: 1 addition & 1 deletion x/ccv/consumer/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (k Keeper) SendPackets(ctx sdk.Context) {
return
}

pending := k.GetPendingPackets(ctx)
pending := k.GetAllPendingPacketsWithIdx(ctx)
idxsForDeletion := []uint64{}
for _, p := range pending {

Expand Down

0 comments on commit dca0dc1

Please sign in to comment.