Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: local chain registry endpoint #1204

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ibc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
}
31 changes: 31 additions & 0 deletions local-interchain/interchain/handlers/chain_registry.go
Original file line number Diff line number Diff line change
@@ -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)
}
24 changes: 24 additions & 0 deletions local-interchain/interchain/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Comment on lines +47 to +62
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we call the same handler here so is it just handling the case where the file could be named either chain_registry.json or chain_registry_assets? is there any reason for this explicit handling of both?

Copy link
Member Author

@Reecepbcups Reecepbcups Aug 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it uses the same as it just returns the JSON object. Since its specific to chain registry and only do that 1 function, it makes sense to just re-use it vs duplicate code.

If we need to add extra logic in the future I would rather keep it scoped to chain registry then a generic JSON viewer

actionsH := handlers.NewActions(ctx, ic, cosmosChains, vals, relayer, eRep, authKey)
r.HandleFunc("/", actionsH.PostActions).Methods(http.MethodPost)

Expand Down
Loading