Skip to content

Commit

Permalink
apply reviewer comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberbono3 committed Aug 25, 2021
1 parent aa76cbc commit fb9e5a3
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 18 deletions.
4 changes: 2 additions & 2 deletions x/auth/keeper/grpc_query.go
Expand Up @@ -129,7 +129,7 @@ func (ak AccountKeeper) AddressBytesToString(ctx context.Context, req *types.Add
return nil, errors.New("empty address bytes is not allowed")
}

text, err := ak.addressCdC.BytesToString(req.AddressBytes)
text, err := ak.addressCdc.BytesToString(req.AddressBytes)
if err != nil {
return nil, err
}
Expand All @@ -146,7 +146,7 @@ func (ak AccountKeeper) AddressStringToBytes(ctx context.Context, req *types.Add
return nil, errors.New("empty address string is not allowed")
}

bz, err := ak.addressCdC.StringToBytes(req.AddressString)
bz, err := ak.addressCdc.StringToBytes(req.AddressString)
if err != nil {
return nil, err
}
Expand Down
100 changes: 86 additions & 14 deletions x/auth/keeper/grpc_query_test.go
Expand Up @@ -10,6 +10,9 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/types"
)

const addrStr = "cosmos13c3d4wq2t22dl0dstraf8jc3f902e3fsy9n3wv"
var addrBytes = []byte{0x8e, 0x22, 0xda, 0xb8, 0xa, 0x5a, 0x94, 0xdf, 0xbd, 0xb0, 0x58, 0xfa, 0x93, 0xcb, 0x11, 0x49, 0x5e, 0xac, 0xc5, 0x30}

func (suite *KeeperTestSuite) TestGRPCQueryAccounts() {
var (
req *types.QueryAccountsRequest
Expand Down Expand Up @@ -194,6 +197,87 @@ func (suite *KeeperTestSuite) TestGRPCQueryParameters() {
}
}

func (suite *KeeperTestSuite) TestGRPCQueryModuleAccounts() {
var (
req *types.QueryModuleAccountsRequest
)

testCases := []struct {
msg string
malleate func()
expPass bool
posttests func(res *types.QueryModuleAccountsResponse)
}{
{
"success",
func() {
req = &types.QueryModuleAccountsRequest{}
},
true,
func(res *types.QueryModuleAccountsResponse) {
var mintModuleExists = false
for _, acc := range res.Accounts {
var account types.AccountI
err := suite.app.InterfaceRegistry().UnpackAny(acc, &account)
suite.Require().NoError(err)

moduleAccount, ok := account.(types.ModuleAccountI)

suite.Require().True(ok)
if moduleAccount.GetName() == "mint" {
mintModuleExists = true
}
}
suite.Require().True(mintModuleExists)
},
},
{
"invalid module name",
func() {
req = &types.QueryModuleAccountsRequest{}
},
true,
func(res *types.QueryModuleAccountsResponse) {
var mintModuleExists = false
for _, acc := range res.Accounts {
var account types.AccountI
err := suite.app.InterfaceRegistry().UnpackAny(acc, &account)
suite.Require().NoError(err)

moduleAccount, ok := account.(types.ModuleAccountI)

suite.Require().True(ok)
if moduleAccount.GetName() == "falseCase" {
mintModuleExists = true
}
}
suite.Require().False(mintModuleExists)
},
},
}

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

tc.malleate()
ctx := sdk.WrapSDKContext(suite.ctx)

res, err := suite.queryClient.ModuleAccounts(ctx, req)

if tc.expPass {
suite.Require().NoError(err)
suite.Require().NotNil(res)
} else {
suite.Require().Error(err)
suite.Require().Nil(res)
}

tc.posttests(res)
})
}
}

func (suite *KeeperTestSuite) TestBech32Prefix() {
suite.SetupTest() // reset
req := &types.Bech32PrefixRequest{}
Expand All @@ -203,11 +287,7 @@ func (suite *KeeperTestSuite) TestBech32Prefix() {
suite.Require().Equal(sdk.Bech32MainPrefix, res.Bech32Prefix)
}


func (suite *KeeperTestSuite) TestAddressBytesToString() {
const addrStr = "cosmos13c3d4wq2t22dl0dstraf8jc3f902e3fsy9n3wv"
addrBytes := []byte{0x8e, 0x22, 0xda, 0xb8, 0xa, 0x5a, 0x94, 0xdf, 0xbd, 0xb0, 0x58, 0xfa, 0x93, 0xcb, 0x11, 0x49, 0x5e, 0xac, 0xc5, 0x30}

testCases := []struct {
msg string
req *types.AddressBytesToStringRequest
Expand All @@ -234,8 +314,6 @@ func (suite *KeeperTestSuite) TestAddressBytesToString() {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset

ctx := sdk.WrapSDKContext(suite.ctx)

res, err := suite.queryClient.AddressBytesToString(context.Background(), tc.req)

if tc.expPass {
Expand All @@ -251,11 +329,7 @@ func (suite *KeeperTestSuite) TestAddressBytesToString() {
}
}


func (suite *KeeperTestSuite) TestAddressStringToBytes() {
const addrStr = "cosmos13c3d4wq2t22dl0dstraf8jc3f902e3fsy9n3wv"
addrBytes := []byte{0x8e, 0x22, 0xda, 0xb8, 0xa, 0x5a, 0x94, 0xdf, 0xbd, 0xb0, 0x58, 0xfa, 0x93, 0xcb, 0x11, 0x49, 0x5e, 0xac, 0xc5, 0x30}

testCases := []struct {
msg string
req *types.AddressStringToBytesRequest
Expand Down Expand Up @@ -288,9 +362,7 @@ func (suite *KeeperTestSuite) TestAddressStringToBytes() {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset

ctx := sdk.WrapSDKContext(suite.ctx)

res, err := suite.queryClient.AddressStringToBytes(ctx, tc.req)
res, err := suite.queryClient.AddressStringToBytes(context.Background(), tc.req)

if tc.expPass {
suite.Require().NoError(err)
Expand All @@ -303,4 +375,4 @@ func (suite *KeeperTestSuite) TestAddressStringToBytes() {

})
}
}
}
4 changes: 2 additions & 2 deletions x/auth/keeper/keeper.go
Expand Up @@ -57,7 +57,7 @@ type AccountKeeper struct {

// The prototypical AccountI constructor.
proto func() types.AccountI
addressCdC address.Codec
addressCdc address.Codec
}

var _ AccountKeeperI = &AccountKeeper{}
Expand Down Expand Up @@ -243,7 +243,7 @@ func (ak AccountKeeper) GetCodec() codec.BinaryCodec { return ak.cdc }

// add getter for bech32Prefix
func (ak AccountKeeper) getBech32Prefix() (string, error) {
bech32Codec, ok := ak.addressCdC.(bech32Codec)
bech32Codec, ok := ak.addressCdc.(bech32Codec)
if !ok {
return "", errors.New("unable cast addressCdc to bech32Codec")
}
Expand Down

0 comments on commit fb9e5a3

Please sign in to comment.