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 (backport #16690) #16724

Merged
merged 4 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/cosmos/bank/module/v1/module.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/cosmos/bank/v1beta1/query.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/cosmos/bank/v1beta1/tx.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.
// 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 @@ -275,10 +275,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 @@ -377,7 +377,7 @@ func (suite *KeeperTestSuite) QueryDenomsMetadataRequest() {
}
}

func (suite *KeeperTestSuite) QueryDenomMetadataRequest() {
func (suite *KeeperTestSuite) TestQueryDenomMetadataRequest() {
var (
req *types.QueryDenomMetadataRequest
expMetadata = types.Metadata{}
Expand Down Expand Up @@ -407,7 +407,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 @@ -50,7 +50,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(minttypes.ModuleName, authtypes.Minter)
multiPermAcc = authtypes.NewEmptyModuleAccount(multiPerm, authtypes.Burner, authtypes.Minter, authtypes.Staking)
Expand Down Expand Up @@ -220,6 +220,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 @@ -316,6 +326,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
24 changes: 0 additions & 24 deletions x/bank/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,6 @@ func NewMsgMultiSend(in Input, out []Output) *MsgMultiSend {
return &MsgMultiSend{Inputs: []Input{in}, Outputs: out}
}

// GetSigners Implements Msg.
func (msg MsgMultiSend) GetSigners() []sdk.AccAddress {
// should not happen as ValidateBasic would have failed
if len(msg.Inputs) == 0 {
return nil
}

addrs, _ := sdk.AccAddressFromBech32(msg.Inputs[0].Address)
return []sdk.AccAddress{addrs}
}

// GetSigners returns the signer addresses that are expected to sign the result
// of GetSignBytes.
func (msg MsgUpdateParams) GetSigners() []sdk.AccAddress {
authority, _ := sdk.AccAddressFromBech32(msg.Authority)
return []sdk.AccAddress{authority}
}

// NewMsgSetSendEnabled Construct a message to set one or more SendEnabled entries.
func NewMsgSetSendEnabled(authority string, sendEnabled []*SendEnabled, useDefaultFor []string) *MsgSetSendEnabled {
return &MsgSetSendEnabled{
Expand All @@ -52,9 +34,3 @@ func NewMsgSetSendEnabled(authority string, sendEnabled []*SendEnabled, useDefau
UseDefaultFor: useDefaultFor,
}
}

// GetSigners returns the expected signers for MsgSoftwareUpgrade.
func (msg MsgSetSendEnabled) GetSigners() []sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(msg.Authority)
return []sdk.AccAddress{addr}
}
16 changes: 6 additions & 10 deletions x/bank/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
<<<<<<< HEAD

Check failure on line 12 in x/bank/types/msgs_test.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing import path

Check failure on line 12 in x/bank/types/msgs_test.go

View workflow job for this annotation

GitHub Actions / dependency-review

missing import path

Check failure on line 12 in x/bank/types/msgs_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: missing import path

Check failure on line 12 in x/bank/types/msgs_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

missing import path (typecheck)

Check failure on line 12 in x/bank/types/msgs_test.go

View workflow job for this annotation

GitHub Actions / split-test-files

missing import path
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
=======

Check failure on line 15 in x/bank/types/msgs_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: missing import path

Check failure on line 15 in x/bank/types/msgs_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

missing import path (typecheck)
>>>>>>> 7c8236d2e (refactor: x/bank audit changes (#16690))

Check failure on line 16 in x/bank/types/msgs_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

invalid character U+0023 '#'

Check failure on line 16 in x/bank/types/msgs_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected ) after top level declaration
)

func TestMsgSendGetSignBytes(t *testing.T) {
Expand Down Expand Up @@ -122,16 +125,6 @@
require.Equal(t, expected, string(res))
}

func TestMsgMultiSendGetSigners(t *testing.T) {
addr := sdk.AccAddress([]byte("input111111111111111"))
input := NewInput(addr, nil)
msg := NewMsgMultiSend(input, nil)

res := msg.GetSigners()
require.Equal(t, 1, len(res))
require.True(t, addr.Equals(res[0]))
}

func TestNewMsgSetSendEnabled(t *testing.T) {
// Punt. Just setting one to all non-default values and making sure they're as expected.
msg := NewMsgSetSendEnabled("milton", []*SendEnabled{{"barrycoin", true}}, []string{"billcoin"})
Expand Down Expand Up @@ -161,6 +154,7 @@
actual := string(actualBz)
assert.Equal(t, expected, actual)
}
<<<<<<< HEAD

Check failure on line 157 in x/bank/types/msgs_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: non-declaration statement outside function body

func TestMsgSetSendEnabledGetSigners(t *testing.T) {
govModuleAddr := authtypes.NewModuleAddress(govtypes.ModuleName)
Expand All @@ -169,3 +163,5 @@
actual := msg.GetSigners()
assert.Equal(t, expected, actual)
}
=======

Check failure on line 166 in x/bank/types/msgs_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: non-declaration statement outside function body
>>>>>>> 7c8236d2e (refactor: x/bank audit changes (#16690))

Check failure on line 167 in x/bank/types/msgs_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

invalid character U+0023 '#' (typecheck)
1 change: 1 addition & 0 deletions x/bank/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x/bank/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading