-
Notifications
You must be signed in to change notification settings - Fork 203
/
provider_payment_storage.go
131 lines (110 loc) · 5.64 KB
/
provider_payment_storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package keeper
import (
"fmt"
"strconv"
"strings"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/x/pairing/types"
)
// SetProviderPaymentStorage set a specific providerPaymentStorage in the store from its index
func (k Keeper) SetProviderPaymentStorage(ctx sdk.Context, providerPaymentStorage types.ProviderPaymentStorage) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ProviderPaymentStorageKeyPrefix))
b := k.cdc.MustMarshal(&providerPaymentStorage)
store.Set(types.ProviderPaymentStorageKey(
providerPaymentStorage.Index,
), b)
}
// GetProviderPaymentStorage returns a providerPaymentStorage from its index
func (k Keeper) GetProviderPaymentStorage(
ctx sdk.Context,
index string,
) (val types.ProviderPaymentStorage, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ProviderPaymentStorageKeyPrefix))
b := store.Get(types.ProviderPaymentStorageKey(
index,
))
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// RemoveProviderPaymentStorage removes a providerPaymentStorage from the store
func (k Keeper) RemoveProviderPaymentStorage(
ctx sdk.Context,
index string,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ProviderPaymentStorageKeyPrefix))
store.Delete(types.ProviderPaymentStorageKey(
index,
))
}
// GetAllProviderPaymentStorage returns all providerPaymentStorage
func (k Keeper) GetAllProviderPaymentStorage(ctx sdk.Context) (list []types.ProviderPaymentStorage) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ProviderPaymentStorageKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.ProviderPaymentStorage
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
func (k Keeper) GetProviderFromProviderPaymentStorage(providerPaymentStorage *types.ProviderPaymentStorage) (string, error) {
index := providerPaymentStorage.Index
// index consists of chain_epoch_providerAddress
lastIndex := strings.LastIndex(index, "_")
if lastIndex != -1 {
return index[lastIndex+1:], nil
}
return "", fmt.Errorf("invalid provider payment storage key %s", index)
}
// Function to get a providerPaymentStorage object's key (key is chainID_epoch_providerAddress, epoch in hex representation)
func (k Keeper) GetProviderPaymentStorageKey(ctx sdk.Context, chainID string, epoch uint64, providerAddress sdk.AccAddress) string {
return chainID + "_" + strconv.FormatUint(epoch, 16) + "_" + providerAddress.String()
}
// 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, projectID string, providerAddress sdk.AccAddress, usedCU uint64, uniqueIdentifier string) (userPayment *types.ProviderPaymentStorage, usedCUConsumerTotal uint64) {
// create an uniquePaymentStorageClientProvider object and set it in the KVStore
uniquePaymentStorageClientProviderEntryAddr := k.AddUniquePaymentStorageClientProvider(ctx, chainID, epoch, projectID, providerAddress, uniqueIdentifier, usedCU)
// get the providerPaymentStorage object
providerPaymentStorageKey := k.GetProviderPaymentStorageKey(ctx, chainID, epoch, providerAddress)
userPaymentStorageInEpoch, found := k.GetProviderPaymentStorage(ctx, providerPaymentStorageKey)
if !found {
// is new entry -> create a new providerPaymentStorage object
userPaymentStorageInEpoch = types.ProviderPaymentStorage{Index: providerPaymentStorageKey, UniquePaymentStorageClientProviderKeys: []string{uniquePaymentStorageClientProviderEntryAddr.GetIndex()}, Epoch: epoch}
usedCUConsumerTotal = usedCU
} else {
// found the providerPaymentStorage object -> append the uniquePaymentStorageClientProvider object's key
userPaymentStorageInEpoch.UniquePaymentStorageClientProviderKeys = append(userPaymentStorageInEpoch.UniquePaymentStorageClientProviderKeys, uniquePaymentStorageClientProviderEntryAddr.GetIndex())
// sum up the used CU for this provider and this consumer over this epoch
usedCUConsumerTotal = k.GetTotalUsedCUForConsumerPerEpoch(ctx, projectID, userPaymentStorageInEpoch.GetUniquePaymentStorageClientProviderKeys(), providerAddress.String())
}
// set the providerPaymentStorage object in the KVStore
k.SetProviderPaymentStorage(ctx, userPaymentStorageInEpoch)
return &userPaymentStorageInEpoch, usedCUConsumerTotal
}
// Function to get the total serviced CU by a provider in this epoch for a specific consumer
func (k Keeper) GetTotalUsedCUForConsumerPerEpoch(ctx sdk.Context, projectID string, uniquePaymentStorageKeys []string, providerAddress string) uint64 {
usedCUProviderTotal := uint64(0)
// go over the uniquePaymentStorageKeys
for _, uniquePaymentKey := range uniquePaymentStorageKeys {
// get the uniquePaymentStorageClientProvider object
uniquePayment, found := k.GetUniquePaymentStorageClientProvider(ctx, uniquePaymentKey)
if !found {
utils.LavaFormatError("could not find uniquePaymentStorageClientProvider object", fmt.Errorf("unique payment object not found"),
utils.Attribute{Key: "providerAddress", Value: providerAddress},
utils.Attribute{Key: "projectID", Value: projectID},
)
continue
}
// if the uniquePaymentStorageClientProvider object is between the provider and the specific consumer, add the serviced CU
if k.GetConsumerFromUniquePayment(&uniquePayment) == projectID {
usedCUProviderTotal += uniquePayment.UsedCU
}
}
return usedCUProviderTotal
}