From f2aa45c960e775e8fcaff168ea2288d0a188bd84 Mon Sep 17 00:00:00 2001 From: oren-lava Date: Sun, 10 Mar 2024 16:36:40 +0200 Subject: [PATCH 1/4] CNS-923: added local cache for validate pairing --- x/pairing/keeper/msg_server_relay_payment.go | 53 +++++++++++++------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/x/pairing/keeper/msg_server_relay_payment.go b/x/pairing/keeper/msg_server_relay_payment.go index 7bc9494a86..de810cbc3a 100644 --- a/x/pairing/keeper/msg_server_relay_payment.go +++ b/x/pairing/keeper/msg_server_relay_payment.go @@ -14,6 +14,7 @@ import ( "github.com/lavanet/lava/utils/sigs" epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types" "github.com/lavanet/lava/x/pairing/types" + subscriptiontypes "github.com/lavanet/lava/x/subscription/types" ) type BadgeData struct { @@ -21,6 +22,11 @@ type BadgeData struct { BadgeSigner sdk.AccAddress } +type PairingData struct { + allowedCU uint64 + providers []epochstoragetypes.StakeEntry +} + func (k msgServer) RelayPayment(goCtx context.Context, msg *types.MsgRelayPayment) (*types.MsgRelayPaymentResponse, error) { if len(msg.LatestBlockReports) > len(msg.Relays) { return nil, utils.LavaFormatError("RelayPayment_invalid_latest_block_reports", fmt.Errorf("invalid latest block reports"), @@ -70,6 +76,7 @@ func (k msgServer) RelayPayment(goCtx context.Context, msg *types.MsgRelayPaymen var rejectedCu uint64 // aggregated rejected CU (due to badge CU overuse or provider double spending) rejected_relays_num := len(msg.Relays) + validatePairingCache := map[string]PairingData{} for relayIdx, relay := range msg.Relays { rejectedCu += relay.CuSum providerAddr, err := sdk.AccAddressFromBech32(relay.Provider) @@ -169,24 +176,36 @@ func (k msgServer) RelayPayment(goCtx context.Context, msg *types.MsgRelayPaymen ) } - isValidPairing, allowedCU, providers, err := k.Keeper.ValidatePairingForClient( - ctx, - relay.SpecId, - providerAddr, - uint64(relay.Epoch), - project, - ) - if err != nil { - return nil, utils.LavaFormatWarning("invalid pairing on proof of relay", err, - utils.Attribute{Key: "client", Value: clientAddr.String()}, - utils.Attribute{Key: "provider", Value: providerAddr.String()}, - ) - } - if !isValidPairing { - return nil, utils.LavaFormatWarning("invalid pairing on proof of relay", fmt.Errorf("pairing result doesn't include provider"), - utils.Attribute{Key: "client", Value: clientAddr.String()}, - utils.Attribute{Key: "provider", Value: providerAddr.String()}, + // generate validate pairing cache key with CuTrackerKey() to reuse code (doesn't relate to CU tracking at all) + validatePairingKey := subscriptiontypes.CuTrackerKey(clientAddr.String(), relay.Provider, relay.SpecId) + providers := []epochstoragetypes.StakeEntry{} + allowedCU := uint64(0) + pairingData, ok := validatePairingCache[validatePairingKey] + if ok { + allowedCU = pairingData.allowedCU + providers = pairingData.providers + } else { + isValidPairing := false + isValidPairing, allowedCU, providers, err = k.Keeper.ValidatePairingForClient( + ctx, + relay.SpecId, + providerAddr, + uint64(relay.Epoch), + project, ) + if err != nil { + return nil, utils.LavaFormatWarning("invalid pairing on proof of relay", err, + utils.Attribute{Key: "client", Value: clientAddr.String()}, + utils.Attribute{Key: "provider", Value: providerAddr.String()}, + ) + } + if !isValidPairing { + return nil, utils.LavaFormatWarning("invalid pairing on proof of relay", fmt.Errorf("pairing result doesn't include provider"), + utils.Attribute{Key: "client", Value: clientAddr.String()}, + utils.Attribute{Key: "provider", Value: providerAddr.String()}, + ) + } + validatePairingCache[validatePairingKey] = PairingData{allowedCU: allowedCU, providers: providers} } rewardedCU, err := k.Keeper.EnforceClientCUsUsageInEpoch(ctx, relay.CuSum, allowedCU, totalCUInEpochForUserProvider, clientAddr, relay.SpecId, uint64(relay.Epoch)) From f2a7b35e754d30c5a9327f87963fc8c074a52b99 Mon Sep 17 00:00:00 2001 From: oren-lava Date: Sun, 10 Mar 2024 16:51:24 +0200 Subject: [PATCH 2/4] CNS-923: lint fix --- x/pairing/keeper/msg_server_relay_payment.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/pairing/keeper/msg_server_relay_payment.go b/x/pairing/keeper/msg_server_relay_payment.go index de810cbc3a..c3e87ab30c 100644 --- a/x/pairing/keeper/msg_server_relay_payment.go +++ b/x/pairing/keeper/msg_server_relay_payment.go @@ -178,7 +178,7 @@ func (k msgServer) RelayPayment(goCtx context.Context, msg *types.MsgRelayPaymen // generate validate pairing cache key with CuTrackerKey() to reuse code (doesn't relate to CU tracking at all) validatePairingKey := subscriptiontypes.CuTrackerKey(clientAddr.String(), relay.Provider, relay.SpecId) - providers := []epochstoragetypes.StakeEntry{} + var providers []epochstoragetypes.StakeEntry allowedCU := uint64(0) pairingData, ok := validatePairingCache[validatePairingKey] if ok { From e33ae32dd77042944191f696e87d51636ed548db Mon Sep 17 00:00:00 2001 From: oren-lava Date: Mon, 11 Mar 2024 11:33:32 +0200 Subject: [PATCH 3/4] CNS-923: recalculate allowed CU in cache --- x/pairing/keeper/msg_server_relay_payment.go | 23 +++++++++++--------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/x/pairing/keeper/msg_server_relay_payment.go b/x/pairing/keeper/msg_server_relay_payment.go index c3e87ab30c..478098e03f 100644 --- a/x/pairing/keeper/msg_server_relay_payment.go +++ b/x/pairing/keeper/msg_server_relay_payment.go @@ -22,11 +22,6 @@ type BadgeData struct { BadgeSigner sdk.AccAddress } -type PairingData struct { - allowedCU uint64 - providers []epochstoragetypes.StakeEntry -} - func (k msgServer) RelayPayment(goCtx context.Context, msg *types.MsgRelayPayment) (*types.MsgRelayPaymentResponse, error) { if len(msg.LatestBlockReports) > len(msg.Relays) { return nil, utils.LavaFormatError("RelayPayment_invalid_latest_block_reports", fmt.Errorf("invalid latest block reports"), @@ -76,7 +71,7 @@ func (k msgServer) RelayPayment(goCtx context.Context, msg *types.MsgRelayPaymen var rejectedCu uint64 // aggregated rejected CU (due to badge CU overuse or provider double spending) rejected_relays_num := len(msg.Relays) - validatePairingCache := map[string]PairingData{} + validatePairingCache := map[string][]epochstoragetypes.StakeEntry{} for relayIdx, relay := range msg.Relays { rejectedCu += relay.CuSum providerAddr, err := sdk.AccAddressFromBech32(relay.Provider) @@ -180,10 +175,18 @@ func (k msgServer) RelayPayment(goCtx context.Context, msg *types.MsgRelayPaymen validatePairingKey := subscriptiontypes.CuTrackerKey(clientAddr.String(), relay.Provider, relay.SpecId) var providers []epochstoragetypes.StakeEntry allowedCU := uint64(0) - pairingData, ok := validatePairingCache[validatePairingKey] + val, ok := validatePairingCache[validatePairingKey] if ok { - allowedCU = pairingData.allowedCU - providers = pairingData.providers + providers = val + strictestPolicy, _, err := k.GetProjectStrictestPolicy(ctx, project, relay.SpecId, epochStart) + if err != nil { + return nil, utils.LavaFormatError("strictest policy calculation for pairing validation cache failed", err, + utils.LogAttr("project", project.Index), + utils.LogAttr("chainID", relay.SpecId), + utils.LogAttr("block", strconv.FormatUint(epochStart, 10)), + ) + } + allowedCU = strictestPolicy.EpochCuLimit } else { isValidPairing := false isValidPairing, allowedCU, providers, err = k.Keeper.ValidatePairingForClient( @@ -205,7 +208,7 @@ func (k msgServer) RelayPayment(goCtx context.Context, msg *types.MsgRelayPaymen utils.Attribute{Key: "provider", Value: providerAddr.String()}, ) } - validatePairingCache[validatePairingKey] = PairingData{allowedCU: allowedCU, providers: providers} + validatePairingCache[validatePairingKey] = providers } rewardedCU, err := k.Keeper.EnforceClientCUsUsageInEpoch(ctx, relay.CuSum, allowedCU, totalCUInEpochForUserProvider, clientAddr, relay.SpecId, uint64(relay.Epoch)) From dc1f7c8da648245fa24b5548fc6e652f769120f6 Mon Sep 17 00:00:00 2001 From: oren-lava Date: Tue, 12 Mar 2024 14:47:12 +0200 Subject: [PATCH 4/4] CNS-923: consume constant gas --- x/pairing/keeper/msg_server_relay_payment.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/x/pairing/keeper/msg_server_relay_payment.go b/x/pairing/keeper/msg_server_relay_payment.go index 478098e03f..6d13cf6161 100644 --- a/x/pairing/keeper/msg_server_relay_payment.go +++ b/x/pairing/keeper/msg_server_relay_payment.go @@ -316,6 +316,10 @@ func (k msgServer) RelayPayment(goCtx context.Context, msg *types.MsgRelayPaymen } utils.LogLavaEvent(ctx, logger, types.LatestBlocksReportEventName, latestBlockReports, "New LatestBlocks Report for provider") + // consume constant gas (dependent on the number of relays) + ctx.GasMeter().RefundGas(ctx.GasMeter().GasConsumed(), "") + ctx.GasMeter().ConsumeGas(uint64(10000+100000*len(msg.Relays)), "") + return &types.MsgRelayPaymentResponse{RejectedRelays: rejected_relays}, nil }