diff --git a/x/pairing/keeper/epoch_payments.go b/x/pairing/keeper/epoch_payments.go index a406de8648..306aa8de0a 100644 --- a/x/pairing/keeper/epoch_payments.go +++ b/x/pairing/keeper/epoch_payments.go @@ -79,11 +79,11 @@ func (k Keeper) GetEpochPaymentsFromBlock(ctx sdk.Context, epoch uint64) (epochP } // Function to add an epoch payment to the epochPayments object -func (k Keeper) AddEpochPayment(ctx sdk.Context, chainID string, epoch uint64, userAddress sdk.AccAddress, providerAddress sdk.AccAddress, usedCU uint64, uniqueIdentifier string) (uint64, error) { +func (k Keeper) AddEpochPayment(ctx sdk.Context, chainID string, epoch uint64, projectID string, providerAddress sdk.AccAddress, usedCU uint64, uniqueIdentifier string) (uint64, error) { // add a uniquePaymentStorageClientProvider object (the object that represent the actual payment) to this epoch's providerPaymentPayment object - userPaymentProviderStorage, usedCUProviderTotal, err := k.AddProviderPaymentInEpoch(ctx, chainID, epoch, userAddress, providerAddress, usedCU, uniqueIdentifier) + userPaymentProviderStorage, usedCUProviderTotal, err := k.AddProviderPaymentInEpoch(ctx, chainID, epoch, projectID, providerAddress, usedCU, uniqueIdentifier) if err != nil { - return 0, utils.LavaFormatError("could not add epoch payment", err, []utils.Attribute{{Key: "userAddress", Value: userAddress}, {Key: "providerAddress", Value: providerAddress}, {Key: "uniqueIdentifier", Value: uniqueIdentifier}, {Key: "epoch", Value: epoch}, {Key: "chainID", Value: chainID}}...) + return 0, utils.LavaFormatError("could not add epoch payment", err, []utils.Attribute{{Key: "userAddress", Value: projectID}, {Key: "providerAddress", Value: providerAddress}, {Key: "uniqueIdentifier", Value: uniqueIdentifier}, {Key: "epoch", Value: epoch}, {Key: "chainID", Value: chainID}}...) } // get this epoch's epochPayments object diff --git a/x/pairing/keeper/msg_server_relay_payment.go b/x/pairing/keeper/msg_server_relay_payment.go index f8d47f6ddf..5947b09a47 100644 --- a/x/pairing/keeper/msg_server_relay_payment.go +++ b/x/pairing/keeper/msg_server_relay_payment.go @@ -185,7 +185,7 @@ func (k msgServer) RelayPayment(goCtx context.Context, msg *types.MsgRelayPaymen } // this prevents double spend attacks, and tracks the CU per session a client can use - totalCUInEpochForUserProvider, err := k.Keeper.AddEpochPayment(ctx, relay.SpecId, epochStart, clientAddr, providerAddr, relay.CuSum, strconv.FormatUint(relay.SessionId, 16)) + totalCUInEpochForUserProvider, err := k.Keeper.AddEpochPayment(ctx, relay.SpecId, epochStart, projectID, providerAddr, relay.CuSum, strconv.FormatUint(relay.SessionId, 16)) if err != nil { // double spending on user detected! return nil, utils.LavaFormatWarning("double spending detected", err, diff --git a/x/pairing/keeper/msg_server_relay_payment_gov_test.go b/x/pairing/keeper/msg_server_relay_payment_gov_test.go index a2c2353f9d..2624ff3bc2 100644 --- a/x/pairing/keeper/msg_server_relay_payment_gov_test.go +++ b/x/pairing/keeper/msg_server_relay_payment_gov_test.go @@ -9,6 +9,7 @@ import ( "github.com/lavanet/lava/utils/sigs" epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types" pairingtypes "github.com/lavanet/lava/x/pairing/types" + "github.com/lavanet/lava/x/projects/types" "github.com/stretchr/testify/require" ) @@ -657,8 +658,10 @@ func verifyRelayPaymentObjects(t *testing.T, ts *testStruct, relayRequest *pairi require.NotEmpty(t, providerPaymentStorageFromEpochPayments.GetIndex()) require.Equal(t, uint64(relayRequest.GetEpoch()), providerPaymentStorageFromEpochPayments.GetEpoch()) + project, err := ts.keepers.Projects.Developer(ts.ctx, &types.QueryDeveloperRequest{Developer: ts.clients[0].Addr.String()}) + require.Nil(t, err) // Get the UniquePaymentStorageClientProvider key - uniquePaymentStorageClientProviderKey := ts.keepers.Pairing.EncodeUniquePaymentKey(sdk.UnwrapSDKContext(ts.ctx), ts.clients[0].Addr, ts.providers[0].Addr, strconv.FormatUint(relayRequest.SessionId, 16), ts.spec.Name) + uniquePaymentStorageClientProviderKey := ts.keepers.Pairing.EncodeUniquePaymentKey(sdk.UnwrapSDKContext(ts.ctx), project.Project.Index, ts.providers[0].Addr, strconv.FormatUint(relayRequest.SessionId, 16), ts.spec.Name) // Get one of the uniquePaymentStorageClientProvider struct from providerPaymentStorageFromEpochPayments (note, this is one of the unique.. structs. So usedCU was calculated above with a function that takes into account all the structs) uniquePaymentStorageClientProviderFromProviderPaymentStorage := pairingtypes.UniquePaymentStorageClientProvider{} diff --git a/x/pairing/keeper/provider_payment_storage.go b/x/pairing/keeper/provider_payment_storage.go index f2e3e6ef77..b9c42d97bf 100644 --- a/x/pairing/keeper/provider_payment_storage.go +++ b/x/pairing/keeper/provider_payment_storage.go @@ -70,9 +70,9 @@ func (k Keeper) GetProviderPaymentStorageKey(ctx sdk.Context, chainID string, ep } // Function to add a payment (which is represented by a uniquePaymentStorageClientProvider object) to a providerPaymentStorage object -func (k Keeper) AddProviderPaymentInEpoch(ctx sdk.Context, chainID string, epoch uint64, userAddress sdk.AccAddress, providerAddress sdk.AccAddress, usedCU uint64, uniqueIdentifier string) (userPayment *types.ProviderPaymentStorage, usedCUConsumerTotal uint64, err error) { +func (k Keeper) AddProviderPaymentInEpoch(ctx sdk.Context, chainID string, epoch uint64, projectID string, providerAddress sdk.AccAddress, usedCU uint64, uniqueIdentifier string) (userPayment *types.ProviderPaymentStorage, usedCUConsumerTotal uint64, err error) { // create an uniquePaymentStorageClientProvider object and set it in the KVStore - isUnique, uniquePaymentStorageClientProviderEntryAddr := k.AddUniquePaymentStorageClientProvider(ctx, chainID, epoch, userAddress, providerAddress, uniqueIdentifier, usedCU) + isUnique, uniquePaymentStorageClientProviderEntryAddr := k.AddUniquePaymentStorageClientProvider(ctx, chainID, epoch, projectID, providerAddress, uniqueIdentifier, usedCU) if !isUnique { // the uniquePaymentStorageClientProvider object is not unique -> tried to use an existing identifier! return nil, 0, fmt.Errorf("failed to add user payment since uniqueIdentifier was already detected, and created on block %d", uniquePaymentStorageClientProviderEntryAddr.Block) @@ -90,7 +90,7 @@ func (k Keeper) AddProviderPaymentInEpoch(ctx sdk.Context, chainID string, epoch userPaymentStorageInEpoch.UniquePaymentStorageClientProviderKeys = append(userPaymentStorageInEpoch.UniquePaymentStorageClientProviderKeys, uniquePaymentStorageClientProviderEntryAddr.GetIndex()) // sum up the used CU for this provider and this consumer over this epoch - usedCUConsumerTotal, err = k.GetTotalUsedCUForConsumerPerEpoch(ctx, userAddress.String(), userPaymentStorageInEpoch.GetUniquePaymentStorageClientProviderKeys(), providerAddress.String()) + usedCUConsumerTotal, err = k.GetTotalUsedCUForConsumerPerEpoch(ctx, projectID, userPaymentStorageInEpoch.GetUniquePaymentStorageClientProviderKeys(), providerAddress.String()) if err != nil { return nil, 0, err } @@ -103,7 +103,7 @@ func (k Keeper) AddProviderPaymentInEpoch(ctx sdk.Context, chainID string, epoch } // Function to get the total serviced CU by a provider in this epoch for a specific consumer -func (k Keeper) GetTotalUsedCUForConsumerPerEpoch(ctx sdk.Context, consumerAddress string, uniquePaymentStorageKeys []string, providerAddress string) (uint64, error) { +func (k Keeper) GetTotalUsedCUForConsumerPerEpoch(ctx sdk.Context, projectID string, uniquePaymentStorageKeys []string, providerAddress string) (uint64, error) { usedCUProviderTotal := uint64(0) // go over the uniquePaymentStorageKeys @@ -113,12 +113,12 @@ func (k Keeper) GetTotalUsedCUForConsumerPerEpoch(ctx sdk.Context, consumerAddre if !found { return 0, utils.LavaFormatError("could not find uniquePaymentStorageClientProvider object", fmt.Errorf("unique payment object not found"), utils.Attribute{Key: "providerAddress", Value: providerAddress}, - utils.Attribute{Key: "consumerAddress", Value: consumerAddress}, + utils.Attribute{Key: "projectID", Value: projectID}, ) } // if the uniquePaymentStorageClientProvider object is between the provider and the specific consumer, add the serviced CU - if k.GetConsumerFromUniquePayment(&uniquePayment) == consumerAddress { + if k.GetConsumerFromUniquePayment(&uniquePayment) == projectID { usedCUProviderTotal += uniquePayment.UsedCU } } diff --git a/x/pairing/keeper/unique_payment_storage_client_provider.go b/x/pairing/keeper/unique_payment_storage_client_provider.go index 2b46e56264..2c5fe5eefa 100644 --- a/x/pairing/keeper/unique_payment_storage_client_provider.go +++ b/x/pairing/keeper/unique_payment_storage_client_provider.go @@ -63,8 +63,8 @@ func (k Keeper) GetAllUniquePaymentStorageClientProvider(ctx sdk.Context) (list return } -func (k Keeper) AddUniquePaymentStorageClientProvider(ctx sdk.Context, chainID string, block uint64, userAddress sdk.AccAddress, providerAddress sdk.AccAddress, uniqueIdentifier string, usedCU uint64) (isUnique bool, entryAddr *types.UniquePaymentStorageClientProvider) { - key := k.EncodeUniquePaymentKey(ctx, userAddress, providerAddress, uniqueIdentifier, chainID) +func (k Keeper) AddUniquePaymentStorageClientProvider(ctx sdk.Context, chainID string, block uint64, projectID string, providerAddress sdk.AccAddress, uniqueIdentifier string, usedCU uint64) (isUnique bool, entryAddr *types.UniquePaymentStorageClientProvider) { + key := k.EncodeUniquePaymentKey(ctx, projectID, providerAddress, uniqueIdentifier, chainID) entry, found := k.GetUniquePaymentStorageClientProvider(ctx, key) if found { return false, &entry @@ -81,20 +81,18 @@ func (k Keeper) GetConsumerFromUniquePayment(uniquePaymentStorageClientProvider return provider } -func maxAddressLengths() (int, int) { - return address.MaxAddrLen, address.MaxAddrLen +func maxAddressLengths() int { + return address.MaxAddrLen } -func (k Keeper) EncodeUniquePaymentKey(ctx sdk.Context, userAddress sdk.AccAddress, providerAddress sdk.AccAddress, uniqueIdentifier string, chainID string) string { - maxAdrLengthUser, maxAdrLengthProvider := maxAddressLengths() - providerLength, clientLength := len(providerAddress.String()), len(userAddress.String()) +func (k Keeper) EncodeUniquePaymentKey(ctx sdk.Context, projectID string, providerAddress sdk.AccAddress, uniqueIdentifier string, chainID string) string { + maxAdrLengthProvider := maxAddressLengths() + providerLength, clientLength := len(providerAddress.String()), len(projectID) if providerLength > maxAdrLengthProvider { panic(fmt.Sprintf("invalid providerAddress found! len(%s) != %d == %d", providerAddress.String(), maxAdrLengthProvider, len(providerAddress.String()))) - } else if clientLength > maxAdrLengthUser { - panic(fmt.Sprintf("invalid userAddress found! len(%s) != %d == %d", userAddress.String(), maxAdrLengthUser, len(userAddress.String()))) } leadingChar := asciiNumberToChar(clientLength) - key := string(leadingChar) + userAddress.String() + providerAddress.String() + uniqueIdentifier + chainID + key := string(leadingChar) + projectID + providerAddress.String() + uniqueIdentifier + chainID return key }