Skip to content

Commit

Permalink
Merge PR #8: Major Relayer Refactor v1.0 (the easy stuff)
Browse files Browse the repository at this point in the history
* Light removal

* pagination refactor

* dead code cleanup

Co-authored-by: jtieri <37750742+jtieri@users.noreply.github.com>
  • Loading branch information
jackzampolin and jtieri committed Oct 25, 2021
1 parent 481bc33 commit c12187b
Show file tree
Hide file tree
Showing 32 changed files with 241 additions and 2,945 deletions.
639 changes: 0 additions & 639 deletions cmd/api.go

This file was deleted.

226 changes: 3 additions & 223 deletions cmd/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import (
"path"
"strings"

"github.com/gorilla/mux"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"

"github.com/cosmos/relayer/helpers"
"github.com/cosmos/relayer/relayer"
)

Expand Down Expand Up @@ -214,7 +212,7 @@ $ %s ch l`, appName, appName)),
var (
light = xIcon
key = xIcon
path = xIcon
p = xIcon
bal = xIcon
)
_, err := c.GetAddress()
Expand All @@ -227,17 +225,12 @@ $ %s ch l`, appName, appName)),
bal = check
}

_, err = c.GetLatestLightHeader()
if err == nil {
light = check
}

for _, pth := range config.Paths {
if pth.Src.ChainID == c.ChainID || pth.Dst.ChainID == c.ChainID {
path = check
p = check
}
}
fmt.Printf("%2d: %-20s -> key(%s) bal(%s) light(%s) path(%s)\n", i, c.ChainID, key, bal, light, path)
fmt.Printf("%2d: %-20s -> key(%s) bal(%s) light(%s) path(%s)\n", i, c.ChainID, key, bal, light, p)
}
return nil
}
Expand Down Expand Up @@ -478,216 +471,3 @@ func urlInputAdd(rawurl string) (cfg *Config, err error) {
}
return config, err
}

// API Handlers

// GetChainsHandler returns the configured chains in json format
func GetChainsHandler(w http.ResponseWriter, r *http.Request) {
helpers.SuccessJSONResponse(http.StatusOK, config.Chains, w)
}

// GetChainHandler returns the configured chains in json format
func GetChainHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
chain, err := config.Chains.Get(vars["name"])
if err != nil {
helpers.WriteErrorResponse(http.StatusBadRequest, err, w)
return
}
helpers.SuccessJSONResponse(http.StatusOK, chain, w)
}

// GetChainStatusHandler returns the configured chains in json format
func GetChainStatusHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
chain, err := config.Chains.Get(vars["name"])
if err != nil {
helpers.WriteErrorResponse(http.StatusBadRequest, err, w)
return
}
helpers.SuccessJSONResponse(http.StatusOK, chainStatusResponse{}.Populate(chain), w)
}

type chainStatusResponse struct {
Light bool `json:"light"`
Path bool `json:"path"`
Key bool `json:"key"`
Balance bool `json:"balance"`
}

func (cs chainStatusResponse) Populate(c *relayer.Chain) chainStatusResponse {
_, err := c.GetAddress()
if err == nil {
cs.Key = true
}

coins, err := c.QueryBalance(c.Key)
if err == nil && !coins.Empty() {
cs.Balance = true
}

_, err = c.GetLatestLightHeader()
if err == nil {
cs.Light = true
}

for _, pth := range config.Paths {
if pth.Src.ChainID == c.ChainID || pth.Dst.ChainID == c.ChainID {
cs.Path = true
}
}
return cs
}

type addChainRequest struct {
Key string `json:"key"`
RPCAddr string `json:"rpc-addr"`
AccountPrefix string `json:"account-prefix"`
GasAdjustment string `json:"gas-adjustment"`
GasPrices string `json:"gas-prices"`
TrustingPeriod string `json:"trusting-period"`
// required: false
FilePath string `json:"file"`
// required: false
URL string `json:"url"`
}

// PostChainHandler handles the route
func PostChainHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
chainID := vars["name"]

var request addChainRequest
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
helpers.WriteErrorResponse(http.StatusBadRequest, err, w)
return
}

