@lodestar/fork-choice 1.43.0-dev.ade910fc78 → 1.43.0-dev.b05ea98d04

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.
@@ -24,16 +24,15 @@ export type VoteIndex = number;
24
24
  * - Syncing: EL is syncing, payload validity unknown (optimistic sync)
25
25
  * - PreMerge: Block is from before The Merge, no execution payload exists
26
26
  * - Invalid: Execution payload was invalidated by the EL (post-import status)
27
- * - PayloadSeparated: Gloas beacon block without embedded execution payload.
28
- * The execution payload arrives separately via SignedExecutionPayloadEnvelope.
29
- * Gloas blocks WITH execution payload (FULL variant) use Valid/Invalid/Syncing.
27
+ *
28
+ * For gloas blocks the PENDING/EMPTY variants inherit `executionStatus` from the parent's chain
29
+ * (Valid/Syncing/PreMerge); the FULL variant carries the EL response for this block's own payload.
30
30
  */
31
31
  export enum ExecutionStatus {
32
32
  Valid = "Valid",
33
33
  Syncing = "Syncing",
34
34
  PreMerge = "PreMerge",
35
35
  Invalid = "Invalid",
36
- PayloadSeparated = "PayloadSeparated",
37
36
  }
38
37
 
39
38
  /**
@@ -61,19 +60,21 @@ export type LVHInvalidResponse = {
61
60
  executionStatus: ExecutionStatus.Invalid;
62
61
  latestValidExecHash: RootHex | null;
63
62
  invalidateFromParentBlockRoot: RootHex;
63
+ // EL block hash from invalid block's bid (gloas) or payload's parentHash (pre-gloas).
64
+ // Disambiguates which variant of the parent (FULL vs EMPTY) to invalidate from.
65
+ invalidateFromParentBlockHash: RootHex;
64
66
  };
65
67
  export type LVHExecResponse = LVHValidResponse | LVHInvalidResponse;
66
68
 
67
69
  /**
68
70
  * Any execution status that is not definitively invalid.
69
- * Pre-Gloas: Valid | Syncing | PreMerge
70
- * Post-Gloas: execution status must be PayloadSeparated (beacon block imported before its payload arrives via SignedExecutionPayloadEnvelope)
71
+ * Valid | Syncing | PreMerge
71
72
  */
72
73
  export type BlockExecutionStatus = Exclude<ExecutionStatus, ExecutionStatus.Invalid>;
73
74
 
74
75
  /**
75
76
  * Execution status for a block whose execution payload is present and has been submitted to the EL.
76
- * Used post-Gloas when transitioning a PayloadSeparated block to FULL via onExecutionPayload().
77
+ * Used post-Gloas when transitioning a PENDING block to FULL via onExecutionPayload().
77
78
  */
78
79
  export type PayloadExecutionStatus = ExecutionStatus.Valid | ExecutionStatus.Syncing;
79
80
 
@@ -1,6 +1,6 @@
1
1
  import {BitArray} from "@chainsafe/ssz";
2
2
  import {GENESIS_EPOCH, PTC_SIZE} from "@lodestar/params";
3
- import {computeEpochAtSlot, computeStartSlotAtEpoch} from "@lodestar/state-transition";
3
+ import {DataAvailabilityStatus, computeEpochAtSlot, computeStartSlotAtEpoch} from "@lodestar/state-transition";
4
4
  import {Epoch, RootHex, Slot} from "@lodestar/types";
5
5
  import {bitCount, toRootHex} from "@lodestar/utils";
6
6
  import {ForkChoiceError, ForkChoiceErrorCode} from "../forkChoice/errors.js";
@@ -109,13 +109,6 @@ export class ProtoArray {
109
109
  null
110
110
  );
111
111
 
112
- // Anchor block PTC votes must be all-true per spec get_forkchoice_store:
113
- // payload_timeliness_vote={anchor_root: Vector[boolean, PTC_SIZE](True for _ in range(PTC_SIZE))}
114
- // Spec: https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.4/specs/gloas/fork-choice.md#modified-get_forkchoice_store
115
- if (protoArray.ptcVotes.has(block.blockRoot)) {
116
- protoArray.ptcVotes.set(block.blockRoot, BitArray.fromBoolArray(Array.from({length: PTC_SIZE}, () => true)));
117
- }
118
-
119
112
  return protoArray;
