From 2a355c8083b86096070e54ef04e125c41522798a Mon Sep 17 00:00:00 2001 From: Reece Williams <31943163+Reecepbcups@users.noreply.github.com> Date: Tue, 20 Aug 2024 11:18:59 -0700 Subject: [PATCH] feat: local chain registry endpoint (#1204) * feat: read chain registry file if found * chain_registry_assets optional endpoint --- ibc/types.go | 6 ++-- .../interchain/handlers/chain_registry.go | 31 +++++++++++++++++++ local-interchain/interchain/router/router.go | 24 ++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 local-interchain/interchain/handlers/chain_registry.go diff --git a/ibc/types.go b/ibc/types.go index d17415cbd..75bf91327 100644 --- a/ibc/types.go +++ b/ibc/types.go @@ -426,7 +426,7 @@ type PathUpdateOptions struct { } type ICSConfig struct { - ProviderVerOverride string `yaml:"provider,omitempty" json:"provider,omitempty"` - ConsumerVerOverride string `yaml:"consumer,omitempty" json:"consumer,omitempty"` - ConsumerCopyProviderKey func(int) bool + ProviderVerOverride string `yaml:"provider,omitempty" json:"provider,omitempty"` + ConsumerVerOverride string `yaml:"consumer,omitempty" json:"consumer,omitempty"` + ConsumerCopyProviderKey func(int) bool `yaml:"-" json:"-"` } diff --git a/local-interchain/interchain/handlers/chain_registry.go b/local-interchain/interchain/handlers/chain_registry.go new file mode 100644 index 000000000..9895f938e --- /dev/null +++ b/local-interchain/interchain/handlers/chain_registry.go @@ -0,0 +1,31 @@ +package handlers + +import ( + "net/http" + "os" +) + +// If the chain_registry.json file is found within the current running directory, show it as an enpoint. +// Used in: spawn + +type chainRegistry struct { + DataJSON []byte `json:"data_json"` +} + +// NewChainRegistry creates a new chainRegistry with the JSON from the file at location. +func NewChainRegistry(loc string) *chainRegistry { + dataJSON, err := os.ReadFile(loc) + if err != nil { + panic(err) + } + + return &chainRegistry{ + DataJSON: dataJSON, + } +} + +func (cr chainRegistry) GetChainRegistry(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + w.Write(cr.DataJSON) +} diff --git a/local-interchain/interchain/router/router.go b/local-interchain/interchain/router/router.go index 15d03b86c..c93bc3b87 100644 --- a/local-interchain/interchain/router/router.go +++ b/local-interchain/interchain/router/router.go @@ -3,7 +3,10 @@ package router import ( "context" "encoding/json" + "log" "net/http" + "os" + "path/filepath" "github.com/gorilla/mux" ictypes "github.com/strangelove-ventures/interchaintest/local-interchain/interchain/types" @@ -36,6 +39,27 @@ func NewRouter( infoH := handlers.NewInfo(config, installDir, ctx, ic, cosmosChains, vals, relayer, eRep) r.HandleFunc("/info", infoH.GetInfo).Methods(http.MethodGet) + wd, err := os.Getwd() + if err != nil { + panic(err) + } + + chainRegistryFile := filepath.Join(wd, "chain_registry.json") + if _, err := os.Stat(chainRegistryFile); err == nil { + crH := handlers.NewChainRegistry(chainRegistryFile) + r.HandleFunc("/chain_registry", crH.GetChainRegistry).Methods(http.MethodGet) + } else { + log.Printf("chain_registry.json not found in %s, not exposing endpoint.", wd) + } + + chainRegistryAssetsFile := filepath.Join(wd, "chain_registry_assets.json") + if _, err := os.Stat(chainRegistryAssetsFile); err == nil { + crH := handlers.NewChainRegistry(chainRegistryAssetsFile) + r.HandleFunc("/chain_registry_assets", crH.GetChainRegistry).Methods(http.MethodGet) + } else { + log.Printf("chain_registry_assets.json not found in %s, not exposing endpoint.", wd) + } + actionsH := handlers.NewActions(ctx, ic, cosmosChains, vals, relayer, eRep, authKey) r.HandleFunc("/", actionsH.PostActions).Methods(http.MethodPost)