Skip to content

Commit

Permalink
move migration script
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Jan 10, 2024
1 parent 89475e5 commit d1feda8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 34 deletions.
2 changes: 1 addition & 1 deletion x/observer/keeper/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ func (m Migrator) Migrate3to4(ctx sdk.Context) error {
}

func (m Migrator) Migrate4to5(ctx sdk.Context) error {
return v5.MigrateStore(ctx, m.observerKeeper.storeKey, m.observerKeeper.cdc)
return v5.MigrateStore(ctx, m.observerKeeper)
}
33 changes: 1 addition & 32 deletions x/observer/migrations/v4/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ type observerKeeper interface {
}

func MigrateStore(ctx sdk.Context, observerKeeper observerKeeper) error {
if err := MigrateCrosschainFlags(ctx, observerKeeper.StoreKey(), observerKeeper.Codec()); err != nil {
return err
}
return MigrateObserverParams(ctx, observerKeeper)
return MigrateCrosschainFlags(ctx, observerKeeper.StoreKey(), observerKeeper.Codec())
}

func MigrateCrosschainFlags(ctx sdk.Context, observerStoreKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
Expand All @@ -45,31 +42,3 @@ func MigrateCrosschainFlags(ctx sdk.Context, observerStoreKey storetypes.StoreKe
store.Set([]byte{0}, b)
return nil
}

// MigrateObserverParams migrates the observer params to the chain params
// the function assumes that each oberver params entry has a corresponding chain params entry
// if the chain is not found, the observer params entry is ignored because it is considered as not supported
func MigrateObserverParams(ctx sdk.Context, observerKeeper observerKeeper) error {
chainParamsList, found := observerKeeper.GetChainParamsList(ctx)
if !found {
// no chain params found, nothing to migrate
return nil
}

// search for the observer params with chain params entry
observerParams := observerKeeper.GetParams(ctx).ObserverParams
for _, observerParam := range observerParams {
for i := range chainParamsList.ChainParams {
// if the chain is found, update the chain params with the observer params
if chainParamsList.ChainParams[i].ChainId == observerParam.Chain.ChainId {
chainParamsList.ChainParams[i].MinObserverDelegation = observerParam.MinObserverDelegation
chainParamsList.ChainParams[i].BallotThreshold = observerParam.BallotThreshold
chainParamsList.ChainParams[i].IsSupported = observerParam.IsSupported
break
}
}
}

observerKeeper.SetChainParamsList(ctx, chainParamsList)
return nil
}
48 changes: 47 additions & 1 deletion x/observer/migrations/v5/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,25 @@ import (
"github.com/zeta-chain/zetacore/x/observer/types"
)

func MigrateStore(ctx sdk.Context, observerStoreKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
// observerKeeper prevents circular dependency
type observerKeeper interface {
GetParams(ctx sdk.Context) types.Params
SetParams(ctx sdk.Context, params types.Params)
GetChainParamsList(ctx sdk.Context) (params types.ChainParamsList, found bool)
SetChainParamsList(ctx sdk.Context, params types.ChainParamsList)
StoreKey() storetypes.StoreKey
Codec() codec.BinaryCodec
}

func MigrateStore(ctx sdk.Context, observerKeeper observerKeeper) error {
if err := MigrateObserverMapper(ctx, observerKeeper.StoreKey(), observerKeeper.Codec()); err != nil {
return err
}
return MigrateObserverParams(ctx, observerKeeper)

}

func MigrateObserverMapper(ctx sdk.Context, observerStoreKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
var legacyObserverMappers []*types.ObserverMapper
legacyObserverMapperStore := prefix.NewStore(ctx.KVStore(observerStoreKey), types.KeyPrefix(types.ObserverMapperKey))
iterator := sdk.KVStorePrefixIterator(legacyObserverMapperStore, []byte{})
Expand All @@ -35,3 +53,31 @@ func MigrateStore(ctx sdk.Context, observerStoreKey storetypes.StoreKey, cdc cod
}
return nil
}

// MigrateObserverParams migrates the observer params to the chain params
// the function assumes that each oberver params entry has a corresponding chain params entry
// if the chain is not found, the observer params entry is ignored because it is considered as not supported
func MigrateObserverParams(ctx sdk.Context, observerKeeper observerKeeper) error {
chainParamsList, found := observerKeeper.GetChainParamsList(ctx)
if !found {
// no chain params found, nothing to migrate
return nil
}

// search for the observer params with chain params entry
observerParams := observerKeeper.GetParams(ctx).ObserverParams
for _, observerParam := range observerParams {
for i := range chainParamsList.ChainParams {
// if the chain is found, update the chain params with the observer params
if chainParamsList.ChainParams[i].ChainId == observerParam.Chain.ChainId {
chainParamsList.ChainParams[i].MinObserverDelegation = observerParam.MinObserverDelegation
chainParamsList.ChainParams[i].BallotThreshold = observerParam.BallotThreshold
chainParamsList.ChainParams[i].IsSupported = observerParam.IsSupported
break
}
}
}

observerKeeper.SetChainParamsList(ctx, chainParamsList)
return nil
}

0 comments on commit d1feda8

Please sign in to comment.