Skip to content

Commit

Permalink
fix: Remove unnecessary version assignment in LightClientUpdates func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
samcm committed Oct 24, 2024
1 parent bceb6c1 commit 60b943f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/attestantio/go-eth2-client v0.21.9
github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9
github.com/creasty/defaults v1.6.0
github.com/ethpandaops/beacon v0.42.1-0.20241023053323-d1834c10e236
github.com/ethpandaops/beacon v0.42.1-0.20241024060031-8e0b4cd9bc1f
github.com/ethpandaops/ethwallclock v0.2.0
github.com/go-co-op/gocron v1.18.0
github.com/julienschmidt/httprouter v1.3.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs=
github.com/ethereum/go-ethereum v1.14.10 h1:kC24WjYeRjDy86LVo6MfF5Xs7nnUu+XG4AjaYIaZYko=
github.com/ethereum/go-ethereum v1.14.10/go.mod h1:+l/fr42Mma+xBnhefL/+z11/hcmJ2egl+ScIVPjhc7E=
github.com/ethpandaops/beacon v0.42.1-0.20241023053323-d1834c10e236 h1:f9W5dVimoPqaf3tuOdc6CdBr9e+Ot1hTqBRfsfurAek=
github.com/ethpandaops/beacon v0.42.1-0.20241023053323-d1834c10e236/go.mod h1:hKfalJGsF4BuWPwcGCX/4fdQR31zDJVaTLWwrkfNTzw=
github.com/ethpandaops/beacon v0.42.1-0.20241024060031-8e0b4cd9bc1f h1:RWIj9+8C2AW6VUXNguXgDPgXZ0UKAi52rIuO/05mq+I=
github.com/ethpandaops/beacon v0.42.1-0.20241024060031-8e0b4cd9bc1f/go.mod h1:hKfalJGsF4BuWPwcGCX/4fdQR31zDJVaTLWwrkfNTzw=
github.com/ethpandaops/ethwallclock v0.2.0 h1:EeFKtZ7v6TAdn/oAh0xaPujD7N4amjBxrWIByraUfLM=
github.com/ethpandaops/ethwallclock v0.2.0/go.mod h1:y0Cu+mhGLlem19vnAV2x0hpFS5KZ7oOi2SWYayv9l24=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ func (h *Handler) handleEthV1BeaconLightClientUpdates(ctx context.Context, r *ht
return NewBadRequestResponse(nil), fmt.Errorf("invalid count: %w", err)
}

updates, version, err := h.eth.LightClientUpdates(ctx, startPeriodInt, countInt)
updates, _, err := h.eth.LightClientUpdates(ctx, startPeriodInt, countInt)
if err != nil {
return NewInternalServerErrorResponse(nil), err
}
Expand All @@ -755,7 +755,7 @@ func (h *Handler) handleEthV1BeaconLightClientUpdates(ctx context.Context, r *ht
},
})

rsp.ExtraData["version"] = version
rsp.IsMultipleResponses = true

return rsp, nil
}
25 changes: 21 additions & 4 deletions pkg/api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ type ContentTypeResolver func() ([]byte, error)
type ContentTypeResolvers map[ContentType]ContentTypeResolver

type HTTPResponse struct {
resolvers ContentTypeResolvers
StatusCode int `json:"status_code"`
Headers map[string]string `json:"headers"`
ExtraData map[string]interface{}
resolvers ContentTypeResolvers
StatusCode int `json:"status_code"`
Headers map[string]string `json:"headers"`
ExtraData map[string]interface{}
IsMultipleResponses bool
}
type jsonResponse struct {
Data json.RawMessage `json:"data"`
Expand All @@ -22,6 +23,9 @@ type jsonResponse struct {
Version string `json:"version,omitempty"`
}

// multipleJSONResponse is a wrapper for multiple json responses.
type multipleJSONResponse []jsonResponse

func (r HTTPResponse) MarshalAs(contentType ContentType) ([]byte, error) {
if _, exists := r.resolvers[contentType]; !exists {
return nil, fmt.Errorf("unsupported content-type: %s", contentType.String())
Expand All @@ -31,6 +35,10 @@ func (r HTTPResponse) MarshalAs(contentType ContentType) ([]byte, error) {
return r.resolvers[contentType]()
}

if r.IsMultipleResponses {
return r.buildWrappedJSONResponses()
}

return r.buildWrappedJSONResponse()
}

Expand Down Expand Up @@ -110,3 +118,12 @@ func (r *HTTPResponse) buildWrappedJSONResponse() ([]byte, error) {

return json.Marshal(rsp)
}

func (r *HTTPResponse) buildWrappedJSONResponses() ([]byte, error) {
data, err := r.resolvers[ContentTypeJSON]()
if err != nil {
return nil, err
}

return data, nil
}
12 changes: 1 addition & 11 deletions pkg/service/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,15 +690,5 @@ func (h *Handler) LightClientUpdates(ctx context.Context, startPeriod, count int
return nil, "", err
}

v, ok := rsp.Metadata["version"]
if !ok {
return nil, "", fmt.Errorf("version not found")
}

ver, ok := v.(string)
if !ok {
return nil, "", fmt.Errorf("version is not a string")
}

return rsp.Data, ver, nil
return rsp.Data, "", nil
}

0 comments on commit 60b943f

Please sign in to comment.