Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: add check for zero rewards #2363

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading