Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Epochstorage migrator hot fix #1630

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion x/epochstorage/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/lavanet/lava/v2/utils"
"github.com/lavanet/lava/v2/x/epochstorage/types"
v3 "github.com/lavanet/lava/v2/x/epochstorage/types/migrations/v3"
v6 "github.com/lavanet/lava/v2/x/epochstorage/types/migrations/v6"
)

type Migrator struct {
Expand All @@ -22,6 +23,53 @@ func NewMigrator(keeper Keeper) Migrator {
return Migrator{keeper: keeper}
}

// Migrate5to6 goes over all existing stake entries and populates the new vault address field with the stake entry address
func (m Migrator) Migrate5to6(ctx sdk.Context) error {
utils.LavaFormatDebug("migrate: epochstorage to include provider and vault addresses")

store := prefix.NewStore(ctx.KVStore(m.keeper.storeKey), types.KeyPrefix(v3.StakeStorageKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var stakeStorageV6 v6.StakeStorage
m.keeper.cdc.MustUnmarshal(iterator.Value(), &stakeStorageV6)

for i := range stakeStorageV6.StakeEntries {
stakeStorageV6.StakeEntries[i].Vault = stakeStorageV6.StakeEntries[i].Address
}

store.Set(iterator.Key(), m.keeper.cdc.MustMarshal(&stakeStorageV6))
}

return nil
}

// Migrate6to7 goes over all existing stake entries and populates the new description field with current moniker
func (m Migrator) Migrate6to7(ctx sdk.Context) error {
utils.LavaFormatDebug("migrate: epochstorage to include detailed description")

store := prefix.NewStore(ctx.KVStore(m.keeper.storeKey), types.KeyPrefix(v3.StakeStorageKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var stakeStorageV7 types.StakeStorage
m.keeper.cdc.MustUnmarshal(iterator.Value(), &stakeStorageV7)

for i := range stakeStorageV7.StakeEntries {
stakeStorageV7.StakeEntries[i].Description.Moniker = stakeStorageV7.StakeEntries[i].Moniker
stakeStorageV7.StakeEntries[i].Moniker = ""
}

store.Set(iterator.Key(), m.keeper.cdc.MustMarshal(&stakeStorageV7))
}

return nil
}

// Migrate7to8 transfers all the stake entries from the old stake storage to the new stake entries store
// StakeStorage is set to the stake entries store
// StakeStorageCurrent is set to the stake entries current store
Expand All @@ -39,7 +87,7 @@ func (m Migrator) Migrate7to8(ctx sdk.Context) error {
key := string(iterator.Key())

// identify stake storage type: regular, current or unstake
if key == v3.StakeStorageKeyUnstakeConst {
if m.isUnstakeStakeStorageKey(key) {
store.Delete(iterator.Key())
continue
}
Expand Down Expand Up @@ -99,6 +147,15 @@ func extractEpochFromStakeStorageKey(key string) (uint64, error) {
return parsedUint, nil
}

func (m Migrator) isUnstakeStakeStorageKey(key string) bool {
key, found := strings.CutSuffix(key, "/")
if !found {
return false
}

return key == v3.StakeStorageKeyUnstakeConst
}

func (m Migrator) isCurrentStakeStorageKey(ctx sdk.Context, key string) bool {
// the legacy StakeStorage key (both regular and current) had a "/" which should be cut off
key, found := strings.CutSuffix(key, "/")
Expand Down
12 changes: 12 additions & 0 deletions x/epochstorage/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {

migrator := keeper.NewMigrator(am.keeper)

// register v5 -> v6 migration
if err := cfg.RegisterMigration(types.ModuleName, 5, migrator.Migrate5to6); err != nil {
// panic:ok: at start up, migration cannot proceed anyhow
panic(fmt.Errorf("%s: failed to register migration to v6: %w", types.ModuleName, err))
}

// register v6 -> v7 migration
if err := cfg.RegisterMigration(types.ModuleName, 6, migrator.Migrate6to7); err != nil {
// panic:ok: at start up, migration cannot proceed anyhow
panic(fmt.Errorf("%s: failed to register migration to v7: %w", types.ModuleName, err))
}

// register v7 -> v8 migration
if err := cfg.RegisterMigration(types.ModuleName, 7, migrator.Migrate7to8); err != nil {
// panic:ok: at start up, migration cannot proceed anyhow
Expand Down
Loading
Loading