From a41ff936169d6de010f8ab6f1e03957afdd6e45e Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Fri, 25 Oct 2024 15:34:09 +0530 Subject: [PATCH 1/9] node syncing call cometBFT RPC Signed-off-by: nidhi-singh02 --- mod/node-api/handlers/node/node.go | 49 +++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/mod/node-api/handlers/node/node.go b/mod/node-api/handlers/node/node.go index 22e69ff67f..3a4a96f140 100644 --- a/mod/node-api/handlers/node/node.go +++ b/mod/node-api/handlers/node/node.go @@ -21,14 +21,35 @@ package node import ( + "context" + "fmt" + "strconv" + nodetypes "github.com/berachain/beacon-kit/mod/node-api/handlers/node/types" "github.com/berachain/beacon-kit/mod/node-api/handlers/types" + cmtclient "github.com/cometbft/cometbft/rpc/client/http" + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/grpc/cmtservice" ) -// Syncing is a placeholder so that beacon API clients don't break. -// -// TODO: Implement with real data. -func (h *Handler[ContextT]) Syncing(ContextT) (any, error) { +// Syncing returns the syncing status of the beacon node. +func (h *Handler[ContextT]) Syncing(_ ContextT) (any, error) { + // Create a new RPC client + rpcClient, err := cmtclient.New("tcp://localhost:26657") + if err != nil { + return nil, fmt.Errorf("failed to create RPC client: %w", err) + } + + // Create client context with the RPC client + clientCtx := client.Context{} + clientCtx = clientCtx.WithClient(rpcClient) + + // Query CometBFT status + status, err := cmtservice.GetNodeStatus(context.Background(), clientCtx) + if err != nil { + return nil, fmt.Errorf("err in getting node status %w", err) + } + type SyncingResponse struct { Data struct { HeadSlot string `json:"head_slot"` @@ -40,9 +61,23 @@ func (h *Handler[ContextT]) Syncing(ContextT) (any, error) { } response := SyncingResponse{} - response.Data.HeadSlot = "0" - response.Data.SyncDistance = "1" - response.Data.IsSyncing = false + response.Data.HeadSlot = strconv.FormatInt( + status.SyncInfo.LatestBlockHeight, + 10, + ) + + // Calculate sync distance + if status.SyncInfo.LatestBlockHeight < status.SyncInfo.EarliestBlockHeight { + syncDistance := status.SyncInfo.EarliestBlockHeight - + status.SyncInfo.LatestBlockHeight + response.Data.SyncDistance = strconv.FormatInt(syncDistance, 10) + response.Data.IsSyncing = status.SyncInfo.CatchingUp + } else { + response.Data.SyncDistance = "0" + response.Data.IsSyncing = false + } + + // Keep existing values for these fields response.Data.IsOptimistic = true response.Data.ELOffline = false From a8fbd4bef463410307ce128cbff5898bbcc02703 Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Mon, 28 Oct 2024 13:06:13 +0530 Subject: [PATCH 2/9] use marshalJSON for response Signed-off-by: nidhi-singh02 --- mod/node-api/handlers/node/node.go | 33 ++++++-------------- mod/node-api/handlers/node/types/response.go | 31 ++++++++++++++++++ 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/mod/node-api/handlers/node/node.go b/mod/node-api/handlers/node/node.go index 3a4a96f140..607fece3f5 100644 --- a/mod/node-api/handlers/node/node.go +++ b/mod/node-api/handlers/node/node.go @@ -23,7 +23,6 @@ package node import ( "context" "fmt" - "strconv" nodetypes "github.com/berachain/beacon-kit/mod/node-api/handlers/node/types" "github.com/berachain/beacon-kit/mod/node-api/handlers/types" @@ -49,39 +48,25 @@ func (h *Handler[ContextT]) Syncing(_ ContextT) (any, error) { if err != nil { return nil, fmt.Errorf("err in getting node status %w", err) } - - 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 = strconv.FormatInt( - status.SyncInfo.LatestBlockHeight, - 10, - ) + response := nodetypes.SyncingData{} + response.HeadSlot = status.SyncInfo.LatestBlockHeight // Calculate sync distance if status.SyncInfo.LatestBlockHeight < status.SyncInfo.EarliestBlockHeight { syncDistance := status.SyncInfo.EarliestBlockHeight - status.SyncInfo.LatestBlockHeight - response.Data.SyncDistance = strconv.FormatInt(syncDistance, 10) - response.Data.IsSyncing = status.SyncInfo.CatchingUp + response.SyncDistance = syncDistance + response.IsSyncing = status.SyncInfo.CatchingUp } else { - response.Data.SyncDistance = "0" - response.Data.IsSyncing = false + response.SyncDistance = 0 + response.IsSyncing = false } // Keep existing values for these fields - response.Data.IsOptimistic = true - response.Data.ELOffline = false + response.IsOptimistic = true + response.ELOffline = false - return response, nil + return types.Wrap(&response), nil } // Version returns the version of the beacon node. diff --git a/mod/node-api/handlers/node/types/response.go b/mod/node-api/handlers/node/types/response.go index be45ef558d..82ac614209 100644 --- a/mod/node-api/handlers/node/types/response.go +++ b/mod/node-api/handlers/node/types/response.go @@ -20,6 +20,37 @@ package types +import ( + "encoding/json" + "strconv" +) + type VersionData struct { Version string `json:"version"` } + +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"` +} + +type syncingJSON 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"` +} + +func (s *SyncingData) MarshalJSON() ([]byte, error) { + return json.Marshal(syncingJSON{ + HeadSlot: strconv.FormatInt(s.HeadSlot, 10), + SyncDistance: strconv.FormatInt(s.SyncDistance, 10), + IsSyncing: s.IsSyncing, + IsOptimistic: s.IsOptimistic, + ELOffline: s.ELOffline, + }) +} From 855699f468a3bb2c1df8764af062b86e2d4e298a Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Mon, 28 Oct 2024 13:35:52 +0530 Subject: [PATCH 3/9] add tests in e2e Signed-off-by: nidhi-singh02 --- mod/node-api/handlers/node/node.go | 9 +++++---- testing/e2e/e2e_node_api_test.go | 20 ++++++++++++++++++++ testing/e2e/suite/types/consensus_client.go | 10 ++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/mod/node-api/handlers/node/node.go b/mod/node-api/handlers/node/node.go index 607fece3f5..3af2f1f8d4 100644 --- a/mod/node-api/handlers/node/node.go +++ b/mod/node-api/handlers/node/node.go @@ -52,11 +52,12 @@ func (h *Handler[ContextT]) Syncing(_ ContextT) (any, error) { response.HeadSlot = status.SyncInfo.LatestBlockHeight // Calculate sync distance - if status.SyncInfo.LatestBlockHeight < status.SyncInfo.EarliestBlockHeight { - syncDistance := status.SyncInfo.EarliestBlockHeight - - status.SyncInfo.LatestBlockHeight + if status.SyncInfo.CatchingUp { + // If we're catching up, the sync distance is the difference between + // the latest block height and the earliest block height + syncDistance := status.SyncInfo.LatestBlockHeight - status.SyncInfo.EarliestBlockHeight response.SyncDistance = syncDistance - response.IsSyncing = status.SyncInfo.CatchingUp + response.IsSyncing = true } else { response.SyncDistance = 0 response.IsSyncing = false diff --git a/testing/e2e/e2e_node_api_test.go b/testing/e2e/e2e_node_api_test.go index 5ee547a704..1caec97426 100644 --- a/testing/e2e/e2e_node_api_test.go +++ b/testing/e2e/e2e_node_api_test.go @@ -22,6 +22,7 @@ package e2e_test import ( beaconapi "github.com/attestantio/go-eth2-client/api" + "github.com/attestantio/go-eth2-client/spec/phase0" "github.com/berachain/beacon-kit/testing/e2e/config" "github.com/berachain/beacon-kit/testing/e2e/suite/types" ) @@ -52,3 +53,22 @@ func (s *BeaconKitE2ESuite) TestNodeVersion() { versionStr := version.Data s.Require().NotEmpty(versionStr) } + +// TestNodeSyncing tests the node api for syncing status of the node. +func (s *BeaconKitE2ESuite) TestNodeSyncing() { + client := s.initNodeTest() + + syncing, err := client.NodeSyncing(s.Ctx(), + &beaconapi.NodeSyncingOpts{}) + s.Require().NoError(err) + s.Require().NotNil(syncing) + syncData := syncing.Data + s.Require().NotEmpty(syncData.HeadSlot) + s.Require().Greater(syncData.HeadSlot, phase0.Slot(0)) + s.Require().NotNil(syncData.SyncDistance) + + s.Require().NotNil(syncData.IsSyncing) + s.Require().True(syncData.IsOptimistic) + + // TODO: Add more assertions. +} diff --git a/testing/e2e/suite/types/consensus_client.go b/testing/e2e/suite/types/consensus_client.go index f94884e44d..5f2b5aa1ba 100644 --- a/testing/e2e/suite/types/consensus_client.go +++ b/testing/e2e/suite/types/consensus_client.go @@ -258,5 +258,15 @@ func (cc ConsensusClient) Spec( return cc.beaconClient.Spec(ctx, opts) } +func (cc ConsensusClient) NodeSyncing( + ctx context.Context, + opts *beaconapi.NodeSyncingOpts, +) (*beaconapi.Response[*apiv1.SyncState], error) { + if cc.beaconClient == nil { + return nil, errors.New("beacon client is not initialized") + } + return cc.beaconClient.NodeSyncing(ctx, opts) +} + // TODO: Add helpers for the beacon node-api client (converting from // go-eth2-client types to beacon-kit consensus types). From fa2ffa6829e2e418d698feb0d50d2232d8de0be5 Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Tue, 29 Oct 2024 11:49:52 +0530 Subject: [PATCH 4/9] create new comet RPC client only in handler - to optimize Signed-off-by: nidhi-singh02 --- mod/node-api/handlers/node/handler.go | 20 +++++++++++++-- mod/node-api/handlers/node/node.go | 35 ++++++++------------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/mod/node-api/handlers/node/handler.go b/mod/node-api/handlers/node/handler.go index f99ea1141d..c7172eccf8 100644 --- a/mod/node-api/handlers/node/handler.go +++ b/mod/node-api/handlers/node/handler.go @@ -23,19 +23,35 @@ package node import ( "github.com/berachain/beacon-kit/mod/node-api/handlers" "github.com/berachain/beacon-kit/mod/node-api/server/context" + cmtclient "github.com/cometbft/cometbft/rpc/client/http" + "github.com/cosmos/cosmos-sdk/client" ) +const RpcEndpoint = "tcp://localhost:26657" + +// Handler is the handler for the node API. type Handler[ContextT context.Context] struct { *handlers.BaseHandler[ContextT] - backend Backend + backend Backend + rpcClient *cmtclient.HTTP + clientCtx client.Context } func NewHandler[ContextT context.Context](backend Backend) *Handler[ContextT] { + rpcClient, err := cmtclient.New(RpcEndpoint) + if err != nil { + // Not returning error to keep the pattern same across all handlers. + rpcClient = nil + } + clientCtx := client.Context{}.WithClient(rpcClient) + h := &Handler[ContextT]{ BaseHandler: handlers.NewBaseHandler( handlers.NewRouteSet[ContextT](""), ), - backend: backend, + backend: backend, + rpcClient: rpcClient, + clientCtx: clientCtx, } return h } diff --git a/mod/node-api/handlers/node/node.go b/mod/node-api/handlers/node/node.go index 3af2f1f8d4..aeebd7cfee 100644 --- a/mod/node-api/handlers/node/node.go +++ b/mod/node-api/handlers/node/node.go @@ -26,47 +26,32 @@ import ( nodetypes "github.com/berachain/beacon-kit/mod/node-api/handlers/node/types" "github.com/berachain/beacon-kit/mod/node-api/handlers/types" - cmtclient "github.com/cometbft/cometbft/rpc/client/http" - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/grpc/cmtservice" ) // Syncing returns the syncing status of the beacon node. func (h *Handler[ContextT]) Syncing(_ ContextT) (any, error) { - // Create a new RPC client - rpcClient, err := cmtclient.New("tcp://localhost:26657") - if err != nil { - return nil, fmt.Errorf("failed to create RPC client: %w", err) + if h.clientCtx.Client == nil { + return nil, fmt.Errorf("RPC client not initialized") } - - // Create client context with the RPC client - clientCtx := client.Context{} - clientCtx = clientCtx.WithClient(rpcClient) - - // Query CometBFT status - status, err := cmtservice.GetNodeStatus(context.Background(), clientCtx) + // Query node status from the cometBFT node + status, err := cmtservice.GetNodeStatus(context.Background(), h.clientCtx) if err != nil { return nil, fmt.Errorf("err in getting node status %w", err) } - response := nodetypes.SyncingData{} - response.HeadSlot = status.SyncInfo.LatestBlockHeight + response := nodetypes.SyncingData{ + HeadSlot: status.SyncInfo.LatestBlockHeight, + IsOptimistic: true, + ELOffline: false, + } // Calculate sync distance if status.SyncInfo.CatchingUp { // If we're catching up, the sync distance is the difference between // the latest block height and the earliest block height - syncDistance := status.SyncInfo.LatestBlockHeight - status.SyncInfo.EarliestBlockHeight - response.SyncDistance = syncDistance + response.SyncDistance = status.SyncInfo.LatestBlockHeight - status.SyncInfo.EarliestBlockHeight response.IsSyncing = true - } else { - response.SyncDistance = 0 - response.IsSyncing = false } - - // Keep existing values for these fields - response.IsOptimistic = true - response.ELOffline = false - return types.Wrap(&response), nil } From fc2a15c84fad9f958facad0b0cb3db2078f751d1 Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Tue, 29 Oct 2024 12:01:45 +0530 Subject: [PATCH 5/9] linter Signed-off-by: nidhi-singh02 --- mod/node-api/handlers/node/handler.go | 4 ++-- mod/node-api/handlers/node/node.go | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/mod/node-api/handlers/node/handler.go b/mod/node-api/handlers/node/handler.go index c7172eccf8..ba4c6c3aa1 100644 --- a/mod/node-api/handlers/node/handler.go +++ b/mod/node-api/handlers/node/handler.go @@ -27,7 +27,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" ) -const RpcEndpoint = "tcp://localhost:26657" +const RPCEndpoint = "tcp://localhost:26657" // Handler is the handler for the node API. type Handler[ContextT context.Context] struct { @@ -38,7 +38,7 @@ type Handler[ContextT context.Context] struct { } func NewHandler[ContextT context.Context](backend Backend) *Handler[ContextT] { - rpcClient, err := cmtclient.New(RpcEndpoint) + rpcClient, err := cmtclient.New(RPCEndpoint) if err != nil { // Not returning error to keep the pattern same across all handlers. rpcClient = nil diff --git a/mod/node-api/handlers/node/node.go b/mod/node-api/handlers/node/node.go index aeebd7cfee..1eaa01467e 100644 --- a/mod/node-api/handlers/node/node.go +++ b/mod/node-api/handlers/node/node.go @@ -22,8 +22,8 @@ package node import ( "context" - "fmt" + "github.com/berachain/beacon-kit/mod/errors" nodetypes "github.com/berachain/beacon-kit/mod/node-api/handlers/node/types" "github.com/berachain/beacon-kit/mod/node-api/handlers/types" "github.com/cosmos/cosmos-sdk/client/grpc/cmtservice" @@ -32,12 +32,12 @@ import ( // Syncing returns the syncing status of the beacon node. func (h *Handler[ContextT]) Syncing(_ ContextT) (any, error) { if h.clientCtx.Client == nil { - return nil, fmt.Errorf("RPC client not initialized") + return nil, errors.New("RPC client not initialized") } // Query node status from the cometBFT node status, err := cmtservice.GetNodeStatus(context.Background(), h.clientCtx) if err != nil { - return nil, fmt.Errorf("err in getting node status %w", err) + return nil, errors.Wrapf(err, "err in getting node status") } response := nodetypes.SyncingData{ HeadSlot: status.SyncInfo.LatestBlockHeight, @@ -49,7 +49,8 @@ func (h *Handler[ContextT]) Syncing(_ ContextT) (any, error) { if status.SyncInfo.CatchingUp { // If we're catching up, the sync distance is the difference between // the latest block height and the earliest block height - response.SyncDistance = status.SyncInfo.LatestBlockHeight - status.SyncInfo.EarliestBlockHeight + response.SyncDistance = status.SyncInfo.LatestBlockHeight - + status.SyncInfo.EarliestBlockHeight response.IsSyncing = true } return types.Wrap(&response), nil From e6141a3f91632a5667b71529cd41964ac25ff8a9 Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Wed, 6 Nov 2024 12:13:42 +0530 Subject: [PATCH 6/9] use node object - remove HTTP call Signed-off-by: nidhi-singh02 --- mod/consensus/pkg/cometbft/service/abci.go | 6 ++++ mod/node-api/backend/backend.go | 8 +++++ mod/node-api/backend/types.go | 3 ++ mod/node-api/handlers/node/backend.go | 3 ++ mod/node-api/handlers/node/handler.go | 22 +++--------- mod/node-api/handlers/node/node.go | 36 ++++++++++---------- mod/node-core/pkg/components/api.go | 2 ++ mod/node-core/pkg/components/api_handlers.go | 2 ++ 8 files changed, 46 insertions(+), 36 deletions(-) diff --git a/mod/consensus/pkg/cometbft/service/abci.go b/mod/consensus/pkg/cometbft/service/abci.go index 91c600db2d..3bb8474320 100644 --- a/mod/consensus/pkg/cometbft/service/abci.go +++ b/mod/consensus/pkg/cometbft/service/abci.go @@ -36,6 +36,7 @@ import ( "github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/json" math "github.com/berachain/beacon-kit/mod/primitives/pkg/math" cmtabci "github.com/cometbft/cometbft/abci/types" + "github.com/cometbft/cometbft/node" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" sdkversion "github.com/cosmos/cosmos-sdk/version" @@ -615,3 +616,8 @@ func (s *Service[_]) GetBlockRetentionHeight(commitHeight int64) int64 { func (s *Service[_]) GetBeaconVersion() (string, error) { return sdkversion.Version, nil } + +// GetCometNode returns the concrete CometBFT node. +func (s *Service[_]) GetCometNode() *node.Node { + return s.node +} diff --git a/mod/node-api/backend/backend.go b/mod/node-api/backend/backend.go index 4fe05342ae..47e5958b3d 100644 --- a/mod/node-api/backend/backend.go +++ b/mod/node-api/backend/backend.go @@ -25,6 +25,7 @@ import ( "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" + "github.com/cometbft/cometbft/node" ) // Backend is the db access layer for the beacon node-api. @@ -237,3 +238,10 @@ func (b *Backend[ } return appVersion, nil } + +// GetNode returns the comet node from the backend. +func (b *Backend[ + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, +]) GetNode() *node.Node { + return b.node.GetCometNode() +} diff --git a/mod/node-api/backend/types.go b/mod/node-api/backend/types.go index ecf8de75a5..c2553da320 100644 --- a/mod/node-api/backend/types.go +++ b/mod/node-api/backend/types.go @@ -30,6 +30,7 @@ import ( "github.com/berachain/beacon-kit/mod/primitives/pkg/math" "github.com/berachain/beacon-kit/mod/primitives/pkg/transition" "github.com/berachain/beacon-kit/mod/state-transition/pkg/core" + "github.com/cometbft/cometbft/node" ) // The AvailabilityStore interface is responsible for validating and storing @@ -112,6 +113,8 @@ type Node[ContextT any] interface { CreateQueryContext(height int64, prove bool) (ContextT, error) // GetBeaconVersion returns the version of the beacon node. GetBeaconVersion() (string, error) + // GetCometNode returns the comet node. + GetCometNode() *node.Node } type StateProcessor[BeaconStateT any] interface { diff --git a/mod/node-api/handlers/node/backend.go b/mod/node-api/handlers/node/backend.go index 032d58c8a6..3888f6d766 100644 --- a/mod/node-api/handlers/node/backend.go +++ b/mod/node-api/handlers/node/backend.go @@ -20,6 +20,9 @@ package node +import "github.com/cometbft/cometbft/node" + type Backend interface { GetNodeVersion() (string, error) + GetNode() *node.Node } diff --git a/mod/node-api/handlers/node/handler.go b/mod/node-api/handlers/node/handler.go index ba4c6c3aa1..19515ce1b4 100644 --- a/mod/node-api/handlers/node/handler.go +++ b/mod/node-api/handlers/node/handler.go @@ -23,35 +23,21 @@ package node import ( "github.com/berachain/beacon-kit/mod/node-api/handlers" "github.com/berachain/beacon-kit/mod/node-api/server/context" - cmtclient "github.com/cometbft/cometbft/rpc/client/http" - "github.com/cosmos/cosmos-sdk/client" ) -const RPCEndpoint = "tcp://localhost:26657" - // Handler is the handler for the node API. type Handler[ContextT context.Context] struct { *handlers.BaseHandler[ContextT] - backend Backend - rpcClient *cmtclient.HTTP - clientCtx client.Context + backend Backend } -func NewHandler[ContextT context.Context](backend Backend) *Handler[ContextT] { - rpcClient, err := cmtclient.New(RPCEndpoint) - if err != nil { - // Not returning error to keep the pattern same across all handlers. - rpcClient = nil - } - clientCtx := client.Context{}.WithClient(rpcClient) - +func NewHandler[ContextT context.Context](backend Backend, +) *Handler[ContextT] { h := &Handler[ContextT]{ BaseHandler: handlers.NewBaseHandler( handlers.NewRouteSet[ContextT](""), ), - backend: backend, - rpcClient: rpcClient, - clientCtx: clientCtx, + backend: backend, } return h } diff --git a/mod/node-api/handlers/node/node.go b/mod/node-api/handlers/node/node.go index 1eaa01467e..2320db1ef7 100644 --- a/mod/node-api/handlers/node/node.go +++ b/mod/node-api/handlers/node/node.go @@ -21,38 +21,38 @@ package node import ( - "context" - "github.com/berachain/beacon-kit/mod/errors" nodetypes "github.com/berachain/beacon-kit/mod/node-api/handlers/node/types" "github.com/berachain/beacon-kit/mod/node-api/handlers/types" - "github.com/cosmos/cosmos-sdk/client/grpc/cmtservice" ) // Syncing returns the syncing status of the beacon node. func (h *Handler[ContextT]) Syncing(_ ContextT) (any, error) { - if h.clientCtx.Client == nil { - return nil, errors.New("RPC client not initialized") - } - // Query node status from the cometBFT node - status, err := cmtservice.GetNodeStatus(context.Background(), h.clientCtx) - if err != nil { - return nil, errors.Wrapf(err, "err in getting node status") + node := h.backend.GetNode() + if node == nil { + return nil, errors.New("node is nil") } + + // Get blockStore for heights + blockStore := node.BlockStore() + latestHeight := blockStore.Height() + baseHeight := blockStore.Base() + + // Get consensus reactor for sync status + consensusReactor := node.ConsensusReactor() + response := nodetypes.SyncingData{ - HeadSlot: status.SyncInfo.LatestBlockHeight, + HeadSlot: latestHeight, IsOptimistic: true, ELOffline: false, } - // Calculate sync distance - if status.SyncInfo.CatchingUp { - // If we're catching up, the sync distance is the difference between - // the latest block height and the earliest block height - response.SyncDistance = status.SyncInfo.LatestBlockHeight - - status.SyncInfo.EarliestBlockHeight - response.IsSyncing = true + // Check if we're catching up by comparing heights + if consensusReactor != nil { + response.SyncDistance = latestHeight - baseHeight } + response.IsSyncing = response.SyncDistance > 0 + return types.Wrap(&response), nil } diff --git a/mod/node-core/pkg/components/api.go b/mod/node-core/pkg/components/api.go index f66bc6ed4d..2cec1eaad8 100644 --- a/mod/node-core/pkg/components/api.go +++ b/mod/node-core/pkg/components/api.go @@ -29,6 +29,7 @@ import ( "github.com/berachain/beacon-kit/mod/node-api/handlers" "github.com/berachain/beacon-kit/mod/node-api/server" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" + "github.com/cometbft/cometbft/node" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -78,6 +79,7 @@ func ProvideNodeAPIBackend[ NodeT interface { CreateQueryContext(height int64, prove bool) (sdk.Context, error) GetBeaconVersion() (string, error) + GetCometNode() *node.Node }, StorageBackendT StorageBackend[ AvailabilityStoreT, BeaconStateT, BeaconBlockStoreT, DepositStoreT, diff --git a/mod/node-core/pkg/components/api_handlers.go b/mod/node-core/pkg/components/api_handlers.go index df5ef6ce1f..1e46eee731 100644 --- a/mod/node-core/pkg/components/api_handlers.go +++ b/mod/node-core/pkg/components/api_handlers.go @@ -30,6 +30,7 @@ import ( eventsapi "github.com/berachain/beacon-kit/mod/node-api/handlers/events" nodeapi "github.com/berachain/beacon-kit/mod/node-api/handlers/node" proofapi "github.com/berachain/beacon-kit/mod/node-api/handlers/proof" + "github.com/cometbft/cometbft/node" ) type NodeAPIHandlersInput[ @@ -146,6 +147,7 @@ func ProvideNodeAPIEventsHandler[ type Backend interface { GetNodeVersion() (string, error) + GetNode() *node.Node } func ProvideNodeAPINodeHandler[ From 0a01fcdf085977a8f1ea57cf41399b8393a73dc7 Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Wed, 6 Nov 2024 12:15:47 +0530 Subject: [PATCH 7/9] remove unncessary changes Signed-off-by: nidhi-singh02 --- mod/node-api/handlers/node/handler.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mod/node-api/handlers/node/handler.go b/mod/node-api/handlers/node/handler.go index 19515ce1b4..f99ea1141d 100644 --- a/mod/node-api/handlers/node/handler.go +++ b/mod/node-api/handlers/node/handler.go @@ -25,14 +25,12 @@ import ( "github.com/berachain/beacon-kit/mod/node-api/server/context" ) -// Handler is the handler for the node API. type Handler[ContextT context.Context] struct { *handlers.BaseHandler[ContextT] backend Backend } -func NewHandler[ContextT context.Context](backend Backend, -) *Handler[ContextT] { +func NewHandler[ContextT context.Context](backend Backend) *Handler[ContextT] { h := &Handler[ContextT]{ BaseHandler: handlers.NewBaseHandler( handlers.NewRouteSet[ContextT](""), From 97ee26f3769ec6e2ce5c596d8c598b4abbd8ee50 Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Wed, 6 Nov 2024 12:34:50 +0530 Subject: [PATCH 8/9] dont use ConsensusReactor Signed-off-by: nidhi-singh02 --- mod/node-api/handlers/node/node.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/mod/node-api/handlers/node/node.go b/mod/node-api/handlers/node/node.go index 2320db1ef7..92a8950749 100644 --- a/mod/node-api/handlers/node/node.go +++ b/mod/node-api/handlers/node/node.go @@ -35,23 +35,26 @@ func (h *Handler[ContextT]) Syncing(_ ContextT) (any, error) { // Get blockStore for heights blockStore := node.BlockStore() + if blockStore == nil { + return nil, errors.New("block store is nil") + } + latestHeight := blockStore.Height() baseHeight := blockStore.Base() - // Get consensus reactor for sync status - consensusReactor := node.ConsensusReactor() - response := nodetypes.SyncingData{ HeadSlot: latestHeight, IsOptimistic: true, ELOffline: false, } - // Check if we're catching up by comparing heights - if consensusReactor != nil { - response.SyncDistance = latestHeight - baseHeight + // 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 } - response.IsSyncing = response.SyncDistance > 0 return types.Wrap(&response), nil } From 3c399f7cc332e2d708b29227df2212310f6d3581 Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Wed, 6 Nov 2024 14:09:14 +0530 Subject: [PATCH 9/9] added error vars Signed-off-by: nidhi-singh02 --- mod/node-api/handlers/node/node.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mod/node-api/handlers/node/node.go b/mod/node-api/handlers/node/node.go index 92a8950749..f986a00f71 100644 --- a/mod/node-api/handlers/node/node.go +++ b/mod/node-api/handlers/node/node.go @@ -26,17 +26,22 @@ import ( "github.com/berachain/beacon-kit/mod/node-api/handlers/types" ) +var ( + errNilBlockStore = errors.New("block store is nil") + errNilNode = errors.New("node is nil") +) + // Syncing returns the syncing status of the beacon node. func (h *Handler[ContextT]) Syncing(_ ContextT) (any, error) { node := h.backend.GetNode() if node == nil { - return nil, errors.New("node is nil") + return nil, errNilNode } // Get blockStore for heights blockStore := node.BlockStore() if blockStore == nil { - return nil, errors.New("block store is nil") + return nil, errNilBlockStore } latestHeight := blockStore.Height()