@lodestar/fork-choice 1.41.0-dev.192806a314 → 1.41.0-dev.20f622cc52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/lib/forkChoice/errors.d.ts +9 -1
  2. package/lib/forkChoice/errors.d.ts.map +1 -1
  3. package/lib/forkChoice/errors.js +4 -0
  4. package/lib/forkChoice/errors.js.map +1 -1
  5. package/lib/forkChoice/forkChoice.d.ts +66 -11
  6. package/lib/forkChoice/forkChoice.d.ts.map +1 -1
  7. package/lib/forkChoice/forkChoice.js +281 -104
  8. package/lib/forkChoice/forkChoice.js.map +1 -1
  9. package/lib/forkChoice/interface.d.ts +46 -14
  10. package/lib/forkChoice/interface.d.ts.map +1 -1
  11. package/lib/forkChoice/interface.js.map +1 -1
  12. package/lib/forkChoice/store.d.ts +40 -16
  13. package/lib/forkChoice/store.d.ts.map +1 -1
  14. package/lib/forkChoice/store.js +22 -4
  15. package/lib/forkChoice/store.js.map +1 -1
  16. package/lib/index.d.ts +4 -4
  17. package/lib/index.d.ts.map +1 -1
  18. package/lib/index.js +2 -2
  19. package/lib/index.js.map +1 -1
  20. package/lib/protoArray/computeDeltas.d.ts.map +1 -1
  21. package/lib/protoArray/computeDeltas.js +3 -0
  22. package/lib/protoArray/computeDeltas.js.map +1 -1
  23. package/lib/protoArray/errors.d.ts +15 -2
  24. package/lib/protoArray/errors.d.ts.map +1 -1
  25. package/lib/protoArray/errors.js +3 -0
  26. package/lib/protoArray/errors.js.map +1 -1
  27. package/lib/protoArray/interface.d.ts +20 -2
  28. package/lib/protoArray/interface.d.ts.map +1 -1
  29. package/lib/protoArray/interface.js +16 -0
  30. package/lib/protoArray/interface.js.map +1 -1
  31. package/lib/protoArray/protoArray.d.ts +211 -16
  32. package/lib/protoArray/protoArray.d.ts.map +1 -1
  33. package/lib/protoArray/protoArray.js +755 -124
  34. package/lib/protoArray/protoArray.js.map +1 -1
  35. package/package.json +7 -7
  36. package/src/forkChoice/errors.ts +7 -2
  37. package/src/forkChoice/forkChoice.ts +356 -113
  38. package/src/forkChoice/interface.ts +57 -14
  39. package/src/forkChoice/store.ts +52 -20
  40. package/src/index.ts +10 -2
  41. package/src/protoArray/computeDeltas.ts +6 -0
  42. package/src/protoArray/errors.ts +7 -1
  43. package/src/protoArray/interface.ts +36 -3
  44. package/src/protoArray/protoArray.ts +882 -125
@@ -1,7 +1,8 @@
1
1
  import {ChainForkConfig} from "@lodestar/config";
