Skip to content

Commit

Permalink
test: adding test for RegisterInterchainAccount & adding check to rel… (
Browse files Browse the repository at this point in the history
#552)

* test: adding test for RegisterInterchainAccount & adding check to relay_test

* Update modules/apps/27-interchain-accounts/host/keeper/relay_test.go

Co-authored-by: Damian Nolan <[email protected]>

* Update modules/apps/27-interchain-accounts/host/keeper/relay_test.go

Co-authored-by: Damian Nolan <[email protected]>

* Update modules/apps/27-interchain-accounts/host/keeper/account_test.go

* Update modules/apps/27-interchain-accounts/host/keeper/account_test.go

Co-authored-by: Damian Nolan <[email protected]>
  • Loading branch information
seantking and damiannolan authored Nov 19, 2021
1 parent b87b806 commit 3a06187
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 19 deletions.
26 changes: 26 additions & 0 deletions modules/apps/27-interchain-accounts/host/keeper/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

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

// RegisterInterchainAccount attempts to create a new account using the provided address and stores it in state keyed by the provided port identifier
// If an account for the provided address already exists this function returns early (no-op)
func (k Keeper) RegisterInterchainAccount(ctx sdk.Context, accAddr sdk.AccAddress, controllerPortID string) {
if acc := k.accountKeeper.GetAccount(ctx, accAddr); acc != nil {
return
}

interchainAccount := types.NewInterchainAccount(
authtypes.NewBaseAccountWithAddress(accAddr),
controllerPortID,
)

k.accountKeeper.NewAccount(ctx, interchainAccount)
k.accountKeeper.SetAccount(ctx, interchainAccount)

k.SetInterchainAccountAddress(ctx, controllerPortID, interchainAccount.Address)
}
33 changes: 33 additions & 0 deletions modules/apps/27-interchain-accounts/host/keeper/account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package keeper_test

import (
sdk "github.com/cosmos/cosmos-sdk/types"

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

func (suite *KeeperTestSuite) TestRegisterInterchainAccount() {
suite.SetupTest()

path := NewICAPath(suite.chainA, suite.chainB)
suite.coordinator.SetupConnections(path)

// InitInterchainAccount
err := SetupICAPath(path, TestOwnerAddress)
suite.Require().NoError(err)

portID, err := types.GeneratePortID(TestOwnerAddress, ibctesting.FirstConnectionID, ibctesting.FirstConnectionID)
suite.Require().NoError(err)

// Get the address of the interchain account stored in state during handshake step
storedAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), portID)
suite.Require().True(found)

icaAddr, err := sdk.AccAddressFromBech32(storedAddr)
suite.Require().NoError(err)

// Check if account is created
interchainAccount := suite.chainB.GetSimApp().AccountKeeper.GetAccount(suite.chainB.GetContext(), icaAddr)
suite.Require().Equal(interchainAccount.GetAddress().String(), storedAddr)
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ func (suite *KeeperTestSuite) TestOnChanOpenTry() {
} else {
suite.Require().Error(err)
}

})
}
}
Expand Down
18 changes: 0 additions & 18 deletions modules/apps/27-interchain-accounts/host/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
"github.com/tendermint/tendermint/libs/log"
Expand Down Expand Up @@ -59,23 +58,6 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s-%s", host.ModuleName, types.ModuleName))
}

// RegisterInterchainAccount attempts to create a new account using the provided address and stores it in state keyed by the provided port identifier
// If an account for the provided address already exists this function returns early (no-op)
func (k Keeper) RegisterInterchainAccount(ctx sdk.Context, accAddr sdk.AccAddress, controllerPortID string) {
if acc := k.accountKeeper.GetAccount(ctx, accAddr); acc != nil {
return
}

interchainAccount := types.NewInterchainAccount(
authtypes.NewBaseAccountWithAddress(accAddr),
controllerPortID,
)

k.accountKeeper.NewAccount(ctx, interchainAccount)
k.accountKeeper.SetAccount(ctx, interchainAccount)
k.SetInterchainAccountAddress(ctx, controllerPortID, interchainAccount.Address)
}

// BindPort stores the provided portID and binds to it, returning the associated capability
func (k Keeper) BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability {
store := ctx.KVStore(k.storeKey)
Expand Down
14 changes: 14 additions & 0 deletions modules/apps/27-interchain-accounts/host/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,20 @@ func (suite *KeeperTestSuite) TestOnRecvPacket() {
err := SetupICAPath(path, TestOwnerAddress)
suite.Require().NoError(err)

portID, err := types.GeneratePortID(TestOwnerAddress, ibctesting.FirstConnectionID, ibctesting.FirstConnectionID)
suite.Require().NoError(err)

// Get the address of the interchain account stored in state during handshake step
storedAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), portID)
suite.Require().True(found)

icaAddr, err := sdk.AccAddressFromBech32(storedAddr)
suite.Require().NoError(err)

// Check if account is created
interchainAccount := suite.chainB.GetSimApp().AccountKeeper.GetAccount(suite.chainB.GetContext(), icaAddr)
suite.Require().Equal(interchainAccount.GetAddress().String(), storedAddr)

suite.fundICAWallet(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10000))))

tc.malleate() // malleate mutates test data
Expand Down

0 comments on commit 3a06187

Please sign in to comment.