diff --git a/simapp/app.go b/simapp/app.go index 96ae2f04dbae..3a322827185e 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -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 diff --git a/x/bank/depinject.go b/x/bank/depinject.go index b17a0cbf7557..22ec40422bcc 100644 --- a/x/bank/depinject.go +++ b/x/bank/depinject.go @@ -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" @@ -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 } @@ -55,7 +57,7 @@ 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) } @@ -63,7 +65,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { } } 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) } @@ -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) } diff --git a/x/bank/keeper/collections_test.go b/x/bank/keeper/collections_test.go index 3e6d470b380a..f18fe08f434a 100644 --- a/x/bank/keeper/collections_test.go +++ b/x/bank/keeper/collections_test.go @@ -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" @@ -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)) diff --git a/x/bank/keeper/genesis.go b/x/bank/keeper/genesis.go index 8e422e5f420c..2f6facc9ab80 100644 --- a/x/bank/keeper/genesis.go +++ b/x/bank/keeper/genesis.go @@ -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 } diff --git a/x/bank/keeper/genesis_test.go b/x/bank/keeper/genesis_test.go index 42f6221092cf..c8c7b58c2c81 100644 --- a/x/bank/keeper/genesis_test.go +++ b/x/bank/keeper/genesis_test.go @@ -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) } diff --git a/x/bank/keeper/grpc_query.go b/x/bank/keeper/grpc_query.go index c6d499143d83..909efe3a9d38 100644 --- a/x/bank/keeper/grpc_query.go +++ b/x/bank/keeper/grpc_query.go @@ -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()) } @@ -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()) } @@ -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()) } @@ -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()) } @@ -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 } diff --git a/x/bank/keeper/grpc_query_test.go b/x/bank/keeper/grpc_query_test.go index 6de9321d404c..4878bf24ce50 100644 --- a/x/bank/keeper/grpc_query_test.go +++ b/x/bank/keeper/grpc_query_test.go @@ -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)) @@ -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{ @@ -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) @@ -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) diff --git a/x/bank/keeper/keeper.go b/x/bank/keeper/keeper.go index 9b1d79c1091b..f8e8aab22974 100644 --- a/x/bank/keeper/keeper.go +++ b/x/bank/keeper/keeper.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" "cosmossdk.io/core/event" errorsmod "cosmossdk.io/errors" @@ -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 @@ -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)) } @@ -97,6 +100,7 @@ func NewBaseKeeper( ak: ak, cdc: cdc, mintCoinsRestrictionFn: types.NoOpMintingRestrictionFn, + addrCdc: addrCdc, } } @@ -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 } @@ -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 } @@ -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 } diff --git a/x/bank/keeper/keeper_test.go b/x/bank/keeper/keeper_test.go index 4fe765039638..1e984109008b 100644 --- a/x/bank/keeper/keeper_test.go +++ b/x/bank/keeper/keeper_test.go @@ -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" @@ -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" @@ -114,6 +114,7 @@ type KeeperTestSuite struct { ctx context.Context bankKeeper keeper.BaseKeeper + addrCdc address.Codec authKeeper *banktestutil.MockAccountKeeper queryClient banktypes.QueryClient @@ -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, @@ -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{ @@ -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]}) @@ -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{ @@ -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]) @@ -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)) @@ -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)) @@ -1932,7 +1934,7 @@ 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...) @@ -1940,7 +1942,7 @@ func (suite *KeeperTestSuite) TestBalanceTrackingEvents() { 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...) } @@ -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] diff --git a/x/bank/keeper/msg_server.go b/x/bank/keeper/msg_server.go index 81d4d2321476..893191ce4805 100644 --- a/x/bank/keeper/msg_server.go +++ b/x/bank/keeper/msg_server.go @@ -29,11 +29,11 @@ func (k msgServer) Send(ctx context.Context, msg *types.MsgSend) (*types.MsgSend ) if base, ok := k.Keeper.(BaseKeeper); ok { - from, err = base.ak.AddressCodec().StringToBytes(msg.FromAddress) + from, err = base.addrCdc.StringToBytes(msg.FromAddress) if err != nil { return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid from address: %s", err) } - to, err = base.ak.AddressCodec().StringToBytes(msg.ToAddress) + to, err = base.addrCdc.StringToBytes(msg.ToAddress) if err != nil { return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid to address: %s", err) } @@ -91,7 +91,7 @@ func (k msgServer) MultiSend(ctx context.Context, msg *types.MsgMultiSend) (*typ for _, out := range msg.Outputs { if base, ok := k.Keeper.(BaseKeeper); ok { - accAddr, err := base.ak.AddressCodec().StringToBytes(out.Address) + accAddr, err := base.addrCdc.StringToBytes(out.Address) if err != nil { return nil, err } @@ -174,7 +174,7 @@ func (k msgServer) Burn(ctx context.Context, msg *types.MsgBurn) (*types.MsgBurn } if base, ok := k.Keeper.(BaseKeeper); ok { - from, err = base.ak.AddressCodec().StringToBytes(msg.FromAddress) + from, err = base.addrCdc.StringToBytes(msg.FromAddress) if err != nil { return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid from address: %s", err) } diff --git a/x/bank/keeper/msg_server_test.go b/x/bank/keeper/msg_server_test.go index c02f6943b90a..475116389593 100644 --- a/x/bank/keeper/msg_server_test.go +++ b/x/bank/keeper/msg_server_test.go @@ -71,9 +71,9 @@ func (suite *KeeperTestSuite) TestMsgSend() { atom0 := sdk.NewCoins(sdk.NewInt64Coin("atom", 0)) atom123eth0 := sdk.Coins{sdk.NewInt64Coin("atom", 123), sdk.NewInt64Coin("eth", 0)} - acc4Addr, err := suite.authKeeper.AddressCodec().BytesToString(accAddrs[4]) + acc4Addr, err := suite.addrCdc.BytesToString(accAddrs[4]) suite.Require().NoError(err) - minterAccAddr, err := suite.authKeeper.AddressCodec().BytesToString(minterAcc.GetAddress()) + minterAccAddr, err := suite.addrCdc.BytesToString(minterAcc.GetAddress()) suite.Require().NoError(err) testCases := []struct { @@ -168,13 +168,13 @@ func (suite *KeeperTestSuite) TestMsgMultiSend() { sendCoins := sdk.NewCoins(sdk.NewInt64Coin(origDenom, 50)) suite.bankKeeper.SetSendEnabled(suite.ctx, origDenom, true) - acc0Addr, err := suite.authKeeper.AddressCodec().BytesToString(accAddrs[0]) + acc0Addr, err := suite.addrCdc.BytesToString(accAddrs[0]) suite.Require().NoError(err) - acc1Addr, err := suite.authKeeper.AddressCodec().BytesToString(accAddrs[1]) + acc1Addr, err := suite.addrCdc.BytesToString(accAddrs[1]) suite.Require().NoError(err) - acc4Addr, err := suite.authKeeper.AddressCodec().BytesToString(accAddrs[4]) + acc4Addr, err := suite.addrCdc.BytesToString(accAddrs[4]) suite.Require().NoError(err) - minterAccAddr, err := suite.authKeeper.AddressCodec().BytesToString(minterAcc.GetAddress()) + minterAccAddr, err := suite.addrCdc.BytesToString(minterAcc.GetAddress()) suite.Require().NoError(err) testCases := []struct { @@ -269,7 +269,7 @@ func (suite *KeeperTestSuite) TestMsgMultiSend() { } func (suite *KeeperTestSuite) TestMsgSetSendEnabled() { - govAccAddr, err := suite.authKeeper.AddressCodec().BytesToString(govAcc.GetAddress()) + govAccAddr, err := suite.addrCdc.BytesToString(govAcc.GetAddress()) suite.Require().NoError(err) testCases := []struct { name string @@ -380,7 +380,7 @@ func (suite *KeeperTestSuite) TestMsgBurn() { origCoins := sdk.NewInt64Coin("eth", 100) atom0 := sdk.NewInt64Coin("atom", 0) - multiPermAccAddr, err := suite.authKeeper.AddressCodec().BytesToString(multiPermAcc.GetAddress()) + multiPermAccAddr, err := suite.addrCdc.BytesToString(multiPermAcc.GetAddress()) suite.Require().NoError(err) testCases := []struct { diff --git a/x/bank/keeper/send.go b/x/bank/keeper/send.go index 929ff2e50285..6afa29b9f21c 100644 --- a/x/bank/keeper/send.go +++ b/x/bank/keeper/send.go @@ -143,7 +143,7 @@ func (k BaseSendKeeper) InputOutputCoins(ctx context.Context, input types.Input, return err } - inAddress, err := k.ak.AddressCodec().StringToBytes(input.Address) + inAddress, err := k.addrCdc.StringToBytes(input.Address) if err != nil { return err } @@ -156,7 +156,7 @@ func (k BaseSendKeeper) InputOutputCoins(ctx context.Context, input types.Input, } sending := make([]toSend, 0) for _, out := range outputs { - outAddress, err := k.ak.AddressCodec().StringToBytes(out.Address) + outAddress, err := k.addrCdc.StringToBytes(out.Address) if err != nil { return err } @@ -218,11 +218,11 @@ func (k BaseSendKeeper) SendCoins(ctx context.Context, fromAddr, toAddr sdk.AccA return err } - fromAddrString, err := k.ak.AddressCodec().BytesToString(fromAddr) + fromAddrString, err := k.addrCdc.BytesToString(fromAddr) if err != nil { return err } - toAddrString, err := k.ak.AddressCodec().BytesToString(toAddr) + toAddrString, err := k.addrCdc.BytesToString(toAddr) if err != nil { return err } @@ -275,7 +275,7 @@ func (k BaseSendKeeper) subUnlockedCoins(ctx context.Context, addr sdk.AccAddres } } - addrStr, err := k.ak.AddressCodec().BytesToString(addr) + addrStr, err := k.addrCdc.BytesToString(addr) if err != nil { return err } @@ -303,7 +303,7 @@ func (k BaseSendKeeper) addCoins(ctx context.Context, addr sdk.AccAddress, amt s } } - addrStr, err := k.ak.AddressCodec().BytesToString(addr) + addrStr, err := k.addrCdc.BytesToString(addr) if err != nil { return err } @@ -359,7 +359,7 @@ func (k BaseSendKeeper) IsSendEnabledCoin(ctx context.Context, coin sdk.Coin) bo // BlockedAddr checks if a given address is restricted from // receiving funds. func (k BaseSendKeeper) BlockedAddr(addr sdk.AccAddress) bool { - addrStr, err := k.ak.AddressCodec().BytesToString(addr) + addrStr, err := k.addrCdc.BytesToString(addr) if err != nil { panic(err) } diff --git a/x/bank/keeper/view.go b/x/bank/keeper/view.go index 54236a60aa07..496e907de8ca 100644 --- a/x/bank/keeper/view.go +++ b/x/bank/keeper/view.go @@ -6,6 +6,7 @@ import ( "cosmossdk.io/collections" "cosmossdk.io/collections/indexes" + "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" errorsmod "cosmossdk.io/errors" "cosmossdk.io/math" @@ -57,8 +58,9 @@ func (b BalancesIndexes) IndexesList() []collections.Index[collections.Pair[sdk. type BaseViewKeeper struct { appmodule.Environment - cdc codec.BinaryCodec - ak types.AccountKeeper + cdc codec.BinaryCodec + ak types.AccountKeeper + addrCdc address.Codec Schema collections.Schema Supply collections.Map[string, math.Int] @@ -75,6 +77,7 @@ func NewBaseViewKeeper(env appmodule.Environment, cdc codec.BinaryCodec, ak type Environment: env, cdc: cdc, ak: ak, + addrCdc: ak.AddressCodec(), Supply: collections.NewMap(sb, types.SupplyKey, "supply", collections.StringKey, sdk.IntValue), DenomMetadata: collections.NewMap(sb, types.DenomMetadataPrefix, "denom_metadata", collections.StringKey, codec.CollValue[types.Metadata](cdc)), SendEnabled: collections.NewMap(sb, types.SendEnabledPrefix, "send_enabled", collections.StringKey, codec.BoolValue), // NOTE: we use a bool value which uses protobuf to retain state backwards compat @@ -112,7 +115,7 @@ func (k BaseViewKeeper) GetAccountsBalances(ctx context.Context) []types.Balance mapAddressToBalancesIdx := make(map[string]int) k.IterateAllBalances(ctx, func(addr sdk.AccAddress, balance sdk.Coin) bool { - addrStr, err := k.ak.AddressCodec().BytesToString(addr) + addrStr, err := k.addrCdc.BytesToString(addr) if err != nil { panic(err) } diff --git a/x/bank/testutil/expected_keepers_mocks.go b/x/bank/testutil/expected_keepers_mocks.go index aeddace241f3..4b420c3b5c4b 100644 --- a/x/bank/testutil/expected_keepers_mocks.go +++ b/x/bank/testutil/expected_keepers_mocks.go @@ -79,21 +79,6 @@ func (mr *MockAccountKeeperMockRecorder) GetModuleAccount(ctx, moduleName interf return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetModuleAccount", reflect.TypeOf((*MockAccountKeeper)(nil).GetModuleAccount), ctx, moduleName) } -// GetModuleAccountAndPermissions mocks base method. -func (m *MockAccountKeeper) GetModuleAccountAndPermissions(ctx context.Context, moduleName string) (types.ModuleAccountI, []string) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetModuleAccountAndPermissions", ctx, moduleName) - ret0, _ := ret[0].(types.ModuleAccountI) - ret1, _ := ret[1].([]string) - return ret0, ret1 -} - -// GetModuleAccountAndPermissions indicates an expected call of GetModuleAccountAndPermissions. -func (mr *MockAccountKeeperMockRecorder) GetModuleAccountAndPermissions(ctx, moduleName interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetModuleAccountAndPermissions", reflect.TypeOf((*MockAccountKeeper)(nil).GetModuleAccountAndPermissions), ctx, moduleName) -} - // GetModuleAddress mocks base method. func (m *MockAccountKeeper) GetModuleAddress(moduleName string) types.AccAddress { m.ctrl.T.Helper() @@ -108,21 +93,6 @@ func (mr *MockAccountKeeperMockRecorder) GetModuleAddress(moduleName interface{} return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetModuleAddress", reflect.TypeOf((*MockAccountKeeper)(nil).GetModuleAddress), moduleName) } -// GetModuleAddressAndPermissions mocks base method. -func (m *MockAccountKeeper) GetModuleAddressAndPermissions(moduleName string) (types.AccAddress, []string) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetModuleAddressAndPermissions", moduleName) - ret0, _ := ret[0].(types.AccAddress) - ret1, _ := ret[1].([]string) - return ret0, ret1 -} - -// GetModuleAddressAndPermissions indicates an expected call of GetModuleAddressAndPermissions. -func (mr *MockAccountKeeperMockRecorder) GetModuleAddressAndPermissions(moduleName interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetModuleAddressAndPermissions", reflect.TypeOf((*MockAccountKeeper)(nil).GetModuleAddressAndPermissions), moduleName) -} - // GetModulePermissions mocks base method. func (m *MockAccountKeeper) GetModulePermissions() map[string]types0.PermissionsForAddress { m.ctrl.T.Helper() @@ -151,34 +121,6 @@ func (mr *MockAccountKeeperMockRecorder) HasAccount(ctx, addr interface{}) *gomo return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HasAccount", reflect.TypeOf((*MockAccountKeeper)(nil).HasAccount), ctx, addr) } -// NewAccount mocks base method. -func (m *MockAccountKeeper) NewAccount(arg0 context.Context, arg1 types.AccountI) types.AccountI { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "NewAccount", arg0, arg1) - ret0, _ := ret[0].(types.AccountI) - return ret0 -} - -// NewAccount indicates an expected call of NewAccount. -func (mr *MockAccountKeeperMockRecorder) NewAccount(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewAccount", reflect.TypeOf((*MockAccountKeeper)(nil).NewAccount), arg0, arg1) -} - -// NewAccountWithAddress mocks base method. -func (m *MockAccountKeeper) NewAccountWithAddress(ctx context.Context, addr types.AccAddress) types.AccountI { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "NewAccountWithAddress", ctx, addr) - ret0, _ := ret[0].(types.AccountI) - return ret0 -} - -// NewAccountWithAddress indicates an expected call of NewAccountWithAddress. -func (mr *MockAccountKeeperMockRecorder) NewAccountWithAddress(ctx, addr interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewAccountWithAddress", reflect.TypeOf((*MockAccountKeeper)(nil).NewAccountWithAddress), ctx, addr) -} - // SetAccount mocks base method. func (m *MockAccountKeeper) SetAccount(ctx context.Context, acc types.AccountI) { m.ctrl.T.Helper() @@ -190,29 +132,3 @@ func (mr *MockAccountKeeperMockRecorder) SetAccount(ctx, acc interface{}) *gomoc mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetAccount", reflect.TypeOf((*MockAccountKeeper)(nil).SetAccount), ctx, acc) } - -// SetModuleAccount mocks base method. -func (m *MockAccountKeeper) SetModuleAccount(ctx context.Context, macc types.ModuleAccountI) { - m.ctrl.T.Helper() - m.ctrl.Call(m, "SetModuleAccount", ctx, macc) -} - -// SetModuleAccount indicates an expected call of SetModuleAccount. -func (mr *MockAccountKeeperMockRecorder) SetModuleAccount(ctx, macc interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetModuleAccount", reflect.TypeOf((*MockAccountKeeper)(nil).SetModuleAccount), ctx, macc) -} - -// ValidatePermissions mocks base method. -func (m *MockAccountKeeper) ValidatePermissions(macc types.ModuleAccountI) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ValidatePermissions", macc) - ret0, _ := ret[0].(error) - return ret0 -} - -// ValidatePermissions indicates an expected call of ValidatePermissions. -func (mr *MockAccountKeeperMockRecorder) ValidatePermissions(macc interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidatePermissions", reflect.TypeOf((*MockAccountKeeper)(nil).ValidatePermissions), macc) -} diff --git a/x/bank/types/expected_keepers.go b/x/bank/types/expected_keepers.go index 013530f8116e..288d64a8f17d 100644 --- a/x/bank/types/expected_keepers.go +++ b/x/bank/types/expected_keepers.go @@ -14,18 +14,11 @@ import ( type AccountKeeper interface { AddressCodec() address.Codec - NewAccount(context.Context, sdk.AccountI) sdk.AccountI - NewAccountWithAddress(ctx context.Context, addr sdk.AccAddress) sdk.AccountI - GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI HasAccount(ctx context.Context, addr sdk.AccAddress) bool SetAccount(ctx context.Context, acc sdk.AccountI) - ValidatePermissions(macc sdk.ModuleAccountI) error GetModuleAddress(moduleName string) sdk.AccAddress - GetModuleAddressAndPermissions(moduleName string) (addr sdk.AccAddress, permissions []string) - GetModuleAccountAndPermissions(ctx context.Context, moduleName string) (sdk.ModuleAccountI, []string) GetModuleAccount(ctx context.Context, moduleName string) sdk.ModuleAccountI - SetModuleAccount(ctx context.Context, macc sdk.ModuleAccountI) GetModulePermissions() map[string]types.PermissionsForAddress } diff --git a/x/distribution/keeper/allocation_test.go b/x/distribution/keeper/allocation_test.go index 703840f15e1b..c0b5d1304009 100644 --- a/x/distribution/keeper/allocation_test.go +++ b/x/distribution/keeper/allocation_test.go @@ -57,6 +57,7 @@ func TestAllocateTokensToValidatorWithCommission(t *testing.T) { valCodec := address.NewBech32Codec("cosmosvaloper") accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress()) + accountKeeper.EXPECT().AddressCodec().Return(cdcOpts.GetAddressCodec()) stakingKeeper.EXPECT().ValidatorAddressCodec().Return(valCodec).AnyTimes() authorityAddr, err := cdcOpts.GetAddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) @@ -120,6 +121,7 @@ func TestAllocateTokensToManyValidators(t *testing.T) { feeCollectorAcc := authtypes.NewEmptyModuleAccount("fee_collector") accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress()) accountKeeper.EXPECT().GetModuleAccount(gomock.Any(), "fee_collector").Return(feeCollectorAcc) + accountKeeper.EXPECT().AddressCodec().Return(cdcOpts.GetAddressCodec()) stakingKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec("cosmosvaloper")).AnyTimes() env := runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()) @@ -258,6 +260,7 @@ func TestAllocateTokensTruncation(t *testing.T) { feeCollectorAcc := authtypes.NewEmptyModuleAccount("fee_collector") accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress()) accountKeeper.EXPECT().GetModuleAccount(gomock.Any(), "fee_collector").Return(feeCollectorAcc) + accountKeeper.EXPECT().AddressCodec().Return(cdcOpts.GetAddressCodec()) stakingKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec("cosmosvaloper")).AnyTimes() env := runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()) @@ -391,8 +394,10 @@ func TestAllocateTokensToValidatorWithoutCommission(t *testing.T) { env := runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()) valCodec := address.NewBech32Codec("cosmosvaloper") + addrCdc := address.NewBech32Codec("cosmos") accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress()) + accountKeeper.EXPECT().AddressCodec().Return(addrCdc) stakingKeeper.EXPECT().ValidatorAddressCodec().Return(valCodec).AnyTimes() authorityAddr, err := cdcOpts.GetAddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) @@ -460,6 +465,7 @@ func TestAllocateTokensWithZeroTokens(t *testing.T) { valCodec := address.NewBech32Codec("cosmosvaloper") accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress()) + accountKeeper.EXPECT().AddressCodec().Return(cdcOpts.GetAddressCodec()) stakingKeeper.EXPECT().ValidatorAddressCodec().Return(valCodec).AnyTimes() authorityAddr, err := cdcOpts.GetAddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) diff --git a/x/distribution/keeper/delegation.go b/x/distribution/keeper/delegation.go index 1959d3a109f2..b261c2e64a48 100644 --- a/x/distribution/keeper/delegation.go +++ b/x/distribution/keeper/delegation.go @@ -87,7 +87,7 @@ func (k Keeper) calculateDelegationRewardsBetween(ctx context.Context, val sdk.V // CalculateDelegationRewards calculate the total rewards accrued by a delegation func (k Keeper) CalculateDelegationRewards(ctx context.Context, val sdk.ValidatorI, del sdk.DelegationI, endingPeriod uint64) (rewards sdk.DecCoins, err error) { - addrCodec := k.authKeeper.AddressCodec() + addrCodec := k.addrCdc delAddr, err := addrCodec.StringToBytes(del.GetDelegatorAddr()) if err != nil { return sdk.DecCoins{}, err @@ -202,7 +202,7 @@ func (k Keeper) CalculateDelegationRewards(ctx context.Context, val sdk.Validato // withdrawDelegationRewards withdraws the rewards accrued by a delegation. func (k Keeper) withdrawDelegationRewards(ctx context.Context, val sdk.ValidatorI, del sdk.DelegationI) (sdk.Coins, error) { - addrCodec := k.authKeeper.AddressCodec() + addrCodec := k.addrCdc delAddr, err := addrCodec.StringToBytes(del.GetDelegatorAddr()) if err != nil { return nil, err diff --git a/x/distribution/keeper/delegation_test.go b/x/distribution/keeper/delegation_test.go index 8ee03d5f3f3b..f7f5cff97a3e 100644 --- a/x/distribution/keeper/delegation_test.go +++ b/x/distribution/keeper/delegation_test.go @@ -32,6 +32,7 @@ func TestCalculateRewardsBasic(t *testing.T) { testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")) encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, distribution.AppModule{}) ctx := testCtx.Ctx.WithHeaderInfo(header.Info{Height: 1}) + addrCdc := address.NewBech32Codec(sdk.Bech32MainPrefix) bankKeeper := distrtestutil.NewMockBankKeeper(ctrl) stakingKeeper := distrtestutil.NewMockStakingKeeper(ctrl) @@ -39,11 +40,11 @@ func TestCalculateRewardsBasic(t *testing.T) { accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress()) stakingKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec(sdk.Bech32PrefixValAddr)).AnyTimes() - accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec(sdk.Bech32MainPrefix)).AnyTimes() + accountKeeper.EXPECT().AddressCodec().Return(addrCdc) env := runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()) - authorityAddr, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + authorityAddr, err := addrCdc.BytesToString(authtypes.NewModuleAddress("gov")) require.NoError(t, err) distrKeeper := keeper.NewKeeper( @@ -70,7 +71,7 @@ func TestCalculateRewardsBasic(t *testing.T) { require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) - addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) + addrStr, err := addrCdc.BytesToString(addr) require.NoError(t, err) valAddrStr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr) require.NoError(t, err) @@ -144,18 +145,18 @@ func TestCalculateRewardsAfterSlash(t *testing.T) { testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")) encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, distribution.AppModule{}) ctx := testCtx.Ctx.WithHeaderInfo(header.Info{Height: 1}) + addrCdc := address.NewBech32Codec(sdk.Bech32MainPrefix) bankKeeper := distrtestutil.NewMockBankKeeper(ctrl) stakingKeeper := distrtestutil.NewMockStakingKeeper(ctrl) accountKeeper := distrtestutil.NewMockAccountKeeper(ctrl) accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress()) + accountKeeper.EXPECT().AddressCodec().Return(addrCdc) stakingKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec(sdk.Bech32PrefixValAddr)).AnyTimes() - accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec(sdk.Bech32MainPrefix)).AnyTimes() - env := runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()) - authorityAddr, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + authorityAddr, err := addrCdc.BytesToString(authtypes.NewModuleAddress("gov")) require.NoError(t, err) distrKeeper := keeper.NewKeeper( @@ -184,7 +185,7 @@ func TestCalculateRewardsAfterSlash(t *testing.T) { require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) - addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) + addrStr, err := addrCdc.BytesToString(addr) require.NoError(t, err) valAddrStr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr) require.NoError(t, err) @@ -259,18 +260,19 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) { testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")) encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, distribution.AppModule{}) ctx := testCtx.Ctx.WithHeaderInfo(header.Info{Height: 1}) + addrCdc := address.NewBech32Codec(sdk.Bech32MainPrefix) bankKeeper := distrtestutil.NewMockBankKeeper(ctrl) stakingKeeper := distrtestutil.NewMockStakingKeeper(ctrl) accountKeeper := distrtestutil.NewMockAccountKeeper(ctrl) accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress()) + accountKeeper.EXPECT().AddressCodec().Return(addrCdc) stakingKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec(sdk.Bech32PrefixValAddr)).AnyTimes() - accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec(sdk.Bech32MainPrefix)).AnyTimes() env := runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()) - authorityAddr, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + authorityAddr, err := addrCdc.BytesToString(authtypes.NewModuleAddress("gov")) require.NoError(t, err) distrKeeper := keeper.NewKeeper( @@ -299,7 +301,7 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) { require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) - addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) + addrStr, err := addrCdc.BytesToString(addr) require.NoError(t, err) valAddrStr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr) require.NoError(t, err) @@ -395,6 +397,7 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) { testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")) encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, distribution.AppModule{}) ctx := testCtx.Ctx.WithHeaderInfo(header.Info{Height: 1}) + addrCdc := address.NewBech32Codec(sdk.Bech32MainPrefix) bankKeeper := distrtestutil.NewMockBankKeeper(ctrl) stakingKeeper := distrtestutil.NewMockStakingKeeper(ctrl) @@ -402,11 +405,11 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) { accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress()) stakingKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec(sdk.Bech32PrefixValAddr)).AnyTimes() - accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec(sdk.Bech32MainPrefix)).AnyTimes() + accountKeeper.EXPECT().AddressCodec().Return(addrCdc) env := runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()) - authorityAddr, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + authorityAddr, err := addrCdc.BytesToString(authtypes.NewModuleAddress("gov")) require.NoError(t, err) distrKeeper := keeper.NewKeeper( @@ -432,7 +435,7 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) { val, err := distrtestutil.CreateValidator(valConsPk0, operatorAddr, math.NewInt(100)) require.NoError(t, err) - addrStr, err := accountKeeper.AddressCodec().BytesToString(addr0) + addrStr, err := addrCdc.BytesToString(addr0) require.NoError(t, err) valAddrStr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr) require.NoError(t, err) @@ -459,7 +462,7 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) { // second delegation addr1 := sdk.AccAddress(valConsAddr1) - _, del1, err := distrtestutil.Delegate(ctx, distrKeeper, addr1, &val, math.NewInt(100), nil, stakingKeeper, accountKeeper.AddressCodec()) + _, del1, err := distrtestutil.Delegate(ctx, distrKeeper, addr1, &val, math.NewInt(100), nil, stakingKeeper, addrCdc) require.NoError(t, err) stakingKeeper.EXPECT().Delegation(gomock.Any(), addr1, valAddr).Return(del1, nil) @@ -504,6 +507,7 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) { testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")) encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, distribution.AppModule{}) ctx := testCtx.Ctx.WithHeaderInfo(header.Info{Height: 1}) + addrCdc := address.NewBech32Codec(sdk.Bech32MainPrefix) bankKeeper := distrtestutil.NewMockBankKeeper(ctrl) stakingKeeper := distrtestutil.NewMockStakingKeeper(ctrl) @@ -511,11 +515,11 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) { accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress()) stakingKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec(sdk.Bech32PrefixValAddr)).AnyTimes() - accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec(sdk.Bech32MainPrefix)).AnyTimes() + accountKeeper.EXPECT().AddressCodec().Return(addrCdc) env := runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()) - authorityAddr, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + authorityAddr, err := addrCdc.BytesToString(authtypes.NewModuleAddress("gov")) require.NoError(t, err) distrKeeper := keeper.NewKeeper( @@ -543,7 +547,7 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) { val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) - addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) + addrStr, err := addrCdc.BytesToString(addr) require.NoError(t, err) valAddrStr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr) require.NoError(t, err) @@ -591,6 +595,7 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) { testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")) encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, distribution.AppModule{}) ctx := testCtx.Ctx.WithHeaderInfo(header.Info{Height: 1}) + addrCdc := address.NewBech32Codec(sdk.Bech32MainPrefix) bankKeeper := distrtestutil.NewMockBankKeeper(ctrl) stakingKeeper := distrtestutil.NewMockStakingKeeper(ctrl) @@ -598,11 +603,11 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) { accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress()) stakingKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec(sdk.Bech32PrefixValAddr)).AnyTimes() - accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec(sdk.Bech32MainPrefix)).AnyTimes() + accountKeeper.EXPECT().AddressCodec().Return(addrCdc) env := runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()) - authorityAddr, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + authorityAddr, err := addrCdc.BytesToString(authtypes.NewModuleAddress("gov")) require.NoError(t, err) distrKeeper := keeper.NewKeeper( @@ -630,7 +635,7 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) { val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) - addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) + addrStr, err := addrCdc.BytesToString(addr) require.NoError(t, err) valAddrStr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr) require.NoError(t, err) @@ -719,6 +724,7 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")) encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, distribution.AppModule{}) ctx := testCtx.Ctx.WithHeaderInfo(header.Info{Height: 1}) + addrCdc := address.NewBech32Codec(sdk.Bech32MainPrefix) bankKeeper := distrtestutil.NewMockBankKeeper(ctrl) stakingKeeper := distrtestutil.NewMockStakingKeeper(ctrl) @@ -726,11 +732,11 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress()) stakingKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec(sdk.Bech32PrefixValAddr)).AnyTimes() - accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec(sdk.Bech32MainPrefix)).AnyTimes() + accountKeeper.EXPECT().AddressCodec().Return(addrCdc) env := runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()) - authorityAddr, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + authorityAddr, err := addrCdc.BytesToString(authtypes.NewModuleAddress("gov")) require.NoError(t, err) distrKeeper := keeper.NewKeeper( @@ -759,7 +765,7 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) - addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) + addrStr, err := addrCdc.BytesToString(addr) require.NoError(t, err) valAddrStr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr) require.NoError(t, err) @@ -809,7 +815,7 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction), nil, stakingKeeper, - accountKeeper.AddressCodec(), + addrCdc, ) require.NoError(t, err) @@ -875,14 +881,15 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { bankKeeper := distrtestutil.NewMockBankKeeper(ctrl) stakingKeeper := distrtestutil.NewMockStakingKeeper(ctrl) accountKeeper := distrtestutil.NewMockAccountKeeper(ctrl) + addrCdc := address.NewBech32Codec(sdk.Bech32MainPrefix) accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress()) + accountKeeper.EXPECT().AddressCodec().Return(addrCdc) stakingKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec(sdk.Bech32PrefixValAddr)).AnyTimes() - accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec(sdk.Bech32MainPrefix)).AnyTimes() env := runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()) - authorityAddr, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + authorityAddr, err := addrCdc.BytesToString(authtypes.NewModuleAddress("gov")) require.NoError(t, err) distrKeeper := keeper.NewKeeper( @@ -909,7 +916,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) - addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) + addrStr, err := addrCdc.BytesToString(addr) require.NoError(t, err) valAddrStr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr) require.NoError(t, err) @@ -944,7 +951,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { math.NewInt(100), nil, stakingKeeper, - accountKeeper.AddressCodec(), + addrCdc, ) require.NoError(t, err) @@ -1083,19 +1090,20 @@ func Test100PercentCommissionReward(t *testing.T) { testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")) encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, distribution.AppModule{}) ctx := testCtx.Ctx.WithHeaderInfo(header.Info{Height: 1}) + addrCdc := address.NewBech32Codec(sdk.Bech32MainPrefix) bankKeeper := distrtestutil.NewMockBankKeeper(ctrl) stakingKeeper := distrtestutil.NewMockStakingKeeper(ctrl) accountKeeper := distrtestutil.NewMockAccountKeeper(ctrl) accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress()) + accountKeeper.EXPECT().AddressCodec().Return(addrCdc) stakingKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec(sdk.Bech32PrefixValAddr)).AnyTimes() stakingKeeper.EXPECT().BondDenom(gomock.Any()).Return("stake", nil).AnyTimes() - accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec(sdk.Bech32MainPrefix)).AnyTimes() env := runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()) - authorityAddr, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + authorityAddr, err := addrCdc.BytesToString(authtypes.NewModuleAddress("gov")) require.NoError(t, err) distrKeeper := keeper.NewKeeper( @@ -1122,7 +1130,7 @@ func Test100PercentCommissionReward(t *testing.T) { require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(10, 1), math.LegacyNewDecWithPrec(10, 1), math.LegacyNewDec(0)) - addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) + addrStr, err := addrCdc.BytesToString(addr) require.NoError(t, err) valAddrStr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr) require.NoError(t, err) diff --git a/x/distribution/keeper/genesis.go b/x/distribution/keeper/genesis.go index b6fa562268e4..41f8a715c163 100644 --- a/x/distribution/keeper/genesis.go +++ b/x/distribution/keeper/genesis.go @@ -23,11 +23,11 @@ func (k Keeper) InitGenesis(ctx context.Context, data types.GenesisState) error } for _, dwi := range data.DelegatorWithdrawInfos { - delegatorAddress, err := k.authKeeper.AddressCodec().StringToBytes(dwi.DelegatorAddress) + delegatorAddress, err := k.addrCdc.StringToBytes(dwi.DelegatorAddress) if err != nil { return err } - withdrawAddress, err := k.authKeeper.AddressCodec().StringToBytes(dwi.WithdrawAddress) + withdrawAddress, err := k.addrCdc.StringToBytes(dwi.WithdrawAddress) if err != nil { return err } @@ -83,7 +83,7 @@ func (k Keeper) InitGenesis(ctx context.Context, data types.GenesisState) error if err != nil { return err } - delegatorAddress, err := k.authKeeper.AddressCodec().StringToBytes(del.DelegatorAddress) + delegatorAddress, err := k.addrCdc.StringToBytes(del.DelegatorAddress) if err != nil { return err } @@ -123,9 +123,6 @@ func (k Keeper) InitGenesis(ctx context.Context, data types.GenesisState) error } balances := k.bankKeeper.GetAllBalances(ctx, moduleAcc.GetAddress()) - if balances.IsZero() { - k.authKeeper.SetModuleAccount(ctx, moduleAcc) - } if !balances.Equal(moduleHoldingsInt) { return fmt.Errorf("distribution module balance does not match the module holdings: %s <-> %s", balances, moduleHoldingsInt) } @@ -146,11 +143,11 @@ func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error) var dwi []types.DelegatorWithdrawInfo err = k.DelegatorsWithdrawAddress.Walk(ctx, nil, func(key, value sdk.AccAddress) (stop bool, err error) { - keyAddr, err := k.authKeeper.AddressCodec().BytesToString(key) + keyAddr, err := k.addrCdc.BytesToString(key) if err != nil { return true, err } - valueAddr, err := k.authKeeper.AddressCodec().BytesToString(value) + valueAddr, err := k.addrCdc.BytesToString(value) if err != nil { return true, err } @@ -241,7 +238,7 @@ func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error) dels := make([]types.DelegatorStartingInfoRecord, 0) err = k.DelegatorStartingInfo.Walk(ctx, nil, func(key collections.Pair[sdk.ValAddress, sdk.AccAddress], value types.DelegatorStartingInfo) (stop bool, err error) { - delAddr, err := k.authKeeper.AddressCodec().BytesToString(key.K2()) + delAddr, err := k.addrCdc.BytesToString(key.K2()) if err != nil { return true, err } diff --git a/x/distribution/keeper/grpc_query.go b/x/distribution/keeper/grpc_query.go index 5337ec58c342..3af717a16ca5 100644 --- a/x/distribution/keeper/grpc_query.go +++ b/x/distribution/keeper/grpc_query.go @@ -86,7 +86,7 @@ func (k Querier) ValidatorDistributionInfo(ctx context.Context, req *types.Query return nil, err } - operatorAddr, err := k.authKeeper.AddressCodec().BytesToString(delAdr) + operatorAddr, err := k.addrCdc.BytesToString(delAdr) if err != nil { return nil, err } @@ -225,7 +225,7 @@ func (k Querier) DelegationRewards(ctx context.Context, req *types.QueryDelegati return nil, errors.Wrap(types.ErrNoValidatorExists, req.ValidatorAddress) } - delAdr, err := k.authKeeper.AddressCodec().StringToBytes(req.DelegatorAddress) + delAdr, err := k.addrCdc.StringToBytes(req.DelegatorAddress) if err != nil { return nil, err } @@ -264,7 +264,7 @@ func (k Querier) DelegationTotalRewards(ctx context.Context, req *types.QueryDel total := sdk.DecCoins{} var delRewards []types.DelegationDelegatorReward - delAdr, err := k.authKeeper.AddressCodec().StringToBytes(req.DelegatorAddress) + delAdr, err := k.addrCdc.StringToBytes(req.DelegatorAddress) if err != nil { return nil, err } @@ -322,7 +322,7 @@ func (k Querier) DelegatorValidators(ctx context.Context, req *types.QueryDelega return nil, status.Error(codes.InvalidArgument, "empty delegator address") } - delAdr, err := k.authKeeper.AddressCodec().StringToBytes(req.DelegatorAddress) + delAdr, err := k.addrCdc.StringToBytes(req.DelegatorAddress) if err != nil { return nil, err } @@ -351,7 +351,7 @@ func (k Querier) DelegatorWithdrawAddress(ctx context.Context, req *types.QueryD if req.DelegatorAddress == "" { return nil, status.Error(codes.InvalidArgument, "empty delegator address") } - delAdr, err := k.authKeeper.AddressCodec().StringToBytes(req.DelegatorAddress) + delAdr, err := k.addrCdc.StringToBytes(req.DelegatorAddress) if err != nil { return nil, err } @@ -361,7 +361,7 @@ func (k Querier) DelegatorWithdrawAddress(ctx context.Context, req *types.QueryD return nil, err } - addr, err := k.authKeeper.AddressCodec().BytesToString(withdrawAddr) + addr, err := k.addrCdc.BytesToString(withdrawAddr) if err != nil { return nil, err } diff --git a/x/distribution/keeper/invariants.go b/x/distribution/keeper/invariants.go index 04f3294320a0..bf01b4cf644d 100644 --- a/x/distribution/keeper/invariants.go +++ b/x/distribution/keeper/invariants.go @@ -80,7 +80,7 @@ func CanWithdrawInvariant(k Keeper) sdk.Invariant { } for _, del := range allDelegations { - delAddr, err := k.authKeeper.AddressCodec().StringToBytes(del.GetDelegatorAddr()) + delAddr, err := k.addrCdc.StringToBytes(del.GetDelegatorAddr()) if err != nil { panic(err) } diff --git a/x/distribution/keeper/keeper.go b/x/distribution/keeper/keeper.go index 6ee76320f982..a44285055dd4 100644 --- a/x/distribution/keeper/keeper.go +++ b/x/distribution/keeper/keeper.go @@ -7,6 +7,7 @@ import ( "cosmossdk.io/collections" collcodec "cosmossdk.io/collections/codec" + "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" "cosmossdk.io/core/comet" "cosmossdk.io/core/event" @@ -25,6 +26,7 @@ type Keeper struct { cometService comet.Service cdc codec.BinaryCodec + addrCdc address.Codec authKeeper types.AccountKeeper bankKeeper types.BankKeeper stakingKeeper types.StakingKeeper @@ -76,6 +78,7 @@ func NewKeeper( Environment: env, cometService: cometService, cdc: cdc, + addrCdc: ak.AddressCodec(), authKeeper: ak, bankKeeper: bk, stakingKeeper: sk, @@ -163,7 +166,7 @@ func (k Keeper) SetWithdrawAddr(ctx context.Context, delegatorAddr, withdrawAddr return types.ErrSetWithdrawAddrDisabled } - addr, err := k.authKeeper.AddressCodec().BytesToString(withdrawAddr) + addr, err := k.addrCdc.BytesToString(withdrawAddr) if err != nil { return err } diff --git a/x/distribution/keeper/keeper_test.go b/x/distribution/keeper/keeper_test.go index 589b738a0b57..0d57217a407f 100644 --- a/x/distribution/keeper/keeper_test.go +++ b/x/distribution/keeper/keeper_test.go @@ -47,8 +47,8 @@ func initFixture(t *testing.T) (sdk.Context, []sdk.AccAddress, keeper.Keeper, de stakingKeeper := distrtestutil.NewMockStakingKeeper(ctrl) accountKeeper := distrtestutil.NewMockAccountKeeper(ctrl) + accountKeeper.EXPECT().AddressCodec().Return(cdcOpts.GetAddressCodec()).AnyTimes() accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress()) - accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes() stakingKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec("cosmosvaloper")).AnyTimes() diff --git a/x/distribution/keeper/msg_server.go b/x/distribution/keeper/msg_server.go index 5d1fbdcba553..370529b95be2 100644 --- a/x/distribution/keeper/msg_server.go +++ b/x/distribution/keeper/msg_server.go @@ -24,12 +24,12 @@ func NewMsgServerImpl(keeper Keeper) types.MsgServer { } func (k msgServer) SetWithdrawAddress(ctx context.Context, msg *types.MsgSetWithdrawAddress) (*types.MsgSetWithdrawAddressResponse, error) { - delegatorAddress, err := k.authKeeper.AddressCodec().StringToBytes(msg.DelegatorAddress) + delegatorAddress, err := k.addrCdc.StringToBytes(msg.DelegatorAddress) if err != nil { return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid delegator address: %s", err) } - withdrawAddress, err := k.authKeeper.AddressCodec().StringToBytes(msg.WithdrawAddress) + withdrawAddress, err := k.addrCdc.StringToBytes(msg.WithdrawAddress) if err != nil { return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid withdraw address: %s", err) } @@ -48,7 +48,7 @@ func (k msgServer) WithdrawDelegatorReward(ctx context.Context, msg *types.MsgWi return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid validator address: %s", err) } - delegatorAddress, err := k.authKeeper.AddressCodec().StringToBytes(msg.DelegatorAddress) + delegatorAddress, err := k.addrCdc.StringToBytes(msg.DelegatorAddress) if err != nil { return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid delegator address: %s", err) } @@ -78,7 +78,7 @@ func (k msgServer) WithdrawValidatorCommission(ctx context.Context, msg *types.M // Deprecated: DO NOT USE // This method uses deprecated message request. Use FundCommunityPool from x/protocolpool module instead. func (k msgServer) FundCommunityPool(ctx context.Context, msg *types.MsgFundCommunityPool) (*types.MsgFundCommunityPoolResponse, error) { - depositor, err := k.authKeeper.AddressCodec().StringToBytes(msg.Depositor) + depositor, err := k.addrCdc.StringToBytes(msg.Depositor) if err != nil { return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid depositor address: %s", err) } @@ -126,7 +126,7 @@ func (k msgServer) CommunityPoolSpend(ctx context.Context, msg *types.MsgCommuni return nil, err } - recipient, err := k.authKeeper.AddressCodec().StringToBytes(msg.Recipient) + recipient, err := k.addrCdc.StringToBytes(msg.Recipient) if err != nil { return nil, fmt.Errorf("invalid recipient address: %w", err) } @@ -141,7 +141,7 @@ func (k msgServer) CommunityPoolSpend(ctx context.Context, msg *types.MsgCommuni } func (k msgServer) DepositValidatorRewardsPool(ctx context.Context, msg *types.MsgDepositValidatorRewardsPool) (*types.MsgDepositValidatorRewardsPoolResponse, error) { - depositor, err := k.authKeeper.AddressCodec().StringToBytes(msg.Depositor) + depositor, err := k.addrCdc.StringToBytes(msg.Depositor) if err != nil { return nil, fmt.Errorf("invalid depositor address: %w", err) } @@ -183,7 +183,7 @@ func (k msgServer) DepositValidatorRewardsPool(ctx context.Context, msg *types.M } func (k *Keeper) validateAuthority(authority string) error { - if _, err := k.authKeeper.AddressCodec().StringToBytes(authority); err != nil { + if _, err := k.addrCdc.StringToBytes(authority); err != nil { return sdkerrors.ErrInvalidAddress.Wrapf("invalid authority address: %s", err) } diff --git a/x/distribution/testutil/expected_keepers_mocks.go b/x/distribution/testutil/expected_keepers_mocks.go index e30590d865e9..d702ed78a61a 100644 --- a/x/distribution/testutil/expected_keepers_mocks.go +++ b/x/distribution/testutil/expected_keepers_mocks.go @@ -51,20 +51,6 @@ func (mr *MockAccountKeeperMockRecorder) AddressCodec() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddressCodec", reflect.TypeOf((*MockAccountKeeper)(nil).AddressCodec)) } -// GetAccount mocks base method. -func (m *MockAccountKeeper) GetAccount(ctx context.Context, addr types0.AccAddress) types0.AccountI { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetAccount", ctx, addr) - ret0, _ := ret[0].(types0.AccountI) - return ret0 -} - -// GetAccount indicates an expected call of GetAccount. -func (mr *MockAccountKeeperMockRecorder) GetAccount(ctx, addr interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAccount", reflect.TypeOf((*MockAccountKeeper)(nil).GetAccount), ctx, addr) -} - // GetModuleAccount mocks base method. func (m *MockAccountKeeper) GetModuleAccount(ctx context.Context, name string) types0.ModuleAccountI { m.ctrl.T.Helper() @@ -93,18 +79,6 @@ func (mr *MockAccountKeeperMockRecorder) GetModuleAddress(name interface{}) *gom return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetModuleAddress", reflect.TypeOf((*MockAccountKeeper)(nil).GetModuleAddress), name) } -// SetModuleAccount mocks base method. -func (m *MockAccountKeeper) SetModuleAccount(arg0 context.Context, arg1 types0.ModuleAccountI) { - m.ctrl.T.Helper() - m.ctrl.Call(m, "SetModuleAccount", arg0, arg1) -} - -// SetModuleAccount indicates an expected call of SetModuleAccount. -func (mr *MockAccountKeeperMockRecorder) SetModuleAccount(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetModuleAccount", reflect.TypeOf((*MockAccountKeeper)(nil).SetModuleAccount), arg0, arg1) -} - // MockBankKeeper is a mock of BankKeeper interface. type MockBankKeeper struct { ctrl *gomock.Controller diff --git a/x/distribution/types/expected_keepers.go b/x/distribution/types/expected_keepers.go index 8288f2206280..e7ec583b9de5 100644 --- a/x/distribution/types/expected_keepers.go +++ b/x/distribution/types/expected_keepers.go @@ -12,11 +12,8 @@ import ( // AccountKeeper defines the expected account keeper used for simulations (noalias) type AccountKeeper interface { AddressCodec() address.Codec - GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI GetModuleAddress(name string) sdk.AccAddress GetModuleAccount(ctx context.Context, name string) sdk.ModuleAccountI - // TODO remove with genesis 2-phases refactor https://github.com/cosmos/cosmos-sdk/issues/2862 - SetModuleAccount(context.Context, sdk.ModuleAccountI) } // BankKeeper defines the expected interface needed to retrieve account balances. diff --git a/x/feegrant/CHANGELOG.md b/x/feegrant/CHANGELOG.md index 9f565f739cb0..a3a9d036f5c3 100644 --- a/x/feegrant/CHANGELOG.md +++ b/x/feegrant/CHANGELOG.md @@ -31,6 +31,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes +* [#21651](https://github.com/cosmos/cosmos-sdk/pull/21651) NewKeeper receives an address.Codec instead of an x/auth keeper. * [#21377](https://github.com/cosmos/cosmos-sdk/pull/21377) Simulation API breaking changes: * `SimulateMsgGrantAllowance` and `SimulateMsgRevokeAllowance` no longer require a `ProtoCodec` parameter. * `WeightedOperations` functions no longer require `ProtoCodec`, `JSONCodec`, or `address.Codec` parameters. diff --git a/x/feegrant/expected_keepers.go b/x/feegrant/expected_keepers.go index ce6a2d0a5698..9f0591ad3ffc 100644 --- a/x/feegrant/expected_keepers.go +++ b/x/feegrant/expected_keepers.go @@ -3,23 +3,9 @@ package feegrant import ( "context" - "cosmossdk.io/core/address" - sdk "github.com/cosmos/cosmos-sdk/types" ) -// AccountKeeper defines the expected auth Account Keeper (noalias) -type AccountKeeper interface { - AddressCodec() address.Codec - - GetModuleAddress(moduleName string) sdk.AccAddress - GetModuleAccount(ctx context.Context, moduleName string) sdk.ModuleAccountI - - NewAccountWithAddress(ctx context.Context, addr sdk.AccAddress) sdk.AccountI - GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI - SetAccount(ctx context.Context, acc sdk.AccountI) -} - // BankKeeper defines the expected supply Keeper (noalias) type BankKeeper interface { SpendableCoins(ctx context.Context, addr sdk.AccAddress) sdk.Coins diff --git a/x/feegrant/keeper/genesis_test.go b/x/feegrant/keeper/genesis_test.go index dd2a1eb27bfc..68edd5915808 100644 --- a/x/feegrant/keeper/genesis_test.go +++ b/x/feegrant/keeper/genesis_test.go @@ -4,18 +4,17 @@ import ( "errors" "testing" - "github.com/golang/mock/gomock" "gotest.tools/v3/assert" + address "cosmossdk.io/core/address" coretesting "cosmossdk.io/core/testing" "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" "cosmossdk.io/x/feegrant" "cosmossdk.io/x/feegrant/keeper" "cosmossdk.io/x/feegrant/module" - feegranttestutil "cosmossdk.io/x/feegrant/testutil" - "github.com/cosmos/cosmos-sdk/codec/address" + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" @@ -24,7 +23,6 @@ import ( "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" ) var ( @@ -37,7 +35,7 @@ var ( type genesisFixture struct { ctx sdk.Context feegrantKeeper keeper.Keeper - accountKeeper *feegranttestutil.MockAccountKeeper + addrCdc address.Codec } func initFixture(t *testing.T) *genesisFixture { @@ -46,22 +44,18 @@ func initFixture(t *testing.T) *genesisFixture { testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")) encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, module.AppModule{}) - ctrl := gomock.NewController(t) - accountKeeper := feegranttestutil.NewMockAccountKeeper(ctrl) - accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes() + addrCdc := addresscodec.NewBech32Codec(sdk.Bech32MainPrefix) return &genesisFixture{ ctx: testCtx.Ctx, - feegrantKeeper: keeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()), encCfg.Codec, accountKeeper), - accountKeeper: accountKeeper, + feegrantKeeper: keeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()), encCfg.Codec, addrCdc), + addrCdc: addrCdc, } } func TestImportExportGenesis(t *testing.T) { f := initFixture(t) - f.accountKeeper.EXPECT().GetAccount(gomock.Any(), granteeAddr).Return(authtypes.NewBaseAccountWithAddress(granteeAddr)).AnyTimes() - coins := sdk.NewCoins(sdk.NewCoin("foo", math.NewInt(1_000))) now := f.ctx.HeaderInfo().Time oneYear := now.AddDate(1, 0, 0) @@ -74,9 +68,9 @@ func TestImportExportGenesis(t *testing.T) { genesis, err := f.feegrantKeeper.ExportGenesis(f.ctx) assert.NilError(t, err) - granter, err := f.accountKeeper.AddressCodec().BytesToString(granterAddr.Bytes()) + granter, err := f.addrCdc.BytesToString(granterAddr.Bytes()) assert.NilError(t, err) - grantee, err := f.accountKeeper.AddressCodec().BytesToString(granteeAddr.Bytes()) + grantee, err := f.addrCdc.BytesToString(granteeAddr.Bytes()) assert.NilError(t, err) // revoke fee allowance @@ -98,7 +92,7 @@ func TestInitGenesis(t *testing.T) { any, err := codectypes.NewAnyWithValue(&testdata.Dog{}) assert.NilError(t, err) - ac := address.NewBech32Codec("cosmos") + ac := addresscodec.NewBech32Codec("cosmos") granter, err := ac.BytesToString(granterAddr.Bytes()) assert.NilError(t, err) diff --git a/x/feegrant/keeper/grpc_query.go b/x/feegrant/keeper/grpc_query.go index f5ff7df04370..09c21cf88c58 100644 --- a/x/feegrant/keeper/grpc_query.go +++ b/x/feegrant/keeper/grpc_query.go @@ -23,12 +23,12 @@ func (q Keeper) Allowance(ctx context.Context, req *feegrant.QueryAllowanceReque return nil, status.Error(codes.InvalidArgument, "invalid request") } - granterAddr, err := q.authKeeper.AddressCodec().StringToBytes(req.Granter) + granterAddr, err := q.addrCdc.StringToBytes(req.Granter) if err != nil { return nil, err } - granteeAddr, err := q.authKeeper.AddressCodec().StringToBytes(req.Grantee) + granteeAddr, err := q.addrCdc.StringToBytes(req.Grantee) if err != nil { return nil, err } @@ -63,7 +63,7 @@ func (q Keeper) Allowances(c context.Context, req *feegrant.QueryAllowancesReque return nil, status.Error(codes.InvalidArgument, "invalid request") } - granteeAddr, err := q.authKeeper.AddressCodec().StringToBytes(req.Grantee) + granteeAddr, err := q.addrCdc.StringToBytes(req.Grantee) if err != nil { return nil, err } @@ -91,7 +91,7 @@ func (q Keeper) AllowancesByGranter(c context.Context, req *feegrant.QueryAllowa return nil, status.Error(codes.InvalidArgument, "invalid request") } - granterAddr, err := q.authKeeper.AddressCodec().StringToBytes(req.Granter) + granterAddr, err := q.addrCdc.StringToBytes(req.Granter) if err != nil { return nil, err } diff --git a/x/feegrant/keeper/keeper.go b/x/feegrant/keeper/keeper.go index 30e47ae36086..6db74f1022d5 100644 --- a/x/feegrant/keeper/keeper.go +++ b/x/feegrant/keeper/keeper.go @@ -5,6 +5,7 @@ import ( "time" "cosmossdk.io/collections" + "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" corecontext "cosmossdk.io/core/context" "cosmossdk.io/core/event" @@ -22,9 +23,9 @@ import ( type Keeper struct { appmodule.Environment - cdc codec.BinaryCodec - authKeeper feegrant.AccountKeeper - Schema collections.Schema + cdc codec.BinaryCodec + addrCdc address.Codec + Schema collections.Schema // FeeAllowance key: grantee+granter | value: Grant FeeAllowance collections.Map[collections.Pair[sdk.AccAddress, sdk.AccAddress], feegrant.Grant] // FeeAllowanceQueue key: expiration time+grantee+granter | value: bool @@ -34,13 +35,13 @@ type Keeper struct { var _ ante.FeegrantKeeper = &Keeper{} // NewKeeper creates a feegrant Keeper -func NewKeeper(env appmodule.Environment, cdc codec.BinaryCodec, ak feegrant.AccountKeeper) Keeper { +func NewKeeper(env appmodule.Environment, cdc codec.BinaryCodec, addrCdc address.Codec) Keeper { sb := collections.NewSchemaBuilder(env.KVStoreService) return Keeper{ Environment: env, cdc: cdc, - authKeeper: ak, + addrCdc: addrCdc, FeeAllowance: collections.NewMap( sb, feegrant.FeeAllowanceKeyPrefix, @@ -85,11 +86,11 @@ func (k Keeper) GrantAllowance(ctx context.Context, granter, grantee sdk.AccAddr } } - granterStr, err := k.authKeeper.AddressCodec().BytesToString(granter) + granterStr, err := k.addrCdc.BytesToString(granter) if err != nil { return err } - granteeStr, err := k.authKeeper.AddressCodec().BytesToString(grantee) + granteeStr, err := k.addrCdc.BytesToString(grantee) if err != nil { return err } @@ -126,11 +127,11 @@ func (k Keeper) UpdateAllowance(ctx context.Context, granter, grantee sdk.AccAdd return err } - granterStr, err := k.authKeeper.AddressCodec().BytesToString(granter) + granterStr, err := k.addrCdc.BytesToString(granter) if err != nil { return err } - granteeStr, err := k.authKeeper.AddressCodec().BytesToString(grantee) + granteeStr, err := k.addrCdc.BytesToString(grantee) if err != nil { return err } @@ -173,11 +174,11 @@ func (k Keeper) revokeAllowance(ctx context.Context, granter, grantee sdk.AccAdd } } - granterStr, err := k.authKeeper.AddressCodec().BytesToString(granter) + granterStr, err := k.addrCdc.BytesToString(granter) if err != nil { return err } - granteeStr, err := k.authKeeper.AddressCodec().BytesToString(grantee) + granteeStr, err := k.addrCdc.BytesToString(grantee) if err != nil { return err } @@ -217,11 +218,11 @@ func (k Keeper) UseGrantedFees(ctx context.Context, granter, grantee sdk.AccAddr return err } - granterStr, err := k.authKeeper.AddressCodec().BytesToString(granter) + granterStr, err := k.addrCdc.BytesToString(granter) if err != nil { return err } - granteeStr, err := k.authKeeper.AddressCodec().BytesToString(grantee) + granteeStr, err := k.addrCdc.BytesToString(grantee) if err != nil { return err } @@ -255,11 +256,11 @@ func (k *Keeper) emitUseGrantEvent(ctx context.Context, granter, grantee string) // InitGenesis will initialize the keeper from a *previously validated* GenesisState func (k Keeper) InitGenesis(ctx context.Context, data *feegrant.GenesisState) error { for _, f := range data.Allowances { - granter, err := k.authKeeper.AddressCodec().StringToBytes(f.Granter) + granter, err := k.addrCdc.StringToBytes(f.Granter) if err != nil { return err } - grantee, err := k.authKeeper.AddressCodec().StringToBytes(f.Grantee) + grantee, err := k.addrCdc.StringToBytes(f.Grantee) if err != nil { return err } diff --git a/x/feegrant/keeper/keeper_test.go b/x/feegrant/keeper/keeper_test.go index af5cde5dc663..7b9ed26b5d50 100644 --- a/x/feegrant/keeper/keeper_test.go +++ b/x/feegrant/keeper/keeper_test.go @@ -3,7 +3,6 @@ package keeper_test import ( "testing" - "github.com/golang/mock/gomock" "github.com/stretchr/testify/suite" "cosmossdk.io/core/header" @@ -13,7 +12,6 @@ import ( "cosmossdk.io/x/feegrant" "cosmossdk.io/x/feegrant/keeper" "cosmossdk.io/x/feegrant/module" - feegranttestutil "cosmossdk.io/x/feegrant/testutil" codecaddress "github.com/cosmos/cosmos-sdk/codec/address" codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" @@ -22,7 +20,6 @@ import ( simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" ) type KeeperTestSuite struct { @@ -34,7 +31,6 @@ type KeeperTestSuite struct { msgSrvr feegrant.MsgServer atom sdk.Coins feegrantKeeper keeper.Keeper - accountKeeper *feegranttestutil.MockAccountKeeper } func TestKeeperTestSuite(t *testing.T) { @@ -48,21 +44,14 @@ func (suite *KeeperTestSuite) SetupTest() { encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, module.AppModule{}) // setup gomock and initialize some globally expected executions - ctrl := gomock.NewController(suite.T()) - suite.accountKeeper = feegranttestutil.NewMockAccountKeeper(ctrl) - for i := 0; i < len(suite.addrs); i++ { - suite.accountKeeper.EXPECT().GetAccount(gomock.Any(), suite.addrs[i]).Return(authtypes.NewBaseAccountWithAddress(suite.addrs[i])).AnyTimes() - } - ac := codecaddress.NewBech32Codec("cosmos") - suite.accountKeeper.EXPECT().AddressCodec().Return(ac).AnyTimes() for _, addr := range suite.addrs { str, err := ac.BytesToString(addr) suite.Require().NoError(err) suite.encodedAddrs = append(suite.encodedAddrs, str) } - suite.feegrantKeeper = keeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()), encCfg.Codec, suite.accountKeeper) + suite.feegrantKeeper = keeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()), encCfg.Codec, ac) suite.ctx = testCtx.Ctx suite.msgSrvr = keeper.NewMsgServerImpl(suite.feegrantKeeper) suite.atom = sdk.NewCoins(sdk.NewCoin("atom", sdkmath.NewInt(555))) @@ -181,7 +170,6 @@ func (suite *KeeperTestSuite) TestKeeperCrud() { address := "cosmos1rxr4mq58w3gtnx5tsc438mwjjafv3mja7k5pnu" accAddr, err := codecaddress.NewBech32Codec("cosmos").StringToBytes(address) suite.Require().NoError(err) - suite.accountKeeper.EXPECT().GetAccount(gomock.Any(), accAddr).Return(authtypes.NewBaseAccountWithAddress(accAddr)).AnyTimes() // let's grant and revoke authorization to non existing account err = suite.feegrantKeeper.GrantAllowance(suite.ctx, suite.addrs[3], accAddr, basic2) diff --git a/x/feegrant/keeper/msg_server.go b/x/feegrant/keeper/msg_server.go index 79e4405ccfb9..e18a1f8f3dce 100644 --- a/x/feegrant/keeper/msg_server.go +++ b/x/feegrant/keeper/msg_server.go @@ -31,12 +31,12 @@ func (k msgServer) GrantAllowance(ctx context.Context, msg *feegrant.MsgGrantAll return nil, errorsmod.Wrap(sdkerrors.ErrInvalidAddress, "cannot self-grant fee authorization") } - grantee, err := k.authKeeper.AddressCodec().StringToBytes(msg.Grantee) + grantee, err := k.addrCdc.StringToBytes(msg.Grantee) if err != nil { return nil, err } - granter, err := k.authKeeper.AddressCodec().StringToBytes(msg.Granter) + granter, err := k.addrCdc.StringToBytes(msg.Granter) if err != nil { return nil, err } @@ -68,12 +68,12 @@ func (k msgServer) RevokeAllowance(ctx context.Context, msg *feegrant.MsgRevokeA return nil, errorsmod.Wrap(sdkerrors.ErrInvalidAddress, "addresses must be different") } - grantee, err := k.authKeeper.AddressCodec().StringToBytes(msg.Grantee) + grantee, err := k.addrCdc.StringToBytes(msg.Grantee) if err != nil { return nil, err } - granter, err := k.authKeeper.AddressCodec().StringToBytes(msg.Granter) + granter, err := k.addrCdc.StringToBytes(msg.Granter) if err != nil { return nil, err } diff --git a/x/feegrant/keeper/msg_server_test.go b/x/feegrant/keeper/msg_server_test.go index 2e4e8f405c80..62fa1aa65079 100644 --- a/x/feegrant/keeper/msg_server_test.go +++ b/x/feegrant/keeper/msg_server_test.go @@ -3,16 +3,12 @@ package keeper_test import ( "time" - "github.com/golang/mock/gomock" - "cosmossdk.io/collections" "cosmossdk.io/core/header" "cosmossdk.io/x/feegrant" - codecaddress "github.com/cosmos/cosmos-sdk/codec/address" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" ) func (suite *KeeperTestSuite) TestGrantAllowance() { @@ -20,8 +16,6 @@ func (suite *KeeperTestSuite) TestGrantAllowance() { oneYear := ctx.HeaderInfo().Time.AddDate(1, 0, 0) yesterday := ctx.HeaderInfo().Time.AddDate(0, 0, -1) - addressCodec := codecaddress.NewBech32Codec("cosmos") - testCases := []struct { name string req func() *feegrant.MsgGrantAllowance @@ -62,22 +56,12 @@ func (suite *KeeperTestSuite) TestGrantAllowance() { name: "valid: grantee account doesn't exist", req: func() *feegrant.MsgGrantAllowance { grantee := "cosmos139f7kncmglres2nf3h4hc4tade85ekfr8sulz5" - granteeAccAddr, err := addressCodec.StringToBytes(grantee) - suite.Require().NoError(err) any, err := codectypes.NewAnyWithValue(&feegrant.BasicAllowance{ SpendLimit: suite.atom, Expiration: &oneYear, }) suite.Require().NoError(err) - suite.accountKeeper.EXPECT().GetAccount(gomock.Any(), granteeAccAddr).Return(nil).AnyTimes() - - acc := authtypes.NewBaseAccountWithAddress(granteeAccAddr) - add, err := addressCodec.StringToBytes(grantee) - suite.Require().NoError(err) - - suite.accountKeeper.EXPECT().NewAccountWithAddress(gomock.Any(), add).Return(acc).AnyTimes() - suite.Require().NoError(err) return &feegrant.MsgGrantAllowance{ Granter: suite.encodedAddrs[0], diff --git a/x/feegrant/module/abci_test.go b/x/feegrant/module/abci_test.go index 12f65c24c89f..0faa384493c8 100644 --- a/x/feegrant/module/abci_test.go +++ b/x/feegrant/module/abci_test.go @@ -3,7 +3,6 @@ package module_test import ( "testing" - "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" "cosmossdk.io/core/header" @@ -13,7 +12,6 @@ import ( "cosmossdk.io/x/feegrant" "cosmossdk.io/x/feegrant/keeper" "cosmossdk.io/x/feegrant/module" - feegranttestutil "cosmossdk.io/x/feegrant/testutil" "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec/address" @@ -23,7 +21,6 @@ import ( simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" ) func TestFeegrantPruning(t *testing.T) { @@ -40,18 +37,11 @@ func TestFeegrantPruning(t *testing.T) { now := testCtx.Ctx.HeaderInfo().Time oneDay := now.AddDate(0, 0, 1) - ctrl := gomock.NewController(t) - accountKeeper := feegranttestutil.NewMockAccountKeeper(ctrl) - accountKeeper.EXPECT().GetAccount(gomock.Any(), grantee).Return(authtypes.NewBaseAccountWithAddress(grantee)).AnyTimes() - accountKeeper.EXPECT().GetAccount(gomock.Any(), granter1).Return(authtypes.NewBaseAccountWithAddress(granter1)).AnyTimes() - accountKeeper.EXPECT().GetAccount(gomock.Any(), granter2).Return(authtypes.NewBaseAccountWithAddress(granter2)).AnyTimes() - accountKeeper.EXPECT().GetAccount(gomock.Any(), granter3).Return(authtypes.NewBaseAccountWithAddress(granter3)).AnyTimes() ac := address.NewBech32Codec("cosmos") - accountKeeper.EXPECT().AddressCodec().Return(ac).AnyTimes() env := runtime.NewEnvironment(runtime.NewKVStoreService(key), coretesting.NewNopLogger()) - feegrantKeeper := keeper.NewKeeper(env, encCfg.Codec, accountKeeper) + feegrantKeeper := keeper.NewKeeper(env, encCfg.Codec, ac) err := feegrantKeeper.GrantAllowance( testCtx.Ctx, diff --git a/x/feegrant/module/depinject.go b/x/feegrant/module/depinject.go index 613d51a14743..85b7508468c5 100644 --- a/x/feegrant/module/depinject.go +++ b/x/feegrant/module/depinject.go @@ -2,6 +2,7 @@ package module import ( modulev1 "cosmossdk.io/api/cosmos/feegrant/module/v1" + "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" "cosmossdk.io/depinject" "cosmossdk.io/depinject/appconfig" @@ -30,15 +31,15 @@ func init() { type FeegrantInputs struct { depinject.In - Environment appmodule.Environment - Cdc codec.Codec - AccountKeeper feegrant.AccountKeeper - BankKeeper feegrant.BankKeeper - Registry cdctypes.InterfaceRegistry + Environment appmodule.Environment + Cdc codec.Codec + AddressCodec address.Codec + BankKeeper feegrant.BankKeeper + Registry cdctypes.InterfaceRegistry } func ProvideModule(in FeegrantInputs) (keeper.Keeper, appmodule.AppModule) { - k := keeper.NewKeeper(in.Environment, in.Cdc, in.AccountKeeper) + k := keeper.NewKeeper(in.Environment, in.Cdc, in.AddressCodec) m := NewAppModule(in.Cdc, k, in.Registry) return k, m } diff --git a/x/feegrant/testutil/expected_keepers_mocks.go b/x/feegrant/testutil/expected_keepers_mocks.go index d7567b8266d3..d1149afa8503 100644 --- a/x/feegrant/testutil/expected_keepers_mocks.go +++ b/x/feegrant/testutil/expected_keepers_mocks.go @@ -8,116 +8,10 @@ import ( context "context" reflect "reflect" - address "cosmossdk.io/core/address" types "github.com/cosmos/cosmos-sdk/types" gomock "github.com/golang/mock/gomock" ) -// MockAccountKeeper is a mock of AccountKeeper interface. -type MockAccountKeeper struct { - ctrl *gomock.Controller - recorder *MockAccountKeeperMockRecorder -} - -// MockAccountKeeperMockRecorder is the mock recorder for MockAccountKeeper. -type MockAccountKeeperMockRecorder struct { - mock *MockAccountKeeper -} - -// NewMockAccountKeeper creates a new mock instance. -func NewMockAccountKeeper(ctrl *gomock.Controller) *MockAccountKeeper { - mock := &MockAccountKeeper{ctrl: ctrl} - mock.recorder = &MockAccountKeeperMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockAccountKeeper) EXPECT() *MockAccountKeeperMockRecorder { - return m.recorder -} - -// AddressCodec mocks base method. -func (m *MockAccountKeeper) AddressCodec() address.Codec { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "AddressCodec") - ret0, _ := ret[0].(address.Codec) - return ret0 -} - -// AddressCodec indicates an expected call of AddressCodec. -func (mr *MockAccountKeeperMockRecorder) AddressCodec() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddressCodec", reflect.TypeOf((*MockAccountKeeper)(nil).AddressCodec)) -} - -// GetAccount mocks base method. -func (m *MockAccountKeeper) GetAccount(ctx context.Context, addr types.AccAddress) types.AccountI { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetAccount", ctx, addr) - ret0, _ := ret[0].(types.AccountI) - return ret0 -} - -// GetAccount indicates an expected call of GetAccount. -func (mr *MockAccountKeeperMockRecorder) GetAccount(ctx, addr interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAccount", reflect.TypeOf((*MockAccountKeeper)(nil).GetAccount), ctx, addr) -} - -// GetModuleAccount mocks base method. -func (m *MockAccountKeeper) GetModuleAccount(ctx context.Context, moduleName string) types.ModuleAccountI { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetModuleAccount", ctx, moduleName) - ret0, _ := ret[0].(types.ModuleAccountI) - return ret0 -} - -// GetModuleAccount indicates an expected call of GetModuleAccount. -func (mr *MockAccountKeeperMockRecorder) GetModuleAccount(ctx, moduleName interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetModuleAccount", reflect.TypeOf((*MockAccountKeeper)(nil).GetModuleAccount), ctx, moduleName) -} - -// GetModuleAddress mocks base method. -func (m *MockAccountKeeper) GetModuleAddress(moduleName string) types.AccAddress { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetModuleAddress", moduleName) - ret0, _ := ret[0].(types.AccAddress) - return ret0 -} - -// GetModuleAddress indicates an expected call of GetModuleAddress. -func (mr *MockAccountKeeperMockRecorder) GetModuleAddress(moduleName interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetModuleAddress", reflect.TypeOf((*MockAccountKeeper)(nil).GetModuleAddress), moduleName) -} - -// NewAccountWithAddress mocks base method. -func (m *MockAccountKeeper) NewAccountWithAddress(ctx context.Context, addr types.AccAddress) types.AccountI { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "NewAccountWithAddress", ctx, addr) - ret0, _ := ret[0].(types.AccountI) - return ret0 -} - -// NewAccountWithAddress indicates an expected call of NewAccountWithAddress. -func (mr *MockAccountKeeperMockRecorder) NewAccountWithAddress(ctx, addr interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewAccountWithAddress", reflect.TypeOf((*MockAccountKeeper)(nil).NewAccountWithAddress), ctx, addr) -} - -// SetAccount mocks base method. -func (m *MockAccountKeeper) SetAccount(ctx context.Context, acc types.AccountI) { - m.ctrl.T.Helper() - m.ctrl.Call(m, "SetAccount", ctx, acc) -} - -// SetAccount indicates an expected call of SetAccount. -func (mr *MockAccountKeeperMockRecorder) SetAccount(ctx, acc interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetAccount", reflect.TypeOf((*MockAccountKeeper)(nil).SetAccount), ctx, acc) -} - // MockBankKeeper is a mock of BankKeeper interface. type MockBankKeeper struct { ctrl *gomock.Controller