if request.FilePath != "" && request.URL != "" {
helpers.WriteErrorResponse(http.StatusBadRequest, errMultipleAddFlags, w)
return
}

var out *Config
switch {
case request.FilePath != "":
if out, err = fileInputAdd(request.FilePath); err != nil {
helpers.WriteErrorResponse(http.StatusBadRequest, err, w)
return
}
case request.URL != "":
if out, err = urlInputAdd(request.URL); err != nil {
helpers.WriteErrorResponse(http.StatusBadRequest, err, w)
return
}
default:
if out, err = addChainByRequest(request, chainID); err != nil {
helpers.WriteErrorResponse(http.StatusBadRequest, err, w)
return
}
}

if err = validateConfig(out); err != nil {
helpers.WriteErrorResponse(http.StatusInternalServerError, err, w)
return
}

if err = overWriteConfig(out); err != nil {
helpers.WriteErrorResponse(http.StatusInternalServerError, err, w)
return
}
helpers.SuccessJSONResponse(http.StatusCreated, fmt.Sprintf("chain %s added successfully", chainID), w)
}

func addChainByRequest(request addChainRequest, chainID string) (cfg *Config, err error) {
c := &relayer.Chain{}

if c, err = c.Update("chain-id", chainID); err != nil {
return nil, err
}

if c, err = c.Update("key", request.Key); err != nil {
return nil, err
}

if c, err = c.Update("rpc-addr", request.RPCAddr); err != nil {
return nil, err
}

if c, err = c.Update("account-prefix", request.AccountPrefix); err != nil {
return nil, err
}

if c, err = c.Update("gas-adjustment", request.GasAdjustment); err != nil {
return nil, err
}

if c, err = c.Update("gas-prices", request.GasPrices); err != nil {
return nil, err
}

if c, err = c.Update("trusting-period", request.TrustingPeriod); err != nil {
return nil, err
}

if err = config.AddChain(c); err != nil {
return nil, err
}

return config, nil
}

type editChainRequest struct {
Key string `json:"key"`
Value string `json:"value"`
}

// PutChainHandler handles the route
func PutChainHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
chain, err := config.Chains.Get(vars["name"])
if err != nil {
helpers.WriteErrorResponse(http.StatusBadRequest, err, w)
return
}

var request editChainRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
helpers.WriteErrorResponse(http.StatusBadRequest, err, w)
return
}

c, err := chain.Update(request.Key, request.Value)
if err != nil {
helpers.WriteErrorResponse(http.StatusBadRequest, err, w)
return
}

if err = config.DeleteChain(vars["name"]).AddChain(c); err != nil {
helpers.WriteErrorResponse(http.StatusBadRequest, err, w)
return
}

if err = overWriteConfig(config); err != nil {
helpers.WriteErrorResponse(http.StatusInternalServerError, err, w)
return
}
helpers.SuccessJSONResponse(http.StatusOK, fmt.Sprintf("chain %s updated", vars["name"]), w)

}

// DeleteChainHandler handles the route
func DeleteChainHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
_, err := config.Chains.Get(vars["name"])
if err != nil {
helpers.WriteErrorResponse(http.StatusBadRequest, err, w)
return
}
if err := overWriteConfig(config.DeleteChain(vars["name"])); err != nil {
helpers.WriteErrorResponse(http.StatusInternalServerError, err, w)
return
}
helpers.SuccessJSONResponse(http.StatusOK, fmt.Sprintf("chain %s deleted", vars["name"]), w)
}
8 changes: 0 additions & 8 deletions cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,6 @@ $ %s development relayer path-test`, appName, appName)),
dst, chains[dst].MustGetAddress())
}

// ensure light clients are initialized
if _, err = chains[src].GetLatestLightHeight(); err != nil {
return fmt.Errorf("no light client on %s, ensure it is initialized before continuing: %w", src, err)
}
if _, err = chains[dst].GetLatestLightHeight(); err != nil {
return fmt.Errorf("no light client on %s, ensure it is initialized before continuing: %w", dst, err)
}

fmt.Printf(`[Unit]
Description=%s
After=network.target
Expand Down
Loading

0 comments on commit c12187b

Please sign in to comment.