Skip to content

Commit

Permalink
fix: PATH service selection
Browse files Browse the repository at this point in the history
  • Loading branch information
red-0ne committed Jan 2, 2025
1 parent 2d1da6d commit 11fc9cc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 62 deletions.
62 changes: 2 additions & 60 deletions e2e/tests/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package e2e
import (
"bytes"
"fmt"
"net"
"net/url"
"os"
"os/exec"
Expand All @@ -26,11 +25,6 @@ var (
defaultHome = os.Getenv("POKTROLLD_HOME")
// defaultPathURL used by curl commands to send relay requests
defaultPathURL = os.Getenv("PATH_URL")
// defaultPathHostOverride overrides the host in the URL used to send requests
// Since the current DevNet infrastructure does not support arbitrary subdomains,
// this is used to specify the host to connect to and the full host (with the service as a subdomain)
// will be sent in the "Host" request header.
defaultPathHostOverride = os.Getenv("PATH_HOST_OVERRIDE")
// defaultDebugOutput provides verbose output on manipulations with binaries (cli command, stdout, stderr)
defaultDebugOutput = os.Getenv("E2E_DEBUG_OUTPUT")
)
Expand Down Expand Up @@ -189,22 +183,6 @@ func (p *pocketdBin) runCurlCmd(rpcBaseURL, service, method, path, appAddr, data
return nil, err
}

// Get the virtual host that will be sent in the "Host" request header
virtualHost := getVirtualHostFromUrlForService(rpcUrl, service)

// TODO_HACK: As of PR #879, the DevNet infrastructure does not support routing
// requests to arbitrary subdomains due to TLS certificate-related complexities.
// In such environment, defaultPathHostOverride (which contains no subdomain)
// is used as:
// 1. The gateway's 'host:port' to connect to
// 2. A base to which the service is added as a subdomain then set as the "Host" request header.
// (i.e. Host: <service>.<defaultPathHostOverride>)
//
// Override the actual connection address if the environment requires it.
if defaultPathHostOverride != "" {
rpcUrl.Host = defaultPathHostOverride
}

// Ensure that if a path is provided, it starts with a "/".
// This is required for RESTful APIs that use a path to identify resources.
// For JSON-RPC APIs, the resource path should be empty, so empty paths are allowed.
Expand All @@ -225,8 +203,9 @@ func (p *pocketdBin) runCurlCmd(rpcBaseURL, service, method, path, appAddr, data
"-v", // verbose output
"-sS", // silent with error
"-H", `Content-Type: application/json`, // HTTP headers
"-H", fmt.Sprintf("Host: %s", virtualHost), // Add virtual host header
"-H", fmt.Sprintf("Host: %s", rpcUrl.Host), // Add virtual host header
"-H", fmt.Sprintf("X-App-Address: %s", appAddr),
"-H", fmt.Sprintf("target-service-id: %s", service),
rpcUrl.String(),
}

Expand Down Expand Up @@ -262,40 +241,3 @@ func (p *pocketdBin) runCurlCmd(rpcBaseURL, service, method, path, appAddr, data

return r, err
}

// formatURLString returns RESTful or JSON-RPC API endpoint URL depending
// on the parameters provided.
func formatURLString(serviceAlias, rpcUrl, path string) string {
// For JSON-RPC APIs, the path should be empty
if len(path) == 0 {
return fmt.Sprintf("http://%s.%s/v1", serviceAlias, rpcUrl)
}

// For RESTful APIs, the path should not be empty.
// We remove the leading / to make the format string below easier to read.
if path[0] == '/' {
path = path[1:]
}
return fmt.Sprintf("http://%s.%s/v1/%s", serviceAlias, rpcUrl, path)
}

// getVirtualHostFromUrlForService returns a virtual host taking into consideration
// the URL's host and the service if it's non-empty.
// Specifically, it:
// 1. Extract's the host from the rpcURL
// 2. Prefixes the service as a subdomain to (1) the given rpcUrl host stripped of the port
//
// TODO_HACK: This is needed as of PR #879 because the DevNet infrastructure does
// not support arbitrary subdomains due to TLS certificate-related complexities.
func getVirtualHostFromUrlForService(rpcUrl *url.URL, service string) string {
// Strip port if it exists and add service prefix
host, _, err := net.SplitHostPort(rpcUrl.Host)
if err != nil {
// err is non-nil if rpcUrl.Host does not have a port.
// Use the entire host as is
host = rpcUrl.Host
}
virtualHost := fmt.Sprintf("%s.%s", service, host)

return virtualHost
}
4 changes: 2 additions & 2 deletions makefiles/relay.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
send_relay_path_JSONRPC: test_e2e_env ## Send a JSONRPC relay through PATH to a local anvil (test ETH) node
curl -X POST -H "Content-Type: application/json" \
-H "X-App-Address: pokt1mrqt5f7qh8uxs27cjm9t7v9e74a9vvdnq5jva4" \
-H "target-service-id: anvil" \
--data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' \
http://anvil.localhost:3000/v1/
# $(subst http://,http://anvil.,$(PATH_URL))/v1
http://localhost:3000/v1/

# TODO_MAINNET(@red-0ne): Re-enable this once PATH Gateway supports REST.
# See https://github.com/buildwithgrove/path/issues/87
Expand Down

0 comments on commit 11fc9cc

Please sign in to comment.