Skip to content

Commit 09102f3

Browse files
committed
appease gh linter
1 parent 9134a24 commit 09102f3

4 files changed

Lines changed: 53 additions & 28 deletions

File tree

node-api/backend/backend.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type Backend struct {
6060
// Genesis related data
6161
sp GenesisStateProcessor // only needed to recreate genesis state upon API loading
6262
db dbm.DB // in-memory DB to store genesis state
63-
muCms sync.RWMutex // mutex to allow copying genesisState in a thread safe way
63+
muSt sync.RWMutex // mutex to allow initializing and copying genesisState in a safe way
6464
cms storetypes.CommitMultiStore
6565
genesisState *state.StateDB // caches genesis data to serve API requests
6666
}
@@ -106,33 +106,23 @@ func (b *Backend) Close() error {
106106
return b.db.Close()
107107
}
108108

109-
// If checkChainIsReady returns nil, we assume genesis state is created
110-
func (b *Backend) checkChainIsReady() error {
111-
switch err := b.node.IsAppReady(); {
112-
case err == nil:
113-
// chain finally ready, time to loading genesis
114-
return b.loadGenesisState()
115-
case errors.Is(err, cometbft.ErrAppNotReady):
116-
return cometbft.ErrAppNotReady
117-
default:
118-
return fmt.Errorf("unable to check whether app is ready: %w", err)
119-
}
120-
}
121-
122109
type backendKVStoreService struct {
123110
ctx sdk.Context
124111
}
125112

126113
func (kvs *backendKVStoreService) OpenKVStore(context.Context) corestore.KVStore {
127-
//nolint:contextcheck // fine with tests
114+
//nolint:contextcheck // fine with this node-api backend
128115
store := sdk.UnwrapSDKContext(kvs.ctx).KVStore(backendStoreKey)
129116
return kvstorage.NewKVStore(store)
130117
}
131118

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

135122
func (b *Backend) loadGenesisState() error {
123+
b.muSt.Lock()
124+
defer b.muSt.Unlock()
125+
136126
if b.genesisState != nil {
137127
// genesis state already initialized, we're fine
138128
return nil

node-api/backend/getters.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
package backend
2222

2323
import (
24+
"errors"
2425
"fmt"
2526
"runtime"
2627

2728
"cosmossdk.io/log"
29+
cometbft "github.com/berachain/beacon-kit/consensus/cometbft/service"
2830
datypes "github.com/berachain/beacon-kit/da/types"
2931
"github.com/berachain/beacon-kit/primitives/common"
3032
"github.com/berachain/beacon-kit/primitives/math"
@@ -43,13 +45,20 @@ func (b *Backend) StateAndSlotFromHeight(height int64) (ReadOnlyBeaconState, mat
4345
return nil, 0, fmt.Errorf("expected height, must be non-negative or -1 to request tip, got %d", height)
4446
}
4547
if height == 0 {
46-
// genesis requested. Serve it from the genesis state recreated locally by node-api
47-
if err := b.checkChainIsReady(); err != nil {
48-
return nil, 0, err
48+
switch err := b.node.IsAppReady(); {
49+
case err == nil:
50+
// chain finally ready, time to loading genesis
51+
if err = b.loadGenesisState(); err != nil {
52+
return nil, 0, fmt.Errorf("failed loading genesis state: %w", err)
53+
}
54+
case errors.Is(err, cometbft.ErrAppNotReady):
55+
return nil, 0, cometbft.ErrAppNotReady
56+
default:
57+
return nil, 0, fmt.Errorf("unable to check whether app is ready: %w", err)
4958
}
5059

51-
b.muCms.Lock()
52-
defer b.muCms.Unlock()
60+
b.muSt.Lock()
61+
defer b.muSt.Unlock()
5362

5463
// Copy the state to ensure clients potential changes won't pollute the state
5564
// Also we make sure to create the copy in a thread-safe way via the muCms mutex.

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)