@lodestar/fork-choice 1.52.0-dev.b76fc9b708 → 1.52.0-dev.d88015cb2a

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.
@@ -4,11 +4,14 @@ import {DataAvailabilityStatus, computeEpochAtSlot, computeStartSlotAtEpoch} fro
4
4
  import {Epoch, RootHex, Slot, ValidatorIndex} from "@lodestar/types";
5
5
  import {bitCount, toRootHex} from "@lodestar/utils";
6
6
  import {ForkChoiceError, ForkChoiceErrorCode} from "../forkChoice/errors.js";
7
- import {LVHExecError, LVHExecErrorCode, ProtoArrayError, ProtoArrayErrorCode} from "./errors.js";
7
+ import {LVHExecErrorCode, ProtoArrayError, ProtoArrayErrorCode} from "./errors.js";
8
8
  import {
9
9
  ExecutionStatus,
10
10
  HEX_ZERO_HASH,
11
11
  LVHExecResponse,
12
+ LVHInvalidJudged,
13
+ LVHNotAppliedReason,
14
+ LVHVerdictResult,
12
15
  PayloadExecutionStatus,
13
16
  PayloadStatus,
14
17
  ProtoBlock,
@@ -94,7 +97,6 @@ export class ProtoArray {
94
97
  * Note: undefined array elements indicate that variant doesn't exist for this block
95
98
  */
96
99
  indices = new Map<RootHex, VariantIndices>();
97
- lvhError?: LVHExecError;
98
100
 
99
101
  private previousProposerBoost: ProposerBoost | null = null;
100
102
  /** Root read by shouldExtendPayload(), set even while the boost weight is withheld */
@@ -1020,22 +1022,19 @@ export class ProtoArray {
1020
1022
 
1021
1023
  /**
1022
1024
  * Apply an execution engine verdict with a latest valid hash (LVH) to the tree. VALID flips Syncing nodes up to the
1023
- * LVH node. INVALID invalidates the node identified by `invalidateFromParentBlockRoot/Hash` and its ancestors up to
1024
- * the LVH node (exclusive), then every descendant. Returns whether any status changed.
1025
+ * LVH node. INVALID invalidates the node identified by `invalidateFromBlockRoot/Hash` and its ancestors up to
1026
+ * the LVH node (exclusive), then every descendant. Returns whether any status changed, and if not, why.
1025
1027
  *
1026
- * A null or unknown LVH means the engine only vouches for the queried payload being invalid: the queried node and
1027
- * the nodes that inherit that payload (gloas PENDING and EMPTY variants) are invalidated, nothing above them.
1028
+ * A verdict never contradicts what fork choice already holds: a node the engine already resolved is never flipped
1029
+ * and an INVALID never crosses the justified node. The LVH is unusable when it is null, not an ancestor of the from
1030
+ * node, or the walk to it meets such a node.
1028
1031
  *
1029
- * `strict` keeps the consensus failure semantics for verdicts about a payload the engine just processed: a VALID
1030
- * or PreMerge node on the invalidated chain poisons the tree. Verdicts from forkchoiceUpdated or about an ancestor
1031
- * chain are applied with `strict: false`: a node the engine already validated is never invalidated and the justified
1032
- * node is never crossed. If the walk to the LVH meets either, the LVH is treated as unknown.
1032
+ * An unusable LVH leaves only the claim about the judged payload. With `LVHInvalidJudged.FromBlock` that is the from
1033
+ * node: it and the nodes that inherit its payload (gloas PENDING and EMPTY variants) are invalidated, nothing above
1034
+ * them. With `LVHInvalidJudged.ChildPayload` the judged payload is not in the tree and the from node is its parent,
1035
+ * so the verdict is dropped.
1033
1036
  */
1034
- validateLatestHash(
1035
- execResponse: LVHExecResponse,
1036
- currentSlot: Slot,
1037
- {strict = true}: {strict?: boolean} = {}
1038
- ): boolean {
1037
+ validateLatestHash(execResponse: LVHExecResponse, currentSlot: Slot): LVHVerdictResult {
1039
1038
  // Look reverse because its highly likely node with latestValidExecHash is towards the
1040
1039
  // the leaves of the forkchoice
1041
1040
  //
@@ -1065,28 +1064,26 @@ export class ProtoArray {
1065
1064
  // We are trying to be as forgiving as possible here because ideally latestValidHashIndex
1066
1065
  // should be found in the forkchoice
1067
1066
  if (latestValidHashIndex < 0) {
1068
- return false;
1067
+ return {applied: false, reason: LVHNotAppliedReason.UnknownLVH};
1069
1068
  }
1070
1069
  const {executionStatus} = this.nodes[latestValidHashIndex];
1071
1070
  if (executionStatus === ExecutionStatus.Valid || executionStatus === ExecutionStatus.PreMerge) {
1072
- return false;
1071
+ return {applied: false, reason: LVHNotAppliedReason.AlreadyApplied};
1073
1072
  }
1074
- if (!strict) {
1075
- // The walk up flips every Syncing ancestor, a lenient verdict must not contradict an invalidated one
1076
- let nodeIndex: number | undefined = latestValidHashIndex;
1077
- while (nodeIndex !== undefined) {
1078
- const node: ProtoNode = this.nodes[nodeIndex];
1079
- if (node.executionStatus === ExecutionStatus.Valid || node.executionStatus === ExecutionStatus.PreMerge) {
1080
- break;
1081
- }
1082
- if (node.executionStatus === ExecutionStatus.Invalid) {
1083
- return false;
1084
- }
1085
- nodeIndex = node.parent;
1073
+ // The walk up flips every Syncing ancestor, it must not contradict an invalidated one
1074
+ let nodeIndex: number | undefined = latestValidHashIndex;
1075
+ while (nodeIndex !== undefined) {
1076
+ const node: ProtoNode = this.nodes[nodeIndex];
1077
+ if (node.executionStatus === ExecutionStatus.Valid || node.executionStatus === ExecutionStatus.PreMerge) {
1078
+ break;
1086
1079
  }
1080
+ if (node.executionStatus === ExecutionStatus.Invalid) {
1081
+ return {applied: false, reason: LVHNotAppliedReason.Contradiction};
1082
+ }
1083
+ nodeIndex = node.parent;
1087
1084
  }
1088
1085
  this.propagateValidExecutionStatusByIndex(latestValidHashIndex);
1089
- return true;
1086
+ return {applied: true};
1090
1087
  }
1091
1088
 
1092
1089
  // In case of invalidation, ideally:
@@ -1102,56 +1099,64 @@ export class ProtoArray {
1102
1099
  // Mark chain ii) as Invalid if LVH is found and non null, else only invalidate invalid_payload
1103
1100
  // if its in fcU.
1104
1101
  //
1105
- const {invalidateFromParentBlockRoot, invalidateFromParentBlockHash, latestValidExecHash} = execResponse;
1106
- const invalidateFromParentIndex = this.getNodeIndexByRootAndBlockHash(
1107
- invalidateFromParentBlockRoot,
1108
- invalidateFromParentBlockHash
1109
- );
1110
- if (invalidateFromParentIndex === undefined) {
1102
+ const {invalidateFromBlockRoot, invalidateFromBlockHash, latestValidExecHash, judged} = execResponse;
1103
+ const invalidateFromIndex = this.getNodeIndexByRootAndBlockHash(invalidateFromBlockRoot, invalidateFromBlockHash);
1104
+ if (invalidateFromIndex === undefined) {
1111
1105
  throw Error(
1112
- `Unable to find invalidateFromParentBlockRoot=${invalidateFromParentBlockRoot} invalidateFromParentBlockHash=${invalidateFromParentBlockHash} in forkChoice`
1106
+ `Unable to find invalidateFromBlockRoot=${invalidateFromBlockRoot} invalidateFromBlockHash=${invalidateFromBlockHash} in forkChoice`
1113
1107
  );
1114
1108
  }
1115
- if (!strict && this.nodes[invalidateFromParentIndex].executionStatus !== ExecutionStatus.Syncing) {
1116
- return false;
1109
+ const fromStatus = this.nodes[invalidateFromIndex].executionStatus;
1110
+ if (fromStatus !== ExecutionStatus.Syncing) {
1111
+ return {
1112
+ applied: false,
1113
+ reason:
1114
+ fromStatus === ExecutionStatus.Invalid
1115
+ ? LVHNotAppliedReason.AlreadyApplied
1116
+ : LVHNotAppliedReason.Contradiction,
1117
+ };
1117
1118
  }
1118
1119
 
1119
1120
  let latestValidHashIndex =
1120
- latestValidExecHash !== null ? this.getNodeIndexFromLVH(latestValidExecHash, invalidateFromParentIndex) : null;
1121
+ latestValidExecHash !== null ? this.getNodeIndexFromLVH(latestValidExecHash, invalidateFromIndex) : null;
1121
1122
  if (latestValidHashIndex === null) {
1122
- if (strict) {
1123
- // The strict from node is the parent of the rejected payload and may be valid, never guess here
1124
- throw Error(`Unable to find latestValidExecHash=${latestValidExecHash} in the forkchoice`);
1123
+ if (judged === LVHInvalidJudged.ChildPayload) {
1124
+ return {applied: false, reason: LVHNotAppliedReason.UnusableLVH};
1125
1125
  }
1126
- latestValidHashIndex = this.getUnknownLVHIndex(invalidateFromParentIndex);
1127
- }
1128
-
1129
- if (!strict) {
1130
- // Never invalidate the justified block, the EMPTY variant sharing its chain, or anything at or below it in
1131
- // the tree. The justified block's own payload (FULL variant) is not part of the checkpoint
1132
- const justifiedIndex = this.getDefaultNodeIndex(this.justifiedRoot) ?? -1;
1133
- let nodeIndex: number | undefined = invalidateFromParentIndex;
1134
- while (nodeIndex !== undefined && nodeIndex > latestValidHashIndex) {
1135
- const node: ProtoNode = this.nodes[nodeIndex];
1136
- if (
1137
- node.executionStatus !== ExecutionStatus.Syncing ||
1138
- nodeIndex <= justifiedIndex ||
1139
- (node.blockRoot === this.justifiedRoot && node.payloadStatus === PayloadStatus.EMPTY)
1140
- ) {
1141
- // Conflicting LVH: fall back to the unknown LVH bound, but never cross this node
1142
- latestValidHashIndex = Math.max(nodeIndex, this.getUnknownLVHIndex(invalidateFromParentIndex));
1143
- break;
1126
+ latestValidHashIndex = this.getUnknownLVHIndex(invalidateFromIndex);
1127
+ }
1128
+
1129
+ // Never invalidate the justified block, the EMPTY variant sharing its chain, or anything at or below it in
1130
+ // the tree. The justified block's own payload (FULL variant) is not part of the checkpoint
1131
+ const justifiedIndex = this.getDefaultNodeIndex(this.justifiedRoot) ?? -1;
1132
+ let nodeIndex: number | undefined = invalidateFromIndex;
1133
+ while (nodeIndex !== undefined && nodeIndex > latestValidHashIndex) {
1134
+ const node: ProtoNode = this.nodes[nodeIndex];
1135
+ const conflict = node.executionStatus !== ExecutionStatus.Syncing;
1136
+ const justified =
1137
+ nodeIndex <= justifiedIndex ||
1138
+ (node.blockRoot === this.justifiedRoot && node.payloadStatus === PayloadStatus.EMPTY);
1139
+ if (conflict || justified) {
1140
+ if (judged === LVHInvalidJudged.FromBlock) {
1141
+ // The LVH is unusable: fall back to the unknown LVH bound, but never cross this node
1142
+ latestValidHashIndex = Math.max(nodeIndex, this.getUnknownLVHIndex(invalidateFromIndex));
1143
+ } else if (conflict) {
1144
+ return {applied: false, reason: LVHNotAppliedReason.Contradiction};
1145
+ } else {
1146
+ // The LVH is below the justified node, it still covers everything above it
1147
+ latestValidHashIndex = nodeIndex;
1144
1148
  }
1145
- nodeIndex = node.parent;
1149
+ break;
1146
1150
  }
1151
+ nodeIndex = node.parent;
1147
1152
  }
1148
1153
 
1149
- if (invalidateFromParentIndex <= latestValidHashIndex) {
1150
- return false;
1154
+ if (invalidateFromIndex <= latestValidHashIndex) {
1155
+ return {applied: false, reason: LVHNotAppliedReason.NothingToInvalidate};
1151
1156
  }
1152
1157
 
1153
- this.propagateInValidExecutionStatusByIndex(invalidateFromParentIndex, latestValidHashIndex, currentSlot);
1154
- return true;
1158
+ this.propagateInValidExecutionStatusByIndex(invalidateFromIndex, latestValidHashIndex, currentSlot);
1159
+ return {applied: true};
1155
1160
  }
1156
1161
 
1157
1162
  /**
@@ -1224,12 +1229,12 @@ export class ProtoArray {
1224
1229
  */
1225
1230
 
1226
1231
  private propagateInValidExecutionStatusByIndex(
1227
- invalidateFromParentIndex: number,
1232
+ invalidateFromIndex: number,
1228
1233
  latestValidHashIndex: number,
1229
1234
  currentSlot: Slot
1230
1235
  ): void {
1231
- // Pass 1: mark invalidateFromParentIndex and its parents invalid
1232
- let invalidateIndex: number | undefined = invalidateFromParentIndex;
1236
+ // Pass 1: mark invalidateFromIndex and its parents invalid
1237
+ let invalidateIndex: number | undefined = invalidateFromIndex;
1233
1238
  while (invalidateIndex !== undefined && invalidateIndex > latestValidHashIndex) {
1234
1239
  const invalidNode = this.invalidateNodeByIndex(invalidateIndex);
1235
1240
  invalidateIndex = invalidNode.parent;
@@ -1278,28 +1283,19 @@ export class ProtoArray {
1278
1283
  private invalidateNodeByIndex(nodeIndex: number): ProtoNode {
1279
1284
  const invalidNode = this.getNodeFromIndex(nodeIndex);
1280
1285
 
1281
- // If node to be invalidated is pre-merge or valid,it is a catastrophe,
1282
- // and indicates consensus failure and a non recoverable damage.
1283
- //
1284
- // There is no further processing that can be done.
1285
- // Just assign error for marking proto-array perma damaged and throw!
1286
+ // validateLatestHash never starts an invalidation that reaches a pre-merge or valid node
1286
1287
  if (
1287
1288
  invalidNode.executionStatus === ExecutionStatus.Valid ||
1288
1289
  invalidNode.executionStatus === ExecutionStatus.PreMerge
1289
1290
  ) {
1290
- const lvhCode =
1291
- invalidNode.executionStatus === ExecutionStatus.Valid
1292
- ? LVHExecErrorCode.ValidToInvalid
1293
- : LVHExecErrorCode.PreMergeToInvalid;
1294
-
1295
- this.lvhError = {
1296
- lvhCode,
1297
- blockRoot: invalidNode.blockRoot,
1298
- execHash: invalidNode.executionPayloadBlockHash ?? ZERO_HASH_HEX,
1299
- };
1300
1291
  throw new ProtoArrayError({
1301
1292
  code: ProtoArrayErrorCode.INVALID_LVH_EXECUTION_RESPONSE,
1302
- ...this.lvhError,
1293
+ lvhCode:
1294
+ invalidNode.executionStatus === ExecutionStatus.Valid
1295
+ ? LVHExecErrorCode.ValidToInvalid
1296
+ : LVHExecErrorCode.PreMergeToInvalid,
1297
+ blockRoot: invalidNode.blockRoot,
1298
+ execHash: invalidNode.executionPayloadBlockHash ?? ZERO_HASH_HEX,
1303
1299
  });
1304
1300
  }
1305
1301
 
@@ -1320,14 +1316,11 @@ export class ProtoArray {
1320
1316
  private validateNodeByIndex(nodeIndex: number): ProtoNode {
1321
1317
  const validNode = this.getNodeFromIndex(nodeIndex);
1322
1318
  if (validNode.executionStatus === ExecutionStatus.Invalid) {
1323
- this.lvhError = {
1319
+ throw new ProtoArrayError({
1320
+ code: ProtoArrayErrorCode.INVALID_LVH_EXECUTION_RESPONSE,
1324
1321
  lvhCode: LVHExecErrorCode.InvalidToValid,
1325
1322
  blockRoot: validNode.blockRoot,
1326
1323
  execHash: validNode.executionPayloadBlockHash,
1327
- };
1328
- throw new ProtoArrayError({
1329
- code: ProtoArrayErrorCode.INVALID_LVH_EXECUTION_RESPONSE,
1330
- ...this.lvhError,
1331
1324
  });
1332
1325
  }
1333
1326
 
@@ -1393,13 +1386,6 @@ export class ProtoArray {
1393
1386
  * For Gloas, may return PENDING/EMPTY/FULL variants.
1394
1387
  */
1395
1388
  findHead(justifiedRoot: RootHex, currentSlot: Slot): ProtoNode {
1396
- if (this.lvhError) {
1397
- throw new ProtoArrayError({
1398
- code: ProtoArrayErrorCode.INVALID_LVH_EXECUTION_RESPONSE,
1399
- ...this.lvhError,
1400
- });
1401
- }
1402
-
1403
1389
  // Get canonical node: FULL for pre-Gloas, PENDING for Gloas
1404
1390
  const justifiedIndex = this.getDefaultNodeIndex(justifiedRoot);
1405
1391
  if (justifiedIndex === undefined) {