Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(node-api): cleanup #1931

Merged
merged 2 commits into from
Aug 19, 2024
Merged
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
56 changes: 27 additions & 29 deletions mod/node-api/backend/mocks/beacon_state.mock.go

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions mod/node-api/backend/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ type BeaconBlockHeader[BeaconBlockHeaderT any] interface {

// BeaconState is the interface for the beacon state.
type BeaconState[
BeaconBlockHeaderT BeaconBlockHeader[BeaconBlockHeaderT],
Eth1DataT, ExecutionPayloadHeaderT, ForkT,
ValidatorT, ValidatorsT, WithdrawalT any,
BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT,
ForkT, ValidatorT, ValidatorsT, WithdrawalT any,
] interface {
// SetSlot sets the slot on the beacon state.
SetSlot(math.Slot) error
Expand Down
9 changes: 4 additions & 5 deletions mod/node-api/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,28 @@ package handlers
import (
"github.com/berachain/beacon-kit/mod/errors"
"github.com/berachain/beacon-kit/mod/log"
"github.com/berachain/beacon-kit/mod/node-api/server/context"
)

// handlerFn enforces a signature for all handler functions.
type handlerFn[ContextT context.Context] func(c ContextT) (any, error)
type handlerFn[ContextT any] func(c ContextT) (any, error)

// Handlers is an interface that all handlers must implement.
type Handlers[ContextT context.Context] interface {
type Handlers[ContextT any] interface {
// RegisterRoutes is a method that registers the routes for the handler.
RegisterRoutes(logger log.Logger[any])
RouteSet() *RouteSet[ContextT]
}

// BaseHandler is a base handler for all handlers. It abstracts the route set
// and logger from the handler.
type BaseHandler[ContextT context.Context] struct {
type BaseHandler[ContextT any] struct {
routes *RouteSet[ContextT]
logger log.Logger[any]
}

// NewBaseHandler initializes a new base handler with the given routes and
// logger.
func NewBaseHandler[ContextT context.Context](
func NewBaseHandler[ContextT any](
routes *RouteSet[ContextT],
) *BaseHandler[ContextT] {
return &BaseHandler[ContextT]{
Expand Down
2 changes: 1 addition & 1 deletion mod/node-api/handlers/proof/block_proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
// GetBlockProposer returns the block proposer pubkey for the given block id
// along with a merkle proof that can be verified against the beacon block root.
func (h *Handler[
ContextT, BeaconBlockHeaderT, _, _, _, _,
BeaconBlockHeaderT, _, _, ContextT, _, _,
]) GetBlockProposer(c ContextT) (any, error) {
params, err := utils.BindAndValidate[types.BlockProposerRequest](
c, h.Logger(),
Expand Down
2 changes: 1 addition & 1 deletion mod/node-api/handlers/proof/execution_fee_recipient.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
// payload header for the given block id, along with the proof that can be
// verified against the beacon block root.
func (h *Handler[
ContextT, BeaconBlockHeaderT, _, _, _, _,
BeaconBlockHeaderT, _, _, ContextT, _, _,
]) GetExecutionFeeRecipient(c ContextT) (any, error) {
params, err := utils.BindAndValidate[types.ExecutionFeeRecipientRequest](
c, h.Logger(),
Expand Down
2 changes: 1 addition & 1 deletion mod/node-api/handlers/proof/execution_number.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
// payload header for the given block id, along with the proof that can be
// verified against the beacon block root.
func (h *Handler[
ContextT, BeaconBlockHeaderT, _, _, _, _,
BeaconBlockHeaderT, _, _, ContextT, _, _,
]) GetExecutionNumber(c ContextT) (any, error) {
params, err := utils.BindAndValidate[types.ExecutionNumberRequest](
c, h.Logger(),
Expand Down
14 changes: 7 additions & 7 deletions mod/node-api/handlers/proof/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ import (

// Handler is the handler for the proof API.
type Handler[
ContextT context.Context,
BeaconBlockHeaderT types.BeaconBlockHeader,
BeaconStateT types.BeaconState[
BeaconStateMarshallableT, ExecutionPayloadHeaderT, ValidatorT,
],
BeaconStateMarshallableT types.BeaconStateMarshallable,
ContextT context.Context,
ExecutionPayloadHeaderT types.ExecutionPayloadHeader,
ValidatorT types.Validator,
] struct {
Expand All @@ -45,23 +45,23 @@ type Handler[

// NewHandler creates a new handler for the proof API.
func NewHandler[
ContextT context.Context,
BeaconBlockHeaderT types.BeaconBlockHeader,
BeaconStateT types.BeaconState[
BeaconStateMarshallableT, ExecutionPayloadHeaderT, ValidatorT,
],
BeaconStateMarshallableT types.BeaconStateMarshallable,
ContextT context.Context,
ExecutionPayloadHeaderT types.ExecutionPayloadHeader,
ValidatorT types.Validator,
](
backend Backend[BeaconBlockHeaderT, BeaconStateT, ValidatorT],
) *Handler[
ContextT, BeaconBlockHeaderT, BeaconStateT, BeaconStateMarshallableT,
ExecutionPayloadHeaderT, ValidatorT,
BeaconBlockHeaderT, BeaconStateT, BeaconStateMarshallableT,
ContextT, ExecutionPayloadHeaderT, ValidatorT,
] {
h := &Handler[
ContextT, BeaconBlockHeaderT, BeaconStateT, BeaconStateMarshallableT,
ExecutionPayloadHeaderT, ValidatorT,
BeaconBlockHeaderT, BeaconStateT, BeaconStateMarshallableT,
ContextT, ExecutionPayloadHeaderT, ValidatorT,
]{
BaseHandler: handlers.NewBaseHandler(
handlers.NewRouteSet[ContextT](""),
Expand All @@ -74,7 +74,7 @@ func NewHandler[
// Get the slot from the given input of execution id, beacon state, and beacon
// block header for the resolved slot.
func (h *Handler[
ContextT, BeaconBlockHeaderT, BeaconStateT, _, _, _,
BeaconBlockHeaderT, BeaconStateT, _, _, _, _,
]) resolveExecutionID(executionID string) (
math.Slot, BeaconStateT, BeaconBlockHeaderT, error,
) {
Expand Down
2 changes: 1 addition & 1 deletion mod/node-api/handlers/proof/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

func (
h *Handler[ContextT, _, _, _, _, _],
h *Handler[_, _, _, ContextT, _, _],
) RegisterRoutes(logger log.Logger[any]) {
h.SetLogger(logger)
h.BaseHandler.AddRoutes([]*handlers.Route[ContextT]{
Expand Down
7 changes: 3 additions & 4 deletions mod/node-api/handlers/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ package handlers

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

// Route is a route for the node API.
type Route[ContextT context.Context] struct {
type Route[ContextT any] struct {
Method string
Path string
Handler handlerFn[ContextT]
Expand All @@ -48,13 +47,13 @@ func (r *Route[ContextT]) DecorateWithLogs(logger log.Logger[any]) {
}

// RouteSet is a set of routes for the node API.
type RouteSet[ContextT context.Context] struct {
type RouteSet[ContextT any] struct {
BasePath string
Routes []*Route[ContextT]
}

// NewRouteSet creates a new route set.
func NewRouteSet[ContextT context.Context](
func NewRouteSet[ContextT any](
basePath string, routes ...*Route[ContextT],
) *RouteSet[ContextT] {
return &RouteSet[ContextT]{
Expand Down
16 changes: 7 additions & 9 deletions mod/node-api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ import (
// Server is the API Server service.
type Server[
ContextT apicontext.Context,
EngineT Engine[ContextT, EngineT],
] struct {
engine EngineT
engine Engine[ContextT]
config Config
logger log.Logger[any]
}
Expand All @@ -44,13 +43,12 @@ type Server[
// disabled.
func New[
ContextT apicontext.Context,
EngineT Engine[ContextT, EngineT],
](
config Config,
engine EngineT,
engine Engine[ContextT],
logger log.Logger[any],
handlers ...handlers.Handlers[ContextT],
) *Server[ContextT, EngineT] {
) *Server[ContextT] {
apiLogger := logger
if !config.Logging {
apiLogger = noop.NewLogger[log.Logger[any]]()
Expand All @@ -59,23 +57,23 @@ func New[
handler.RegisterRoutes(apiLogger)
engine.RegisterRoutes(handler.RouteSet(), apiLogger)
}
return &Server[ContextT, EngineT]{
return &Server[ContextT]{
engine: engine,
config: config,
logger: logger,
}
}

// Start starts the API Server at the configured address.
func (s *Server[_, _]) Start(ctx context.Context) error {
func (s *Server[_]) Start(ctx context.Context) error {
if !s.config.Enabled {
return nil
}
go s.start(ctx)
return nil
}

func (s *Server[_, _]) start(ctx context.Context) {
func (s *Server[_]) start(ctx context.Context) {
errCh := make(chan error)
go func() {
errCh <- s.engine.Run(s.config.Address)
Expand All @@ -91,6 +89,6 @@ func (s *Server[_, _]) start(ctx context.Context) {
}

// Name returns the name of the API server service.
func (s *Server[_, _]) Name() string {
func (s *Server[_]) Name() string {
return "node-api-server"
}
2 changes: 1 addition & 1 deletion mod/node-api/server/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

// Engine is a generic interface for an API engine.
type Engine[ContextT context.Context, T any] interface {
type Engine[ContextT context.Context] interface {
Run(addr string) error
RegisterRoutes(*handlers.RouteSet[ContextT], log.Logger[any])
}
5 changes: 1 addition & 4 deletions mod/node-core/pkg/components/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ func ProvideNodeAPIServer[
](in NodeAPIServerInput[LoggerT]) *NodeAPIServer {
in.Logger.AddKeyValColor("service", "node-api-server",
log.Blue)
return server.New[
NodeAPIContext,
*NodeAPIEngine,
](
return server.New[NodeAPIContext](
in.Config.NodeAPI,
in.Engine,
in.Logger.With("service", "node-api-server"),
Expand Down
5 changes: 4 additions & 1 deletion mod/node-core/pkg/components/api_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ func ProvideNodeAPINodeHandler() *NodeAPIHandler {
}

func ProvideNodeAPIProofHandler(b *NodeAPIBackend) *ProofAPIHandler {
return proofapi.NewHandler[NodeAPIContext](b)
return proofapi.NewHandler[
*BeaconBlockHeader, *BeaconState, *BeaconStateMarshallable,
NodeAPIContext, *ExecutionPayloadHeader, *Validator,
](b)
}

func DefaultNodeAPIHandlers() []any {
Expand Down
9 changes: 3 additions & 6 deletions mod/node-core/pkg/components/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,7 @@ type (
NodeAPIEngine = echo.Engine

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

// PayloadAttributes is a type alias for the payload attributes.
PayloadAttributes = engineprimitives.PayloadAttributes[*Withdrawal]
Expand Down Expand Up @@ -489,7 +486,7 @@ type (

// ProofAPIHandler is a type alias for the proof handler.
ProofAPIHandler = proofapi.Handler[
NodeAPIContext, *BeaconBlockHeader, *BeaconState,
*BeaconStateMarshallable, *ExecutionPayloadHeader, *Validator,
*BeaconBlockHeader, *BeaconState, *BeaconStateMarshallable,
NodeAPIContext, *ExecutionPayloadHeader, *Validator,
]
)
7 changes: 3 additions & 4 deletions mod/state-transition/pkg/core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
// is a combination of the read-only and write-only beacon state types.
type BeaconState[
T any,
BeaconBlockHeaderT BeaconBlockHeader[BeaconBlockHeaderT],
BeaconBlockHeaderT,
Eth1DataT,
ExecutionPayloadHeaderT,
ForkT,
Expand Down Expand Up @@ -60,9 +60,8 @@ type BeaconState[

// ReadOnlyBeaconState is the interface for a read-only beacon state.
type ReadOnlyBeaconState[
BeaconBlockHeaderT BeaconBlockHeader[BeaconBlockHeaderT],
Eth1DataT, ExecutionPayloadHeaderT, ForkT,
ValidatorT, ValidatorsT, WithdrawalT any,
BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT,
ForkT, ValidatorT, ValidatorsT, WithdrawalT any,
] interface {
ReadOnlyEth1Data[Eth1DataT, ExecutionPayloadHeaderT]
ReadOnlyRandaoMixes
Expand Down
Loading