Skip to content

Commit

Permalink
Merge PR #785: point path fetch to chain-reg and avoid overwriting …
Browse files Browse the repository at this point in the history
…channel-filters

* point pth fetch to chain-reg

* mod file include necessities for integration

* specify go-git versino

* upgrade go-git version and change variable names

* use path.Join

* use io.Readall

* use FprintF

* remove underscore

* defer statment and return errors

* use error.As

* update function name

* properly close GH client

* use permute package

* minor clean up

* error handling

* clean up loop

* mod tidy
  • Loading branch information
boojamya authored Jun 15, 2022
1 parent 26f5157 commit 29b3628
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 126 deletions.
10 changes: 10 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
flagOrder = "order"
flagVersion = "version"
flagDebugAddr = "debug-addr"
flagOverwriteConfig = "overwrite"
flagOffset = "offset"
flagLimit = "limit"
flagHeight = "height"
Expand Down Expand Up @@ -285,3 +286,12 @@ func debugServerFlags(v *viper.Viper, cmd *cobra.Command) *cobra.Command {
}
return cmd
}

func OverwriteConfigFlag(v *viper.Viper, cmd *cobra.Command) *cobra.Command {
cmd.Flags().BoolP(flagOverwriteConfig, "o", false,
"overwrite already configured paths - will clear channel filter(s)")
if err := v.BindPFlag(flagOverwriteConfig, cmd.Flags().Lookup(flagOverwriteConfig)); err != nil {
panic(err)
}
return cmd
}
138 changes: 75 additions & 63 deletions cmd/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package cmd

import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"

