Skip to content

Commit

Permalink
address reviewer comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberbono3 committed Aug 24, 2021
1 parent 9aae756 commit 3d2d5b0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 39 deletions.
2 changes: 1 addition & 1 deletion x/auth/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (ak AccountKeeper) ModuleAccounts(c context.Context, req *types.QueryModule
}

func (ak AccountKeeper) Bech32Prefix(ctx context.Context, req *types.Bech32PrefixRequest) (*types.Bech32PrefixResponse, error) {
bech32Prefix, err := ak.GetBech32Prefix()
bech32Prefix, err := ak.getBech32Prefix()
if err != nil {
return nil, err
}
Expand Down
59 changes: 25 additions & 34 deletions x/auth/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package keeper_test

import (
"bytes"
"fmt"
"context"
"bytes"

"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -194,48 +195,38 @@ func (suite *KeeperTestSuite) TestGRPCQueryParameters() {
}

func (suite *KeeperTestSuite) TestBech32Prefix() {
suite.SetupTest() // reset
req := &types.Bech32PrefixRequest{}
res, err := suite.queryClient.Bech32Prefix(context.Background(), req)
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().Equal(sdk.Bech32MainPrefix, res.Bech32Prefix)
})
suite.SetupTest() // reset
req := &types.Bech32PrefixRequest{}
res, err := suite.queryClient.Bech32Prefix(context.Background(), req)
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().Equal(sdk.Bech32MainPrefix, res.Bech32Prefix)
}

func (suite *KeeperTestSuite) TestAddressBytesToString() {

_, _, addr := testdata.KeyTestPubAddr()
addrBytes := []byte(addr)

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
expPass bool
posttests func(res *types.AddressBytesToStringResponse)
}{
{
"success",
&types.AddressBytesToStringRequest{AddressBytes: addrBytes},
true,
func(res *types.AddressBytesToStringResponse) {
text, err := suite.app.AccountKeeper.GetAddressCdC().BytesToString(addrBytes)
suite.Require().NoError(err)
suite.Require().NotNil(text)
suite.Require().Equal(text, res.AddressString)
},
},
{
"request is empty",
&types.AddressBytesToStringRequest{},
false,
func(res *types.AddressBytesToStringResponse) {},
},
{
"empty account address in request",
&types.AddressBytesToStringRequest{AddressBytes: []byte{}},
false,
func(res *types.AddressBytesToStringResponse) {},
},
}

Expand All @@ -250,48 +241,48 @@ func (suite *KeeperTestSuite) TestAddressBytesToString() {
if tc.expPass {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().Equal(res.AddressString, addrStr)
} else {
suite.Require().Error(err)
suite.Require().Nil(res)
}

tc.posttests(res)
})
}
}


func (suite *KeeperTestSuite) TestAddressStringToBytes() {
_, _, addr := testdata.KeyTestPubAddr()
addrBytes := []byte(addr)
text, err := suite.app.AccountKeeper.GetAddressCdC().BytesToString(addrBytes)
suite.Require().NoError(err)
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
expPass bool
posttests func(res *types.AddressStringToBytesResponse)
}{
{
"success",
&types.AddressStringToBytesRequest{AddressString: text},
&types.AddressStringToBytesRequest{AddressString: addrStr},
true,
func(res *types.AddressStringToBytesResponse) {
suite.Require().True(bytes.Equal(res.AddressBytes, addrBytes))
},
},
{
"request is empty",
&types.AddressStringToBytesRequest{},
false,
func(res *types.AddressStringToBytesResponse) {},
},
{
"AddressString field in request is empty",
&types.AddressStringToBytesRequest{AddressString: ""},
false,
func(res *types.AddressStringToBytesResponse) {},
},
{
"address prefix is incorrect",
&types.AddressStringToBytesRequest{AddressString: "regen13c3d4wq2t22dl0dstraf8jc3f902e3fsy9n3wv" },
false,
},

}

for _, tc := range testCases {
Expand All @@ -305,12 +296,12 @@ func (suite *KeeperTestSuite) TestAddressStringToBytes() {
if tc.expPass {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().True(bytes.Equal(res.AddressBytes, addrBytes))
} else {
suite.Require().Error(err)
suite.Require().Nil(res)
}

tc.posttests(res)
})
}
}
5 changes: 1 addition & 4 deletions x/auth/keeper/keeper.go
Original file line number Diff line number Diff line change
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 @@ -241,9 +241,6 @@ func (ak AccountKeeper) UnmarshalAccount(bz []byte) (types.AccountI, error) {
// GetCodec return codec.Codec object used by the keeper
func (ak AccountKeeper) GetCodec() codec.BinaryCodec { return ak.cdc }

// GetAddressCdc returns address.Codec object used by the keeper
func (ak AccountKeeper) GetAddressCdc() address.Codec { return ak.addressCdC }

// add getter for bech32Prefix
func (ak AccountKeeper) getBech32Prefix() (string, error) {
bech32Codec, ok := ak.addressCdC.(bech32Codec)
Expand Down

0 comments on commit 3d2d5b0

Please sign in to comment.