@lodestar/fork-choice 1.51.0 → 1.52.0-dev.03eb33e6f1

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 (45) hide show
  1. package/lib/forkChoice/errors.d.ts +12 -2
  2. package/lib/forkChoice/errors.d.ts.map +1 -1
  3. package/lib/forkChoice/errors.js +2 -0
  4. package/lib/forkChoice/errors.js.map +1 -1
  5. package/lib/forkChoice/fastConfirmation/utils.d.ts +12 -12
  6. package/lib/forkChoice/fastConfirmation/utils.d.ts.map +1 -1
  7. package/lib/forkChoice/fastConfirmation/utils.js +48 -61
  8. package/lib/forkChoice/fastConfirmation/utils.js.map +1 -1
  9. package/lib/forkChoice/forkChoice.d.ts +10 -12
  10. package/lib/forkChoice/forkChoice.d.ts.map +1 -1
  11. package/lib/forkChoice/forkChoice.js +62 -22
  12. package/lib/forkChoice/forkChoice.js.map +1 -1
  13. package/lib/forkChoice/interface.d.ts +10 -5
  14. package/lib/forkChoice/interface.d.ts.map +1 -1
  15. package/lib/forkChoice/interface.js.map +1 -1
  16. package/lib/index.d.ts +2 -2
  17. package/lib/index.d.ts.map +1 -1
  18. package/lib/index.js +1 -1
  19. package/lib/index.js.map +1 -1
  20. package/lib/metrics.d.ts +3 -0
  21. package/lib/metrics.d.ts.map +1 -1
  22. package/lib/metrics.js +5 -0
  23. package/lib/metrics.js.map +1 -1
  24. package/lib/protoArray/errors.d.ts +8 -10
  25. package/lib/protoArray/errors.d.ts.map +1 -1
  26. package/lib/protoArray/errors.js +7 -7
  27. package/lib/protoArray/errors.js.map +1 -1
  28. package/lib/protoArray/interface.d.ts +35 -2
  29. package/lib/protoArray/interface.d.ts.map +1 -1
  30. package/lib/protoArray/interface.js +22 -0
  31. package/lib/protoArray/interface.js.map +1 -1
  32. package/lib/protoArray/protoArray.d.ts +25 -11
  33. package/lib/protoArray/protoArray.d.ts.map +1 -1
  34. package/lib/protoArray/protoArray.js +159 -91
  35. package/lib/protoArray/protoArray.js.map +1 -1
  36. package/package.json +8 -8
  37. package/src/forkChoice/errors.ts +6 -2
  38. package/src/forkChoice/fastConfirmation/utils.ts +84 -94
  39. package/src/forkChoice/forkChoice.ts +67 -23
  40. package/src/forkChoice/interface.ts +10 -5
  41. package/src/index.ts +9 -1
  42. package/src/metrics.ts +5 -0
  43. package/src/protoArray/errors.ts +8 -5
  44. package/src/protoArray/interface.ts +32 -4
  45. package/src/protoArray/protoArray.ts +171 -98
package/src/index.ts CHANGED
@@ -41,10 +41,18 @@ export type {
41
41
  BlockExtraMeta,
42
42
  LVHInvalidResponse,
43
43
  LVHValidResponse,
44
+ LVHVerdictResult,
44
45
  PayloadExecutionStatus,
45
46
  ProtoBlock,
46
47
  ProtoNode,
47
48
  PtcQuorum,
48
49
  } from "./protoArray/interface.js";
49
- export {ExecutionStatus, PayloadStatus, getPtcVerdict, isGloasBlock} from "./protoArray/interface.js";
50
+ export {
51
+ ExecutionStatus,
52
+ LVHInvalidJudged,
53
+ LVHNotAppliedReason,
54
+ PayloadStatus,
55
+ getPtcVerdict,
56
+ isGloasBlock,
57
+ } from "./protoArray/interface.js";
50
58
  export {ProtoArray} from "./protoArray/protoArray.js";
