@lodestar/fork-choice 1.45.0-dev.fadf0fbb1f → 1.46.0-dev.8fc4e297d2

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 (35) hide show
  1. package/lib/forkChoice/fastConfirmation/metrics.d.ts +1 -0
  2. package/lib/forkChoice/fastConfirmation/metrics.d.ts.map +1 -1
  3. package/lib/forkChoice/fastConfirmation/metrics.js +4 -0
  4. package/lib/forkChoice/fastConfirmation/metrics.js.map +1 -1
  5. package/lib/forkChoice/forkChoice.d.ts +46 -7
  6. package/lib/forkChoice/forkChoice.d.ts.map +1 -1
  7. package/lib/forkChoice/forkChoice.js +166 -49
  8. package/lib/forkChoice/forkChoice.js.map +1 -1
  9. package/lib/forkChoice/interface.d.ts +14 -2
  10. package/lib/forkChoice/interface.d.ts.map +1 -1
  11. package/lib/forkChoice/interface.js.map +1 -1
  12. package/lib/index.d.ts +1 -1
  13. package/lib/index.d.ts.map +1 -1
  14. package/lib/index.js +1 -1
  15. package/lib/index.js.map +1 -1
  16. package/lib/metrics.d.ts +1 -0
  17. package/lib/metrics.d.ts.map +1 -1
  18. package/lib/protoArray/computeDeltas.d.ts +3 -3
  19. package/lib/protoArray/computeDeltas.d.ts.map +1 -1
  20. package/lib/protoArray/computeDeltas.js +12 -12
  21. package/lib/protoArray/computeDeltas.js.map +1 -1
  22. package/lib/protoArray/interface.d.ts +6 -0
  23. package/lib/protoArray/interface.d.ts.map +1 -1
  24. package/lib/protoArray/protoArray.d.ts +22 -2
  25. package/lib/protoArray/protoArray.d.ts.map +1 -1
  26. package/lib/protoArray/protoArray.js +93 -13
  27. package/lib/protoArray/protoArray.js.map +1 -1
  28. package/package.json +9 -9
  29. package/src/forkChoice/fastConfirmation/metrics.ts +4 -0
  30. package/src/forkChoice/forkChoice.ts +181 -50
  31. package/src/forkChoice/interface.ts +11 -12
  32. package/src/index.ts +1 -1
  33. package/src/protoArray/computeDeltas.ts +13 -13
  34. package/src/protoArray/interface.ts +6 -0
  35. package/src/protoArray/protoArray.ts +102 -17
@@ -27,6 +27,12 @@ const PAYLOAD_TIMELY_THRESHOLD = Math.floor(PTC_SIZE / 2);
27
27
  */
28
28
  const DATA_AVAILABILITY_TIMELY_THRESHOLD = Math.floor(PTC_SIZE / 2);
29
29
 
30
+ /**
31
+ * Proposer boost deltas, back-propagated to the boosted node's ancestors in applyScoreChanges().
32
+ * Reuse the array to avoid memory reallocation and gc, as computeDeltas does for attestation deltas.
33
+ */
34
+ const boostDeltas = new Array<number>();
35
+
30
36
  /**
31
37
  * popcount(attended AND NOT yes) — explicit False-vote count.
32
38
  * Excludes PTC members who didn't attest (the None state).
@@ -348,7 +354,7 @@ export class ProtoArray {
348
354
  * - If required, update the parents best-descendant with the current node or its best-descendant.
349
355
  */
