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

Updates for rly v2.1.0 #295

Merged
merged 13 commits into from
Sep 20, 2022
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ jobs:
# run tests
- name: run all tests
# show cosmos chain and relayer logs on test failure
run: (go test -timeout 30m -v -p 2 ./...) || (echo "\n\n*****CHAIN and RELAYER LOGS*****" && cat "$HOME/.ibctest/logs/ibctest.log" && exit 1)
run: (go test -race -timeout 30m -v -p 2 ./...) || (echo "\n\n*****CHAIN and RELAYER LOGS*****" && cat "$HOME/.ibctest/logs/ibctest.log" && exit 1)
14 changes: 12 additions & 2 deletions chain/cosmos/chain_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ func (tn *ChainNode) InitValidatorGenTx(
if err := tn.CreateKey(ctx, valKey); err != nil {
return err
}
bech32, err := tn.KeyBech32(ctx, valKey)
bech32, err := tn.AccountKeyBech32(ctx, valKey)
if err != nil {
return err
}
Expand Down Expand Up @@ -925,12 +925,17 @@ func (tn *ChainNode) NodeID(ctx context.Context) (string, error) {
}

// KeyBech32 retrieves the named key's address in bech32 format from the node.
func (tn *ChainNode) KeyBech32(ctx context.Context, name string) (string, error) {
// bech is the bech32 prefix (acc|val|cons). If empty, defaults to the account key (same as "acc").
func (tn *ChainNode) KeyBech32(ctx context.Context, name string, bech string) (string, error) {
command := []string{tn.Chain.Config().Bin, "keys", "show", "--address", name,
"--home", tn.HomeDir(),
"--keyring-backend", keyring.BackendTest,
}

if bech != "" {
command = append(command, "--bech", bech)
}

stdout, stderr, err := tn.Exec(ctx, command, nil)
if err != nil {
return "", fmt.Errorf("failed to show key %q (stderr=%q): %w", name, stderr, err)
Expand All @@ -939,6 +944,11 @@ func (tn *ChainNode) KeyBech32(ctx context.Context, name string) (string, error)
return string(bytes.TrimSuffix(stdout, []byte("\n"))), nil
}

// AccountKeyBech32 retrieves the named key's address in bech32 account format.
func (tn *ChainNode) AccountKeyBech32(ctx context.Context, name string) (string, error) {
return tn.KeyBech32(ctx, name, "")
}

// PeerString returns the string for connecting the nodes passed in
func (nodes ChainNodes) PeerString(ctx context.Context) string {
addrs := make([]string, len(nodes))
Expand Down
44 changes: 30 additions & 14 deletions chain/cosmos/cosmos_chain.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package cosmos

import (
"bytes"
"context"
"fmt"
"io"
"os"
"strconv"
"strings"
"sync"
"time"

"github.com/avast/retry-go/v4"
Expand Down Expand Up @@ -40,6 +42,8 @@ type CosmosChain struct {
FullNodes ChainNodes

log *zap.Logger

findTxMu sync.Mutex
}

func NewCosmosHeighlinerChainConfig(name string,
Expand Down Expand Up @@ -156,6 +160,8 @@ func (c *CosmosChain) Initialize(ctx context.Context, testName string, cli *clie
}

func (c *CosmosChain) getFullNode() *ChainNode {
c.findTxMu.Lock()
defer c.findTxMu.Unlock()
if len(c.FullNodes) > 0 {
// use first full node
return c.FullNodes[0]
Expand Down Expand Up @@ -208,7 +214,7 @@ func (c *CosmosChain) RecoverKey(ctx context.Context, keyName, mnemonic string)

// Implements Chain interface
func (c *CosmosChain) GetAddress(ctx context.Context, keyName string) ([]byte, error) {
b32Addr, err := c.getFullNode().KeyBech32(ctx, keyName)
b32Addr, err := c.getFullNode().AccountKeyBech32(ctx, keyName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -504,6 +510,8 @@ func (c *CosmosChain) initializeChainNodes(
if err := eg.Wait(); err != nil {
return err
}
c.findTxMu.Lock()
defer c.findTxMu.Unlock()
c.Validators = newVals
c.FullNodes = newFullNodes
return nil
Expand Down Expand Up @@ -534,21 +542,16 @@ func (c *CosmosChain) Start(testName string, ctx context.Context, additionalGene
chainCfg := c.Config()

genesisAmount := types.Coin{
Amount: types.NewInt(1000000000000),
Amount: types.NewInt(10_000_000_000_000),
Denom: chainCfg.Denom,
}

genesisStakeAmount := types.Coin{
Amount: types.NewInt(1000000000000),
Denom: "stake",
}

genesisSelfDelegation := types.Coin{
Amount: types.NewInt(100000000000),
Denom: "stake",
Amount: types.NewInt(5_000_000_000_000),
Denom: chainCfg.Denom,
}

genesisAmounts := []types.Coin{genesisAmount, genesisStakeAmount}
genesisAmounts := []types.Coin{genesisAmount}

configFileOverrides := chainCfg.ConfigFileOverrides

Expand Down Expand Up @@ -622,7 +625,7 @@ func (c *CosmosChain) Start(testName string, ctx context.Context, additionalGene
for i := 1; i < len(c.Validators); i++ {
validatorN := c.Validators[i]

bech32, err := validatorN.KeyBech32(ctx, valKey)
bech32, err := validatorN.AccountKeyBech32(ctx, valKey)
if err != nil {
return err
}
Expand Down Expand Up @@ -651,16 +654,23 @@ func (c *CosmosChain) Start(testName string, ctx context.Context, additionalGene
return err
}

genbz = bytes.ReplaceAll(genbz, []byte(`"stake"`), []byte(fmt.Sprintf(`"%s"`, chainCfg.Denom)))

if c.cfg.ModifyGenesis != nil {
genbz, err = c.cfg.ModifyGenesis(chainCfg, genbz)
if err != nil {
return err
}
}

// Provide EXPORT_GENESIS_FILE_PATH to help debug genesis file
// Provide EXPORT_GENESIS_FILE_PATH and EXPORT_GENESIS_CHAIN to help debug genesis file
exportGenesis := os.Getenv("EXPORT_GENESIS_FILE_PATH")
if exportGenesis != "" {
exportGenesisChain := os.Getenv("EXPORT_GENESIS_CHAIN")
if exportGenesis != "" && exportGenesisChain == c.cfg.Name {
c.log.Debug("Exporting genesis file",
zap.String("chain", exportGenesisChain),
zap.String("path", exportGenesis),
)
_ = os.WriteFile(exportGenesis, genbz, 0600)
}

Expand Down Expand Up @@ -780,7 +790,10 @@ func (c *CosmosChain) Timeouts(ctx context.Context, height uint64) ([]ibc.Packet

// FindTxs implements blockdb.BlockSaver.
func (c *CosmosChain) FindTxs(ctx context.Context, height uint64) ([]blockdb.Tx, error) {
return c.getFullNode().FindTxs(ctx, height)
fn := c.getFullNode()
c.findTxMu.Lock()
defer c.findTxMu.Unlock()
return fn.FindTxs(ctx, height)
}

// StopAllNodes stops and removes all long running containers (validators and full nodes)
Expand All @@ -801,6 +814,9 @@ func (c *CosmosChain) StopAllNodes(ctx context.Context) error {
// StartAllNodes creates and starts new containers for each node.
// Should only be used if the chain has previously been started with .Start.
func (c *CosmosChain) StartAllNodes(ctx context.Context) error {
// prevent client calls during this time
c.findTxMu.Lock()
defer c.findTxMu.Unlock()
var eg errgroup.Group
for _, n := range c.Nodes() {
n := n
Expand Down
2 changes: 2 additions & 0 deletions chainspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ func (s *ChainSpec) Config() (*ibc.ChainConfig, error) {
cfg = ibc.ChainConfig{}
}

cfg = cfg.Clone()

// Apply any overrides from this ChainSpec.
cfg = cfg.MergeChainSpecConfig(s.ChainConfig)

Expand Down
3 changes: 2 additions & 1 deletion cmd/ibctest/ibctest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/strangelove-ventures/ibctest/v5/internal/blockdb"
blockdbtui "github.com/strangelove-ventures/ibctest/v5/internal/blockdb/tui"
"github.com/strangelove-ventures/ibctest/v5/internal/version"
"github.com/strangelove-ventures/ibctest/v5/relayer"
"github.com/strangelove-ventures/ibctest/v5/testreporter"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -171,7 +172,7 @@ func configureTestReporter() error {
func getRelayerFactory(name string, logger *zap.Logger) (ibctest.RelayerFactory, error) {
switch name {
case "rly", "cosmos/relayer":
return ibctest.NewBuiltinRelayerFactory(ibc.CosmosRly, logger), nil
return ibctest.NewBuiltinRelayerFactory(ibc.CosmosRly, logger, relayer.StartupFlags("-b", "100")), nil
case "hermes":
return ibctest.NewBuiltinRelayerFactory(ibc.Hermes, logger), nil
default:
Expand Down
2 changes: 2 additions & 0 deletions examples/cosmos_chain_upgrade_ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/strangelove-ventures/ibctest/v5/chain/cosmos"
"github.com/strangelove-ventures/ibctest/v5/conformance"
"github.com/strangelove-ventures/ibctest/v5/ibc"
"github.com/strangelove-ventures/ibctest/v5/relayer"
"github.com/strangelove-ventures/ibctest/v5/test"
"github.com/strangelove-ventures/ibctest/v5/testreporter"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -58,6 +59,7 @@ func CosmosChainUpgradeIBCTest(t *testing.T, chainName, initialVersion, upgradeV
rf := ibctest.NewBuiltinRelayerFactory(
ibc.CosmosRly,
zaptest.NewLogger(t),
relayer.StartupFlags("-b", "100"),
)

r := rf.Build(t, client, network)
Expand Down
2 changes: 1 addition & 1 deletion examples/interchain_queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestInterchainQueries(t *testing.T) {
r := ibctest.NewBuiltinRelayerFactory(
ibc.CosmosRly,
zaptest.NewLogger(t),
relayer.StartupFlags("-p", "events", "-b", "100"),
relayer.StartupFlags("-b", "100"),
).Build(t, client, network)

// Build the network; spin up the chains and configure the relayer
Expand Down
48 changes: 27 additions & 21 deletions examples/packet_forward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@ func TestPacketForwardMiddleware(t *testing.T) {

cf := ibctest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*ibctest.ChainSpec{
{Name: "gaia", ChainName: "gaia-fork", Version: "bugfix-replace_default_transfer_with_router_module"},
{Name: "osmosis", ChainName: "osmosis", Version: "v7.3.0"},
{Name: "juno", ChainName: "juno", Version: "v6.0.0"},
{Name: "osmosis", ChainName: "osmosis", Version: "v11.0.1"},
{Name: "juno", ChainName: "juno", Version: "v9.0.0"},
})

chains, err := cf.Chains(t.Name())
require.NoError(t, err)

gaia, osmosis, juno := chains[0], chains[1], chains[2]

r := ibctest.NewBuiltinRelayerFactory(ibc.CosmosRly, zaptest.NewLogger(t)).Build(
r := ibctest.NewBuiltinRelayerFactory(
ibc.CosmosRly,
zaptest.NewLogger(t),
).Build(
t, client, network,
)

Expand All @@ -63,9 +66,10 @@ func TestPacketForwardMiddleware(t *testing.T) {
})

require.NoError(t, ic.Build(ctx, eRep, ibctest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
TestName: t.Name(),
Client: client,
NetworkID: network,
BlockDatabaseFile: ibctest.DefaultBlockDatabaseFilepath(),

SkipPathCreation: false,
}))
Expand All @@ -76,14 +80,14 @@ func TestPacketForwardMiddleware(t *testing.T) {
const userFunds = int64(10_000_000_000)
users := ibctest.GetAndFundTestUsers(t, ctx, t.Name(), userFunds, osmosis, gaia, juno)

channels, err := r.GetChannels(ctx, eRep, gaia.Config().ChainID)
osmoChannels, err := r.GetChannels(ctx, eRep, osmosis.Config().ChainID)
require.NoError(t, err)

// Start the relayer on both paths
err = r.StartRelayer(ctx, eRep, pathOsmoHub)
junoChannels, err := r.GetChannels(ctx, eRep, juno.Config().ChainID)
require.NoError(t, err)

err = r.StartRelayer(ctx, eRep, pathJunoHub)
// Start the relayer on both paths
err = r.StartRelayer(ctx, eRep, pathOsmoHub, pathJunoHub)
require.NoError(t, err)

t.Cleanup(
Expand All @@ -96,24 +100,24 @@ func TestPacketForwardMiddleware(t *testing.T) {
)

// Get original account balances
osmosisUser := users[0]
gaiaUser := users[1]
junoUser := users[2]
osmosisUser, gaiaUser, junoUser := users[0], users[1], users[2]

osmosisBalOG, err := osmosis.GetBalance(ctx, osmosisUser.Bech32Address(osmosis.Config().Bech32Prefix), osmosis.Config().Denom)
require.NoError(t, err)

// Send packet from Osmosis->Hub->Juno
// receiver format: {intermediate_refund_address}|{foward_port}/{forward_channel}:{final_destination_address}
const transferAmount int64 = 100000
receiver := fmt.Sprintf("%s|%s/%s:%s", gaiaUser.Bech32Address(gaia.Config().Bech32Prefix), channels[1].PortID, channels[1].ChannelID, junoUser.Bech32Address(juno.Config().Bech32Prefix))
gaiaJunoChan := junoChannels[0].Counterparty
receiver := fmt.Sprintf("%s|%s/%s:%s", gaiaUser.Bech32Address(gaia.Config().Bech32Prefix), gaiaJunoChan.PortID, gaiaJunoChan.ChannelID, junoUser.Bech32Address(juno.Config().Bech32Prefix))
transfer := ibc.WalletAmount{
Address: receiver,
Denom: osmosis.Config().Denom,
Amount: transferAmount,
}

_, err = osmosis.SendIBCTransfer(ctx, channels[0].ChannelID, osmosisUser.KeyName, transfer, nil)
osmosisGaiaChan := osmoChannels[0]
_, err = osmosis.SendIBCTransfer(ctx, osmosisGaiaChan.ChannelID, osmosisUser.KeyName, transfer, nil)
require.NoError(t, err)

// Wait for transfer to be relayed
Expand All @@ -126,8 +130,10 @@ func TestPacketForwardMiddleware(t *testing.T) {
require.Equal(t, osmosisBalOG-transferAmount, osmosisBal)

// Compose the prefixed denoms and ibc denom for asserting balances
firstHopDenom := transfertypes.GetPrefixedDenom(channels[0].Counterparty.PortID, channels[0].Counterparty.ChannelID, osmosis.Config().Denom)
secondHopDenom := transfertypes.GetPrefixedDenom(channels[1].Counterparty.PortID, channels[1].Counterparty.ChannelID, firstHopDenom)
gaiaOsmoChan := osmoChannels[0].Counterparty
junoGaiaChan := junoChannels[0]
firstHopDenom := transfertypes.GetPrefixedDenom(gaiaOsmoChan.PortID, gaiaOsmoChan.ChannelID, osmosis.Config().Denom)
secondHopDenom := transfertypes.GetPrefixedDenom(junoGaiaChan.Counterparty.PortID, junoGaiaChan.Counterparty.ChannelID, firstHopDenom)
dstIbcDenom := transfertypes.ParseDenomTrace(secondHopDenom)

// Check that the funds sent are present in the acc on juno
Expand All @@ -136,14 +142,14 @@ func TestPacketForwardMiddleware(t *testing.T) {
require.Equal(t, transferAmount, junoBal)

// Send packet back from Juno->Hub->Osmosis
receiver = fmt.Sprintf("%s|%s/%s:%s", gaiaUser.Bech32Address(gaia.Config().Bech32Prefix), channels[0].Counterparty.PortID, channels[0].Counterparty.ChannelID, osmosisUser.Bech32Address(osmosis.Config().Bech32Prefix))
receiver = fmt.Sprintf("%s|%s/%s:%s", gaiaUser.Bech32Address(gaia.Config().Bech32Prefix), gaiaOsmoChan.PortID, gaiaOsmoChan.ChannelID, osmosisUser.Bech32Address(osmosis.Config().Bech32Prefix))
transfer = ibc.WalletAmount{
Address: receiver,
Denom: dstIbcDenom.IBCDenom(),
Amount: transferAmount,
}

_, err = juno.SendIBCTransfer(ctx, channels[1].Counterparty.ChannelID, junoUser.KeyName, transfer, nil)
_, err = juno.SendIBCTransfer(ctx, junoGaiaChan.ChannelID, junoUser.KeyName, transfer, nil)
require.NoError(t, err)

// Wait for transfer to be relayed
Expand All @@ -162,14 +168,14 @@ func TestPacketForwardMiddleware(t *testing.T) {

// Send a malformed packet with invalid receiver address from Osmosis->Hub->Juno
// This should succeed in the first hop and fail to make the second hop; funds should end up in the intermediary account.
receiver = fmt.Sprintf("%s|%s/%s:%s", gaiaUser.Bech32Address(gaia.Config().Bech32Prefix), channels[1].PortID, channels[1].ChannelID, "xyz1t8eh66t2w5k67kwurmn5gqhtq6d2ja0vp7jmmq")
receiver = fmt.Sprintf("%s|%s/%s:%s", gaiaUser.Bech32Address(gaia.Config().Bech32Prefix), gaiaJunoChan.PortID, gaiaJunoChan.ChannelID, "xyz1t8eh66t2w5k67kwurmn5gqhtq6d2ja0vp7jmmq")
transfer = ibc.WalletAmount{
Address: receiver,
Denom: osmosis.Config().Denom,
Amount: transferAmount,
}

_, err = osmosis.SendIBCTransfer(ctx, channels[0].ChannelID, osmosisUser.KeyName, transfer, nil)
_, err = osmosis.SendIBCTransfer(ctx, osmosisGaiaChan.ChannelID, osmosisUser.KeyName, transfer, nil)
require.NoError(t, err)

// Wait for transfer to be relayed
Expand Down
2 changes: 1 addition & 1 deletion ibc/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type Relayer interface {
// After configuration is initialized, begin relaying.
// This method is intended to create a background worker that runs the relayer.
// You must call StopRelayer to cleanly stop the relaying.
StartRelayer(ctx context.Context, rep RelayerExecReporter, pathName string) error
StartRelayer(ctx context.Context, rep RelayerExecReporter, pathNames ...string) error

// StopRelayer stops a relayer that started work through StartRelayer.
StopRelayer(ctx context.Context, rep RelayerExecReporter) error
Expand Down
8 changes: 8 additions & 0 deletions ibc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ type ChainConfig struct {
EncodingConfig *simappparams.EncodingConfig
}

func (c ChainConfig) Clone() ChainConfig {
x := c
images := make([]DockerImage, len(c.Images))
copy(images, c.Images)
x.Images = images
return x
}

func (c ChainConfig) MergeChainSpecConfig(other ChainConfig) ChainConfig {
// Make several in-place modifications to c,
// which is a value, not a reference,
Expand Down
Loading