"github.com/cespare/permute/v2"
"github.com/cosmos/relayer/v2/relayer"
"github.com/go-git/go-git/v5"
"github.com/google/go-github/v43/github"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -256,77 +255,90 @@ func pathsFetchCmd(a *appState) *cobra.Command {
$ %s paths fetch --home %s
$ %s pth fch`, 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 := os.MkdirTemp("", "")
if err != nil {
return err
overwrite, _ := cmd.Flags().GetBool(flagOverwriteConfig)

chains := []string{}
for chainName := range a.Config.Chains {
chains = append(chains, chainName)
}

if _, err = git.PlainClone(localRepo, false, &git.CloneOptions{
URL: REPOURL,
Progress: io.Discard,
ReferenceName: "refs/heads/main",
}); err != nil {
return err
// find all combinations of paths for configured chains
// note: path file names on the chain-registry are in alphabetical order ex: achain-zchain.json
p := permute.Slice(chains)
chainCombinations := make(map[string]bool)
for p.Permute() {
a, b := chains[0], chains[1]
pair := a + "-" + b
if b < a {
pair = b + "-" + a
}
chainCombinations[pair] = true
}

// Try to fetch path info for each configured chain that has canonical chain/path info in the GH repo
for _, srcChain := range a.Config.Chains {
for _, dstChain := range a.Config.Chains {
client := github.NewClient(nil)

// Add paths to rly config from {localRepo}/interchain/chaind-id/
localPathsDir := path.Join(localRepo, "interchain", srcChain.ChainID())
for pthName := range chainCombinations {
_, exist := a.Config.Paths[pthName]
if exist && !overwrite {
fmt.Fprintf(cmd.ErrOrStderr(), "skipping: %s already exists in config, use -o to overwrite (clears filters)\n", pthName)
continue
}

dir := path.Clean(localPathsDir)
files, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "path info does not exist for chain: %s. Consider adding its info to %s. Error: %v\n", srcChain.ChainID(), path.Join(PATHSURL, "interchain"), err)
break
// TODO: Don't use github api. Potentially use: https://github.com/eco-stake/cosmos-directory once they integrate IBC data into restAPI. This will avoid rate limits.
fileName := pthName + ".json"
regPath := path.Join("_IBC", fileName)
client, _, err := client.Repositories.DownloadContents(cmd.Context(), "cosmos", "chain-registry", regPath, nil)
if err != nil {
if errors.As(err, new(*github.RateLimitError)) {
return fmt.Errorf("error message: %w", err)
}
fmt.Fprintf(cmd.ErrOrStderr(), "failure retrieving: %s: consider adding to cosmos/chain-registry: ERR: %v\n", pthName, err)
continue
}
defer client.Close()

// For each path file, check that the dst is also a configured chain in the relayers config
for _, f := range files {
pth := filepath.Join(dir, f.Name())

if f.IsDir() {
fmt.Fprintf(cmd.ErrOrStderr(), "directory at %s, skipping...\n", pth)
continue
}

byt, err := os.ReadFile(pth)
if err != nil {
cleanupDir(localRepo)
return fmt.Errorf("failed to read file %s: %w", pth, err)
}

p := &relayer.Path{}
if err = json.Unmarshal(byt, p); err != nil {
cleanupDir(localRepo)
return fmt.Errorf("failed to unmarshal file %s: %w", pth, err)
}

if p.Dst.ChainID == dstChain.ChainID() {
pthName := strings.Split(f.Name(), ".")[0]
if err = a.Config.AddPath(pthName, p); err != nil {
return fmt.Errorf("failed to add path %s: %w", pth, err)
}

fmt.Fprintf(cmd.ErrOrStderr(), "added path %s...\n", pthName)
}
}
b, err := io.ReadAll(client)
if err != nil {
return fmt.Errorf("error reading response body: %w", err)
}

if err := a.OverwriteConfig(a.Config); err != nil {
return err
}
ibc := &relayer.IBCdata{}
if err = json.Unmarshal(b, &ibc); err != nil {
return fmt.Errorf("failed to unmarshal: %w ", err)
}

srcChainName := ibc.Chain1.ChainName
dstChainName := ibc.Chain2.ChainName

srcPathEnd := &relayer.PathEnd{
ChainID: a.Config.Chains[srcChainName].ChainID(),
ClientID: ibc.Chain1.ClientID,
ConnectionID: ibc.Chain1.ConnectionID,
}
dstPathEnd := &relayer.PathEnd{
ChainID: a.Config.Chains[dstChainName].ChainID(),
ClientID: ibc.Chain2.ClientID,
ConnectionID: ibc.Chain2.ConnectionID,
}
newPath := &relayer.Path{
Src: srcPathEnd,
Dst: dstPathEnd,
}
client.Close()

if err = a.Config.AddPath(pthName, newPath); err != nil {
return fmt.Errorf("failed to add path %s: %w", pthName, err)
}
fmt.Fprintf(cmd.ErrOrStderr(), "added: %s\n", pthName)

}

if err := a.OverwriteConfig(a.Config); err != nil {
return err
}
cleanupDir(localRepo)
return nil

},
}
return cmd
}

func cleanupDir(dir string) {
_ = os.RemoveAll(dir)
return OverwriteConfigFlag(a.Viper, cmd)
}
15 changes: 2 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.18

require (
github.com/cosmos/cosmos-sdk v0.45.5-0.20220523154235-2921a1c3c918
github.com/go-git/go-git/v5 v5.4.2
github.com/gogo/protobuf v1.3.3
github.com/gorilla/mux v1.8.0 // indirect
github.com/spf13/cobra v1.4.0
Expand All @@ -17,8 +16,10 @@ require (

require (
github.com/avast/retry-go/v4 v4.0.5
github.com/cespare/permute/v2 v2.0.0-beta2
github.com/cosmos/ibc-go/v3 v3.0.0
github.com/google/go-cmp v0.5.8
github.com/google/go-github/v43 v43.0.0
github.com/jsternberg/zap-logfmt v1.2.0
github.com/ory/dockertest/v3 v3.9.1
github.com/strangelove-ventures/lens v0.5.1
Expand All @@ -35,9 +36,7 @@ require (
github.com/DataDog/zstd v1.4.5 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/Workiva/go-datastructures v1.0.53 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/armon/go-metrics v0.4.0 // indirect
github.com/benbjohnson/clock v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down Expand Up @@ -69,12 +68,9 @@ require (
github.com/docker/go-units v0.4.0 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/dvsekhvalnov/jose2go v0.0.0-20200901110807-248326c1351b // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/ethereum/go-ethereum v1.10.16 // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
Expand All @@ -83,7 +79,6 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.0.0 // indirect
github.com/google/go-github/v43 v43.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/orderedcode v0.0.1 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
Expand All @@ -101,10 +96,7 @@ require (
github.com/imdario/mergo v0.3.12 // indirect
github.com/improbable-eng/grpc-web v0.15.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jhump/protoreflect v1.12.0 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/keybase/go-keychain v0.0.0-20190712205309-48d3d31d256d // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/lib/pq v1.10.6 // indirect
Expand Down Expand Up @@ -136,7 +128,6 @@ require (
github.com/rs/cors v1.8.2 // indirect
github.com/rs/zerolog v1.23.0 // indirect
github.com/sasha-s/go-deadlock v0.2.1-0.20190427202633-1595213edefa // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
Expand All @@ -150,7 +141,6 @@ require (
github.com/tendermint/tm-db v0.6.7 // indirect
github.com/tharsis/ethermint v0.16.0 // indirect
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
Expand All @@ -165,7 +155,6 @@ require (
google.golang.org/grpc v1.47.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
nhooyr.io/websocket v1.8.6 // indirect
)
Expand Down
Loading

0 comments on commit 29b3628

Please sign in to comment.