From 31a1524177698ae9b07d36cc41f1b4dcf30495ff Mon Sep 17 00:00:00 2001 From: Marius Poke Date: Fri, 18 Oct 2024 13:25:40 +0200 Subject: [PATCH] fix!: add check for zero rewards (#2363) * add check for zero rewards * add changelog entries --- .../unreleased/bug-fixes/2363-zero-rewards.md | 2 ++ .../state-breaking/2363-zero-rewards.md | 2 ++ x/ccv/provider/keeper/distribution.go | 34 +++++++++++-------- 3 files changed, 24 insertions(+), 14 deletions(-) create mode 100644 .changelog/unreleased/bug-fixes/2363-zero-rewards.md create mode 100644 .changelog/unreleased/state-breaking/2363-zero-rewards.md diff --git a/.changelog/unreleased/bug-fixes/2363-zero-rewards.md b/.changelog/unreleased/bug-fixes/2363-zero-rewards.md new file mode 100644 index 0000000000..819cfee817 --- /dev/null +++ b/.changelog/unreleased/bug-fixes/2363-zero-rewards.md @@ -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)) \ No newline at end of file diff --git a/.changelog/unreleased/state-breaking/2363-zero-rewards.md b/.changelog/unreleased/state-breaking/2363-zero-rewards.md new file mode 100644 index 0000000000..819cfee817 --- /dev/null +++ b/.changelog/unreleased/state-breaking/2363-zero-rewards.md @@ -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)) \ No newline at end of file diff --git a/x/ccv/provider/keeper/distribution.go b/x/ccv/provider/keeper/distribution.go index 8b5a0571d7..c03ecafb1e 100644 --- a/x/ccv/provider/keeper/distribution.go +++ b/x/ccv/provider/keeper/distribution.go @@ -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( @@ -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. @@ -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", @@ -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()