From d1feda83a5798a165ab2b0d2fd619aa71e740cb5 Mon Sep 17 00:00:00 2001 From: lumtis Date: Tue, 9 Jan 2024 22:23:33 -0800 Subject: [PATCH] move migration script --- x/observer/keeper/migrator.go | 2 +- x/observer/migrations/v4/migrate.go | 33 +------------------- x/observer/migrations/v5/migrate.go | 48 ++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 34 deletions(-) diff --git a/x/observer/keeper/migrator.go b/x/observer/keeper/migrator.go index e256358d87..47b8ecfcc7 100644 --- a/x/observer/keeper/migrator.go +++ b/x/observer/keeper/migrator.go @@ -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) } diff --git a/x/observer/migrations/v4/migrate.go b/x/observer/migrations/v4/migrate.go index a23cc28dd5..7444c46233 100644 --- a/x/observer/migrations/v4/migrate.go +++ b/x/observer/migrations/v4/migrate.go @@ -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 { @@ -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 -} diff --git a/x/observer/migrations/v5/migrate.go b/x/observer/migrations/v5/migrate.go index a5666ff43b..c774f8b352 100644 --- a/x/observer/migrations/v5/migrate.go +++ b/x/observer/migrations/v5/migrate.go @@ -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{}) @@ -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 +}