Skip to content

Commit

Permalink
Fix another bug in the limitConsumer
Browse files Browse the repository at this point in the history
  • Loading branch information
shleikes committed Mar 10, 2024
1 parent 502a8bc commit 8a30495
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions x/pairing/keeper/limitConsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
planstypes "github.com/lavanet/lava/x/plans/types"
)

func (k Keeper) EnforceClientCUsUsageInEpoch(ctx sdk.Context, relayCU, allowedCU, totalCUInEpochForUserProvider uint64, clientAddr sdk.AccAddress, chainID string, epoch uint64) (uint64, error) {
func (k Keeper) EnforceClientCUsUsageInEpoch(ctx sdk.Context, relayCU, epochAllowedCU, totalCUInEpochForUserProvider uint64, clientAddr sdk.AccAddress, chainID string, epoch uint64) (uint64, error) {
project, err := k.GetProjectData(ctx, clientAddr, chainID, epoch)
if err != nil {
return 0, err
Expand All @@ -32,15 +32,32 @@ func (k Keeper) EnforceClientCUsUsageInEpoch(ctx sdk.Context, relayCU, allowedCU
return 0, utils.LavaFormatError("total cu in epoch for consumer exceeded the amount of CU left in the subscription", fmt.Errorf("consumer CU limit exceeded for subscription"), []utils.Attribute{{Key: "subscriptionCuLeft", Value: sub.GetMonthCuLeft()}}...)
}

_, effectiveTotalCu := k.CalculateEffectiveAllowedCuPerEpochFromPolicies(policies, project.UsedCu, sub.MonthCuLeft)
if !planstypes.VerifyTotalCuUsage(effectiveTotalCu, totalCUInEpochForUserProvider) {
return effectiveTotalCu - project.UsedCu, nil
_, effectivePolicyTotalCu := k.CalculateEffectiveAllowedCuPerEpochFromPolicies(policies, project.UsedCu, sub.MonthCuLeft)
if !planstypes.VerifyTotalCuUsage(effectivePolicyTotalCu, totalCUInEpochForUserProvider) {
return effectivePolicyTotalCu - project.UsedCu, nil
}

culimit := allowedCU * k.downtimeKeeper.GetDowntimeFactor(ctx, epoch)
if totalCUInEpochForUserProvider > culimit {
epochCuLimit := epochAllowedCU * k.downtimeKeeper.GetDowntimeFactor(ctx, epoch)
// Check if the total CUs used in the epoch is larger than the CU left for this epoch
if totalCUInEpochForUserProvider > epochCuLimit {
// Return remaining CU allowed for this epoch
originalTotalCUInEpochForUserProvider := totalCUInEpochForUserProvider - relayCU
return culimit - originalTotalCUInEpochForUserProvider, nil
if originalTotalCUInEpochForUserProvider <= epochCuLimit {
// Remaining CU allowed for this epoch is the epoch limit minus the original total used
return epochCuLimit - originalTotalCUInEpochForUserProvider, nil
}

utils.LavaFormatWarning("Client exceeded epoch CU limit", fmt.Errorf("client CU usage exceeded epoch limit"),
utils.LogAttr("epochCuLimit", epochCuLimit),
utils.LogAttr("totalCUInEpochForUserProvider", totalCUInEpochForUserProvider),
utils.LogAttr("relayCU", relayCU),
utils.LogAttr("clientAddr", clientAddr.String()),
utils.LogAttr("chainID", chainID),
utils.LogAttr("epoch", epoch),
)

// There's no CU left for this epoch, so relay usage is 0
return 0, nil
}

return relayCU, nil
Expand Down

0 comments on commit 8a30495

Please sign in to comment.