Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

refactor: x/bank audit changes #16690

Merged
merged 8 commits into from
Jun 27, 2023
2 changes: 1 addition & 1 deletion proto/cosmos/bank/module/v1/module.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ message Module {
go_import: "github.com/cosmos/cosmos-sdk/x/bank"
};

// blocked_module_accounts configures exceptional module accounts which should be blocked from receiving funds.
// blocked_module_accounts_override configures exceptional module accounts which should be blocked from receiving funds.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should re-generate the protos.

// If left empty it defaults to the list of account names supplied in the auth module configuration as
// module_account_permissions
repeated string blocked_module_accounts_override = 1;
Expand Down
1 change: 1 addition & 0 deletions proto/cosmos/bank/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ message QueryParamsRequest {}

// QueryParamsResponse defines the response type for querying x/bank parameters.
message QueryParamsResponse {
// params provides the parameters of the bank module.
Params params = 1 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
}

Expand Down
1 change: 1 addition & 0 deletions proto/cosmos/bank/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ message MsgSetSendEnabled {
option (cosmos.msg.v1.signer) = "authority";
option (amino.name) = "cosmos-sdk/MsgSetSendEnabled";

// authority is the address that controls the module.
string authority = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// send_enabled is the list of entries to add or update.
Expand Down
2 changes: 1 addition & 1 deletion x/bank/client/cli/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type CLITestSuite struct {
baseCtx client.Context
}

func TestMigrateTestSuite(t *testing.T) {
func TestCLITestSuite(t *testing.T) {
suite.Run(t, new(CLITestSuite))
}

Expand Down
8 changes: 4 additions & 4 deletions x/bank/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,10 @@ func (suite *KeeperTestSuite) TestQueryParams() {
suite.Require().Equal(suite.bankKeeper.GetParams(suite.ctx), res.GetParams())
}

func (suite *KeeperTestSuite) QueryDenomsMetadataRequest() {
func (suite *KeeperTestSuite) TestQueryDenomsMetadataRequest() {
var (
req *types.QueryDenomsMetadataRequest
expMetadata = []types.Metadata{}
expMetadata = []types.Metadata(nil)
)

testCases := []struct {
Expand Down Expand Up @@ -376,7 +376,7 @@ func (suite *KeeperTestSuite) QueryDenomsMetadataRequest() {
}
}

func (suite *KeeperTestSuite) QueryDenomMetadataRequest() {
func (suite *KeeperTestSuite) TestQueryDenomMetadataRequest() {
var (
req *types.QueryDenomMetadataRequest
expMetadata = types.Metadata{}
Expand Down Expand Up @@ -406,7 +406,7 @@ func (suite *KeeperTestSuite) QueryDenomMetadataRequest() {
{
"success",
func() {
expMetadata := types.Metadata{
expMetadata = types.Metadata{
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{
Expand Down
4 changes: 4 additions & 0 deletions x/bank/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
// AllInvariants runs all invariants of the X/bank module.
func AllInvariants(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
res, stop := NonnegativeBalanceInvariant(k)(ctx)
if stop {
return res, stop
}
return TotalSupply(k)(ctx)
}
}
Expand Down
73 changes: 72 additions & 1 deletion x/bank/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const (

var (
holderAcc = authtypes.NewEmptyModuleAccount(holder)
burnerAcc = authtypes.NewEmptyModuleAccount(authtypes.Burner, authtypes.Burner)
burnerAcc = authtypes.NewEmptyModuleAccount(authtypes.Burner, authtypes.Burner, authtypes.Staking)
minterAcc = authtypes.NewEmptyModuleAccount(authtypes.Minter, authtypes.Minter)
mintAcc = authtypes.NewEmptyModuleAccount(banktypes.MintModuleName, authtypes.Minter)
multiPermAcc = authtypes.NewEmptyModuleAccount(multiPerm, authtypes.Burner, authtypes.Minter, authtypes.Staking)
Expand Down Expand Up @@ -218,6 +218,16 @@ func (suite *KeeperTestSuite) mockSpendableCoins(ctx sdk.Context, acc sdk.Accoun
suite.authKeeper.EXPECT().GetAccount(ctx, acc.GetAddress()).Return(acc)
}

func (suite *KeeperTestSuite) mockDelegateCoinsFromAccountToModule(acc *authtypes.BaseAccount, moduleAcc *authtypes.ModuleAccount) {
suite.authKeeper.EXPECT().GetModuleAccount(suite.ctx, moduleAcc.Name).Return(moduleAcc)
suite.mockDelegateCoins(suite.ctx, acc, moduleAcc)
}

func (suite *KeeperTestSuite) mockUndelegateCoinsFromModuleToAccount(moduleAcc *authtypes.ModuleAccount, accAddr *authtypes.BaseAccount) {
suite.authKeeper.EXPECT().GetModuleAccount(suite.ctx, moduleAcc.Name).Return(moduleAcc)
suite.mockUnDelegateCoins(suite.ctx, accAddr, moduleAcc)
}

func (suite *KeeperTestSuite) mockDelegateCoins(ctx context.Context, acc, mAcc sdk.AccountI) {
vacc, ok := acc.(banktypes.VestingAccount)
if ok {
Expand Down Expand Up @@ -314,6 +324,67 @@ func (suite *KeeperTestSuite) TestSendCoinsFromModuleToAccount_Blocklist() {
))
}

func (suite *KeeperTestSuite) TestSupply_DelegateUndelegateCoins() {
ctx := suite.ctx
require := suite.Require()
authKeeper, keeper := suite.authKeeper, suite.bankKeeper

// set initial balances
suite.mockMintCoins(mintAcc)
require.NoError(keeper.MintCoins(ctx, banktypes.MintModuleName, initCoins))

suite.mockSendCoinsFromModuleToAccount(mintAcc, holderAcc.GetAddress())
require.NoError(keeper.SendCoinsFromModuleToAccount(ctx, banktypes.MintModuleName, holderAcc.GetAddress(), initCoins))

authKeeper.EXPECT().GetModuleAddress("").Return(nil)
require.Panics(func() {
_ = keeper.SendCoinsFromModuleToAccount(ctx, "", holderAcc.GetAddress(), initCoins) //nolint:errcheck // we're testing for a panic, not an error
})

authKeeper.EXPECT().GetModuleAddress(burnerAcc.Name).Return(burnerAcc.GetAddress())
authKeeper.EXPECT().GetModuleAccount(ctx, "").Return(nil)
require.Panics(func() {
_ = keeper.SendCoinsFromModuleToModule(ctx, authtypes.Burner, "", initCoins) //nolint:errcheck // we're testing for a panic, not an error
})

authKeeper.EXPECT().GetModuleAddress("").Return(nil)
require.Panics(func() {
_ = keeper.SendCoinsFromModuleToAccount(ctx, "", baseAcc.GetAddress(), initCoins) //nolint:errcheck // we're testing for a panic, not an error
})

authKeeper.EXPECT().GetModuleAddress(holderAcc.Name).Return(holderAcc.GetAddress())
authKeeper.EXPECT().GetAccount(suite.ctx, holderAcc.GetAddress()).Return(holderAcc)
require.Error(
keeper.SendCoinsFromModuleToAccount(ctx, holderAcc.GetName(), baseAcc.GetAddress(), initCoins.Add(initCoins...)),
)
suite.mockSendCoinsFromModuleToModule(holderAcc, burnerAcc)
require.NoError(
keeper.SendCoinsFromModuleToModule(ctx, holderAcc.GetName(), authtypes.Burner, initCoins),
)

require.Equal(sdk.NewCoins(), keeper.GetAllBalances(ctx, holderAcc.GetAddress()))
require.Equal(initCoins, keeper.GetAllBalances(ctx, burnerAcc.GetAddress()))

suite.mockSendCoinsFromModuleToAccount(burnerAcc, baseAcc.GetAddress())
require.NoError(
keeper.SendCoinsFromModuleToAccount(ctx, authtypes.Burner, baseAcc.GetAddress(), initCoins),
)
require.Equal(sdk.NewCoins(), keeper.GetAllBalances(ctx, burnerAcc.GetAddress()))
require.Equal(initCoins, keeper.GetAllBalances(ctx, baseAcc.GetAddress()))

suite.mockDelegateCoinsFromAccountToModule(baseAcc, burnerAcc)

sdkCtx := sdk.UnwrapSDKContext(ctx)
require.NoError(keeper.DelegateCoinsFromAccountToModule(sdkCtx, baseAcc.GetAddress(), authtypes.Burner, initCoins))
require.Equal(sdk.NewCoins(), keeper.GetAllBalances(ctx, baseAcc.GetAddress()))
require.Equal(initCoins, keeper.GetAllBalances(ctx, burnerAcc.GetAddress()))

suite.mockUndelegateCoinsFromModuleToAccount(burnerAcc, baseAcc)
require.NoError(keeper.UndelegateCoinsFromModuleToAccount(sdkCtx, authtypes.Burner, baseAcc.GetAddress(), initCoins))
require.Equal(sdk.NewCoins(), keeper.GetAllBalances(ctx, burnerAcc.GetAddress()))
require.Equal(initCoins, keeper.GetAllBalances(ctx, baseAcc.GetAddress()))
}

func (suite *KeeperTestSuite) TestSupply_SendCoins() {
ctx := suite.ctx
require := suite.Require()
Expand Down