2
- import {SLOTS_PER_EPOCH, SLOTS_PER_HISTORICAL_ROOT} from "@lodestar/params";
2
+ import {ForkSeq, SLOTS_PER_EPOCH, SLOTS_PER_HISTORICAL_ROOT} from "@lodestar/params";
3
3
  import {
4
4
  CachedBeaconStateAllForks,
5
+ CachedBeaconStateGloas,
5
6
  DataAvailabilityStatus,
6
7
  EffectiveBalanceIncrements,
7
8
  ZERO_HASH,
@@ -37,9 +38,11 @@ import {
37
38
  LVHExecResponse,
38
39
  MaybeValidExecutionStatus,
39
40
  NULL_VOTE_INDEX,
41
+ PayloadStatus,
40
42
  ProtoBlock,
41
43
  ProtoNode,
42
44
  VoteIndex,
45
+ isGloasBlock,
43
46
  } from "../protoArray/interface.js";
44
47
  import {ProtoArray} from "../protoArray/protoArray.js";
45
48
  import {ForkChoiceError, ForkChoiceErrorCode, InvalidAttestationCode, InvalidBlockCode} from "./errors.js";
@@ -51,7 +54,7 @@ import {
51
54
  NotReorgedReason,
52
55
  ShouldOverrideForkChoiceUpdateResult,
53
56
  } from "./interface.js";
54
- import {CheckpointWithHex, IForkChoiceStore, JustifiedBalances, toCheckpointWithHex} from "./store.js";
57
+ import {CheckpointWithPayload, IForkChoiceStore, JustifiedBalances, toCheckpointWithPayload} from "./store.js";
55
58
 
56
59
  export type ForkChoiceOpts = {
57
60
  proposerBoost?: boolean;
@@ -71,7 +74,7 @@ export type UpdateAndGetHeadOpt =
71
74
  | {mode: UpdateHeadOpt.GetPredictedProposerHead; secFromSlot: number; slot: Slot};
72
75
 
73
76
  // the initial vote epoch for all validators
74
- const INIT_VOTE_EPOCH: Epoch = 0;
77
+ const INIT_VOTE_SLOT: Slot = 0;
75
78
 
76
79
  /**
77
80
  * Provides an implementation of "Ethereum Consensus -- Beacon Chain Fork Choice":
@@ -94,18 +97,28 @@ export class ForkChoice implements IForkChoice {
94
97
  irrecoverableError?: Error;
95
98
  /**
96
99
  * Votes currently tracked in the protoArray. Instead of tracking a VoteTracker of currentIndex, nextIndex and epoch,
97
- * we decompose the struct and track them in 3 separate arrays for performance reason.
100
+ * we decompose the struct and track them in separate arrays for performance reason.
101
+ *
102
+ * For Gloas (ePBS), LatestMessage tracks slot instead of epoch and includes payload_present flag.
103
+ * Spec: gloas/fork-choice.md#modified-latestmessage
104
+ *
105
+ * IMPORTANT: voteCurrentIndices and voteNextIndices point to the EXACT variant node index.
106
+ * The payload status is encoded in the node index itself (different variants have different indices).
107
+ * For example, if a validator votes for the EMPTY variant, voteNextIndices[i] points to that specific EMPTY node.
98
108
  */
99
109
  private readonly voteCurrentIndices: VoteIndex[];
100
110
  private readonly voteNextIndices: VoteIndex[];
101
- private readonly voteNextEpochs: Epoch[];
111
+ private readonly voteNextSlots: Slot[];
102
112
 
103
113
  /**
104
114
  * Attestations that arrived at the current slot and must be queued for later processing.
105
115
  * NOT currently tracked in the protoArray
116
+ *
117
+ * Modified for Gloas to track PayloadStatus per validator.
118
+ * Maps: Slot -> BlockRoot -> ValidatorIndex -> PayloadStatus
106
119
  */
107
- private readonly queuedAttestations: MapDef<Slot, MapDef<RootHex, Set<ValidatorIndex>>> = new MapDef(
108
- () => new MapDef(() => new Set())
120
+ private readonly queuedAttestations: MapDef<Slot, MapDef<RootHex, Map<ValidatorIndex, PayloadStatus>>> = new MapDef(
121
+ () => new MapDef(() => new Map())
109
122
  );
110
123
 
111
124
  /**
@@ -150,13 +163,14 @@ export class ForkChoice implements IForkChoice {
150
163
  this.voteCurrentIndices = new Array(validatorCount).fill(NULL_VOTE_INDEX);
151
164
  this.voteNextIndices = new Array(validatorCount).fill(NULL_VOTE_INDEX);
152
165
  // when compute deltas, we ignore epoch if voteNextIndex is NULL_VOTE_INDEX anyway
153
- this.voteNextEpochs = new Array(validatorCount).fill(INIT_VOTE_EPOCH);
166
+
167
+ this.voteNextSlots = new Array(validatorCount).fill(0);
154
168
 
155
169
  this.head = this.updateHead();
156
170
  this.balances = this.fcStore.justified.balances;
157
171
 
158
172
  metrics?.forkChoice.votes.addCollect(() => {
159
- metrics.forkChoice.votes.set(this.voteNextEpochs.length);
173
+ metrics.forkChoice.votes.set(this.voteNextSlots.length);
160
174
  metrics.forkChoice.queuedAttestations.set(this.queuedAttestationsPreviousSlot);
161
175
  metrics.forkChoice.validatedAttestationDatas.set(this.validatedAttestationDatas.size);
162
176
  metrics.forkChoice.balancesLength.set(this.balances.length);
@@ -177,7 +191,7 @@ export class ForkChoice implements IForkChoice {
177
191
  *
178
192
  * https://github.com/ethereum/consensus-specs/blob/v1.1.10/specs/phase0/fork-choice.md#get_ancestor
179
193
  */
180
- getAncestor(blockRoot: RootHex, ancestorSlot: Slot): RootHex {
194
+ getAncestor(blockRoot: RootHex, ancestorSlot: Slot): ProtoNode {
181
195
  return this.protoArray.getAncestor(blockRoot, ancestorSlot);
182
196
  }
183
197
 
@@ -237,11 +251,10 @@ export class ForkChoice implements IForkChoice {
237
251
  // Return false otherwise.
238
252
  // Note when proposer boost reorg is disabled, it always returns false
239
253
  shouldOverrideForkChoiceUpdate(
240
- blockRoot: RootHex,
254
+ headBlock: ProtoBlock,
241
255
  secFromSlot: number,
242
256
  currentSlot: Slot
243
257
  ): ShouldOverrideForkChoiceUpdateResult {
244
- const headBlock = this.getBlockHex(blockRoot);
245
258
  if (headBlock === null) {
246
259
  // should not happen because this block just got imported. Fall back to no-reorg.
247
260
  return {shouldOverrideFcu: false, reason: NotReorgedReason.HeadBlockNotAvailable};
@@ -257,7 +270,10 @@ export class ForkChoice implements IForkChoice {
257
270
  return {shouldOverrideFcu: false, reason: NotReorgedReason.ProposerBoostReorgDisabled};
258
271
  }
259
272
 
260
- const parentBlock = this.protoArray.getBlock(headBlock.parentRoot);
273
+ const parentBlock = this.protoArray.getBlock(
274
+ headBlock.parentRoot,
275
+ this.protoArray.getParentPayloadStatus(headBlock)
276
+ );
261
277
  const proposalSlot = headBlock.slot + 1;
262
278
 
263
279
  // No reorg if parentBlock isn't available
@@ -282,7 +298,10 @@ export class ForkChoice implements IForkChoice {
282
298
  return {shouldOverrideFcu: false, reason: NotReorgedReason.ReorgMoreThanOneSlot};
283
299
  }
284
300
 
285
- this.logger?.verbose("Block is weak. Should override forkchoice update", {blockRoot, slot: currentSlot});
301
+ this.logger?.verbose("Block is weak. Should override forkchoice update", {
302
+ blockRoot: headBlock.blockRoot,
303
+ slot: currentSlot,
304
+ });
286
305
  return {shouldOverrideFcu: true, parentBlock};
287
306
  }
288
307
 
@@ -316,7 +335,7 @@ export class ForkChoice implements IForkChoice {
316
335
  }
317
336
 
318
337
  const blockRoot = headBlock.blockRoot;
319
- const result = this.shouldOverrideForkChoiceUpdate(blockRoot, secFromSlot, currentSlot);
338
+ const result = this.shouldOverrideForkChoiceUpdate(headBlock, secFromSlot, currentSlot);
320
339
 
321
340
  if (result.shouldOverrideFcu) {
322
341
  this.logger?.verbose("Current head is weak. Predicting next block to be built on parent of head.", {
@@ -363,7 +382,10 @@ export class ForkChoice implements IForkChoice {
363
382
  return {proposerHead, isHeadTimely, notReorgedReason: NotReorgedReason.ProposerBoostReorgDisabled};
364
383
  }
365
384
 
366
- const parentBlock = this.protoArray.getBlock(headBlock.parentRoot);
385
+ const parentBlock = this.protoArray.getBlock(
386
+ headBlock.parentRoot,
387
+ this.protoArray.getParentPayloadStatus(headBlock)
388
+ );
367
389
 
368
390
  // No reorg if parentBlock isn't available
369
391
  if (parentBlock === undefined) {
@@ -400,7 +422,7 @@ export class ForkChoice implements IForkChoice {
400
422
  slotsPerEpoch: SLOTS_PER_EPOCH,
401
423
  committeePercent: this.config.REORG_HEAD_WEIGHT_THRESHOLD,
402
424
  });
403
- const headNode = this.protoArray.getNode(headBlock.blockRoot);
425
+ const headNode = this.protoArray.getNode(headBlock.blockRoot, headBlock.payloadStatus);
404
426
  // If headNode is unavailable, give up reorg
405
427
  if (headNode === undefined || headNode.weight >= reorgThreshold) {
406
428
  return {proposerHead, isHeadTimely, notReorgedReason: NotReorgedReason.HeadBlockNotWeak};
@@ -412,7 +434,7 @@ export class ForkChoice implements IForkChoice {
412
434
  slotsPerEpoch: SLOTS_PER_EPOCH,
413
435
  committeePercent: this.config.REORG_PARENT_WEIGHT_THRESHOLD,
414
436
  });
415
- const parentNode = this.protoArray.getNode(parentBlock.blockRoot);
437
+ const parentNode = this.protoArray.getNode(parentBlock.blockRoot, parentBlock.payloadStatus);
416
438
  // If parentNode is unavailable, give up reorg
417
439
  if (parentNode === undefined || parentNode.weight <= parentThreshold) {
418
440
  return {proposerHead, isHeadTimely, notReorgedReason: NotReorgedReason.ParentBlockNotStrong};
@@ -509,23 +531,10 @@ export class ForkChoice implements IForkChoice {
509
531
  currentSlot,
510
532
  });
511
533
 
512
- const headRoot = this.protoArray.findHead(this.fcStore.justified.checkpoint.rootHex, currentSlot);
513
- const headIndex = this.protoArray.indices.get(headRoot);
514
- if (headIndex === undefined) {
515
- throw new ForkChoiceError({
516
- code: ForkChoiceErrorCode.MISSING_PROTO_ARRAY_BLOCK,
517
- root: headRoot,
518
- });
519
- }
520
- const headNode = this.protoArray.nodes[headIndex];
521
- if (headNode === undefined) {
522
- throw new ForkChoiceError({
523
- code: ForkChoiceErrorCode.MISSING_PROTO_ARRAY_BLOCK,
524
- root: headRoot,
525
- });
526
- }
534
+ // findHead returns the ProtoNode representing the head
535
+ const head = this.protoArray.findHead(this.fcStore.justified.checkpoint.rootHex, currentSlot);
527
536
 
528
- this.head = headNode;
537
+ this.head = head;
529
538
  return this.head;
530
539
  }
531
540
 
@@ -548,11 +557,11 @@ export class ForkChoice implements IForkChoice {
548
557
  return this.protoArray.nodes;
549
558
  }
550
559
 
551
- getFinalizedCheckpoint(): CheckpointWithHex {
560
+ getFinalizedCheckpoint(): CheckpointWithPayload {
552
561
  return this.fcStore.finalizedCheckpoint;
553
562
  }
554
563
 
555
- getJustifiedCheckpoint(): CheckpointWithHex {
564
+ getJustifiedCheckpoint(): CheckpointWithPayload {
556
565
  return this.fcStore.justified.checkpoint;
557
566
  }
558
567
 
@@ -585,14 +594,18 @@ export class ForkChoice implements IForkChoice {
585
594
  ): ProtoBlock {
586
595
  const {parentRoot, slot} = block;
587
596
  const parentRootHex = toRootHex(parentRoot);
588
- // Parent block must be known
589
- const parentBlock = this.protoArray.getBlock(parentRootHex);
597
+ // Parent block must be known because state_transition would have failed otherwise.
598
+ const parentHashHex = isGloasBeaconBlock(block)
599
+ ? toRootHex(block.body.signedExecutionPayloadBid.message.parentBlockHash)
600
+ : null;
601
+ const parentBlock = this.protoArray.getParent(parentRootHex, parentHashHex);
590
602
  if (!parentBlock) {
591
603
  throw new ForkChoiceError({
592
604
  code: ForkChoiceErrorCode.INVALID_BLOCK,
593
605
  err: {
594
606
  code: InvalidBlockCode.UNKNOWN_PARENT,
595
607
  root: parentRootHex,
608
+ hash: parentHashHex,
596
609
  },
597
610
  });
598
611
  }
@@ -627,15 +640,18 @@ export class ForkChoice implements IForkChoice {
627
640
  }
628
641
 
629
642
  // Check block is a descendant of the finalized block at the checkpoint finalized slot.
630
- const blockAncestorRoot = this.getAncestor(parentRootHex, finalizedSlot);
631
- const finalizedRoot = this.fcStore.finalizedCheckpoint.rootHex;
632
- if (blockAncestorRoot !== finalizedRoot) {
643
+ const blockAncestorNode = this.getAncestor(parentRootHex, finalizedSlot);
644
+ const fcStoreFinalized = this.fcStore.finalizedCheckpoint;
645
+ if (
646
+ blockAncestorNode.blockRoot !== fcStoreFinalized.rootHex ||
647
+ blockAncestorNode.payloadStatus !== fcStoreFinalized.payloadStatus
648
+ ) {
633
649
  throw new ForkChoiceError({
634
650
  code: ForkChoiceErrorCode.INVALID_BLOCK,
635
651
  err: {
636
652
  code: InvalidBlockCode.NOT_FINALIZED_DESCENDANT,
637
- finalizedRoot,
638
- blockAncestor: blockAncestorRoot,
653
+ finalizedRoot: fcStoreFinalized.rootHex,
654
+ blockAncestor: blockAncestorNode.blockRoot,
639
655
  },
640
656
  });
641
657
  }
@@ -655,10 +671,15 @@ export class ForkChoice implements IForkChoice {
655
671
  this.proposerBoostRoot = blockRootHex;
656
672
  }
657
673
 
658
- const justifiedCheckpoint = toCheckpointWithHex(state.currentJustifiedCheckpoint);
659
- const finalizedCheckpoint = toCheckpointWithHex(state.finalizedCheckpoint);
674
+ // Get justified checkpoint with payload status for Gloas
675
+ const justifiedPayloadStatus = getCheckpointPayloadStatus(state, state.currentJustifiedCheckpoint.epoch);
676
+ const justifiedCheckpoint = toCheckpointWithPayload(state.currentJustifiedCheckpoint, justifiedPayloadStatus);
660
677
  const stateJustifiedEpoch = justifiedCheckpoint.epoch;
661
678
 
679
+ // Get finalized checkpoint with payload status for Gloas
680
+ const finalizedPayloadStatus = getCheckpointPayloadStatus(state, state.finalizedCheckpoint.epoch);
681
+ const finalizedCheckpoint = toCheckpointWithPayload(state.finalizedCheckpoint, finalizedPayloadStatus);
682
+
662
683
  // Justified balances for `justifiedCheckpoint` are new to the fork-choice. Compute them on demand only if
663
684
  // the justified checkpoint changes
664
685
  this.updateCheckpoints(justifiedCheckpoint, finalizedCheckpoint, () =>
@@ -679,29 +700,57 @@ export class ForkChoice implements IForkChoice {
679
700
  // This is an optimization. It should reduce the amount of times we run
680
701
  // `process_justification_and_finalization` by approximately 1/3rd when the chain is
681
702
  // performing optimally.
682
- let unrealizedJustifiedCheckpoint: CheckpointWithHex;
683
- let unrealizedFinalizedCheckpoint: CheckpointWithHex;
703
+ let unrealizedJustifiedCheckpoint: CheckpointWithPayload;
704
+ let unrealizedFinalizedCheckpoint: CheckpointWithPayload;
684
705
  if (this.opts?.computeUnrealized) {
685
706
  if (
686
707
  parentBlock.unrealizedJustifiedEpoch === blockEpoch &&
687
708
  parentBlock.unrealizedFinalizedEpoch + 1 >= blockEpoch
688
709
  ) {
689
710
  // reuse from parent, happens at 1/3 last blocks of epoch as monitored in mainnet
711
+ // Get payload status for unrealized justified checkpoint
712
+ const unrealizedJustifiedPayloadStatus = getCheckpointPayloadStatus(
713
+ state,
714
+ parentBlock.unrealizedJustifiedEpoch
715
+ );
690
716
  unrealizedJustifiedCheckpoint = {
691
717
  epoch: parentBlock.unrealizedJustifiedEpoch,
692
718
  root: fromHex(parentBlock.unrealizedJustifiedRoot),
693
719
  rootHex: parentBlock.unrealizedJustifiedRoot,
720
+ payloadStatus: unrealizedJustifiedPayloadStatus,
694
721
  };
722
+ // Get payload status for unrealized finalized checkpoint
723
+ const unrealizedFinalizedPayloadStatus = getCheckpointPayloadStatus(
724
+ state,
725
+ parentBlock.unrealizedFinalizedEpoch
726
+ );
695
727
  unrealizedFinalizedCheckpoint = {
696
728
  epoch: parentBlock.unrealizedFinalizedEpoch,
697
729
  root: fromHex(parentBlock.unrealizedFinalizedRoot),
698
730
  rootHex: parentBlock.unrealizedFinalizedRoot,
731
+ payloadStatus: unrealizedFinalizedPayloadStatus,
699
732
  };
700
733
  } else {
701
734
  // compute new, happens 2/3 first blocks of epoch as monitored in mainnet
702
735
  const unrealized = computeUnrealizedCheckpoints(state);
703
- unrealizedJustifiedCheckpoint = toCheckpointWithHex(unrealized.justifiedCheckpoint);
704
- unrealizedFinalizedCheckpoint = toCheckpointWithHex(unrealized.finalizedCheckpoint);
736
+ // Get payload status for unrealized justified checkpoint
737
+ const unrealizedJustifiedPayloadStatus = getCheckpointPayloadStatus(
738
+ state,
739
+ unrealized.justifiedCheckpoint.epoch
740
+ );
741
+ unrealizedJustifiedCheckpoint = toCheckpointWithPayload(
742
+ unrealized.justifiedCheckpoint,
743
+ unrealizedJustifiedPayloadStatus
744
+ );
745
+ // Get payload status for unrealized finalized checkpoint
746
+ const unrealizedFinalizedPayloadStatus = getCheckpointPayloadStatus(
747
+ state,
748
+ unrealized.finalizedCheckpoint.epoch
749
+ );
750
+ unrealizedFinalizedCheckpoint = toCheckpointWithPayload(
751
+ unrealized.finalizedCheckpoint,
752
+ unrealizedFinalizedPayloadStatus
753
+ );
705
754
  }
706
755
  } else {
707
756
  unrealizedJustifiedCheckpoint = justifiedCheckpoint;
@@ -743,32 +792,56 @@ export class ForkChoice implements IForkChoice {
743
792
  unrealizedFinalizedEpoch: unrealizedFinalizedCheckpoint.epoch,
744
793
  unrealizedFinalizedRoot: unrealizedFinalizedCheckpoint.rootHex,
745
794
 
746
- // TODO GLOAS: Need to update this when we are merging nc/epbs-fc. Need to define `getPostGloasExecStatus`
747
- // to make sure execution status of post-gloas blocks is ExecutionStatus.PayloadSeparated
748
- ...(isExecutionBlockBodyType(block.body) && isExecutionStateType(state) && isExecutionEnabled(state, block)
749
- ? {
750
- executionPayloadBlockHash: toRootHex(block.body.executionPayload.blockHash),
751
- executionPayloadNumber: block.body.executionPayload.blockNumber,
752
- executionStatus: this.getPreGloasExecStatus(executionStatus),
753
- dataAvailabilityStatus,
754
- }
755
- : {
756
- executionPayloadBlockHash: null,
757
- executionStatus: this.getPreMergeExecStatus(executionStatus),
758
- dataAvailabilityStatus: this.getPreMergeDataStatus(dataAvailabilityStatus),
759
- }),
760
795
  ...(isGloasBeaconBlock(block)
761
796
  ? {
762
- builderIndex: block.body.signedExecutionPayloadBid.message.builderIndex,
763
- blockHashHex: toRootHex(block.body.signedExecutionPayloadBid.message.blockHash),
797
+ executionPayloadBlockHash: toRootHex(block.body.signedExecutionPayloadBid.message.parentBlockHash), // post-gloas, we don't know payload hash until we import execution payload. Set to parent payload hash for now
798
+ executionPayloadNumber: (() => {
799
+ // Determine parent's execution payload number based on which variant the block extends
800
+ const parentBlockHashFromBid = toRootHex(block.body.signedExecutionPayloadBid.message.parentBlockHash);
801
+
802
+ // If parent is pre-merge, return 0
803
+ if (parentBlock.executionPayloadBlockHash === null) {
804
+ return 0;
805
+ }
806
+
807
+ // If parent is pre-Gloas, it only has FULL variant
808
+ if (parentBlock.parentBlockHash === null) {
809
+ return parentBlock.executionPayloadNumber;
810
+ }
811
+
812
+ // Parent is Gloas: get the variant that matches the parentBlockHash from bid
813
+ const parentVariant = this.getBlockHexAndBlockHash(parentRootHex, parentBlockHashFromBid);
814
+ if (parentVariant && parentVariant.executionPayloadBlockHash !== null) {
815
+ return parentVariant.executionPayloadNumber;
816
+ }
817
+ // Fallback to parent block's number (we know it's post-merge from check above)
818
+ return parentBlock.executionPayloadNumber;
819
+ })(),
820
+ executionStatus: this.getPostGloasExecStatus(executionStatus),
821
+ dataAvailabilityStatus,
764
822
  }
765
- : {
766
- builderIndex: undefined,
767
- blockHashHex: undefined,
768
- }),
823
+ : isExecutionBlockBodyType(block.body) && isExecutionStateType(state) && isExecutionEnabled(state, block)
824
+ ? {
825
+ executionPayloadBlockHash: toRootHex(block.body.executionPayload.blockHash),
826
+ executionPayloadNumber: block.body.executionPayload.blockNumber,
827
+ executionStatus: this.getPreGloasExecStatus(executionStatus),
828
+ dataAvailabilityStatus,
829
+ }
830
+ : {
831
+ executionPayloadBlockHash: null,
832
+ executionStatus: this.getPreMergeExecStatus(executionStatus),
833
+ dataAvailabilityStatus: this.getPreMergeDataStatus(dataAvailabilityStatus),
834
+ }),
835
+
836
+ payloadStatus: isGloasBeaconBlock(block) ? PayloadStatus.PENDING : PayloadStatus.FULL,
837
+ builderIndex: isGloasBeaconBlock(block) ? block.body.signedExecutionPayloadBid.message.builderIndex : null,
838
+ blockHashFromBid: isGloasBeaconBlock(block)
839
+ ? toRootHex(block.body.signedExecutionPayloadBid.message.blockHash)
840
+ : null,
841
+ parentBlockHash: parentHashHex,
769
842
  };
770
843
 
771
- this.protoArray.onBlock(protoBlock, currentSlot);
844
+ this.protoArray.onBlock(protoBlock, currentSlot, this.proposerBoostRoot);
772
845
 
773
846
  return protoBlock;
774
847
  }
@@ -815,10 +888,45 @@ export class ForkChoice implements IForkChoice {
815
888
 
816
889
  this.validateOnAttestation(attestation, slot, blockRootHex, targetEpoch, attDataRoot, forceImport);
817
890
 
891
+ // Pre-gloas: payload is always present
892
+ // Post-gloas:
893
+ // - always add weight to PENDING
894
+ // - if message.slot > block.slot, it also add weights to FULL or EMPTY
895
+ let payloadStatus: PayloadStatus;
896
+
897
+ // We need to retrieve block to check if it's Gloas and to compare slot
898
+ // https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.1/specs/gloas/fork-choice.md#new-is_supporting_vote
899
+ const block = this.getBlockHexDefaultStatus(blockRootHex);
900
+
901
+ if (block && isGloasBlock(block)) {
902
+ // Post-Gloas block: determine FULL/EMPTY/PENDING based on slot and committee index
903
+ // If slot > block.slot, we can determine FULL or EMPTY. Else always PENDING
904
+ if (slot > block.slot) {
905
+ if (attestationData.index === 1) {
906
+ payloadStatus = PayloadStatus.FULL;
907
+ } else if (attestationData.index === 0) {
908
+ payloadStatus = PayloadStatus.EMPTY;
909
+ } else {
910
+ throw new ForkChoiceError({
911
+ code: ForkChoiceErrorCode.INVALID_ATTESTATION,
912
+ err: {
913
+ code: InvalidAttestationCode.INVALID_DATA_INDEX,
914
+ index: attestationData.index,
915
+ },
916
+ });
917
+ }
918
+ } else {
919
+ payloadStatus = PayloadStatus.PENDING;
920
+ }
921
+ } else {
922
+ // Pre-Gloas block or block not found: always FULL
923
+ payloadStatus = PayloadStatus.FULL;
924
+ }
925
+
818
926
  if (slot < this.fcStore.currentSlot) {
819
927
  for (const validatorIndex of attestation.attestingIndices) {
820
928
  if (!this.fcStore.equivocatingIndices.has(validatorIndex)) {
821
- this.addLatestMessage(validatorIndex, targetEpoch, blockRootHex);
929
+ this.addLatestMessage(validatorIndex, slot, blockRootHex, payloadStatus);
822
930
  }
823
931
  }
824
932
  } else {
@@ -829,10 +937,10 @@ export class ForkChoice implements IForkChoice {
829
937
  // Delay consideration in the fork choice until their slot is in the past.
830
938
  // ```
831
939
  const byRoot = this.queuedAttestations.getOrDefault(slot);
832
- const validatorIndices = byRoot.getOrDefault(blockRootHex);
940
+ const validatorVotes = byRoot.getOrDefault(blockRootHex);
833
941
  for (const validatorIndex of attestation.attestingIndices) {
834
942
  if (!this.fcStore.equivocatingIndices.has(validatorIndex)) {
835
- validatorIndices.add(validatorIndex);
943
+ validatorVotes.set(validatorIndex, payloadStatus);
836
944
  }
837
945
  }
838
946
  }
@@ -851,6 +959,36 @@ export class ForkChoice implements IForkChoice {
851
959
  }
852
960
  }
853
961
 
962
+ /**
963
+ * Process a PTC (Payload Timeliness Committee) message
964
+ * Updates the PTC votes for multiple validators attesting to a block
965
+ * Spec: gloas/fork-choice.md#new-on_payload_attestation_message
966
+ */
967
+ notifyPtcMessages(blockRoot: RootHex, ptcIndices: number[], payloadPresent: boolean): void {
968
+ this.protoArray.notifyPtcMessages(blockRoot, ptcIndices, payloadPresent);
969
+ }
970
+
971
+ /**
972
+ * Notify fork choice that an execution payload has arrived (Gloas fork)
973
+ * Creates the FULL variant of a Gloas block when the payload becomes available
974
+ * Spec: gloas/fork-choice.md#new-on_execution_payload
975
+ */
976
+ onExecutionPayload(
977
+ blockRoot: RootHex,
978
+ executionPayloadBlockHash: RootHex,
979
+ executionPayloadNumber: number,
980
+ executionPayloadStateRoot: RootHex
981
+ ): void {
982
+ this.protoArray.onExecutionPayload(
983
+ blockRoot,
984
+ this.fcStore.currentSlot,
985
+ executionPayloadBlockHash,
986
+ executionPayloadNumber,
987
+ executionPayloadStateRoot,
988
+ this.proposerBoostRoot
989
+ );
990
+ }
991
+
854
992
  /**
855
993
  * Call `onTick` for all slots between `fcStore.getCurrentSlot()` and the provided `currentSlot`.
856
994
  * This should only be called once per slot because:
@@ -882,15 +1020,21 @@ export class ForkChoice implements IForkChoice {
882
1020
  return this.hasBlockHex(toRootHex(blockRoot));
883
1021
  }
884
1022
  /** Returns a `ProtoBlock` if the block is known **and** a descendant of the finalized root. */
885
- getBlock(blockRoot: Root): ProtoBlock | null {
886
- return this.getBlockHex(toRootHex(blockRoot));
1023
+ getBlock(blockRoot: Root, payloadStatus: PayloadStatus): ProtoBlock | null {
1024
+ return this.getBlockHex(toRootHex(blockRoot), payloadStatus);
1025
+ }
1026
+
1027
+ getBlockDefaultStatus(blockRoot: Root): ProtoBlock | null {
1028
+ return this.getBlockHexDefaultStatus(toRootHex(blockRoot));
887
1029
  }
888
1030
 
889
1031
  /**
890
1032
  * Returns `true` if the block is known **and** a descendant of the finalized root.
1033
+ * Uses default variant (PENDING for Gloas, FULL for pre-Gloas).
891
1034
  */
892
1035
  hasBlockHex(blockRoot: RootHex): boolean {
893
- const node = this.protoArray.getNode(blockRoot);
1036
+ const defaultStatus = this.protoArray.getDefaultVariant(blockRoot);
1037
+ const node = defaultStatus !== undefined ? this.protoArray.getNode(blockRoot, defaultStatus) : undefined;
894
1038
  if (node === undefined) {
895
1039
  return false;
896
1040
  }
@@ -915,8 +1059,8 @@ export class ForkChoice implements IForkChoice {
915
1059
  /**
916
1060
  * Returns a MUTABLE `ProtoBlock` if the block is known **and** a descendant of the finalized root.
917
1061
  */
918
- getBlockHex(blockRoot: RootHex): ProtoBlock | null {
919
- const node = this.protoArray.getNode(blockRoot);
1062
+ getBlockHex(blockRoot: RootHex, payloadStatus: PayloadStatus): ProtoBlock | null {
1063
+ const node = this.protoArray.getNode(blockRoot, payloadStatus);
920
1064
  if (!node) {
921
1065
  return null;
922
1066
  }
@@ -930,23 +1074,49 @@ export class ForkChoice implements IForkChoice {
930
1074
  };
931
1075
  }
932
1076
 
1077
+ /**
1078
+ * Returns a `ProtoBlock` with the default variant for the given block root
1079
+ * - Pre-Gloas blocks: returns FULL variant (only variant)
1080
+ * - Gloas blocks: returns PENDING variant
1081
+ *
1082
+ * Use this when you need the canonical block reference regardless of payload status.
1083
+ * For searching by execution payload hash and variant-specific info, use `getBlockHexAndBlockHash` instead.
1084
+ */
1085
+ getBlockHexDefaultStatus(blockRoot: RootHex): ProtoBlock | null {
1086
+ const defaultStatus = this.protoArray.getDefaultVariant(blockRoot);
1087
+ if (defaultStatus === undefined) {
1088
+ return null;
1089
+ }
1090
+
1091
+ return this.getBlockHex(blockRoot, defaultStatus);
1092
+ }
1093
+
1094
+ /**
1095
+ * Returns EMPTY or FULL `ProtoBlock` that has matching block root and block hash
1096
+ */
1097
+ getBlockHexAndBlockHash(blockRoot: RootHex, blockHash: RootHex): ProtoBlock | null {
1098
+ return this.protoArray.getBlockHexAndBlockHash(blockRoot, blockHash);
1099
+ }
1100
+
933
1101
  getJustifiedBlock(): ProtoBlock {
934
- const block = this.getBlockHex(this.fcStore.justified.checkpoint.rootHex);
1102
+ const {rootHex, payloadStatus} = this.fcStore.justified.checkpoint;
1103
+ const block = this.getBlockHex(rootHex, payloadStatus);
935
1104
  if (!block) {
936
1105
  throw new ForkChoiceError({
937
1106
  code: ForkChoiceErrorCode.MISSING_PROTO_ARRAY_BLOCK,
938
- root: this.fcStore.justified.checkpoint.rootHex,
1107
+ root: rootHex,
939
1108
  });
940
1109
  }
941
1110
  return block;
942
1111
  }
943
1112
 
944
1113
  getFinalizedBlock(): ProtoBlock {
945
- const block = this.getBlockHex(this.fcStore.finalizedCheckpoint.rootHex);
1114
+ const {rootHex, payloadStatus} = this.fcStore.finalizedCheckpoint;
1115
+ const block = this.getBlockHex(rootHex, payloadStatus);
946
1116
  if (!block) {
947
1117
  throw new ForkChoiceError({
948
1118
  code: ForkChoiceErrorCode.MISSING_PROTO_ARRAY_BLOCK,
949
- root: this.fcStore.finalizedCheckpoint.rootHex,
1119
+ root: rootHex,
950
1120
  });
951
1121
  }
952
1122
  return block;
@@ -973,7 +1143,7 @@ export class ForkChoice implements IForkChoice {
973
1143
  prune(finalizedRoot: RootHex): ProtoBlock[] {
974
1144
  const prunedNodes = this.protoArray.maybePrune(finalizedRoot);
975
1145
  const prunedCount = prunedNodes.length;
976
- for (let i = 0; i < this.voteNextEpochs.length; i++) {
1146
+ for (let i = 0; i < this.voteNextSlots.length; i++) {
977
1147
  const currentIndex = this.voteCurrentIndices[i];
978
1148
 
979
1149
  if (currentIndex !== NULL_VOTE_INDEX) {
@@ -1076,17 +1246,24 @@ export class ForkChoice implements IForkChoice {
1076
1246
  return this.protoArray.nodes;
1077
1247
  }
1078
1248
 
1249
+ // TODO GLOAS: this function is ambiguous, consumer should also provide payload, or it should accept a ProtoBlock instead
1250
+ // also consumer may want PENDING or EMPTY only
1079
1251
  *forwardIterateDescendants(blockRoot: RootHex): IterableIterator<ProtoBlock> {
1080
1252
  const rootsInChain = new Set([blockRoot]);
1081
1253
 
1082
- const blockIndex = this.protoArray.indices.get(blockRoot);
1083
- if (blockIndex === undefined) {
1254
+ const blockVariants = this.protoArray.indices.get(blockRoot);
1255
+ if (blockVariants === undefined) {
1084
1256
  throw new ForkChoiceError({
1085
1257
  code: ForkChoiceErrorCode.MISSING_PROTO_ARRAY_BLOCK,
1086
1258
  root: blockRoot,
1087
1259
  });
1088
1260
  }
1089
1261
 
1262
+ // Find the minimum index among all variants to start iteration
1263
+ const blockIndex = Array.isArray(blockVariants)
1264
+ ? Math.min(...blockVariants.filter((idx) => idx !== undefined))
1265
+ : blockVariants;
1266
+
1090
1267
  for (let i = blockIndex + 1; i < this.protoArray.nodes.length; i++) {
1091
1268
  const node = this.protoArray.nodes[i];
1092
1269
  if (rootsInChain.has(node.parentRoot)) {
@@ -1116,8 +1293,8 @@ export class ForkChoice implements IForkChoice {
1116
1293
 
1117
1294
  /** Returns the distance of common ancestor of nodes to the max of the newNode and the prevNode. */
1118
1295
  getCommonAncestorDepth(prevBlock: ProtoBlock, newBlock: ProtoBlock): AncestorResult {
1119
- const prevNode = this.protoArray.getNode(prevBlock.blockRoot);
1120
- const newNode = this.protoArray.getNode(newBlock.blockRoot);
1296
+ const prevNode = this.protoArray.getNode(prevBlock.blockRoot, prevBlock.payloadStatus);
1297
+ const newNode = this.protoArray.getNode(newBlock.blockRoot, newBlock.payloadStatus);
1121
1298
  if (!prevNode || !newNode) {
1122
1299
  return {code: AncestorStatus.BlockUnknown};
1123
1300
  }
@@ -1198,12 +1375,17 @@ export class ForkChoice implements IForkChoice {
1198
1375
  return block.parentRoot;
1199
1376
  }
1200
1377
 
1201
- block =
1202
- block.blockRoot === block.targetRoot
1203
- ? // For the first slot of the epoch, a block is it's own target
1204
- this.protoArray.getBlockReadonly(block.parentRoot)
1205
- : // else we can navigate much faster jumping to the target block
1206
- this.protoArray.getBlockReadonly(block.targetRoot);
1378
+ // For the first slot of the epoch, a block is it's own target
1379
+ const nextRoot = block.blockRoot === block.targetRoot ? block.parentRoot : block.targetRoot;
1380
+ // Use default variant (PENDING for Gloas, FULL for pre-Gloas)
1381
+ // For Gloas: we search for PENDING blocks because dependent root is determined by the block itself,
1382
+ // not the payload. In state-transition, block parentage is independent of payload status,
1383
+ // so linking by PENDING block in fork-choice is correct.
1384
+ const defaultStatus = this.protoArray.getDefaultVariant(nextRoot);
1385
+ if (defaultStatus === undefined) {
1386
+ throw Error(`No block for root ${nextRoot}`);
1387
+ }
1388
+ block = this.protoArray.getBlockReadonly(nextRoot, defaultStatus);
1207
1389
  }
1208
1390
 
1209
1391
  throw Error(`Not found dependent root for block slot ${block.slot}, epoch difference ${epochDifference}`);
@@ -1252,6 +1434,14 @@ export class ForkChoice implements IForkChoice {
1252
1434
  return executionStatus;
1253
1435
  }
1254
1436
 
1437
+ private getPostGloasExecStatus(executionStatus: MaybeValidExecutionStatus): ExecutionStatus.PayloadSeparated {
1438
+ if (executionStatus !== ExecutionStatus.PayloadSeparated)
1439
+ throw Error(
1440
+ `Invalid post-gloas execution status: expected: ${ExecutionStatus.PayloadSeparated}, got ${executionStatus}`
1441
+ );
1442
+ return executionStatus;
1443
+ }
1444
+
1255
1445
  /**
1256
1446
  * Why `getJustifiedBalances` getter?
1257
1447
  * - updateCheckpoints() is called in both on_block and on_tick.
@@ -1269,12 +1459,12 @@ export class ForkChoice implements IForkChoice {
1269
1459
  *
1270
1460
  * **`on_tick`**
1271
1461
  * May need the justified balances of:
1272
- * - unrealizedJustified: Already available in `CheckpointHexWithBalance`
1462
+ * - unrealizedJustified: Already available in `CheckpointWithPayloadAndBalance`
1273
1463
  * Since this balances are already available the getter is just `() => balances`, without cache interaction
1274
1464
  */
1275
1465
  private updateCheckpoints(
1276
- justifiedCheckpoint: CheckpointWithHex,
1277
- finalizedCheckpoint: CheckpointWithHex,
1466
+ justifiedCheckpoint: CheckpointWithPayload,
1467
+ finalizedCheckpoint: CheckpointWithPayload,
1278
1468
  getJustifiedBalances: () => JustifiedBalances
1279
1469
  ): void {
1280
1470
  // Update justified checkpoint.
@@ -1294,8 +1484,8 @@ export class ForkChoice implements IForkChoice {
1294
1484
  * Update unrealized checkpoints in store if necessary
1295
1485
  */
1296
1486
  private updateUnrealizedCheckpoints(
1297
- unrealizedJustifiedCheckpoint: CheckpointWithHex,
1298
- unrealizedFinalizedCheckpoint: CheckpointWithHex,
1487
+ unrealizedJustifiedCheckpoint: CheckpointWithPayload,
1488
+ unrealizedFinalizedCheckpoint: CheckpointWithPayload,
1299
1489
  getJustifiedBalances: () => JustifiedBalances
1300
1490
  ): void {
1301
1491
  if (unrealizedJustifiedCheckpoint.epoch > this.fcStore.unrealizedJustified.checkpoint.epoch) {
@@ -1414,7 +1604,9 @@ export class ForkChoice implements IForkChoice {
1414
1604
  //
1415
1605
  // Attestations must be for a known block. If the block is unknown, we simply drop the
1416
1606
  // attestation and do not delay consideration for later.
1417
- const block = this.protoArray.getBlock(beaconBlockRootHex);
1607
+ // We don't care which variant it is, just need to find the block
1608
+ const defaultStatus = this.protoArray.getDefaultVariant(beaconBlockRootHex);
1609
+ const block = defaultStatus !== undefined ? this.protoArray.getBlock(beaconBlockRootHex, defaultStatus) : undefined;
1418
1610
  if (!block) {
1419
1611
  throw new ForkChoiceError({
1420
1612
  code: ForkChoiceErrorCode.INVALID_ATTESTATION,
@@ -1455,33 +1647,57 @@ export class ForkChoice implements IForkChoice {
1455
1647
  });
1456
1648
  }
1457
1649
 
1650
+ // For Gloas blocks, attestation index must be 0 or 1
1651
+ if (isGloasBlock(block) && attestationData.index !== 0 && attestationData.index !== 1) {
1652
+ throw new ForkChoiceError({
1653
+ code: ForkChoiceErrorCode.INVALID_ATTESTATION,
1654
+ err: {
1655
+ code: InvalidAttestationCode.INVALID_DATA_INDEX,
1656
+ index: attestationData.index,
1657
+ },
1658
+ });
1659
+ }
1660
+
1458
1661
  this.validatedAttestationDatas.add(attDataRoot);
1459
1662
  }
1460
1663
 
1461
1664
  /**
1462
1665
  * Add a validator's latest message to the tracked votes.
1463
1666
  * Always sync voteCurrentIndices and voteNextIndices so that it'll not throw in computeDeltas()
1667
+ *
1668
+ * Modified for Gloas to accept slot and payloadPresent.
1669
+ * Spec: gloas/fork-choice.md#modified-update_latest_messages
1670
+ *
1671
+ * For backward compatibility with Fulu (pre-Gloas):
1672
+ * - Accepts both epoch-derived and slot parameters
1673
+ * - payloadPresent defaults to true for Fulu (payloads embedded in blocks)
1464
1674
  */
1465
- private addLatestMessage(validatorIndex: ValidatorIndex, nextEpoch: Epoch, nextRoot: RootHex): void {
1675
+ private addLatestMessage(
1676
+ validatorIndex: ValidatorIndex,
1677
+ nextSlot: Slot,
1678
+ nextRoot: RootHex,
1679
+ nextPayloadStatus: PayloadStatus
1680
+ ): void {
1466
1681
  // should not happen, attestation is validated before this step
1467
- const nextIndex = this.protoArray.indices.get(nextRoot);
1682
+ // Get the node index for the voted block
1683
+ const nextIndex = this.protoArray.getNodeIndexByRootAndStatus(nextRoot, nextPayloadStatus);
1468
1684
  if (nextIndex === undefined) {
1469
- throw new Error(`Could not find proto index for nextRoot ${nextRoot}`);
1685
+ throw new Error(`Could not find proto index for nextRoot ${nextRoot} with payloadStatus ${nextPayloadStatus}`);
1470
1686
  }
1471
1687
 
1472
1688
  // ensure there is no undefined entries in Votes arrays
1473
- if (this.voteNextEpochs.length < validatorIndex + 1) {
1474
- for (let i = this.voteNextEpochs.length; i < validatorIndex + 1; i++) {
1475
- this.voteNextEpochs[i] = INIT_VOTE_EPOCH;
1689
+ if (this.voteNextSlots.length < validatorIndex + 1) {
1690
+ for (let i = this.voteNextSlots.length; i < validatorIndex + 1; i++) {
1691
+ this.voteNextSlots[i] = INIT_VOTE_SLOT;
1476
1692
  this.voteCurrentIndices[i] = this.voteNextIndices[i] = NULL_VOTE_INDEX;
1477
1693
  }
1478
1694
  }
1479
1695
 
1480
- const existingNextEpoch = this.voteNextEpochs[validatorIndex];
1481
- if (existingNextEpoch === INIT_VOTE_EPOCH || nextEpoch > existingNextEpoch) {
1696
+ const existingNextSlot = this.voteNextSlots[validatorIndex];
1697
+ if (existingNextSlot === INIT_VOTE_SLOT || computeEpochAtSlot(nextSlot) > computeEpochAtSlot(existingNextSlot)) {
1482
1698
  // nextIndex is transfered to currentIndex in computeDeltas()
1483
1699
  this.voteNextIndices[validatorIndex] = nextIndex;
1484
- this.voteNextEpochs[validatorIndex] = nextEpoch;
1700
+ this.voteNextSlots[validatorIndex] = nextSlot;
1485
1701
  }
1486
1702
  // else its an old vote, don't count it
1487
1703
  }
@@ -1493,18 +1709,17 @@ export class ForkChoice implements IForkChoice {
1493
1709
  private processAttestationQueue(): void {
1494
1710
  const currentSlot = this.fcStore.currentSlot;
1495
1711
  for (const [slot, byRoot] of this.queuedAttestations.entries()) {
1496
- const targetEpoch = computeEpochAtSlot(slot);
1497
1712
  if (slot < currentSlot) {
1498
1713
  this.queuedAttestations.delete(slot);
1499
- for (const [blockRoot, validatorIndices] of byRoot.entries()) {
1714
+ for (const [blockRoot, validatorVotes] of byRoot.entries()) {
1500
1715
  const blockRootHex = blockRoot;
1501
- for (const validatorIndex of validatorIndices) {
1716
+ for (const [validatorIndex, payloadStatus] of validatorVotes.entries()) {
1502
1717
  // equivocatingIndices was checked in onAttestation
1503
- this.addLatestMessage(validatorIndex, targetEpoch, blockRootHex);
1718
+ this.addLatestMessage(validatorIndex, slot, blockRootHex, payloadStatus);
1504
1719
  }
1505
1720
 
1506
1721
  if (slot === currentSlot - 1) {
1507
- this.queuedAttestationsPreviousSlot += validatorIndices.size;
1722
+ this.queuedAttestationsPreviousSlot += validatorVotes.size;
1508
1723
  }
1509
1724
  }
1510
1725
  } else {
@@ -1618,3 +1833,31 @@ export function getCommitteeFraction(
1618
1833
  const committeeWeight = Math.floor(justifiedTotalActiveBalanceByIncrement / config.slotsPerEpoch);
1619
1834
  return Math.floor((committeeWeight * config.committeePercent) / 100);
1620
1835
  }
1836
+
1837
+ /**
1838
+ * Get the payload status for a checkpoint.
1839
+ *
1840
+ * Pre-Gloas: always FULL (payload embedded in block)
1841
+ * Gloas: determined by state.execution_payload_availability
1842
+ *
1843
+ * @param state - The state to check execution_payload_availability
1844
+ * @param checkpointEpoch - The epoch of the checkpoint
1845
+ */
1846
+ export function getCheckpointPayloadStatus(state: CachedBeaconStateAllForks, checkpointEpoch: number): PayloadStatus {
1847
+ // Compute checkpoint slot first to determine the correct fork
1848
+ const checkpointSlot = computeStartSlotAtEpoch(checkpointEpoch);
1849
+ const fork = state.config.getForkSeq(checkpointSlot);
1850
+
1851
+ // Pre-Gloas: always FULL
1852
+ if (fork < ForkSeq.gloas) {
1853
+ return PayloadStatus.FULL;
1854
+ }
1855
+
1856
+ // For Gloas, check state.execution_payload_availability
1857
+ // - For non-skipped slots at checkpoint: returns false (EMPTY) since payload hasn't arrived yet
1858
+ // - For skipped slots at checkpoint: returns the actual availability status from state
1859
+ const gloasState = state as CachedBeaconStateGloas;
1860
+ const payloadAvailable = gloasState.executionPayloadAvailability.get(checkpointSlot % SLOTS_PER_HISTORICAL_ROOT);
1861
+
1862
+ return payloadAvailable ? PayloadStatus.FULL : PayloadStatus.EMPTY;
1863
+ }