Skip to content

Commit 31a1524

Browse files
authored
fix!: add check for zero rewards (#2363)
* add check for zero rewards * add changelog entries
1 parent da02ef3 commit 31a1524

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `[x/provider]` Add check for zero rewards to the rewards distribution logic.
2+
([\#2363](https://github.com/cosmos/interchain-security/pull/2363))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `[x/provider]` Add check for zero rewards to the rewards distribution logic.
2+
([\#2363](https://github.com/cosmos/interchain-security/pull/2363))

x/ccv/provider/keeper/distribution.go

+20-14
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ func (k Keeper) DeleteConsumerRewardsAllocationByDenom(ctx sdk.Context, consumer
139139

140140
// AllocateConsumerRewards allocates the given rewards to provider consumer chain with the given consumer id
141141
func (k Keeper) AllocateConsumerRewards(ctx sdk.Context, consumerId string, alloc types.ConsumerRewardsAllocation) (types.ConsumerRewardsAllocation, error) {
142-
if alloc.Rewards.IsZero() {
143-
return types.ConsumerRewardsAllocation{}, nil
144-
}
145-
146142
chainId, err := k.GetConsumerChainId(ctx, consumerId)
147143
if err != nil {
148144
k.Logger(ctx).Error(
@@ -271,7 +267,6 @@ func (k Keeper) AllocateConsumerRewards(ctx sdk.Context, consumerId string, allo
271267
// AllocateTokens performs rewards distribution to the community pool and validators
272268
// based on the Partial Set Security distribution specification.
273269
func (k Keeper) AllocateTokens(ctx sdk.Context) {
274-
275270
// Iterate over all launched consumer chains.
276271
// To avoid large iterations over all the consumer IDs, iterate only over
277272
// chains with an IBC client created.
@@ -302,7 +297,12 @@ func (k Keeper) AllocateTokens(ctx sdk.Context) {
302297
)
303298
continue
304299
}
305-
remainingRewardDec, err := k.AllocateConsumerRewards(cachedCtx, consumerId, consumerRewards)
300+
if consumerRewards.Rewards.IsZero() {
301+
// note that GetConsumerRewardsAllocationByDenom returns an empty ConsumerRewardsAllocation
302+
// when there is no (consumerId, denom) key for consumer rewards allocations
303+
continue
304+
}
305+
remainingRewardAllocation, err := k.AllocateConsumerRewards(cachedCtx, consumerId, consumerRewards)
306306
if err != nil {
307307
k.Logger(ctx).Error(
308308
"fail to allocate rewards for consumer chain",
@@ -312,14 +312,20 @@ func (k Keeper) AllocateTokens(ctx sdk.Context) {
312312
continue
313313
}
314314

315-
err = k.SetConsumerRewardsAllocationByDenom(cachedCtx, consumerId, denom, remainingRewardDec)
316-
if err != nil {
317-
k.Logger(ctx).Error(
318-
"fail to set rewards for consumer chain",
319-
"consumer id", consumerId,
320-
"error", err.Error(),
321-
)
322-
continue
315+
if remainingRewardAllocation.Rewards.IsZero() {
316+
// if there is no remaining consumer rewards allocation, then just delete the (consumerId, denom) key
317+
k.DeleteConsumerRewardsAllocationByDenom(cachedCtx, consumerId, denom)
318+
} else {
319+
// otherwise, update the consumer rewards allocation
320+
err = k.SetConsumerRewardsAllocationByDenom(cachedCtx, consumerId, denom, remainingRewardAllocation)
321+
if err != nil {
322+
k.Logger(ctx).Error(
323+
"fail to set rewards for consumer chain",
324+
"consumer id", consumerId,
325+
"error", err.Error(),
326+
)
327+
continue
328+
}
323329
}
324330

325331
writeCache()

0 commit comments

Comments
 (0)