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

chore: code coverage and minor cleanup #440

Merged
merged 1 commit into from
Sep 27, 2021
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
2 changes: 1 addition & 1 deletion modules/apps/27-interchain-accounts/keeper/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (k Keeper) InitInterchainAccount(ctx sdk.Context, connectionID, counterpart
return err
}

if k.IsBound(ctx, portID) {
if k.portKeeper.IsBound(ctx, portID) {
Copy link
Member Author

@damiannolan damiannolan Sep 24, 2021

Choose a reason for hiding this comment

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

Based off of #288, is this the intended usage? @seantking @colin-axner

I've added IsBound to expected keeper interface and re-enabled the previously commented out test.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes it is 👍 Thanks!

return sdkerrors.Wrap(types.ErrPortAlreadyBound, portID)
}

Expand Down
29 changes: 15 additions & 14 deletions modules/apps/27-interchain-accounts/keeper/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,28 @@ func (suite *KeeperTestSuite) TestInitInterchainAccount() {
{
"success", func() {}, true,
},
/*
// TODO: https://github.com/cosmos/ibc-go/issues/288
{
"port is already bound", func() {
// mock init interchain account
portID := suite.chainA.GetSimApp().ICAKeeper.GeneratePortId(owner, path.EndpointA.ConnectionID)
suite.chainA.GetSimApp().IBCKeeper.PortKeeper.BindPort(suite.chainA.GetContext(), portID)
}, false,
},
*/
{
"fails to generate port-id", func() {
"port is already bound",
func() {
suite.chainA.GetSimApp().IBCKeeper.PortKeeper.BindPort(suite.chainA.GetContext(), TestPortID)
},
false,
},
{
"fails to generate port-id",
func() {
owner = ""
}, false,
},
false,
},
{
"MsgChanOpenInit fails - channel is already active", func() {
"MsgChanOpenInit fails - channel is already active",
func() {
portID, err := types.GeneratePortID(owner, path.EndpointA.ConnectionID, path.EndpointB.ConnectionID)
suite.Require().NoError(err)
suite.chainA.GetSimApp().ICAKeeper.SetActiveChannel(suite.chainA.GetContext(), portID, path.EndpointA.ChannelID)
}, false,
},
false,
},
}

Expand Down
8 changes: 4 additions & 4 deletions modules/apps/27-interchain-accounts/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (ia InterchainAccount) Validate() error {
return ia.BaseAccount.Validate()
}

type InterchainAccountPretty struct {
type interchainAccountPretty struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
PubKey string `json:"public_key" yaml:"public_key"`
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
Expand All @@ -104,7 +104,7 @@ func (ia InterchainAccount) MarshalYAML() ([]byte, error) {
return nil, err
}

bz, err := yaml.Marshal(InterchainAccountPretty{
bz, err := yaml.Marshal(interchainAccountPretty{
Address: accAddr,
PubKey: "",
AccountNumber: ia.AccountNumber,
Expand All @@ -126,7 +126,7 @@ func (ia InterchainAccount) MarshalJSON() ([]byte, error) {
return nil, err
}

bz, err := json.Marshal(InterchainAccountPretty{
bz, err := json.Marshal(interchainAccountPretty{
Address: accAddr,
PubKey: "",
AccountNumber: ia.AccountNumber,
Expand All @@ -143,7 +143,7 @@ func (ia InterchainAccount) MarshalJSON() ([]byte, error) {

// UnmarshalJSON unmarshals raw JSON bytes into a ModuleAccount.
func (ia *InterchainAccount) UnmarshalJSON(bz []byte) error {
var alias InterchainAccountPretty
var alias interchainAccountPretty
if err := json.Unmarshal(bz, &alias); err != nil {
return err
}
Expand Down
36 changes: 18 additions & 18 deletions modules/apps/27-interchain-accounts/types/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/stretchr/testify/suite"
"gopkg.in/yaml.v2"

"github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types"
ibctesting "github.com/cosmos/ibc-go/v2/testing"
Expand Down Expand Up @@ -48,6 +47,13 @@ func (suite *TypesTestSuite) TestGenerateAddress() {
suite.Require().NotEmpty(accAddr)
}

func (suite *TypesTestSuite) TestParseAddressFromVersion() {
version := types.NewAppVersion(types.VersionPrefix, TestOwnerAddress)

addr := types.ParseAddressFromVersion(version)
suite.Require().Equal(TestOwnerAddress, addr)
}

func (suite *TypesTestSuite) TestGeneratePortID() {
var (
path *ibctesting.Path
Expand Down Expand Up @@ -75,25 +81,25 @@ func (suite *TypesTestSuite) TestGeneratePortID() {
true,
},
{
"invalid owner address",
"invalid connectionID",
func() {
owner = " "
path.EndpointA.ConnectionID = "connection"
},
"",
false,
},
{
"invalid connectionID",
"invalid counterparty connectionID",
func() {
path.EndpointA.ConnectionID = "connection"
path.EndpointB.ConnectionID = "connection"
},
"",
false,
},
{
"invalid counterparty connectionID",
"invalid owner address",
func() {
path.EndpointB.ConnectionID = "connection"
owner = " "
},
"",
false,
Expand Down Expand Up @@ -172,20 +178,14 @@ func (suite *TypesTestSuite) TestGenesisAccountValidate() {

func (suite *TypesTestSuite) TestInterchainAccountMarshalYAML() {
addr := suite.chainA.SenderAccount.GetAddress()
ba := authtypes.NewBaseAccountWithAddress(addr)
baseAcc := authtypes.NewBaseAccountWithAddress(addr)

interchainAcc := types.NewInterchainAccount(ba, suite.chainB.SenderAccount.GetAddress().String())
bz, err := yaml.Marshal(types.InterchainAccountPretty{
Address: addr,
PubKey: "",
AccountNumber: interchainAcc.AccountNumber,
Sequence: interchainAcc.Sequence,
AccountOwner: interchainAcc.AccountOwner,
})
interchainAcc := types.NewInterchainAccount(baseAcc, suite.chainB.SenderAccount.GetAddress().String())
bz, err := interchainAcc.MarshalYAML()
suite.Require().NoError(err)

bz1, err := interchainAcc.MarshalYAML()
suite.Require().Equal(string(bz), string(bz1))
expected := fmt.Sprintf("address: %s\npublic_key: \"\"\naccount_number: 0\nsequence: 0\naccount_owner: %s\n", suite.chainA.SenderAccount.GetAddress(), suite.chainB.SenderAccount.GetAddress())
Copy link
Member Author

Choose a reason for hiding this comment

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

Making interchainAccountPretty private resulted in having to re-write this test.
This is adapted from: https://github.com/cosmos/cosmos-sdk/blob/master/x/auth/types/account_test.go#L123

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah that's why I made it public :D Is it not better to leave it the other way?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it makes more sense to maintain a "prettifying" struct as private, an external party should be unaware of its existence.

Probably best to opt for consistency with the sdk.

Copy link
Contributor

Choose a reason for hiding this comment

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

consistency with sdk is my preference

suite.Require().Equal(expected, string(bz))
}

func (suite *TypesTestSuite) TestInterchainAccountJSON() {
Expand Down
8 changes: 4 additions & 4 deletions modules/apps/27-interchain-accounts/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

var (
ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
)

// RegisterLegacyAminoCodec registers the account interfaces and concrete types on the
// provided LegacyAmino codec. These types are used for Amino JSON serialization
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
Expand All @@ -18,7 +22,3 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations((*authtypes.AccountI)(nil), &InterchainAccount{})
}

var (
ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
)
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ type ConnectionKeeper interface {
// PortKeeper defines the expected IBC port keeper
type PortKeeper interface {
BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability
IsBound(ctx sdk.Context, portID string) bool
LookupModuleByPort(ctx sdk.Context, portID string) (string, *capabilitytypes.Capability, error)
}
8 changes: 4 additions & 4 deletions modules/apps/27-interchain-accounts/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ func NewAppVersion(versionPrefix, accAddr string) string {
}

// KeyActiveChannel creates and returns a new key used for active channels store operations
func KeyActiveChannel(portId string) []byte {
return []byte(fmt.Sprintf("activeChannel/%s", portId))
func KeyActiveChannel(portID string) []byte {
return []byte(fmt.Sprintf("activeChannel/%s", portID))
}

// KeyOwnerAccount creates and returns a new key used for owner account store operations
func KeyOwnerAccount(portId string) []byte {
return []byte(fmt.Sprintf("owner/%s", portId))
func KeyOwnerAccount(portID string) []byte {
return []byte(fmt.Sprintf("owner/%s", portID))
}
15 changes: 8 additions & 7 deletions modules/apps/27-interchain-accounts/types/keys_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package types_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types"
)

func TestKeyActiveChannel(t *testing.T) {
key := types.KeyActiveChannel("owner")
require.Equal(t, string(key), "activeChannel/owner")
func (suite *TypesTestSuite) TestKeyActiveChannel() {
key := types.KeyActiveChannel("port-id")
suite.Require().Equal("activeChannel/port-id", string(key))
}

func (suite *TypesTestSuite) TestKeyOwnerAccount() {
key := types.KeyOwnerAccount("port-id")
suite.Require().Equal("owner/port-id", string(key))
}
7 changes: 0 additions & 7 deletions modules/apps/27-interchain-accounts/types/querier.go

This file was deleted.