Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(x/bank,x/distr,x/feegrant)!: reduce auth keeper dependency #21651

Merged
merged 13 commits into from
Oct 9, 2024
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func NewSimApp(
appCodec, legacyAmino, app.StakingKeeper, govModuleAddr,
)

app.FeeGrantKeeper = feegrantkeeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(keys[feegrant.StoreKey]), logger.With(log.ModuleKey, "x/feegrant")), appCodec, app.AuthKeeper)
app.FeeGrantKeeper = feegrantkeeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(keys[feegrant.StoreKey]), logger.With(log.ModuleKey, "x/feegrant")), appCodec, app.AuthKeeper.AddressCodec())

// register the staking hooks
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
Expand Down
14 changes: 8 additions & 6 deletions x/bank/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sort"

modulev1 "cosmossdk.io/api/cosmos/bank/module/v1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"
"cosmossdk.io/depinject/appconfig"
Expand All @@ -33,9 +34,10 @@ func init() {
type ModuleInputs struct {
depinject.In

Config *modulev1.Module
Cdc codec.Codec
Environment appmodule.Environment
Config *modulev1.Module
Cdc codec.Codec
Environment appmodule.Environment
AddressCodec address.Codec

AccountKeeper types.AccountKeeper
}
Expand All @@ -55,15 +57,15 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
blockedAddresses := make(map[string]bool)
if len(in.Config.BlockedModuleAccountsOverride) > 0 {
for _, moduleName := range in.Config.BlockedModuleAccountsOverride {
addrStr, err := in.AccountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress(moduleName))
addrStr, err := in.AddressCodec.BytesToString(authtypes.NewModuleAddress(moduleName))
if err != nil {
panic(err)
}
blockedAddresses[addrStr] = true
}
} else {
for _, permission := range in.AccountKeeper.GetModulePermissions() {
addrStr, err := in.AccountKeeper.AddressCodec().BytesToString(permission.GetAddress())
addrStr, err := in.AddressCodec.BytesToString(permission.GetAddress())
if err != nil {
panic(err)
}
Expand All @@ -77,7 +79,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority)
}

authStr, err := in.AccountKeeper.AddressCodec().BytesToString(authority)
authStr, err := in.AddressCodec.BytesToString(authority)
if err != nil {
panic(err)
}
Expand Down
5 changes: 2 additions & 3 deletions x/bank/keeper/collections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
banktestutil "cosmossdk.io/x/bank/testutil"
banktypes "cosmossdk.io/x/bank/types"

"github.com/cosmos/cosmos-sdk/codec/address"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
Expand All @@ -32,13 +31,13 @@ func TestBankStateCompatibility(t *testing.T) {
encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{})

env := runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger())
ac := codectestutil.CodecOptions{}.GetAddressCodec()

// gomock initializations
ctrl := gomock.NewController(t)
authKeeper := banktestutil.NewMockAccountKeeper(ctrl)
authKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()
authKeeper.EXPECT().AddressCodec().Return(ac).AnyTimes()

ac := codectestutil.CodecOptions{}.GetAddressCodec()
addr, err := ac.BytesToString(accAddrs[4])
require.NoError(t, err)
authority, err := ac.BytesToString(authtypes.NewModuleAddress(banktypes.GovModuleName))
Expand Down
4 changes: 2 additions & 2 deletions x/bank/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func (k BaseKeeper) InitGenesis(ctx context.Context, genState *types.GenesisStat
}
totalSupplyMap := sdk.NewMapCoins(sdk.Coins{})

genState.Balances, err = types.SanitizeGenesisBalances(genState.Balances, k.ak.AddressCodec())
genState.Balances, err = types.SanitizeGenesisBalances(genState.Balances, k.addrCdc)
if err != nil {
return err
}

