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

CNS-923: added local cache for validate pairing #1288

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Changes from 2 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
53 changes: 36 additions & 17 deletions x/pairing/keeper/msg_server_relay_payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ 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 {
Badge types.Badge
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"),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
var 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,
Yaroms marked this conversation as resolved.
Show resolved Hide resolved
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))
Expand Down
Loading