Skip to content

Commit

Permalink
Merge branch 'CNS-368-fixation-store-fix-refcount' into CNS-376-fixat…
Browse files Browse the repository at this point in the history
…ion-versioning-and-migration
  • Loading branch information
orenl-lava committed Apr 18, 2023
2 parents dea157d + 4616ae1 commit 1a3cb21
Show file tree
Hide file tree
Showing 37 changed files with 933 additions and 331 deletions.
6 changes: 6 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ import (
"github.com/lavanet/lava/app/upgrades/v0_5_1"
"github.com/lavanet/lava/app/upgrades/v0_5_2"
"github.com/lavanet/lava/app/upgrades/v0_9_1"
"github.com/lavanet/lava/app/upgrades/v0_9_6"
"github.com/lavanet/lava/app/upgrades/v0_9_7"
"github.com/lavanet/lava/app/upgrades/v0_9_8"
"github.com/lavanet/lava/docs"
conflictmodule "github.com/lavanet/lava/x/conflict"
conflictmodulekeeper "github.com/lavanet/lava/x/conflict/keeper"
Expand Down Expand Up @@ -148,6 +151,9 @@ var Upgrades = []upgrades.Upgrade{
upgrades.Upgrade_0_9_2,
upgrades.Upgrade_0_9_3,
upgrades.Upgrade_0_9_5,
v0_9_6.Upgrade,
v0_9_7.Upgrade,
v0_9_8.Upgrade,
}

// this line is used by starport scaffolding # stargate/wasm/app/enabledProposals
Expand Down
14 changes: 14 additions & 0 deletions app/upgrades/v0_9_6/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package v0_9_6

import (
store "github.com/cosmos/cosmos-sdk/store/types"
"github.com/lavanet/lava/app/upgrades"
)

const UpgradeName = "v0.9.6"

var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName, // upgrade name defined few lines above
CreateUpgradeHandler: CreateUpgradeHandler, // create CreateUpgradeHandler in upgrades.go below
StoreUpgrades: store.StoreUpgrades{}, // StoreUpgrades has 3 fields: Added/Renamed/Deleted any module that fits these description should be added in the way below
}
49 changes: 49 additions & 0 deletions app/upgrades/v0_9_6/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package v0_9_6

import (
"log"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/lavanet/lava/app/keepers"
"github.com/lavanet/lava/app/upgrades"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
)

const (
// ClientPaymentStorageKeyPrefix is the prefix to retrieve all ClientPaymentStorage
ClientPaymentStorageKeyPrefix = "ClientPaymentStorage/value/"
// UniquePaymentStorageClientProviderKeyPrefix is the prefix to retrieve all UniquePaymentStorageClientProvider
UniquePaymentStorageClientProviderKeyPrefix = "UniquePaymentStorageClientProvider/value/"
)

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
bpm upgrades.BaseAppParamManager,
keepers *keepers.LavaKeepers,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
log.Println("########################")
log.Println("# STARTING UPGRADE #")
log.Println("########################")

chainIDs := keepers.SpecKeeper.GetAllChainIDs(ctx)
for _, chainID := range chainIDs {
storage, found := keepers.EpochstorageKeeper.GetStakeStorageCurrent(ctx, epochstoragetypes.ProviderKey, chainID)
if !found {
continue
}

for _, entry := range storage.StakeEntries {
err := keepers.PairingKeeper.FreezeProvider(ctx, entry.Address, []string{chainID}, "")
if err != nil {
continue
}
}
}

return mm.RunMigrations(ctx, configurator, vm)
}
}
14 changes: 14 additions & 0 deletions app/upgrades/v0_9_7/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package v0_9_7

import (
store "github.com/cosmos/cosmos-sdk/store/types"
"github.com/lavanet/lava/app/upgrades"
)

const UpgradeName = "v0.9.7"

var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName, // upgrade name defined few lines above
CreateUpgradeHandler: CreateUpgradeHandler, // create CreateUpgradeHandler in upgrades.go below
StoreUpgrades: store.StoreUpgrades{}, // StoreUpgrades has 3 fields: Added/Renamed/Deleted any module that fits these description should be added in the way below
}
49 changes: 49 additions & 0 deletions app/upgrades/v0_9_7/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package v0_9_7

import (
"log"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/lavanet/lava/app/keepers"
"github.com/lavanet/lava/app/upgrades"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
)

