diff --git a/x/authority/keeper/migrator.go b/x/authority/keeper/migrator.go new file mode 100644 index 0000000000..74085f6c54 --- /dev/null +++ b/x/authority/keeper/migrator.go @@ -0,0 +1,23 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + v2 "github.com/zeta-chain/zetacore/x/authority/migrations/v2" +) + +// Migrator is a struct for handling in-place store migrations. +type Migrator struct { + authorityKeeper Keeper +} + +// NewMigrator returns a new Migrator for the authority module. +func NewMigrator(keeper Keeper) Migrator { + return Migrator{ + authorityKeeper: keeper, + } +} + +// Migrate1to2 migrates the authority store from consensus version 1 to 2 +func (m Migrator) Migrate1to2(ctx sdk.Context) error { + return v2.MigrateStore(ctx, m.authorityKeeper) +} diff --git a/x/authority/migrations/v2/migrate.go b/x/authority/migrations/v2/migrate.go new file mode 100644 index 0000000000..9f5b7aaf80 --- /dev/null +++ b/x/authority/migrations/v2/migrate.go @@ -0,0 +1,20 @@ +package v2 + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/zeta-chain/zetacore/x/authority/types" +) + +type authorityKeeper interface { + SetAuthorizationList(ctx sdk.Context, list types.AuthorizationList) +} + +// MigrateStore migrates the authority module state from the consensus version 1 to 2 +func MigrateStore( + ctx sdk.Context, + keeper authorityKeeper, +) error { + ctx.Logger().Info("Migrating authority store from version 1 to 2") + keeper.SetAuthorizationList(ctx, types.DefaultAuthorizationsList()) + return nil +} diff --git a/x/authority/migrations/v2/migrate_test.go b/x/authority/migrations/v2/migrate_test.go new file mode 100644 index 0000000000..ee82f004ae --- /dev/null +++ b/x/authority/migrations/v2/migrate_test.go @@ -0,0 +1,27 @@ +package v2_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + keepertest "github.com/zeta-chain/zetacore/testutil/keeper" + v2 "github.com/zeta-chain/zetacore/x/authority/migrations/v2" + "github.com/zeta-chain/zetacore/x/authority/types" +) + +func TestMigrateStore(t *testing.T) { + t.Run("Set authorization list", func(t *testing.T) { + k, ctx := keepertest.AuthorityKeeper(t) + + list, found := k.GetAuthorizationList(ctx) + require.False(t, found) + require.Equal(t, types.AuthorizationList{}, list) + + err := v2.MigrateStore(ctx, *k) + require.NoError(t, err) + + list, found = k.GetAuthorizationList(ctx) + require.True(t, found) + require.Equal(t, types.DefaultAuthorizationsList(), list) + }) +} diff --git a/x/authority/module.go b/x/authority/module.go index f92da86e37..696c9c1edb 100644 --- a/x/authority/module.go +++ b/x/authority/module.go @@ -122,6 +122,10 @@ func (am AppModule) Name() string { func (am AppModule) RegisterServices(cfg module.Configurator) { types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) types.RegisterQueryServer(cfg.QueryServer(), am.keeper) + m := keeper.NewMigrator(am.keeper) + if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { + panic(err) + } } // RegisterInvariants registers the authority module's invariants. @@ -146,7 +150,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw } // ConsensusVersion implements AppModule/ConsensusVersion. -func (AppModule) ConsensusVersion() uint64 { return 1 } +func (AppModule) ConsensusVersion() uint64 { return 2 } // BeginBlock executes all ABCI BeginBlock logic respective to the authority module. func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} diff --git a/x/authority/types/authorization_list_test.go b/x/authority/types/authorization_list_test.go index 009e958c15..a41515ab1e 100644 --- a/x/authority/types/authorization_list_test.go +++ b/x/authority/types/authorization_list_test.go @@ -209,6 +209,11 @@ func TestAuthorizationList_Validate(t *testing.T) { authorizations types.AuthorizationList expectedError error }{ + { + name: "validate default authorizations list", + authorizations: types.DefaultAuthorizationsList(), + expectedError: nil, + }, { name: "validate successfully", authorizations: types.AuthorizationList{Authorizations: []types.Authorization{