Skip to content

Commit

Permalink
add migration script
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Apr 24, 2024
1 parent 482b15f commit 8091422
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/setup_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
)

const releaseVersion = "v15"
const releaseVersion = "v16"

func SetupHandlers(app *App) {
app.UpgradeKeeper.SetUpgradeHandler(releaseVersion, func(ctx sdk.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) {
Expand Down
9 changes: 6 additions & 3 deletions pkg/chains/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ func BtcDustOffset() int64 {
}

// DefaultChainsList returns a list of default chains
// TODO : Check why polygon is not in this list
func DefaultChainsList() []*Chain {
return chainListPointers([]Chain{
BtcMainnetChain(),
Expand All @@ -243,7 +242,7 @@ func DefaultChainsList() []*Chain {
})
}

// MainnetChainList returns a list of mainnet chains
// ChainListByNetworkType returns a list of chains by network type
func ChainListByNetworkType(networkType NetworkType) []*Chain {
var mainNetList []*Chain
for _, chain := range DefaultChainsList() {
Expand All @@ -254,6 +253,7 @@ func ChainListByNetworkType(networkType NetworkType) []*Chain {
return mainNetList
}

// ChainListByNetwork returns a list of chains by network
func ChainListByNetwork(network Network) []*Chain {
var chainList []*Chain
for _, chain := range DefaultChainsList() {
Expand All @@ -275,6 +275,7 @@ func ExternalChainList() []*Chain {
return chainList
}

// ChainListByConsensus returns a list of chains by consensus
func ChainListByConsensus(consensus Consensus) []*Chain {
var chainList []*Chain
for _, chain := range DefaultChainsList() {
Expand All @@ -285,6 +286,8 @@ func ChainListByConsensus(consensus Consensus) []*Chain {
return chainList

}

// ChainListForHeaderSupport returns a list of chains that support headers
func ChainListForHeaderSupport() []*Chain {
var chainList []*Chain
for _, chain := range DefaultChainsList() {
Expand All @@ -295,7 +298,7 @@ func ChainListForHeaderSupport() []*Chain {
return chainList
}

// ZetaChainFromChainID returns a ZetaChain chainobject from a Cosmos chain ID
// ZetaChainFromChainID returns a ZetaChain chain object from a Cosmos chain ID
func ZetaChainFromChainID(chainID string) (Chain, error) {
ethChainID, err := CosmosToEthChainID(chainID)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions x/observer/keeper/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
v5 "github.com/zeta-chain/zetacore/x/observer/migrations/v5"
v6 "github.com/zeta-chain/zetacore/x/observer/migrations/v6"
v7 "github.com/zeta-chain/zetacore/x/observer/migrations/v7"
v8 "github.com/zeta-chain/zetacore/x/observer/migrations/v8"
)

// Migrator is a struct for handling in-place store migrations.
Expand Down Expand Up @@ -48,3 +49,7 @@ func (m Migrator) Migrate5to6(ctx sdk.Context) error {
func (m Migrator) Migrate6to7(ctx sdk.Context) error {
return v7.MigrateStore(ctx, m.observerKeeper)
}

func (m Migrator) Migrate7to8(ctx sdk.Context) error {
return v8.MigrateStore(ctx, m.observerKeeper)
}
24 changes: 24 additions & 0 deletions x/observer/migrations/v8/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package v8

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/x/observer/types"
)

type observerKeeper interface {
GetParamsIfExists(ctx sdk.Context) (params types.Params)
SetParams(ctx sdk.Context, params types.Params)
}

// MigrateStore performs in-place store migrations from v6 to v7
func MigrateStore(ctx sdk.Context, observerKeeper observerKeeper) error {
ctx.Logger().Info("Migrating observer store from v6 to v7")
params := observerKeeper.GetParamsIfExists(ctx)
for _, ob := range params.ObserverParams {
chain := chains.GetChainFromChainID(ob.Chain.ChainId)
ob.Chain = chain
}
observerKeeper.SetParams(ctx, params)
return nil
}
79 changes: 79 additions & 0 deletions x/observer/migrations/v8/migrate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package v8_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/pkg/chains"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
v8 "github.com/zeta-chain/zetacore/x/observer/migrations/v8"
"github.com/zeta-chain/zetacore/x/observer/types"
)

func Test_MigrateStore(t *testing.T) {
tt := []struct {
name string
params types.Params
chainList []*chains.Chain
}{
{
name: "privnet params",
params: LegacyParamsForNetwork(chains.NetworkType_PRIVNET),
chainList: chains.ChainListByNetworkType(chains.NetworkType_PRIVNET),
},
{
name: "testnet params",
params: LegacyParamsForNetwork(chains.NetworkType_TESTNET),
chainList: chains.ChainListByNetworkType(chains.NetworkType_TESTNET),
},
{
name: "mainnet params",
params: LegacyParamsForNetwork(chains.NetworkType_MAINNET),
chainList: chains.ChainListByNetworkType(chains.NetworkType_MAINNET),
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)
k.SetParams(ctx, tc.params)

chainListOld := make([]chains.Chain, len(tc.params.ObserverParams))
for i, ob := range tc.params.ObserverParams {
chainListOld[i] = *ob.Chain
}

err := v8.MigrateStore(ctx, k)
require.NoError(t, err)

params := k.GetParamsIfExists(ctx)
chainListNew := make([]chains.Chain, len(params.ObserverParams))
for i, ob := range params.ObserverParams {
chainListNew[i] = *ob.Chain
}
chainListTest := make([]chains.Chain, len(tc.chainList))
for i, chain := range tc.chainList {
chainListTest[i] = *chain
}
require.NotEqual(t, chainListTest, chainListOld)
require.Equal(t, chainListTest, chainListNew)
})

}

}

func LegacyParamsForNetwork(networkType chains.NetworkType) types.Params {
chainList := chains.ChainListByNetworkType(networkType)
observerParams := make([]*types.ObserverParams, len(chainList))
for i, chain := range chainList {
observerParams[i] = &types.ObserverParams{
IsSupported: true,
// Set chain-Id and chain-name only for legacy params
Chain: &chains.Chain{ChainId: chain.ChainId, ChainName: chain.ChainName},
BallotThreshold: sdk.MustNewDecFromStr("0.66"),
MinObserverDelegation: sdk.MustNewDecFromStr("1000000000000000000000"),
}
}
return types.NewParams(observerParams, types.DefaultAdminPolicy(), 100)
}
5 changes: 4 additions & 1 deletion x/observer/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
if err := cfg.RegisterMigration(types.ModuleName, 6, m.Migrate6to7); err != nil {
panic(err)
}
if err := cfg.RegisterMigration(types.ModuleName, 7, m.Migrate7to8); err != nil {
panic(err)
}
}

// RegisterInvariants registers the observer module's invariants.
Expand All @@ -180,7 +183,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 7 }
func (AppModule) ConsensusVersion() uint64 { return 8 }

// BeginBlock executes all ABCI BeginBlock logic respective to the observer module.
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
Expand Down

0 comments on commit 8091422

Please sign in to comment.