const (
// ClientPaymentStorageKeyPrefix is the prefix to retrieve all ClientPaymentStorage
ClientPaymentStorageKeyPrefix = "ClientPaymentStorage/value/"
// UniquePaymentStorageClientProviderKeyPrefix is the prefix to retrieve all UniquePaymentStorageClientProvider
UniquePaymentStorageClientProviderKeyPrefix = "UniquePaymentStorageClientProvider/value/"
)

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
bpm upgrades.BaseAppParamManager,
keepers *keepers.LavaKeepers,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
log.Println("########################")
log.Println("# STARTING UPGRADE #")
log.Println("########################")

chainIDs := keepers.SpecKeeper.GetAllChainIDs(ctx)
for _, chainID := range chainIDs {
storage, found := keepers.EpochstorageKeeper.GetStakeStorageCurrent(ctx, epochstoragetypes.ProviderKey, chainID)
if !found {
continue
}

for _, entry := range storage.StakeEntries {
err := keepers.PairingKeeper.FreezeProvider(ctx, entry.Address, []string{chainID}, "")
if err != nil {
continue
}
}
}

return mm.RunMigrations(ctx, configurator, vm)
}
}
14 changes: 14 additions & 0 deletions app/upgrades/v0_9_8/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package v0_9_8

import (
store "github.com/cosmos/cosmos-sdk/store/types"
"github.com/lavanet/lava/app/upgrades"
)

const UpgradeName = "v0.9.8"

var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName, // upgrade name defined few lines above
CreateUpgradeHandler: CreateUpgradeHandler, // create CreateUpgradeHandler in upgrades.go below
StoreUpgrades: store.StoreUpgrades{}, // StoreUpgrades has 3 fields: Added/Renamed/Deleted any module that fits these description should be added in the way below
}
49 changes: 49 additions & 0 deletions app/upgrades/v0_9_8/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package v0_9_8

import (
"log"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/lavanet/lava/app/keepers"
"github.com/lavanet/lava/app/upgrades"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
)

const (
// ClientPaymentStorageKeyPrefix is the prefix to retrieve all ClientPaymentStorage
ClientPaymentStorageKeyPrefix = "ClientPaymentStorage/value/"
// UniquePaymentStorageClientProviderKeyPrefix is the prefix to retrieve all UniquePaymentStorageClientProvider
UniquePaymentStorageClientProviderKeyPrefix = "UniquePaymentStorageClientProvider/value/"
)

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
bpm upgrades.BaseAppParamManager,
keepers *keepers.LavaKeepers,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
log.Println("########################")
log.Println("# STARTING UPGRADE #")
log.Println("########################")

chainIDs := keepers.SpecKeeper.GetAllChainIDs(ctx)
for _, chainID := range chainIDs {
storage, found := keepers.EpochstorageKeeper.GetStakeStorageCurrent(ctx, epochstoragetypes.ProviderKey, chainID)
if !found {
continue
}

for _, entry := range storage.StakeEntries {
err := keepers.PairingKeeper.FreezeProvider(ctx, entry.Address, []string{chainID}, "")
if err != nil {
continue
}
}
}

return mm.RunMigrations(ctx, configurator, vm)
}
}
12 changes: 9 additions & 3 deletions common/fixation_entry_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ func (fs FixationStore) removeEntryIndex(ctx sdk.Context, safeIndex string) {
store.Delete(types.KeyPrefix(fs.createEntryIndexKey(safeIndex)))
}

