Skip to content

Commit 43640f1

Browse files
authored
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.
1 parent 6eb42a6 commit 43640f1

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

beacon/light/api/light_api.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"io"
2525
"net/http"
26+
"net/url"
2627
"sync"
2728
"time"
2829

@@ -121,7 +122,11 @@ func NewBeaconLightApi(url string, customHeaders map[string]string) *BeaconLight
121122
}
122123

123124
func (api *BeaconLightApi) httpGet(path string) ([]byte, error) {
124-
req, err := http.NewRequest("GET", api.url+path, nil)
125+
uri, err := api.buildURL(path, nil)
126+
if err != nil {
127+
return nil, err
128+
}
129+
req, err := http.NewRequest("GET", uri, nil)
125130
if err != nil {
126131
return nil, err
127132
}
@@ -545,9 +550,13 @@ func (api *BeaconLightApi) StartHeadListener(listener HeadEventListener) func()
545550
// established. It can only return nil when the context is canceled.
546551
func (api *BeaconLightApi) startEventStream(ctx context.Context, listener *HeadEventListener) *eventsource.Stream {
547552
for retry := true; retry; retry = ctxSleep(ctx, 5*time.Second) {
548-
path := "/eth/v1/events?topics=head&topics=light_client_finality_update&topics=light_client_optimistic_update"
549553
log.Trace("Sending event subscription request")
550-
req, err := http.NewRequestWithContext(ctx, "GET", api.url+path, nil)
554+
uri, err := api.buildURL("/eth/v1/events", map[string][]string{"topics": {"head", "light_client_finality_update", "light_client_optimistic_update"}})
555+
if err != nil {
556+
listener.OnError(fmt.Errorf("error creating event subscription URL: %v", err))
557+
continue
558+
}
559+
req, err := http.NewRequestWithContext(ctx, "GET", uri, nil)
551560
if err != nil {
552561
listener.OnError(fmt.Errorf("error creating event subscription request: %v", err))
553562
continue
@@ -576,3 +585,15 @@ func ctxSleep(ctx context.Context, timeout time.Duration) (ok bool) {
576585
return false
577586
}
578587
}
588+
589+
func (api *BeaconLightApi) buildURL(path string, params url.Values) (string, error) {
590+
uri, err := url.Parse(api.url)
591+
if err != nil {
592+
return "", err
593+
}
594+
uri = uri.JoinPath(path)
595+
if params != nil {
596+
uri.RawQuery = params.Encode()
597+
}
598+
return uri.String(), nil
599+
}

0 commit comments

Comments
 (0)