From 8a304950313fe96d1d28abb0cb92e9563d4239c7 Mon Sep 17 00:00:00 2001 From: Elad Gildnur Date: Thu, 7 Mar 2024 14:37:03 +0200 Subject: [PATCH] Fix another bug in the limitConsumer --- x/pairing/keeper/limitConsumer.go | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/x/pairing/keeper/limitConsumer.go b/x/pairing/keeper/limitConsumer.go index af2e5b6fe3..46fb6b71d2 100644 --- a/x/pairing/keeper/limitConsumer.go +++ b/x/pairing/keeper/limitConsumer.go @@ -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 @@ -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