Skip to content

Commit

Permalink
ibc.go
Browse files Browse the repository at this point in the history
  • Loading branch information
toteki committed Oct 9, 2023
1 parent ad36a20 commit 4acd3d0
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 140 deletions.
153 changes: 153 additions & 0 deletions tests/e2e/setup/ibc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package setup

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
"time"

"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
)

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

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

gaiaVal := s.Chain.GaiaValidators[0]
// umeeVal for the relayer needs to be a different account
// than what we use for runPriceFeeder.
umeeVal := s.Chain.Validators[1]
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: "umee-gaia-relayer",
Repository: "ghcr.io/umee-network/hermes-e2e",
Tag: "latest",
NetworkID: s.DkrNet.Network.ID,
Mounts: []string{
fmt.Sprintf("%s/:/home/hermes", hermesCfgPath),
},
ExposedPorts: []string{"3031"},
PortBindings: map[docker.Port][]docker.PortBinding{
"3031/tcp": {{HostIP: "", HostPort: "3031"}},
},
Env: []string{
fmt.Sprintf("UMEE_E2E_GAIA_CHAIN_ID=%s", GaiaChainID),
fmt.Sprintf("UMEE_E2E_UMEE_CHAIN_ID=%s", s.Chain.ID),
fmt.Sprintf("UMEE_E2E_GAIA_VAL_MNEMONIC=%s", gaiaVal.mnemonic),
fmt.Sprintf("UMEE_E2E_UMEE_VAL_MNEMONIC=%s", umeeVal.mnemonic),
fmt.Sprintf("UMEE_E2E_GAIA_VAL_HOST=%s", s.GaiaResource.Container.Name[1:]),
fmt.Sprintf("UMEE_E2E_UMEE_VAL_HOST=%s", s.ValResources[1].Container.Name[1:]),
},
Entrypoint: []string{
"sh",
"-c",
"chmod +x /home/hermes/hermes_bootstrap.sh && /home/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)
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)

// create the client, connection and channel between the Umee and Gaia chains
s.connectIBCChains()
}

func (s *E2ETestSuite) connectIBCChains() {
s.T().Logf("connecting %s and %s chains via IBC", s.Chain.ID, GaiaChainID)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

exec, err := s.DkrPool.Client.CreateExec(docker.CreateExecOptions{
Context: ctx,
AttachStdout: true,
AttachStderr: true,
Container: s.HermesResource.Container.ID,
User: "root",
Cmd: []string{
"hermes",
"create",
"channel",
s.Chain.ID,
GaiaChainID,
"--port-a=transfer",
"--port-b=transfer",
},
})
s.Require().NoError(err)

var (
outBuf bytes.Buffer
errBuf bytes.Buffer
)

err = s.DkrPool.Client.StartExec(exec.ID, docker.StartExecOptions{
Context: ctx,
Detach: false,
OutputStream: &outBuf,
ErrorStream: &errBuf,
})
s.Require().NoErrorf(
err,
"failed connect chains; stdout: %s, stderr: %s", outBuf.String(), errBuf.String(),
)

s.Require().Containsf(
errBuf.String(),
"successfully opened init channel",
"failed to connect chains via IBC: %s", errBuf.String(),
)

s.T().Logf("connected %s and %s chains via IBC", s.Chain.ID, GaiaChainID)
}
140 changes: 0 additions & 140 deletions tests/e2e/setup/setup.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package setup

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -408,93 +404,6 @@ func (s *E2ETestSuite) runValidators() {
)
}

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

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

gaiaVal := s.Chain.GaiaValidators[0]
// umeeVal for the relayer needs to be a different account
// than what we use for runPriceFeeder.
umeeVal := s.Chain.Validators[1]
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: "umee-gaia-relayer",
Repository: "ghcr.io/umee-network/hermes-e2e",
Tag: "latest",
NetworkID: s.DkrNet.Network.ID,
Mounts: []string{
fmt.Sprintf("%s/:/home/hermes", hermesCfgPath),
},
ExposedPorts: []string{"3031"},
PortBindings: map[docker.Port][]docker.PortBinding{
"3031/tcp": {{HostIP: "", HostPort: "3031"}},
},
Env: []string{
fmt.Sprintf("UMEE_E2E_GAIA_CHAIN_ID=%s", GaiaChainID),
fmt.Sprintf("UMEE_E2E_UMEE_CHAIN_ID=%s", s.Chain.ID),
fmt.Sprintf("UMEE_E2E_GAIA_VAL_MNEMONIC=%s", gaiaVal.mnemonic),
fmt.Sprintf("UMEE_E2E_UMEE_VAL_MNEMONIC=%s", umeeVal.mnemonic),
fmt.Sprintf("UMEE_E2E_GAIA_VAL_HOST=%s", s.GaiaResource.Container.Name[1:]),
fmt.Sprintf("UMEE_E2E_UMEE_VAL_HOST=%s", s.ValResources[1].Container.Name[1:]),
},
Entrypoint: []string{
"sh",
"-c",
"chmod +x /home/hermes/hermes_bootstrap.sh && /home/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)
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)

// create the client, connection and channel between the Umee and Gaia chains
s.connectIBCChains()
}

func (s *E2ETestSuite) initUmeeClient() {
var err error
mnemonics := make(map[string]string)
Expand All @@ -514,55 +423,6 @@ func (s *E2ETestSuite) initUmeeClient() {
s.Require().NoError(err)
}

func (s *E2ETestSuite) connectIBCChains() {
s.T().Logf("connecting %s and %s chains via IBC", s.Chain.ID, GaiaChainID)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

exec, err := s.DkrPool.Client.CreateExec(docker.CreateExecOptions{
Context: ctx,
AttachStdout: true,
AttachStderr: true,
Container: s.HermesResource.Container.ID,
User: "root",
Cmd: []string{
"hermes",
"create",
"channel",
s.Chain.ID,
GaiaChainID,
"--port-a=transfer",
"--port-b=transfer",
},
})
s.Require().NoError(err)

var (
outBuf bytes.Buffer
errBuf bytes.Buffer
)

err = s.DkrPool.Client.StartExec(exec.ID, docker.StartExecOptions{
Context: ctx,
Detach: false,
OutputStream: &outBuf,
ErrorStream: &errBuf,
})
s.Require().NoErrorf(
err,
"failed connect chains; stdout: %s, stderr: %s", outBuf.String(), errBuf.String(),
)

s.Require().Containsf(
errBuf.String(),
"successfully opened init channel",
"failed to connect chains via IBC: %s", errBuf.String(),
)

s.T().Logf("connected %s and %s chains via IBC", s.Chain.ID, GaiaChainID)
}

func noRestart(config *docker.HostConfig) {
// in this case we don't want the nodes to restart on failure
config.RestartPolicy = docker.RestartPolicy{
Expand Down

0 comments on commit 4acd3d0

Please sign in to comment.