Skip to content

Commit

Permalink
Make ICA waits more explicit (#1157)
Browse files Browse the repository at this point in the history
* Make ICA waits more explicit

* poll for timeout

* poll for channel close confirm

* Comment out sqlite db file for scenarios tests
  • Loading branch information
agouin committed Apr 4, 2023
1 parent d57648a commit d7219fc
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 70 deletions.
81 changes: 50 additions & 31 deletions interchaintest/ica_channel_close_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/cosmos/cosmos-sdk/crypto/keyring"
chantypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
relayerinterchaintest "github.com/cosmos/relayer/v2/interchaintest"
interchaintest "github.com/strangelove-ventures/interchaintest/v7"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
Expand Down Expand Up @@ -61,7 +62,7 @@ func TestScenarioICAChannelClose(t *testing.T) {
chains, err := cf.Chains(t.Name())
require.NoError(t, err)

chain1, chain2 := chains[0], chains[1]
chain1, chain2 := chains[0].(*cosmos.CosmosChain), chains[1].(*cosmos.CosmosChain)

// Get a relayer instance
r := relayerinterchaintest.
Expand All @@ -84,11 +85,12 @@ func TestScenarioICAChannelClose(t *testing.T) {
})

require.NoError(t, ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
SkipPathCreation: true,
BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),
TestName: t.Name(),
Client: client,
NetworkID: network,
SkipPathCreation: true,
// Uncomment this to load blocks, txs, msgs, and events into sqlite db as test runs
// BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),
}))

// Fund a user account on chain1 and chain2
Expand All @@ -105,21 +107,34 @@ func TestScenarioICAChannelClose(t *testing.T) {
err = r.CreateClients(ctx, eRep, pathName, ibc.CreateClientOptions{TrustingPeriod: "330h"})
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 5, chain1, chain2)
err = testutil.WaitForBlocks(ctx, 2, chain1, chain2)
require.NoError(t, err)

// Create a new connection
err = r.CreateConnections(ctx, eRep, pathName)
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 5, chain1, chain2)
err = testutil.WaitForBlocks(ctx, 2, chain1, chain2)
require.NoError(t, err)

// Query for the newly created connection
connections, err := r.GetConnections(ctx, eRep, chain1.Config().ChainID)
require.NoError(t, err)
require.Equal(t, 1, len(connections))

// Start the relayer and set the cleanup function.
err = r.StartRelayer(ctx, eRep, pathName)
require.NoError(t, err)

t.Cleanup(
func() {
err := r.StopRelayer(ctx, eRep)
if err != nil {
t.Logf("an error occurred while stopping the relayer: %s", err)
}
},
)

// Register a new interchain account on chain2, on behalf of the user acc on chain1
chain1Addr := chain1User.(*cosmos.CosmosWallet).FormattedAddressWithPrefix(chain1.Config().Bech32Prefix)