120
113
  }
121
114
 
@@ -221,6 +214,11 @@ export class ProtoArray {
221
214
  return PayloadStatus.FULL;
222
215
  }
223
216
 
217
+ // Genesis block has no parent in the proto array
218
+ if (block.parentRoot === HEX_ZERO_HASH) {
219
+ return PayloadStatus.FULL;
220
+ }
221
+
224
222
  const parentBlock = this.getBlockHexAndBlockHash(block.parentRoot, parentBlockHash);
225
223
  if (parentBlock == null) {
226
224
  throw new ProtoArrayError({
@@ -259,38 +257,43 @@ export class ProtoArray {
259
257
  }
260
258
 
261
259
  /**
262
- * Returns an EMPTY or FULL `ProtoBlock` that has matching block root and block hash
260
+ * Returns the node index of an EMPTY or FULL variant matching block root and block hash.
261
+ * Pre-gloas: checks the single variant. Post-gloas: prefers FULL, falls back to EMPTY.
263
262
  */
264
- getBlockHexAndBlockHash(blockRoot: RootHex, blockHash: RootHex): ProtoBlock | null {
263
+ getNodeIndexByRootAndBlockHash(blockRoot: RootHex, blockHash: RootHex): number | undefined {
265
264
  const variantIndices = this.indices.get(blockRoot);
266
265
  if (variantIndices === undefined) {
267
- return null;
266
+ return undefined;
268
267
  }
269
268
 
270
269
  // Pre-Gloas
271
270
  if (!Array.isArray(variantIndices)) {
272
- const node = this.nodes[variantIndices];
273
- return node.executionPayloadBlockHash === blockHash ? node : null;
271
+ return this.nodes[variantIndices].executionPayloadBlockHash === blockHash ? variantIndices : undefined;
274
272
  }
275
273
 
276
- // Post-Gloas, check empty and full variants
274
+ // Post-Gloas, prefer FULL then EMPTY
277
275
  const fullNodeIndex = variantIndices[PayloadStatus.FULL];
278
- if (fullNodeIndex !== undefined) {
279
- const fullNode = this.nodes[fullNodeIndex];
280
- if (fullNode && fullNode.executionPayloadBlockHash === blockHash) {
281
- return fullNode;
282
- }
276
+ if (fullNodeIndex !== undefined && this.nodes[fullNodeIndex].executionPayloadBlockHash === blockHash) {
277
+ return fullNodeIndex;
283
278
  }
284
279
 
285
- const emptyNode = this.nodes[variantIndices[PayloadStatus.EMPTY]];
286
- if (emptyNode && emptyNode.executionPayloadBlockHash === blockHash) {
287
- return emptyNode;
280
+ const emptyNodeIndex = variantIndices[PayloadStatus.EMPTY];
281
+ if (this.nodes[emptyNodeIndex].executionPayloadBlockHash === blockHash) {
282
+ return emptyNodeIndex;
288
283
  }
289
284
 
290
285
  // PENDING is the same to EMPTY so not likely we can return it
291
286
  // also it's only specific for fork-choice
292
287
 
293
- return null;
288
+ return undefined;
289
+ }
290
+
291
+ /**
292
+ * Returns an EMPTY or FULL `ProtoBlock` that has matching block root and block hash
293
+ */
294
+ getBlockHexAndBlockHash(blockRoot: RootHex, blockHash: RootHex): ProtoBlock | null {
295
+ const idx = this.getNodeIndexByRootAndBlockHash(blockRoot, blockHash);
296
+ return idx !== undefined ? this.nodes[idx] : null;
294
297
  }
295
298
 
296
299
  /**
@@ -555,9 +558,9 @@ export class ProtoArray {
555
558
  currentSlot: Slot,
556
559
  executionPayloadBlockHash: RootHex,
557
560
  executionPayloadNumber: number,
558
- executionPayloadStateRoot: RootHex,
559
561
  proposerBoostRoot: RootHex | null,
560
- executionStatus: PayloadExecutionStatus
562
+ executionStatus: PayloadExecutionStatus,
563
+ dataAvailabilityStatus: DataAvailabilityStatus
561
564
  ): void {
562
565
  // First check if block exists
563
566
  const variants = this.indices.get(blockRoot);
@@ -599,7 +602,7 @@ export class ProtoArray {
599
602
  });
600
603
  }
601
604
 
602
- // Create FULL variant as a child of PENDING (sibling to EMPTY)
605
+ // Create FULL variant as a child of PENDING (sibling to EMPTY).
603
606
  const fullNode: ProtoNode = {
604
607
  ...pendingNode,
605
608
  parent: pendingIndex, // Points to own PENDING (same as EMPTY)
@@ -607,11 +610,10 @@ export class ProtoArray {
607
610
  weight: 0,
608
611
  bestChild: undefined,
609
612
  bestDescendant: undefined,
610
- // TODO GLOAS: handle optimistic sync
611
613
  executionStatus,
612
614
  executionPayloadBlockHash,
613
615
  executionPayloadNumber,
614
- stateRoot: executionPayloadStateRoot,
616
+ dataAvailabilityStatus,
615
617
  };
616
618
 
617
619
  const fullIndex = this.nodes.length;
@@ -620,6 +622,12 @@ export class ProtoArray {
620
622
  // Add FULL variant to the indices array
621
623
  variants[PayloadStatus.FULL] = fullIndex;
622
624
 
625
+ if (executionStatus === ExecutionStatus.Valid) {
626
+ // Walk up from FULL's parent (its own PENDING). FULL is already Valid; the loop breaks
627
+ // immediately if we start at FULL. Same pattern as pre-gloas onBlock at line ~533.
628
+ this.propagateValidExecutionStatusByIndex(pendingIndex);
629
+ }
630
+
623
631
  // Update bestChild for PENDING node (may now prefer FULL over EMPTY)
624
632
  this.maybeUpdateBestChildAndDescendant(pendingIndex, fullIndex, currentSlot, proposerBoostRoot);
625
633
  }
@@ -648,6 +656,16 @@ export class ProtoArray {
648
656
  }
649
657
  }
650
658
 
659
+ getPTCVotes(blockRootHex: RootHex): BitArray | null {
660
+ const votes = this.ptcVotes.get(blockRootHex);
661
+ if (votes === undefined) {
662
+ // Block not found or not a Gloas block
663
+ return null;
664
+ }
665
+
666
+ return votes;
667
+ }
668
+
651
669
  /**
652
670
  * Check if execution payload for a block is timely
653
671
  * Spec: gloas/fork-choice.md#new-is_payload_timely
@@ -690,7 +708,7 @@ export class ProtoArray {
690
708
  * Determine if we should extend the payload (prefer FULL over EMPTY)
691
709
  * Spec: gloas/fork-choice.md#new-should_extend_payload
692
710
  *
693
- * Returns true if:
711
+ * Returns true if payload is verified (FULL variant exists) AND:
694
712
  * 1. Payload is timely, OR
695
713
  * 2. No proposer boost root (empty/zero hash), OR
696
714
  * 3. Proposer boost root's parent is not this block, OR
@@ -700,6 +718,10 @@ export class ProtoArray {
700
718
  * @param proposerBoostRoot - Current proposer boost root (from ForkChoice)
701
719
  */
702
720
  shouldExtendPayload(blockRoot: RootHex, proposerBoostRoot: RootHex | null): boolean {
721
+ if (!this.hasPayload(blockRoot)) {
722
+ return false;
723
+ }
724
+
703
725
  // Condition 1: Payload is timely
704
726
  if (this.isPayloadTimely(blockRoot)) {
705
727
  return true;
@@ -780,11 +802,15 @@ export class ProtoArray {
780
802
  // Mark chain ii) as Invalid if LVH is found and non null, else only invalidate invalid_payload
781
803
  // if its in fcU.
782
804
  //
783
- const {invalidateFromParentBlockRoot, latestValidExecHash} = execResponse;
784
- // TODO GLOAS: verify if getting the default/canonical node index is correct here
785
- const invalidateFromParentIndex = this.getDefaultNodeIndex(invalidateFromParentBlockRoot);
805
+ const {invalidateFromParentBlockRoot, invalidateFromParentBlockHash, latestValidExecHash} = execResponse;
806
+ const invalidateFromParentIndex = this.getNodeIndexByRootAndBlockHash(
807
+ invalidateFromParentBlockRoot,
808
+ invalidateFromParentBlockHash
809
+ );
786
810
  if (invalidateFromParentIndex === undefined) {
787
- throw Error(`Unable to find invalidateFromParentBlockRoot=${invalidateFromParentBlockRoot} in forkChoice`);
811
+ throw Error(
812
+ `Unable to find invalidateFromParentBlockRoot=${invalidateFromParentBlockRoot} invalidateFromParentBlockHash=${invalidateFromParentBlockHash} in forkChoice`
813
+ );
788
814
  }
789
815
  const latestValidHashIndex =
790
816
  latestValidExecHash !== null ? this.getNodeIndexFromLVH(latestValidExecHash, invalidateFromParentIndex) : null;
@@ -820,12 +846,6 @@ export class ProtoArray {
820
846
  if (node.executionStatus === ExecutionStatus.PreMerge || node.executionStatus === ExecutionStatus.Valid) {
821
847
  break;
822
848
  }
823
- // If PayloadSeparated, that means the node is either PENDING or EMPTY, there could be
824
- // some ancestor still has syncing status.
825
- if (node.executionStatus === ExecutionStatus.PayloadSeparated) {
826
- nodeIndex = node.parent;
827
- continue;
828
- }
829
849
  this.validateNodeByIndex(nodeIndex);
830
850
  nodeIndex = node.parent;
831
851
  }
@@ -923,6 +943,13 @@ export class ProtoArray {
923
943
  invalidNode.executionStatus = ExecutionStatus.Invalid;
924
944
  invalidNode.bestChild = undefined;
925
945
  invalidNode.bestDescendant = undefined;
946
+ // Gloas: PENDING and sibling EMPTY share chain status, flip together
947
+ if (invalidNode.payloadStatus === PayloadStatus.PENDING) {
948
+ const variants = this.indices.get(invalidNode.blockRoot);
949
+ if (Array.isArray(variants)) {
950
+ this.invalidateNodeByIndex(variants[PayloadStatus.EMPTY]);
951
+ }
952
+ }
926
953
 
927
954
  return invalidNode;
928
955
  }
@@ -944,6 +971,13 @@ export class ProtoArray {
944
971
  if (validNode.executionStatus === ExecutionStatus.Syncing) {
945
972
  validNode.executionStatus = ExecutionStatus.Valid;
946
973
  }
974
+ // Gloas: PENDING and sibling EMPTY share chain status, flip together
975
+ if (validNode.payloadStatus === PayloadStatus.PENDING) {
976
+ const variants = this.indices.get(validNode.blockRoot);
977
+ if (Array.isArray(variants)) {
978
+ this.validateNodeByIndex(variants[PayloadStatus.EMPTY]);
979
+ }
980
+ }
947
981
  return validNode;
948
982
  }
949
983
 
@@ -1655,10 +1689,9 @@ export class ProtoArray {
1655
1689
  const ancestors: ProtoNode[] = [];
1656
1690
  const nonAncestors: ProtoNode[] = [];
1657
1691
 
1658
- // Include starting node if it's not PENDING (i.e., pre-Gloas or EMPTY/FULL variant post-Gloas)
1659
- if (node.payloadStatus !== PayloadStatus.PENDING) {
1660
- ancestors.push(node);
1661
- }
1692
+ // caller of this method may pass default status
1693
+ // this is the only node that we accept PENDING
1694
+ ancestors.push(node);
1662
1695
 
1663
1696
  let nodeIndex = startIndex;
1664
1697
  while (node.parent !== undefined) {