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

test: add e2e test for ibc bypass msg #2532

Merged
merged 22 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
29 changes: 29 additions & 0 deletions tests/e2e/e2e_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,35 @@ func (s *IntegrationTestSuite) executeGaiaTxCommand(ctx context.Context, c *chai
}
}

func (s *IntegrationTestSuite) executeHermesCommand(ctx context.Context, hermesCmd []string) (string, string) {
var (
outBuf bytes.Buffer
errBuf bytes.Buffer
)
exec, err := s.dkrPool.Client.CreateExec(docker.CreateExecOptions{
Context: ctx,
AttachStdout: true,
AttachStderr: true,
Container: s.hermesResource1.Container.ID,
User: "root",
Cmd: hermesCmd,
})
s.Require().NoError(err)

err = s.dkrPool.Client.StartExec(exec.ID, docker.StartExecOptions{
Context: ctx,
Detach: false,
OutputStream: &outBuf,
ErrorStream: &errBuf,
})
s.Require().NoError(err)

stdOut := outBuf.Bytes()
stdErr := errBuf.Bytes()

return string(stdOut), string(stdErr)
}

func (s *IntegrationTestSuite) expectErrExecValidation(chain *chain, valIdx int, expectErr bool) func([]byte, []byte) bool {
return func(stdOut []byte, stdErr []byte) bool {
var txResp sdk.TxResponse
Expand Down
198 changes: 93 additions & 105 deletions tests/e2e/e2e_ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
)

Expand All @@ -34,102 +28,6 @@ type PacketMetadata struct {
Forward *ForwardMetadata `json:"forward"`
}

func (s *IntegrationTestSuite) runIBCRelayer() {
s.T().Log("starting Hermes relayer container...")

tmpDir, err := os.MkdirTemp("", "gaia-e2e-testnet-hermes-")
s.Require().NoError(err)
s.tmpDirs = append(s.tmpDirs, tmpDir)

gaiaAVal := s.chainA.validators[0]
gaiaBVal := s.chainB.validators[0]

gaiaARly := s.chainA.genesisAccounts[relayerAccountIndex]
gaiaBRly := s.chainB.genesisAccounts[relayerAccountIndex]

hermesCfgPath := path.Join(tmpDir, "hermes")

s.Require().NoError(os.MkdirAll(hermesCfgPath, 0o755))
_, err = copyFile(
filepath.Join("./scripts/", "hermes_bootstrap.sh"),
filepath.Join(hermesCfgPath, "hermes_bootstrap.sh"),
)
s.Require().NoError(err)

s.hermesResource, err = s.dkrPool.RunWithOptions(
&dockertest.RunOptions{
Name: fmt.Sprintf("%s-%s-relayer", s.chainA.id, s.chainB.id),
Repository: "ghcr.io/cosmos/hermes-e2e",
Tag: "1.0.0",
NetworkID: s.dkrNet.Network.ID,
Mounts: []string{
fmt.Sprintf("%s/:/root/hermes", hermesCfgPath),
},
PortBindings: map[docker.Port][]docker.PortBinding{
"3031/tcp": {{HostIP: "", HostPort: "3031"}},
},
Env: []string{
fmt.Sprintf("GAIA_A_E2E_CHAIN_ID=%s", s.chainA.id),
fmt.Sprintf("GAIA_B_E2E_CHAIN_ID=%s", s.chainB.id),
fmt.Sprintf("GAIA_A_E2E_VAL_MNEMONIC=%s", gaiaAVal.mnemonic),
fmt.Sprintf("GAIA_B_E2E_VAL_MNEMONIC=%s", gaiaBVal.mnemonic),
fmt.Sprintf("GAIA_A_E2E_RLY_MNEMONIC=%s", gaiaARly.mnemonic),
fmt.Sprintf("GAIA_B_E2E_RLY_MNEMONIC=%s", gaiaBRly.mnemonic),
fmt.Sprintf("GAIA_A_E2E_VAL_HOST=%s", s.valResources[s.chainA.id][0].Container.Name[1:]),
fmt.Sprintf("GAIA_B_E2E_VAL_HOST=%s", s.valResources[s.chainB.id][0].Container.Name[1:]),
},
Entrypoint: []string{
"sh",
"-c",
"chmod +x /root/hermes/hermes_bootstrap.sh && /root/hermes/hermes_bootstrap.sh",
},
},
noRestart,
)
s.Require().NoError(err)

endpoint := fmt.Sprintf("http://%s/state", s.hermesResource.GetHostPort("3031/tcp"))
s.Require().Eventually(
func() bool {
resp, err := http.Get(endpoint) //nolint:gosec // this is a test
if err != nil {
return false
}

defer resp.Body.Close()

bz, err := io.ReadAll(resp.Body)
if err != nil {
return false
}

var respBody map[string]interface{}
if err := json.Unmarshal(bz, &respBody); err != nil {
return false
}

status := respBody["status"].(string)
result := respBody["result"].(map[string]interface{})

return status == "success" && len(result["chains"].([]interface{})) == 2
},
5*time.Minute,
time.Second,
"hermes relayer not healthy",
)

s.T().Logf("started Hermes relayer container: %s", s.hermesResource.Container.ID)

// XXX: Give time to both networks to start, otherwise we might see gRPC
// transport errors.
time.Sleep(10 * time.Second)

// create the client, connection and channel between the two Gaia chains
s.createConnection()
time.Sleep(10 * time.Second)
s.createChannel()
}

func (s *IntegrationTestSuite) sendIBC(c *chain, valIdx int, sender, recipient, token, fees, note string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
Expand Down Expand Up @@ -158,6 +56,96 @@ func (s *IntegrationTestSuite) sendIBC(c *chain, valIdx int, sender, recipient,
s.T().Log("successfully sent IBC tokens")
}

func (s *IntegrationTestSuite) hermesTransfer(configPath, srcChainID, dstChainID, srcChannelID, denom string, sendAmt, timeOutOffset, numMsg int) bool {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

hermesCmd := []string{
hermesBinary,
fmt.Sprintf("--config=%s", configPath),
"tx",
"ft-transfer",
fmt.Sprintf("--dst-chain=%s", dstChainID),
fmt.Sprintf("--src-chain=%s", srcChainID),
fmt.Sprintf("--src-channel=%s", srcChannelID),
fmt.Sprintf("--src-port=%s", "transfer"),
fmt.Sprintf("--amount=%v", sendAmt),
fmt.Sprintf("--denom=%s", denom),
fmt.Sprintf("--timeout-height-offset=%v", timeOutOffset),
fmt.Sprintf("--number-msgs=%v", numMsg),
}

stdout, stderr := s.executeHermesCommand(ctx, hermesCmd)
if strings.Contains(stdout, "ERROR") || strings.Contains(stderr, "ERROR") {
return false
yaruwangway marked this conversation as resolved.
Show resolved Hide resolved
}

return true
}

func (s *IntegrationTestSuite) hermesClearPacket(configPath, chainID, channelID string) bool {
yaruwangway marked this conversation as resolved.
Show resolved Hide resolved
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

hermesCmd := []string{
hermesBinary,
fmt.Sprintf("--config=%s", configPath),
"clear",
"packets",
fmt.Sprintf("--chain=%s", chainID),
fmt.Sprintf("--channel=%s", channelID),
fmt.Sprintf("--port=%s", "transfer"),
}

stdout, stderr := s.executeHermesCommand(ctx, hermesCmd)
if strings.Contains(stdout, "ERROR") || strings.Contains(stderr, "ERROR") {
return false
}

return true
}

func (s *IntegrationTestSuite) hermesPendingPackets(configPath, chainID, channelID string) bool {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
hermesCmd := []string{
hermesBinary,
fmt.Sprintf("--config=%s", configPath),
"query",
"packet",
"pending",
fmt.Sprintf("--chain=%s", chainID),
fmt.Sprintf("--channel=%s", channelID),
fmt.Sprintf("--port=%s", "transfer"),
}

stdout, _ := s.executeHermesCommand(ctx, hermesCmd)
// "start: Sequence" is the pending packets sequence
if strings.Contains(stdout, "Sequence") {
return true
Copy link
Contributor

Choose a reason for hiding this comment

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

The meaning of this returned bool is also unclear to me, and why a sequence string should affect it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so when querying pending packet, if the result contains "sequence", there are pending packets,

2023-05-25T09:07:57.345611Z  INFO ThreadId(01) running Hermes v1.4.1
SUCCESS Summary {
    src: PendingPackets {
        unreceived_packets: [],
        unreceived_acks: [],
    },
    dst: PendingPackets {
        unreceived_packets: [
            Collated {
                start: Sequence(
                    15,
                ),
                end: Sequence(
                    15,
                ),
            },
        ],
        unreceived_acks: [],
    },
}

if the result is like:

SUCCESS Summary {
    src: PendingPackets {
        unreceived_packets: [],
        unreceived_acks: [],
    },
    dst: PendingPackets {
        unreceived_packets: [],
        unreceived_acks: [],
    },
}

there is no pending packet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can also write a helper func to parse the result string, to find the exact list of unreceived_packets.

}

return false
}

func (s *IntegrationTestSuite) queryRelayerWalletsBalances() (sdk.Coin, sdk.Coin) {
chainAAPIEndpoint := fmt.Sprintf("http://%s", s.valResources[s.chainA.id][0].GetHostPort("1317/tcp"))
scrRelayerBalance, err := getSpecificBalance(
chainAAPIEndpoint,
s.chainA.genesisAccounts[relayerAccountIndexHermes1].keyInfo.GetAddress().String(),
uatomDenom)
s.Require().NoError(err)

chainBAPIEndpoint := fmt.Sprintf("http://%s", s.valResources[s.chainB.id][0].GetHostPort("1317/tcp"))
dstRelayerBalance, err := getSpecificBalance(
chainBAPIEndpoint,
s.chainB.genesisAccounts[relayerAccountIndexHermes1].keyInfo.GetAddress().String(),
uatomDenom)
s.Require().NoError(err)

return scrRelayerBalance, dstRelayerBalance
}

func (s *IntegrationTestSuite) createConnection() {
s.T().Logf("connecting %s and %s chains via IBC", s.chainA.id, s.chainB.id)

Expand All @@ -168,7 +156,7 @@ func (s *IntegrationTestSuite) createConnection() {
Context: ctx,
AttachStdout: true,
AttachStderr: true,
Container: s.hermesResource.Container.ID,
Container: s.hermesResource0.Container.ID,
User: "root",
Cmd: []string{
"hermes",
Expand Down Expand Up @@ -211,7 +199,7 @@ func (s *IntegrationTestSuite) createChannel() {
Context: ctx,
AttachStdout: true,
AttachStderr: true,
Container: s.hermesResource.Container.ID,
Container: s.hermesResource0.Container.ID,
User: "root",
Cmd: []string{
"hermes",
Expand Down Expand Up @@ -249,7 +237,7 @@ func (s *IntegrationTestSuite) createChannel() {
}

func (s *IntegrationTestSuite) testIBCTokenTransfer() {
time.Sleep(30 * time.Second)
// time.Sleep(30 * time.Second)
yaruwangway marked this conversation as resolved.
Show resolved Hide resolved
s.Run("send_uatom_to_chainB", func() {
// require the recipient account receives the IBC tokens (IBC packets ACKd)
var (
Expand Down
Loading