@@ -23,6 +23,7 @@ package beacon
2323import (
2424 "errors"
2525 "fmt"
26+ stdmath "math"
2627
2728 "github.com/berachain/beacon-kit/node-api/handlers"
2829 beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types"
@@ -42,7 +43,7 @@ func (h *Handler) GetBlockHeaders(c handlers.Context) (any, error) {
4243 switch {
4344 case len (req .Slot ) == 0 && len (req .ParentRoot ) == 0 :
4445 // no parameter specified, pick chain HEAD
45- // by requesting special height 0 .
46+ // by requesting special height -1 .
4647 height := utils .Head
4748 return h .makeBlockHeaderResponse (height , true /*resultsInList*/ )
4849
@@ -52,15 +53,20 @@ func (h *Handler) GetBlockHeaders(c handlers.Context) (any, error) {
5253 if errSlot != nil {
5354 return nil , fmt .Errorf ("failed retrieving slot from input parameters: %w" , errSlot )
5455 }
55- return h .makeBlockHeaderResponse (int64 (slot ), true /*resultsInList*/ ) //#nosec: G115 // practically safe
56+ if slot > stdmath .MaxInt64 { // appease linters
57+ return 0 , fmt .Errorf ("%w: slot %d" , utils .ErrFailedMappingHeightTooHigh , slot )
58+ }
59+ return h .makeBlockHeaderResponse (int64 (slot ), true /*resultsInList*/ ) //#nosec: G115 // safe
5660
5761 case len (req .Slot ) == 0 && len (req .ParentRoot ) != 0 :
58- parentSlot , errParent := utils .BlockIDToHeight (req .ParentRoot , h .backend )
62+ parentHeight , errParent := utils .BlockIDToHeight (req .ParentRoot , h .backend )
5963 if errParent != nil {
6064 return nil , fmt .Errorf ("%w, failed retrieving parent root with error: %w" , handlertypes .ErrNotFound , errParent )
6165 }
62- // TODO ABENEGIA: what happens here if HEAD is requested. Should be already broken??
63- height := parentSlot + 1
66+ if parentHeight == utils .Head {
67+ return nil , fmt .Errorf ("%w, requested header of tip's child" , handlertypes .ErrNotFound )
68+ }
69+ height := parentHeight + 1
6470 return h .makeBlockHeaderResponse (height , true /*resultsInList*/ )
6571
6672 default :
@@ -72,7 +78,10 @@ func (h *Handler) GetBlockHeaders(c handlers.Context) (any, error) {
7278 return nil , err
7379 }
7480
75- height := int64 (slot ) //#nosec: G115 // practically safe
81+ if slot > stdmath .MaxInt64 { // appease linters
82+ return 0 , fmt .Errorf ("%w: slot %d" , utils .ErrFailedMappingHeightTooHigh , slot )
83+ }
84+ height := int64 (slot ) //#nosec: G115 // safe
7685 if height != parentSlot + 1 {
7786 return nil , fmt .Errorf ("%w: request slot %d, parent block slot %d" , ErrMismatchedSlotAndParentBlock , slot , parentSlot )
7887 }
0 commit comments