From 6e195daee7d55f6a8ece28ad8fec4edfb4f4b9f7 Mon Sep 17 00:00:00 2001 From: Sam Calder-Mason Date: Tue, 11 Jun 2024 14:27:28 +1000 Subject: [PATCH] feat: Add Eth-Consensus-Version to state response (#168) * feat: Add Eth-Consensus-Version to state response * Add to headers * build: Update module versions and dependencies * refactor: Remove unnecessary linters from configuration * refactor: Improve error handling and log message in downloadServingCheckpoint --- .golangci.yml | 3 --- pkg/api/handler.go | 29 +++++++++++++++++++++++------ pkg/api/response.go | 4 ++++ pkg/beacon/download.go | 23 +++++++++++++++++------ 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 3aee4fde..559d0f51 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -21,7 +21,6 @@ linters: disable-all: true enable: - bodyclose - - deadcode - dogsled - dupl - errcheck @@ -39,13 +38,11 @@ linters: - nakedret - prealloc - staticcheck - - structcheck - stylecheck - thelper - tparallel - typecheck - unconvert - - varcheck - whitespace - wsl diff --git a/pkg/api/handler.go b/pkg/api/handler.go index e51b5a7e..117be27d 100644 --- a/pkg/api/handler.go +++ b/pkg/api/handler.go @@ -224,12 +224,27 @@ func (h *Handler) handleEthV2DebugBeaconStates(ctx context.Context, r *http.Requ return NewUnsupportedMediaTypeResponse(nil), err } - id, err := eth.NewStateIdentifier(p.ByName("state_id")) + blockID, err := eth.NewBlockIdentifier(p.ByName("state_id")) if err != nil { return NewBadRequestResponse(nil), err } - state, err := h.eth.BeaconState(ctx, id) + block, err := h.eth.BeaconBlock(ctx, blockID) + if err != nil { + return NewInternalServerErrorResponse(nil), err + } + + slot, err := block.Slot() + if err != nil { + return NewInternalServerErrorResponse(nil), err + } + + stateID, err := eth.NewStateIdentifier(fmt.Sprintf("%d", slot)) + if err != nil { + return NewInternalServerErrorResponse(nil), err + } + + state, err := h.eth.BeaconState(ctx, stateID) if err != nil { return NewInternalServerErrorResponse(nil), err } @@ -244,17 +259,19 @@ func (h *Handler) handleEthV2DebugBeaconStates(ctx context.Context, r *http.Requ }, }) - switch id.Type() { - case eth.StateIDRoot, eth.StateIDGenesis, eth.StateIDSlot: + switch blockID.Type() { + case eth.BlockIDRoot, eth.BlockIDGenesis, eth.BlockIDSlot: // TODO(sam.calder-mason): This should be calculated using the Weak-Subjectivity period. rsp.SetCacheControl("public, s-max-age=6000") - case eth.StateIDFinalized: + case eth.BlockIDFinalized: // TODO(sam.calder-mason): This should be calculated using the Weak-Subjectivity period. rsp.SetCacheControl("public, s-max-age=180") - case eth.StateIDHead: + case eth.BlockIDHead: rsp.SetCacheControl("public, s-max-age=30") } + rsp.SetEthConsensusVersion(block.Version.String()) + return rsp, nil } diff --git a/pkg/api/response.go b/pkg/api/response.go index e155fd1a..e7a71104 100644 --- a/pkg/api/response.go +++ b/pkg/api/response.go @@ -42,6 +42,10 @@ func (r HTTPResponse) SetCacheControl(v string) { r.Headers["Cache-Control"] = v } +func (r HTTPResponse) SetEthConsensusVersion(version string) { + r.Headers["Eth-Consensus-Version"] = version +} + func NewSuccessResponse(resolvers ContentTypeResolvers) *HTTPResponse { return &HTTPResponse{ resolvers: resolvers, diff --git a/pkg/beacon/download.go b/pkg/beacon/download.go index 9af35deb..ca122dcd 100644 --- a/pkg/beacon/download.go +++ b/pkg/beacon/download.go @@ -23,7 +23,23 @@ func (d *Default) downloadServingCheckpoint(ctx context.Context, checkpoint *v1. return errors.New("finalized checkpoint is nil") } - d.log.WithField("epoch", checkpoint.Finalized.Epoch).Info("Downloading serving checkpoint") + sp, err := d.Spec() + if err != nil { + return fmt.Errorf("failed to fetch spec: %w", err) + } + + fork, err := sp.ForkEpochs.CurrentFork( + phase0.Slot(uint64(checkpoint.Finalized.Epoch)*uint64(sp.SlotsPerEpoch)), + sp.SlotsPerEpoch, + ) + if err != nil { + return fmt.Errorf("failed to get current fork: %w", err) + } + + d.log. + WithField("epoch", checkpoint.Finalized.Epoch). + WithField("fork_name", fork.Name). + Info("Downloading serving checkpoint") upstream, err := d.nodes. Ready(ctx). @@ -46,11 +62,6 @@ func (d *Default) downloadServingCheckpoint(ctx context.Context, checkpoint *v1. return fmt.Errorf("failed to get slot from block: %w", err) } - sp, err := d.Spec() - if err != nil { - return fmt.Errorf("failed to fetch spec: %w", err) - } - if blockSlot%sp.SlotsPerEpoch != 0 { return fmt.Errorf("block slot is not aligned from an epoch boundary: %d", blockSlot) }