88 "github.com/ethereum-optimism/optimism/op-node/rollup"
99 "github.com/ethereum-optimism/optimism/op-node/rollup/derive"
1010 "github.com/ethereum-optimism/optimism/op-service/eth"
11+ "github.com/ethereum-optimism/optimism/op-service/event"
1112)
1213
1314type BuildStartEvent struct {
@@ -18,64 +19,70 @@ func (ev BuildStartEvent) String() string {
1819 return "build-start"
1920}
2021
21- func (eq * EngineController ) onBuildStart (ctx context.Context , ev BuildStartEvent ) {
22+ // startBuild contains the core logic for beginning a block build. It is used by
23+ // StartBuildAsync in both event-system (async) and non-event (sync) contexts.
24+ func (eq * EngineController ) startBuild (ctx context.Context , attrs * derive.AttributesWithParent ) error {
2225 rpcCtx , cancel := context .WithTimeout (eq .ctx , buildStartTimeout )
2326 defer cancel ()
2427
25- if ev . Attributes .DerivedFrom != (eth.L1BlockRef {}) &&
26- eq .PendingSafeL2Head ().Hash != ev . Attributes .Parent .Hash {
28+ if attrs .DerivedFrom != (eth.L1BlockRef {}) &&
29+ eq .PendingSafeL2Head ().Hash != attrs .Parent .Hash {
2730 // Warn about small reorgs, happens when pending safe head is getting rolled back
2831 eq .log .Warn ("block-attributes derived from L1 do not build on pending safe head, likely reorg" ,
29- "pending_safe" , eq .PendingSafeL2Head (), "attributes_parent" , ev . Attributes .Parent )
32+ "pending_safe" , eq .PendingSafeL2Head (), "attributes_parent" , attrs .Parent )
3033 }
3134
3235 fcEvent := ForkchoiceUpdateEvent {
33- UnsafeL2Head : ev . Attributes .Parent ,
36+ UnsafeL2Head : attrs .Parent ,
3437 SafeL2Head : eq .safeHead ,
3538 FinalizedL2Head : eq .finalizedHead ,
3639 }
3740 if fcEvent .UnsafeL2Head .Number < fcEvent .FinalizedL2Head .Number {
3841 err := fmt .Errorf ("invalid block-building pre-state, unsafe head %s is behind finalized head %s" , fcEvent .UnsafeL2Head , fcEvent .FinalizedL2Head )
3942 eq .emitter .Emit (ctx , rollup.CriticalErrorEvent {Err : err }) // make the node exit, things are very wrong.
40- return
43+ return err
4144 }
4245 fc := eth.ForkchoiceState {
4346 HeadBlockHash : fcEvent .UnsafeL2Head .Hash ,
4447 SafeBlockHash : fcEvent .SafeL2Head .Hash ,
4548 FinalizedBlockHash : fcEvent .FinalizedL2Head .Hash ,
4649 }
4750 buildStartTime := time .Now ()
48- id , errTyp , err := startPayload (rpcCtx , eq .engine , fc , ev . Attributes .Attributes )
51+ id , errTyp , err := startPayload (rpcCtx , eq .engine , fc , attrs .Attributes )
4952 if err != nil {
5053 switch errTyp {
5154 case BlockInsertTemporaryErr :
5255 // RPC errors are recoverable, we can retry the buffered payload attributes later.
5356 eq .emitter .Emit (ctx , rollup.EngineTemporaryErrorEvent {
5457 Err : fmt .Errorf ("temporarily cannot insert new safe block: %w" , err ),
5558 })
56- return
5759 case BlockInsertPrestateErr :
5860 eq .emitter .Emit (ctx , rollup.ResetEvent {
5961 Err : fmt .Errorf ("need reset to resolve pre-state problem: %w" , err ),
6062 })
61- return
6263 case BlockInsertPayloadErr :
63- eq .emitter .Emit (ctx , BuildInvalidEvent {Attributes : ev .Attributes , Err : err })
64- return
64+ eq .emitter .Emit (ctx , BuildInvalidEvent {Attributes : attrs , Err : err })
6565 default :
6666 eq .emitter .Emit (ctx , rollup.CriticalErrorEvent {
6767 Err : fmt .Errorf ("unknown error type %d: %w" , errTyp , err ),
6868 })
69- return
7069 }
70+ return err
7171 }
7272 eq .emitter .Emit (ctx , fcEvent )
7373
7474 eq .emitter .Emit (ctx , BuildStartedEvent {
75- Info : eth.PayloadInfo {ID : id , Timestamp : uint64 (ev . Attributes .Attributes .Timestamp )},
75+ Info : eth.PayloadInfo {ID : id , Timestamp : uint64 (attrs .Attributes .Timestamp )},
7676 BuildStarted : buildStartTime ,
77- Concluding : ev . Attributes .Concluding ,
78- DerivedFrom : ev . Attributes .DerivedFrom ,
79- Parent : ev . Attributes .Parent ,
77+ Concluding : attrs .Concluding ,
78+ DerivedFrom : attrs .DerivedFrom ,
79+ Parent : attrs .Parent ,
8080 })
81+ return nil
82+ }
83+
84+ func (eq * EngineController ) StartBuildAsync (ctx context.Context , attrs * derive.AttributesWithParent ) event.Promise0 [error ] {
85+ return event .Spawn0 (ctx , func (ctx context.Context ) error {
86+ return eq .startBuild (ctx , attrs )
87+ }, event .WithSpawnLegacyEvent (BuildStartEvent {Attributes : attrs }))
8188}
0 commit comments