From fa5866c7de5f42e24def9b902f0dd19e63c0a9a6 Mon Sep 17 00:00:00 2001 From: Ian Shim <100327837+ian-shim@users.noreply.github.com> Date: Tue, 21 May 2024 20:51:58 -0700 Subject: [PATCH] [node] Use URL builder to get reachability URL (#575) --- node/node.go | 28 ++++++++++++++++++++++------ node/node_test.go | 9 +++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/node/node.go b/node/node.go index 76f2b60e4d..a39f6dcc8b 100644 --- a/node/node.go +++ b/node/node.go @@ -12,7 +12,6 @@ import ( "net/http" "net/url" "os" - "strings" "sync" "time" @@ -478,9 +477,9 @@ func (n *Node) checkNodeReachability() { return } - checkUrl, err := url.Parse(fmt.Sprintf("%s/api/v1/operators-info/port-check?operator_id=%s", strings.TrimSuffix(n.Config.DataApiUrl, "/"), n.Config.ID.Hex())) + checkURL, err := GetReachabilityURL(n.Config.DataApiUrl, n.Config.ID.Hex()) if err != nil { - n.Logger.Error("Reachability check failed - invalid check url", err, "checkUrl", checkUrl.String()) + n.Logger.Error("Failed to get reachability check URL", err) return } @@ -491,16 +490,16 @@ func (n *Node) checkNodeReachability() { for { <-ticker.C - n.Logger.Debug("Calling reachability check", "url", checkUrl.String()) + n.Logger.Debug("Calling reachability check", "url", checkURL) - resp, err := http.Get(checkUrl.String()) + resp, err := http.Get(checkURL) if err != nil { n.Logger.Error("Reachability check request failed", err) continue } else if resp.StatusCode == 404 { body, _ := io.ReadAll(resp.Body) if string(body) == "404 page not found" { - n.Logger.Error("Invalid reachability check url", "checkUrl", checkUrl.String()) + n.Logger.Error("Invalid reachability check url", "checkUrl", checkURL) } else { n.Logger.Warn("Reachability check operator id not found", "status", resp.StatusCode, "operator_id", n.Config.ID.Hex()) } @@ -539,3 +538,20 @@ func (n *Node) checkNodeReachability() { } } } + +func GetReachabilityURL(dataApiUrl, operatorID string) (string, error) { + checkURLString, err := url.JoinPath(dataApiUrl, "/api/v1/operators-info/port-check") + if err != nil { + return "", err + } + checkURL, err := url.Parse(checkURLString) + if err != nil { + return "", err + } + + q := checkURL.Query() + q.Set("operator_id", operatorID) + checkURL.RawQuery = q.Encode() + + return checkURL.String(), nil +} diff --git a/node/node_test.go b/node/node_test.go index ed732445c5..e5a55643db 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -139,3 +139,12 @@ func TestNodeStartOperatorIDDoesNotMatch(t *testing.T) { err := c.node.Start(context.Background()) assert.ErrorContains(t, err, "operator ID mismatch") } + +func TestGetReachabilityURL(t *testing.T) { + url, err := node.GetReachabilityURL("https://dataapi.eigenda.xyz/", "123123123") + assert.NoError(t, err) + assert.Equal(t, "https://dataapi.eigenda.xyz/api/v1/operators-info/port-check?operator_id=123123123", url) + url, err = node.GetReachabilityURL("https://dataapi.eigenda.xyz", "123123123") + assert.NoError(t, err) + assert.Equal(t, "https://dataapi.eigenda.xyz/api/v1/operators-info/port-check?operator_id=123123123", url) +}