Skip to content

Commit

Permalink
CNS-930: change GetAllProviderConsumerEpochCu return type
Browse files Browse the repository at this point in the history
  • Loading branch information
oren-lava committed Mar 28, 2024
1 parent d5cd7b8 commit a04a6b5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
19 changes: 15 additions & 4 deletions x/pairing/keeper/epoch_cu.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,29 @@ func (k Keeper) RemoveProviderConsumerEpochCu(ctx sdk.Context, epoch uint64, pro
}

// GetAllProviderConsumerEpochCuStore returns all the ProviderConsumerEpochCu from the store (used for genesis)
func (k Keeper) GetAllProviderConsumerEpochCu(ctx sdk.Context, epoch uint64) (keys []string, providerConsumerEpochCus []types.ProviderConsumerEpochCu) {
func (k Keeper) GetAllProviderConsumerEpochCu(ctx sdk.Context, epoch uint64) []types.ProviderConsumerEpochCuGenesis {
providerConsumerEpochCus := []types.ProviderConsumerEpochCuGenesis{}
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.ProviderConsumerEpochCuKeyPrefix())
iterator := sdk.KVStorePrefixIterator(store, []byte(strconv.FormatUint(epoch, 10)))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
keys = append(keys, string(iterator.Key()))
epoch, provider, project, chainID, err := types.DecodeProviderConsumerEpochCuKey(string(iterator.Key()))
if err != nil {
utils.LavaFormatError("could not decode ProviderConsumerEpochCu with epoch", err, utils.LogAttr("key", string(iterator.Key())))
continue
}
var pcec types.ProviderConsumerEpochCu
k.cdc.MustUnmarshal(iterator.Value(), &pcec)
providerConsumerEpochCus = append(providerConsumerEpochCus, pcec)
providerConsumerEpochCus = append(providerConsumerEpochCus, types.ProviderConsumerEpochCuGenesis{
Epoch: epoch,
Provider: provider,
Project: project,
ChainId: chainID,
ProviderConsumerEpochCu: pcec,
})
}

return keys, providerConsumerEpochCus
return providerConsumerEpochCus
}

// GetAllProviderConsumerEpochCuStore returns all the ProviderConsumerEpochCu from the store (used for genesis)
Expand Down
24 changes: 14 additions & 10 deletions x/pairing/keeper/epoch_cu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,24 @@ func TestProviderConsumerEpochCuRemove(t *testing.T) {
func TestProviderConsumerEpochCuGetAll(t *testing.T) {
keeper, ctx := keepertest.PairingKeeper(t)
items := createNProviderConsumerEpochCu(keeper, ctx, 10)
expectedKeys := []string{}
actualKeys := []string{}
actualPecs := []types.ProviderConsumerEpochCu{}
expectedPcecs := []types.ProviderConsumerEpochCuGenesis{}
actualPcecs := []types.ProviderConsumerEpochCuGenesis{}
for i := range items {
name := strconv.Itoa(i)
key := string(types.ProviderConsumerEpochCuKey(uint64(i), name, name, name))
expectedKeys = append(expectedKeys, key)
keys, pecs := keeper.GetAllProviderConsumerEpochCu(ctx, uint64(i))
actualKeys = append(actualKeys, keys...)
actualPecs = append(actualPecs, pecs...)
expectedPcecs = append(expectedPcecs, types.ProviderConsumerEpochCuGenesis{
Epoch: uint64(i),
Provider: name,
Project: name,
ChainId: name,
ProviderConsumerEpochCu: types.ProviderConsumerEpochCu{
Cu: uint64(i),
},
})
pecs := keeper.GetAllProviderConsumerEpochCu(ctx, uint64(i))
actualPcecs = append(actualPcecs, pecs...)
}

require.ElementsMatch(t, nullify.Fill(expectedKeys), nullify.Fill(actualKeys))
require.ElementsMatch(t, items, actualPecs)
require.ElementsMatch(t, expectedPcecs, actualPcecs)
}

func TestProviderConsumerEpochCuGetAllStore(t *testing.T) {
Expand Down
19 changes: 6 additions & 13 deletions x/pairing/keeper/epoch_payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/x/pairing/types"
)

Expand Down Expand Up @@ -47,7 +46,7 @@ func (k Keeper) RemoveAllEpochPaymentsForBlockAppendAdjustments(ctx sdk.Context,
// remove all provider epoch cu
k.RemoveAllProviderEpochCu(ctx, epochToDelete)

keys, pcecs := k.GetAllProviderConsumerEpochCu(ctx, epochToDelete)
pcecs := k.GetAllProviderConsumerEpochCu(ctx, epochToDelete)

// TODO: update Qos in providerQosFS. new consumers (cluster.subUsage = 0) get default QoS (what is default?)
consumerUsage := map[string]uint64{}
Expand All @@ -58,22 +57,16 @@ func (k Keeper) RemoveAllEpochPaymentsForBlockAppendAdjustments(ctx sdk.Context,
// we are keeping the iteration keys to keep determinism when going over the map
iterationOrder := []couplingConsumerProvider{}
couplingUsage := map[couplingConsumerProvider]uint64{}
for i := range keys {
_, provider, project, chainID, err := types.DecodeProviderConsumerEpochCuKey(keys[i])
if err != nil {
utils.LavaFormatError("invalid provider consumer epoch cu key", err, utils.LogAttr("key", keys[i]))
continue
}

coupling := couplingConsumerProvider{consumer: project, provider: provider}
for _, pcec := range pcecs {
coupling := couplingConsumerProvider{consumer: pcec.Project, provider: pcec.Provider}
if _, ok := couplingUsage[coupling]; !ok {
// only add it if it doesn't exist
iterationOrder = append(iterationOrder, coupling)
}
consumerUsage[project] += pcecs[i].Cu
couplingUsage[coupling] += pcecs[i].Cu
consumerUsage[pcec.Project] += pcec.ProviderConsumerEpochCu.Cu
couplingUsage[coupling] += pcec.ProviderConsumerEpochCu.Cu

k.RemoveProviderConsumerEpochCu(ctx, epochToDelete, provider, project, chainID)
k.RemoveProviderConsumerEpochCu(ctx, epochToDelete, pcec.Provider, pcec.Project, pcec.ChainId)
}
for _, coupling := range iterationOrder {
k.subscriptionKeeper.AppendAdjustment(ctx, coupling.consumer, coupling.provider, consumerUsage[coupling.consumer], couplingUsage[coupling])
Expand Down
3 changes: 1 addition & 2 deletions x/pairing/keeper/msg_server_relay_payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,8 @@ func TestEpochPaymentDeletion(t *testing.T) {
_, found := ts.Keepers.Pairing.GetProviderEpochCu(ts.Ctx, epoch, providerAddr, ts.spec.Index)
require.False(t, found)

listB, listC := ts.Keepers.Pairing.GetAllProviderConsumerEpochCu(ts.Ctx, epoch)
listB := ts.Keepers.Pairing.GetAllProviderConsumerEpochCu(ts.Ctx, epoch)
require.Len(t, listB, 0)
require.Len(t, listC, 0)
}

// Test that after the consumer uses some CU it's updated in its project and subscription
Expand Down

0 comments on commit a04a6b5

Please sign in to comment.