Skip to content

Commit

Permalink
Merge PR cosmos#569: Refactor & improve new path generation in the re…
Browse files Browse the repository at this point in the history
…layer

* use chain registry + formatting fixes

* remove fetch commands and add chain and path fetching behind chains and paths cmd trees

* actually fetch a chains assetlist.json file from chain registry

* fix quick-start guide

* small fixes to README.md

* more small fixes to README.md

* fix tabs in README.md

* fix tabs in README.md

* undo last changes

* minor changes to readme

* added ds_store

* Merge PR cosmos#564: Add command for creating a new blank path

* add command to create a new blank path in the config

* add retries & cleanup CreateClients

* use tagged lens version

* add logging of txs back into relayer

* add CreateClient & RelayMsg commands. Fix race conditions

* make ibc update headers query work in parallel + small fixes

* testing with updated lens repo

* add better retries to connection creation

* add better retries to client creation

* add better retries to channel creation

* fix lgtm bot warnings

* fix broken tests

* remove -race flag from tests

* fix broken bash script testing environment

* remove double import statement

Co-authored-by: Dan Kanefsky <daniel.kanefsky@gmail.com>
Co-authored-by: Jack Zampolin <jack.zampolin@gmail.com>
  • Loading branch information
3 people authored Feb 4, 2022
1 parent 32b2af9 commit 5bb5a67
Show file tree
Hide file tree
Showing 14 changed files with 779 additions and 148 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ test-akash:
@go test -mod=readonly -v -run TestAkashToGaiaRelaying ./test/...

test-short:
@go test -mod=readonly -v -run TestOsmoToGaiaRelaying ./test/...
@go test -mod=readonly -v -run TestOsmoToGaiaRelaying ./test/...