package/src/metrics.ts CHANGED
@@ -32,6 +32,11 @@ export function getForkChoiceMetrics(register: MetricsRegisterExtra) {
32
32
  name: "beacon_fork_choice_changed_head_total",
33
33
  help: "Count of occasions fork choice has found a new head",
34
34
  }),
35
+ executionInvalidations: register.gauge<{source: string}>({
36
+ name: "beacon_fork_choice_execution_invalidations_total",
37
+ help: "Count of execution engine INVALID verdicts applied to fork choice, by verdict source",
38
+ labelNames: ["source"],
39
+ }),
35
40
  reorg: register.gauge({
36
41
  name: "beacon_fork_choice_reorg_total",
37
42
  help: "Count of occasions fork choice has switched to a different chain",
@@ -1,14 +1,12 @@
1
1
  import {Epoch, RootHex} from "@lodestar/types";
2
2
  import {LodestarError} from "@lodestar/utils";
3
3
 
4
- export enum LVHExecErrorCode {
4
+ export enum ExecutionStatusTransition {
5
5
  PreMergeToInvalid = "PreMergeToInvalid",
6
6
  ValidToInvalid = "ValidToInvalid",
7
7
  InvalidToValid = "InvalidToValid",
8
8
  }
9
9
 
10
- export type LVHExecError = {lvhCode: LVHExecErrorCode; blockRoot: RootHex; execHash: RootHex};
11
-
12
10
  export enum ProtoArrayErrorCode {
13
11
  FINALIZED_NODE_UNKNOWN = "PROTO_ARRAY_ERROR_FINALIZED_NODE_UNKNOWN",
14
12
  JUSTIFIED_NODE_UNKNOWN = "PROTO_ARRAY_ERROR_JUSTIFIED_NODE_UNKNOWN",
@@ -28,7 +26,7 @@ export enum ProtoArrayErrorCode {
28
26
  INVALID_BEST_NODE = "PROTO_ARRAY_ERROR_INVALID_BEST_NODE",
29
27
  INVALID_BLOCK_EXECUTION_STATUS = "PROTO_ARRAY_INVALID_BLOCK_EXECUTION_STATUS",
30
28
  INVALID_JUSTIFIED_EXECUTION_STATUS = "PROTO_ARRAY_INVALID_JUSTIFIED_EXECUTION_STATUS",
31
- INVALID_LVH_EXECUTION_RESPONSE = "PROTO_ARRAY_INVALID_LVH_EXECUTION_RESPONSE",
29
+ INVALID_EXECUTION_STATUS_TRANSITION = "PROTO_ARRAY_ERROR_INVALID_EXECUTION_STATUS_TRANSITION",
32
30
  PRE_GLOAS_BLOCK = "PROTO_ARRAY_ERROR_PRE_GLOAS_BLOCK",
33
31
  }
34
32
 
@@ -59,7 +57,12 @@ export type ProtoArrayErrorType =
59
57
  }
60
58
  | {code: ProtoArrayErrorCode.INVALID_BLOCK_EXECUTION_STATUS; root: RootHex}
61
59
  | {code: ProtoArrayErrorCode.INVALID_JUSTIFIED_EXECUTION_STATUS; root: RootHex}
62
- | ({code: ProtoArrayErrorCode.INVALID_LVH_EXECUTION_RESPONSE} & LVHExecError)
60
+ | {
61
+ code: ProtoArrayErrorCode.INVALID_EXECUTION_STATUS_TRANSITION;
62
+ transition: ExecutionStatusTransition;
63
+ blockRoot: RootHex;
64
+ execHash: RootHex;
65
+ }
63
66
  | {code: ProtoArrayErrorCode.PRE_GLOAS_BLOCK; root: RootHex};
64
67
 
65
68
  export class ProtoArrayError extends LodestarError<ProtoArrayErrorType> {}
@@ -55,17 +55,45 @@ export function isGloasBlock(block: ProtoBlock): block is ProtoBlock & {parentBl
55
55
  export type LVHValidResponse = {
56
56
  executionStatus: ExecutionStatus.Valid;
57
57
  latestValidExecHash: RootHex;
58
+ /** Resolves the exact node to validate when several nodes carry the same payload hash (gloas variants) */
59
+ blockRoot?: RootHex;
58
60
  };
61
+ /** Which payload the execution engine judged INVALID, relative to the node the invalidation starts from */
62
+ export enum LVHInvalidJudged {
63
+ /** The from node's own payload, e.g. the head sent in forkchoiceUpdated */
64
+ FromBlock = "from_block",
65
+ /** A payload built on the from node that is not in fork choice, e.g. a payload rejected by newPayload */
66
+ ChildPayload = "child_payload",
67
+ }
59
68
  export type LVHInvalidResponse = {
60
69
  executionStatus: ExecutionStatus.Invalid;
61
70
  latestValidExecHash: RootHex | null;
62
- invalidateFromParentBlockRoot: RootHex;
63
- // EL block hash from invalid block's bid (gloas) or payload's parentHash (pre-gloas).
64
- // Disambiguates which variant of the parent (FULL vs EMPTY) to invalidate from.
65
- invalidateFromParentBlockHash: RootHex;
71
+ invalidateFromBlockRoot: RootHex;
72
+ // EL block hash of the from node's payload chain, disambiguates which variant (FULL vs EMPTY) to invalidate from.
73
+ // For `ChildPayload` it is the judged payload's parent hash.
74
+ invalidateFromBlockHash: RootHex;
75
+ judged: LVHInvalidJudged;
66
76
  };
67
77
  export type LVHExecResponse = LVHValidResponse | LVHInvalidResponse;
68
78
 
79
+ export enum LVHNotAppliedReason {
80
+ /** VALID verdict whose latest valid hash is not in fork choice */
81
+ UnknownLVH = "unknown_lvh",
82
+ /** Fork choice already holds the verdict */
83
+ AlreadyApplied = "already_applied",
84
+ /** The verdict contradicts a status the engine already resolved */
85
+ Contradiction = "contradiction",
86
+ /** INVALID verdict on a payload that is not in fork choice, with a latest valid hash that is null or unknown */
87
+ UnusableLVH = "unusable_lvh",
88
+ /** INVALID verdict that covers no node above the latest valid hash or the justified block */
89
+ NothingToInvalidate = "nothing_to_invalidate",
90
+ Error = "error",
91
+ }
92
+ export type LVHVerdictResult =
93
+ | {applied: true}
94
+ | {applied: false; reason: Exclude<LVHNotAppliedReason, LVHNotAppliedReason.Error>}
95
+ | {applied: false; reason: LVHNotAppliedReason.Error; error: Error};
96
+
69
97
  /**
70
98
  * Any execution status that is not definitively invalid.
71
99
  * Valid | Syncing | PreMerge
@@ -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 {ExecutionStatusTransition, 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 */
@@ -709,8 +711,14 @@ export class ProtoArray {
709
711
  this.propagateValidExecutionStatusByIndex(pendingIndex);
710
712
  }
711
713
 
712
- // Update bestChild for PENDING node (may now prefer FULL over EMPTY)
713
- 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
+ }
714
722
  }
715
723
 
716
724
  /** Count blocks selected as FULL or EMPTY by the supplied head chain in the inclusive slot range. */
@@ -1019,12 +1027,20 @@ export class ProtoArray {
1019
1027
  }
1020
1028
 
1021
1029
  /**
1022
- * Optimistic sync validate till validated latest hash, invalidate any descendant branch
1023
- * if invalidate till hash provided. If consensus fails, this will invalidate entire
1024
- * forkChoice which will throw on any call to findHead
1030
+ * Apply an execution engine verdict with a latest valid hash (LVH) to the tree. VALID flips Syncing nodes up to the
1031
+ * LVH node. INVALID invalidates the node identified by `invalidateFromBlockRoot/Hash` and its ancestors up to
1032
+ * the LVH node (exclusive), then every descendant. Returns whether any status changed, and if not, why.
1033
+ *
1034
+ * A verdict never contradicts what fork choice already holds: a node the engine already resolved is never flipped
1035
+ * and an INVALID never crosses the justified node. The LVH is unusable when it is null, not an ancestor of the from
1036
+ * node, or the walk to it meets such a node.
1037
+ *
1038
+ * An unusable LVH leaves only the claim about the judged payload. With `LVHInvalidJudged.FromBlock` that is the from
1039
+ * node: it and the nodes that inherit its payload (gloas PENDING and EMPTY variants) are invalidated, nothing above
1040
+ * them. With `LVHInvalidJudged.ChildPayload` the judged payload is not in the tree and the from node is its parent,
1041
+ * so the verdict is dropped.
1025
1042
  */
1026
- // TODO GLOAS: Review usage of this post-gloas
1027
- validateLatestHash(execResponse: LVHExecResponse, currentSlot: Slot): void {
1043
+ validateLatestHash(execResponse: LVHExecResponse, currentSlot: Slot): LVHVerdictResult {
1028
1044
  // Look reverse because its highly likely node with latestValidExecHash is towards the
1029
1045
  // the leaves of the forkchoice
1030
1046
  //
@@ -1035,71 +1051,134 @@ export class ProtoArray {
1035
1051
  // https://github.com/ChainSafe/lodestar/pull/4182#discussion_r914770167
1036
1052
 
1037
1053
  if (execResponse.executionStatus === ExecutionStatus.Valid) {
1038
- const {latestValidExecHash} = execResponse;
1054
+ const {latestValidExecHash, blockRoot} = execResponse;
1039
1055
  // We use -1 for denoting not found
1040
1056
  let latestValidHashIndex = -1;
1041
1057
 
1042
- for (let nodeIndex = this.nodes.length - 1; nodeIndex >= 0; nodeIndex--) {
1043
- if (this.nodes[nodeIndex].executionPayloadBlockHash === latestValidExecHash) {
1044
- latestValidHashIndex = nodeIndex;
1045
- // We found the block corresponding to latestValidHashIndex, exit the loop
1046
- break;
1058
+ if (blockRoot !== undefined) {
1059
+ latestValidHashIndex = this.getNodeIndexByRootAndBlockHash(blockRoot, latestValidExecHash) ?? -1;
1060
+ } else {
1061
+ for (let nodeIndex = this.nodes.length - 1; nodeIndex >= 0; nodeIndex--) {
1062
+ if (this.nodes[nodeIndex].executionPayloadBlockHash === latestValidExecHash) {
1063
+ latestValidHashIndex = nodeIndex;
1064
+ // We found the block corresponding to latestValidHashIndex, exit the loop
1065
+ break;
1066
+ }
1047
1067
  }
1048
1068
  }
1049
1069
 
1050
1070
  // We are trying to be as forgiving as possible here because ideally latestValidHashIndex
1051
1071
  // should be found in the forkchoice
1052
- if (latestValidHashIndex >= 0) {
1053
- this.propagateValidExecutionStatusByIndex(latestValidHashIndex);
1072
+ if (latestValidHashIndex < 0) {
1073
+ return {applied: false, reason: LVHNotAppliedReason.UnknownLVH};
1054
1074
  }
1055
- } else {
1056
- // In case of invalidation, ideally:
1057
- // i) Find the invalid payload
1058
- // ii) Obtain a chain [LVH.child, LVH.child.child, ....., invalid_payload]
1059
- // iii) Obtain a chain [Last_known_valid_node, ...., LVH]
1060
- //
1061
- // Mark chain iii) as Valid if LVH is non null but right now LVH can be non null without
1062
- // gurranteing chain iii) to be valid: for e.g. in following scenario LVH can be returned
1063
- // as any of SYNCING: SYNCING, SYNCING, SYNCING, INVALID (due to simple check)/
1064
- // So we currently ignore this chain and hope eventually it gets resolved
1065
- //
1066
- // Mark chain ii) as Invalid if LVH is found and non null, else only invalidate invalid_payload
1067
- // if its in fcU.
1068
- //
1069
- const {invalidateFromParentBlockRoot, invalidateFromParentBlockHash, latestValidExecHash} = execResponse;
1070
- const invalidateFromParentIndex = this.getNodeIndexByRootAndBlockHash(
1071
- invalidateFromParentBlockRoot,
1072
- invalidateFromParentBlockHash
1075
+ const {executionStatus} = this.nodes[latestValidHashIndex];
1076
+ if (executionStatus === ExecutionStatus.Valid || executionStatus === ExecutionStatus.PreMerge) {
1077
+ return {applied: false, reason: LVHNotAppliedReason.AlreadyApplied};
1078
+ }
1079
+ // The walk up flips every Syncing ancestor, it must not contradict an invalidated one
1080
+ let nodeIndex: number | undefined = latestValidHashIndex;
1081
+ while (nodeIndex !== undefined) {
1082
+ const node: ProtoNode = this.nodes[nodeIndex];
1083
+ if (node.executionStatus === ExecutionStatus.Valid || node.executionStatus === ExecutionStatus.PreMerge) {
1084
+ break;
1085
+ }
1086
+ if (node.executionStatus === ExecutionStatus.Invalid) {
1087
+ return {applied: false, reason: LVHNotAppliedReason.Contradiction};
1088
+ }
1089
+ nodeIndex = node.parent;
1090
+ }
1091
+ this.propagateValidExecutionStatusByIndex(latestValidHashIndex);
1092
+ return {applied: true};
1093
+ }
1094
+
1095
+ // In case of invalidation, ideally:
1096
+ // i) Find the invalid payload
1097
+ // ii) Obtain a chain [LVH.child, LVH.child.child, ....., invalid_payload]
1098
+ // iii) Obtain a chain [Last_known_valid_node, ...., LVH]
1099
+ //
1100
+ // Mark chain iii) as Valid if LVH is non null but right now LVH can be non null without
1101
+ // gurranteing chain iii) to be valid: for e.g. in following scenario LVH can be returned
1102
+ // as any of SYNCING: SYNCING, SYNCING, SYNCING, INVALID (due to simple check)/
1103
+ // So we currently ignore this chain and hope eventually it gets resolved
1104
+ //
1105
+ // Mark chain ii) as Invalid if LVH is found and non null, else only invalidate invalid_payload
1106
+ // if its in fcU.
1107
+ //
1108
+ const {invalidateFromBlockRoot, invalidateFromBlockHash, latestValidExecHash, judged} = execResponse;
1109
+ const invalidateFromIndex = this.getNodeIndexByRootAndBlockHash(invalidateFromBlockRoot, invalidateFromBlockHash);
1110
+ if (invalidateFromIndex === undefined) {
1111
+ throw Error(
1112
+ `Unable to find invalidateFromBlockRoot=${invalidateFromBlockRoot} invalidateFromBlockHash=${invalidateFromBlockHash} in forkChoice`
1073
1113
  );
1074
- if (invalidateFromParentIndex === undefined) {
1075
- throw Error(
1076
- `Unable to find invalidateFromParentBlockRoot=${invalidateFromParentBlockRoot} invalidateFromParentBlockHash=${invalidateFromParentBlockHash} in forkChoice`
1077
- );
1114
+ }
1115
+ const fromStatus = this.nodes[invalidateFromIndex].executionStatus;
1116
+ if (fromStatus !== ExecutionStatus.Syncing) {
1117
+ return {
1118
+ applied: false,
1119
+ reason:
1120
+ fromStatus === ExecutionStatus.Invalid
1121
+ ? LVHNotAppliedReason.AlreadyApplied
1122
+ : LVHNotAppliedReason.Contradiction,
1123
+ };
1124
+ }
1125
+
1126
+ let latestValidHashIndex =
1127
+ latestValidExecHash !== null ? this.getNodeIndexFromLVH(latestValidExecHash, invalidateFromIndex) : null;
1128
+ if (latestValidHashIndex === null) {
1129
+ if (judged === LVHInvalidJudged.ChildPayload) {
1130
+ return {applied: false, reason: LVHNotAppliedReason.UnusableLVH};
1078
1131
  }
1079
- const latestValidHashIndex =
1080
- latestValidExecHash !== null ? this.getNodeIndexFromLVH(latestValidExecHash, invalidateFromParentIndex) : null;
1081
- if (latestValidHashIndex === null) {
1082
- /**
1083
- * The LVH (latest valid hash) is null or not found.
1084
- *
1085
- * The spec gives an allowance for the EL being able to return a nullish LVH if it could not
1086
- * "determine" one. There are two interpretations:
1087
- *
1088
- * - "the LVH is unknown" - simply throw and move on. We can't determine which chain to invalidate
1089
- * since we don't know which ancestor is valid.
1090
- *
1091
- * - "the LVH doesn't exist" - this means that the entire ancestor chain is invalid, and should
1092
- * be marked as such.
1093
- *
1094
- * The more robust approach is to treat nullish LVH as "the LVH is unknown" rather than
1095
- * "the LVH doesn't exist". The alternative means that we will poison a valid chain when the
1096
- * EL is lazy (or buggy) with its LVH response.
1097
- */
1098
- throw Error(`Unable to find latestValidExecHash=${latestValidExecHash} in the forkchoice`);
1132
+ latestValidHashIndex = this.getParentPayloadIndex(invalidateFromIndex);
1133
+ }
1134
+
1135
+ // Never invalidate the justified block, the EMPTY variant sharing its chain, or anything at or below it in
1136
+ // the tree. The justified block's own payload (FULL variant) is not part of the checkpoint
1137
+ const justifiedIndex = this.getDefaultNodeIndex(this.justifiedRoot) ?? -1;
1138
+ let nodeIndex: number | undefined = invalidateFromIndex;
1139
+ while (nodeIndex !== undefined && nodeIndex > latestValidHashIndex) {
1140
+ const node: ProtoNode = this.nodes[nodeIndex];
1141
+ const conflict = node.executionStatus !== ExecutionStatus.Syncing;
1142
+ const justified =
1143
+ nodeIndex <= justifiedIndex ||
1144
+ (node.blockRoot === this.justifiedRoot && node.payloadStatus === PayloadStatus.EMPTY);
1145
+ if (conflict || justified) {
1146
+ if (judged === LVHInvalidJudged.FromBlock) {
1147
+ // The LVH is unusable: invalidate only the judged payload, but never cross this node
1148
+ latestValidHashIndex = Math.max(nodeIndex, this.getParentPayloadIndex(invalidateFromIndex));
1149
+ } else if (conflict) {
1150
+ return {applied: false, reason: LVHNotAppliedReason.Contradiction};
1151
+ } else {
1152
+ // The LVH is below the justified node, it still covers everything above it
1153
+ latestValidHashIndex = nodeIndex;
1154
+ }
1155
+ break;
1099
1156
  }
1157
+ nodeIndex = node.parent;
1158
+ }
1100
1159
 
1101
- this.propagateInValidExecutionStatusByIndex(invalidateFromParentIndex, latestValidHashIndex, currentSlot);
1160
+ if (invalidateFromIndex <= latestValidHashIndex) {
1161
+ return {applied: false, reason: LVHNotAppliedReason.NothingToInvalidate};
1102
1162
  }
1163
+
1164
+ this.propagateInValidExecutionStatusByIndex(invalidateFromIndex, latestValidHashIndex, currentSlot);
1165
+ return {applied: true};
1166
+ }
1167
+
1168
+ /**
1169
+ * Index of the nearest ancestor carrying a different payload than `fromIndex`, i.e. the node holding its parent
1170
+ * payload. Pre-gloas this is the parent of `fromIndex`. For a gloas EMPTY or PENDING node it skips past the FULL
1171
+ * node that owns the payload. -1 when the chain reaches the root.
1172
+ */
1173
+ private getParentPayloadIndex(fromIndex: number): number {
1174
+ const payloadBlockHash = this.nodes[fromIndex].executionPayloadBlockHash;
1175
+ let nodeIndex = fromIndex;
1176
+ let parentIndex = this.nodes[nodeIndex].parent;
1177
+ while (parentIndex !== undefined && this.nodes[parentIndex].executionPayloadBlockHash === payloadBlockHash) {
1178
+ nodeIndex = parentIndex;
1179
+ parentIndex = this.nodes[nodeIndex].parent;
1180
+ }
1181
+ return parentIndex ?? -1;
1103
1182
  }
1104
1183
 
1105
1184
  private propagateValidExecutionStatusByIndex(validNodeIndex: number): void {
@@ -1125,19 +1204,23 @@ export class ProtoArray {
1125
1204
  }
1126
1205
 
1127
1206
  /**
1128
- * Gloas PENDING and EMPTY variants have no payload of their own, they inherit the execution status of
1129
- * the payload they build on, which is their `executionPayloadBlockHash`. Once that payload is VALID, all
1130
- * of them are, not only the ones on the chain the validation walked up (e.g. siblings, or blocks
1131
- * building on a sibling's EMPTY variant).
1207
+ * A node's execution status is that of the payload in its `executionPayloadBlockHash`: its own for FULL, the
1208
+ * inherited parent payload for gloas PENDING and EMPTY. Once that payload is VALID every node carrying it is, not
1209
+ * only the chain the validation walked up (e.g. siblings, blocks on a sibling's EMPTY variant, or an equivocating
1210
+ * block with the same payload). A node is only flipped below a Valid parent, otherwise a later INVALID for the
1211
+ * parent would reach it. A parent has a lower index than its children, so the scan updates it first.
1132
1212
  */
1133
1213
  private validatePayloadDependents(payloadHashes: Set<RootHex>): void {
1134
1214
  for (let nodeIndex = 0; nodeIndex < this.nodes.length; nodeIndex++) {
1135
1215
  const node = this.nodes[nodeIndex];
1216
+ const parentStatus = node.parent !== undefined ? this.nodes[node.parent].executionStatus : undefined;
1136
1217
  if (
1137
- node.payloadStatus === PayloadStatus.PENDING &&
1138
1218
  node.executionStatus === ExecutionStatus.Syncing &&
1139
1219
  node.executionPayloadBlockHash !== null &&
1140
- payloadHashes.has(node.executionPayloadBlockHash)
1220
+ payloadHashes.has(node.executionPayloadBlockHash) &&
1221
+ (parentStatus === undefined ||
1222
+ parentStatus === ExecutionStatus.Valid ||
1223
+ parentStatus === ExecutionStatus.PreMerge)
1141
1224
  ) {
1142
1225
  // Also flips the sibling EMPTY variant
1143
1226
  this.validateNodeByIndex(nodeIndex);
@@ -1156,12 +1239,12 @@ export class ProtoArray {
1156
1239
  */
1157
1240
 
1158
1241
  private propagateInValidExecutionStatusByIndex(
1159
- invalidateFromParentIndex: number,
1242
+ invalidateFromIndex: number,
1160
1243
  latestValidHashIndex: number,
1161
1244
  currentSlot: Slot
1162
1245
  ): void {
1163
- // Pass 1: mark invalidateFromParentIndex and its parents invalid
1164
- let invalidateIndex: number | undefined = invalidateFromParentIndex;
1246
+ // Pass 1: mark invalidateFromIndex and its parents invalid
1247
+ let invalidateIndex: number | undefined = invalidateFromIndex;
1165
1248
  while (invalidateIndex !== undefined && invalidateIndex > latestValidHashIndex) {
1166
1249
  const invalidNode = this.invalidateNodeByIndex(invalidateIndex);
1167
1250
  invalidateIndex = invalidNode.parent;
@@ -1210,28 +1293,19 @@ export class ProtoArray {
1210
1293
  private invalidateNodeByIndex(nodeIndex: number): ProtoNode {
1211
1294
  const invalidNode = this.getNodeFromIndex(nodeIndex);
1212
1295
 
1213
- // If node to be invalidated is pre-merge or valid,it is a catastrophe,
1214
- // and indicates consensus failure and a non recoverable damage.
1215
- //
1216
- // There is no further processing that can be done.
1217
- // Just assign error for marking proto-array perma damaged and throw!
1296
+ // validateLatestHash never starts an invalidation that reaches a pre-merge or valid node
1218
1297
  if (
1219
1298
  invalidNode.executionStatus === ExecutionStatus.Valid ||
1220
1299
  invalidNode.executionStatus === ExecutionStatus.PreMerge
1221
1300
  ) {
1222
- const lvhCode =
1223
- invalidNode.executionStatus === ExecutionStatus.Valid
1224
- ? LVHExecErrorCode.ValidToInvalid
1225
- : LVHExecErrorCode.PreMergeToInvalid;
1226
-
1227
- this.lvhError = {
1228
- lvhCode,
1301
+ throw new ProtoArrayError({
1302
+ code: ProtoArrayErrorCode.INVALID_EXECUTION_STATUS_TRANSITION,
1303
+ transition:
1304
+ invalidNode.executionStatus === ExecutionStatus.Valid
1305
+ ? ExecutionStatusTransition.ValidToInvalid
1306
+ : ExecutionStatusTransition.PreMergeToInvalid,
1229
1307
  blockRoot: invalidNode.blockRoot,
1230
1308
  execHash: invalidNode.executionPayloadBlockHash ?? ZERO_HASH_HEX,
1231
- };
1232
- throw new ProtoArrayError({
1233
- code: ProtoArrayErrorCode.INVALID_LVH_EXECUTION_RESPONSE,
1234
- ...this.lvhError,
1235
1309
  });
1236
1310
  }
1237
1311
 
@@ -1252,14 +1326,11 @@ export class ProtoArray {
1252
1326
  private validateNodeByIndex(nodeIndex: number): ProtoNode {
1253
1327
  const validNode = this.getNodeFromIndex(nodeIndex);
1254
1328
  if (validNode.executionStatus === ExecutionStatus.Invalid) {
1255
- this.lvhError = {
1256
- lvhCode: LVHExecErrorCode.InvalidToValid,
1329
+ throw new ProtoArrayError({
1330
+ code: ProtoArrayErrorCode.INVALID_EXECUTION_STATUS_TRANSITION,
1331
+ transition: ExecutionStatusTransition.InvalidToValid,
1257
1332
  blockRoot: validNode.blockRoot,
1258
1333
  execHash: validNode.executionPayloadBlockHash,
1259
- };
1260
- throw new ProtoArrayError({
1261
- code: ProtoArrayErrorCode.INVALID_LVH_EXECUTION_RESPONSE,
1262
- ...this.lvhError,
1263
1334
  });
1264
1335
  }
1265
1336
 
@@ -1295,6 +1366,15 @@ export class ProtoArray {
1295
1366
 
1296
1367
  // For Gloas: check if from previous slot
1297
1368
  if (node.slot + 1 !== currentSlot) {
1369
+ // During the block's own slot a payload known to not satisfy the inclusion list
1370
+ // constraints ranks below its EMPTY variant, as it will from the next slot on
1371
+ if (
1372
+ node.slot === currentSlot &&
1373
+ node.payloadStatus === PayloadStatus.FULL &&
1374
+ this.payloadInclusionListSatisfaction.get(node.blockRoot) === false
1375
+ ) {
1376
+ return PayloadStatus.PENDING;
1377
+ }
1298
1378
  return node.payloadStatus;
1299
1379
  }
1300
1380
 
@@ -1316,13 +1396,6 @@ export class ProtoArray {
1316
1396
  * For Gloas, may return PENDING/EMPTY/FULL variants.
1317
1397
  */
1318
1398
  findHead(justifiedRoot: RootHex, currentSlot: Slot): ProtoNode {
1319
- if (this.lvhError) {
1320
- throw new ProtoArrayError({
1321
- code: ProtoArrayErrorCode.INVALID_LVH_EXECUTION_RESPONSE,
1322
- ...this.lvhError,
1323
- });
1324
- }
1325
-
1326
1399
  // Get canonical node: FULL for pre-Gloas, PENDING for Gloas
1327
1400
  const justifiedIndex = this.getDefaultNodeIndex(justifiedRoot);
1328
1401
  if (justifiedIndex === undefined) {