@@ -34,12 +34,13 @@ proc addResolvedHeadBlock(
3434       trustedBlock: ForkyTrustedSignedBeaconBlock ,
3535       optimisticStatus: OptimisticStatus ,
3636       parent: BlockRef , cache: var  StateCache ,
37-        onBlockAdded: OnForkyBlockAdded ,
37+        onBlockAdded: OnBlockAdded ,
3838       stateDataDur, sigVerifyDur, stateVerifyDur: Duration 
3939     ): BlockRef  = 
4040  doAssert  state.matches_block_slot (
4141    trustedBlock.root, trustedBlock.message.slot),
4242    " Given state must have the new block applied" 
43+   const  consensusFork =  typeof (trustedBlock).kind
4344
4445  let 
4546    blockRoot =  trustedBlock.root
@@ -100,12 +101,15 @@ proc addResolvedHeadBlock(
100101  #  Notify others of the new block before processing the quarantine, such that
101102  #  notifications for parents happens before those of the children
102103  if  onBlockAdded !=  nil :
103-     let  unrealized =   withState (state): 
104+     let  unrealized = 
104105      when  consensusFork >=  ConsensusFork .Altair :
105-         forkyState .data.compute_unrealized_finality ()
106+         state. forky (consensusFork) .data.compute_unrealized_finality ()
106107      else :
107-         forkyState.data.compute_unrealized_finality (cache)
108-     onBlockAdded (blockRef, trustedBlock, epochRef, unrealized)
108+         state.forky (consensusFork).data.compute_unrealized_finality (cache)
109+     onBlockAdded (
110+       blockRef, trustedBlock, state.forky (consensusFork).data, epochRef, unrealized
111+     )
112+ 
109113  if  not (isNil (dag.onBlockAdded)):
110114    dag.onBlockAdded (ForkedTrustedSignedBeaconBlock .init (trustedBlock))
111115
@@ -135,29 +139,6 @@ proc checkStateTransition(
135139  else :
136140    ok ()
137141
138- proc  advanceClearanceState * (dag: ChainDAGRef , nextSlot: Slot ) = 
139-   #  When the chain is synced, the most likely block to be produced is the block
140-   #  right after head - we can exploit this assumption and advance the state
141-   #  to that slot before the block arrives, thus allowing us to do the expensive
142-   #  epoch transition ahead of time.
143-   #  Notably, we use the clearance state here because that's where the block will
144-   #  first be seen - later, this state will be copied to the head state!
145-   let  head =  dag.head
146-   if  dag.clearanceState.matches_block_slot (head.root, nextSlot):
147-     return 
148- 
149-   let  startTick =  Moment .now ()
150-   var  cache =  StateCache  ()
151-   if  dag.updateState (
152-     dag.clearanceState,
153-     BlockSlotId .init (head.bid, nextSlot),
154-     true ,
155-     cache,
156-     dag.updateFlags,
157-   ):
158-     debug  " Prepared clearance state for next block"  ,
159-       nextSlot, head, updateStateDur =  Moment .now () -  startTick
160- 
161142proc  checkHeadBlock * (
162143    dag: ChainDAGRef , signedBlock: ForkySignedBeaconBlock ):
163144    Result [BlockRef , VerifierError ] = 
@@ -228,7 +209,7 @@ proc checkHeadBlock*(
228209proc  addHeadBlockWithParent * (
229210    dag: ChainDAGRef , verifier: var  BatchVerifier ,
230211    signedBlock: ForkySignedBeaconBlock , parent: BlockRef ,
231-     optimisticStatus: OptimisticStatus , onBlockAdded: OnForkyBlockAdded 
212+     optimisticStatus: OptimisticStatus , onBlockAdded: OnBlockAdded 
232213    ): Result [BlockRef , VerifierError ] = 
233214  # # Try adding a block to the chain, verifying first that it passes the state
234215  # # transition function and contains correct cryptographic signature.
@@ -347,8 +328,7 @@ proc addBackfillBlock*(
347328          info  " Invalid genesis block signature" 
348329          return  err (VerifierError .Invalid )
349330      else :
350-         let  proposerKey =  dag.validatorKey (blck.proposer_index)
351-         if  proposerKey.isNone ():
331+         let  proposerKey =  dag.validatorKey (blck.proposer_index).valueOr:
352332          #  We've verified that the block root matches our expectations by following
353333          #  the chain of parents all the way from checkpoint. If all those blocks
354334          #  were valid, the proposer_index in this block must also be valid, and we
@@ -365,7 +345,7 @@ proc addBackfillBlock*(
365345            getStateField (dag.headState, genesis_validators_root),
366346            blck.slot,
367347            signedBlock.root,
368-             proposerKey. get () ,
348+             proposerKey,
369349            signedBlock.signature):
370350          info  " Block signature verification failed" 
371351          return  err (VerifierError .Invalid )
@@ -455,26 +435,6 @@ proc addBackfillBlock*(
455435
456436  ok ()
457437
458- template  BlockAdded (kind: static   ConsensusFork ): untyped  = 
459-   when  kind ==  ConsensusFork .Gloas :
460-     OnGloasBlockAdded 
461-   elif  kind ==  ConsensusFork .Fulu :
462-     OnFuluBlockAdded 
463-   elif  kind ==  ConsensusFork .Electra :
464-     OnElectraBlockAdded 
465-   elif  kind ==  ConsensusFork .Deneb :
466-     OnDenebBlockAdded 
467-   elif  kind ==  ConsensusFork .Capella :
468-     OnCapellaBlockAdded 
469-   elif  kind ==  ConsensusFork .Bellatrix :
470-     OnBellatrixBlockAdded 
471-   elif  kind ==  ConsensusFork .Altair :
472-     OnAltairBlockAdded 
473-   elif  kind ==  ConsensusFork .Phase0 :
474-     OnPhase0BlockAdded 
475-   else :
476-     static : raiseAssert  " Unreachable" 
477- 
478438proc  verifyBlockProposer * (
479439    verifier: var  BatchVerifier ,
480440    fork: Fork ,
@@ -494,73 +454,57 @@ proc verifyBlockProposer*(
494454
495455proc  addBackfillBlockData * (
496456    dag: ChainDAGRef ,
457+     consensusFork: static   ConsensusFork ,
497458    bdata: BlockData ,
498459    onStateUpdated: OnStateUpdated ,
499-     onBlockAdded: OnForkedBlockAdded 
460+     onBlockAdded: OnBlockAdded , 
500461): Result [void , VerifierError ] = 
501462  var  cache =  StateCache  ()
463+   template  forkyBlck : untyped  =  bdata.blck.forky (consensusFork)
464+   let 
465+     parent =  checkHeadBlock (dag, forkyBlck).valueOr:
466+       if  error ==  VerifierError .Duplicate :
467+         return  ok ()
468+       return  err (error)
469+     startTick =  Moment .now ()
470+     clearanceBlock =  BlockSlotId .init (parent.bid, forkyBlck.message.slot)
471+     updateFlags1 =  dag.updateFlags
472+       #  TODO  (cheatfate): {skipLastStateRootCalculation} flag here could
473+       #  improve performance by 100%, but this approach needs some
474+       #  improvements, which is unclear.
475+ 
476+   if  not  updateState (dag, dag.clearanceState, clearanceBlock, true , cache,
477+                       updateFlags1):
478+     error  " Unable to load clearance state for parent block, "   & 
479+           " database corrupt?"  , clearanceBlock =  shortLog (clearanceBlock)
480+     return  err (VerifierError .MissingParent )
502481
503-   withBlck (bdata.blck):
504-     let 
505-       parent =  checkHeadBlock (dag, forkyBlck).valueOr:
506-         if  error ==  VerifierError .Duplicate :
507-           return  ok ()
508-         return  err (error)
509-       startTick =  Moment .now ()
510-       clearanceBlock =  BlockSlotId .init (parent.bid, forkyBlck.message.slot)
511-       updateFlags1 =  dag.updateFlags
512-         #  TODO  (cheatfate): {skipLastStateRootCalculation} flag here could
513-         #  improve performance by 100%, but this approach needs some
514-         #  improvements, which is unclear.
515- 
516-     if  not  updateState (dag, dag.clearanceState, clearanceBlock, true , cache,
517-                        updateFlags1):
518-       error  " Unable to load clearance state for parent block, "   & 
519-             " database corrupt?"  , clearanceBlock =  shortLog (clearanceBlock)
520-       return  err (VerifierError .MissingParent )
482+   let  proposerVerifyTick =  Moment .now ()
521483
522-     let  proposerVerifyTick =  Moment .now ()
523- 
524-     if  not (isNil (onStateUpdated)):
525-       ?  onStateUpdated (forkyBlck.message.slot)
526- 
527-     let 
528-       stateDataTick =  Moment .now ()
529-       updateFlags2 = 
530-         dag.updateFlags +  {skipBlsValidation, skipStateRootValidation}
531- 
532-     ?  checkStateTransition (dag, forkyBlck.asSigVerified (), cache, updateFlags2)
533- 
534-     let  stateVerifyTick =  Moment .now ()
535- 
536-     if  bdata.blob.isSome ():
537-       for  blob in  bdata.blob.get ():
538-         dag.db.putBlobSidecar (blob[])
539- 
540-     type   Trusted  =  typeof  forkyBlck.asTrusted ()
541- 
542-     proc  onBlockAddedHandler (
543-         blckRef: BlockRef ,
544-         trustedBlock: Trusted ,
545-         epochRef: EpochRef ,
546-         unrealized: FinalityCheckpoints 
547-     ) {.gcsafe , raises : [].} = 
548-       onBlockAdded (
549-         blckRef,
550-         ForkedTrustedSignedBeaconBlock .init (trustedBlock),
551-         epochRef,
552-         unrealized)
553- 
554-     let  blockHandler: BlockAdded  (consensusFork) =  onBlockAddedHandler
555- 
556-     discard  addResolvedHeadBlock (
557-       dag, dag.clearanceState,
558-       forkyBlck.asTrusted (),
559-       OptimisticStatus .notValidated,
560-       parent, cache,
561-       blockHandler,
562-       proposerVerifyTick -  startTick,
563-       stateDataTick -  proposerVerifyTick,
564-       stateVerifyTick -  stateDataTick)
484+   if  not (isNil (onStateUpdated)):
485+     ?  onStateUpdated (forkyBlck.message.slot)
486+ 
487+   let 
488+     stateDataTick =  Moment .now ()
489+     updateFlags2 = 
490+       dag.updateFlags +  {skipBlsValidation, skipStateRootValidation}
491+ 
492+   ?  checkStateTransition (dag, forkyBlck.asSigVerified (), cache, updateFlags2)
493+ 
494+   let  stateVerifyTick =  Moment .now ()
495+ 
496+   if  bdata.blob.isSome ():
497+     for  blob in  bdata.blob.get ():
498+       dag.db.putBlobSidecar (blob[])
499+ 
500+   discard  addResolvedHeadBlock (
501+     dag, dag.clearanceState,
502+     forkyBlck.asTrusted (),
503+     OptimisticStatus .notValidated,
504+     parent, cache,
505+     onBlockAdded,
506+     proposerVerifyTick -  startTick,
507+     stateDataTick -  proposerVerifyTick,
508+     stateVerifyTick -  stateDataTick)
565509
566510  ok ()
0 commit comments