Skip to content

Commit

Permalink
bet
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdevbear committed Jun 21, 2024
1 parent 9a81ca1 commit efb931e
Show file tree
Hide file tree
Showing 22 changed files with 215 additions and 100 deletions.
4 changes: 3 additions & 1 deletion mod/beacon/blockchain/execution_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ func (s *Service[
) {
if _, _, err := s.ee.NotifyForkchoiceUpdate(
ctx,
engineprimitives.BuildForkchoiceUpdateRequest(
engineprimitives.
//nolint:lll // annoying formatter.
BuildForkchoiceUpdateRequest[*engineprimitives.PayloadAttributes[*engineprimitives.Withdrawal]](
&engineprimitives.ForkchoiceStateV1{
HeadBlockHash: lph.GetBlockHash(),
SafeBlockHash: lph.GetParentHash(),
Expand Down
5 changes: 3 additions & 2 deletions mod/beacon/blockchain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sync"

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/primitives/pkg/common"
"github.com/berachain/beacon-kit/mod/primitives/pkg/transition"
Expand Down Expand Up @@ -58,7 +59,7 @@ type Service[
// cs holds the chain specifications.
cs common.ChainSpec
// ee is the execution engine responsible for processing execution payloads.
ee ExecutionEngine
ee ExecutionEngine[*engineprimitives.PayloadAttributes[*engineprimitives.Withdrawal]]
// lb is a local builder for constructing new beacon states.
lb LocalBuilder[BeaconStateT]
// bp is the blob processor for processing incoming blobs.
Expand Down Expand Up @@ -109,7 +110,7 @@ func NewService[
],
logger log.Logger[any],
cs common.ChainSpec,
ee ExecutionEngine,
ee ExecutionEngine[*engineprimitives.PayloadAttributes[*engineprimitives.Withdrawal]],
lb LocalBuilder[BeaconStateT],
bp BlobProcessor[
AvailabilityStoreT, BeaconBlockBodyT, BlobSidecarsT, ExecutionPayloadT,
Expand Down
4 changes: 2 additions & 2 deletions mod/beacon/blockchain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ type BlobSidecars interface {
}

// ExecutionEngine is the interface for the execution engine.
type ExecutionEngine interface {
type ExecutionEngine[PayloadAttributesT any] interface {
// NotifyForkchoiceUpdate notifies the execution client of a forkchoice
// update.
NotifyForkchoiceUpdate(
ctx context.Context,
req *engineprimitives.ForkchoiceUpdateRequest,
req *engineprimitives.ForkchoiceUpdateRequest[PayloadAttributesT],
) (*engineprimitives.PayloadID, *common.ExecutionHash, error)
}

Expand Down
21 changes: 21 additions & 0 deletions mod/engine-primitives/pkg/engine-primitives/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ func NewPayloadAttributes[
return p, nil
}

// New empty PayloadAttributes.
func (p *PayloadAttributes[WithdrawalT]) New(
forkVersion uint32,
timestamp uint64,
prevRandao common.Bytes32,
suggestedFeeRecipient common.ExecutionAddress,
withdrawals []WithdrawalT,
parentBeaconBlockRoot common.Root,
) (*PayloadAttributes[WithdrawalT], error) {
var err error
p, err = NewPayloadAttributes(
forkVersion,
timestamp,
prevRandao,
suggestedFeeRecipient,
withdrawals,
parentBeaconBlockRoot,
)
return p, err
}

// IsNil returns true if the PayloadAttributes is nil.
func (p *PayloadAttributes[WithdrawalT]) IsNil() bool {
return p == nil
Expand Down
12 changes: 6 additions & 6 deletions mod/engine-primitives/pkg/engine-primitives/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,23 +228,23 @@ func (n *NewPayloadRequest[ExecutionPayloadT, WithdrawalT]) HasValidVersionedAnd
return nil
}

type ForkchoiceUpdateRequest struct {
type ForkchoiceUpdateRequest[PayloadAttributesT any] struct {
// State is the forkchoice state.
State *ForkchoiceStateV1
// PayloadAttributes is the payload attributer.
PayloadAttributes PayloadAttributer
PayloadAttributes PayloadAttributesT
// ForkVersion is the fork version that we
// are going to be submitting for.
ForkVersion uint32
}

// BuildForkchoiceUpdateRequest builds a forkchoice update request.
func BuildForkchoiceUpdateRequest(
func BuildForkchoiceUpdateRequest[PayloadAttributesT PayloadAttributer](
state *ForkchoiceStateV1,
payloadAttributes PayloadAttributer,
payloadAttributes PayloadAttributesT,
forkVersion uint32,
) *ForkchoiceUpdateRequest {
return &ForkchoiceUpdateRequest{
) *ForkchoiceUpdateRequest[PayloadAttributesT] {
return &ForkchoiceUpdateRequest[PayloadAttributesT]{
State: state,
PayloadAttributes: payloadAttributes,
ForkVersion: forkVersion,
Expand Down
4 changes: 2 additions & 2 deletions mod/execution/pkg/client/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

// jwtRefreshLoop refreshes the JWT token for the execution client.
func (s *EngineClient[ExecutionPayloadT]) jwtRefreshLoop(
func (s *EngineClient[ExecutionPayloadT, PayloadAttributesT]) jwtRefreshLoop(
ctx context.Context,
) {
s.logger.Info("Starting JWT refresh loop 🔄")
Expand Down Expand Up @@ -55,7 +55,7 @@ func (s *EngineClient[ExecutionPayloadT]) jwtRefreshLoop(
// attached for authorization.
//
//nolint:lll
func (s *EngineClient[ExecutionPayloadT]) buildJWTHeader() (http.Header, error) {
func (s *EngineClient[ExecutionPayloadT, PayloadAttributesT]) buildJWTHeader() (http.Header, error) {
header := make(http.Header)

// Build the JWT token.
Expand Down
29 changes: 17 additions & 12 deletions mod/execution/pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"strings"
"time"

engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives"
"github.com/berachain/beacon-kit/mod/errors"
"github.com/berachain/beacon-kit/mod/execution/pkg/client/cache"
"github.com/berachain/beacon-kit/mod/execution/pkg/client/ethclient"
Expand All @@ -44,6 +45,7 @@ type EngineClient[
json.Marshaler
json.Unmarshaler
},
PayloadAttributesT engineprimitives.PayloadAttributer,
] struct {
// Eth1Client is a struct that holds the Ethereum 1 client and
// its configuration.
Expand All @@ -68,19 +70,22 @@ type EngineClient[
// New creates a new engine client EngineClient.
// It takes an Eth1Client as an argument and returns a pointer to an
// EngineClient.
func New[ExecutionPayloadT interface {
Empty(uint32) ExecutionPayloadT
Version() uint32
json.Marshaler
json.Unmarshaler
}](
func New[
ExecutionPayloadT interface {
Empty(uint32) ExecutionPayloadT
Version() uint32
json.Marshaler
json.Unmarshaler
},
PayloadAttributesT engineprimitives.PayloadAttributer,
](
cfg *Config,
logger log.Logger[any],
jwtSecret *jwt.Secret,
telemetrySink TelemetrySink,
eth1ChainID *big.Int,
) *EngineClient[ExecutionPayloadT] {
return &EngineClient[ExecutionPayloadT]{
) *EngineClient[ExecutionPayloadT, PayloadAttributesT] {
return &EngineClient[ExecutionPayloadT, PayloadAttributesT]{
cfg: cfg,
logger: logger,
jwtSecret: jwtSecret,
Expand All @@ -93,12 +98,12 @@ func New[ExecutionPayloadT interface {
}

// Name returns the name of the engine client.
func (s *EngineClient[ExecutionPayloadT]) Name() string {
func (s *EngineClient[ExecutionPayloadT, PayloadAttributesT]) Name() string {
return "engine-client"
}

// Start the engine client.
func (s *EngineClient[ExecutionPayloadT]) Start(
func (s *EngineClient[ExecutionPayloadT, PayloadAttributesT]) Start(
ctx context.Context,
) error {
if s.cfg.RPCDialURL.IsHTTP() || s.cfg.RPCDialURL.IsHTTPS() {
Expand Down Expand Up @@ -152,7 +157,7 @@ func (s *EngineClient[ExecutionPayloadT]) Start(

// setupConnection dials the execution client and
// ensures the chain ID is correct.
func (s *EngineClient[ExecutionPayloadT]) initializeConnection(
func (s *EngineClient[ExecutionPayloadT, PayloadAttributesT]) initializeConnection(
ctx context.Context,
) error {
var (
Expand Down Expand Up @@ -215,7 +220,7 @@ func (s *EngineClient[ExecutionPayloadT]) initializeConnection(
/* -------------------------------------------------------------------------- */

// dialExecutionRPCClient dials the execution client's RPC endpoint.
func (s *EngineClient[ExecutionPayloadT]) dialExecutionRPCClient(
func (s *EngineClient[ExecutionPayloadT, PayloadAttributesT]) dialExecutionRPCClient(
ctx context.Context,
) error {
var (
Expand Down
8 changes: 4 additions & 4 deletions mod/execution/pkg/client/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
/* -------------------------------------------------------------------------- */

// NewPayload calls the engine_newPayloadVX method via JSON-RPC.
func (s *EngineClient[ExecutionPayloadT]) NewPayload(
func (s *EngineClient[ExecutionPayloadT, PayloadAttributesT]) NewPayload(
ctx context.Context,
payload ExecutionPayloadT,
versionedHashes []common.ExecutionHash,
Expand Down Expand Up @@ -81,7 +81,7 @@ func (s *EngineClient[ExecutionPayloadT]) NewPayload(
/* -------------------------------------------------------------------------- */

// ForkchoiceUpdated calls the engine_forkchoiceUpdatedV1 method via JSON-RPC.
func (s *EngineClient[ExecutionPayloadT]) ForkchoiceUpdated(
func (s *EngineClient[ExecutionPayloadT, PayloadAttributesT]) ForkchoiceUpdated(
ctx context.Context,
state *engineprimitives.ForkchoiceStateV1,
attrs engineprimitives.PayloadAttributer,
Expand Down Expand Up @@ -130,7 +130,7 @@ func (s *EngineClient[ExecutionPayloadT]) ForkchoiceUpdated(

// GetPayload calls the engine_getPayloadVX method via JSON-RPC. It returns
// the execution data as well as the blobs bundle.
func (s *EngineClient[ExecutionPayloadT]) GetPayload(
func (s *EngineClient[ExecutionPayloadT, PayloadAttributesT]) GetPayload(
ctx context.Context,
payloadID engineprimitives.PayloadID,
forkVersion uint32,
Expand Down Expand Up @@ -161,7 +161,7 @@ func (s *EngineClient[ExecutionPayloadT]) GetPayload(

// ExchangeCapabilities calls the engine_exchangeCapabilities method via
// JSON-RPC.
func (s *EngineClient[ExecutionPayloadT]) ExchangeCapabilities(
func (s *EngineClient[ExecutionPayloadT, PayloadAttributesT]) ExchangeCapabilities(
ctx context.Context,
) ([]string, error) {
result, err := s.Eth1Client.ExchangeCapabilities(
Expand Down
2 changes: 1 addition & 1 deletion mod/execution/pkg/client/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var (
)

// Handles errors received from the RPC server according to the specification.
func (s *EngineClient[ExecutionPayloadT]) handleRPCError(err error) error {
func (s *EngineClient[ExecutionPayloadT, PayloadAttributesT]) handleRPCError(err error) error {
// Exit early if there is no error.
if err == nil {
return nil
Expand Down
4 changes: 2 additions & 2 deletions mod/execution/pkg/client/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

// HeaderByNumber retrieves the block header by its number.
func (s *EngineClient[ExecutionPayloadT]) HeaderByNumber(
func (s *EngineClient[ExecutionPayloadT, PayloadAttributesT]) HeaderByNumber(
ctx context.Context,
number *big.Int,
) (*engineprimitives.Header, error) {
Expand Down Expand Up @@ -59,7 +59,7 @@ func (s *EngineClient[ExecutionPayloadT]) HeaderByNumber(
}

// HeaderByHash retrieves the block header by its hash.
func (s *EngineClient[ExecutionPayloadT]) HeaderByHash(
func (s *EngineClient[ExecutionPayloadT, PayloadAttributesT]) HeaderByHash(
ctx context.Context,
hash common.ExecutionHash,
) (*engineprimitives.Header, error) {
Expand Down
2 changes: 1 addition & 1 deletion mod/execution/pkg/client/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (

// createContextWithTimeout creates a context with a timeout and returns it
// along with the cancel function.
func (s *EngineClient[ExecutionPayloadT]) createContextWithTimeout(
func (s *EngineClient[ExecutionPayloadT, PayloadAttributesT]) createContextWithTimeout(
ctx context.Context,
) (context.Context, context.CancelFunc) {
startTime := time.Now()
Expand Down
23 changes: 12 additions & 11 deletions mod/execution/pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ type Engine[
ExecutionPayloadT ExecutionPayload[
ExecutionPayloadT, *engineprimitives.Withdrawal,
],
PayloadAttributesT engineprimitives.PayloadAttributer,
PayloadIDT ~[8]byte,
] struct {
// ec is the engine client that the engine will use to
// interact with the execution layer.
ec *client.EngineClient[ExecutionPayloadT]
ec *client.EngineClient[ExecutionPayloadT, PayloadAttributesT]
// logger is the logger for the engine.
logger log.Logger[any]
// metrics is the metrics for the engine.
Expand All @@ -62,17 +63,18 @@ func New[
ExecutionPayloadT ExecutionPayload[
ExecutionPayloadT, *engineprimitives.Withdrawal,
],
PayloadAttributesT engineprimitives.PayloadAttributer,
PayloadIDT ~[8]byte,
](
ec *client.EngineClient[ExecutionPayloadT],
ec *client.EngineClient[ExecutionPayloadT, PayloadAttributesT],
logger log.Logger[any],
statusFeed *event.FeedOf[
asynctypes.EventID,
*asynctypes.Event[*service.StatusEvent],
],
telemtrySink TelemetrySink,
) *Engine[ExecutionPayloadT, PayloadIDT] {
return &Engine[ExecutionPayloadT, PayloadIDT]{
) *Engine[ExecutionPayloadT, PayloadAttributesT, PayloadIDT] {
return &Engine[ExecutionPayloadT, PayloadAttributesT, PayloadIDT]{
ec: ec,
logger: logger,
metrics: newEngineMetrics(telemtrySink, logger),
Expand All @@ -81,7 +83,7 @@ func New[
}

// Start spawns any goroutines required by the service.
func (ee *Engine[ExecutionPayloadT, PayloadIDT]) Start(
func (ee *Engine[ExecutionPayloadT, PayloadAttributesT, PayloadIDT]) Start(
ctx context.Context,
) error {
go func() {
Expand All @@ -94,7 +96,7 @@ func (ee *Engine[ExecutionPayloadT, PayloadIDT]) Start(
}

// GetPayload returns the payload and blobs bundle for the given slot.
func (ee *Engine[ExecutionPayloadT, PayloadIDT]) GetPayload(
func (ee *Engine[ExecutionPayloadT, PayloadAttributesT, PayloadIDT]) GetPayload(
ctx context.Context,
req *engineprimitives.GetPayloadRequest[engineprimitives.PayloadID],
) (engineprimitives.BuiltExecutionPayloadEnv[ExecutionPayloadT], error) {
Expand All @@ -105,13 +107,12 @@ func (ee *Engine[ExecutionPayloadT, PayloadIDT]) GetPayload(
}

// NotifyForkchoiceUpdate notifies the execution client of a forkchoice update.
func (ee *Engine[ExecutionPayloadT, PayloadIDT]) NotifyForkchoiceUpdate(
func (ee *Engine[ExecutionPayloadT, PayloadAttributesT, PayloadIDT]) NotifyForkchoiceUpdate(
ctx context.Context,
req *engineprimitives.ForkchoiceUpdateRequest,
req *engineprimitives.ForkchoiceUpdateRequest[PayloadAttributesT],
) (*engineprimitives.PayloadID, *common.ExecutionHash, error) {
// Log the forkchoice update attempt.
hasPayloadAttributes := req.PayloadAttributes != nil &&
!req.PayloadAttributes.IsNil()
hasPayloadAttributes := !req.PayloadAttributes.IsNil()
ee.metrics.markNotifyForkchoiceUpdateCalled(
req.State, hasPayloadAttributes,
)
Expand Down Expand Up @@ -176,7 +177,7 @@ func (ee *Engine[ExecutionPayloadT, PayloadIDT]) NotifyForkchoiceUpdate(

// VerifyAndNotifyNewPayload verifies the new payload and notifies the
// execution client.
func (ee *Engine[ExecutionPayloadT, PayloadIDT]) VerifyAndNotifyNewPayload(
func (ee *Engine[ExecutionPayloadT, PayloadAttributesT, PayloadIDT]) VerifyAndNotifyNewPayload(
ctx context.Context,
req *engineprimitives.NewPayloadRequest[
ExecutionPayloadT, *engineprimitives.Withdrawal],
Expand Down
16 changes: 14 additions & 2 deletions mod/node-core/pkg/components/attributes_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package components

import (
"github.com/berachain/beacon-kit/mod/config"
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/payload/pkg/attributes"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
Expand All @@ -30,13 +31,24 @@ import (
// ProvideAttributesFactory provides an AttributesFactory for the client.
func ProvideAttributesFactory[
BeaconStateT attributes.BeaconState[WithdrawalT],
PayloadAttributesT interface {
engineprimitives.PayloadAttributer
New(
uint32,
uint64,
common.Bytes32,
common.ExecutionAddress,
[]WithdrawalT,
common.Root,
) (PayloadAttributesT, error)
},
WithdrawalT any,
](
chainSpec common.ChainSpec,
logger log.Logger[any],
cfg *config.Config,
) (*attributes.Factory[BeaconStateT, WithdrawalT], error) {
return attributes.NewAttributesFactory[BeaconStateT, WithdrawalT](
) (*attributes.Factory[BeaconStateT, PayloadAttributesT, WithdrawalT], error) {
return attributes.NewAttributesFactory[BeaconStateT, PayloadAttributesT, WithdrawalT](
chainSpec,
logger,
cfg.PayloadBuilder.SuggestedFeeRecipient,
Expand Down
Loading

0 comments on commit efb931e

Please sign in to comment.