Skip to content

Commit 88e1a88

Browse files
committed
appease gh linter
1 parent 9134a24 commit 88e1a88

3 files changed

Lines changed: 35 additions & 9 deletions

File tree

node-api/backend/backend.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ type backendKVStoreService struct {
124124
}
125125

126126
func (kvs *backendKVStoreService) OpenKVStore(context.Context) corestore.KVStore {
127-
//nolint:contextcheck // fine with tests
127+
//nolint:contextcheck // fine with this node-api backend
128128
store := sdk.UnwrapSDKContext(kvs.ctx).KVStore(backendStoreKey)
129129
return kvstorage.NewKVStore(store)
130130
}
131131

132-
//nolint:gochecknoglobals // todo: fix later
132+
//nolint:gochecknoglobals // fine with this node-api backend
133133
var backendStoreKey = storetypes.NewKVStoreKey("backend-genesis")
134134

135135
func (b *Backend) loadGenesisState() error {

node-api/handlers/beacon/header.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package beacon
2323
import (
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
}

node-api/handlers/utils/mappings.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@
2121
package utils
2222

2323
import (
24+
"fmt"
25+
stdmath "math"
2426
"strings"
2527

2628
"github.com/berachain/beacon-kit/errors"
2729
"github.com/berachain/beacon-kit/primitives/common"
2830
"github.com/berachain/beacon-kit/primitives/math"
2931
)
3032

31-
var ErrNoSlotForStateRoot = errors.New("slot not found at state root")
33+
var (
34+
ErrNoSlotForStateRoot = errors.New("slot not found at state root")
35+
ErrFailedMappingHeightTooHigh = errors.New("failed mapping height too high")
36+
)
3237

3338
// TODO: define unique types for each of the query-able IDs (state & block from
3439
// spec, execution unique to beacon-kit). For each type define validation
@@ -54,6 +59,9 @@ func StateIDToHeight[StorageBackendT interface {
5459
if err != nil {
5560
return 0, ErrNoSlotForStateRoot
5661
}
62+
if slot > stdmath.MaxInt64 { // appease linters
63+
return 0, fmt.Errorf("%w: slot %d", ErrFailedMappingHeightTooHigh, slot)
64+
}
5765
return int64(slot), nil //#nosec: G115 // practically safe
5866
}
5967

@@ -74,6 +82,9 @@ func BlockIDToHeight[StorageBackendT interface {
7482
return 0, err
7583
}
7684
slot, err := storage.GetSlotByBlockRoot(root)
85+
if slot > stdmath.MaxInt64 { // appease linters
86+
return 0, fmt.Errorf("%w: slot %d", ErrFailedMappingHeightTooHigh, slot)
87+
}
7788
return int64(slot), err //#nosec: G115 // practically safe
7889
}
7990

@@ -104,6 +115,9 @@ func TimestampIDToParentHeight[StorageBackendT interface {
104115
)
105116
}
106117
slot, err := storage.GetParentSlotByTimestamp(timestamp)
118+
if slot > stdmath.MaxInt64 { // appease linters
119+
return 0, fmt.Errorf("%w: slot %d", ErrFailedMappingHeightTooHigh, slot)
120+
}
107121
return int64(slot), err //#nosec: G115 // practically safe
108122
}
109123

@@ -128,6 +142,9 @@ func stateIDToHeight(id string) (int64, error) {
128142
if err != nil {
129143
return 0, errors.Wrapf(err, "failed mapping stateID %q to slot", id)
130144
}
145+
if slot > stdmath.MaxInt64 { // appease linters
146+
return 0, fmt.Errorf("%w: slot %d", ErrFailedMappingHeightTooHigh, slot)
147+
}
131148
return int64(slot), nil //#nosec: G115 // practically safe
132149
}
133150
}

0 commit comments

Comments
 (0)