Skip to content

Commit

Permalink
chore: remove code that use global codec
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeseung-bae committed Feb 6, 2024
1 parent 12fd57e commit d050b22
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 23 deletions.
10 changes: 7 additions & 3 deletions x/bankplus/keeper/inactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ func (keeper BaseKeeper) isStoredInactiveAddr(ctx context.Context, address sdk.A
// addToInactiveAddr adds a blocked address to the store.
func (keeper BaseKeeper) addToInactiveAddr(ctx context.Context, address sdk.AccAddress) {
store := keeper.storeService.OpenKVStore(ctx)
blockedCAddr := types.InactiveAddr{Address: address.String()}
bz := keeper.cdc.MustMarshal(&blockedCAddr)
err := store.Set(inactiveAddrKey(address), bz)
addrString, err := keeper.addrCdc.BytesToString(address)
if err != nil {
panic(err)
}

blockedCAddr := types.InactiveAddr{Address: addrString}
bz := keeper.cdc.MustMarshal(&blockedCAddr)
if err := store.Set(inactiveAddrKey(address), bz); err != nil {
panic(err)
}
}

// deleteFromInactiveAddr deletes blocked address from store
Expand Down
47 changes: 33 additions & 14 deletions x/bankplus/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"

"cosmossdk.io/core/address"
"cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
Expand All @@ -29,10 +30,11 @@ type Keeper interface {
type BaseKeeper struct {
bankkeeper.BaseKeeper

ak types.AccountKeeper
cdc codec.Codec
storeService store.KVStoreService
ak types.AccountKeeper
cdc codec.Codec
addrCdc address.Codec

storeService store.KVStoreService
inactiveAddrs map[string]bool
deactMultiSend bool
}
Expand All @@ -48,6 +50,7 @@ func NewBaseKeeper(
storeService: storeService,
inactiveAddrs: map[string]bool{},
deactMultiSend: deactMultiSend,
addrCdc: cdc.InterfaceRegistry().SigningContext().AddressCodec(),
}
}

Expand Down Expand Up @@ -104,7 +107,11 @@ func (keeper BaseKeeper) SendCoinsFromAccountToModule(
}

func (keeper BaseKeeper) isInactiveAddr(addr sdk.AccAddress) bool {
return keeper.inactiveAddrs[addr.String()]
addrString, err := keeper.addrCdc.BytesToString(addr)
if err != nil {
panic(err)
}
return keeper.inactiveAddrs[addrString]
}

// SendCoins transfers amt coins from a sending account to a receiving account.
Expand All @@ -120,26 +127,38 @@ func (keeper BaseKeeper) SendCoins(ctx context.Context, fromAddr, toAddr sdk.Acc
}

// AddToInactiveAddr adds the address to `inactiveAddr`.
func (keeper BaseKeeper) AddToInactiveAddr(ctx context.Context, address sdk.AccAddress) {
if !keeper.inactiveAddrs[address.String()] {
keeper.inactiveAddrs[address.String()] = true
func (keeper BaseKeeper) AddToInactiveAddr(ctx context.Context, addr sdk.AccAddress) {
addrString, err := keeper.addrCdc.BytesToString(addr)
if err != nil {
panic(err)
}
if !keeper.inactiveAddrs[addrString] {
keeper.inactiveAddrs[addrString] = true

keeper.addToInactiveAddr(ctx, address)
keeper.addToInactiveAddr(ctx, addr)
}
}

// DeleteFromInactiveAddr removes the address from `inactiveAddr`.
func (keeper BaseKeeper) DeleteFromInactiveAddr(ctx context.Context, address sdk.AccAddress) {
if keeper.inactiveAddrs[address.String()] {
delete(keeper.inactiveAddrs, address.String())
func (keeper BaseKeeper) DeleteFromInactiveAddr(ctx context.Context, addr sdk.AccAddress) {
addrString, err := keeper.addrCdc.BytesToString(addr)
if err != nil {
panic(err)
}
if keeper.inactiveAddrs[addrString] {
delete(keeper.inactiveAddrs, addrString)

keeper.deleteFromInactiveAddr(ctx, address)
keeper.deleteFromInactiveAddr(ctx, addr)
}
}

// IsInactiveAddr returns if the address is added in inactiveAddr.
func (keeper BaseKeeper) IsInactiveAddr(address sdk.AccAddress) bool {
return keeper.inactiveAddrs[address.String()]
func (keeper BaseKeeper) IsInactiveAddr(addr sdk.AccAddress) bool {
addrString, err := keeper.addrCdc.BytesToString(addr)
if err != nil {
panic(err)
}
return keeper.inactiveAddrs[addrString]
}

func (keeper BaseKeeper) InputOutputCoins(ctx context.Context, input types.Input, outputs []types.Output) error {
Expand Down
33 changes: 27 additions & 6 deletions x/bankplus/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,35 @@ type ModuleInputs struct {
LegacySubspace exported.Subspace `optional:"true"`
}

func ProvideModule(in ModuleInputs) bank.ModuleOutputs {
type ModuleOutputs struct {
depinject.Out

BankKeeper keeper.BaseKeeper
Module appmodule.AppModule
}

func ProvideModule(in ModuleInputs) ModuleOutputs {
// Configure blocked module accounts.
//
// Default behavior for blockedAddresses is to regard any module mentioned in
// AccountKeeper's module account permissions as blocked.
blockedAddresses := make(map[string]bool)
addrCodec := in.Cdc.InterfaceRegistry().SigningContext().AddressCodec()
if len(in.Config.BlockedModuleAccountsOverride) > 0 {
for _, moduleName := range in.Config.BlockedModuleAccountsOverride {
blockedAddresses[authtypes.NewModuleAddress(moduleName).String()] = true
moduleAddrString, err := addrCodec.BytesToString(authtypes.NewModuleAddress(moduleName))
if err != nil {
panic(err)
}
blockedAddresses[moduleAddrString] = true
}
} else {
for _, permission := range in.AccountKeeper.GetModulePermissions() {
blockedAddresses[permission.GetAddress().String()] = true
permAddr, err := addrCodec.BytesToString(permission.GetAddress())
if err != nil {
panic(err)
}
blockedAddresses[permAddr] = true
}
}

Expand All @@ -143,19 +159,24 @@ func ProvideModule(in ModuleInputs) bank.ModuleOutputs {
if in.Config.Authority != "" {
authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority)
}
authorityString, err := addrCodec.BytesToString(authority)
if err != nil {
panic(err)
}

bankKeeper := keeper.NewBaseKeeper(
in.Cdc,
in.StoreService,
in.AccountKeeper,
blockedAddresses,
true, // in.DeactMultiSend, // FIXME: inject properly
authority.String(),
authorityString,
in.Logger,
)

m := NewAppModule(in.Cdc, bankKeeper, in.AccountKeeper, in.LegacySubspace)
return bank.ModuleOutputs{
BankKeeper: bankKeeper.BaseKeeper,
return ModuleOutputs{
BankKeeper: bankKeeper,
Module: m,
}
}

0 comments on commit d050b22

Please sign in to comment.