@lodestar/beacon-node 2.1.0-dev.56e0e09e0d → 2.1.0-dev.5aa241fdf6

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 (153) hide show
  1. package/lib/api/impl/beacon/blocks/index.d.ts.map +1 -1
  2. package/lib/api/impl/beacon/blocks/index.js +1 -3
  3. package/lib/api/impl/beacon/blocks/index.js.map +1 -1
  4. package/lib/api/impl/beacon/pool/index.d.ts.map +1 -1
  5. package/lib/api/impl/beacon/pool/index.js +8 -0
  6. package/lib/api/impl/beacon/pool/index.js.map +1 -1
  7. package/lib/api/impl/beacon/state/index.d.ts.map +1 -1
  8. package/lib/api/impl/beacon/state/index.js +3 -2
  9. package/lib/api/impl/beacon/state/index.js.map +1 -1
  10. package/lib/api/impl/beacon/state/utils.js +1 -1
  11. package/lib/api/impl/beacon/state/utils.js.map +1 -1
  12. package/lib/api/impl/validator/index.js +1 -1
  13. package/lib/api/impl/validator/index.js.map +1 -1
  14. package/lib/chain/ColumnReconstructionTracker.d.ts +3 -0
  15. package/lib/chain/ColumnReconstructionTracker.d.ts.map +1 -1
  16. package/lib/chain/ColumnReconstructionTracker.js +3 -1
  17. package/lib/chain/ColumnReconstructionTracker.js.map +1 -1
  18. package/lib/chain/GetBlobsTracker.d.ts +3 -0
  19. package/lib/chain/GetBlobsTracker.d.ts.map +1 -1
  20. package/lib/chain/GetBlobsTracker.js +3 -1
  21. package/lib/chain/GetBlobsTracker.js.map +1 -1
  22. package/lib/chain/blocks/importBlock.d.ts.map +1 -1
  23. package/lib/chain/blocks/importBlock.js +14 -5
  24. package/lib/chain/blocks/importBlock.js.map +1 -1
  25. package/lib/chain/blocks/importExecutionPayload.d.ts.map +1 -1
  26. package/lib/chain/blocks/importExecutionPayload.js +4 -2
  27. package/lib/chain/blocks/importExecutionPayload.js.map +1 -1
  28. package/lib/chain/blocks/payloadEnvelopeInput/types.d.ts +2 -1
  29. package/lib/chain/blocks/payloadEnvelopeInput/types.d.ts.map +1 -1
  30. package/lib/chain/chain.d.ts +2 -1
  31. package/lib/chain/chain.d.ts.map +1 -1
  32. package/lib/chain/chain.js +29 -6
  33. package/lib/chain/chain.js.map +1 -1
  34. package/lib/chain/errors/dataColumnSidecarError.d.ts +7 -0
  35. package/lib/chain/errors/dataColumnSidecarError.d.ts.map +1 -1
  36. package/lib/chain/errors/dataColumnSidecarError.js +1 -0
  37. package/lib/chain/errors/dataColumnSidecarError.js.map +1 -1
  38. package/lib/chain/forkChoice/index.d.ts +11 -2
  39. package/lib/chain/forkChoice/index.d.ts.map +1 -1
  40. package/lib/chain/forkChoice/index.js +181 -13
  41. package/lib/chain/forkChoice/index.js.map +1 -1
  42. package/lib/chain/interface.d.ts +2 -1
  43. package/lib/chain/interface.d.ts.map +1 -1
  44. package/lib/chain/interface.js.map +1 -1
  45. package/lib/chain/opPools/opPool.d.ts +9 -3
  46. package/lib/chain/opPools/opPool.d.ts.map +1 -1
  47. package/lib/chain/opPools/opPool.js +22 -16
  48. package/lib/chain/opPools/opPool.js.map +1 -1
  49. package/lib/chain/options.d.ts.map +1 -1
  50. package/lib/chain/options.js +1 -0
  51. package/lib/chain/options.js.map +1 -1
  52. package/lib/chain/seenCache/index.d.ts +1 -0
  53. package/lib/chain/seenCache/index.d.ts.map +1 -1
  54. package/lib/chain/seenCache/index.js +1 -0
  55. package/lib/chain/seenCache/index.js.map +1 -1
  56. package/lib/chain/seenCache/seenBlockProposers.d.ts +1 -0
  57. package/lib/chain/seenCache/seenBlockProposers.d.ts.map +1 -1
  58. package/lib/chain/seenCache/seenBlockProposers.js +5 -1
  59. package/lib/chain/seenCache/seenBlockProposers.js.map +1 -1
  60. package/lib/chain/seenCache/seenDataColumnSidecars.d.ts +16 -0
  61. package/lib/chain/seenCache/seenDataColumnSidecars.d.ts.map +1 -0
  62. package/lib/chain/seenCache/seenDataColumnSidecars.js +56 -0
  63. package/lib/chain/seenCache/seenDataColumnSidecars.js.map +1 -0
  64. package/lib/chain/seenCache/seenPayloadEnvelopeInput.d.ts +11 -2
  65. package/lib/chain/seenCache/seenPayloadEnvelopeInput.d.ts.map +1 -1
  66. package/lib/chain/seenCache/seenPayloadEnvelopeInput.js +28 -2
  67. package/lib/chain/seenCache/seenPayloadEnvelopeInput.js.map +1 -1
  68. package/lib/chain/validation/attestation.d.ts.map +1 -1
  69. package/lib/chain/validation/attestation.js +4 -2
  70. package/lib/chain/validation/attestation.js.map +1 -1
  71. package/lib/chain/validation/block.d.ts.map +1 -1
  72. package/lib/chain/validation/block.js +20 -7
  73. package/lib/chain/validation/block.js.map +1 -1
  74. package/lib/chain/validation/dataColumnSidecar.js +1 -1
  75. package/lib/chain/validation/dataColumnSidecar.js.map +1 -1
  76. package/lib/chain/validation/proposerSlashing.js +5 -1
  77. package/lib/chain/validation/proposerSlashing.js.map +1 -1
  78. package/lib/network/forks.d.ts +2 -2
  79. package/lib/network/forks.d.ts.map +1 -1
  80. package/lib/network/forks.js +3 -3
  81. package/lib/network/forks.js.map +1 -1
  82. package/lib/network/gossip/scoringParameters.js +19 -14
  83. package/lib/network/gossip/scoringParameters.js.map +1 -1
  84. package/lib/network/processor/gossipHandlers.d.ts.map +1 -1
  85. package/lib/network/processor/gossipHandlers.js +75 -9
  86. package/lib/network/processor/gossipHandlers.js.map +1 -1
  87. package/lib/network/reqresp/handlers/dataColumnSidecarsByRange.d.ts.map +1 -1
  88. package/lib/network/reqresp/handlers/dataColumnSidecarsByRange.js +3 -1
  89. package/lib/network/reqresp/handlers/dataColumnSidecarsByRange.js.map +1 -1
  90. package/lib/sync/range/chain.d.ts.map +1 -1
  91. package/lib/sync/range/chain.js +1 -0
  92. package/lib/sync/range/chain.js.map +1 -1
  93. package/lib/sync/range/range.d.ts.map +1 -1
  94. package/lib/sync/range/range.js +5 -1
  95. package/lib/sync/range/range.js.map +1 -1
  96. package/lib/sync/unknownBlock.d.ts.map +1 -1
  97. package/lib/sync/unknownBlock.js +14 -1
  98. package/lib/sync/unknownBlock.js.map +1 -1
  99. package/lib/sync/utils/downloadByRange.d.ts +6 -0
  100. package/lib/sync/utils/downloadByRange.d.ts.map +1 -1
  101. package/lib/sync/utils/downloadByRange.js +17 -8
  102. package/lib/sync/utils/downloadByRange.js.map +1 -1
  103. package/lib/sync/utils/downloadByRoot.d.ts.map +1 -1
  104. package/lib/sync/utils/downloadByRoot.js +1 -1
  105. package/lib/sync/utils/downloadByRoot.js.map +1 -1
  106. package/lib/sync/utils/pendingBlocksTree.d.ts.map +1 -1
  107. package/lib/sync/utils/pendingBlocksTree.js +6 -1
  108. package/lib/sync/utils/pendingBlocksTree.js.map +1 -1
  109. package/lib/util/dataColumns.d.ts +2 -1
  110. package/lib/util/dataColumns.d.ts.map +1 -1
  111. package/lib/util/dataColumns.js +7 -2
  112. package/lib/util/dataColumns.js.map +1 -1
  113. package/lib/util/execution.d.ts +2 -1
  114. package/lib/util/execution.d.ts.map +1 -1
  115. package/lib/util/execution.js +7 -2
  116. package/lib/util/execution.js.map +1 -1
  117. package/package.json +17 -20
  118. package/src/api/impl/beacon/blocks/index.ts +1 -5
  119. package/src/api/impl/beacon/pool/index.ts +11 -0
  120. package/src/api/impl/beacon/state/index.ts +3 -7
  121. package/src/api/impl/beacon/state/utils.ts +1 -1
  122. package/src/api/impl/validator/index.ts +1 -1
  123. package/src/chain/ColumnReconstructionTracker.ts +5 -1
  124. package/src/chain/GetBlobsTracker.ts +5 -0
  125. package/src/chain/blocks/importBlock.ts +14 -7
  126. package/src/chain/blocks/importExecutionPayload.ts +4 -2
  127. package/src/chain/blocks/payloadEnvelopeInput/types.ts +8 -1
  128. package/src/chain/chain.ts +34 -5
  129. package/src/chain/errors/dataColumnSidecarError.ts +8 -0
  130. package/src/chain/forkChoice/index.ts +235 -13
  131. package/src/chain/interface.ts +2 -0
  132. package/src/chain/opPools/opPool.ts +27 -16
  133. package/src/chain/options.ts +1 -0
  134. package/src/chain/seenCache/index.ts +1 -0
  135. package/src/chain/seenCache/seenBlockProposers.ts +11 -1
  136. package/src/chain/seenCache/seenDataColumnSidecars.ts +64 -0
  137. package/src/chain/seenCache/seenPayloadEnvelopeInput.ts +28 -2
  138. package/src/chain/validation/attestation.ts +4 -2
  139. package/src/chain/validation/block.ts +19 -7
  140. package/src/chain/validation/dataColumnSidecar.ts +1 -1
  141. package/src/chain/validation/proposerSlashing.ts +5 -1
  142. package/src/network/forks.ts +7 -3
  143. package/src/network/gossip/scoringParameters.ts +23 -14
  144. package/src/network/processor/gossipHandlers.ts +134 -17
  145. package/src/network/reqresp/handlers/dataColumnSidecarsByRange.ts +4 -1
  146. package/src/sync/range/chain.ts +1 -0
  147. package/src/sync/range/range.ts +7 -1
  148. package/src/sync/unknownBlock.ts +20 -1
  149. package/src/sync/utils/downloadByRange.ts +30 -7
  150. package/src/sync/utils/downloadByRoot.ts +12 -9
  151. package/src/sync/utils/pendingBlocksTree.ts +6 -1
  152. package/src/util/dataColumns.ts +13 -1
  153. package/src/util/execution.ts +16 -1
