From 43640f12d8576cf5e27a99f34280b3e20fb900d3 Mon Sep 17 00:00:00 2001 From: Sina M <1591639+s1na@users.noreply.github.com> Date: Fri, 16 Aug 2024 14:39:57 +0200 Subject: [PATCH] beacon/light: handle endpoint URL more gracefully (#30306) blsync was failing if the light endpoint it was provided ended with a `/`. This change should handle the joining more gracefully. --- beacon/light/api/light_api.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/beacon/light/api/light_api.go b/beacon/light/api/light_api.go index 6f60fc0cc6..45b425164f 100755 --- a/beacon/light/api/light_api.go +++ b/beacon/light/api/light_api.go @@ -23,6 +23,7 @@ import ( "fmt" "io" "net/http" + "net/url" "sync" "time" @@ -121,7 +122,11 @@ func NewBeaconLightApi(url string, customHeaders map[string]string) *BeaconLight } func (api *BeaconLightApi) httpGet(path string) ([]byte, error) { - req, err := http.NewRequest("GET", api.url+path, nil) + uri, err := api.buildURL(path, nil) + if err != nil { + return nil, err + } + req, err := http.NewRequest("GET", uri, nil) if err != nil { return nil, err } @@ -545,9 +550,13 @@ func (api *BeaconLightApi) StartHeadListener(listener HeadEventListener) func() // established. It can only return nil when the context is canceled. func (api *BeaconLightApi) startEventStream(ctx context.Context, listener *HeadEventListener) *eventsource.Stream { for retry := true; retry; retry = ctxSleep(ctx, 5*time.Second) { - path := "/eth/v1/events?topics=head&topics=light_client_finality_update&topics=light_client_optimistic_update" log.Trace("Sending event subscription request") - req, err := http.NewRequestWithContext(ctx, "GET", api.url+path, nil) + uri, err := api.buildURL("/eth/v1/events", map[string][]string{"topics": {"head", "light_client_finality_update", "light_client_optimistic_update"}}) + if err != nil { + listener.OnError(fmt.Errorf("error creating event subscription URL: %v", err)) + continue + } + req, err := http.NewRequestWithContext(ctx, "GET", uri, nil) if err != nil { listener.OnError(fmt.Errorf("error creating event subscription request: %v", err)) continue @@ -576,3 +585,15 @@ func ctxSleep(ctx context.Context, timeout time.Duration) (ok bool) { return false } } + +func (api *BeaconLightApi) buildURL(path string, params url.Values) (string, error) { + uri, err := url.Parse(api.url) + if err != nil { + return "", err + } + uri = uri.JoinPath(path) + if params != nil { + uri.RawQuery = params.Encode() + } + return uri.String(), nil +}