@lodestar/fork-choice 2.1.0-dev.2c328e4c17 → 2.1.0-dev.3a93673fb5

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.
@@ -73,7 +73,6 @@ import {CheckpointWithHex, IForkChoiceStore, JustifiedBalances, toCheckpointWith
73
73
  export type ForkChoiceOpts = {
74
74
  proposerBoost?: boolean;
75
75
  proposerBoostReorg?: boolean;
76
- computeUnrealized?: boolean;
77
76
  fastConfirmation?: boolean;
78
77
  };
79
78
 
@@ -882,33 +881,28 @@ export class ForkChoice implements IForkChoice {
882
881
  // performing optimally.
883
882
  let unrealizedJustifiedCheckpoint: CheckpointWithHex;
884
883
  let unrealizedFinalizedCheckpoint: CheckpointWithHex;
885
- if (this.opts?.computeUnrealized) {
886
- if (
887
- block.body.proposerSlashings.length === 0 &&
888
- block.body.attesterSlashings.length === 0 &&
889
- parentBlock.unrealizedJustifiedEpoch === blockEpoch &&
890
- parentBlock.unrealizedFinalizedEpoch + 1 >= blockEpoch
891
- ) {
892
- // reuse from parent, happens at 1/3 last blocks of epoch as monitored in mainnet
893
- unrealizedJustifiedCheckpoint = {
894
- epoch: parentBlock.unrealizedJustifiedEpoch,
895
- root: fromHex(parentBlock.unrealizedJustifiedRoot),
896
- rootHex: parentBlock.unrealizedJustifiedRoot,
897
- };
898
- unrealizedFinalizedCheckpoint = {
899
- epoch: parentBlock.unrealizedFinalizedEpoch,
900
- root: fromHex(parentBlock.unrealizedFinalizedRoot),
901
- rootHex: parentBlock.unrealizedFinalizedRoot,
902
- };
903
- } else {
904
- // compute new, happens 2/3 first blocks of epoch as monitored in mainnet
905
- const unrealized = state.computeUnrealizedCheckpoints();
906
- unrealizedJustifiedCheckpoint = toCheckpointWithHex(unrealized.justifiedCheckpoint);
907
- unrealizedFinalizedCheckpoint = toCheckpointWithHex(unrealized.finalizedCheckpoint);
908
- }
884
+ if (
885
+ block.body.proposerSlashings.length === 0 &&
886
+ block.body.attesterSlashings.length === 0 &&
887
+ parentBlock.unrealizedJustifiedEpoch === blockEpoch &&
888
+ parentBlock.unrealizedFinalizedEpoch + 1 >= blockEpoch
889
+ ) {
890
+ // reuse from parent, happens at 1/3 last blocks of epoch as monitored in mainnet
891
+ unrealizedJustifiedCheckpoint = {
892
+ epoch: parentBlock.unrealizedJustifiedEpoch,
893
+ root: fromHex(parentBlock.unrealizedJustifiedRoot),
894
+ rootHex: parentBlock.unrealizedJustifiedRoot,
895
+ };
896
+ unrealizedFinalizedCheckpoint = {
897
+ epoch: parentBlock.unrealizedFinalizedEpoch,
898
+ root: fromHex(parentBlock.unrealizedFinalizedRoot),
899
+ rootHex: parentBlock.unrealizedFinalizedRoot,
900
+ };
909
901
  } else {
910
- unrealizedJustifiedCheckpoint = justifiedCheckpoint;
911
- unrealizedFinalizedCheckpoint = finalizedCheckpoint;
902
+ // compute new, happens 2/3 first blocks of epoch as monitored in mainnet
903
+ const unrealized = state.computeUnrealizedCheckpoints();
904
+ unrealizedJustifiedCheckpoint = toCheckpointWithHex(unrealized.justifiedCheckpoint);
905
+ unrealizedFinalizedCheckpoint = toCheckpointWithHex(unrealized.finalizedCheckpoint);
912
906
  }
913
907
 
914
908
  const targetSlot = computeStartSlotAtEpoch(blockEpoch);
@@ -1740,7 +1734,7 @@ export class ForkChoice implements IForkChoice {
1740
1734
  * Return true if the block is "weak" ie. its weight is below REORG_HEAD_WEIGHT_THRESHOLD of the
1741
1735
  * total attester weight per slot.
1742
1736
  *
1743
- * https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.11/specs/phase0/fork-choice.md#is_head_weak
1737
+ * https://github.com/ethereum/consensus-specs/blob/v1.7.0-beta.5/specs/phase0/fork-choice.md#is_head_weak
1744
1738
  * https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.11/specs/gloas/fork-choice.md#modified-is_head_weak
1745
1739
  */
1746
1740
  private isHeadWeak(blockRoot: RootHex): boolean {
@@ -1759,10 +1753,6 @@ export class ForkChoice implements IForkChoice {
1759
1753
  committeePercent: this.config.REORG_HEAD_WEIGHT_THRESHOLD,
1760
1754
  });
1761
1755
 
1762
- if (!isForkPostGloas(this.config.getForkName(node.slot))) {
1763
- return node.weight < reorgThreshold;
1764
- }
1765
-
1766
1756
  let headWeight = node.attestationScore;
1767
1757
 
1768
1758
  const {equivocatingIndices} = this.fcStore;
@@ -1803,7 +1793,7 @@ export class ForkChoice implements IForkChoice {
1803
1793
  * Return true if the parent block is "strong" ie. its weight exceeds REORG_PARENT_WEIGHT_THRESHOLD
1804
1794
  * of the total attester weight per slot.
1805
1795
  *
1806
- * https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.12/specs/phase0/fork-choice.md#is_parent_strong
1796
+ * https://github.com/ethereum/consensus-specs/blob/v1.7.0-beta.5/specs/phase0/fork-choice.md#is_parent_strong
1807
1797
  * https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.12/specs/gloas/fork-choice.md#modified-is_parent_strong
1808
1798
  */
1809
1799
  private isParentStrong(parentRoot: RootHex): boolean {
@@ -1818,9 +1808,7 @@ export class ForkChoice implements IForkChoice {
1818
1808
  committeePercent: this.config.REORG_PARENT_WEIGHT_THRESHOLD,
1819
1809
  });
1820
1810
 
1821
- // pre-gloas uses get_weight() (boost-inclusive), gloas uses get_attestation_score() (boost-excluded)
1822
- const parentWeight = isForkPostGloas(this.config.getForkName(node.slot)) ? node.attestationScore : node.weight;
1823
- return parentWeight > parentThreshold;
1811
+ return node.attestationScore > parentThreshold;
1824
1812
  }
1825
1813
 
1826
1814
  /**
@@ -2226,25 +2214,25 @@ export class ForkChoice implements IForkChoice {
2226
2214
 
2227
2215
  /**
2228
2216
  * Add a validator's latest message to the tracked votes.
2229
- * Always sync voteCurrentIndices and voteNextIndices so that it'll not throw in computeDeltas()
2230
- *
2231
- * Modified for Gloas to accept slot and payloadPresent.
2232
- * Spec: gloas/fork-choice.md#modified-update_latest_messages
2217
+ * Keep voteCurrentIndices and voteNextIndices the same length, as required by computeDeltas().
2233
2218
  *
2234
- * For backward compatibility with Fulu (pre-Gloas):
2235
- * - Accepts both epoch-derived and slot parameters
2236
- * - payloadPresent defaults to true for Fulu (payloads embedded in blocks)
2219
+ * Callers pass the attestation slot on every fork. Before Gloas, votes are compared by epoch
2220
+ * and the caller passes FULL (Fulu embeds the payload in the block). From Gloas, votes are
2221
+ * compared by slot and the caller passes the payload status supported by the attestation.
2222
+ * https://github.com/ethereum/consensus-specs/blob/v1.7.0-beta.5/specs/gloas/fork-choice.md#modified-update_latest_messages
2237
2223
  */
2238
2224
  private addLatestMessage(
2239
2225
  validatorIndex: ValidatorIndex,
2240
2226
  nextSlot: Slot,
2241
- nextRoot: RootHex,
2227
+ nextRoot: RootHex | null,
2242
2228
  nextPayloadStatus: PayloadStatus
2243
2229
  ): void {
2244
- // should not happen, attestation is validated before this step
2245
- // Get the node index for the voted block
2246
- const nextIndex = this.protoArray.getNodeIndexByRootAndStatus(nextRoot, nextPayloadStatus);
2230
+ // A null root means the queued vote's block was pruned after validation. If the vote is newer,
2231
+ // NULL_VOTE_INDEX lets computeDeltas remove the previous vote's weight without crediting a new block.
2232
+ const nextIndex =
2233
+ nextRoot === null ? NULL_VOTE_INDEX : this.protoArray.getNodeIndexByRootAndStatus(nextRoot, nextPayloadStatus);
2247
2234
  if (nextIndex === undefined) {
2235
+ // A supplied root must resolve to the node for the attestation's payload status.
2248
2236
  throw new Error(`Could not find proto index for nextRoot ${nextRoot} with payloadStatus ${nextPayloadStatus}`);
2249
2237
  }
2250
2238
 
@@ -2279,18 +2267,16 @@ export class ForkChoice implements IForkChoice {
2279
2267
  if (slot < currentSlot) {
2280
2268
  this.queuedAttestations.delete(slot);
2281
2269
  for (const [blockRoot, validatorVotes] of byRoot.entries()) {
2282
- const blockRootHex = blockRoot;
2270
+ const nextRoot = this.protoArray.hasBlock(blockRoot) ? blockRoot : null;
2283
2271
  for (const [validatorIndex, payloadStatus] of validatorVotes.entries()) {
2284
2272
  // equivocatingIndices was checked in onAttestation
2285
- this.addLatestMessage(validatorIndex, slot, blockRootHex, payloadStatus);
2273
+ this.addLatestMessage(validatorIndex, slot, nextRoot, payloadStatus);
2286
2274
  }
2287
2275
 
2288
- if (slot === currentSlot - 1) {
2276
+ if (nextRoot !== null && slot === currentSlot - 1) {
2289
2277
  this.queuedAttestationsPreviousSlot += validatorVotes.size;
2290
2278
  }
2291
2279
  }
2292
- } else {
2293
- break;
2294
2280
  }
2295
2281
  }
2296
2282
  }
@@ -70,6 +70,7 @@ export enum NotReorgedReason {
70
70
  NotProposingOnTime = "notProposingOnTime",
71
71
  NotProposerOfNextSlot = "notProposerOfNextSlot",
72
72
  HeadBlockNotAvailable = "headBlockNotAvailable", // Should not happen because head block should be in cache
73
+ HeadStateNotAvailable = "headStateNotAvailable",
73
74
  Unknown = "unknown", // A placeholder in case reason is not provided
74
75
  }
75
76