Skip to content

Commit

Permalink
added path to chains.json
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnguyen22 authored and luyzdeleon committed Apr 14, 2019
1 parent 151b324 commit a316d9c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
34 changes: 25 additions & 9 deletions node/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package node

import (
"encoding/json"
"errors"
"fmt"
"github.com/pokt-network/pocket-core/util"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"strconv"
"sync"
"time"
)

// A structure that specifies a non-native blockchain.
Expand All @@ -22,6 +25,7 @@ type HostedChain struct {
Blockchain `json:"blockchain"`
Port string `json:"port"`
Host string `json:"host"`
Path string `json:"path"` // url path for token based authentication
Medium string `json:"medium"` // http, ws, tcp, etc.
}

Expand Down Expand Up @@ -81,11 +85,12 @@ func ChainToHosted(b Blockchain) HostedChain {
}

// "dialHC" attempts to connect to the specific host:port hosting the chain.
func dialHC(host string, port string) error {
if _, err := net.DialTimeout("tcp", host+":"+port, time.Duration(1*time.Second)); err != nil {
return err
func dialHC(u *url.URL) error {
resp, _ := http.Get(u.String())
if resp.StatusCode == 400 || resp.StatusCode == 200 {
return nil
}
return nil
return errors.New(strconv.Itoa(resp.StatusCode) + " : " + resp.Status)
}

// "TestChains" tests for hosted blockchain clients.
Expand All @@ -94,10 +99,21 @@ func TestChains() {
mux.Lock()
defer mux.Unlock()
for _, c := range hc {
if err := dialHC(c.Host, c.Port); err != nil {
fmt.Fprint(os.Stderr, c.Name+" client is not detected @ "+c.Host+":"+c.Port+"\n")
s, err := util.URLProto(c.Host + ":" + c.Port)
if err != nil {
ExitGracefully(err.Error())
}
u, err := url.ParseRequestURI(s)
if err != nil {
ExitGracefully(err.Error())
}
if c.Path != "" {
u.Path = c.Path
}
if err := dialHC(u); err != nil {
fmt.Fprint(os.Stderr, c.Name+" client is not detected @ "+u.String()+"\n")
ExitGracefully(c.Name + " client isn't detected" + "\n")
}
fmt.Println(c.Name + " NetID:" + c.NetID + " client is active and ready for service on port " + c.Port)
fmt.Println(c.Name + " NetID:" + c.NetID + " client is active and ready for service @ " + u.String())
}
}
7 changes: 4 additions & 3 deletions plugin/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (
"github.com/pokt-network/pocket-core/util"
"io/ioutil"
"net/http"
"net/url"
)

// "ExecuteRequest" takes in the raw json string and forwards it to the port
func ExecuteRequest(jsonStr []byte, host string, port string) (string, error) {
url, err := util.URLProto(host + ":" + port)
func ExecuteRequest(jsonStr []byte, u *url.URL) (string, error) {
ur, err := util.URLProto(u.String())
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req, err := http.NewRequest("POST", ur, bytes.NewBuffer(jsonStr))
req.Close = true
req.Header.Set("Content-Type", "application/json")
resp, err := (&http.Client{}).Do(req)
Expand Down
16 changes: 8 additions & 8 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package service

import (
"encoding/json"
"errors"
"net/url"
"os"

"github.com/pokt-network/pocket-core/config"
"github.com/pokt-network/pocket-core/const"
"github.com/pokt-network/pocket-core/logs"
"github.com/pokt-network/pocket-core/node"
"github.com/pokt-network/pocket-core/plugin/rpc"
)
Expand All @@ -25,13 +24,14 @@ type Relay struct {
func RouteRelay(relay Relay) (string, error) {
if node.EnsureDWL(node.DWL(), relay.DevID) {
hc := node.ChainToHosted(node.Blockchain{Name: relay.Blockchain, NetID: relay.NetworkID})
port := hc.Port
host := hc.Host
if port == "" || host == "" {
logs.NewLog("Not a supported blockchain", logs.ErrorLevel, logs.JSONLogFormat)
return "This blockchain is not supported by this node", errors.New("not a supported blockchain")
u, err := url.ParseRequestURI(hc.Host + ":" + hc.Port)
if err != nil {
return "", err
}
return rpc.ExecuteRequest([]byte(relay.Data), host, port)
if hc.Path != "" {
u.Path = hc.Path
}
return rpc.ExecuteRequest([]byte(relay.Data), u)
}
return "Invalid credentials", nil
}
Expand Down

0 comments on commit a316d9c

Please sign in to comment.