Skip to content

Commit

Permalink
chore: clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeseung-bae committed Mar 13, 2024
1 parent b2f0ef6 commit e1ba3f4
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 78 deletions.
18 changes: 12 additions & 6 deletions x/bankplus/keeper/deprecator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
7 changes: 3 additions & 4 deletions x/bankplus/keeper/deprecator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()))
Expand All @@ -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
}

Expand All @@ -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)
}
}
22 changes: 4 additions & 18 deletions x/bankplus/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper

import (
"cosmossdk.io/core/address"
"cosmossdk.io/core/store"
"cosmossdk.io/log"

Expand All @@ -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
}
27 changes: 0 additions & 27 deletions x/bankplus/keeper/migrations.go

This file was deleted.

11 changes: 0 additions & 11 deletions x/bankplus/migrations/v1/types.go

This file was deleted.

33 changes: 21 additions & 12 deletions x/bankplus/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,21 @@ 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{
AppModuleBasic: appModule.AppModuleBasic,
},
bankAppModule: appModule,
bankKeeper: keeper,
accountKeeper: accKeeper,
legacySubspace: ss,
bankplusKeeper: bankplus,
bankplusKeeper: bankplusKeeper,
}
}

Expand All @@ -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))
}
}
Expand Down Expand Up @@ -158,7 +167,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
panic(err)
}

bankKeeper := keeper.NewBaseKeeper(
bankplusKeeper := keeper.NewBaseKeeper(
in.Cdc,
in.StoreService,
in.AccountKeeper,
Expand All @@ -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,
}
}

0 comments on commit e1ba3f4

Please sign in to comment.