Skip to content

Commit

Permalink
refactor HandlePacketDataForChain
Browse files Browse the repository at this point in the history
  • Loading branch information
shaspitz committed Dec 16, 2022
1 parent 068f7bc commit 8f6a296
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions x/ccv/provider/keeper/throttle.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,21 @@ func (k Keeper) HandlePacketDataForChain(ctx sdktypes.Context, consumerChainID s
vscMaturedPacketHandler func(sdktypes.Context, string, ccvtypes.VSCMaturedPacketData),
) {
// Get slash packet data and trailing vsc matured packet data, handle it all.
slashFound, slashData, vscMaturedData, seqNums := k.GetSlashAndTrailingData(ctx, consumerChainID)
if slashFound {
slashPacketHandler(ctx, consumerChainID, slashData)
slashFound, slashData, slashSeqNum, vscMaturedData, vscMaturedSeqNums := k.GetSlashAndTrailingData(ctx, consumerChainID)
if !slashFound {
// This should only happen if the queue data has become corrupted.
ctx.Logger().Error(fmt.Sprintf("no slash packet data found for chain %s", consumerChainID))
return
}
slashPacketHandler(ctx, consumerChainID, slashData)
for _, vscMData := range vscMaturedData {
vscMaturedPacketHandler(ctx, consumerChainID, vscMData)
}

// Delete handled data after it has all been handled.
k.DeleteThrottledPacketData(ctx, consumerChainID, seqNums...)
// Delete data using ibc seq numbers after it has all been handled.
toDel := []uint64{slashSeqNum}
toDel = append(toDel, vscMaturedSeqNums...)
k.DeleteThrottledPacketData(ctx, consumerChainID, toDel...)
}

// InitializeSlashMeter initializes the slash meter to it's max value (also its allowance),
Expand Down Expand Up @@ -323,8 +328,8 @@ func (k Keeper) QueueThrottledPacketData(
// Note: this method is not tested directly, but is covered indirectly
// by TestHandlePacketDataForChain and e2e tests.
func (k Keeper) GetSlashAndTrailingData(ctx sdktypes.Context, consumerChainID string) (
slashFound bool, slashData ccvtypes.SlashPacketData,
vscMaturedData []ccvtypes.VSCMaturedPacketData, ibcSeqNums []uint64) {
slashFound bool, slashData ccvtypes.SlashPacketData, slashSeqNum uint64,
vscMaturedData []ccvtypes.VSCMaturedPacketData, vscMaturedSeqNums []uint64) {

store := ctx.KVStore(k.storeKey)
iteratorPrefix := providertypes.ChainIdWithLenKey(providertypes.ThrottledPacketDataBytePrefix, consumerChainID)
Expand All @@ -333,8 +338,9 @@ func (k Keeper) GetSlashAndTrailingData(ctx sdktypes.Context, consumerChainID st

slashFound = false
slashData = ccvtypes.SlashPacketData{}
slashSeqNum = 0
vscMaturedData = []ccvtypes.VSCMaturedPacketData{}
ibcSeqNums = []uint64{}
vscMaturedSeqNums = []uint64{}

for ; iterator.Valid(); iterator.Next() {

Expand All @@ -348,21 +354,21 @@ func (k Keeper) GetSlashAndTrailingData(ctx sdktypes.Context, consumerChainID st
panic(fmt.Sprintf("failed to unmarshal pending packet data: %v", err))
}
slashFound = true
_, slashSeqNum = providertypes.MustParseThrottledPacketDataKey(iterator.Key())
}
} else if bz[0] == vscMaturedPacketData {
vscMData := ccvtypes.VSCMaturedPacketData{}
if err := vscMData.Unmarshal(bz[1:]); err != nil {
panic(fmt.Sprintf("failed to unmarshal pending packet data: %v", err))
}
vscMaturedData = append(vscMaturedData, vscMData)
_, newSeqNum := providertypes.MustParseThrottledPacketDataKey(iterator.Key())
vscMaturedSeqNums = append(vscMaturedSeqNums, newSeqNum)
} else {
panic("invalid packet data type")
}

_, ibcSeqNum := providertypes.MustParseThrottledPacketDataKey(iterator.Key())
ibcSeqNums = append(ibcSeqNums, ibcSeqNum)
}
return slashFound, slashData, vscMaturedData, ibcSeqNums
return slashFound, slashData, slashSeqNum, vscMaturedData, vscMaturedSeqNums
}

// GetAllThrottledPacketData returns all throttled packet data for a specific consumer chain.
Expand Down

0 comments on commit 8f6a296

Please sign in to comment.