From e9dfb09f379f0ab69fd8f442c96ec0de52ad908f Mon Sep 17 00:00:00 2001 From: Sean King Date: Fri, 19 Nov 2021 15:43:50 +0100 Subject: [PATCH 1/5] test: adding test for RegisterInterchainAccount & adding check to relay_test --- .../host/keeper/account.go | 26 +++++++++++++++ .../host/keeper/account_test.go | 33 +++++++++++++++++++ .../host/keeper/handshake_test.go | 1 - .../host/keeper/keeper.go | 18 ---------- .../host/keeper/relay_test.go | 14 ++++++++ 5 files changed, 73 insertions(+), 19 deletions(-) create mode 100644 modules/apps/27-interchain-accounts/host/keeper/account.go create mode 100644 modules/apps/27-interchain-accounts/host/keeper/account_test.go diff --git a/modules/apps/27-interchain-accounts/host/keeper/account.go b/modules/apps/27-interchain-accounts/host/keeper/account.go new file mode 100644 index 00000000000..d9e213f4ace --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/keeper/account.go @@ -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) +} diff --git a/modules/apps/27-interchain-accounts/host/keeper/account_test.go b/modules/apps/27-interchain-accounts/host/keeper/account_test.go new file mode 100644 index 00000000000..2ebc5427eed --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/keeper/account_test.go @@ -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) +} diff --git a/modules/apps/27-interchain-accounts/host/keeper/handshake_test.go b/modules/apps/27-interchain-accounts/host/keeper/handshake_test.go index 65b9a159945..fde51a961ff 100644 --- a/modules/apps/27-interchain-accounts/host/keeper/handshake_test.go +++ b/modules/apps/27-interchain-accounts/host/keeper/handshake_test.go @@ -168,7 +168,6 @@ func (suite *KeeperTestSuite) TestOnChanOpenTry() { } else { suite.Require().Error(err) } - }) } } diff --git a/modules/apps/27-interchain-accounts/host/keeper/keeper.go b/modules/apps/27-interchain-accounts/host/keeper/keeper.go index bd21aa90397..44735ae81ea 100644 --- a/modules/apps/27-interchain-accounts/host/keeper/keeper.go +++ b/modules/apps/27-interchain-accounts/host/keeper/keeper.go @@ -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" @@ -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) diff --git a/modules/apps/27-interchain-accounts/host/keeper/relay_test.go b/modules/apps/27-interchain-accounts/host/keeper/relay_test.go index 7bb47ed88a6..f02d99c912e 100644 --- a/modules/apps/27-interchain-accounts/host/keeper/relay_test.go +++ b/modules/apps/27-interchain-accounts/host/keeper/relay_test.go @@ -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 From 917e1a8ae904fd5b1617560f3a948ca3226786ad Mon Sep 17 00:00:00 2001 From: Sean King Date: Fri, 19 Nov 2021 16:28:23 +0100 Subject: [PATCH 2/5] Update modules/apps/27-interchain-accounts/host/keeper/relay_test.go Co-authored-by: Damian Nolan --- modules/apps/27-interchain-accounts/host/keeper/relay_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/apps/27-interchain-accounts/host/keeper/relay_test.go b/modules/apps/27-interchain-accounts/host/keeper/relay_test.go index f02d99c912e..6b52fccf414 100644 --- a/modules/apps/27-interchain-accounts/host/keeper/relay_test.go +++ b/modules/apps/27-interchain-accounts/host/keeper/relay_test.go @@ -348,7 +348,7 @@ func (suite *KeeperTestSuite) TestOnRecvPacket() { 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) + storedAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), portID) suite.Require().True(found) icaAddr, err := sdk.AccAddressFromBech32(storedAddr) From 55c69c208978fcaa494c7cf7968b10bb451023f7 Mon Sep 17 00:00:00 2001 From: Sean King Date: Fri, 19 Nov 2021 16:28:34 +0100 Subject: [PATCH 3/5] Update modules/apps/27-interchain-accounts/host/keeper/relay_test.go Co-authored-by: Damian Nolan --- modules/apps/27-interchain-accounts/host/keeper/relay_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/apps/27-interchain-accounts/host/keeper/relay_test.go b/modules/apps/27-interchain-accounts/host/keeper/relay_test.go index 6b52fccf414..9109f7559e5 100644 --- a/modules/apps/27-interchain-accounts/host/keeper/relay_test.go +++ b/modules/apps/27-interchain-accounts/host/keeper/relay_test.go @@ -344,7 +344,7 @@ func (suite *KeeperTestSuite) TestOnRecvPacket() { err := SetupICAPath(path, TestOwnerAddress) suite.Require().NoError(err) - portId, err := types.GeneratePortID(TestOwnerAddress, ibctesting.FirstConnectionID, ibctesting.FirstConnectionID) + 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 From f0420a44c68ee70b1a3a8fcf219babe90e87548a Mon Sep 17 00:00:00 2001 From: Sean King Date: Fri, 19 Nov 2021 16:34:07 +0100 Subject: [PATCH 4/5] Update modules/apps/27-interchain-accounts/host/keeper/account_test.go --- modules/apps/27-interchain-accounts/host/keeper/account_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/apps/27-interchain-accounts/host/keeper/account_test.go b/modules/apps/27-interchain-accounts/host/keeper/account_test.go index 2ebc5427eed..e9f55aed4f0 100644 --- a/modules/apps/27-interchain-accounts/host/keeper/account_test.go +++ b/modules/apps/27-interchain-accounts/host/keeper/account_test.go @@ -17,7 +17,7 @@ func (suite *KeeperTestSuite) TestRegisterInterchainAccount() { err := SetupICAPath(path, TestOwnerAddress) suite.Require().NoError(err) - portId, err := types.GeneratePortID(TestOwnerAddress, ibctesting.FirstConnectionID, ibctesting.FirstConnectionID) + 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 From 7c6d2a91ecb430bb9e3f342880a1eb22dc43d70b Mon Sep 17 00:00:00 2001 From: Sean King Date: Fri, 19 Nov 2021 16:34:33 +0100 Subject: [PATCH 5/5] Update modules/apps/27-interchain-accounts/host/keeper/account_test.go --- modules/apps/27-interchain-accounts/host/keeper/account_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/apps/27-interchain-accounts/host/keeper/account_test.go b/modules/apps/27-interchain-accounts/host/keeper/account_test.go index e9f55aed4f0..65ce1646905 100644 --- a/modules/apps/27-interchain-accounts/host/keeper/account_test.go +++ b/modules/apps/27-interchain-accounts/host/keeper/account_test.go @@ -21,7 +21,7 @@ func (suite *KeeperTestSuite) TestRegisterInterchainAccount() { 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) + storedAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), portID) suite.Require().True(found) icaAddr, err := sdk.AccAddressFromBech32(storedAddr)