Skip to content

Commit

Permalink
feat: optimize GetPayload method, replace NewPayload with NewPayloadById
Browse files Browse the repository at this point in the history
  • Loading branch information
SilasZhr committed Oct 20, 2024
1 parent 0220aed commit 41c8e77
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 23 deletions.
21 changes: 11 additions & 10 deletions op-node/rollup/engine/build_seal.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,17 @@ func (eq *EngDeriver) onBuildSeal(ev BuildSealEvent) {
return
}

if err := sanityCheckPayload(envelope.ExecutionPayload); err != nil {
eq.emitter.Emit(PayloadSealInvalidEvent{
Info: ev.Info,
Err: fmt.Errorf("failed sanity-check of execution payload contents (ID: %s, blockhash: %s): %w",
ev.Info.ID, envelope.ExecutionPayload.BlockHash, err),
IsLastInSpan: ev.IsLastInSpan,
DerivedFrom: ev.DerivedFrom,
})
return
}
//Temporarily bypass sanity check to avoid processing all txns
// if err := sanityCheckPayload(envelope.ExecutionPayload); err != nil {
// eq.emitter.Emit(PayloadSealInvalidEvent{
// Info: ev.Info,
// Err: fmt.Errorf("failed sanity-check of execution payload contents (ID: %s, blockhash: %s): %w",
// ev.Info.ID, envelope.ExecutionPayload.BlockHash, err),
// IsLastInSpan: ev.IsLastInSpan,
// DerivedFrom: ev.DerivedFrom,
// })
// return
// }