coverage:
@echo "viewing test coverage..."
Expand Down
2 changes: 1 addition & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ func initConfig(cmd *cobra.Command) error {
for _, pcfg := range cfgWrapper.ProviderConfigs {
prov, err := pcfg.Value.(provider.ProviderConfig).NewProvider(homePath, debug)
if err != nil {
return fmt.Errorf("Error while building ChainProviders. Err: %s\n", err.Error())
return fmt.Errorf("Error while building ChainProviders. Err: %w\n", err)
}

chain := &relayer.Chain{ChainProvider: prov}
Expand Down
140 changes: 139 additions & 1 deletion cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package cmd

import (
"fmt"
"strconv"
"strings"
"time"

"github.com/avast/retry-go"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ibc-go/v2/modules/core/exported"
"github.com/cosmos/relayer/relayer"
"github.com/spf13/cobra"
)
Expand All @@ -29,16 +32,19 @@ Most of these commands take a [path] argument. Make sure:
linkCmd(),
linkThenStartCmd(),
relayMsgsCmd(),
relayMsgCmd(),
relayAcksCmd(),
xfersend(),
flags.LineBreak,
createClientsCmd(),
createClientCmd(),
updateClientsCmd(),
upgradeClientsCmd(),
//upgradeChainCmd(),
createConnectionCmd(),
closeChannelCmd(),
flags.LineBreak,

//sendCmd(),
)

Expand Down Expand Up @@ -146,6 +152,95 @@ func createClientsCmd() *cobra.Command {
return overrideFlag(clientParameterFlags(cmd))
}

func createClientCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "client [src-chain-id] [dst-chain-id] [path-name]",
Short: "create a client between two configured chains with a configured path",
Long: "Creates a working ibc client for chain configured on each end of the" +
" path by querying headers from each chain and then sending the corresponding create-client messages",
Args: cobra.ExactArgs(3),
Example: strings.TrimSpace(fmt.Sprintf(`$ %s transact clients demo-path`, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
allowUpdateAfterExpiry, err := cmd.Flags().GetBool(flagUpdateAfterExpiry)
if err != nil {
return err
}

allowUpdateAfterMisbehaviour, err := cmd.Flags().GetBool(flagUpdateAfterMisbehaviour)
if err != nil {
return err
}

override, err := cmd.Flags().GetBool(flagOverride)
if err != nil {
return err
}

src := args[0]
dst := args[1]
c, err := config.Chains.Gets(src, dst)
if err != nil {
return err
}

pathName := args[2]
path, err := config.Paths.Get(pathName)
if err != nil {
return err
}

c[src].PathEnd = path.End(c[src].ChainID())
c[dst].PathEnd = path.End(c[dst].ChainID())

// ensure that keys exist
if exists := c[src].ChainProvider.KeyExists(c[src].ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on chain %s \n", c[src].ChainProvider.Key(), c[src].ChainID())
}
if exists := c[dst].ChainProvider.KeyExists(c[dst].ChainProvider.Key()); !exists {
return fmt.Errorf("key %s not found on chain %s \n", c[dst].ChainProvider.Key(), c[dst].ChainID())
}

// Query the latest heights on src and dst and retry if the query fails
var srch, dsth int64
if err = retry.Do(func() error {
srch, dsth, err = relayer.QueryLatestHeights(c[src], c[dst])
if srch == 0 || dsth == 0 || err != nil {
return fmt.Errorf("failed to query latest heights. Err: %w", err)
}
return err
}, relayer.RtyAtt, relayer.RtyDel, relayer.RtyErr); err != nil {
return err
}

// Query the light signed headers for src & dst at the heights srch & dsth, retry if the query fails
var srcUpdateHeader, dstUpdateHeader exported.Header
if err = retry.Do(func() error {
srcUpdateHeader, dstUpdateHeader, err = relayer.GetLightSignedHeadersAtHeights(c[src], c[dst], srch, dsth)
if err != nil {
return fmt.Errorf("failed to query light signed headers. Err: %w", err)
}
return err
}, relayer.RtyAtt, relayer.RtyDel, relayer.RtyErr, retry.OnRetry(func(n uint, err error) {
c[src].LogRetryGetLightSignedHeader(n, err)
srch, dsth, _ = relayer.QueryLatestHeights(c[src], c[dst])
})); err != nil {
return err
}

modified, err := relayer.CreateClient(c[src], c[dst], srcUpdateHeader, dstUpdateHeader, allowUpdateAfterExpiry, allowUpdateAfterMisbehaviour, override)
if modified {
if err = overWriteConfig(config); err != nil {
return err
}
}

return nil
},
}

return overrideFlag(clientParameterFlags(cmd))
}

func updateClientsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "update-clients [path-name]",
Expand Down Expand Up @@ -407,7 +502,7 @@ $ %s tx connect demo-path`,
}

// create channel if it isn't already created
modified, err = c[src].CreateOpenChannels(c[dst], 3, to)
modified, err = c[src].CreateOpenChannels(c[dst], retries, to)
if modified {
if err := overWriteConfig(config); err != nil {
return err
Expand Down Expand Up @@ -452,6 +547,49 @@ $ %s tx link-then-start demo-path --timeout 5s`, appName, appName)),
return overrideFlag(clientParameterFlags(strategyFlag(retryFlag(timeoutFlag(cmd)))))
}

func relayMsgCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "relay-packet [path-name] [seq-num]",
Aliases: []string{"relay-pkt"},
Short: "relay a non-relayed packet with a specific sequence number, in both directions",
Args: cobra.ExactArgs(2),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s transact relay-packet demo-path 1
$ %s tx relay-pkt demo-path 1`,
appName, appName,
)),
RunE: func(cmd *cobra.Command, args []string) error {
c, src, dst, err := config.ChainsFromPath(args[0])
if err != nil {
return err
}

if err = ensureKeysExist(c); err != nil {
return err
}

maxTxSize, maxMsgLength, err := GetStartOptions(cmd)
if err != nil {
return err
}

seqNum, err := strconv.Atoi(args[1])
if err != nil {
return err
}

sp, err := relayer.UnrelayedSequences(c[src], c[dst])
if err != nil {
return err
}

return relayer.RelayPacket(c[src], c[dst], sp, maxTxSize, maxMsgLength, uint64(seqNum))
},
}

return strategyFlag(cmd)
}

func relayMsgsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "relay-packets [path-name]",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (

require (
github.com/pkg/errors v0.9.1
github.com/strangelove-ventures/lens v0.2.1
github.com/strangelove-ventures/lens v0.2.2-0.20220131192754-f2a69f2e3fd7
)

require (
Expand Down
28 changes: 28 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1119,8 +1119,36 @@ github.com/strangelove-ventures/lens v0.2.1-0.20220122022854-f56147e20e5d h1:p54
github.com/strangelove-ventures/lens v0.2.1-0.20220122022854-f56147e20e5d/go.mod h1:QSKpnL9bDirjJsMApJoHmVU3ZBujFnbXsvY9d5JG8M8=
github.com/strangelove-ventures/lens v0.2.1-0.20220122023000-6e6ff07a5193 h1:LAL3EP8RfxaEf+iZqHTpq5xmlKJkcZbDQVGutAJQB58=
github.com/strangelove-ventures/lens v0.2.1-0.20220122023000-6e6ff07a5193/go.mod h1:QSKpnL9bDirjJsMApJoHmVU3ZBujFnbXsvY9d5JG8M8=
github.com/strangelove-ventures/lens v0.2.1-0.20220124194236-ba2d744c75fa h1:FN4EJIaG0vRqm8VgyzDxDowBC1hcQoVLl7lj0/cGUdM=
github.com/strangelove-ventures/lens v0.2.1-0.20220124194236-ba2d744c75fa/go.mod h1:QSKpnL9bDirjJsMApJoHmVU3ZBujFnbXsvY9d5JG8M8=
github.com/strangelove-ventures/lens v0.2.1-0.20220124200714-c0e835fd6bc4 h1:aw972fGIxQ13w0OxCcIb6bHQqHsxJltRn6tw5X9PbWA=
github.com/strangelove-ventures/lens v0.2.1-0.20220124200714-c0e835fd6bc4/go.mod h1:QSKpnL9bDirjJsMApJoHmVU3ZBujFnbXsvY9d5JG8M8=
github.com/strangelove-ventures/lens v0.2.1-0.20220124202932-3b0f30df4040 h1:LCpKzDyqazxdkj03psIsbJZMwehQe6jq3TCOJt3tPFQ=
github.com/strangelove-ventures/lens v0.2.1-0.20220124202932-3b0f30df4040/go.mod h1:QSKpnL9bDirjJsMApJoHmVU3ZBujFnbXsvY9d5JG8M8=
github.com/strangelove-ventures/lens v0.2.1-0.20220124213244-c8de769cb83d h1:t6pwRMNdjRNkLGAQRE0Mfcmq6iybf5fDGVQBBYrQRY8=
github.com/strangelove-ventures/lens v0.2.1-0.20220124213244-c8de769cb83d/go.mod h1:QSKpnL9bDirjJsMApJoHmVU3ZBujFnbXsvY9d5JG8M8=
github.com/strangelove-ventures/lens v0.2.1 h1:naP3VyQKh9b3sYHZMecBmv4sjSRC1PQzPmKyBpSF5/k=
github.com/strangelove-ventures/lens v0.2.1/go.mod h1:QSKpnL9bDirjJsMApJoHmVU3ZBujFnbXsvY9d5JG8M8=
github.com/strangelove-ventures/lens v0.2.2-0.20220125165849-21346dbd1f6b h1:xzOR0OlfOxwUaRivKUMm7RWPpAONXGP3POL2kw97vVE=
github.com/strangelove-ventures/lens v0.2.2-0.20220125165849-21346dbd1f6b/go.mod h1:QSKpnL9bDirjJsMApJoHmVU3ZBujFnbXsvY9d5JG8M8=
github.com/strangelove-ventures/lens v0.2.2-0.20220125221701-305c589edb5d h1:fpi5gBJD7X1yS2yfNng4SxLqQMkv9InoObtdGrgWMSw=
github.com/strangelove-ventures/lens v0.2.2-0.20220125221701-305c589edb5d/go.mod h1:QSKpnL9bDirjJsMApJoHmVU3ZBujFnbXsvY9d5JG8M8=
github.com/strangelove-ventures/lens v0.2.2-0.20220125222839-86536e89a85e h1:8qvfIhGxJNdSnOZz+7L7Zp9D00z6Td8mybYNoTXpt2o=
github.com/strangelove-ventures/lens v0.2.2-0.20220125222839-86536e89a85e/go.mod h1:QSKpnL9bDirjJsMApJoHmVU3ZBujFnbXsvY9d5JG8M8=
github.com/strangelove-ventures/lens v0.2.2-0.20220125223041-5729e6a4a1e1 h1:KVNioTrEOQl9NvoqCyiRL0tK3i0OOKJj1MoATB+3HxE=
github.com/strangelove-ventures/lens v0.2.2-0.20220125223041-5729e6a4a1e1/go.mod h1:QSKpnL9bDirjJsMApJoHmVU3ZBujFnbXsvY9d5JG8M8=
github.com/strangelove-ventures/lens v0.2.2-0.20220127175506-c30f1b75eb70 h1:SR02xBZ6VOn3VyMXsfdaBK1OsYrE9NBGBiDKYP1TGOc=
github.com/strangelove-ventures/lens v0.2.2-0.20220127175506-c30f1b75eb70/go.mod h1:QSKpnL9bDirjJsMApJoHmVU3ZBujFnbXsvY9d5JG8M8=
github.com/strangelove-ventures/lens v0.2.2-0.20220127183102-acd44266fed9 h1:3YE+qiQQ/+efRM/aRrflXZBGVz5DPwsT2QP7vhygEu4=
github.com/strangelove-ventures/lens v0.2.2-0.20220127183102-acd44266fed9/go.mod h1:QSKpnL9bDirjJsMApJoHmVU3ZBujFnbXsvY9d5JG8M8=
github.com/strangelove-ventures/lens v0.2.2-0.20220127184319-56d6f5e912c6 h1:TmT0iWbayW1+inQil6oXSqGghi8ZNFx0nHToUt0fXMg=
github.com/strangelove-ventures/lens v0.2.2-0.20220127184319-56d6f5e912c6/go.mod h1:QSKpnL9bDirjJsMApJoHmVU3ZBujFnbXsvY9d5JG8M8=
github.com/strangelove-ventures/lens v0.2.2-0.20220128163830-0f63f3a94653 h1:NicvNuR8QkLvuXHywj9ZLrU8eHMhebUU0+HFEusba+Q=
github.com/strangelove-ventures/lens v0.2.2-0.20220128163830-0f63f3a94653/go.mod h1:QSKpnL9bDirjJsMApJoHmVU3ZBujFnbXsvY9d5JG8M8=
github.com/strangelove-ventures/lens v0.2.2-0.20220131191229-2c7ba13ad8c8 h1:M5mLJ07qLKvD70SoeSkXSw2IIxEDO2/9Zh6dHKrK/l0=
github.com/strangelove-ventures/lens v0.2.2-0.20220131191229-2c7ba13ad8c8/go.mod h1:QSKpnL9bDirjJsMApJoHmVU3ZBujFnbXsvY9d5JG8M8=
github.com/strangelove-ventures/lens v0.2.2-0.20220131192754-f2a69f2e3fd7 h1:3vwNTIh39q4Vl8tkmV02lAS37SZ6WrkKX/GlsjdVqqg=
github.com/strangelove-ventures/lens v0.2.2-0.20220131192754-f2a69f2e3fd7/go.mod h1:QSKpnL9bDirjJsMApJoHmVU3ZBujFnbXsvY9d5JG8M8=
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI=
Expand Down
76 changes: 72 additions & 4 deletions relayer/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,37 @@ package relayer
import (
"encoding/json"
"fmt"
"github.com/avast/retry-go"
"net/url"
"os"
"strings"
"time"

"github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/avast/retry-go"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
authz "github.com/cosmos/cosmos-sdk/x/authz/module"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/capability"
"github.com/cosmos/cosmos-sdk/x/crisis"
"github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/upgrade"
"github.com/cosmos/ibc-go/v2/modules/apps/transfer"
ibc "github.com/cosmos/ibc-go/v2/modules/core"

cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
simparams "github.com/cosmos/cosmos-sdk/simapp/params"
distrclient "github.com/cosmos/cosmos-sdk/x/distribution/client"
feegrant "github.com/cosmos/cosmos-sdk/x/feegrant/module"
"github.com/cosmos/cosmos-sdk/x/params"
paramsclient "github.com/cosmos/cosmos-sdk/x/params/client"
upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client"
clienttypes "github.com/cosmos/ibc-go/v2/modules/core/02-client/types"
"github.com/cosmos/relayer/relayer/provider"
"github.com/gogo/protobuf/proto"
Expand All @@ -21,6 +45,26 @@ var (
RtyAtt = retry.Attempts(RtyAttNum)
RtyDel = retry.Delay(time.Millisecond * 400)
RtyErr = retry.LastErrorOnly(true)

ModuleBasics = []module.AppModuleBasic{
auth.AppModuleBasic{},
authz.AppModuleBasic{},
bank.AppModuleBasic{},
capability.AppModuleBasic{},
gov.NewAppModuleBasic(
paramsclient.ProposalHandler, distrclient.ProposalHandler, upgradeclient.ProposalHandler, upgradeclient.CancelProposalHandler,
),
crisis.AppModuleBasic{},
distribution.AppModuleBasic{},
feegrant.AppModuleBasic{},
mint.AppModuleBasic{},
params.AppModuleBasic{},
slashing.AppModuleBasic{},
staking.AppModuleBasic{},
upgrade.AppModuleBasic{},
transfer.AppModuleBasic{},
ibc.AppModuleBasic{},
}
)

// Chain represents the necessary data for connecting to and identifying a chain and its counterparties
Expand All @@ -30,13 +74,34 @@ type Chain struct {
Chainid string `yaml:"chain-id" json:"chain-id"`
RPCAddr string `yaml:"rpc-addr" json:"rpc-addr"`

PathEnd *PathEnd `yaml:"-" json:"-"`
Encoding params.EncodingConfig `yaml:"-" json:"-"`
PathEnd *PathEnd `yaml:"-" json:"-"`
Encoding simparams.EncodingConfig `yaml:"-" json:"-"`

logger log.Logger
debug bool
}

func MakeCodec(moduleBasics []module.AppModuleBasic) simparams.EncodingConfig {
modBasic := module.NewBasicManager(moduleBasics...)
encodingConfig := MakeCodecConfig()
std.RegisterLegacyAminoCodec(encodingConfig.Amino)
std.RegisterInterfaces(encodingConfig.InterfaceRegistry)
modBasic.RegisterLegacyAminoCodec(encodingConfig.Amino)
modBasic.RegisterInterfaces(encodingConfig.InterfaceRegistry)
return encodingConfig
}

func MakeCodecConfig() simparams.EncodingConfig {
interfaceRegistry := cdctypes.NewInterfaceRegistry()
marshaler := codec.NewProtoCodec(interfaceRegistry)
return simparams.EncodingConfig{
InterfaceRegistry: interfaceRegistry,
Marshaler: marshaler,
TxConfig: tx.NewTxConfig(marshaler, tx.DefaultSignModes),
Amino: codec.NewLegacyAmino(),
}
}

// ValidatePaths takes two chains and validates their paths
func ValidatePaths(src, dst *Chain) error {
if err := src.PathEnd.ValidateFull(); err != nil {
Expand Down Expand Up @@ -101,6 +166,9 @@ func (c *Chain) Init(logger log.Logger, debug bool) {
if c.logger == nil {
c.logger = defaultChainLogger()
}

// TODO logging/encoding needs refactored
c.Encoding = MakeCodec(ModuleBasics)
}

func defaultChainLogger() log.Logger {
Expand Down
Loading

0 comments on commit 5bb5a67

Please sign in to comment.