From 8a3553318befd4e86e90bb6939e9c8d92e019381 Mon Sep 17 00:00:00 2001 From: Shawn <44221603+smarshall-spitzbart@users.noreply.github.com> Date: Mon, 7 Aug 2023 13:32:09 -0700 Subject: [PATCH] DeleteHeadOfPendingPackets unit tests --- x/ccv/consumer/keeper/keeper.go | 3 +-- x/ccv/consumer/keeper/keeper_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/x/ccv/consumer/keeper/keeper.go b/x/ccv/consumer/keeper/keeper.go index b94f1a855d..94d5c790fd 100644 --- a/x/ccv/consumer/keeper/keeper.go +++ b/x/ccv/consumer/keeper/keeper.go @@ -607,12 +607,11 @@ func (k Keeper) getAndIncrementPendingPacketsIdx(ctx sdk.Context) (toReturn uint } // DeleteHeadOfPendingPackets deletes the head of the pending packets queue. -// TODO: UTs func (k Keeper) DeleteHeadOfPendingPackets(ctx sdk.Context) { store := ctx.KVStore(k.storeKey) iterator := sdk.KVStorePrefixIterator(store, []byte{types.PendingDataPacketsBytePrefix}) defer iterator.Close() - if !iterator.Valid() { // TODO: needed? + if !iterator.Valid() { return } store.Delete(iterator.Key()) diff --git a/x/ccv/consumer/keeper/keeper_test.go b/x/ccv/consumer/keeper/keeper_test.go index 269c60d9c5..78c8429642 100644 --- a/x/ccv/consumer/keeper/keeper_test.go +++ b/x/ccv/consumer/keeper/keeper_test.go @@ -579,3 +579,31 @@ func TestPrevStandaloneChainFlag(t *testing.T) { ck.MarkAsPrevStandaloneChain(ctx) require.True(t, ck.IsPrevStandaloneChain(ctx)) } + +func TestDeleteHeadOfPendingPackets(t *testing.T) { + consumerKeeper, ctx, ctrl, _ := testkeeper.GetConsumerKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t)) + defer ctrl.Finish() + + // append some pending packets + consumerKeeper.AppendPendingPacket(ctx, ccv.VscMaturedPacket, &ccv.ConsumerPacketData_VscMaturedPacketData{}) + consumerKeeper.AppendPendingPacket(ctx, ccv.SlashPacket, &ccv.ConsumerPacketData_SlashPacketData{}) + consumerKeeper.AppendPendingPacket(ctx, ccv.VscMaturedPacket, &ccv.ConsumerPacketData_VscMaturedPacketData{}) + + // Check there's 3 pending packets, vsc matured at head + pp := consumerKeeper.GetPendingPackets(ctx) + require.Len(t, pp, 3) + require.Equal(t, pp[0].Type, ccv.VscMaturedPacket) + + // Delete the head, confirm slash packet is now at head + consumerKeeper.DeleteHeadOfPendingPackets(ctx) + pp = consumerKeeper.GetPendingPackets(ctx) + require.Len(t, pp, 2) + require.Equal(t, pp[0].Type, ccv.SlashPacket) + + // Delete the head, confirm vsc matured packet is now at head + consumerKeeper.DeleteHeadOfPendingPackets(ctx) + pp = consumerKeeper.GetPendingPackets(ctx) + require.Len(t, pp, 1) + require.Equal(t, pp[0].Type, ccv.VscMaturedPacket) + +}