for _, balance := range genState.Balances {
addr := balance.GetAddress()
bz, err := k.ak.AddressCodec().StringToBytes(addr)
bz, err := k.addrCdc.StringToBytes(addr)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion x/bank/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (suite *KeeperTestSuite) TestExportGenesis() {

for i := range []int{1, 2} {
suite.bankKeeper.SetDenomMetaData(ctx, expectedMetadata[i])
accAddr, err1 := suite.authKeeper.AddressCodec().StringToBytes(expectedBalances[i].Address)
accAddr, err1 := suite.addrCdc.StringToBytes(expectedBalances[i].Address)
if err1 != nil {
panic(err1)
}
Expand Down
10 changes: 5 additions & 5 deletions x/bank/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (k BaseKeeper) Balance(ctx context.Context, req *types.QueryBalanceRequest)
return nil, status.Error(codes.InvalidArgument, err.Error())
}

address, err := k.ak.AddressCodec().StringToBytes(req.Address)
address, err := k.addrCdc.StringToBytes(req.Address)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid address: %s", err.Error())
}
Expand All @@ -53,7 +53,7 @@ func (k BaseKeeper) AllBalances(ctx context.Context, req *types.QueryAllBalances
return nil, status.Error(codes.InvalidArgument, "empty request")
}

addr, err := k.ak.AddressCodec().StringToBytes(req.Address)
addr, err := k.addrCdc.StringToBytes(req.Address)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid address: %s", err.Error())
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func (k BaseKeeper) SpendableBalances(ctx context.Context, req *types.QuerySpend
return nil, status.Error(codes.InvalidArgument, "empty request")
}

addr, err := k.ak.AddressCodec().StringToBytes(req.Address)
addr, err := k.addrCdc.StringToBytes(req.Address)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid address: %s", err.Error())
}
Expand Down Expand Up @@ -120,7 +120,7 @@ func (k BaseKeeper) SpendableBalanceByDenom(ctx context.Context, req *types.Quer
return nil, status.Error(codes.InvalidArgument, "empty request")
}

