Skip to content

Commit

Permalink
api
Browse files Browse the repository at this point in the history
  • Loading branch information
archbear committed Aug 12, 2024
1 parent 1b9591d commit 7474b12
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 80 deletions.
267 changes: 188 additions & 79 deletions mod/node-core/pkg/components/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
}
}
15 changes: 14 additions & 1 deletion mod/node-core/pkg/components/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -705,7 +707,8 @@ type (
IsPartiallyWithdrawable(amount1 math.Gwei, amount2 math.Gwei) bool
}

Validators interface {
Validators[ValidatorT any] interface {
~[]ValidatorT
HashTreeRoot() common.Root
}

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 7474b12

Please sign in to comment.