Skip to content

Commit

Permalink
chore(api): Dropped node api context generic (#2456)
Browse files Browse the repository at this point in the history
Signed-off-by: aBear <[email protected]>
  • Loading branch information
abi87 authored Feb 2, 2025
1 parent d4cf42b commit 928024c
Show file tree
Hide file tree
Showing 39 changed files with 175 additions and 279 deletions.
18 changes: 9 additions & 9 deletions cmd/beacond/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,20 @@ func DefaultComponents() []any {
components.ProvideShutDownService[*Logger],
}
c = append(c,
components.ProvideNodeAPIServer[*Logger, NodeAPIContext],
components.ProvideNodeAPIServer[*Logger],
components.ProvideNodeAPIEngine,
components.ProvideNodeAPIBackend,
)

c = append(c,
components.ProvideNodeAPIHandlers[NodeAPIContext],
components.ProvideNodeAPIBeaconHandler[NodeAPIContext],
components.ProvideNodeAPIBuilderHandler[NodeAPIContext],
components.ProvideNodeAPIConfigHandler[NodeAPIContext],
components.ProvideNodeAPIDebugHandler[NodeAPIContext],
components.ProvideNodeAPIEventsHandler[NodeAPIContext],
components.ProvideNodeAPINodeHandler[NodeAPIContext],
components.ProvideNodeAPIProofHandler[NodeAPIContext],
components.ProvideNodeAPIHandlers,
components.ProvideNodeAPIBeaconHandler,
components.ProvideNodeAPIBuilderHandler,
components.ProvideNodeAPIConfigHandler,
components.ProvideNodeAPIDebugHandler,
components.ProvideNodeAPIEventsHandler,
components.ProvideNodeAPINodeHandler,
components.ProvideNodeAPIProofHandler,
)

return c
Expand Down
5 changes: 1 addition & 4 deletions cmd/beacond/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type (
NodeAPIEngine = echo.Engine

// NodeAPIServer is a type alias for the node API server.
NodeAPIServer = server.Server[NodeAPIContext]
NodeAPIServer = server.Server

// ReportingService is a type alias for the reporting service.
ReportingService = version.ReportingService
Expand Down Expand Up @@ -136,7 +136,4 @@ type (

// NodeAPIBackend is a type alias for the node API backend.
NodeAPIBackend = backend.Backend

// NodeAPIContext is a type alias for the node API context.
NodeAPIContext = echo.Context
)
5 changes: 1 addition & 4 deletions node-api/engines/echo/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ func (e *Engine) Run(addr string) error {
}

// RegisterRoutes registers the given route set with the Echo engine.
func (e *Engine) RegisterRoutes(
hs *handlers.RouteSet[Context],
logger log.Logger,
) {
func (e *Engine) RegisterRoutes(hs *handlers.RouteSet, logger log.Logger) {
e.logger = logger
group := e.Group(hs.BasePath)
for _, route := range hs.Routes {
Expand Down
6 changes: 2 additions & 4 deletions node-api/engines/echo/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ type ErrorResponse struct {

// responseMiddleware is a middleware that converts errors to an HTTP status
// code and response.
func responseMiddleware(
handler *handlers.Route[Context],
) echo.HandlerFunc {
return func(c Context) error {
func responseMiddleware(handler *handlers.Route) echo.HandlerFunc {
return func(c handlers.Context) error {
data, err := handler.Handler(c)
code, response := responseFromError(data, err)
return c.JSON(code, response)
Expand Down
27 changes: 0 additions & 27 deletions node-api/engines/echo/types.go

This file was deleted.

3 changes: 2 additions & 1 deletion node-api/handlers/beacon/blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ package beacon
import (
"strconv"

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

// GetBlobSidecars provides an implementation for the
// "/eth/v1/beacon/blob_sidecars/:block_id" API endpoint.
func (h *Handler[ContextT]) GetBlobSidecars(c ContextT) (any, error) {
func (h *Handler) GetBlobSidecars(c handlers.Context) (any, error) {
req, err := utils.BindAndValidate[apitypes.GetBlobSidecarsRequest](
c, h.Logger(),
)
Expand Down
3 changes: 2 additions & 1 deletion node-api/handlers/beacon/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
package beacon

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

func (h *Handler[ContextT]) GetBlockRewards(c ContextT) (any, error) {
func (h *Handler) GetBlockRewards(c handlers.Context) (any, error) {
req, err := utils.BindAndValidate[beacontypes.GetBlockRewardsRequest](
c, h.Logger(),
)
Expand Down
3 changes: 2 additions & 1 deletion node-api/handlers/beacon/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
package beacon

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

func (h *Handler[ContextT]) GetGenesis(_ ContextT) (any, error) {
func (h *Handler) GetGenesis(_ handlers.Context) (any, error) {
genesisRoot, err := h.backend.GenesisValidatorsRoot(utils.Genesis)
if err != nil {
return nil, err
Expand Down
17 changes: 5 additions & 12 deletions node-api/handlers/beacon/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,19 @@ package beacon

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

// Handler is the handler for the beacon API.
type Handler[
ContextT context.Context,
] struct {
*handlers.BaseHandler[ContextT]
type Handler struct {
*handlers.BaseHandler
backend Backend
}

// NewHandler creates a new handler for the beacon API.
func NewHandler[
ContextT context.Context,
](
backend Backend,
) *Handler[ContextT] {
h := &Handler[ContextT]{
func NewHandler(backend Backend) *Handler {
h := &Handler{
BaseHandler: handlers.NewBaseHandler(
handlers.NewRouteSet[ContextT](""),
handlers.NewRouteSet(""),
),
backend: backend,
}
Expand Down
5 changes: 3 additions & 2 deletions node-api/handlers/beacon/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
package beacon

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

func (h *Handler[ContextT]) GetBlockHeaders(c ContextT) (any, error) {
func (h *Handler) GetBlockHeaders(c handlers.Context) (any, error) {
req, err := utils.BindAndValidate[beacontypes.GetBlockHeadersRequest](
c, h.Logger(),
)
Expand Down Expand Up @@ -54,7 +55,7 @@ func (h *Handler[ContextT]) GetBlockHeaders(c ContextT) (any, error) {
}, nil
}

func (h *Handler[ContextT]) GetBlockHeaderByID(c ContextT) (any, error) {
func (h *Handler) GetBlockHeaderByID(c handlers.Context) (any, error) {
req, err := utils.BindAndValidate[beacontypes.GetBlockHeaderRequest](
c, h.Logger(),
)
Expand Down
5 changes: 3 additions & 2 deletions node-api/handlers/beacon/historical.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
package beacon

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

func (h *Handler[ContextT]) GetStateRoot(c ContextT) (any, error) {
func (h *Handler) GetStateRoot(c handlers.Context) (any, error) {
req, err := utils.BindAndValidate[beacontypes.GetStateRootRequest](
c, h.Logger(),
)
Expand All @@ -51,7 +52,7 @@ func (h *Handler[ContextT]) GetStateRoot(c ContextT) (any, error) {
}, nil
}

func (h *Handler[ContextT]) GetStateFork(c ContextT) (any, error) {
func (h *Handler) GetStateFork(c handlers.Context) (any, error) {
req, err := utils.BindAndValidate[beacontypes.GetStateForkRequest](
c, h.Logger(),
)
Expand Down
3 changes: 2 additions & 1 deletion node-api/handlers/beacon/randao.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
package beacon

import (
"github.com/berachain/beacon-kit/node-api/handlers"
beacontypes "github.com/berachain/beacon-kit/node-api/handlers/beacon/types"
"github.com/berachain/beacon-kit/node-api/handlers/utils"
"github.com/berachain/beacon-kit/primitives/constants"
)

func (h *Handler[ContextT]) GetRandao(c ContextT) (any, error) {
func (h *Handler) GetRandao(c handlers.Context) (any, error) {
req, err := utils.BindAndValidate[beacontypes.GetRandaoRequest](
c,
h.Logger(),
Expand Down
4 changes: 2 additions & 2 deletions node-api/handlers/beacon/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (
)

//nolint:funlen // routes are long
func (h *Handler[ContextT]) RegisterRoutes(
func (h *Handler) RegisterRoutes(
logger log.Logger,
) {
h.SetLogger(logger)
h.BaseHandler.AddRoutes([]*handlers.Route[ContextT]{
h.BaseHandler.AddRoutes([]*handlers.Route{
{
Method: http.MethodGet,
Path: "/eth/v1/beacon/genesis",
Expand Down
21 changes: 6 additions & 15 deletions node-api/handlers/beacon/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
package beacon

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

func (h *Handler[ContextT]) GetStateValidators(
c ContextT,
) (any, error) {
func (h *Handler) GetStateValidators(c handlers.Context) (any, error) {
req, err := utils.BindAndValidate[beacontypes.GetStateValidatorsRequest](
c, h.Logger(),
)
Expand Down Expand Up @@ -61,9 +60,7 @@ func (h *Handler[ContextT]) GetStateValidators(
}, nil
}

func (h *Handler[ContextT]) PostStateValidators(
c ContextT,
) (any, error) {
func (h *Handler) PostStateValidators(c handlers.Context) (any, error) {
req, err := utils.BindAndValidate[beacontypes.PostStateValidatorsRequest](
c, h.Logger(),
)
Expand Down Expand Up @@ -93,9 +90,7 @@ func (h *Handler[ContextT]) PostStateValidators(
}, nil
}

func (h *Handler[ContextT]) GetStateValidator(
c ContextT,
) (any, error) {
func (h *Handler) GetStateValidator(c handlers.Context) (any, error) {
req, err := utils.BindAndValidate[beacontypes.GetStateValidatorRequest](
c, h.Logger(),
)
Expand All @@ -116,9 +111,7 @@ func (h *Handler[ContextT]) GetStateValidator(
return validator, nil
}

func (h *Handler[ContextT]) GetStateValidatorBalances(
c ContextT,
) (any, error) {
func (h *Handler) GetStateValidatorBalances(c handlers.Context) (any, error) {
req, err := utils.BindAndValidate[beacontypes.GetValidatorBalancesRequest](
c, h.Logger(),
)
Expand All @@ -143,9 +136,7 @@ func (h *Handler[ContextT]) GetStateValidatorBalances(
}, nil
}

func (h *Handler[ContextT]) PostStateValidatorBalances(
c ContextT,
) (any, error) {
func (h *Handler) PostStateValidatorBalances(c handlers.Context) (any, error) {
req, err := utils.BindAndValidate[beacontypes.PostValidatorBalancesRequest](
c, h.Logger(),
)
Expand Down
11 changes: 5 additions & 6 deletions node-api/handlers/builder/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ package builder

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

type Handler[ContextT context.Context] struct {
*handlers.BaseHandler[ContextT]
type Handler struct {
*handlers.BaseHandler
}

func NewHandler[ContextT context.Context]() *Handler[ContextT] {
h := &Handler[ContextT]{
func NewHandler() *Handler {
h := &Handler{
BaseHandler: handlers.NewBaseHandler(
handlers.NewRouteSet[ContextT](""),
handlers.NewRouteSet(""),
),
}
return h
Expand Down
6 changes: 2 additions & 4 deletions node-api/handlers/builder/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ import (
"github.com/berachain/beacon-kit/node-api/handlers"
)

func (h *Handler[ContextT]) RegisterRoutes(
logger log.Logger,
) {
func (h *Handler) RegisterRoutes(logger log.Logger) {
h.SetLogger(logger)
h.BaseHandler.AddRoutes([]*handlers.Route[ContextT]{
h.BaseHandler.AddRoutes([]*handlers.Route{
{
Method: http.MethodGet,
Path: "/eth/v1/builder/states/:state_id/expected_withdrawals",
Expand Down
15 changes: 6 additions & 9 deletions node-api/handlers/config/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,16 @@

package config

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

type Handler[ContextT context.Context] struct {
*handlers.BaseHandler[ContextT]
type Handler struct {
*handlers.BaseHandler
}

func NewHandler[ContextT context.Context]() *Handler[ContextT] {
h := &Handler[ContextT]{
func NewHandler() *Handler {
h := &Handler{
BaseHandler: handlers.NewBaseHandler(
handlers.NewRouteSet[ContextT](""),
handlers.NewRouteSet(""),
),
}
return h
Expand Down
6 changes: 2 additions & 4 deletions node-api/handlers/config/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ import (
"github.com/berachain/beacon-kit/node-api/handlers"
)

func (h *Handler[ContextT]) RegisterRoutes(
logger log.Logger,
) {
func (h *Handler) RegisterRoutes(logger log.Logger) {
h.SetLogger(logger)
h.BaseHandler.AddRoutes([]*handlers.Route[ContextT]{
h.BaseHandler.AddRoutes([]*handlers.Route{
{
Method: http.MethodGet,
Path: "/eth/v1/config/fork_schedule",
Expand Down
Loading

0 comments on commit 928024c

Please sign in to comment.