350
356
  applyScoreChanges({
351
- deltas,
357
+ attestationDeltas,
352
358
  proposerBoost,
353
359
  justifiedEpoch,
354
360
  justifiedRoot,
@@ -356,7 +362,7 @@ export class ProtoArray {
356
362
  finalizedRoot,
357
363
  currentSlot,
358
364
  }: {
359
- deltas: number[];
365
+ attestationDeltas: number[];
360
366
  proposerBoost: ProposerBoost | null;
361
367
  justifiedEpoch: Epoch;
362
368
  justifiedRoot: RootHex;
@@ -364,14 +370,17 @@ export class ProtoArray {
364
370
  finalizedRoot: RootHex;
365
371
  currentSlot: Slot;
366
372
  }): void {
367
- if (deltas.length !== this.nodes.length) {
373
+ if (attestationDeltas.length !== this.nodes.length) {
368
374
  throw new ProtoArrayError({
369
375
  code: ProtoArrayErrorCode.INVALID_DELTA_LEN,
370
- deltas: deltas.length,
376
+ deltas: attestationDeltas.length,
371
377
  indices: this.nodes.length,
372
378
  });
373
379
  }
374
380
 
381
+ boostDeltas.length = this.nodes.length;
382
+ boostDeltas.fill(0);
383
+
375
384
  if (
376
385
  justifiedEpoch !== this.justifiedEpoch ||
377
386
  finalizedEpoch !== this.finalizedEpoch ||
@@ -416,18 +425,22 @@ export class ProtoArray {
416
425
  // If this node's execution status has been marked invalid, then the weight of the node
417
426
  // needs to be taken out of consideration after which the node weight will become 0
418
427
  // for subsequent iterations of applyScoreChanges
419
- const nodeDelta =
420
- node.executionStatus === ExecutionStatus.Invalid
421
- ? -node.weight
422
- : deltas[nodeIndex] + currentBoost - previousBoost;
423
-
424
- // Apply the delta to the node
425
- node.weight += nodeDelta;
426
-
427
- // Update the parent delta (if any)
428
+ const isInvalid = node.executionStatus === ExecutionStatus.Invalid;
429
+ const attestationDelta = isInvalid ? -node.attestationScore : attestationDeltas[nodeIndex];
430
+ const boostDelta = isInvalid
431
+ ? // old boost = weight - attestationScore
432
+ -(node.weight - node.attestationScore)
433
+ : boostDeltas[nodeIndex] + currentBoost - previousBoost;
434
+
435
+ // Apply the deltas to the node. Their sum is the node's total delta, so weight is unaffected
436
+ // by tracking the two scores apart.
437
+ node.attestationScore += attestationDelta;
438
+ node.weight += attestationDelta + boostDelta;
439
+
440
+ // Update the parent deltas (if any)
428
441
  const parentIndex = node.parent;
429
442
  if (parentIndex !== undefined) {
430
- const parentDelta = deltas[parentIndex];
443
+ const parentDelta = attestationDeltas[parentIndex];
431
444
  if (parentDelta === undefined) {
432
445
  throw new ProtoArrayError({
433
446
  code: ProtoArrayErrorCode.INVALID_PARENT_DELTA,
@@ -435,8 +448,9 @@ export class ProtoArray {
435
448
  });
436
449
  }
437
450
 
438
- // back-propagate the nodes delta to its parent
439
- deltas[parentIndex] += nodeDelta;
451
+ // back-propagate the nodes deltas to its parent
452
+ attestationDeltas[parentIndex] += attestationDelta;
453
+ boostDeltas[parentIndex] += boostDelta;
440
454
  }
441
455
  }
442
456
 
@@ -514,6 +528,7 @@ export class ProtoArray {
514
528
  parent: parentIndex, // Points to parent's EMPTY/FULL or FULL (for transition)
515
529
  payloadStatus: PayloadStatus.PENDING,
516
530
  weight: 0,
531
+ attestationScore: 0,
517
532
  bestChild: undefined,
518
533
  bestDescendant: undefined,
519
534
  };
@@ -527,6 +542,7 @@ export class ProtoArray {
527
542
  parent: pendingIndex, // Points to own PENDING
528
543
  payloadStatus: PayloadStatus.EMPTY,
529
544
  weight: 0,
545
+ attestationScore: 0,
530
546
  bestChild: undefined,
531
547
  bestDescendant: undefined,
532
548
  };
@@ -562,6 +578,7 @@ export class ProtoArray {
562
578
  parent: this.getNodeIndexByRootAndStatus(block.parentRoot, PayloadStatus.FULL),
563
579
  payloadStatus: PayloadStatus.FULL,
564
580
  weight: 0,
581
+ attestationScore: 0,
565
582
  bestChild: undefined,
566
583
  bestDescendant: undefined,
567
584
  };
@@ -647,6 +664,7 @@ export class ProtoArray {
647
664
  parent: pendingIndex, // Points to own PENDING (same as EMPTY)
648
665
  payloadStatus: PayloadStatus.FULL,
649
666
  weight: 0,
667
+ attestationScore: 0,
650
668
  bestChild: undefined,
651
669
  bestDescendant: undefined,
652
670
  executionStatus,
@@ -672,6 +690,28 @@ export class ProtoArray {
672
690
  this.maybeUpdateBestChildAndDescendant(pendingIndex, fullIndex, currentSlot, proposerBoostRoot);
673
691
  }
674
692
 
693
+ /**
694
+ * Count gloas blocks with fromSlot <= slot <= toSlot and how many of them have a revealed
695
+ * payload (FULL variant exists). Used by the builder circuit breaker.
696
+ */
697
+ getPayloadRevealCounts(fromSlot: Slot, toSlot: Slot): {blocksPresent: number; payloadsRevealed: number} {
698
+ let blocksPresent = 0;
699
+ let payloadsRevealed = 0;
700
+ // Full scan, nodes are in import order not slot order (an old block can be imported after newer
701
+ // ones during sync or reorg resolution), so we cannot stop early on an out-of-window slot
702
+ for (const node of this.nodes) {
703
+ // Count each gloas block once via its PENDING variant, pre-gloas nodes are FULL only
704
+ if (node.slot < fromSlot || node.slot > toSlot || node.payloadStatus !== PayloadStatus.PENDING) {
705
+ continue;
706
+ }
707
+ blocksPresent++;
708
+ if (this.hasPayload(node.blockRoot)) {
709
+ payloadsRevealed++;
710
+ }
711
+ }
712
+ return {blocksPresent, payloadsRevealed};
713
+ }
714
+
675
715
  /**
676
716
  * Update PTC votes for multiple validators attesting to a block
677
717
  * Spec: gloas/fork-choice.md#new-notify_ptc_messages
@@ -1030,7 +1070,7 @@ export class ProtoArray {
1030
1070
 
1031
1071
  // update the forkchoice as the invalidation can change the entire forkchoice DAG
1032
1072
  this.applyScoreChanges({
1033
- deltas: Array.from({length: this.nodes.length}, () => 0),
1073
+ attestationDeltas: Array.from({length: this.nodes.length}, () => 0),
1034
1074
  proposerBoost: this.previousProposerBoost,
1035
1075
  justifiedEpoch: this.justifiedEpoch,
1036
1076
  justifiedRoot: this.justifiedRoot,
@@ -1541,6 +1581,38 @@ export class ProtoArray {
1541
1581
  return correctJustified && correctFinalized;
1542
1582
  }
1543
1583
 
1584
+ /** Weights are in EFFECTIVE_BALANCE_INCREMENT units (NOT Gwei); callers scale as needed. */
1585
+ getViableHeads(currentSlot: Slot): {root: RootHex; payloadStatus: PayloadStatus; weight: number}[] {
1586
+ // Mirror the spec's `get_filtered_block_tree`, which is rooted at the store's justified
1587
+ // checkpoint: a viable head is a leaf (no viable descendant, i.e. `bestChild === undefined`)
1588
+ // that descends from the justified checkpoint block AND is itself viable for head. Iterating
1589
+ // all nodes without the justified-descendant filter would wrongly include FFG-viable leaves
1590
+ // that hang off the finalized checkpoint on a branch not under the current justified checkpoint.
1591
+ const justifiedVariant = this.getDefaultVariant(this.justifiedRoot);
1592
+ // Gloas payload-status variants of one blockRoot are distinct nodes in the spec's filtered
1593
+ // tree, identified by (root, payload_status, weight) — emit one entry per variant.
1594
+ const heads: {root: RootHex; payloadStatus: PayloadStatus; weight: number}[] = [];
1595
+ for (const node of this.nodes) {
1596
+ if (node.bestChild !== undefined || !this.nodeIsViableForHead(node, currentSlot)) {
1597
+ continue;
1598
+ }
1599
+ if (this.justifiedEpoch !== GENESIS_EPOCH) {
1600
+ // Same-root short-circuit: every payload-status variant of the justified block itself is
1601
+ // in the filtered tree, but `isDescendant` from the default (PENDING) variant does not
1602
+ // reach the sibling EMPTY/FULL variants of the same root.
1603
+ const descendsFromJustified =
1604
+ node.blockRoot === this.justifiedRoot ||
1605
+ (justifiedVariant !== undefined &&
1606
+ this.isDescendant(this.justifiedRoot, justifiedVariant, node.blockRoot, node.payloadStatus));
1607
+ if (!descendsFromJustified) {
1608
+ continue;
1609
+ }
1610
+ }
1611
+ heads.push({root: node.blockRoot, payloadStatus: node.payloadStatus, weight: node.weight});
1612
+ }
1613
+ return heads;
1614
+ }
1615
+
1544
1616
  /**
1545
1617
  * Return `true` if `node` is equal to or a descendant of the finalized node.
1546
1618
  * This function helps improve performance of nodeIsViableForHead a lot by avoiding
@@ -1898,6 +1970,19 @@ export class ProtoArray {
1898
1970
  return this.getNodeByIndex(blockIndex);
1899
1971
  }
1900
1972
 
1973
+ /**
1974
+ * Return ProtoNode for the default/canonical variant in a single hash lookup
1975
+ * - Pre-Gloas blocks: the FULL variant
1976
+ * - Gloas blocks: the PENDING variant
1977
+ */
1978
+ getNodeDefaultStatus(blockRoot: RootHex): ProtoNode | undefined {
1979
+ const nodeIndex = this.getDefaultNodeIndex(blockRoot);
1980
+ if (nodeIndex === undefined) {
1981
+ return undefined;
1982
+ }
1983
+ return this.getNodeByIndex(nodeIndex);
1984
+ }
1985
+
1901
1986
  /**
1902
1987
  * Return MUTABLE ProtoBlock for blockRoot with explicit payload status
1903
1988
  *