Expand All @@ -136,21 +151,18 @@ func TestScenarioICAChannelClose(t *testing.T) {
_, _, err = chain1.Exec(ctx, registerICA, nil)
require.NoError(t, err)

// Start the relayer and set the cleanup function.
err = r.StartRelayer(ctx, eRep, pathName)
ir := cosmos.DefaultEncoding().InterfaceRegistry

c2h, err := chain2.Height(ctx)
require.NoError(t, err)

t.Cleanup(
func() {
err := r.StopRelayer(ctx, eRep)
if err != nil {
t.Logf("an error occured while stopping the relayer: %s", err)
}
},
)
channelFound := func(found *chantypes.MsgChannelOpenConfirm) bool {
return found.PortId == "icahost"
}

// Wait for relayer to start up and finish channel handshake
err = testutil.WaitForBlocks(ctx, 15, chain1, chain2)
// Wait for channel open confirm
_, err = cosmos.PollForMessage(ctx, chain2, ir,
c2h, c2h+30, channelFound)
require.NoError(t, err)

// Query for the newly registered interchain account
Expand Down Expand Up @@ -185,10 +197,6 @@ func TestScenarioICAChannelClose(t *testing.T) {
err = chain2.SendFunds(ctx, chain2User.KeyName(), transfer)
require.NoError(t, err)

// Wait for transfer to be complete and assert balances
err = testutil.WaitForBlocks(ctx, 5, chain2)
require.NoError(t, err)

chain2Bal, err := chain2.GetBalance(ctx, chain2Addr, chain2.Config().Denom)
require.NoError(t, err)
require.Equal(t, chain2OrigBal-transferAmount, chain2Bal)
Expand Down Expand Up @@ -225,8 +233,18 @@ func TestScenarioICAChannelClose(t *testing.T) {
_, _, err = chain1.Exec(ctx, sendICATransfer, nil)
require.NoError(t, err)

// Wait for tx to be relayed
err = testutil.WaitForBlocks(ctx, 10, chain2)
c1h, err := chain1.Height(ctx)
require.NoError(t, err)

ackFound := func(found *chantypes.MsgAcknowledgement) bool {
return found.Packet.Sequence == 1 &&
found.Packet.SourcePort == "icacontroller-"+chain1Addr &&
found.Packet.DestinationPort == "icahost"
}

// Wait for ack
_, err = cosmos.PollForMessage(ctx, chain1, ir,
c1h, c1h+10, ackFound)
require.NoError(t, err)

// Assert that the funds have been received by the user account on chain2
Expand All @@ -243,9 +261,6 @@ func TestScenarioICAChannelClose(t *testing.T) {
err = r.StopRelayer(ctx, eRep)
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 5, chain1, chain2)
require.NoError(t, err)

// Send another bank transfer msg to ICA on chain2 from the user account on chain1.
// This message should timeout and the channel will be closed when we re-start the relayer.
_, _, err = chain1.Exec(ctx, sendICATransfer, nil)
Expand Down Expand Up @@ -291,8 +306,12 @@ func TestScenarioICAChannelClose(t *testing.T) {
_, _, err = chain1.Exec(ctx, registerICA, nil)
require.NoError(t, err)

// Wait for channel handshake to finish
err = testutil.WaitForBlocks(ctx, 15, chain1, chain2)
c2h, err = chain2.Height(ctx)
require.NoError(t, err)

// Wait for channel open confirm
_, err = cosmos.PollForMessage(ctx, chain2, ir,
c2h, c2h+30, channelFound)
require.NoError(t, err)

// Assert that a new channel has been opened and the same ICA is in use
Expand Down
81 changes: 55 additions & 26 deletions interchaintest/interchain_accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/cosmos/cosmos-sdk/crypto/keyring"
chantypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
relayerinterchaintest "github.com/cosmos/relayer/v2/interchaintest"
interchaintest "github.com/strangelove-ventures/interchaintest/v7"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
Expand Down Expand Up @@ -62,7 +63,7 @@ func TestScenarioInterchainAccounts(t *testing.T) {
chains, err := cf.Chains(t.Name())
require.NoError(t, err)

chain1, chain2 := chains[0], chains[1]
chain1, chain2 := chains[0].(*cosmos.CosmosChain), chains[1].(*cosmos.CosmosChain)

// Get a relayer instance
r := relayerinterchaintest.
Expand All @@ -89,6 +90,8 @@ func TestScenarioInterchainAccounts(t *testing.T) {
Client: client,
NetworkID: network,
SkipPathCreation: true,
// Uncomment this to load blocks, txs, msgs, and events into sqlite db as test runs
// BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),
}))

// Fund a user account on chain1 and chain2
Expand All @@ -105,21 +108,34 @@ func TestScenarioInterchainAccounts(t *testing.T) {
err = r.CreateClients(ctx, eRep, pathName, ibc.CreateClientOptions{TrustingPeriod: "330h"})
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 5, chain1, chain2)
err = testutil.WaitForBlocks(ctx, 2, chain1, chain2)
require.NoError(t, err)

// Create a new connection
err = r.CreateConnections(ctx, eRep, pathName)
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 5, chain1, chain2)
err = testutil.WaitForBlocks(ctx, 2, chain1, chain2)
require.NoError(t, err)

// Query for the newly created connection
connections, err := r.GetConnections(ctx, eRep, chain1.Config().ChainID)
require.NoError(t, err)
require.Equal(t, 1, len(connections))

// Start the relayer and set the cleanup function.
err = r.StartRelayer(ctx, eRep, pathName)
require.NoError(t, err)

t.Cleanup(
func() {
err := r.StopRelayer(ctx, eRep)
if err != nil {
t.Logf("an error occured while stopping the relayer: %s", err)
}
},
)

// Register a new interchain account on chain2, on behalf of the user acc on chain1
chain1Addr := chain1User.(*cosmos.CosmosWallet).FormattedAddressWithPrefix(chain1.Config().Bech32Prefix)

Expand All @@ -136,21 +152,18 @@ func TestScenarioInterchainAccounts(t *testing.T) {
_, _, err = chain1.Exec(ctx, registerICA, nil)
require.NoError(t, err)

// Start the relayer and set the cleanup function.
err = r.StartRelayer(ctx, eRep, pathName)
ir := cosmos.DefaultEncoding().InterfaceRegistry

c2h, err := chain2.Height(ctx)
require.NoError(t, err)

t.Cleanup(
func() {
err := r.StopRelayer(ctx, eRep)
if err != nil {
t.Logf("an error occured while stopping the relayer: %s", err)
}
},
)
channelFound := func(found *chantypes.MsgChannelOpenConfirm) bool {
return found.PortId == "icahost"
}

// Wait for relayer to start up and finish channel handshake
err = testutil.WaitForBlocks(ctx, 15, chain1, chain2)
// Wait for channel open confirm
_, err = cosmos.PollForMessage(ctx, chain2, ir,
c2h, c2h+30, channelFound)
require.NoError(t, err)

// Query for the newly registered interchain account
Expand Down Expand Up @@ -185,10 +198,6 @@ func TestScenarioInterchainAccounts(t *testing.T) {
err = chain2.SendFunds(ctx, chain2User.KeyName(), transfer)
require.NoError(t, err)

// Wait for transfer to be complete and assert balances
err = testutil.WaitForBlocks(ctx, 5, chain2)
require.NoError(t, err)

chain2Bal, err := chain2.GetBalance(ctx, chain2Addr, chain2.Config().Denom)
require.NoError(t, err)
require.Equal(t, chain2OrigBal-transferAmount, chain2Bal)
Expand Down Expand Up @@ -225,8 +234,18 @@ func TestScenarioInterchainAccounts(t *testing.T) {
_, _, err = chain1.Exec(ctx, sendICATransfer, nil)
require.NoError(t, err)

// Wait for tx to be relayed
err = testutil.WaitForBlocks(ctx, 10, chain2)
c1h, err := chain1.Height(ctx)
require.NoError(t, err)

ackFound := func(found *chantypes.MsgAcknowledgement) bool {
return found.Packet.Sequence == 1 &&
found.Packet.SourcePort == "icacontroller-"+chain1Addr &&
found.Packet.DestinationPort == "icahost"
}

// Wait for ack
_, err = cosmos.PollForMessage(ctx, chain1, ir,
c1h, c1h+10, ackFound)
require.NoError(t, err)

// Assert that the funds have been received by the user account on chain2
Expand All @@ -243,9 +262,6 @@ func TestScenarioInterchainAccounts(t *testing.T) {
err = r.StopRelayer(ctx, eRep)
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 5, chain1, chain2)
require.NoError(t, err)

// Send another bank transfer msg to ICA on chain2 from the user account on chain1.
// This message should timeout and the channel will be closed when we re-start the relayer.
_, _, err = chain1.Exec(ctx, sendICATransfer, nil)
Expand All @@ -258,7 +274,15 @@ func TestScenarioInterchainAccounts(t *testing.T) {
err = r.StartRelayer(ctx, eRep, pathName)
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 15, chain1, chain2)
c2h, err = chain2.Height(ctx)
require.NoError(t, err)

chanCloseFound := func(found *chantypes.MsgChannelCloseConfirm) bool {
return found.PortId == "icahost"
}

// Wait for channel close confirm
_, err = cosmos.PollForMessage(ctx, chain2, ir, c2h, c2h+30, chanCloseFound)
require.NoError(t, err)

// Assert that the packet timed out and that the acc balances are correct
Expand Down Expand Up @@ -286,7 +310,12 @@ func TestScenarioInterchainAccounts(t *testing.T) {
require.NoError(t, err)

// Wait for channel handshake to finish
err = testutil.WaitForBlocks(ctx, 15, chain1, chain2)
c2h, err = chain2.Height(ctx)
require.NoError(t, err)

// Wait for channel open confirm
_, err = cosmos.PollForMessage(ctx, chain2, ir,
c2h, c2h+30, channelFound)
require.NoError(t, err)

// Assert that a new channel has been opened and the same ICA is in use
Expand Down
9 changes: 5 additions & 4 deletions interchaintest/relay_many_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ func TestRelayerMultiplePathsSingleProcess(t *testing.T) {
client, network := interchaintest.DockerSetup(t)

require.NoError(t, ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),
TestName: t.Name(),
Client: client,
NetworkID: network,
// Uncomment this to load blocks, txs, msgs, and events into sqlite db as test runs
// BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),

SkipPathCreation: false,
}))
Expand Down
9 changes: 5 additions & 4 deletions interchaintest/relayer_override_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ func TestClientOverrideFlag(t *testing.T) {
})

require.NoError(t, ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),
TestName: t.Name(),
Client: client,
NetworkID: network,
// Uncomment this to load blocks, txs, msgs, and events into sqlite db as test runs
// BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),

SkipPathCreation: true,
}))
Expand Down
11 changes: 6 additions & 5 deletions interchaintest/tendermint_v0.37_boundary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ func TestScenarioTendermint37Boundary(t *testing.T) {
rep := testreporter.NewNopReporter()

require.NoError(t, ic.Build(ctx, rep.RelayerExecReporter(t), interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),
SkipPathCreation: false,
TestName: t.Name(),
Client: client,
NetworkID: network,
// Uncomment this to load blocks, txs, msgs, and events into sqlite db as test runs
// BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),
SkipPathCreation: false,
}))
t.Cleanup(func() {
_ = ic.Close()
Expand Down

0 comments on commit d7219fc

Please sign in to comment.