refactor(x/bank): rm cyclic deps with gov, staking, distr (#16578)

Co-authored-by: Marko <marbar3778@yahoo.com>
Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com>
This commit is contained in:
Matt Kocubinski 2023-06-16 15:36:42 -05:00 committed by GitHub
parent f51992d0c4
commit c2a07cea27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 41 additions and 35 deletions

View File

@ -399,7 +399,7 @@ func TestMsgSetSendEnabled(t *testing.T) {
"invalid authority",
"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
addr1Str,
"expected gov account as only signer for proposal message",
"invalid signer",
},
},
{

View File

@ -15,6 +15,7 @@ import (
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"
_ "github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

View File

@ -6,7 +6,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/bank/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
)
func (suite *KeeperTestSuite) TestExportGenesis() {
@ -30,11 +29,11 @@ func (suite *KeeperTestSuite) TestExportGenesis() {
suite.mockMintCoins(mintAcc)
suite.
Require().
NoError(suite.bankKeeper.MintCoins(ctx, minttypes.ModuleName, expectedBalances[i].Coins))
NoError(suite.bankKeeper.MintCoins(ctx, types.MintModuleName, expectedBalances[i].Coins))
suite.mockSendCoinsFromModuleToAccount(mintAcc, accAddr)
suite.
Require().
NoError(suite.bankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, accAddr, expectedBalances[i].Coins))
NoError(suite.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.MintModuleName, accAddr, expectedBalances[i].Coins))
}
suite.Require().NoError(suite.bankKeeper.SetParams(ctx, types.DefaultParams()))

View File

