e2e_util_test.go 14.32 KiB
package e2e
import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"regexp"
	"strconv"
	"strings"
	"time"
	sdk "github.com/cosmos/cosmos-sdk/types"
	banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
	govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
	stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
	"github.com/ory/dockertest/v3/docker"
	"github.com/osmosis-labs/osmosis/v10/tests/e2e/initialization"
	"github.com/osmosis-labs/osmosis/v10/tests/e2e/util"
	superfluidtypes "github.com/osmosis-labs/osmosis/v10/x/superfluid/types"
func (s *IntegrationTestSuite) ExecTx(chainId string, validatorIndex int, command []string, success string) (bytes.Buffer, bytes.Buffer, error) {
	ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()
	var containerId string
	if chainId == "" {
		containerId = s.hermesResource.Container.ID
	} else {
		containerId = s.valResources[chainId][validatorIndex].Container.ID
	var (
		outBuf bytes.Buffer
		errBuf bytes.Buffer
	s.Require().Eventually(
		func() bool {
			exec, err := s.dkrPool.Client.CreateExec(docker.CreateExecOptions{
				Context:      ctx,
				AttachStdout: true,
				AttachStderr: true,
				Container:    containerId,
				User:         "root",
				Cmd:          command,
			s.Require().NoError(err)
			err = s.dkrPool.Client.StartExec(exec.ID, docker.StartExecOptions{
				Context:      ctx,
				Detach:       false,
				OutputStream: &outBuf,
				ErrorStream:  &errBuf,
			if err != nil {
				return false
			if success != "" {
				return strings.Contains(outBuf.String(), success) || strings.Contains(errBuf.String(), success)
			return true
		time.Minute,
		time.Second,
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
"tx returned a non-zero code; stdout: %s, stderr: %s", outBuf.String(), errBuf.String(), ) return outBuf, errBuf, nil } func (s *IntegrationTestSuite) ExecQueryRPC(path string) ([]byte, error) { var err error var resp *http.Response retriesLeft := 5 for { resp, err = http.Get(path) if resp.StatusCode == http.StatusServiceUnavailable { retriesLeft-- if retriesLeft == 0 { return nil, err } time.Sleep(10 * time.Second) } else { break } } if err != nil { return nil, fmt.Errorf("failed to execute HTTP request: %w", err) } defer resp.Body.Close() bz, err := io.ReadAll(resp.Body) if err != nil { return nil, err } return bz, nil } func (s *IntegrationTestSuite) connectIBCChains(chainA *chainConfig, chainB *chainConfig) { s.T().Logf("connecting %s and %s chains via IBC", chainA.meta.Id, chainB.meta.Id) cmd := []string{"hermes", "create", "channel", chainA.meta.Id, chainB.meta.Id, "--port-a=transfer", "--port-b=transfer"} s.ExecTx("", 0, cmd, "successfully opened init channel") s.T().Logf("connected %s and %s chains via IBC", chainA.meta.Id, chainB.meta.Id) } func (s *IntegrationTestSuite) sendIBC(srcChain *chainConfig, dstChain *chainConfig, recipient string, token sdk.Coin) { cmd := []string{"hermes", "tx", "raw", "ft-transfer", dstChain.meta.Id, srcChain.meta.Id, "transfer", "channel-0", token.Amount.String(), fmt.Sprintf("--denom=%s", token.Denom), fmt.Sprintf("--receiver=%s", recipient), "--timeout-height-offset=1000"} s.ExecTx("", 0, cmd, "Success") s.T().Logf("sending %s from %s to %s (%s)", token, srcChain.meta.Id, dstChain.meta.Id, recipient) balancesBPre, err := s.queryBalances(dstChain, 0, recipient) s.Require().NoError(err) s.Require().Eventually( func() bool { balancesBPost, err := s.queryBalances(dstChain, 0, recipient) s.Require().NoError(err) ibcCoin := balancesBPost.Sub(balancesBPre) if ibcCoin.Len() == 1 { tokenPre := balancesBPre.AmountOfNoDenomValidation(ibcCoin[0].Denom) tokenPost := balancesBPost.AmountOfNoDenomValidation(ibcCoin[0].Denom) resPre := initialization.OsmoToken.Amount resPost := tokenPost.Sub(tokenPre) return resPost.Uint64() == resPre.Uint64() } else { return false } }, 5*time.Minute, time.Second, "tx not received on destination chain",
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
) s.T().Log("successfully sent IBC tokens") } func (s *IntegrationTestSuite) submitUpgradeProposal(c *chainConfig, initialDeposit sdk.Coin) { upgradeHeightStr := strconv.Itoa(c.propHeight) s.T().Logf("submitting upgrade proposal on %s container: %s", s.valResources[c.meta.Id][0].Container.Name[1:], s.valResources[c.meta.Id][0].Container.ID) cmd := []string{"osmosisd", "tx", "gov", "submit-proposal", "software-upgrade", upgradeVersion, fmt.Sprintf("--title=\"%s upgrade\"", upgradeVersion), "--description=\"upgrade proposal submission\"", fmt.Sprintf("--upgrade-height=%s", upgradeHeightStr), "--upgrade-info=\"\"", fmt.Sprintf("--chain-id=%s", c.meta.Id), "--from=val", "-b=block", "--yes", "--keyring-backend=test", "--log_format=json", fmt.Sprintf("--deposit=%s", initialDeposit)} s.ExecTx(c.meta.Id, 0, cmd, "code: 0") s.T().Log("successfully submitted upgrade proposal") c.latestProposalNumber = c.latestProposalNumber + 1 } func (s *IntegrationTestSuite) submitSuperfluidProposal(c *chainConfig, asset string, initialDeposit sdk.Coin) { s.T().Logf("submitting superfluid proposal for asset %s on %s container: %s", asset, s.valResources[c.meta.Id][0].Container.Name[1:], s.valResources[c.meta.Id][0].Container.ID) cmd := []string{"osmosisd", "tx", "gov", "submit-proposal", "set-superfluid-assets-proposal", fmt.Sprintf("--superfluid-assets=%s", asset), fmt.Sprintf("--title=\"%s superfluid asset\"", asset), fmt.Sprintf("--description=\"%s superfluid asset\"", asset), "--from=val", "-b=block", "--yes", "--keyring-backend=test", "--log_format=json", fmt.Sprintf("--chain-id=%s", c.meta.Id), fmt.Sprintf("--deposit=%s", initialDeposit)} s.ExecTx(c.meta.Id, 0, cmd, "code: 0") s.T().Log("successfully submitted superfluid proposal") c.latestProposalNumber = c.latestProposalNumber + 1 } func (s *IntegrationTestSuite) submitTextProposal(c *chainConfig, text string, initialDeposit sdk.Coin) { s.T().Logf("submitting text proposal on %s container: %s", s.valResources[c.meta.Id][0].Container.Name[1:], s.valResources[c.meta.Id][0].Container.ID) cmd := []string{"osmosisd", "tx", "gov", "submit-proposal", "--type=text", fmt.Sprintf("--title=\"%s\"", text), "--description=\"test text proposal\"", "--from=val", "-b=block", "--yes", "--keyring-backend=test", "--log_format=json", fmt.Sprintf("--chain-id=%s", c.meta.Id), fmt.Sprintf("--deposit=%s", initialDeposit)} s.ExecTx(c.meta.Id, 0, cmd, "code: 0") s.T().Log("successfully submitted text proposal") c.latestProposalNumber = c.latestProposalNumber + 1 } func (s *IntegrationTestSuite) depositProposal(c *chainConfig) { propStr := strconv.Itoa(c.latestProposalNumber) s.T().Logf("depositing to proposal from %s container: %s", s.valResources[c.meta.Id][0].Container.Name[1:], s.valResources[c.meta.Id][0].Container.ID) cmd := []string{"osmosisd", "tx", "gov", "deposit", propStr, "500000000uosmo", "--from=val", fmt.Sprintf("--chain-id=%s", c.meta.Id), "-b=block", "--yes", "--keyring-backend=test"} s.ExecTx(c.meta.Id, 0, cmd, "code: 0") s.T().Log("successfully deposited to proposal") } func (s *IntegrationTestSuite) voteProposal(c *chainConfig) { propStr := strconv.Itoa(c.latestProposalNumber) s.T().Logf("voting yes on proposal for chain-id: %s", c.meta.Id) cmd := []string{"osmosisd", "tx", "gov", "vote", propStr, "yes", "--from=val", fmt.Sprintf("--chain-id=%s", c.meta.Id), "-b=block", "--yes", "--keyring-backend=test"} for i := range c.validators { if _, ok := c.skipRunValidatorIndexes[i]; ok { continue } s.ExecTx(c.meta.Id, i, cmd, "code: 0") s.T().Logf("successfully voted yes on proposal from %s container: %s", s.valResources[c.meta.Id][i].Container.Name[1:], s.valResources[c.meta.Id][i].Container.ID) } } func (s *IntegrationTestSuite) voteNoProposal(c *chainConfig, i int, from string) { propStr := strconv.Itoa(c.latestProposalNumber) s.T().Logf("voting no on proposal for chain-id: %s", c.meta.Id) cmd := []string{"osmosisd", "tx", "gov", "vote", propStr, "no", fmt.Sprintf("--from=%s", from), fmt.Sprintf("--chain-id=%s", c.meta.Id), "-b=block", "--yes", "--keyring-backend=test"} s.ExecTx(c.meta.Id, i, cmd, "code: 0") s.T().Logf("successfully voted no for proposal from %s container: %s", s.valResources[c.meta.Id][i].Container.Name[1:], s.valResources[c.meta.Id][i].Container.ID) } func (s *IntegrationTestSuite) chainStatus(c *chainConfig, i int) []byte { cmd := []string{"osmosisd", "status"} _, errBuf, err := s.ExecTx(c.meta.Id, i, cmd, "") s.Require().NoError(err) return errBuf.Bytes() } func (s *IntegrationTestSuite) getCurrentChainHeight(c *chainConfig, i int) int { var block syncInfo s.Require().Eventually( func() bool {