Skip to content

Commit

Permalink
Updates for rly v2.1.0 (#295)
Browse files Browse the repository at this point in the history
* fix race condition

* test for race conditions in CI

* fix race with FindTx and StartAllNodes

* Clone ibc.ChainConfig because of slice

* remove unnecessary change

* Fix race 3/3 for c.FullNodes/c.Validators

* fix mutex lock placement for FindTxs

* Use native denom instead of stake for delegation. Increase faucet amount. Pass bech flag to KeyBech32. faucet reqs in parallel. link paths in parallel.

* Updates for v2.1.0

* relay multiple paths same process

* use -b 100 flag

* Fix packet forward middleware test

* Add AccountKeyBech32 for default account key use case
  • Loading branch information
agouin authored Sep 20, 2022
1 parent 27dccc3 commit 7653dfa
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 87 deletions.
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
29 changes: 16 additions & 13 deletions chain/cosmos/cosmos_chain.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cosmos

import (
"bytes"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -213,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 @@ -541,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 @@ -629,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 @@ -658,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
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
Loading

0 comments on commit 7653dfa

Please sign in to comment.