From e1ba3f4626534622c1cc5431cd0b0d7962970311 Mon Sep 17 00:00:00 2001 From: "jaeseung.bae" Date: Wed, 13 Mar 2024 18:04:14 +0900 Subject: [PATCH] chore: clean-up --- x/bankplus/keeper/deprecator.go | 18 ++++++++++----- x/bankplus/keeper/deprecator_test.go | 7 +++--- x/bankplus/keeper/keeper.go | 22 ++++--------------- x/bankplus/keeper/migrations.go | 27 ----------------------- x/bankplus/migrations/v1/types.go | 11 ---------- x/bankplus/module/module.go | 33 ++++++++++++++++++---------- 6 files changed, 40 insertions(+), 78 deletions(-) delete mode 100644 x/bankplus/keeper/migrations.go delete mode 100644 x/bankplus/migrations/v1/types.go diff --git a/x/bankplus/keeper/deprecator.go b/x/bankplus/keeper/deprecator.go index a364ec6f24..5d3eecf19d 100644 --- a/x/bankplus/keeper/deprecator.go +++ b/x/bankplus/keeper/deprecator.go @@ -3,22 +3,28 @@ package keeper import ( "context" - "cosmossdk.io/core/store" storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/runtime" - - v1 "github.com/Finschia/finschia-sdk/x/bankplus/migrations/v1" + sdk "github.com/cosmos/cosmos-sdk/types" ) +// inactiveAddrsKeyPrefix Keys for bankplus store but this prefix must not be overlap with bank key prefix. +var inactiveAddrsKeyPrefix = []byte{0xa0} + +// inactiveAddrKey key of a specific inactiveAddr from store +func inactiveAddrKey(addr sdk.AccAddress) []byte { + return append(inactiveAddrsKeyPrefix, addr.Bytes()...) +} + // DeprecateBankPlus performs in-place store migrations for bankplus v1 // migration includes: // // - Remove all the state(inactive addresses) -func DeprecateBankPlus(ctx context.Context, storeService store.KVStoreService) error { - kvStore := storeService.OpenKVStore(ctx) +func DeprecateBankPlus(ctx context.Context, keeper BaseKeeper) error { + kvStore := keeper.storeService.OpenKVStore(ctx) adapter := runtime.KVStoreAdapter(kvStore) - iterator := storetypes.KVStorePrefixIterator(adapter, v1.InactiveAddrsKeyPrefix) + iterator := storetypes.KVStorePrefixIterator(adapter, inactiveAddrsKeyPrefix) defer iterator.Close() for ; iterator.Valid(); iterator.Next() { diff --git a/x/bankplus/keeper/deprecator_test.go b/x/bankplus/keeper/deprecator_test.go index f96a37f2f0..c26bdc96ef 100644 --- a/x/bankplus/keeper/deprecator_test.go +++ b/x/bankplus/keeper/deprecator_test.go @@ -21,7 +21,6 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - v1 "github.com/Finschia/finschia-sdk/x/bankplus/migrations/v1" "github.com/Finschia/finschia-sdk/x/bankplus/types" ) @@ -63,7 +62,7 @@ func (s *DeprecationTestSuite) TestDeprecateBankPlus() { s.Require().True(isStoredInactiveAddr(s.ctx, s.storeService, oldAcc.GetAddress())) s.Require().True(isStoredInactiveAddr(s.ctx, s.storeService, anotherOldAcc.GetAddress())) - err := DeprecateBankPlus(s.ctx, s.storeService) + err := DeprecateBankPlus(s.ctx, BaseKeeper{storeService: s.storeService}) s.Require().NoError(err) s.Require().False(isStoredInactiveAddr(s.ctx, s.storeService, oldAcc.GetAddress())) @@ -73,7 +72,7 @@ func (s *DeprecationTestSuite) TestDeprecateBankPlus() { // isStoredInactiveAddr checks if the address is stored or not as blocked address func isStoredInactiveAddr(ctx context.Context, storeService store.KVStoreService, address sdk.AccAddress) bool { kvStore := storeService.OpenKVStore(ctx) - bz, _ := kvStore.Get(v1.InactiveAddrKey(address)) + bz, _ := kvStore.Get(inactiveAddrKey(address)) return bz != nil } @@ -87,7 +86,7 @@ func addToInactiveAddr(ctx context.Context, storeService store.KVStoreService, c blockedCAddr := types.InactiveAddr{Address: addrString} bz := cdc.MustMarshal(&blockedCAddr) - if err := kvStore.Set(v1.InactiveAddrKey(address), bz); err != nil { + if err := kvStore.Set(inactiveAddrKey(address), bz); err != nil { panic(err) } } diff --git a/x/bankplus/keeper/keeper.go b/x/bankplus/keeper/keeper.go index a28312e93d..c19408a159 100644 --- a/x/bankplus/keeper/keeper.go +++ b/x/bankplus/keeper/keeper.go @@ -1,7 +1,6 @@ package keeper import ( - "cosmossdk.io/core/address" "cosmossdk.io/core/store" "cosmossdk.io/log" @@ -14,28 +13,15 @@ var _ bankkeeper.Keeper = (*BaseKeeper)(nil) type BaseKeeper struct { bankkeeper.BaseKeeper - - ak types.AccountKeeper - cdc codec.Codec - addrCdc address.Codec - - storeService store.KVStoreService - inactiveAddrs map[string]bool + storeService store.KVStoreService } func NewBaseKeeper( cdc codec.Codec, storeService store.KVStoreService, ak types.AccountKeeper, blockedAddr map[string]bool, authority string, logger log.Logger, ) BaseKeeper { - keeper := bankkeeper.NewBaseKeeper(cdc, storeService, ak, blockedAddr, authority, logger) - baseKeeper := BaseKeeper{ - BaseKeeper: keeper, - ak: ak, - cdc: cdc, - storeService: storeService, - inactiveAddrs: map[string]bool{}, - addrCdc: cdc.InterfaceRegistry().SigningContext().AddressCodec(), + return BaseKeeper{ + BaseKeeper: bankkeeper.NewBaseKeeper(cdc, storeService, ak, blockedAddr, authority, logger), + storeService: storeService, } - - return baseKeeper } diff --git a/x/bankplus/keeper/migrations.go b/x/bankplus/keeper/migrations.go deleted file mode 100644 index 088f02a02a..0000000000 --- a/x/bankplus/keeper/migrations.go +++ /dev/null @@ -1,27 +0,0 @@ -package keeper - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - bankv2 "github.com/cosmos/cosmos-sdk/x/bank/migrations/v2" - bankv3 "github.com/cosmos/cosmos-sdk/x/bank/migrations/v3" -) - -type Migrator struct { - keeper BaseKeeper -} - -func NewMigrator(keeper BaseKeeper) *Migrator { - return &Migrator{keeper: keeper} -} - -func (m Migrator) WrappedMigrateBankplusWithBankMigrate1to2n3(ctx sdk.Context) error { - if err := DeprecateBankPlus(ctx, m.keeper.storeService); err != nil { - return err - } - - if err := bankv2.MigrateStore(ctx, m.keeper.storeService, m.keeper.cdc); err != nil { - return err - } - - return bankv3.MigrateStore(ctx, m.keeper.storeService, m.keeper.cdc) -} diff --git a/x/bankplus/migrations/v1/types.go b/x/bankplus/migrations/v1/types.go deleted file mode 100644 index 4cd285e700..0000000000 --- a/x/bankplus/migrations/v1/types.go +++ /dev/null @@ -1,11 +0,0 @@ -package v1 - -import sdk "github.com/cosmos/cosmos-sdk/types" - -// InactiveAddrsKeyPrefix Keys for bankplus store but this prefix must not be overlap with bank key prefix. -var InactiveAddrsKeyPrefix = []byte{0xa0} - -// InactiveAddrKey key of a specific inactiveAddr from store -func InactiveAddrKey(addr sdk.AccAddress) []byte { - return append(InactiveAddrsKeyPrefix, addr.Bytes()...) -} diff --git a/x/bankplus/module/module.go b/x/bankplus/module/module.go index e04cbf393d..d973a0046f 100644 --- a/x/bankplus/module/module.go +++ b/x/bankplus/module/module.go @@ -43,13 +43,12 @@ type AppModule struct { bankAppModule bank.AppModule bankKeeper bankkeeper.Keeper - accountKeeper banktypes.AccountKeeper legacySubspace exported.Subspace bankplusKeeper keeper.BaseKeeper } -func NewAppModule(cdc codec.Codec, keeper bankkeeper.Keeper, accKeeper banktypes.AccountKeeper, ss exported.Subspace, bankplus keeper.BaseKeeper) AppModule { +func NewAppModule(cdc codec.Codec, keeper bankkeeper.Keeper, accKeeper banktypes.AccountKeeper, ss exported.Subspace, bankplusKeeper keeper.BaseKeeper) AppModule { appModule := bank.NewAppModule(cdc, keeper, accKeeper, ss) return AppModule{ AppModuleBasic: AppModuleBasic{ @@ -57,9 +56,8 @@ func NewAppModule(cdc codec.Codec, keeper bankkeeper.Keeper, accKeeper banktypes }, bankAppModule: appModule, bankKeeper: keeper, - accountKeeper: accKeeper, legacySubspace: ss, - bankplusKeeper: bankplus, + bankplusKeeper: bankplusKeeper, } } @@ -76,13 +74,24 @@ func (a AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { func (a AppModule) RegisterServices(cfg module.Configurator) { banktypes.RegisterMsgServer(cfg.MsgServer(), bankkeeper.NewMsgServerImpl(a.bankKeeper)) banktypes.RegisterQueryServer(cfg.QueryServer(), a.bankKeeper) - bkplusMigrator := keeper.NewMigrator(a.bankplusKeeper) - if err := cfg.RegisterMigration(banktypes.ModuleName, 2, bkplusMigrator.WrappedMigrateBankplusWithBankMigrate1to2n3); err != nil { - panic(fmt.Sprintf("failed to migrate x/bank from version 1 to 2,3(including original bank1 to 2,3: %v", err)) - } m := bankkeeper.NewMigrator(a.bankKeeper.(bankkeeper.BaseKeeper), a.legacySubspace) - if err := cfg.RegisterMigration(banktypes.ModuleName, 3, m.Migrate3to4); err != nil { + if err := cfg.RegisterMigration(banktypes.ModuleName, 1, m.Migrate1to2); err != nil { + panic(fmt.Sprintf("failed to migrate x/bank from version 1 to 2: %v", err)) + } + + if err := cfg.RegisterMigration(banktypes.ModuleName, 2, m.Migrate2to3); err != nil { + panic(fmt.Sprintf("failed to migrate x/bank from version 2 to 3: %v", err)) + } + + if err := cfg.RegisterMigration(banktypes.ModuleName, 3, + func(ctx sdk.Context) error { + // This is only necessary for a legacy bankplus module of finschia + if err := keeper.DeprecateBankPlus(ctx, a.bankplusKeeper); err != nil { + return fmt.Errorf("bankplus migration logic for deprecation failed: %w", err) + } + return m.Migrate3to4(ctx) + }); err != nil { panic(fmt.Sprintf("failed to migrate x/bank from version 3 to 4: %v", err)) } } @@ -158,7 +167,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { panic(err) } - bankKeeper := keeper.NewBaseKeeper( + bankplusKeeper := keeper.NewBaseKeeper( in.Cdc, in.StoreService, in.AccountKeeper, @@ -168,10 +177,10 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { ) originalBankKeeper := bankkeeper.NewBaseKeeper(in.Cdc, in.StoreService, in.AccountKeeper, blockedAddresses, authorityString, in.Logger) - m := NewAppModule(in.Cdc, originalBankKeeper, in.AccountKeeper, in.LegacySubspace, bankKeeper) + m := NewAppModule(in.Cdc, originalBankKeeper, in.AccountKeeper, in.LegacySubspace, bankplusKeeper) return ModuleOutputs{ - BankKeeper: bankKeeper, + BankKeeper: bankplusKeeper, Module: m, } }