@lodestar/fork-choice 2.1.0-dev.7fecddd274 → 2.1.0-dev.81778521fa

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.
@@ -7,6 +7,8 @@ export enum InvalidBlockCode {
7
7
  FINALIZED_SLOT = "FINALIZED_SLOT",
8
8
  NOT_FINALIZED_DESCENDANT = "NOT_FINALIZED_DESCENDANT",
9
9
  PARENT_EXECUTION_INVALID = "PARENT_EXECUTION_INVALID",
10
+ EXECUTION_INVALID = "EXECUTION_INVALID",
11
+ PAYLOAD_PARENT_MISMATCH = "PAYLOAD_PARENT_MISMATCH",
10
12
  }
11
13
 
12
14
  export type InvalidBlock =
@@ -14,7 +16,9 @@ export type InvalidBlock =
14
16
  | {code: InvalidBlockCode.FUTURE_SLOT; currentSlot: Slot; blockSlot: Slot}
15
17
  | {code: InvalidBlockCode.FINALIZED_SLOT; finalizedSlot: Slot; blockSlot: Slot}
16
18
  | {code: InvalidBlockCode.NOT_FINALIZED_DESCENDANT; finalizedRoot: RootHex; blockAncestor?: RootHex}
17
- | {code: InvalidBlockCode.PARENT_EXECUTION_INVALID; root: RootHex; hash: RootHex | null};
19
+ | {code: InvalidBlockCode.PARENT_EXECUTION_INVALID; root: RootHex; hash: RootHex | null}
20
+ | {code: InvalidBlockCode.EXECUTION_INVALID; root: RootHex; hash: RootHex}
21
+ | {code: InvalidBlockCode.PAYLOAD_PARENT_MISMATCH; root: RootHex; hash: RootHex};
18
22
 