// GetAllEntryIndices returns all Entry indices
func (fs FixationStore) GetAllEntryIndices(ctx sdk.Context) []string {
// GetAllEntryIndexWithPrefix returns all Entry indices with a given prefix
func (fs FixationStore) GetAllEntryIndicesWithPrefix(ctx sdk.Context, prefix string) []string {
store := fs.getEntryIndexStore(ctx)
iterator := sdk.KVStorePrefixIterator(store, []byte{})
entryPrefix := types.KeyPrefix(fs.createEntryIndexKey(prefix))
iterator := sdk.KVStorePrefixIterator(store, entryPrefix)
defer iterator.Close()

// iterate over the store's values and save the indices in a list
Expand All @@ -58,6 +59,11 @@ func (fs FixationStore) GetAllEntryIndices(ctx sdk.Context) []string {
return indexList
}

// GetAllEntryIndices returns all Entry indices
func (fs FixationStore) GetAllEntryIndices(ctx sdk.Context) []string {
return fs.GetAllEntryIndicesWithPrefix(ctx, "")
}

// GetAllEntryVersions returns a list of all versions (blocks) of an entry.
// If stale == true, then the output will include stale versions (for testing).
func (fs *FixationStore) GetAllEntryVersions(ctx sdk.Context, index string, stale bool) (blocks []uint64) {
Expand Down
31 changes: 27 additions & 4 deletions common/fixation_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,14 @@ func testWithFixationTemplate(t *testing.T, playbook []fixationTemplate, countOb
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + play.count)
fs[play.store].AdvanceBlock(ctx)
case "getall":
indices := fs[play.store].GetAllEntryIndices(ctx)
require.Equal(t, int(play.count), len(indices), what)
indexList := fs[play.store].GetAllEntryIndices(ctx)
require.Equal(t, int(play.count), len(indexList), what)
case "getvers":
indices := fs[play.store].GetAllEntryVersions(ctx, index, true)
require.Equal(t, int(play.count), len(indices), what)
indexList := fs[play.store].GetAllEntryVersions(ctx, index, true)
require.Equal(t, int(play.count), len(indexList), what)
case "getallprefix":
indexList := fs[play.store].GetAllEntryIndicesWithPrefix(ctx, index)
require.Equal(t, int(play.count), len(indexList), what)
}
}
}
Expand Down Expand Up @@ -361,3 +364,23 @@ func TestEntriesSort(t *testing.T) {

testWithFixationTemplate(t, playbook, 3, 1)
}

func TestGetAllEntries(t *testing.T) {
block0 := int64(10)

playbook := []fixationTemplate{
{ op: "append", name: "entry #1", index: "prefix1_a", count: block0, coin: 0 },
{ op: "append", name: "entry #1", index: "prefix1_b", count: block0, coin: 1 },
{ op: "append", name: "entry #1", index: "prefix1_c", count: block0, coin: 2 },
{ op: "append", name: "entry #1", index: "prefix2_a", count: block0, coin: 3 },
{ op: "append", name: "entry #1", index: "prefix2_b", count: block0, coin: 4 },
{ op: "append", name: "entry #1", index: "prefix3_a", count: block0, coin: 5 },
{ op: "getall", name: "to check all indices", count: 6 },
{ op: "getallprefix", name: "to check all indices with prefix", index: "prefix", count: 6 },
{ op: "getallprefix", name: "to check indices with prefix1", index: "prefix1", count: 3 },
{ op: "getallprefix", name: "to check indices with prefix2", index: "prefix2", count: 2 },
{ op: "getallprefix", name: "to check indices with prefix3", index: "prefix3", count: 1 },
}

testWithFixationTemplate(t, playbook, 6, 1)
}
7 changes: 6 additions & 1 deletion common/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,24 @@ func (tstore *TimerStore) tickValue(ctx sdk.Context, which types.TimerType, tick
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()

// iterate over the timers: collect those that expire
var removals []timerTuple

// iterate over the timers and collect those that expire: because the
// key is block height/timestamp, the iterator yields entries ordered
// by height/timestamp.

for ; iterator.Valid(); iterator.Next() {
value := types.DecodeKey(iterator.Key())
if value > tickValue {
// stop at first not-expired timer (update next timeout)
tstore.setNextTimeout(ctx, which, value)
break
}
tuple := timerTuple{value, string(iterator.Value())}
removals = append(removals, tuple)
}

// if no more pending timers - then set next timeout to infinity
if !iterator.Valid() {
tstore.setNextTimeout(ctx, which, math.MaxInt64)
}
Expand Down
20 changes: 20 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29674,6 +29674,16 @@ paths:
in: query
required: false
type: boolean
- name: pagination.reverse
description: >-
reverse is set to true if results are to be returned in the
descending order.


Since: cosmos-sdk 0.43
in: query
required: false
type: boolean
tags:
- Query
'/lavanet/lava/epochstorage/fixated_params/{index}':
Expand Down Expand Up @@ -29933,6 +29943,16 @@ paths:
in: query
required: false
type: boolean
- name: pagination.reverse
description: >-
reverse is set to true if results are to be returned in the
descending order.


Since: cosmos-sdk 0.43
in: query
required: false
type: boolean
tags:
- Query
'/lavanet/lava/epochstorage/stake_storage/{index}':
Expand Down
Loading

0 comments on commit 1a3cb21

Please sign in to comment.