@@ -114,16 +114,12 @@ export function getBeaconStateApi({
114
114
  if (resp.valid) {
115
115
  const validatorIndex = resp.validatorIndex;
116
116
  const validator = state.getValidator(validatorIndex);
117
- const status = getValidatorStatus(validator, currentEpoch);
117
+ const balance = state.getBalance(validatorIndex);
118
+ const status = getValidatorStatus(validator, balance, currentEpoch);
118
119
  if (statuses.length && !statuses.includes(status) && !statuses.includes(mapToGeneralStatus(status))) {
119
120
  continue;
120
121
  }
121
- const validatorResponse = toValidatorResponse(
122
- validatorIndex,
123
- validator,
124
- state.getBalance(validatorIndex),
125
- currentEpoch
126
- );
122
+ const validatorResponse = toValidatorResponse(validatorIndex, validator, balance, currentEpoch);
127
123
  validatorResponses.push(validatorResponse);
128
124
  }
129
125
  }
@@ -109,7 +109,7 @@ export function toValidatorResponse(
109
109
  ): routes.beacon.ValidatorResponse {
110
110
  return {
111
111
  index,
112
- status: getValidatorStatus(validator, currentEpoch),
112
+ status: getValidatorStatus(validator, balance, currentEpoch),
113
113
  balance,
114
114
  validator,
115
115
  };
@@ -2174,7 +2174,7 @@ export function getValidatorApi(
2174
2174
  if (validatorIndex === null || validatorIndex >= headState.validatorCount) return false;
2175
2175
 
2176
2176
  const validator = headState.getValidator(validatorIndex);
2177
- const status = getValidatorStatus(validator, currentEpoch);
2177
+ const status = getValidatorStatus(validator, headState.getBalance(validatorIndex), currentEpoch);
2178
2178
  return (
2179
2179
  status === "active_exiting" ||
2180
2180
  status === "active_ongoing" ||
@@ -8,6 +8,7 @@ import {JobItemQueue} from "../util/queue/index.js";
8
8
  import {BlockInputColumns} from "./blocks/blockInput/index.js";
9
9
  import {PayloadEnvelopeInput} from "./blocks/payloadEnvelopeInput/index.js";
10
10
  import {ChainEventEmitter} from "./emitter.js";
11
+ import {SeenDataColumnSidecars} from "./seenCache/seenDataColumnSidecars.js";
11
12
 
12
13
  /**
13
14
  * Minimum time to wait before attempting reconstruction
@@ -31,6 +32,7 @@ export type ColumnReconstructionInput = BlockInputColumns | PayloadEnvelopeInput
31
32
  export type ColumnReconstructionTrackerInit = {
32
33
  logger: Logger;
33
34
  emitter: ChainEventEmitter;
35
+ seenDataColumnSidecars: SeenDataColumnSidecars;
34
36
  metrics: Metrics | null;
35
37
  config: ChainForkConfig;
36
38
  signal: AbortSignal;
@@ -44,6 +46,7 @@ export type ColumnReconstructionTrackerInit = {
44
46
  export class ColumnReconstructionTracker {
45
47
  logger: Logger;
46
48
  emitter: ChainEventEmitter;
49
+ seenDataColumnSidecars: SeenDataColumnSidecars;
47
50
  metrics: Metrics | null;
48
51
  config: ChainForkConfig;
49
52
 
@@ -54,6 +57,7 @@ export class ColumnReconstructionTracker {
54
57
  constructor(init: ColumnReconstructionTrackerInit) {
55
58
  this.logger = init.logger;
56
59
  this.emitter = init.emitter;
60
+ this.seenDataColumnSidecars = init.seenDataColumnSidecars;
57
61
  this.metrics = init.metrics;
58
62
  this.config = init.config;
59
63
  this.signal = init.signal;
@@ -104,7 +108,7 @@ export class ColumnReconstructionTracker {
104
108
  const logCtx = {slot: input.slot, root: input.blockRootHex};
105
109
  this.logger.debug("Attempting data column sidecar reconstruction", logCtx);
106
110
  try {
107
- const result = await recoverDataColumnSidecars(input, this.emitter, this.metrics);
111
+ const result = await recoverDataColumnSidecars(input, this.emitter, this.seenDataColumnSidecars, this.metrics);
108
112
  this.metrics?.recoverDataColumnSidecars.reconstructionResult.inc({result});
109
113
  this.logger.debug("Data column sidecar reconstruction complete", {...logCtx, result});
110
114
  } catch (e) {
@@ -8,6 +8,7 @@ import {DataColumnEngineResult, getDataColumnSidecarsFromExecution} from "../uti
8
8
  import {IBlockInput, isBlockInputColumns} from "./blocks/blockInput/index.js";
9
9
  import {PayloadEnvelopeInput} from "./blocks/payloadEnvelopeInput/index.js";
10
10
  import {ChainEventEmitter} from "./emitter.js";
11
+ import {SeenDataColumnSidecars} from "./seenCache/seenDataColumnSidecars.js";
11
12
 
12
13
  /** A `null` answer only changes if the EL receives the transactions later, so retries are few and spaced out */
13
14
  export const MAX_GET_BLOBS_ATTEMPTS = 3;
@@ -18,6 +19,7 @@ export type GetBlobsTrackerInit = {
18
19
  logger: Logger;
19
20
  executionEngine: Pick<IExecutionEngine, "getBlobs">;
20
21
  emitter: ChainEventEmitter;
22
+ seenDataColumnSidecars: SeenDataColumnSidecars;
21
23
  metrics: Metrics | null;
22
24
  config: ChainForkConfig;
23
25
  };
@@ -29,6 +31,7 @@ export class GetBlobsTracker {
29
31
  logger: Logger;
30
32
  executionEngine: Pick<IExecutionEngine, "getBlobs">;
31
33
  emitter: ChainEventEmitter;
34
+ seenDataColumnSidecars: SeenDataColumnSidecars;
32
35
  metrics: Metrics | null;
33
36
  config: ChainForkConfig;
34
37
  activeReconstructions = new Set<string>();
@@ -42,6 +45,7 @@ export class GetBlobsTracker {
42
45
  this.logger = init.logger;
43
46
  this.executionEngine = init.executionEngine;
44
47
  this.emitter = init.emitter;
48
+ this.seenDataColumnSidecars = init.seenDataColumnSidecars;
45
49
  this.metrics = init.metrics;
46
50
  this.config = init.config;
47
51
  }
@@ -101,6 +105,7 @@ export class GetBlobsTracker {
101
105
  this.config,
102
106
  this.executionEngine,
103
107
  this.emitter,
108
+ this.seenDataColumnSidecars,
104
109
  input,
105
110
  this.metrics,
106
111
  this.blobsAndProofsBuffers[freeIndex].buffers
@@ -39,6 +39,7 @@ import {
39
39
  } from "@lodestar/types";
40
40
  import {isErrorAborted, toRootHex} from "@lodestar/utils";
41
41
  import {GENESIS_SLOT, ZERO_HASH_HEX} from "../../constants/index.js";
42
+ import {ExecutionPayloadStatus} from "../../execution/engine/interface.js";
42
43
  import {callInNextEventLoop} from "../../util/eventLoop.js";
43
44
  import {isOptimisticBlock} from "../../util/forkChoice.js";
44
45
  import {isQueueErrorAborted} from "../../util/queue/index.js";
@@ -146,13 +147,19 @@ export async function importBlock(
146
147
  dataAvailabilityStatus
147
148
  );
148
149
  } catch (e) {
149
- // The parent can be invalidated by a forkchoiceUpdated verdict after verifyBlocksSanityChecks passed
150
- if (
151
- e instanceof ForkChoiceError &&
152
- e.type.code === ForkChoiceErrorCode.INVALID_BLOCK &&
153
- e.type.err.code === InvalidBlockCode.PARENT_EXECUTION_INVALID
154
- ) {
155
- throw new BlockError(block, {code: BlockErrorCode.PARENT_EXECUTION_INVALID, parentRoot: e.type.err.root});
150
+ if (e instanceof ForkChoiceError && e.type.code === ForkChoiceErrorCode.INVALID_BLOCK) {
151
+ // The parent can be invalidated by a forkchoiceUpdated verdict after verifyBlocksSanityChecks passed
152
+ if (e.type.err.code === InvalidBlockCode.PARENT_EXECUTION_INVALID) {
153
+ throw new BlockError(block, {code: BlockErrorCode.PARENT_EXECUTION_INVALID, parentRoot: e.type.err.root});
154
+ }
155
+ // A pre-Gloas block delivering a payload fork choice already invalidated
156
+ if (e.type.err.code === InvalidBlockCode.EXECUTION_INVALID) {
157
+ throw new BlockError(block, {
158
+ code: BlockErrorCode.EXECUTION_ENGINE_INVALID,
159
+ execStatus: ExecutionPayloadStatus.INVALID,
160
+ errorMessage: `payload ${e.type.err.hash} was invalidated in fork choice`,
161
+ });
162
+ }
156
163
  }
157
164
  throw e;
158
165
  }
@@ -368,11 +368,13 @@ export async function importExecutionPayload(
368
368
  dataAvailabilityStatus
369
369
  );
370
370
  } catch (e) {
371
- // The payload chain can be invalidated by a forkchoiceUpdated verdict while the engine was awaited
371
+ // The payload chain can be invalidated by a forkchoiceUpdated verdict while the engine was awaited, and the
372
+ // payload itself may already be invalidated through another block delivering it
372
373
  if (
373
374
  e instanceof ForkChoiceError &&
374
375
  e.type.code === ForkChoiceErrorCode.INVALID_BLOCK &&
375
- e.type.err.code === InvalidBlockCode.PARENT_EXECUTION_INVALID
376
+ (e.type.err.code === InvalidBlockCode.PARENT_EXECUTION_INVALID ||
377
+ e.type.err.code === InvalidBlockCode.EXECUTION_INVALID)
376
378
  ) {
377
379
  throw new PayloadError(payloadInput, {code: PayloadErrorCode.BLOCK_EXECUTION_INVALID, blockRootHex});
378
380
  }
@@ -22,9 +22,16 @@ export enum PayloadEnvelopeInputSource {
22
22
  * - finalized: below the finalized slot
23
23
  * - prune: explicit prune by root
24
24
  * - cap: insertion-order backstop cap (MAX_PAYLOAD_ENVELOPE_INPUT_CACHE_SIZE)
25
+ * - capUnvalidated: cap eviction of an entry whose block is not validated and imported yet, evicted before the rest
25
26
  * - invalid: the cached envelope failed import with a definitive invalid verdict
26
27
  */
27
- export type PayloadEnvelopeInputPruneReason = "belowParent" | "finalized" | "remove" | "cap" | "invalid";
28
+ export type PayloadEnvelopeInputPruneReason =
29
+ | "belowParent"
30
+ | "finalized"
31
+ | "remove"
32
+ | "cap"
33
+ | "capUnvalidated"
34
+ | "invalid";
28
35
 
29
36
  export type SourceMeta = {
30
37
  source: PayloadEnvelopeInputSource;
@@ -127,6 +127,7 @@ import {
127
127
  SeenAttesters,
128
128
  SeenBlockProposers,
129
129
  SeenContributionAndProof,
130
+ SeenDataColumnSidecars,
130
131
  SeenExecutionPayloadBids,
131
132
  SeenPayloadAttesters,
132
133
  SeenPayloadEnvelopeInput,
@@ -224,6 +225,7 @@ export class BeaconChain implements IBeaconChain {
224
225
  readonly seenAggregatedAttestations: SeenAggregatedAttestations;
225
226
  readonly seenExecutionPayloadBids = new SeenExecutionPayloadBids();
226
227
  readonly seenBlockProposers = new SeenBlockProposers();
228
+ readonly seenDataColumnSidecars = new SeenDataColumnSidecars();
227
229
  /** Proposer indexes with an in-flight proposer slashing production */
228
230
  private readonly producingProposerSlashing = new Set<ValidatorIndex>();
229
231
  readonly seenSyncCommitteeMessages = new SeenSyncCommitteeMessages();
@@ -501,12 +503,14 @@ export class BeaconChain implements IBeaconChain {
501
503
  });
502
504
 
503
505
  const anchorBlockSlot = anchorState.latestBlockHeader.slot;
504
- if (isStatePostGloas(anchorState) && anchorBlockSlot > 0) {
506
+ const anchorBlockFork = config.getForkName(anchorBlockSlot);
507
+ // A pre-Gloas anchor in a state upgraded to Gloas carried its own payload, no envelope follows it
508
+ if (isStatePostGloas(anchorState) && anchorBlockSlot > 0 && isForkPostGloas(anchorBlockFork)) {
505
509
  const anchorBid = anchorState.latestExecutionPayloadBid;
506
510
  this.seenPayloadEnvelopeInputCache.addFromBid({
507
511
  blockRootHex: toRootHex(checkpoint.root),
508
512
  slot: anchorBlockSlot,
509
- forkName: anchorState.forkName,
513
+ forkName: anchorBlockFork,
510
514
  proposerIndex: anchorState.latestBlockHeader.proposerIndex,
511
515
  bid: anchorBid,
512
516
  sampledColumns: this.custodyConfig.sampledColumns,
@@ -525,12 +529,14 @@ export class BeaconChain implements IBeaconChain {
525
529
  logger,
526
530
  executionEngine: this.executionEngine,
527
531
  emitter,
532
+ seenDataColumnSidecars: this.seenDataColumnSidecars,
528
533
  metrics,
529
534
  config,
530
535
  });
531
536
  this.columnReconstructionTracker = new ColumnReconstructionTracker({
532
537
  logger,
533
538
  emitter,
539
+ seenDataColumnSidecars: this.seenDataColumnSidecars,
534
540
  metrics,
535
541
  config,
536
542
  signal,
@@ -2037,9 +2043,18 @@ export class BeaconChain implements IBeaconChain {
2037
2043
 
2038
2044
  const finalizedSlot = computeStartSlotAtEpoch(cp.epoch);
2039
2045
  this.seenBlockProposers.prune(finalizedSlot);
2046
+ this.seenDataColumnSidecars.prune(finalizedSlot);
2040
2047
 
2041
2048
  // Update validator custody to account for effective balance changes
2042
- await this.updateValidatorsCustodyRequirement(cp);
2049
+ try {
2050
+ await this.updateValidatorsCustodyRequirement(cp);
2051
+ } catch (e) {
2052
+ this.logger.warn(
2053
+ "Error updating validators custody requirement",
2054
+ {epoch: cp.epoch, root: cp.rootHex},
2055
+ e as Error
2056
+ );
2057
+ }
2043
2058
 
2044
2059
  // TODO: Improve using regen here
2045
2060
  const {blockRoot, stateRoot, slot} = this.forkChoice.getHead();
@@ -2050,7 +2065,19 @@ export class BeaconChain implements IBeaconChain {
2050
2065
  }
2051
2066
 
2052
2067
  if (headState) {
2053
- this.opPool.pruneAll(blockResult.block, headState);
2068
+ // The finalized checkpoint state may be loaded from disk, it is only needed to prune voluntary exits
2069
+ let finalizedState: IBeaconStateView | Uint8Array | null = null;
2070
+ if (this.opPool.voluntaryExitsSize > 0) {
2071
+ finalizedState = await this.regen.getCheckpointStateOrBytes({epoch: cp.epoch, rootHex: cp.rootHex});
2072
+ if (finalizedState === null) {
2073
+ // A newer finalization can prune this checkpoint state while this handler awaits, its handler prunes exits
2074
+ this.logger.debug("Finalized checkpoint state not available, skip pruning voluntary exits", {
2075
+ epoch: cp.epoch,
2076
+ root: cp.rootHex,
2077
+ });
2078
+ }
2079
+ }
2080
+ this.opPool.pruneAll(blockResult.block, headState, finalizedState);
2054
2081
  }
2055
2082
 
2056
2083
  if (headState === null) {
@@ -2133,7 +2160,9 @@ export class BeaconChain implements IBeaconChain {
2133
2160
  if (stateOrBytes instanceof Uint8Array) {
2134
2161
  effectiveBalances = getEffectiveBalancesFromStateBytes(this.config, stateOrBytes, validatorIndices);
2135
2162
  } else {
2136
- effectiveBalances = validatorIndices.map((index) => stateOrBytes.getValidator(index).effectiveBalance ?? 0);
2163
+ effectiveBalances = validatorIndices.map((index) =>
2164
+ index < stateOrBytes.validatorCount ? stateOrBytes.getValidator(index).effectiveBalance : 0
2165
+ );
2137
2166
  }
2138
2167
  }
2139
2168
 
@@ -35,6 +35,7 @@ export enum DataColumnSidecarErrorCode {
35
35
 
36
36
  // following errors are adapted from the block errors
37
37
  ALREADY_KNOWN = "DATA_COLUMN_SIDECAR_ERROR_ALREADY_KNOWN",
38
+ REPEAT_PROPOSAL = "DATA_COLUMN_SIDECAR_ERROR_REPEAT_PROPOSAL",
38
39
  FUTURE_SLOT = "DATA_COLUMN_SIDECAR_ERROR_FUTURE_SLOT",
39
40
  WOULD_REVERT_FINALIZED_SLOT = "DATA_COLUMN_SIDECAR_ERROR_WOULD_REVERT_FINALIZED_SLOT",
40
41
  PARENT_UNKNOWN = "DATA_COLUMN_SIDECAR_ERROR_PARENT_UNKNOWN",
@@ -79,6 +80,13 @@ export type DataColumnSidecarErrorType =
79
80
  limit: number;
80
81
  }
81
82
  | {code: DataColumnSidecarErrorCode.ALREADY_KNOWN; columnIndex: number; slot: Slot}
83
+ | {
84
+ code: DataColumnSidecarErrorCode.REPEAT_PROPOSAL;
85
+ slot: Slot;
86
+ proposerIndex: number;
87
+ columnIndex: number;
88
+ root: RootHex;
89
+ }
82
90
  | {code: DataColumnSidecarErrorCode.FUTURE_SLOT; blockSlot: Slot; currentSlot: Slot}
83
91
  | {code: DataColumnSidecarErrorCode.WOULD_REVERT_FINALIZED_SLOT; blockSlot: Slot; finalizedSlot: Slot}
84
92
  | {
@@ -5,28 +5,33 @@ import {
5
5
  ForkChoice,
6
6
  ForkChoiceStateGetter,
7
7
  ForkChoiceStore,
8
+ IForkChoice,
8
9
  JustifiedBalancesGetter,
10
+ NativeForkChoice,
9
11
  PayloadStatus,
10
12
  ProtoArray,
11
13
  ProtoBlock,
12
14
  ForkChoiceOpts as RawForkChoiceOpts,
13
15
  } from "@lodestar/fork-choice";
14
- import {ZERO_HASH_HEX} from "@lodestar/params";
16
+ import {ForkSeq, SLOTS_PER_HISTORICAL_ROOT, ZERO_HASH_HEX, isForkPostGloas} from "@lodestar/params";
15
17
  import {
16
18
  DataAvailabilityStatus,
17
19
  IBeaconStateView,
20
+ ZERO_HASH,
18
21
  computeEpochAtSlot,
19
22
  computeStartSlotAtEpoch,
20
23
  isStatePostBellatrix,
21
24
  isStatePostGloas,
22
25
  } from "@lodestar/state-transition";
23
- import {Slot, ssz} from "@lodestar/types";
24
- import {Logger, toRootHex} from "@lodestar/utils";
26
+ import {Slot, phase0, ssz} from "@lodestar/types";
27
+ import {Logger, fromHex, toRootHex} from "@lodestar/utils";
25
28
  import {GENESIS_SLOT} from "../../constants/index.js";
26
29
  import {Metrics} from "../../metrics/index.js";
27
30
  import {ChainEvent, ChainEventEmitter} from "../emitter.js";
28
31
 
29
32
  export type ForkChoiceOpts = RawForkChoiceOpts & {
33
+ /** Use the native (Zig) fork choice store instead of the proto-array */
34
+ nativeForkChoice?: boolean;
30
35
  // for testing only
31
36
  forkchoiceConstructor?: typeof ForkChoice;
32
37
  };
@@ -58,7 +63,22 @@ export function initializeForkChoice(
58
63
  stateGetter: ForkChoiceStateGetter,
59
64
  metrics: Metrics | null,
60
65
  logger?: Logger
61
- ): ForkChoice {
66
+ ): IForkChoice {
67
+ if (opts.nativeForkChoice) {
68
+ return initializeNativeForkChoice(
69
+ config,
70
+ emitter,
71
+ currentSlot,
72
+ state,
73
+ isFinalizedState,
74
+ opts,
75
+ justifiedBalancesGetter,
76
+ stateGetter,
77
+ metrics,
78
+ logger
79
+ );
80
+ }
81
+
62
82
  return isFinalizedState
63
83
  ? initializeForkChoiceFromFinalizedState(
64
84
  config,
@@ -115,7 +135,7 @@ export function initializeForkChoiceFromFinalizedState(
115
135
  // production code use ForkChoice constructor directly
116
136
  const forkchoiceConstructor = opts.forkchoiceConstructor ?? ForkChoice;
117
137
 
118
- const isForkPostGloas = computeEpochAtSlot(state.slot) >= config.GLOAS_FORK_EPOCH;
138
+ const isPostGloasAnchor = isStatePostGloas(state) && isForkPostGloas(config.getForkName(blockHeader.slot));
119
139
 
120
140
  return new forkchoiceConstructor(
121
141
  config,
@@ -164,15 +184,20 @@ export function initializeForkChoiceFromFinalizedState(
164
184
  // TODO GLOAS: executionPayloadNumber/GasLimit are not tracked in BeaconState post-gloas
165
185
  // (EIP-7732 removed latestExecutionPayloadHeader). Using 0 as unavailable fallback —
166
186
  // see initializeForkChoiceFromUnfinalizedState for the same caveat on validation.
187
+ // A pre-Gloas anchor's gas limit is kept in the state's bid by the upgrade.
167
188
  executionPayloadNumber: isStatePostGloas(state) ? 0 : state.payloadBlockNumber,
168
- executionPayloadGasLimit: isStatePostGloas(state) ? 0 : state.latestExecutionPayloadHeader.gasLimit,
189
+ executionPayloadGasLimit: isPostGloasAnchor
190
+ ? 0
191
+ : isStatePostGloas(state)
192
+ ? Number(state.latestExecutionPayloadBid.gasLimit)
193
+ : state.latestExecutionPayloadHeader.gasLimit,
169
194
  executionStatus: blockHeader.slot === GENESIS_SLOT ? ExecutionStatus.Valid : ExecutionStatus.Syncing,
170
195
  }
171
196
  : {executionPayloadBlockHash: null, executionStatus: ExecutionStatus.PreMerge}),
172
197
 
173
198
  dataAvailabilityStatus: DataAvailabilityStatus.PreData,
174
- payloadStatus: isForkPostGloas ? PayloadStatus.PENDING : PayloadStatus.FULL,
175
- parentBlockHash: isStatePostGloas(state) ? toRootHex(state.latestBlockHash) : null,
199
+ payloadStatus: isPostGloasAnchor ? PayloadStatus.PENDING : PayloadStatus.FULL,
200
+ parentBlockHash: isPostGloasAnchor ? toRootHex(state.latestBlockHash) : null,
176
201
  },
177
202
  currentSlot
178
203
  ),
@@ -183,6 +208,200 @@ export function initializeForkChoiceFromFinalizedState(
183
208
  );
184
209
  }
185
210
 
211
+ /**
212
+ * Initialize the native fork choice. A finalized anchor is both the justified and the finalized
213
+ * checkpoint, as in the spec's `get_forkchoice_store`. An unfinalized one keeps its state's
214
+ * checkpoints, with stand-ins for the ancestors that connect it to them: nothing is reported as
215
+ * finalized that the chain has not finalized.
216
+ */
217
+ export function initializeNativeForkChoice(
218
+ config: ChainForkConfig,
219
+ emitter: ChainEventEmitter,
220
+ currentSlot: Slot,
221
+ state: IBeaconStateView,
222
+ isFinalizedState: boolean,
223
+ opts: ForkChoiceOpts,
224
+ justifiedBalancesGetter: JustifiedBalancesGetter,
225
+ stateGetter: ForkChoiceStateGetter,
226
+ metrics: Metrics | null,
227
+ logger?: Logger
228
+ ): NativeForkChoice {
229
+ const {blockHeader, checkpoint} = state.computeAnchorCheckpoint();
230
+ const anchorRoot = toRootHex(checkpoint.root);
231
+ const isPostGloas = isStatePostGloas(state);
232
+ // A state upgraded to Gloas through skipped slots still holds a pre-Gloas block, which carried its own payload
233
+ const isPostGloasAnchor = isPostGloas && config.getForkSeq(blockHeader.slot) >= ForkSeq.gloas;
234
+ const bid = isPostGloasAnchor ? state.latestExecutionPayloadBid : null;
235
+ const hasPayload = isStatePostBellatrix(state) && state.isExecutionStateType && state.isMergeTransitionComplete;
236
+ // The state caches the bid of its latest block, except at genesis: the genesis block carries no bid
237
+ const anchorBid = blockHeader.slot === GENESIS_SLOT ? null : bid;
238
+
239
+ // The store aliases no root to genesis, so a checkpoint the chain never moved off genesis is
240
+ // given the genesis block root. Only possible while that root is still in the state's history.
241
+ const withGenesisRoot = (cp: phase0.Checkpoint): phase0.Checkpoint => {
242
+ if (!ssz.Root.equals(cp.root, ZERO_HASH)) return cp;
243
+ if (state.slot > GENESIS_SLOT + SLOTS_PER_HISTORICAL_ROOT) {
244
+ throw Error(
245
+ `The native fork choice cannot start from an unfinalized state at slot ${state.slot} of a chain that never justified a checkpoint: the genesis block root has left the state's block roots. Start without --chain.nativeForkChoice`
246
+ );
247
+ }
248
+ return {epoch: cp.epoch, root: state.getBlockRootAtSlot(GENESIS_SLOT)};
249
+ };
250
+ const justifiedCheckpoint = isFinalizedState ? checkpoint : withGenesisRoot(state.currentJustifiedCheckpoint);
251
+ const finalizedCheckpoint = isFinalizedState ? checkpoint : withGenesisRoot(state.finalizedCheckpoint);
252
+ const justifiedRoot = toRootHex(justifiedCheckpoint.root);
253
+ const finalizedRoot = toRootHex(finalizedCheckpoint.root);
254
+
255
+ if (!isFinalizedState) {
256
+ logger?.warn("Initializing fork choice from unfinalized state", {
257
+ currentSlot,
258
+ stateSlot: state.slot,
259
+ headSlot: blockHeader.slot,
260
+ headRoot: anchorRoot,
261
+ finalizedEpoch: finalizedCheckpoint.epoch,
262
+ finalizedRoot,
263
+ justifiedEpoch: justifiedCheckpoint.epoch,
264
+ justifiedRoot,
265
+ });
266
+ }
267
+
268
+ const store = new ForkChoiceStore(
269
+ currentSlot,
270
+ justifiedCheckpoint,
271
+ finalizedCheckpoint,
272
+ // For an unfinalized anchor this is not the justified state, but no other state is at hand
273
+ state.getEffectiveBalanceIncrementsZeroInactive(),
274
+ justifiedBalancesGetter,
275
+ stateGetter,
276
+ {
277
+ onJustified: (cp) => emitter.emit(ChainEvent.forkChoiceJustified, cp),
278
+ onFinalized: (cp) => emitter.emit(ChainEvent.forkChoiceFinalized, cp),
279
+ onFastConfirmation: ({block, slot, currentSlot}) =>
280
+ emitter.emit(routes.events.EventType.fastConfirmation, {block, slot, currentSlot}),
281
+ onPtcQuorum: (data) => emitter.emit(ChainEvent.forkChoicePtcQuorum, data),
282
+ }
283
+ );
284
+
285
+ const checkpoints = {
286
+ justifiedEpoch: justifiedCheckpoint.epoch,
287
+ justifiedRoot,
288
+ finalizedEpoch: finalizedCheckpoint.epoch,
289
+ finalizedRoot,
290
+ unrealizedJustifiedEpoch: justifiedCheckpoint.epoch,
291
+ unrealizedJustifiedRoot: justifiedRoot,
292
+ unrealizedFinalizedEpoch: finalizedCheckpoint.epoch,
293
+ unrealizedFinalizedRoot: finalizedRoot,
294
+ };
295
+
296
+ const block: ProtoBlock = {
297
+ slot: blockHeader.slot,
298
+ parentRoot: toRootHex(blockHeader.parentRoot),
299
+ stateRoot: toRootHex(blockHeader.stateRoot),
300
+ blockRoot: anchorRoot,
301
+ targetRoot: anchorRoot,
302
+ timeliness: true,
303
+ ptcTimeliness: true,
304
+ importedTimely: true,
305
+ proposerIndex: blockHeader.proposerIndex,
306
+ ...checkpoints,
307
+
308
+ ...(hasPayload
309
+ ? {
310
+ executionPayloadBlockHash: isPostGloas
311
+ ? toRootHex(state.latestBlockHash)
312
+ : toRootHex(state.latestExecutionPayloadHeader.blockHash),
313
+ // TODO GLOAS: not tracked in BeaconState post-gloas, see initializeForkChoiceFromFinalizedState
314
+ executionPayloadNumber: isPostGloas ? 0 : state.payloadBlockNumber,
315
+ // The upgrade to Gloas keeps a pre-Gloas block's payload gas limit in the state's bid
316
+ executionPayloadGasLimit: isPostGloasAnchor
317
+ ? 0
318
+ : isPostGloas
319
+ ? Number(state.latestExecutionPayloadBid.gasLimit)
320
+ : state.latestExecutionPayloadHeader.gasLimit,
321
+ // A Gloas payload is judged when its envelope arrives, the genesis one included
322
+ executionStatus:
323
+ blockHeader.slot === GENESIS_SLOT && !isPostGloasAnchor ? ExecutionStatus.Valid : ExecutionStatus.Syncing,
324
+ }
325
+ : {executionPayloadBlockHash: null, executionStatus: ExecutionStatus.PreMerge}),
326
+
327
+ dataAvailabilityStatus: DataAvailabilityStatus.PreData,
328
+ payloadStatus: isPostGloasAnchor ? PayloadStatus.PENDING : PayloadStatus.FULL,
329
+ parentBlockHash: bid === null ? null : toRootHex(bid.parentBlockHash),
330
+ };
331
+
332
+ // Ancestors of the anchor known by root only: the finalized block, the justified block, the
333
+ // anchor's parent, and for the anchor's epoch and the one before the checkpoint block, which the
334
+ // chain can still justify and which current votes target, and the block before it, the
335
+ // dependent root of the next epoch's duties. Their slots are upper bounds and their payloads
336
+ // unknown, so they carry none.
337
+ const unfinalizedAncestors: ProtoBlock[] = [];
338
+ if (!isFinalizedState) {
339
+ const finalizedSlot = computeStartSlotAtEpoch(finalizedCheckpoint.epoch);
340
+ const candidates: [string, Slot][] = [
341
+ [finalizedRoot, finalizedSlot],
342
+ [justifiedRoot, computeStartSlotAtEpoch(justifiedCheckpoint.epoch)],
343
+ [block.parentRoot, blockHeader.slot - 1],
344
+ ];
345
+ const anchorEpoch = computeEpochAtSlot(state.slot);
346
+ for (const epoch of [anchorEpoch - 1, anchorEpoch]) {
347
+ const checkpointSlot = computeStartSlotAtEpoch(epoch);
348
+ for (const slot of [checkpointSlot - 1, checkpointSlot]) {
349
+ if (slot > finalizedSlot && slot < blockHeader.slot) {
350
+ candidates.push([toRootHex(state.getBlockRootAtSlot(slot)), slot]);
351
+ }
352
+ }
353
+ }
354
+ // Roots repeat over skipped slots: a block keeps the lowest slot it is known at
355
+ candidates.sort((a, b) => a[1] - b[1]);
356
+ for (const [blockRoot, slot] of candidates) {
357
+ const isKnown = blockRoot === anchorRoot || unfinalizedAncestors.some((b) => b.blockRoot === blockRoot);
358
+ if (isKnown || blockRoot === ZERO_HASH_HEX) continue;
359
+ const isGloas = config.getForkSeq(slot) >= ForkSeq.gloas;
360
+ const targetSlot = computeStartSlotAtEpoch(computeEpochAtSlot(slot));
361
+ unfinalizedAncestors.push({
362
+ slot,
363
+ blockRoot,
364
+ parentRoot: unfinalizedAncestors.at(-1)?.blockRoot ?? ZERO_HASH_HEX,
365
+ stateRoot: ZERO_HASH_HEX,
366
+ targetRoot: targetSlot === slot ? blockRoot : toRootHex(state.getBlockRootAtSlot(targetSlot)),
367
+ timeliness: false,
368
+ ptcTimeliness: false,
369
+ importedTimely: false,
370
+ proposerIndex: 0,
371
+ ...checkpoints,
372
+ executionPayloadBlockHash: null,
373
+ executionStatus: ExecutionStatus.PreMerge,
374
+ dataAvailabilityStatus: DataAvailabilityStatus.PreData,
375
+ payloadStatus: isGloas ? PayloadStatus.PENDING : PayloadStatus.FULL,
376
+ parentBlockHash: isGloas ? ZERO_HASH_HEX : null,
377
+ });
378
+ }
379
+ }
380
+
381
+ return new NativeForkChoice(
382
+ config,
383
+ store,
384
+ {
385
+ block,
386
+ stateSlot: state.slot,
387
+ genesisTime: state.genesisTime,
388
+ genesisValidatorsRoot: state.genesisValidatorsRoot,
389
+ justifiedCheckpoint: isFinalizedState ? state.currentJustifiedCheckpoint : justifiedCheckpoint,
390
+ finalizedCheckpoint: isFinalizedState ? state.finalizedCheckpoint : finalizedCheckpoint,
391
+ unfinalizedAncestors: isFinalizedState ? undefined : unfinalizedAncestors,
392
+ payloadBlockHash: isPostGloasAnchor
393
+ ? (anchorBid?.blockHash ?? null)
394
+ : block.executionPayloadBlockHash === null
395
+ ? null
396
+ : fromHex(block.executionPayloadBlockHash),
397
+ bidParentBlockHash: anchorBid?.parentBlockHash ?? null,
398
+ },
399
+ metrics,
400
+ opts,
401
+ logger
402
+ );
403
+ }
404
+
186
405
  /**
187
406
  * Initialize forkchoice from an unfinalized state.
188
407
  */
@@ -217,7 +436,7 @@ export function initializeForkChoiceFromUnfinalizedState(
217
436
  // this is not the justified state, but there is no other ways to get justified balances
218
437
  const justifiedBalances = unfinalizedState.getEffectiveBalanceIncrementsZeroInactive();
219
438
 
220
- const isForkPostGloas = computeEpochAtSlot(unfinalizedState.slot) >= config.GLOAS_FORK_EPOCH;
439
+ const isPostGloasAnchor = isStatePostGloas(unfinalizedState) && isForkPostGloas(config.getForkName(blockHeader.slot));
221
440
 
222
441
  const store = new ForkChoiceStore(
223
442
  currentSlot,
@@ -270,17 +489,20 @@ export function initializeForkChoiceFromUnfinalizedState(
270
489
  // PENDING/EMPTY convention), so gossip bids that reference the head's *own* payload
271
490
  // hash won't match this variant anyway and will IGNORE until `onExecutionPayload`
272
491
  // upgrades the head to FULL with real values.
492
+ // A pre-Gloas anchor's gas limit is kept in the state's bid by the upgrade.
273
493
  executionPayloadNumber: isStatePostGloas(unfinalizedState) ? 0 : unfinalizedState.payloadBlockNumber,
274
- executionPayloadGasLimit: isStatePostGloas(unfinalizedState)
494
+ executionPayloadGasLimit: isPostGloasAnchor
275
495
  ? 0
276
- : unfinalizedState.latestExecutionPayloadHeader.gasLimit,
496
+ : isStatePostGloas(unfinalizedState)
497
+ ? Number(unfinalizedState.latestExecutionPayloadBid.gasLimit)
498
+ : unfinalizedState.latestExecutionPayloadHeader.gasLimit,
277
499
  executionStatus: blockHeader.slot === GENESIS_SLOT ? ExecutionStatus.Valid : ExecutionStatus.Syncing,
278
500
  }
279
501
  : {executionPayloadBlockHash: null, executionStatus: ExecutionStatus.PreMerge}),
280
502
 
281
503
  dataAvailabilityStatus: DataAvailabilityStatus.PreData,
282
- payloadStatus: isForkPostGloas ? PayloadStatus.PENDING : PayloadStatus.FULL,
283
- parentBlockHash: isStatePostGloas(unfinalizedState) ? toRootHex(unfinalizedState.latestBlockHash) : null,
504
+ payloadStatus: isPostGloasAnchor ? PayloadStatus.PENDING : PayloadStatus.FULL,
505
+ parentBlockHash: isPostGloasAnchor ? toRootHex(unfinalizedState.latestBlockHash) : null,
284
506
  };
285
507
 
286
508
  const parentSlot = blockHeader.slot - 1;
@@ -68,6 +68,7 @@ import {
68
68
  SeenAttesters,
69
69
  SeenBlockProposers,
70
70
  SeenContributionAndProof,
71
+ SeenDataColumnSidecars,
71
72
  SeenExecutionPayloadBids,
72
73
  SeenPayloadAttesters,
73
74
  SeenSyncCommitteeMessages,
@@ -147,6 +148,7 @@ export interface IBeaconChain {
147
148
  readonly seenAggregatedAttestations: SeenAggregatedAttestations;
148
149
  readonly seenExecutionPayloadBids: SeenExecutionPayloadBids;
149
150
  readonly seenBlockProposers: SeenBlockProposers;
151
+ readonly seenDataColumnSidecars: SeenDataColumnSidecars;
150
152
  readonly seenSyncCommitteeMessages: SeenSyncCommitteeMessages;
151
153
  readonly seenContributionAndProof: SeenContributionAndProof;
152
154
  readonly seenAttestationDatas: SeenAttestationDatas;