19
23
  export enum InvalidAttestationCode {
20
24
  /**
@@ -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);
@@ -1405,7 +1399,8 @@ export class ForkChoice implements IForkChoice {
1405
1399
  * Returns true if the `descendantRoot` has an ancestor with `ancestorRoot`.
1406
1400
  *
1407
1401
  * Always returns `false` if either input roots are unknown.
1408
- * Still returns `true` if `ancestorRoot===descendantRoot` (and the roots are known)
1402
+ * Still returns `true` if `ancestorRoot===descendantRoot` and the ancestor payload status is `PENDING` or equal
1403
+ * to the descendant payload status (and both nodes are known)
1409
1404
  */
1410
1405
  isDescendant(
1411
1406
  ancestorRoot: RootHex,
@@ -1739,7 +1734,7 @@ export class ForkChoice implements IForkChoice {
1739
1734
  * Return true if the block is "weak" ie. its weight is below REORG_HEAD_WEIGHT_THRESHOLD of the
1740
1735
  * total attester weight per slot.
1741
1736
  *
1742
- * 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
1743
1738
  * https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.11/specs/gloas/fork-choice.md#modified-is_head_weak
1744
1739
  */
1745
1740
  private isHeadWeak(blockRoot: RootHex): boolean {
@@ -1758,10 +1753,6 @@ export class ForkChoice implements IForkChoice {
1758
1753
  committeePercent: this.config.REORG_HEAD_WEIGHT_THRESHOLD,
1759
1754
  });
1760
1755
 
1761
- if (!isForkPostGloas(this.config.getForkName(node.slot))) {
1762
- return node.weight < reorgThreshold;
1763
- }
1764
-
1765
1756
  let headWeight = node.attestationScore;
1766
1757
 
1767
1758
  const {equivocatingIndices} = this.fcStore;
@@ -1802,7 +1793,7 @@ export class ForkChoice implements IForkChoice {
1802
1793
  * Return true if the parent block is "strong" ie. its weight exceeds REORG_PARENT_WEIGHT_THRESHOLD
1803
1794
  * of the total attester weight per slot.
1804
1795
  *
1805
- * 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
1806
1797
  * https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.12/specs/gloas/fork-choice.md#modified-is_parent_strong
1807
1798
  */
1808
1799
  private isParentStrong(parentRoot: RootHex): boolean {
@@ -1817,9 +1808,7 @@ export class ForkChoice implements IForkChoice {
1817
1808
  committeePercent: this.config.REORG_PARENT_WEIGHT_THRESHOLD,
1818
1809
  });
1819
1810
 
1820
- // pre-gloas uses get_weight() (boost-inclusive), gloas uses get_attestation_score() (boost-excluded)
1821
- const parentWeight = isForkPostGloas(this.config.getForkName(node.slot)) ? node.attestationScore : node.weight;
1822
- return parentWeight > parentThreshold;
1811
+ return node.attestationScore > parentThreshold;
1823
1812
  }
1824
1813
 
1825
1814
  /**
@@ -2225,25 +2214,25 @@ export class ForkChoice implements IForkChoice {
2225
2214
 
2226
2215
  /**
2227
2216
  * Add a validator's latest message to the tracked votes.
2228
- * Always sync voteCurrentIndices and voteNextIndices so that it'll not throw in computeDeltas()
2229
- *
2230
- * Modified for Gloas to accept slot and payloadPresent.
2231
- * Spec: gloas/fork-choice.md#modified-update_latest_messages
2217
+ * Keep voteCurrentIndices and voteNextIndices the same length, as required by computeDeltas().
2232
2218
  *
2233
- * For backward compatibility with Fulu (pre-Gloas):
2234
- * - Accepts both epoch-derived and slot parameters
2235
- * - 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
2236
2223
  */
2237
2224
  private addLatestMessage(
2238
2225
  validatorIndex: ValidatorIndex,
2239
2226
  nextSlot: Slot,
2240
- nextRoot: RootHex,
2227
+ nextRoot: RootHex | null,
2241
2228
  nextPayloadStatus: PayloadStatus
2242
2229
  ): void {
2243
- // should not happen, attestation is validated before this step
2244
- // Get the node index for the voted block
2245
- 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);
2246
2234
  if (nextIndex === undefined) {
2235
+ // A supplied root must resolve to the node for the attestation's payload status.
2247
2236
  throw new Error(`Could not find proto index for nextRoot ${nextRoot} with payloadStatus ${nextPayloadStatus}`);
2248
2237
  }
2249
2238
 
@@ -2278,18 +2267,16 @@ export class ForkChoice implements IForkChoice {
2278
2267
  if (slot < currentSlot) {
2279
2268
  this.queuedAttestations.delete(slot);
2280
2269
  for (const [blockRoot, validatorVotes] of byRoot.entries()) {
2281
- const blockRootHex = blockRoot;
2270
+ const nextRoot = this.protoArray.hasBlock(blockRoot) ? blockRoot : null;
2282
2271
  for (const [validatorIndex, payloadStatus] of validatorVotes.entries()) {
2283
2272
  // equivocatingIndices was checked in onAttestation
2284
- this.addLatestMessage(validatorIndex, slot, blockRootHex, payloadStatus);
2273
+ this.addLatestMessage(validatorIndex, slot, nextRoot, payloadStatus);
2285
2274
  }
2286
2275
 
2287
- if (slot === currentSlot - 1) {
2276
+ if (nextRoot !== null && slot === currentSlot - 1) {
2288
2277
  this.queuedAttestationsPreviousSlot += validatorVotes.size;
2289
2278
  }
2290
2279
  }
2291
- } else {
2292
- break;
2293
2280
  }
2294
2281
  }
2295
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
 
@@ -296,7 +297,8 @@ export interface IForkChoice {
296
297
  * Returns true if the `descendantRoot` has an ancestor with `ancestorRoot`.
297
298
  *
298
299
  * Always returns `false` if either input roots are unknown.
299
- * Still returns `true` if `ancestorRoot===descendantRoot` (and the roots are known)
300
+ * Still returns `true` if `ancestorRoot===descendantRoot` and the ancestor payload status is `PENDING` or equal
301
+ * to the descendant payload status (and both nodes are known)
300
302
  */
301
303
  isDescendant(
302
304
  ancestorRoot: RootHex,