Skip to content

Commit

Permalink
feat: Add Eth-Consensus-Version to state response (#168)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
samcm committed Jun 11, 2024
1 parent f4631b7 commit 6e195da
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ linters:
disable-all: true
enable:
- bodyclose
- deadcode
- dogsled
- dupl
- errcheck
Expand All @@ -39,13 +38,11 @@ linters:
- nakedret
- prealloc
- staticcheck
- structcheck
- stylecheck
- thelper
- tparallel
- typecheck
- unconvert
- varcheck
- whitespace
- wsl

Expand Down
29 changes: 23 additions & 6 deletions pkg/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 17 additions & 6 deletions pkg/beacon/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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)
}
Expand Down

0 comments on commit 6e195da

Please sign in to comment.