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 QA (backport #21459) #21463

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 3 additions & 15 deletions x/bank/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
sdkmath "cosmossdk.io/math"
"cosmossdk.io/x/bank/types"

codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
)
Expand Down Expand Up @@ -50,25 +49,14 @@ func (suite *KeeperTestSuite) TestExportGenesis() {
}

func (suite *KeeperTestSuite) getTestBalancesAndSupply() ([]types.Balance, sdk.Coins) {
ac := codectestutil.CodecOptions{}.GetAddressCodec()
addr2, err := suite.authKeeper.AddressCodec().StringToBytes("cosmos1f9xjhxm0plzrh9cskf4qee4pc2xwp0n0556gh0")
suite.Require().NoError(err)
addr1, _ := suite.authKeeper.AddressCodec().StringToBytes("cosmos1t5u0jfg3ljsjrh2m9e47d4ny2hea7eehxrzdgd")
suite.Require().NoError(err)
addr1Balance := sdk.Coins{sdk.NewInt64Coin("testcoin3", 10)}
addr2Balance := sdk.Coins{sdk.NewInt64Coin("testcoin1", 32), sdk.NewInt64Coin("testcoin2", 34)}

totalSupply := addr1Balance
totalSupply = totalSupply.Add(addr2Balance...)

addr2Str, err := ac.BytesToString(addr2)
suite.Require().NoError(err)
addr1Str, err := ac.BytesToString(addr1)
suite.Require().NoError(err)
totalSupply := addr1Balance.Add(addr2Balance...)

return []types.Balance{
{Address: addr2Str, Coins: addr2Balance},
{Address: addr1Str, Coins: addr1Balance},
{Address: "cosmos1f9xjhxm0plzrh9cskf4qee4pc2xwp0n0556gh0", Coins: addr2Balance},
{Address: "cosmos1t5u0jfg3ljsjrh2m9e47d4ny2hea7eehxrzdgd", Coins: addr1Balance},
}, totalSupply
}

Expand Down
102 changes: 98 additions & 4 deletions x/bank/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"fmt"
"time"

v1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
"cosmossdk.io/core/header"
authtypes "cosmossdk.io/x/auth/types"
vestingtypes "cosmossdk.io/x/auth/vesting/types"
"cosmossdk.io/x/bank/testutil"
"cosmossdk.io/x/bank/types"

codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
Expand All @@ -21,7 +21,7 @@ func (suite *KeeperTestSuite) TestQueryBalance() {
ctx, queryClient := suite.ctx, suite.queryClient
_, _, addr := testdata.KeyTestPubAddr()

addrStr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(addr)
addrStr, err := suite.authKeeper.AddressCodec().BytesToString(addr)
suite.Require().NoError(err)

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

addrStr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(addr)
addrStr, err := suite.authKeeper.AddressCodec().BytesToString(addr)
suite.Require().NoError(err)

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

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

ctx := sdk.UnwrapSDKContext(suite.ctx)
Expand Down Expand Up @@ -598,6 +598,100 @@ func (suite *KeeperTestSuite) TestQueryDenomMetadataByQueryStringRequest() {
}
}

func (suite *KeeperTestSuite) TestGRPCDenomMetadataV2() {
var (
req *v1beta1.QueryDenomMetadataRequest
metadata = types.Metadata{
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{
Denom: "uatom",
Exponent: 0,
Aliases: []string{"microatom"},
},
{
Denom: "atom",
Exponent: 6,
Aliases: []string{"ATOM"},
},
},
Base: "uatom",
Display: "atom",
}
expMetadata = &v1beta1.Metadata{}
)

testCases := []struct {
msg string
malleate func()
expPass bool
}{
{
"empty denom",
func() {
req = &v1beta1.QueryDenomMetadataRequest{}
},
false,
},
{
"not found denom",
func() {
req = &v1beta1.QueryDenomMetadataRequest{
Denom: "foo",
}
},
false,
},
{
"success",
func() {
expMetadata = &v1beta1.Metadata{
Description: metadata.Description,
DenomUnits: []*v1beta1.DenomUnit{
{
Denom: metadata.DenomUnits[0].Denom,
Exponent: metadata.DenomUnits[0].Exponent,
Aliases: metadata.DenomUnits[0].Aliases,
},
{
Denom: metadata.DenomUnits[1].Denom,
Exponent: metadata.DenomUnits[1].Exponent,
Aliases: metadata.DenomUnits[1].Aliases,
},
},
Base: metadata.Base,
Display: metadata.Display,
}

suite.bankKeeper.SetDenomMetaData(suite.ctx, metadata)
req = &v1beta1.QueryDenomMetadataRequest{
Denom: expMetadata.Base,
}
},
true,
},
}

for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset

tc.malleate()
ctx := suite.ctx

res, err := suite.bankKeeper.DenomMetadataV2(ctx, req)

if tc.expPass {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().Equal(expMetadata, res.Metadata)
} else {
suite.Require().Error(err)
}
})
}
}

func (suite *KeeperTestSuite) TestGRPCDenomOwners() {
ctx := suite.ctx

Expand Down
2 changes: 1 addition & 1 deletion x/bank/simulation/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func ProposalMsgs() []simtypes.WeightedProposalMsg {
// SimulateMsgUpdateParams returns a random MsgUpdateParams
func SimulateMsgUpdateParams(_ context.Context, r *rand.Rand, _ []simtypes.Account, ac coreaddress.Codec) (sdk.Msg, error) {
// use the default gov module account address as authority
authority, err := ac.BytesToString(address.Module("gov"))
authority, err := ac.BytesToString(address.Module(types.GovModuleName))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/bank/simulation/proposals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestProposalMsgs(t *testing.T) {
msgUpdateParams, ok := msg.(*types.MsgUpdateParams)
assert.Assert(t, ok)

authority, err := ac.BytesToString(address.Module("gov"))
authority, err := ac.BytesToString(address.Module(types.GovModuleName))
assert.NilError(t, err)
assert.Equal(t, authority, msgUpdateParams.Authority)
assert.Assert(t, len(msgUpdateParams.Params.SendEnabled) == 0) //nolint:staticcheck // we're testing the old way here
Expand Down
Loading