Skip to content

Commit

Permalink
add check for zero rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoke committed Oct 17, 2024
1 parent 621af72 commit f729341
Showing 1 changed file with 20 additions and 14 deletions.
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 f729341

Please sign in to comment.