addr, err := k.ak.AddressCodec().StringToBytes(req.Address)
addr, err := k.addrCdc.StringToBytes(req.Address)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid address: %s", err.Error())
}
Expand Down Expand Up @@ -295,7 +295,7 @@ func (k BaseKeeper) DenomOwners(
if err != nil {
return nil, err
}
addr, err := k.ak.AddressCodec().BytesToString(key.K2())
addr, err := k.addrCdc.BytesToString(key.K2())
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions x/bank/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (suite *KeeperTestSuite) TestQueryBalance() {
ctx, queryClient := suite.ctx, suite.queryClient
_, _, addr := testdata.KeyTestPubAddr()

addrStr, err := suite.authKeeper.AddressCodec().BytesToString(addr)
addrStr, err := suite.addrCdc.BytesToString(addr)
suite.Require().NoError(err)

origCoins := sdk.NewCoins(newBarCoin(30))
Expand Down Expand Up @@ -105,7 +105,7 @@ func (suite *KeeperTestSuite) TestQueryAllBalances() {
_, err := queryClient.AllBalances(gocontext.Background(), &types.QueryAllBalancesRequest{})
suite.Require().Error(err)

addrStr, err := suite.authKeeper.AddressCodec().BytesToString(addr)
addrStr, err := suite.addrCdc.BytesToString(addr)
suite.Require().NoError(err)

pageReq := &query.PageRequest{
Expand Down Expand Up @@ -178,7 +178,7 @@ func (suite *KeeperTestSuite) TestQueryAllBalances() {

func (suite *KeeperTestSuite) TestSpendableBalances() {
_, _, addr := testdata.KeyTestPubAddr()
addrStr, err := suite.authKeeper.AddressCodec().BytesToString(addr)
addrStr, err := suite.addrCdc.BytesToString(addr)
suite.Require().NoError(err)

ctx := sdk.UnwrapSDKContext(suite.ctx)
Expand Down Expand Up @@ -241,7 +241,7 @@ func (suite *KeeperTestSuite) TestSpendableBalanceByDenom() {
_, err := queryClient.SpendableBalanceByDenom(ctx, &types.QuerySpendableBalanceByDenomRequest{})
suite.Require().Error(err)

addrStr, err := suite.authKeeper.AddressCodec().BytesToString(addr)
addrStr, err := suite.addrCdc.BytesToString(addr)
suite.Require().NoError(err)

req := types.NewQuerySpendableBalanceByDenomRequest(addrStr, fooDenom)
Expand Down
12 changes: 8 additions & 4 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/event"
errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -60,6 +61,7 @@ type BaseKeeper struct {
ak types.AccountKeeper
cdc codec.BinaryCodec
mintCoinsRestrictionFn types.MintingRestrictionFn
addrCdc address.Codec
}

// GetPaginatedTotalSupply queries for the supply, ignoring 0 coins, with a given pagination
Expand Down Expand Up @@ -87,7 +89,8 @@ func NewBaseKeeper(
blockedAddrs map[string]bool,
authority string,
) BaseKeeper {
if _, err := ak.AddressCodec().StringToBytes(authority); err != nil {
addrCdc := ak.AddressCodec()
if _, err := addrCdc.StringToBytes(authority); err != nil {
panic(fmt.Errorf("invalid bank authority address: %w", err))
}

Expand All @@ -97,6 +100,7 @@ func NewBaseKeeper(
ak: ak,
cdc: cdc,
mintCoinsRestrictionFn: types.NoOpMintingRestrictionFn,
addrCdc: addrCdc,
}
}

Expand Down Expand Up @@ -146,7 +150,7 @@ func (k BaseKeeper) DelegateCoins(ctx context.Context, delegatorAddr, moduleAccA
return errorsmod.Wrap(err, "failed to track delegation")
}
// emit coin spent event
delAddrStr, err := k.ak.AddressCodec().BytesToString(delegatorAddr)
delAddrStr, err := k.addrCdc.BytesToString(delegatorAddr)
if err != nil {
return err
}
Expand Down Expand Up @@ -362,7 +366,7 @@ func (k BaseKeeper) MintCoins(ctx context.Context, moduleName string, amounts sd

k.Logger.Debug("minted coins from module account", "amount", amounts.String(), "from", moduleName)

addrStr, err := k.ak.AddressCodec().BytesToString(acc.GetAddress())
addrStr, err := k.addrCdc.BytesToString(acc.GetAddress())
if err != nil {
return err
}
Expand Down Expand Up @@ -403,7 +407,7 @@ func (k BaseKeeper) BurnCoins(ctx context.Context, address []byte, amounts sdk.C
k.setSupply(ctx, supply)
}

addrStr, err := k.ak.AddressCodec().BytesToString(acc.GetAddress())
addrStr, err := k.addrCdc.BytesToString(acc.GetAddress())
if err != nil {
return err
}
Expand Down
42 changes: 22 additions & 20 deletions x/bank/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"

"cosmossdk.io/core/address"
coreevent "cosmossdk.io/core/event"
"cosmossdk.io/core/header"
coretesting "cosmossdk.io/core/testing"
Expand All @@ -24,7 +25,6 @@ import (
banktypes "cosmossdk.io/x/bank/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec/address"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
Expand Down Expand Up @@ -114,6 +114,7 @@ type KeeperTestSuite struct {

ctx context.Context
bankKeeper keeper.BaseKeeper
addrCdc address.Codec
authKeeper *banktestutil.MockAccountKeeper

queryClient banktypes.QueryClient
Expand Down Expand Up @@ -143,9 +144,10 @@ func (suite *KeeperTestSuite) SetupTest() {
// gomock initializations
ctrl := gomock.NewController(suite.T())
authKeeper := banktestutil.NewMockAccountKeeper(ctrl)
authKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()
authKeeper.EXPECT().AddressCodec().Return(ac).AnyTimes()
suite.ctx = ctx
suite.authKeeper = authKeeper
suite.addrCdc = ac
suite.bankKeeper = keeper.NewBaseKeeper(
env,
encCfg.Codec,
Expand Down Expand Up @@ -320,9 +322,9 @@ func (suite *KeeperTestSuite) TestGetAuthority() {
authority,
)
}
govAddr, err := suite.authKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress(banktypes.GovModuleName))
govAddr, err := suite.addrCdc.BytesToString(authtypes.NewModuleAddress(banktypes.GovModuleName))
suite.Require().NoError(err)
modAddr, err := suite.authKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress(banktypes.MintModuleName))
modAddr, err := suite.addrCdc.BytesToString(authtypes.NewModuleAddress(banktypes.MintModuleName))
suite.Require().NoError(err)

tests := map[string]string{
Expand Down Expand Up @@ -647,9 +649,9 @@ func (suite *KeeperTestSuite) TestInputOutputNewAccount() {

require.Empty(suite.bankKeeper.GetAllBalances(ctx, accAddrs[1]))

acc0StrAddr, err := suite.authKeeper.AddressCodec().BytesToString(accAddrs[0])
acc0StrAddr, err := suite.addrCdc.BytesToString(accAddrs[0])
suite.Require().NoError(err)
acc1StrAddr, err := suite.authKeeper.AddressCodec().BytesToString(accAddrs[1])
acc1StrAddr, err := suite.addrCdc.BytesToString(accAddrs[1])
suite.Require().NoError(err)

suite.mockInputOutputCoins([]sdk.AccountI{authtypes.NewBaseAccountWithAddress(accAddrs[0])}, []sdk.AccAddress{accAddrs[1]})
Expand All @@ -674,11 +676,11 @@ func (suite *KeeperTestSuite) TestInputOutputCoins() {

acc0 := authtypes.NewBaseAccountWithAddress(accAddrs[0])

acc0StrAddr, err := suite.authKeeper.AddressCodec().BytesToString(accAddrs[0])
acc0StrAddr, err := suite.addrCdc.BytesToString(accAddrs[0])
suite.Require().NoError(err)
acc1StrAddr, err := suite.authKeeper.AddressCodec().BytesToString(accAddrs[1])
acc1StrAddr, err := suite.addrCdc.BytesToString(accAddrs[1])
suite.Require().NoError(err)
acc2StrAddr, err := suite.authKeeper.AddressCodec().BytesToString(accAddrs[2])
acc2StrAddr, err := suite.addrCdc.BytesToString(accAddrs[2])
suite.Require().NoError(err)

input := banktypes.Input{
Expand Down Expand Up @@ -786,16 +788,16 @@ func (suite *KeeperTestSuite) TestInputOutputCoinsWithRestrictions() {
setupCtx := suite.ctx
balances := sdk.NewCoins(newFooCoin(1000), newBarCoin(500))
fromAddr := accAddrs[0]
fromStrAddr, err := suite.authKeeper.AddressCodec().BytesToString(fromAddr)
fromStrAddr, err := suite.addrCdc.BytesToString(fromAddr)
suite.Require().NoError(err)
fromAcc := authtypes.NewBaseAccountWithAddress(fromAddr)
inputAccs := []sdk.AccountI{fromAcc}
suite.authKeeper.EXPECT().GetAccount(suite.ctx, inputAccs[0].GetAddress()).Return(inputAccs[0]).AnyTimes()
toAddr1 := accAddrs[1]
toAddr1Str, err := suite.authKeeper.AddressCodec().BytesToString(toAddr1)
toAddr1Str, err := suite.addrCdc.BytesToString(toAddr1)
suite.Require().NoError(err)
toAddr2 := accAddrs[2]
toAddr2Str, err := suite.authKeeper.AddressCodec().BytesToString(toAddr2)
toAddr2Str, err := suite.addrCdc.BytesToString(toAddr2)
suite.Require().NoError(err)

suite.mockFundAccount(accAddrs[0])
Expand Down Expand Up @@ -1365,9 +1367,9 @@ func (suite *KeeperTestSuite) TestMsgSendEvents() {

acc0 := authtypes.NewBaseAccountWithAddress(accAddrs[0])

acc0StrAddr, err := suite.authKeeper.AddressCodec().BytesToString(accAddrs[0])
acc0StrAddr, err := suite.addrCdc.BytesToString(accAddrs[0])
suite.Require().NoError(err)
acc1StrAddr, err := suite.authKeeper.AddressCodec().BytesToString(accAddrs[1])
acc1StrAddr, err := suite.addrCdc.BytesToString(accAddrs[1])
suite.Require().NoError(err)

newCoins := sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50))
Expand Down Expand Up @@ -1407,11 +1409,11 @@ func (suite *KeeperTestSuite) TestMsgMultiSendEvents() {

require.NoError(suite.bankKeeper.SetParams(ctx, banktypes.DefaultParams()))

acc0StrAddr, err := suite.authKeeper.AddressCodec().BytesToString(accAddrs[0])
acc0StrAddr, err := suite.addrCdc.BytesToString(accAddrs[0])
suite.Require().NoError(err)
acc2StrAddr, err := suite.authKeeper.AddressCodec().BytesToString(accAddrs[2])
acc2StrAddr, err := suite.addrCdc.BytesToString(accAddrs[2])
suite.Require().NoError(err)
acc3StrAddr, err := suite.authKeeper.AddressCodec().BytesToString(accAddrs[3])
acc3StrAddr, err := suite.addrCdc.BytesToString(accAddrs[3])
suite.Require().NoError(err)

coins := sdk.NewCoins(sdk.NewInt64Coin(fooDenom, 50), sdk.NewInt64Coin(barDenom, 100))
Expand Down Expand Up @@ -1932,15 +1934,15 @@ func (suite *KeeperTestSuite) TestBalanceTrackingEvents() {
case banktypes.EventTypeCoinSpent:
coinsSpent, err := sdk.ParseCoinsNormalized(e.Attributes[1].Value)
require.NoError(err)
_, err = suite.authKeeper.AddressCodec().StringToBytes(e.Attributes[0].Value)
_, err = suite.addrCdc.StringToBytes(e.Attributes[0].Value)
require.NoError(err)

balances[e.Attributes[0].Value] = balances[e.Attributes[0].Value].Sub(coinsSpent...)

case banktypes.EventTypeCoinReceived:
coinsRecv, err := sdk.ParseCoinsNormalized(e.Attributes[1].Value)
require.NoError(err)
_, err = suite.authKeeper.AddressCodec().StringToBytes(e.Attributes[0].Value)
_, err = suite.addrCdc.StringToBytes(e.Attributes[0].Value)
require.NoError(err)
balances[e.Attributes[0].Value] = balances[e.Attributes[0].Value].Add(coinsRecv...)
}
Expand All @@ -1958,7 +1960,7 @@ func (suite *KeeperTestSuite) TestBalanceTrackingEvents() {
return false
}

addr, err := suite.authKeeper.AddressCodec().BytesToString(address)
addr, err := suite.addrCdc.BytesToString(address)
suite.Require().NoError(err)

balance, exists := balances[addr]
Expand Down
Loading
Loading