Skip to content

Commit

Permalink
fix: Epochstorage migrator hot fix (#1630)
Browse files Browse the repository at this point in the history
* migrator hot fix

* rewrite old migrators

* register old epochstorage migrations
  • Loading branch information
oren-lava authored Aug 14, 2024
1 parent 1bd1d53 commit 26a8fad
Show file tree
Hide file tree
Showing 5 changed files with 2,136 additions and 1 deletion.
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

0 comments on commit 26a8fad

Please sign in to comment.