ref, err := derive.PayloadToBlockRef(eq.cfg, envelope.ExecutionPayload)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions op-node/rollup/engine/build_sealed.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (eq *EngDeriver) onBuildSealed(ev BuildSealedEvent) {
eq.emitter.Emit(PayloadProcessEvent{
IsLastInSpan: ev.IsLastInSpan,
DerivedFrom: ev.DerivedFrom,
Info: ev.Info,
Envelope: ev.Envelope,
Ref: ev.Ref,
})
Expand Down
1 change: 1 addition & 0 deletions op-node/rollup/engine/engine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type ExecEngine interface {
GetPayload(ctx context.Context, payloadInfo eth.PayloadInfo) (*eth.ExecutionPayloadEnvelope, error)
ForkchoiceUpdate(ctx context.Context, state *eth.ForkchoiceState, attr *eth.PayloadAttributes) (*eth.ForkchoiceUpdatedResult, error)
NewPayload(ctx context.Context, payload *eth.ExecutionPayload, parentBeaconBlockRoot *common.Hash) (*eth.PayloadStatusV1, error)
NewPayloadWithPayloadId(ctx context.Context, payloadInfo eth.PayloadInfo, parentBeaconBlockRoot *common.Hash) (*eth.PayloadStatusV1, error)
L2BlockRefByLabel(ctx context.Context, label eth.BlockLabel) (eth.L2BlockRef, error)
}

Expand Down
8 changes: 5 additions & 3 deletions op-node/rollup/engine/payload_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type PayloadProcessEvent struct {
// payload is promoted to pending-safe if non-zero
DerivedFrom eth.L1BlockRef

Info eth.PayloadInfo

Envelope *eth.ExecutionPayloadEnvelope
Ref eth.L2BlockRef
}
Expand All @@ -25,9 +27,9 @@ func (ev PayloadProcessEvent) String() string {
func (eq *EngDeriver) onPayloadProcess(ev PayloadProcessEvent) {
ctx, cancel := context.WithTimeout(eq.ctx, payloadProcessTimeout)
defer cancel()

status, err := eq.ec.engine.NewPayload(ctx,
ev.Envelope.ExecutionPayload, ev.Envelope.ParentBeaconBlockRoot)
eq.log.Info("payload-process, NewPayloadWithPayloadId, payload info:", ev.Info)
status, err := eq.ec.engine.NewPayloadWithPayloadId(ctx,
ev.Info, ev.Envelope.ParentBeaconBlockRoot)
if err != nil {
eq.emitter.Emit(rollup.EngineTemporaryErrorEvent{
Err: fmt.Errorf("failed to insert execution payload: %w", err)})
Expand Down
2 changes: 2 additions & 0 deletions op-node/rollup/engine/payload_success.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type PayloadSuccessEvent struct {
// payload is promoted to pending-safe if non-zero
DerivedFrom eth.L1BlockRef

Info eth.PayloadInfo

Envelope *eth.ExecutionPayloadEnvelope
Ref eth.L2BlockRef
}
Expand Down
21 changes: 12 additions & 9 deletions op-node/rollup/sequencing/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,23 +262,26 @@ func (d *Sequencer) onBuildSealed(x engine.BuildSealedEvent) {
"txs", len(x.Envelope.ExecutionPayload.Transactions),
"time", uint64(x.Envelope.ExecutionPayload.Timestamp))

//TODO(silas): CommitUnsafePayload
// generous timeout, the conductor is important
ctx, cancel := context.WithTimeout(d.ctx, time.Second*30)
defer cancel()
if err := d.conductor.CommitUnsafePayload(ctx, x.Envelope); err != nil {
d.emitter.Emit(rollup.EngineTemporaryErrorEvent{
Err: fmt.Errorf("failed to commit unsafe payload to conductor: %w", err)})
return
}

// ctx, cancel := context.WithTimeout(d.ctx, time.Second*30)
// defer cancel()
// if err := d.conductor.CommitUnsafePayload(ctx, x.Envelope); err != nil {
// d.emitter.Emit(rollup.EngineTemporaryErrorEvent{
// Err: fmt.Errorf("failed to commit unsafe payload to conductor: %w", err)})
// return
// }

//TODO(silas): start gossip
// begin gossiping as soon as possible
// asyncGossip.Clear() will be called later if an non-temporary error is found,
// or if the payload is successfully inserted
d.asyncGossip.Gossip(x.Envelope)
//d.asyncGossip.Gossip(x.Envelope)
// Now after having gossiped the block, try to put it in our own canonical chain
d.emitter.Emit(engine.PayloadProcessEvent{
IsLastInSpan: x.IsLastInSpan,
DerivedFrom: x.DerivedFrom,
Info: x.Info,
Envelope: x.Envelope,
Ref: x.Ref,
})
Expand Down
2 changes: 2 additions & 0 deletions op-service/eth/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ const (
NewPayloadV2 EngineAPIMethod = "engine_newPayloadV2"
NewPayloadV3 EngineAPIMethod = "engine_newPayloadV3"

NewPayloadV3ById EngineAPIMethod = "engine_newPayloadV3ById"

GetPayloadV2 EngineAPIMethod = "engine_getPayloadV2"
GetPayloadV3 EngineAPIMethod = "engine_getPayloadV3"
)
23 changes: 22 additions & 1 deletion op-service/sources/engine_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,27 @@ func (s *EngineAPIClient) NewPayload(ctx context.Context, payload *eth.Execution
return &result, nil
}

// NewPayload executes a full block on the execution engine.
// This returns a PayloadStatusV1 which encodes any validation/processing error,
// and this type of error is kept separate from the returned `error` used for RPC errors, like timeouts.
func (s *EngineAPIClient) NewPayloadWithPayloadId(ctx context.Context, payloadInfo eth.PayloadInfo, parentBeaconBlockRoot *common.Hash) (*eth.PayloadStatusV1, error) {
e := s.log.New("engine_newPayloadV3ById, payload_id:", payloadInfo.ID)
e.Trace("sending payload id for execution")

execCtx, cancel := context.WithTimeout(ctx, time.Second*5)
defer cancel()
var result eth.PayloadStatusV1

var err = s.RPC.CallContext(execCtx, &result, string(eth.NewPayloadV3ById), payloadInfo.ID, []common.Hash{}, parentBeaconBlockRoot)

e.Trace("Received payload execution result", "status", result.Status, "latestValidHash", result.LatestValidHash, "message", result.ValidationError)
if err != nil {
e.Error("Payload execution failed", "err", err)
return nil, fmt.Errorf("failed to execute payload: %w", err)
}
return &result, nil
}

// GetPayload gets the execution payload associated with the PayloadId.
// There may be two types of error:
// 1. `error` as eth.InputError: the payload ID may be unknown
Expand All @@ -168,7 +189,7 @@ func (s *EngineAPIClient) GetPayload(ctx context.Context, payloadInfo eth.Payloa
}
return nil, err
}
e.Trace("Received payload")
e.Trace("Received payload", string(method), *result.ExecutionPayload)
return &result, nil
}

Expand Down

0 comments on commit 41c8e77

Please sign in to comment.