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

feat(types): Modular PayloadID #1550

Merged
merged 2 commits into from
Jun 20, 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
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 @@ -253,20 +253,20 @@ func BuildForkchoiceUpdateRequest(
}

// GetPayloadRequest represents a request to get a payload.
type GetPayloadRequest struct {
type GetPayloadRequest[PayloadIDT ~[8]byte] struct {
// PayloadID is the payload ID.
PayloadID PayloadID
PayloadID PayloadIDT
// ForkVersion is the fork version that we are
// currently on.
ForkVersion uint32
}

// BuildGetPayloadRequest builds a get payload request.
func BuildGetPayloadRequest(
payloadID PayloadID,
func BuildGetPayloadRequest[PayloadIDT ~[8]byte](
payloadID PayloadIDT,
forkVersion uint32,
) *GetPayloadRequest {
return &GetPayloadRequest{
) *GetPayloadRequest[PayloadIDT] {
return &GetPayloadRequest[PayloadIDT]{
PayloadID: payloadID,
ForkVersion: forkVersion,
}
Expand Down
16 changes: 9 additions & 7 deletions mod/execution/pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Engine[
ExecutionPayloadT ExecutionPayload[
ExecutionPayloadT, *engineprimitives.Withdrawal,
],
PayloadIDT ~[8]byte,
] struct {
// ec is the engine client that the engine will use to
// interact with the execution layer.
Expand All @@ -61,6 +62,7 @@ func New[
ExecutionPayloadT ExecutionPayload[
ExecutionPayloadT, *engineprimitives.Withdrawal,
],
PayloadIDT ~[8]byte,
](
ec *client.EngineClient[ExecutionPayloadT],
logger log.Logger[any],
Expand All @@ -69,8 +71,8 @@ func New[
*asynctypes.Event[*service.StatusEvent],
],
telemtrySink TelemetrySink,
) *Engine[ExecutionPayloadT] {
return &Engine[ExecutionPayloadT]{
) *Engine[ExecutionPayloadT, PayloadIDT] {
return &Engine[ExecutionPayloadT, PayloadIDT]{
ec: ec,
logger: logger,
metrics: newEngineMetrics(telemtrySink, logger),
Expand All @@ -79,7 +81,7 @@ func New[
}

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

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

// NotifyForkchoiceUpdate notifies the execution client of a forkchoice update.
func (ee *Engine[ExecutionPayloadT]) NotifyForkchoiceUpdate(
func (ee *Engine[ExecutionPayloadT, PayloadIDT]) NotifyForkchoiceUpdate(
ctx context.Context,
req *engineprimitives.ForkchoiceUpdateRequest,
) (*engineprimitives.PayloadID, *common.ExecutionHash, error) {
Expand Down Expand Up @@ -174,7 +176,7 @@ func (ee *Engine[ExecutionPayloadT]) NotifyForkchoiceUpdate(

// VerifyAndNotifyNewPayload verifies the new payload and notifies the
// execution client.
func (ee *Engine[ExecutionPayloadT]) VerifyAndNotifyNewPayload(
func (ee *Engine[ExecutionPayloadT, PayloadIDT]) VerifyAndNotifyNewPayload(
ctx context.Context,
req *engineprimitives.NewPayloadRequest[
ExecutionPayloadT, *engineprimitives.Withdrawal],
Expand Down
4 changes: 1 addition & 3 deletions mod/log/pkg/phuslu/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,9 @@ func (f *Formatter) Format(
f.ensureLineBreak(buffer)

if args.Stack != "" {
buffer.Bytes = append(buffer.Bytes, args.Stack...)
if args.Stack[len(args.Stack)-1] != '\n' {
buffer.Bytes = append(buffer.Bytes, args.Stack...)
buffer.Bytes = append(buffer.Bytes, '\n')
} else {
buffer.Bytes = append(buffer.Bytes, args.Stack...)
}
}

Expand Down
7 changes: 5 additions & 2 deletions mod/log/pkg/phuslu/phuslu.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,11 @@ func (l Logger[ImplT]) With(keyVals ...any) ImplT {

// Add the new context to the existing context.
for i := 0; i < len(keyVals); i += 2 {
key, val := keyVals[i].(string), keyVals[i+1]
newLogger.context[key] = val
key, ok := keyVals[i].(string)
if !ok {
panic("context key must be a string")
}
newLogger.context[key] = keyVals[i+1]
}

return any(&newLogger).(ImplT)
Expand Down
3 changes: 2 additions & 1 deletion mod/node-core/pkg/components/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"github.com/berachain/beacon-kit/mod/config"
engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives"
engineclient "github.com/berachain/beacon-kit/mod/execution/pkg/client"
execution "github.com/berachain/beacon-kit/mod/execution/pkg/engine"
"github.com/berachain/beacon-kit/mod/interfaces"
Expand Down Expand Up @@ -88,7 +89,7 @@ func ProvideExecutionEngine[
](
in ExecutionEngineInput,
) *ExecutionEngine {
return execution.New[*ExecutionPayload](
return execution.New[*ExecutionPayload, engineprimitives.PayloadID](
itsdevbear marked this conversation as resolved.
Show resolved Hide resolved
in.EngineClient,
in.Logger.With("service", "execution-engine"),
in.StatusFeed,
Expand Down
7 changes: 5 additions & 2 deletions mod/node-core/pkg/components/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ type (
EngineClient = engineclient.EngineClient[*ExecutionPayload]

// EngineClient is a type alias for the engine client.
ExecutionEngine = execution.Engine[*ExecutionPayload]
ExecutionEngine = execution.Engine[
*ExecutionPayload, engineprimitives.PayloadID,
]

// ExecutionPayload type aliases.
ExecutionPayload = types.ExecutionPayload
Expand All @@ -161,7 +163,8 @@ type (

// LocalBuilder is a type alias for the local builder.
LocalBuilder = payloadbuilder.PayloadBuilder[
BeaconState, *ExecutionPayload, *ExecutionPayloadHeader,
BeaconState, *ExecutionPayload,
*ExecutionPayloadHeader, engineprimitives.PayloadID,
]

// StateProcessor is the type alias for the state processor inteface.
Expand Down
2 changes: 1 addition & 1 deletion mod/payload/pkg/builder/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
// slot. The attribute is required to initiate a payload build process in the
// context of an `engine_forkchoiceUpdated` call.
func (pb *PayloadBuilder[
BeaconStateT, ExecutionPayloadT, ExecutionPayloadHeaderT,
BeaconStateT, ExecutionPayloadT, ExecutionPayloadHeaderT, PayloadIDT,
]) getPayloadAttribute(
st BeaconStateT,
slot math.Slot,
Expand Down
17 changes: 9 additions & 8 deletions mod/payload/pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package builder

import (
engineprimitves "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/cache"
"github.com/berachain/beacon-kit/mod/primitives"
Expand All @@ -44,6 +43,7 @@ type PayloadBuilder[
GetBlockHash() common.ExecutionHash
GetParentHash() common.ExecutionHash
},
PayloadIDT ~[8]byte,
] struct {
// cfg holds the configuration settings for the PayloadBuilder.
cfg *Config
Expand All @@ -52,12 +52,12 @@ type PayloadBuilder[
// logger is used for logging within the PayloadBuilder.
logger log.Logger[any]
// ee is the execution engine.
ee ExecutionEngine[ExecutionPayloadT]
ee ExecutionEngine[ExecutionPayloadT, PayloadIDT]
// pc is the payload ID cache, it is used to store
// "in-flight" payloads that are being built on
// the execution client.
pc *cache.PayloadIDCache[
engineprimitves.PayloadID, [32]byte, math.Slot,
PayloadIDT, [32]byte, math.Slot,
]
}

Expand All @@ -75,19 +75,20 @@ func New[
GetBlockHash() common.ExecutionHash
GetParentHash() common.ExecutionHash
},
PayloadIDT ~[8]byte,
](
cfg *Config,
chainSpec primitives.ChainSpec,
logger log.Logger[any],
ee ExecutionEngine[ExecutionPayloadT],
ee ExecutionEngine[ExecutionPayloadT, PayloadIDT],
pc *cache.PayloadIDCache[
engineprimitves.PayloadID, [32]byte, math.Slot,
PayloadIDT, [32]byte, math.Slot,
],
) *PayloadBuilder[
BeaconStateT, ExecutionPayloadT, ExecutionPayloadHeaderT,
BeaconStateT, ExecutionPayloadT, ExecutionPayloadHeaderT, PayloadIDT,
] {
return &PayloadBuilder[
BeaconStateT, ExecutionPayloadT, ExecutionPayloadHeaderT,
BeaconStateT, ExecutionPayloadT, ExecutionPayloadHeaderT, PayloadIDT,
]{
cfg: cfg,
chainSpec: chainSpec,
Expand All @@ -99,7 +100,7 @@ func New[

// Enabled returns true if the payload builder is enabled.
func (pb *PayloadBuilder[
BeaconStateT, ExecutionPayloadT, ExecutionPayloadHeaderT,
BeaconStateT, ExecutionPayloadT, ExecutionPayloadHeaderT, PayloadIDT,
]) Enabled() bool {
return pb.cfg.Enabled
}
16 changes: 8 additions & 8 deletions mod/payload/pkg/builder/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
// RequestPayloadAsync builds a payload for the given slot and
// returns the payload ID.
func (pb *PayloadBuilder[
BeaconStateT, ExecutionPayloadT, ExecutionPayloadHeaderT,
BeaconStateT, ExecutionPayloadT, ExecutionPayloadHeaderT, PayloadIDT,
]) RequestPayloadAsync(
ctx context.Context,
st BeaconStateT,
Expand All @@ -43,7 +43,7 @@ func (pb *PayloadBuilder[
parentBlockRoot primitives.Root,
headEth1BlockHash common.ExecutionHash,
finalEth1BlockHash common.ExecutionHash,
) (*engineprimitives.PayloadID, error) {
) (*PayloadIDT, error) {
if !pb.Enabled() {
return nil, ErrPayloadBuilderDisabled
}
Expand All @@ -66,7 +66,7 @@ func (pb *PayloadBuilder[
}

// Submit the forkchoice update to the execution client.
var payloadID *engineprimitives.PayloadID
var payloadID *PayloadIDT
payloadID, _, err = pb.ee.NotifyForkchoiceUpdate(
ctx, &engineprimitives.ForkchoiceUpdateRequest{
State: &engineprimitives.ForkchoiceStateV1{
Expand Down Expand Up @@ -105,7 +105,7 @@ func (pb *PayloadBuilder[
// RequestPayloadSync request a payload for the given slot and
// blocks until the payload is delivered.
func (pb *PayloadBuilder[
BeaconStateT, ExecutionPayloadT, ExecutionPayloadHeaderT,
BeaconStateT, ExecutionPayloadT, ExecutionPayloadHeaderT, PayloadIDT,
]) RequestPayloadSync(
ctx context.Context,
st BeaconStateT,
Expand Down Expand Up @@ -153,7 +153,7 @@ func (pb *PayloadBuilder[
// Get the payload from the execution client.
return pb.ee.GetPayload(
ctx,
&engineprimitives.GetPayloadRequest{
&engineprimitives.GetPayloadRequest[PayloadIDT]{
PayloadID: *payloadID,
ForkVersion: pb.chainSpec.ActiveForkVersionForSlot(slot),
},
Expand All @@ -165,7 +165,7 @@ func (pb *PayloadBuilder[
// retrieve a payload, it will build a new payload and wait for the
// execution client to return the payload.
func (pb *PayloadBuilder[
BeaconStateT, ExecutionPayloadT, ExecutionPayloadHeaderT,
BeaconStateT, ExecutionPayloadT, ExecutionPayloadHeaderT, PayloadIDT,
]) RetrievePayload(
ctx context.Context,
slot math.Slot,
Expand All @@ -184,7 +184,7 @@ func (pb *PayloadBuilder[

envelope, err := pb.ee.GetPayload(
ctx,
&engineprimitives.GetPayloadRequest{
&engineprimitives.GetPayloadRequest[PayloadIDT]{
PayloadID: payloadID,
ForkVersion: pb.chainSpec.ActiveForkVersionForSlot(slot),
},
Expand Down Expand Up @@ -235,7 +235,7 @@ func (pb *PayloadBuilder[
// TODO: This should be moved onto a "sync service"
// of some kind.
func (pb *PayloadBuilder[
BeaconStateT, ExecutionPayloadT, ExecutionPayloadHeaderT,
BeaconStateT, ExecutionPayloadT, ExecutionPayloadHeaderT, PayloadIDT,
]) SendForceHeadFCU(
ctx context.Context,
st BeaconStateT,
Expand Down
6 changes: 3 additions & 3 deletions mod/payload/pkg/builder/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ type BeaconState[ExecutionPayloadHeaderT interface {
}

// ExecutionEngine is the interface for the execution engine.
type ExecutionEngine[ExecutionPayloadT any] interface {
type ExecutionEngine[ExecutionPayloadT any, PayloadIDT ~[8]byte] interface {
// GetPayload returns the payload and blobs bundle for the given slot.
GetPayload(
ctx context.Context,
req *engineprimitives.GetPayloadRequest,
req *engineprimitives.GetPayloadRequest[PayloadIDT],
) (engineprimitives.BuiltExecutionPayloadEnv[ExecutionPayloadT], error)
// NotifyForkchoiceUpdate notifies the execution client of a forkchoice
// update.
NotifyForkchoiceUpdate(
ctx context.Context,
req *engineprimitives.ForkchoiceUpdateRequest,
) (*engineprimitives.PayloadID, *common.ExecutionHash, error)
) (*PayloadIDT, *common.ExecutionHash, error)
}
Loading