Skip to content

Commit

Permalink
Merge pull request #573 from lavanet/CNS-ProjectID-event
Browse files Browse the repository at this point in the history
CNS - epoch payments now use project id
  • Loading branch information
Yaroms authored Jun 25, 2023
2 parents 5d1dd4d + 4dbf00a commit 850be5b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
6 changes: 3 additions & 3 deletions x/pairing/keeper/epoch_payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion x/pairing/keeper/msg_server_relay_payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion x/pairing/keeper/msg_server_relay_payment_gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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{}
Expand Down
12 changes: 6 additions & 6 deletions x/pairing/keeper/provider_payment_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
}
Expand Down
18 changes: 8 additions & 10 deletions x/pairing/keeper/unique_payment_storage_client_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down

0 comments on commit 850be5b

Please sign in to comment.