Skip to content

Commit

Permalink
Merge PR #36: Add fetch cmds for configuring chains and canonical paths
Browse files Browse the repository at this point in the history
* add chain info

* add outline of fetch cmds

* download chain json file outline

* update json for test

* configure fetched chain automatically

* don't validate if client/conn ids are empty

* fetch paths for configured chains

* add default key & rpc-addr values

* adding path json files

* add hub path files

* testing

* revert changes

* better err handling and output

* finish path json files

* oops

* remove dead code & point to main
  • Loading branch information
jtieri authored and jackzampolin committed Oct 25, 2021
1 parent ba064cc commit 149533c
Show file tree
Hide file tree
Showing 101 changed files with 2,289 additions and 10 deletions.
8 changes: 5 additions & 3 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func cfgFilesAddPaths(dir string) (cfg *Config, err error) {
return nil, fmt.Errorf("failed to add path %s: %w", pth, err)
}

fmt.Printf("added path %s...\n", pthName)
fmt.Printf("added path %s...\n\n", pthName)
}

return cfg, nil
Expand Down Expand Up @@ -569,11 +569,13 @@ func (c *Config) ValidatePathEnd(pe *relayer.PathEnd) error {

chain, err := c.Chains.Get(pe.ChainID)
if err != nil {
return err
fmt.Printf("Chain %s is not currently configured. \n"+
"Run `rly fetch chain %s` if you plan to relay to/from this chain. \n", pe.ChainID, pe.ChainID)
return nil
}

// if the identifiers are empty, don't do any validation
if pe.ClientID == "" && pe.ConnectionID == "" && pe.ChannelID == "" {
if pe.ClientID == "" && pe.ConnectionID == "" {
return nil
}

Expand Down
164 changes: 164 additions & 0 deletions cmd/fetch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"github.com/cosmos/relayer/relayer"
"github.com/go-git/go-git/v5"
"github.com/spf13/cobra"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"strings"
)

const (
jsonURL = "https://raw.githubusercontent.com/strangelove-ventures/relayer/main/interchain/chains/"
repoURL = "https://github.com/strangelove-ventures/relayer"
)

func fetchCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "fetch",
Aliases: []string{"fch"},
Short: "Fetch canonical chain and path info for bootstrapping the relayer",
}

cmd.AddCommand(
fetchChainCmd(),
fetchPathsCmd(),
)

return cmd
}

func fetchChainCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "chain [chain-id]",
Args: cobra.ExactArgs(1),
Aliases: []string{"chn", "c"},
Short: "Fetches the json file necessary to configure the specified chain",
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s fetch chain osmosis-1 --home %s
$ %s fch chn cosmoshub-4`, appName, defaultHome, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
chainID := args[0]
fName := fmt.Sprintf("%s.json", chainID)

// Check that the constructed URL is valid
u, err := url.Parse(fmt.Sprintf("%s%s", jsonURL, fName))
if err != nil || u.Scheme == "" || u.Host == "" {
return errors.New("invalid URL")
}

// Fetch the json file via HTTP request
resp, err := http.Get(u.String())
if err != nil {
return fmt.Errorf("Error fetching data for %s be sure it's data is in %s. err: %s \n", chainID, repoURL, err)
}
defer resp.Body.Close()

// Decode the HTTP response and attempt to add chain to the config file
var c = &relayer.Chain{
Key: "",
ChainID: "",
RPCAddr: "",
AccountPrefix: "",
GasAdjustment: 0,
GasPrices: "",
TrustingPeriod: "",
}
d := json.NewDecoder(resp.Body)
d.DisallowUnknownFields()

if err = d.Decode(c); err != nil {
return fmt.Errorf("Error fetching data for %s be sure it's data is in %s. err: %s \n", chainID, repoURL, err)
}

if err = config.AddChain(c); err != nil {
return fmt.Errorf("Error fetching data for %s be sure it's data is in %s. err: %s \n", chainID, repoURL, err)
}

err = overWriteConfig(config)
if err != nil {
return fmt.Errorf("Be sure you have initialized the relayer config with `rly config init` err: %s \n", err)
}

fmt.Printf("Successfully added %s to the relayer configuration. \n", chainID)
fmt.Printf("Be sure to change default key & rpc-addr values in %s. \n", homePath)

return nil
},
}
return cmd
}

func fetchPathsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "paths",
Aliases: []string{"pths"},
Short: "Fetches the json files necessary to setup the paths for the configured chains",
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s fetch paths --home %s
$ %s fch pths`, appName, defaultHome, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
// Clone the GH repo to tmp dir, we will extract the path files from here
localRepo, err := ioutil.TempDir("", "")
if err != nil {
return err
}

if _, err = git.PlainClone(localRepo, false, &git.CloneOptions{
URL: repoURL,
Progress: ioutil.Discard,
ReferenceName: "refs/heads/main",
}); err != nil {
return err
}

// Try to fetch path info for each configured chain that has canonical chain/path info in GH localRepo
for _, c := range config.Chains {
fName := fmt.Sprintf("%s.json", c.ChainID)

// Check that the constructed URL is valid
u, err := url.Parse(fmt.Sprintf("%s%s", jsonURL, fName))
if err != nil || u.Scheme == "" || u.Host == "" {
cleanupDir(localRepo)
return errors.New("invalid URL")
}

// Check that the chain c, has provided canonical chain/path info in GH localRepo
resp, err := http.Get(u.String())
if err != nil || resp.StatusCode == 404 {
fmt.Printf("Chain %s is not currently supported by fetch. Consider adding it's info to %s \n", c.ChainID, repoURL)
continue
}

// Add paths to rly config from {localRepo}/interchain/chaind-id
pathsDir := path.Join(localRepo, "interchain", c.ChainID)

var cfg *Config
if cfg, err = cfgFilesAddPaths(pathsDir); err != nil {
fmt.Printf("Failed to add files from %s for chain %s. \n", pathsDir, c.ChainID)
continue
}

err = overWriteConfig(cfg)
if err != nil {
return err
}
}

cleanupDir(localRepo)
return nil
},
}
return cmd
}

func cleanupDir(dir string) {
_ = os.RemoveAll(dir)
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func NewRootCmd() *cobra.Command {
devCommand(),
testnetsCmd(),
getVersionCmd(),
fetchCmd(),
)

// This is a bit of a cheat :shushing_face:
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ module github.com/cosmos/relayer
go 1.15

require (
github.com/Microsoft/go-winio v0.4.15 // indirect
github.com/avast/retry-go v2.6.0+incompatible
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/containerd/continuity v0.0.0-20200928162600-f2cc35102c2a // indirect
github.com/cosmos/cosmos-sdk v0.43.0
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/ibc-go v1.0.1
github.com/go-git/go-git/v5 v5.4.2
github.com/gogo/protobuf v1.3.3
github.com/gorilla/mux v1.8.0
github.com/kr/text v0.2.0 // indirect
github.com/lib/pq v1.9.0 // indirect
github.com/moby/term v0.0.0-20201101162038-25d840ce174a // indirect
github.com/onsi/ginkgo v1.14.2 // indirect
Expand Down
Loading

0 comments on commit 149533c

Please sign in to comment.