Skip to content

Commit

Permalink
fix!: add check for zero rewards (#2363)
Browse files Browse the repository at this point in the history
* add check for zero rewards

* add changelog entries
  • Loading branch information
mpoke authored Oct 18, 2024
1 parent da02ef3 commit 31a1524
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/bug-fixes/2363-zero-rewards.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `[x/provider]` Add check for zero rewards to the rewards distribution logic.
([\#2363](https://github.com/cosmos/interchain-security/pull/2363))
2 changes: 2 additions & 0 deletions .changelog/unreleased/state-breaking/2363-zero-rewards.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `[x/provider]` Add check for zero rewards to the rewards distribution logic.
([\#2363](https://github.com/cosmos/interchain-security/pull/2363))
34 changes: 20 additions & 14 deletions x/ccv/provider/keeper/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,6 @@ func (k Keeper) DeleteConsumerRewardsAllocationByDenom(ctx sdk.Context, consumer

// AllocateConsumerRewards allocates the given rewards to provider consumer chain with the given consumer id
func (k Keeper) AllocateConsumerRewards(ctx sdk.Context, consumerId string, alloc types.ConsumerRewardsAllocation) (types.ConsumerRewardsAllocation, error) {
if alloc.Rewards.IsZero() {
return types.ConsumerRewardsAllocation{}, nil
}

chainId, err := k.GetConsumerChainId(ctx, consumerId)
if err != nil {
k.Logger(ctx).Error(
Expand Down Expand Up @@ -271,7 +267,6 @@ func (k Keeper) AllocateConsumerRewards(ctx sdk.Context, consumerId string, allo
// AllocateTokens performs rewards distribution to the community pool and validators
// based on the Partial Set Security distribution specification.
func (k Keeper) AllocateTokens(ctx sdk.Context) {

// Iterate over all launched consumer chains.
// To avoid large iterations over all the consumer IDs, iterate only over
// chains with an IBC client created.
Expand Down Expand Up @@ -302,7 +297,12 @@ func (k Keeper) AllocateTokens(ctx sdk.Context) {
)
continue
}
remainingRewardDec, err := k.AllocateConsumerRewards(cachedCtx, consumerId, consumerRewards)
if consumerRewards.Rewards.IsZero() {
// note that GetConsumerRewardsAllocationByDenom returns an empty ConsumerRewardsAllocation
// when there is no (consumerId, denom) key for consumer rewards allocations
continue
}
remainingRewardAllocation, err := k.AllocateConsumerRewards(cachedCtx, consumerId, consumerRewards)
if err != nil {
k.Logger(ctx).Error(
"fail to allocate rewards for consumer chain",
Expand All @@ -312,14 +312,20 @@ func (k Keeper) AllocateTokens(ctx sdk.Context) {
continue
}

err = k.SetConsumerRewardsAllocationByDenom(cachedCtx, consumerId, denom, remainingRewardDec)
if err != nil {
k.Logger(ctx).Error(
"fail to set rewards for consumer chain",
"consumer id", consumerId,
"error", err.Error(),
)
continue
if remainingRewardAllocation.Rewards.IsZero() {
// if there is no remaining consumer rewards allocation, then just delete the (consumerId, denom) key
k.DeleteConsumerRewardsAllocationByDenom(cachedCtx, consumerId, denom)
} else {
// otherwise, update the consumer rewards allocation
err = k.SetConsumerRewardsAllocationByDenom(cachedCtx, consumerId, denom, remainingRewardAllocation)
if err != nil {
k.Logger(ctx).Error(
"fail to set rewards for consumer chain",
"consumer id", consumerId,
"error", err.Error(),
)
continue
}
}

writeCache()
Expand Down

0 comments on commit 31a1524

Please sign in to comment.