Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions consensus/cometbft/service/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
errorsmod "cosmossdk.io/errors"
cmtabci "github.com/cometbft/cometbft/abci/types"
abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
"github.com/cometbft/cometbft/node"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
sdkversion "github.com/cosmos/cosmos-sdk/version"
)
Expand Down Expand Up @@ -234,3 +235,8 @@ func (*Service) CheckTx(
) (*abci.CheckTxResponse, error) {
return &abci.CheckTxResponse{}, nil
}

// GetCometNode returns the concrete CometBFT node.
func (s *Service) GetCometNode() *node.Node {
return s.node
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should we rename this consensusInstance or something like that?

}
7 changes: 7 additions & 0 deletions node-api/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/berachain/beacon-kit/primitives/common"
"github.com/berachain/beacon-kit/primitives/math"
cmtcfg "github.com/cometbft/cometbft/config"
"github.com/cometbft/cometbft/node"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
)

Expand Down Expand Up @@ -113,3 +114,9 @@ func (b *Backend) Spec() (chain.Spec, error) {
}
return b.cs, nil
}

// GetNode returns the comet node from the backend.
// This is part of the NodeAPIBackend interface.
func (b *Backend) GetNode() *node.Node {
return b.node.GetCometNode()
}
5 changes: 5 additions & 0 deletions node-api/backend/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/berachain/beacon-kit/node-core/components/metrics"
statedb "github.com/berachain/beacon-kit/state-transition/core/state"
"github.com/berachain/beacon-kit/storage/beacondb"
"github.com/cometbft/cometbft/node"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand Down Expand Up @@ -79,3 +80,7 @@ func (t *testConsensusService) Name() string {
func (t *testConsensusService) LastBlockHeight() int64 {
panic(errTestMemberNotImplemented)
}

func (t *testConsensusService) GetCometNode() *node.Node {
panic(errTestMemberNotImplemented)
}
28 changes: 28 additions & 0 deletions node-api/handlers/node/backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2025, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package node

import "github.com/cometbft/cometbft/node"

// Backend is the interface for backend of the node API.
type Backend interface {
GetNode() *node.Node
}
10 changes: 8 additions & 2 deletions node-api/handlers/node/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@

package node

import "github.com/berachain/beacon-kit/node-api/handlers"
import (
"github.com/berachain/beacon-kit/node-api/handlers"
)

// Handler is the handler for the node API.
type Handler struct {
*handlers.BaseHandler
backend Backend
}

func NewHandler() *Handler {
// NewHandler creates a new handler for the node API.
func NewHandler(backend Backend) *Handler {
h := &Handler{
BaseHandler: handlers.NewBaseHandler(
handlers.NewRouteSet(""),
),
backend: backend,
}
return h
}
66 changes: 66 additions & 0 deletions node-api/handlers/node/sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2025, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package node

import (
"errors"

"github.com/berachain/beacon-kit/node-api/handlers"
nodetypes "github.com/berachain/beacon-kit/node-api/handlers/node/types"
)

var (
errNilBlockStore = errors.New("block store is nil")
errNilNode = errors.New("node is nil")
)

// Syncing returns the syncing status of the node.
func (h *Handler) Syncing(_ handlers.Context) (any, error) {
node := h.backend.GetNode()
if node == nil {
return nil, errNilNode
}

// Get blockStore for heights
blockStore := node.BlockStore()
if blockStore == nil {
return nil, errNilBlockStore
}

latestHeight := blockStore.Height()
baseHeight := blockStore.Base()

response := nodetypes.SyncingData{
HeadSlot: latestHeight,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would comment here that this relays on the fact that there is a one to one matching among comet blocks and payloads. Should this ever change in the future, this would not be correct anymore

IsOptimistic: true,
ELOffline: false,
Comment on lines +53 to +54
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure how do we set these data by only looking at CometBFT.
In fact all of these data seems to me related to beaconKit state only.
Even the isSyncing quantity can be stored in `consensus/cometbft/service object by only looking at the SyncingToHeight attribute in FinalizeBlockRequest.

}

// Calculate sync distance using block heights
response.SyncDistance = latestHeight - baseHeight
// If SyncDistance is greater than 0,
// we consider the node to be syncing
if response.SyncDistance > 0 {
response.IsSyncing = true
}

return response, nil
}
29 changes: 29 additions & 0 deletions node-api/handlers/node/types/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2025, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package types

type SyncingData struct {
HeadSlot int64 `json:"head_slot"`
SyncDistance int64 `json:"sync_distance"`
IsSyncing bool `json:"is_syncing"`
IsOptimistic bool `json:"is_optimistic"`
ELOffline bool `json:"el_offline"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,7 @@ package node

import "github.com/berachain/beacon-kit/node-api/handlers"

// Syncing is a placeholder so that beacon API clients don't break.
//
// TODO: Implement with real data.
func (h *Handler) Syncing(handlers.Context) (any, error) {
type SyncingResponse struct {
Data struct {
HeadSlot string `json:"head_slot"`
SyncDistance string `json:"sync_distance"`
IsSyncing bool `json:"is_syncing"`
IsOptimistic bool `json:"is_optimistic"`
ELOffline bool `json:"el_offline"`
} `json:"data"`
}

response := SyncingResponse{}
response.Data.HeadSlot = "0"
response.Data.SyncDistance = "1"
response.Data.IsSyncing = false
response.Data.IsOptimistic = true
response.Data.ELOffline = false

return response, nil
}

// Version is a placeholder so that beacon API clients don't break.
//
// TODO: Implement with real data.
func (h *Handler) Version(handlers.Context) (any, error) {
type VersionResponse struct {
Expand Down
4 changes: 2 additions & 2 deletions node-core/components/api_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func ProvideNodeAPIEventsHandler() *eventsapi.Handler {
return eventsapi.NewHandler()
}

func ProvideNodeAPINodeHandler() *nodeapi.Handler {
return nodeapi.NewHandler()
func ProvideNodeAPINodeHandler(b NodeAPIBackend) *nodeapi.Handler {
return nodeapi.NewHandler(b)
}

func ProvideNodeAPIProofHandler(b NodeAPIBackend) *proofapi.Handler {
Expand Down
2 changes: 2 additions & 0 deletions node-core/components/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
statedb "github.com/berachain/beacon-kit/state-transition/core/state"
"github.com/berachain/beacon-kit/storage/block"
"github.com/berachain/beacon-kit/storage/deposit"
"github.com/cometbft/cometbft/node"
)

type (
Expand Down Expand Up @@ -499,6 +500,7 @@ type (
GetSlotByBlockRoot(root common.Root) (math.Slot, error)
GetSlotByStateRoot(root common.Root) (math.Slot, error)
GetParentSlotByTimestamp(timestamp math.U64) (math.Slot, error)
GetNode() *node.Node

NodeAPIBeaconBackend
NodeAPIProofBackend
Expand Down
2 changes: 2 additions & 0 deletions node-core/types/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"cosmossdk.io/store"
"github.com/berachain/beacon-kit/beacon/blockchain"
service "github.com/berachain/beacon-kit/node-core/services/registry"
"github.com/cometbft/cometbft/node"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -58,4 +59,5 @@ type ConsensusService interface {
prove bool,
) (sdk.Context, error)
LastBlockHeight() int64
GetCometNode() *node.Node
}
15 changes: 15 additions & 0 deletions testing/e2e/e2e_beacon_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,3 +934,18 @@ func (s *BeaconKitE2ESuite) TestConfigSpec() {
s.Require().Contains(specData, "INACTIVITY_PENALTY_QUOTIENT_ALTAIR")
s.Require().Zero(specData["INACTIVITY_PENALTY_QUOTIENT_ALTAIR"])
}

// TestSyncing tests querying the syncing status of the beacon node.
func (s *BeaconKitE2ESuite) TestNodeSyncing() {
client := s.initBeaconTest()

resp, err := client.NodeSyncing(s.Ctx())
s.Require().NoError(err)
s.Require().NotNil(resp)
s.Require().NotNil(resp.Data)

s.Require().Equal(resp.Data.HeadSlot, phase0.Slot(0))
s.Require().Equal(resp.Data.SyncDistance, phase0.Slot(0))
s.Require().Equal(resp.Data.IsSyncing, false)
s.Require().Equal(resp.Data.IsOptimistic, false)
}
2 changes: 1 addition & 1 deletion testing/e2e/e2e_staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (s *BeaconKitE2ESuite) TestDepositRobustness() {
s.Require().NoError(err)
nextEpoch := chainSpec.SlotToEpoch(math.Slot(blkNum)) + 1
nextEpochBlockNum := nextEpoch.Unwrap() * chainSpec.SlotsPerEpoch()
err = s.WaitForFinalizedBlockNumber(nextEpochBlockNum + 1)
err = s.WaitForFinalizedBlockNumber(nextEpochBlockNum + 10)
s.Require().NoError(err)

increaseAmt := new(big.Int).Mul(depositAmountGwei, big.NewInt(int64(NumDepositsLoad/config.NumValidators)))
Expand Down
8 changes: 8 additions & 0 deletions testing/e2e/suite/types/consensus_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ func (cc ConsensusClient) BlockProposerProof(
return cc.beaconClient.BlockProposerProof(ctx, timestampID)
}

// Syncing returns the syncing status of the beacon node.
func (cc ConsensusClient) NodeSyncing(ctx context.Context) (*beaconapi.Response[*apiv1.SyncState], error) {
if cc.beaconClient == nil {
return nil, errors.New("beacon client is not initialized")
}
return cc.beaconClient.NodeSyncing(ctx, &beaconapi.NodeSyncingOpts{})
}

// Commit returns the commit for a block.
func (cc ConsensusClient) Commit(
ctx context.Context,
Expand Down
5 changes: 5 additions & 0 deletions testing/simulated/simcomet.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/berachain/beacon-kit/node-core/builder"
"github.com/berachain/beacon-kit/node-core/components/metrics"
cmtcfg "github.com/cometbft/cometbft/config"
"github.com/cometbft/cometbft/node"
dbm "github.com/cosmos/cosmos-db"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand Down Expand Up @@ -87,3 +88,7 @@ func (s *SimComet) CreateQueryContext(height int64, prove bool) (sdk.Context, er
func (s *SimComet) LastBlockHeight() int64 {
panic("unimplemented")
}

func (s *SimComet) GetCometNode() *node.Node {
return s.Comet.GetCometNode()
}