@ -12,7 +12,6 @@ import (
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
"github.com/cosmos/cosmos-sdk/x/bank/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
)
func (suite *KeeperTestSuite) TestQueryBalance() {
@ -235,7 +234,7 @@ func (suite *KeeperTestSuite) TestQueryTotalSupply() {
suite.mockMintCoins(mintAcc)
suite.
Require().
NoError(suite.bankKeeper.MintCoins(ctx, minttypes.ModuleName, testCoins))
NoError(suite.bankKeeper.MintCoins(ctx, types.MintModuleName, testCoins))
res, err = queryClient.TotalSupply(gocontext.Background(), &types.QueryTotalSupplyRequest{})
suite.Require().NoError(err)
@ -256,7 +255,7 @@ func (suite *KeeperTestSuite) TestQueryTotalSupplyOf() {
suite.mockMintCoins(mintAcc)
suite.
Require().
NoError(suite.bankKeeper.MintCoins(ctx, minttypes.ModuleName, expectedTotalSupply))
NoError(suite.bankKeeper.MintCoins(ctx, types.MintModuleName, expectedTotalSupply))
_, err := queryClient.SupplyOf(gocontext.Background(), &types.QuerySupplyOfRequest{})
suite.Require().Error(err)
@ -460,7 +459,7 @@ func (suite *KeeperTestSuite) TestGRPCDenomOwners() {
keeper := suite.bankKeeper
suite.mockMintCoins(mintAcc)
suite.Require().NoError(keeper.MintCoins(ctx, minttypes.ModuleName, initCoins))
suite.Require().NoError(keeper.MintCoins(ctx, types.MintModuleName, initCoins))
for i := 0; i < 10; i++ {
addr := sdk.AccAddress(fmt.Sprintf("account-%d", i))
@ -470,7 +469,7 @@ func (suite *KeeperTestSuite) TestGRPCDenomOwners() {
sdk.TokensFromConsensusPower(initialPower/10, sdk.DefaultPowerReduction),
))
suite.mockSendCoinsFromModuleToAccount(mintAcc, addr)
suite.Require().NoError(keeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, bal))
suite.Require().NoError(keeper.SendCoinsFromModuleToAccount(ctx, types.MintModuleName, addr, bal))
}
testCases := map[string]struct {

View File

@ -32,8 +32,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
)
const (
@ -52,7 +50,7 @@ var (
holderAcc = authtypes.NewEmptyModuleAccount(holder)
burnerAcc = authtypes.NewEmptyModuleAccount(authtypes.Burner, authtypes.Burner)
minterAcc = authtypes.NewEmptyModuleAccount(authtypes.Minter, authtypes.Minter)
mintAcc = authtypes.NewEmptyModuleAccount(minttypes.ModuleName, authtypes.Minter)
mintAcc = authtypes.NewEmptyModuleAccount(banktypes.MintModuleName, authtypes.Minter)
multiPermAcc = authtypes.NewEmptyModuleAccount(multiPerm, authtypes.Burner, authtypes.Minter, authtypes.Staking)
baseAcc = authtypes.NewBaseAccountWithAddress(sdk.AccAddress([]byte("baseAcc")))
@ -144,7 +142,7 @@ func (suite *KeeperTestSuite) SetupTest() {
storeService,
suite.authKeeper,
map[string]bool{accAddrs[4].String(): true},
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
authtypes.NewModuleAddress(banktypes.GovModuleName).String(),
log.NewNopLogger(),
)
@ -254,8 +252,8 @@ func (suite *KeeperTestSuite) TestGetAuthority() {
tests := map[string]string{
"some random account": "cosmos139f7kncmglres2nf3h4hc4tade85ekfr8sulz5",
"gov module account": authtypes.NewModuleAddress(govtypes.ModuleName).String(),
"another module account": authtypes.NewModuleAddress(minttypes.ModuleName).String(),
"gov module account": authtypes.NewModuleAddress(banktypes.GovModuleName).String(),
"another module account": authtypes.NewModuleAddress(banktypes.MintModuleName).String(),
}
for name, expected := range tests {
@ -308,11 +306,11 @@ func (suite *KeeperTestSuite) TestSendCoinsFromModuleToAccount_Blocklist() {
keeper := suite.bankKeeper
suite.mockMintCoins(mintAcc)
require.NoError(keeper.MintCoins(ctx, minttypes.ModuleName, initCoins))
require.NoError(keeper.MintCoins(ctx, banktypes.MintModuleName, initCoins))
suite.authKeeper.EXPECT().GetModuleAddress(mintAcc.Name).Return(mintAcc.GetAddress())
require.Error(keeper.SendCoinsFromModuleToAccount(
ctx, minttypes.ModuleName, accAddrs[4], initCoins,
ctx, banktypes.MintModuleName, accAddrs[4], initCoins,
))
}
@ -323,10 +321,10 @@ func (suite *KeeperTestSuite) TestSupply_SendCoins() {
// set initial balances
suite.mockMintCoins(mintAcc)
require.NoError(keeper.MintCoins(ctx, minttypes.ModuleName, initCoins))
require.NoError(keeper.MintCoins(ctx, banktypes.MintModuleName, initCoins))
suite.mockSendCoinsFromModuleToAccount(mintAcc, holderAcc.GetAddress())
require.NoError(keeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, holderAcc.GetAddress(), initCoins))
require.NoError(keeper.SendCoinsFromModuleToAccount(ctx, banktypes.MintModuleName, holderAcc.GetAddress(), initCoins))
authKeeper.EXPECT().GetModuleAddress("").Return(nil)
require.Panics(func() {

View File

@ -11,7 +11,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)
type msgServer struct {
@ -133,7 +132,7 @@ func (k msgServer) MultiSend(goCtx context.Context, msg *types.MsgMultiSend) (*t
func (k msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if k.GetAuthority() != req.Authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.GetAuthority(), req.Authority)
return nil, errorsmod.Wrapf(types.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.GetAuthority(), req.Authority)
}
if err := req.Params.Validate(); err != nil {
@ -150,7 +149,7 @@ func (k msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParam
func (k msgServer) SetSendEnabled(goCtx context.Context, msg *types.MsgSetSendEnabled) (*types.MsgSetSendEnabledResponse, error) {
if k.GetAuthority() != msg.Authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.GetAuthority(), msg.Authority)
return nil, errorsmod.Wrapf(types.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.GetAuthority(), msg.Authority)
}
seen := map[string]bool{}

View File

@ -4,10 +4,9 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)
var govAcc = authtypes.NewEmptyModuleAccount(govtypes.ModuleName, authtypes.Minter)
var govAcc = authtypes.NewEmptyModuleAccount(banktypes.GovModuleName, authtypes.Minter)
func (suite *KeeperTestSuite) TestMsgUpdateParams() {
// default params

View File

@ -31,7 +31,6 @@ import (
v1bank "github.com/cosmos/cosmos-sdk/x/bank/migrations/v1"
"github.com/cosmos/cosmos-sdk/x/bank/simulation"
"github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)
// ConsensusVersion defines the current x/bank module consensus version.
@ -249,7 +248,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
}
// default to governance authority if not provided
authority := authtypes.NewModuleAddress(govtypes.ModuleName)
authority := authtypes.NewModuleAddress(types.GovModuleName)
if in.Config.Authority != "" {
authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority)
}

View File

@ -12,7 +12,6 @@ import (
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/types"
disttypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/cosmos/cosmos-sdk/x/simulation"
)
@ -22,6 +21,8 @@ const (
OpWeightMsgMultiSend = "op_weight_msg_multisend"
DefaultWeightMsgSend = 100 // from simappparams.DefaultWeightMsgSend
DefaultWeightMsgMultiSend = 10 // from simappparams.DefaultWeightMsgMultiSend
distributionModuleName = "distribution"
)
// WeightedOperations returns all the operations from the module with their respective weights
@ -420,7 +421,7 @@ func getModuleAccounts(ak types.AccountKeeper, ctx sdk.Context, moduleAccCount i
moduleAccounts := make([]simtypes.Account, moduleAccCount)
for i := 0; i < moduleAccCount; i++ {
acc := ak.GetModuleAccount(ctx, disttypes.ModuleName)
acc := ak.GetModuleAccount(ctx, distributionModuleName)
mAcc := simtypes.Account{
Address: acc.GetAddress(),
PrivKey: nil,

View File

@ -5,7 +5,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)
// FundAccount is a utility function that funds an account by minting and
@ -15,11 +15,11 @@ import (
// TODO: Instead of using the mint module account, which has the
// permission of minting, create a "faucet" account. (@fdymylja)
func FundAccount(ctx context.Context, bankKeeper bankkeeper.Keeper, addr sdk.AccAddress, amounts sdk.Coins) error {
if err := bankKeeper.MintCoins(ctx, minttypes.ModuleName, amounts); err != nil {
if err := bankKeeper.MintCoins(ctx, types.MintModuleName, amounts); err != nil {
return err
}
return bankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, amounts)
return bankKeeper.SendCoinsFromModuleToAccount(ctx, types.MintModuleName, addr, amounts)
}
// FundModuleAccount is a utility function that funds a module account by
@ -29,9 +29,9 @@ func FundAccount(ctx context.Context, bankKeeper bankkeeper.Keeper, addr sdk.Acc
// TODO: Instead of using the mint module account, which has the
// permission of minting, create a "faucet" account. (@fdymylja)
func FundModuleAccount(ctx context.Context, bankKeeper bankkeeper.Keeper, recipientMod string, amounts sdk.Coins) error {
if err := bankKeeper.MintCoins(ctx, minttypes.ModuleName, amounts); err != nil {
if err := bankKeeper.MintCoins(ctx, types.MintModuleName, amounts); err != nil {
return err
}
return bankKeeper.SendCoinsFromModuleToModule(ctx, minttypes.ModuleName, recipientMod, amounts)
return bankKeeper.SendCoinsFromModuleToModule(ctx, types.MintModuleName, recipientMod, amounts)
}

View File

@ -12,4 +12,5 @@ var (
ErrInvalidKey = errors.Register(ModuleName, 7, "invalid key")
ErrDuplicateEntry = errors.Register(ModuleName, 8, "duplicate entry")
ErrMultipleSenders = errors.Register(ModuleName, 9, "multiple senders not allowed")
ErrInvalidSigner = errors.Register(ModuleName, 10, "invalid signer")
)

View File

@ -4,6 +4,7 @@ import (
"cosmossdk.io/collections"
collcodec "cosmossdk.io/collections/codec"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@ -16,6 +17,16 @@ const (
// RouterKey defines the module's message routing key
RouterKey = ModuleName
// GovModuleName duplicates the gov module's name to avoid a cyclic dependency with x/gov.
// It should be synced with the gov module's name if it is ever changed.
// See: https://github.com/cosmos/cosmos-sdk/blob/b62a28aac041829da5ded4aeacfcd7a42873d1c8/x/gov/types/keys.go#L9
GovModuleName = "gov"
// MintModuleName duplicates the mint module's name to avoid a cyclic dependency with x/mint.
// It should be synced with the mint module's name if it is ever changed.
// See: https://github.com/cosmos/cosmos-sdk/blob/0e34478eb7420b69869ed50f129fc274a97a9b06/x/mint/types/keys.go#L13
MintModuleName = "mint"
)
// KVStore keys

View File

@ -10,7 +10,6 @@ import (
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)
func TestMsgSendGetSignBytes(t *testing.T) {
@ -163,7 +162,7 @@ func TestMsgSetSendEnabledGetSignBytes(t *testing.T) {
}
func TestMsgSetSendEnabledGetSigners(t *testing.T) {
govModuleAddr := authtypes.NewModuleAddress(govtypes.ModuleName)
govModuleAddr := authtypes.NewModuleAddress(GovModuleName)
msg := NewMsgSetSendEnabled(govModuleAddr.String(), nil, nil)
expected := []sdk.AccAddress{govModuleAddr}
actual := msg.GetSigners()