@lodestar/fork-choice 1.52.0-dev.a94ada0e67 → 1.52.0-dev.b17bb95924

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.
@@ -34,7 +34,6 @@ import {
34
34
  import {Logger, MapDef, fromHex, toRootHex, withObservedDuration} from "@lodestar/utils";
35
35
  import {ForkChoiceMetrics} from "../metrics.js";
36
36
  import {computeDeltas} from "../protoArray/computeDeltas.js";
37
- import {ProtoArrayError, ProtoArrayErrorCode} from "../protoArray/errors.js";
38
37
  import {
39
38
  BlockExecutionStatus,
40
39
  ExecutionStatus,
@@ -112,7 +111,6 @@ const INIT_VOTE_SLOT: Slot = 0;
112
111
  * - Time is not updated automatically, updateTime MUST be called every slot
113
112
  */
114
113
  export class ForkChoice implements IForkChoice {
115
- irrecoverableError?: Error;
116
114
  /**
117
115
  * Votes currently tracked in the protoArray. Instead of tracking a VoteTracker of currentIndex, nextIndex and epoch,
118
116
  * we decompose the struct and track them in separate arrays for performance reason.
@@ -152,6 +150,8 @@ export class ForkChoice implements IForkChoice {
152
150
 
153
151
  /** Cached head */
154
152
  private head: ProtoBlock;
153
+ /** Slot of the last `updateHead()`, scores and best child links reflect votes, boost and viability at that slot */
154
+ private updateHeadSlot = 0;
155
155
  /**
156
156
  * Only cache attestation data root hex if it's tree backed since it's available.
157
157
  **/
@@ -695,18 +695,8 @@ export class ForkChoice implements IForkChoice {
695
695
  }
696
696
 
697
697
  // findHead returns the ProtoNode representing the head
698
- let head: ProtoNode;
699
- try {
700
- head = this.protoArray.findHead(this.fcStore.justified.checkpoint.rootHex, currentSlot);
701
- } catch (e) {
702
- // A poisoned tree never recovers, shut the node down instead of failing every head computation
703
- if (e instanceof ProtoArrayError && e.type.code === ProtoArrayErrorCode.INVALID_JUSTIFIED_EXECUTION_STATUS) {
704
- this.irrecoverableError = e;
705
- }
706
- throw e;
707
- }
708
-
709
- this.head = head;
698
+ this.head = this.protoArray.findHead(this.fcStore.justified.checkpoint.rootHex, currentSlot);
699
+ this.updateHeadSlot = currentSlot;
710
700
  return this.head;
711
701
  }
712
702
 
@@ -1161,7 +1151,7 @@ export class ForkChoice implements IForkChoice {
1161
1151
 
1162
1152
  /**
1163
1153
  * Notify fork choice that an execution payload has arrived (Gloas fork)
1164
- * Creates the FULL variant of a Gloas block when the payload becomes available
1154
+ * Creates the FULL variant of a Gloas block when the payload becomes available and refreshes the cached head
1165
1155
  * Spec: gloas/fork-choice.md#new-on_execution_payload
1166
1156
  */
1167
1157
  onExecutionPayload(
@@ -1185,9 +1175,10 @@ export class ForkChoice implements IForkChoice {
1185
1175
  });
1186
1176
  }
1187
1177
 
1178
+ const currentSlot = this.fcStore.currentSlot;
1188
1179
  this.protoArray.onExecutionPayload(
1189
1180
  blockRoot,
1190
- this.fcStore.currentSlot,
1181
+ currentSlot,
1191
1182
  executionPayloadBlockHash,
1192
1183
  executionPayloadNumber,
1193
1184
  executionPayloadGasLimit,
@@ -1195,6 +1186,11 @@ export class ForkChoice implements IForkChoice {
1195
1186
  executionStatus,
1196
1187
  dataAvailabilityStatus
1197
1188
  );
1189
+ if (currentSlot === this.updateHeadSlot) {
1190
+ this.head = this.protoArray.findHead(this.fcStore.justified.checkpoint.rootHex, currentSlot);
1191
+ } else {
1192
+ this.updateHead();
1193
+ }
1198
1194
  }
1199
1195
 
1200
1196
  /**
@@ -1211,6 +1207,18 @@ export class ForkChoice implements IForkChoice {
1211
1207
  const previousSlot = this.fcStore.currentSlot;
1212
1208
  // Note: we are relying upon `onTick` to update `fcStore.time` to ensure we don't get stuck in a loop.
1213
1209
  const didUpdateCheckpoints = this.onTick(previousSlot + 1);
1210
+ if (didUpdateCheckpoints) {
1211
+ // The pull-up realizes the unrealized justified checkpoint regardless of its execution status. Throw before
1212
+ // fast confirmation, which swallows head computation errors
1213
+ const justified = this.fcStore.justified.checkpoint;
1214
+ if (this.getBlockHexDefaultStatus(justified.rootHex)?.executionStatus === ExecutionStatus.Invalid) {
1215
+ throw new ForkChoiceError({
1216
+ code: ForkChoiceErrorCode.INVALID_JUSTIFIED_EXECUTION_STATUS,
1217
+ epoch: justified.epoch,
1218
+ root: justified.rootHex,
1219
+ });
1220
+ }
1221
+ }
1214
1222
  this.queuedAttestationsPreviousSlot = 0;
1215
1223
  // Process any attestations that might now be eligible before running FCR for this slot.
1216
1224
  this.processAttestationQueue();
@@ -78,8 +78,6 @@ export type ShouldOverrideForkChoiceUpdateResult =
78
78
  | {shouldOverrideFcu: false; reason: NotReorgedReason};
79
79
 
80
80
  export interface IForkChoice {
81
- irrecoverableError?: Error;
82
-
83
81
  /**
84
82
  * Returns the ancestor node of `block_root` at the given `slot`. (Note: `slot` refers
85
83
  * to the block that is *returned*, not the one that is supplied.)
@@ -213,7 +211,7 @@ export interface IForkChoice {
213
211
  ): void;
214
212
  /**
215
213
  * Notify fork choice that an execution payload has arrived (Gloas fork)
216
- * Creates the FULL variant of a Gloas block when the payload becomes available
214
+ * Creates the FULL variant of a Gloas block when the payload becomes available and refreshes the cached head
217
215
  *
218
216
  * ## Specification
219
217
  *
@@ -711,8 +711,14 @@ export class ProtoArray {
711
711
  this.propagateValidExecutionStatusByIndex(pendingIndex);
712
712
  }
713
713
 
714
- // Update bestChild for PENDING node (may now prefer FULL over EMPTY)
715
- this.maybeUpdateBestChildAndDescendant(pendingIndex, fullIndex, currentSlot, proposerBoostRoot);
714
+ // A zero-weight leaf only changes best child and descendant links on its path to the root
715
+ let childIndex = fullIndex;
716
+ let parentIndex: number | undefined = pendingIndex;
717
+ while (parentIndex !== undefined) {
718
+ this.maybeUpdateBestChildAndDescendant(parentIndex, childIndex, currentSlot, proposerBoostRoot);
719
+ childIndex = parentIndex;
720
+ parentIndex = this.nodes[childIndex].parent;
721
+ }
716
722
  }
717
723
 
718
724
  /** Count blocks selected as FULL or EMPTY by the supplied head chain in the inclusive slot range. */