From 7474b12650e7c9135d6ef68ad2b4751ab6c576f7 Mon Sep 17 00:00:00 2001 From: archbear Date: Mon, 12 Aug 2024 18:45:08 -0400 Subject: [PATCH] api --- mod/node-core/pkg/components/api.go | 267 +++++++++++++++------ mod/node-core/pkg/components/interfaces.go | 15 +- 2 files changed, 202 insertions(+), 80 deletions(-) diff --git a/mod/node-core/pkg/components/api.go b/mod/node-core/pkg/components/api.go index 5fa909272e..502816dbfa 100644 --- a/mod/node-core/pkg/components/api.go +++ b/mod/node-core/pkg/components/api.go @@ -20,90 +20,199 @@ package components -// import ( -// "cosmossdk.io/depinject" -// sdklog "cosmossdk.io/log" -// "github.com/berachain/beacon-kit/mod/config" -// "github.com/berachain/beacon-kit/mod/log" -// "github.com/berachain/beacon-kit/mod/node-api/backend" -// "github.com/berachain/beacon-kit/mod/node-api/engines/echo" -// "github.com/berachain/beacon-kit/mod/node-api/handlers" -// "github.com/berachain/beacon-kit/mod/node-api/server" -// nodetypes "github.com/berachain/beacon-kit/mod/node-core/pkg/types" -// "github.com/berachain/beacon-kit/mod/primitives/pkg/common" -// sdk "github.com/cosmos/cosmos-sdk/types" -// ) +import ( + "cosmossdk.io/depinject" + "github.com/berachain/beacon-kit/mod/config" + "github.com/berachain/beacon-kit/mod/log" + "github.com/berachain/beacon-kit/mod/node-api/backend" + "github.com/berachain/beacon-kit/mod/node-api/engines/echo" + "github.com/berachain/beacon-kit/mod/node-api/handlers" + "github.com/berachain/beacon-kit/mod/node-api/server" + nodetypes "github.com/berachain/beacon-kit/mod/node-core/pkg/types" + "github.com/berachain/beacon-kit/mod/primitives/pkg/common" + sdk "github.com/cosmos/cosmos-sdk/types" +) -// // TODO: we could make engine type configurable -// func ProvideNodeAPIEngine() *NodeAPIEngine { -// return echo.NewDefaultEngine() -// } +// TODO: we could make engine type configurable +func ProvideNodeAPIEngine() *echo.Engine { + return echo.NewDefaultEngine() +} -// type NodeAPIBackendInput struct { -// depinject.In +type NodeAPIBackendInput[ + AvailabilityStoreT any, + BeaconBlockT any, + BeaconStateT any, + BlobSidecarsT any, + BlockStoreT any, + ContextT any, + DepositT any, + DepositStoreT any, + ExecutionPayloadT any, + StateProcessorT StateProcessor[ + BeaconBlockT, BeaconStateT, ContextT, + DepositT, ExecutionPayloadT, + ], + StorageBackendT StorageBackend[ + AvailabilityStoreT, BeaconStateT, BlockStoreT, DepositStoreT, + ], +] struct { + depinject.In -// ChainSpec common.ChainSpec -// StateProcessor *StateProcessor -// StorageBackend *StorageBackend -// } + ChainSpec common.ChainSpec + StateProcessor StateProcessorT + StorageBackend StorageBackendT +} -// func ProvideNodeAPIBackend(in NodeAPIBackendInput) *NodeAPIBackend { -// return backend.New[ -// *AvailabilityStore, -// *BeaconBlock, -// *BeaconBlockBody, -// *BeaconBlockHeader, -// *BeaconState, -// *BeaconStateMarshallable, -// *BlobSidecars, -// *BlockStore, -// sdk.Context, -// *Deposit, -// *DepositStore, -// *Eth1Data, -// *ExecutionPayloadHeader, -// *Fork, -// nodetypes.Node, -// *KVStore, -// *StorageBackend, -// *Validator, -// Validators, -// *Withdrawal, -// WithdrawalCredentials, -// ]( -// in.StorageBackend, -// in.ChainSpec, -// in.StateProcessor, -// ) -// } +func ProvideNodeAPIBackend[ + AvailabilityStoreT AvailabilityStore[BeaconBlockBodyT, BlobSidecarsT], + BeaconBlockT any, + BeaconBlockBodyT any, + BeaconBlockHeaderT BeaconBlockHeader[BeaconBlockHeaderT], + BeaconStateT BeaconState[ + BeaconStateT, BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT, + ForkT, KVStoreT, ValidatorT, ValidatorsT, WithdrawalT, + ], + BeaconStateMarshallableT any, + BlobSidecarsT any, + BlockStoreT BlockStore[BeaconBlockT], + DepositT any, + DepositStoreT DepositStore[DepositT], + Eth1DataT any, + ExecutionPayloadT any, + ExecutionPayloadHeaderT any, + ForkT any, + KVStoreT any, + NodeT nodetypes.Node, + StateProcessorT StateProcessor[ + BeaconBlockT, BeaconStateT, TransitionContextT, + DepositT, ExecutionPayloadT, + ], + StorageBackendT StorageBackend[ + AvailabilityStoreT, BeaconStateT, BlockStoreT, DepositStoreT, + ], + TransitionContextT any, + ValidatorT Validator[ValidatorT, WithdrawalCredentialsT], + ValidatorsT Validators[ValidatorT], + WithdrawalT Withdrawal[WithdrawalT], + WithdrawalCredentialsT WithdrawalCredentials, +](in NodeAPIBackendInput[ + AvailabilityStoreT, BeaconBlockT, BeaconStateT, BlobSidecarsT, + BlockStoreT, TransitionContextT, DepositT, DepositStoreT, ExecutionPayloadT, + StateProcessorT, StorageBackendT, +]) *backend.Backend[ + AvailabilityStoreT, BeaconBlockT, BeaconBlockBodyT, + BeaconBlockHeaderT, BeaconStateT, BeaconStateMarshallableT, + BlobSidecarsT, BlockStoreT, sdk.Context, DepositT, DepositStoreT, + Eth1DataT, ExecutionPayloadHeaderT, ForkT, NodeT, KVStoreT, + StorageBackendT, ValidatorT, ValidatorsT, WithdrawalT, + WithdrawalCredentialsT, +] { + return backend.New[ + AvailabilityStoreT, + BeaconBlockT, + BeaconBlockBodyT, + BeaconBlockHeaderT, + BeaconStateT, + BeaconStateMarshallableT, + BlobSidecarsT, + BlockStoreT, + sdk.Context, + DepositT, + DepositStoreT, + Eth1DataT, + ExecutionPayloadHeaderT, + ForkT, + NodeT, + KVStoreT, + StorageBackendT, + ValidatorT, + ValidatorsT, + WithdrawalT, + WithdrawalCredentialsT, + ]( + in.StorageBackend, + in.ChainSpec, + in.StateProcessor, + ) +} -// type NodeAPIServerInput struct { -// depinject.In +type NodeAPIServerInput[ + ContextT NodeAPIContext, + EngineT NodeAPIEngine[ContextT], + LoggerT log.AdvancedLogger[any, LoggerT], +] struct { + depinject.In -// Engine *NodeAPIEngine -// Config *config.Config -// Handlers []handlers.Handlers[NodeAPIContext] -// Logger log.AdvancedLogger[any, sdklog.Logger] -// } + Engine EngineT + Config *config.Config + Handlers []handlers.Handlers[ContextT] + Logger LoggerT +} -// func ProvideNodeAPIServer(in NodeAPIServerInput) *NodeAPIServer { -// in.Logger.AddKeyValColor("service", "node-api-server", -// log.Blue) -// return server.New[ -// NodeAPIContext, -// *NodeAPIEngine, -// ]( -// in.Config.NodeAPI, -// in.Engine, -// in.Logger.With("service", "node-api-server"), -// in.Handlers..., -// ) -// } +func ProvideNodeAPIServer[ + ContextT NodeAPIContext, + EngineT NodeAPIEngine[ContextT], + LoggerT log.AdvancedLogger[any, LoggerT], +](in NodeAPIServerInput[ + ContextT, EngineT, LoggerT, +]) *server.Server[ContextT, EngineT] { + in.Logger.AddKeyValColor("service", "node-api-server", + log.Blue) + return server.New[ContextT, EngineT]( + in.Config.NodeAPI, + in.Engine, + in.Logger.With("service", "node-api-server"), + in.Handlers..., + ) +} -// func DefaultNodeAPIComponents() []any { -// return []any{ -// ProvideNodeAPIServer, -// ProvideNodeAPIEngine, -// ProvideNodeAPIBackend, -// } -// } +func DefaultNodeAPIComponents[ + AvailabilityStoreT AvailabilityStore[BeaconBlockBodyT, BlobSidecarsT], + BeaconBlockT any, + BeaconBlockBodyT any, + BeaconBlockHeaderT BeaconBlockHeader[BeaconBlockHeaderT], + BeaconStateT BeaconState[ + BeaconStateT, BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT, + ForkT, KVStoreT, ValidatorT, ValidatorsT, WithdrawalT, + ], + BeaconStateMarshallableT any, + BlobSidecarsT any, + BlockStoreT BlockStore[BeaconBlockT], + ContextT NodeAPIContext, + DepositT any, + DepositStoreT DepositStore[DepositT], + EngineT NodeAPIEngine[ContextT], + Eth1DataT any, + ExecutionPayloadT any, + ExecutionPayloadHeaderT any, + ForkT any, + KVStoreT any, + LoggerT log.AdvancedLogger[any, LoggerT], + NodeT nodetypes.Node, + StateProcessorT StateProcessor[ + BeaconBlockT, BeaconStateT, TransitionContextT, + DepositT, ExecutionPayloadT, + ], + StorageBackendT StorageBackend[ + AvailabilityStoreT, BeaconStateT, BlockStoreT, DepositStoreT, + ], + TransitionContextT any, + ValidatorT Validator[ValidatorT, WithdrawalCredentialsT], + ValidatorsT Validators[ValidatorT], + WithdrawalT Withdrawal[WithdrawalT], + WithdrawalCredentialsT WithdrawalCredentials, +]() []any { + return []any{ + ProvideNodeAPIServer[ContextT, EngineT, LoggerT], + ProvideNodeAPIEngine, + ProvideNodeAPIBackend[ + AvailabilityStoreT, BeaconBlockT, BeaconBlockBodyT, + BeaconBlockHeaderT, BeaconStateT, BeaconStateMarshallableT, + BlobSidecarsT, BlockStoreT, DepositT, DepositStoreT, + Eth1DataT, ExecutionPayloadT, ExecutionPayloadHeaderT, ForkT, + KVStoreT, NodeT, StateProcessorT, StorageBackendT, + TransitionContextT, ValidatorT, ValidatorsT, WithdrawalT, + WithdrawalCredentialsT, + ], + } +} diff --git a/mod/node-core/pkg/components/interfaces.go b/mod/node-core/pkg/components/interfaces.go index 44ab781235..bb16992a6d 100644 --- a/mod/node-core/pkg/components/interfaces.go +++ b/mod/node-core/pkg/components/interfaces.go @@ -8,6 +8,8 @@ import ( asynctypes "github.com/berachain/beacon-kit/mod/async/pkg/types" engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives" + "github.com/berachain/beacon-kit/mod/log" + "github.com/berachain/beacon-kit/mod/node-api/handlers" "github.com/berachain/beacon-kit/mod/node-api/handlers/beacon/types" nodetypes "github.com/berachain/beacon-kit/mod/node-core/pkg/types" "github.com/berachain/beacon-kit/mod/primitives/pkg/bytes" @@ -705,7 +707,8 @@ type ( IsPartiallyWithdrawable(amount1 math.Gwei, amount2 math.Gwei) bool } - Validators interface { + Validators[ValidatorT any] interface { + ~[]ValidatorT HashTreeRoot() common.Root } @@ -1029,6 +1032,16 @@ type ( /* -------------------------------------------------------------------------- */ type ( + NodeAPIContext interface { + context.Context + Bind(any) error + Validate(any) error + } + // Engine is a generic interface for an API engine. + NodeAPIEngine[ContextT NodeAPIContext] interface { + Run(addr string) error + RegisterRoutes(*handlers.RouteSet[ContextT], log.Logger[any]) + } NodeAPIBackend[ BeaconStateT any, BeaconBlockHeaderT any,