@lodestar/beacon-node 1.46.0-dev.e6f04acd82 → 1.46.0-dev.eabf12abae

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 (110) hide show
  1. package/lib/api/impl/beacon/blocks/index.d.ts.map +1 -1
  2. package/lib/api/impl/beacon/blocks/index.js +37 -15
  3. package/lib/api/impl/beacon/blocks/index.js.map +1 -1
  4. package/lib/api/impl/validator/index.d.ts.map +1 -1
  5. package/lib/api/impl/validator/index.js +15 -6
  6. package/lib/api/impl/validator/index.js.map +1 -1
  7. package/lib/chain/blocks/importBlock.d.ts.map +1 -1
  8. package/lib/chain/blocks/importBlock.js +3 -3
  9. package/lib/chain/blocks/importBlock.js.map +1 -1
  10. package/lib/chain/blocks/importExecutionPayload.d.ts.map +1 -1
  11. package/lib/chain/blocks/importExecutionPayload.js +3 -4
  12. package/lib/chain/blocks/importExecutionPayload.js.map +1 -1
  13. package/lib/chain/blocks/index.d.ts.map +1 -1
  14. package/lib/chain/blocks/index.js +6 -0
  15. package/lib/chain/blocks/index.js.map +1 -1
  16. package/lib/chain/blocks/utils/chainSegment.d.ts.map +1 -1
  17. package/lib/chain/blocks/utils/chainSegment.js +31 -25
  18. package/lib/chain/blocks/utils/chainSegment.js.map +1 -1
  19. package/lib/chain/blocks/utils/polarBearBanner.d.ts +2 -0
  20. package/lib/chain/blocks/utils/polarBearBanner.d.ts.map +1 -0
  21. package/lib/chain/blocks/utils/polarBearBanner.js +51 -0
  22. package/lib/chain/blocks/utils/polarBearBanner.js.map +1 -0
  23. package/lib/chain/blocks/verifyBlock.d.ts.map +1 -1
  24. package/lib/chain/blocks/verifyBlock.js +21 -1
  25. package/lib/chain/blocks/verifyBlock.js.map +1 -1
  26. package/lib/chain/chain.d.ts +5 -0
  27. package/lib/chain/chain.d.ts.map +1 -1
  28. package/lib/chain/chain.js +55 -0
  29. package/lib/chain/chain.js.map +1 -1
  30. package/lib/chain/emitter.d.ts +6 -0
  31. package/lib/chain/emitter.d.ts.map +1 -1
  32. package/lib/chain/emitter.js +5 -0
  33. package/lib/chain/emitter.js.map +1 -1
  34. package/lib/chain/interface.d.ts +2 -0
  35. package/lib/chain/interface.d.ts.map +1 -1
  36. package/lib/chain/options.d.ts +2 -0
  37. package/lib/chain/options.d.ts.map +1 -1
  38. package/lib/chain/options.js +1 -0
  39. package/lib/chain/options.js.map +1 -1
  40. package/lib/chain/prepareNextSlot.d.ts.map +1 -1
  41. package/lib/chain/prepareNextSlot.js +107 -29
  42. package/lib/chain/prepareNextSlot.js.map +1 -1
  43. package/lib/chain/produceBlock/produceBlockBody.d.ts.map +1 -1
  44. package/lib/chain/produceBlock/produceBlockBody.js +6 -6
  45. package/lib/chain/produceBlock/produceBlockBody.js.map +1 -1
  46. package/lib/chain/seenCache/seenBlockProposers.d.ts +20 -4
  47. package/lib/chain/seenCache/seenBlockProposers.d.ts.map +1 -1
  48. package/lib/chain/seenCache/seenBlockProposers.js +49 -2
  49. package/lib/chain/seenCache/seenBlockProposers.js.map +1 -1
  50. package/lib/chain/validation/block.d.ts +4 -1
  51. package/lib/chain/validation/block.d.ts.map +1 -1
  52. package/lib/chain/validation/block.js +32 -18
  53. package/lib/chain/validation/block.js.map +1 -1
  54. package/lib/chain/validation/dataColumnSidecar.d.ts.map +1 -1
  55. package/lib/chain/validation/dataColumnSidecar.js +21 -16
  56. package/lib/chain/validation/dataColumnSidecar.js.map +1 -1
  57. package/lib/metrics/metrics/lodestar.d.ts +14 -0
  58. package/lib/metrics/metrics/lodestar.d.ts.map +1 -1
  59. package/lib/metrics/metrics/lodestar.js +38 -0
  60. package/lib/metrics/metrics/lodestar.js.map +1 -1
  61. package/lib/network/core/networkCore.d.ts.map +1 -1
  62. package/lib/network/core/networkCore.js +12 -1
  63. package/lib/network/core/networkCore.js.map +1 -1
  64. package/lib/network/core/networkCoreWorkerHandler.d.ts.map +1 -1
  65. package/lib/network/core/networkCoreWorkerHandler.js +20 -3
  66. package/lib/network/core/networkCoreWorkerHandler.js.map +1 -1
  67. package/lib/network/network.d.ts +1 -0
  68. package/lib/network/network.d.ts.map +1 -1
  69. package/lib/network/network.js +11 -1
  70. package/lib/network/network.js.map +1 -1
  71. package/lib/network/processor/gossipHandlers.d.ts.map +1 -1
  72. package/lib/network/processor/gossipHandlers.js +8 -0
  73. package/lib/network/processor/gossipHandlers.js.map +1 -1
  74. package/lib/network/reqresp/score.d.ts.map +1 -1
  75. package/lib/network/reqresp/score.js +20 -3
  76. package/lib/network/reqresp/score.js.map +1 -1
  77. package/lib/util/clock.d.ts +2 -7
  78. package/lib/util/clock.d.ts.map +1 -1
  79. package/lib/util/clock.js +2 -7
  80. package/lib/util/clock.js.map +1 -1
  81. package/lib/util/workerEvents.d.ts +7 -1
  82. package/lib/util/workerEvents.d.ts.map +1 -1
  83. package/lib/util/workerEvents.js +17 -5
  84. package/lib/util/workerEvents.js.map +1 -1
  85. package/package.json +15 -15
  86. package/src/api/impl/beacon/blocks/index.ts +65 -16
  87. package/src/api/impl/validator/index.ts +19 -6
  88. package/src/chain/blocks/importBlock.ts +3 -2
  89. package/src/chain/blocks/importExecutionPayload.ts +8 -4
  90. package/src/chain/blocks/index.ts +7 -0
  91. package/src/chain/blocks/utils/chainSegment.ts +35 -28
  92. package/src/chain/blocks/utils/polarBearBanner.ts +50 -0
  93. package/src/chain/blocks/verifyBlock.ts +28 -2
  94. package/src/chain/chain.ts +60 -0
  95. package/src/chain/emitter.ts +7 -0
  96. package/src/chain/interface.ts +3 -0
  97. package/src/chain/options.ts +3 -0
  98. package/src/chain/prepareNextSlot.ts +140 -39
  99. package/src/chain/produceBlock/produceBlockBody.ts +11 -6
  100. package/src/chain/seenCache/seenBlockProposers.ts +72 -5
  101. package/src/chain/validation/block.ts +41 -19
  102. package/src/chain/validation/dataColumnSidecar.ts +27 -17
  103. package/src/metrics/metrics/lodestar.ts +39 -0
  104. package/src/network/core/networkCore.ts +13 -1
  105. package/src/network/core/networkCoreWorkerHandler.ts +18 -3
  106. package/src/network/network.ts +16 -1
  107. package/src/network/processor/gossipHandlers.ts +7 -0
  108. package/src/network/reqresp/score.ts +22 -3
  109. package/src/util/clock.ts +2 -7
  110. package/src/util/workerEvents.ts +18 -6
@@ -1,19 +1,29 @@
1
1
  import {routes} from "@lodestar/api";
2
2
  import {ChainForkConfig} from "@lodestar/config";
3
- import {getSafeExecutionBlockHash} from "@lodestar/fork-choice";
4
- import {ForkPostBellatrix, ForkSeq, SLOTS_PER_EPOCH, isForkPostBellatrix, isForkPostGloas} from "@lodestar/params";
3
+ import {getFinalizedExecutionBlockHash, getSafeExecutionBlockHash} from "@lodestar/fork-choice";
5
4
  import {
5
+ ForkPostBellatrix,
6
+ ForkSeq,
7
+ SLOTS_PER_EPOCH,
8
+ isForkPostBellatrix,
9
+ isForkPostFulu,
10
+ isForkPostGloas,
11
+ } from "@lodestar/params";
12
+ import {
13
+ GLOAS_PREVERIFY_WINDOW_EPOCHS,
6
14
  IBeaconStateView,
7
15
  IBeaconStateViewBellatrix,
16
+ MAX_BUILDER_DEPOSITS_PER_SLOT,
8
17
  StateHashTreeRootSource,
9
18
  computeEpochAtSlot,
10
19
  computeTimeAtSlot,
11
20
  isStatePostBellatrix,
21
+ isStatePostFulu,
12
22
  isStatePostGloas,
13
23
  } from "@lodestar/state-transition";
14
- import {Bytes32, Slot} from "@lodestar/types";
24
+ import {Bytes32, Slot, ValidatorIndex} from "@lodestar/types";
15
25
  import {Logger, fromHex, isErrorAborted, sleep} from "@lodestar/utils";
16
- import {GENESIS_SLOT, ZERO_HASH_HEX} from "../constants/constants.js";
26
+ import {GENESIS_SLOT} from "../constants/constants.js";
17
27
  import {BuilderStatus} from "../execution/builder/http.js";
18
28
  import {Metrics} from "../metrics/index.js";
19
29
  import {ClockEvent} from "../util/clock.js";
@@ -30,6 +40,11 @@ export const PREPARE_NEXT_SLOT_BPS = 6667;
30
40
  /* We don't want to do more epoch transition than this */
31
41
  const PREPARE_EPOCH_LIMIT = 1;
32
42
 
43
+ /* Fraction of a slot (out of 10_000) the pre-Gloas builder-deposit pre-verify may spend per tick.
44
+ * 2000 = 20% of slot (2.4s on mainnet), within the spare window left after PREPARE_NEXT_SLOT_BPS.
45
+ * Expressed in bps so it scales with slot duration (mainnet vs minimal/devnet). */
46
+ const BUILDER_PREVERIFY_LIMIT_BPS = 2000;
47
+
33
48
  /**
34
49
  * At Bellatrix, if we are responsible for proposing in next slot, we want to prepare payload
35
50
  * 4s before the start of next slot at PREPARE_NEXT_SLOT_BPS of the current slot.
@@ -110,30 +125,38 @@ export class PrepareNextSlotScheduler {
110
125
  ? this.metrics?.precomputeNextEpochTransition.duration.startTimer()
111
126
  : null;
112
127
  const start = Date.now();
128
+ let feeRecipient: string | undefined;
113
129
  // No need to wait for this or the clock drift
114
- // Pre Bellatrix: we only do precompute state transition for the last slot of epoch
115
- // For Bellatrix, we always do the `processSlots()` to prepare payload for the next slot
116
- const prepareState = await this.chain.regen.getBlockSlotState(
117
- headBlock,
118
- prepareSlot,
119
- // the slot 0 of next epoch will likely use this Previous Root Checkpoint state for state transition so we transfer cache here
120
- // the resulting state with cache will be cached in Checkpoint State Cache which is used for the upcoming block processing
121
- // for other slots dontTransferCached=true because we don't run state transition on this state
122
- {dontTransferCache: !isEpochTransition},
123
- RegenCaller.precomputeEpoch
124
- );
125
-
126
130
  if (isForkPostBellatrix(fork)) {
127
- const proposerIndex = prepareState.getBeaconProposer(prepareSlot);
128
- const feeRecipient = this.chain.beaconProposerCache.get(proposerIndex);
129
- let updatedPrepareState = prepareState;
131
+ let preparedState: IBeaconStateView | undefined;
132
+
133
+ const getProposerIndex = async (): Promise<ValidatorIndex> => {
134
+ if (isForkPostFulu(fork)) {
135
+ // getBeaconProposer covers the current + next epoch; should not throw
136
+ // PROPOSER_EPOCH_MISMATCH here due to the PREPARE_EPOCH_LIMIT check above.
137
+ return this.chain.getHeadState().getBeaconProposer(prepareSlot);
138
+ }
139
+ // the slot 0 of next epoch will likely use this Previous Root Checkpoint state for state transition so we transfer cache here
140
+ // the resulting state with cache will be cached in Checkpoint State Cache which is used for the upcoming block processing
141
+ // for other slots dontTransferCached=true because we don't run state transition on this state
142
+ preparedState = await this.chain.regen.getBlockSlotState(
143
+ headBlock,
144
+ prepareSlot,
145
+ {dontTransferCache: !isEpochTransition},
146
+ RegenCaller.precomputeEpoch
147
+ );
148
+ return preparedState.getBeaconProposer(prepareSlot);
149
+ };
150
+
151
+ const proposerIndex = await getProposerIndex();
152
+ feeRecipient = this.chain.beaconProposerCache.get(proposerIndex);
130
153
 
131
154
  if (feeRecipient) {
132
155
  // If we are proposing next slot, we need to predict if we can proposer-boost-reorg or not
133
156
  const proposerHead = this.chain.predictProposerHead(clockSlot);
134
157
  const {slot: proposerHeadSlot, blockRoot: proposerHeadRoot} = proposerHead;
135
158
 
136
- // If we predict we can reorg, update prepareState with proposer head block
159
+ // If we predict we can reorg, we build on the proposer head (parent) block instead
137
160
  if (proposerHeadRoot !== headRoot || proposerHeadSlot !== headSlot) {
138
161
  this.logger.verbose("Weak head detected. May build on parent block instead", {
139
162
  proposerHeadSlot,
@@ -142,13 +165,6 @@ export class PrepareNextSlotScheduler {
142
165
  headRoot,
143
166
  });
144
167
  this.metrics?.weakHeadDetected.inc();
145
- updatedPrepareState = await this.chain.regen.getBlockSlotState(
146
- proposerHead,
147
- prepareSlot,
148
- // only transfer cache if epoch transition because that's the state we will use to stateTransition() the 1st block of epoch
149
- {dontTransferCache: !isEpochTransition},
150
- RegenCaller.predictProposerHead
151
- );
152
168
  updatedHead = proposerHead;
153
169
  }
154
170
 
@@ -165,30 +181,41 @@ export class PrepareNextSlotScheduler {
165
181
  }
166
182
  }
167
183
 
168
- if (!isStatePostBellatrix(updatedPrepareState)) {
184
+ // post-fulu we'll always reach here because preparedState is undefined
185
+ if (preparedState === undefined || updatedHead !== headBlock) {
186
+ preparedState = await this.chain.regen.getBlockSlotState(
187
+ updatedHead,
188
+ prepareSlot,
189
+ // only transfer cache if epoch transition because that's the state we will use to stateTransition() the 1st block of epoch
190
+ {dontTransferCache: !isEpochTransition},
191
+ updatedHead === headBlock ? RegenCaller.precomputeEpoch : RegenCaller.predictProposerHead
192
+ );
193
+ }
194
+
195
+ if (!isStatePostBellatrix(preparedState)) {
169
196
  throw new Error("Expected Bellatrix state for payload attributes");
170
197
  }
171
198
 
172
199
  let parentBlockHash: Bytes32;
173
200
  // Apply parent payload once here as it's reused by EL prep and SSE emit below
174
- let stateAfterParentPayload: IBeaconStateViewBellatrix = updatedPrepareState;
175
- if (isStatePostGloas(updatedPrepareState)) {
201
+ let stateAfterParentPayload: IBeaconStateViewBellatrix = preparedState;
202
+ if (isStatePostGloas(preparedState)) {
176
203
  // Spec: should_build_on_full(store, head, slot) - see produceBlockBody.ts for context.
177
204
  if (this.chain.forkChoice.shouldBuildOnFull(updatedHead, prepareSlot)) {
178
- parentBlockHash = updatedPrepareState.latestExecutionPayloadBid.blockHash;
205
+ parentBlockHash = preparedState.latestExecutionPayloadBid.blockHash;
179
206
  // Skip applying parent payload unless we're proposing the next slot or have to emit payload_attributes events
180
207
  if (feeRecipient !== undefined || this.chain.opts.emitPayloadAttributes === true) {
181
208
  const parentExecutionRequests = await this.chain.getParentExecutionRequests(
182
209
  updatedHead.slot,
183
210
  updatedHead.blockRoot
184
211
  );
185
- stateAfterParentPayload = updatedPrepareState.withParentPayloadApplied(parentExecutionRequests);
212
+ stateAfterParentPayload = preparedState.withParentPayloadApplied(parentExecutionRequests);
186
213
  }
187
214
  } else {
188
- parentBlockHash = updatedPrepareState.latestExecutionPayloadBid.parentBlockHash;
215
+ parentBlockHash = preparedState.latestExecutionPayloadBid.parentBlockHash;
189
216
  }
190
217
  } else {
191
- parentBlockHash = updatedPrepareState.latestExecutionPayloadHeader.blockHash;
218
+ parentBlockHash = preparedState.latestExecutionPayloadHeader.blockHash;
192
219
  }
193
220
 
194
221
  if (feeRecipient) {
@@ -196,9 +223,8 @@ export class PrepareNextSlotScheduler {
196
223
  computeTimeAtSlot(this.config, prepareSlot, this.chain.genesisTime) - Date.now() / 1000;
197
224
  this.metrics?.blockPayload.payloadAdvancePrepTime.observe(preparationTime);
198
225
 
199
- const safeBlockHash = getSafeExecutionBlockHash(this.chain.forkChoice);
200
- const finalizedBlockHash =
201
- this.chain.forkChoice.getFinalizedBlock().executionPayloadBlockHash ?? ZERO_HASH_HEX;
226
+ const safeBlockHash = getSafeExecutionBlockHash(this.chain.forkChoice, this.logger);
227
+ const finalizedBlockHash = getFinalizedExecutionBlockHash(this.chain.forkChoice, this.logger);
202
228
 
203
229
  // awaiting here instead of throwing an async call because there is no other task
204
230
  // left for scheduler and this gives nice semantics to catch and log errors in the
@@ -221,7 +247,7 @@ export class PrepareNextSlotScheduler {
221
247
  });
222
248
  }
223
249
 
224
- this.computeStateHashTreeRoot(updatedPrepareState, isEpochTransition);
250
+ this.computeStateHashTreeRoot(preparedState, isEpochTransition);
225
251
 
226
252
  // If emitPayloadAttributes is true emit a SSE payloadAttributes event for
227
253
  // every slot. Without the flag, only emit the event if we are proposing in the next slot.
@@ -239,7 +265,14 @@ export class PrepareNextSlotScheduler {
239
265
  this.chain.emitter.emit(routes.events.EventType.payloadAttributes, {data, version: fork});
240
266
  }
241
267
  } else {
242
- this.computeStateHashTreeRoot(prepareState, isEpochTransition);
268
+ // Pre-bellatrix only reaches here at an epoch transition to precompute the next epoch state
269
+ const preparedState = await this.chain.regen.getBlockSlotState(
270
+ headBlock,
271
+ prepareSlot,
272
+ {dontTransferCache: !isEpochTransition},
273
+ RegenCaller.precomputeEpoch
274
+ );
275
+ this.computeStateHashTreeRoot(preparedState, isEpochTransition);
243
276
  }
244
277
 
245
278
  // assuming there is no reorg, it caches the checkpoint state & helps avoid doing a full state transition in the next slot
@@ -262,6 +295,74 @@ export class PrepareNextSlotScheduler {
262
295
  });
263
296
 
264
297
  precomputeEpochTransitionTimer?.();
298
+
299
+ // Nothing more to do on an epoch-transition slot: that precompute already ran a full (>1s)
300
+ // epoch transition, so we don't pile the builder-deposit pre-verify (below) on top of it.
301
+ return;
302
+ }
303
+
304
+ // Pre-verify builder-deposit signatures in the GLOAS_PREVERIFY_WINDOW_EPOCHS epochs leading
305
+ // up to the Gloas fork, off the block-import hot path, so onboardBuildersFromPendingDeposits
306
+ // at the transition is mostly cache hits. Reached only on non-epoch-transition slots (the
307
+ // epoch-transition branch returned above). Runs on the head state — its pendingDeposits are
308
+ // what the fork transition derives from, and its epochCtx holds the shared, app-wide
309
+ // builderDepositSignatureCache.
310
+ const headState = this.chain.getHeadState();
311
+ if (isStatePostFulu(headState)) {
312
+ const gloasEpoch = this.config.GLOAS_FORK_EPOCH;
313
+ const finalizedEpoch = this.chain.forkChoice.getFinalizedCheckpoint().epoch;
314
+ if (finalizedEpoch >= gloasEpoch) {
315
+ // Gloas is finalized — the pre-verify cache has served its purpose; release its memory.
316
+ headState.clearPreGloasBuilderDepositCache();
317
+ } else {
318
+ const clockEpoch = computeEpochAtSlot(clockSlot);
319
+ if (
320
+ ForkSeq[fork] < ForkSeq.gloas && // only before the fork
321
+ clockEpoch >= gloasEpoch - GLOAS_PREVERIFY_WINDOW_EPOCHS && // Infinity when Gloas unscheduled → skip
322
+ clockEpoch < gloasEpoch &&
323
+ // skip when we're the next-slot proposer — don't compete with block-production prep.
324
+ feeRecipient === undefined
325
+ ) {
326
+ const maxDurationMs = this.config.getSlotComponentDurationMs(BUILDER_PREVERIFY_LIMIT_BPS);
327
+ const preVerifyTimer = this.metrics?.builderDepositPreVerify.duration.startTimer();
328
+ const result = headState.preVerifyBuilderDepositsPreGloas(MAX_BUILDER_DEPOSITS_PER_SLOT, maxDurationMs);
329
+ preVerifyTimer?.();
330
+
331
+ const preVerifyMetrics = this.metrics?.builderDepositPreVerify;
332
+ if (preVerifyMetrics) {
333
+ preVerifyMetrics.pendingDeposits.set(result.pendingDepositsCount);
334
+ preVerifyMetrics.cachedDeposits.set(result.totalCachedDeposits);
335
+ preVerifyMetrics.scannedDeposits.set(result.scannedPendingDeposits);
336
+ preVerifyMetrics.builderPubkeys.set(result.totalBuilderPubkeys);
337
+ if (result.validBuilderSignaturesCount > 0) {
338
+ preVerifyMetrics.validSignatures.inc({type: "builder"}, result.validBuilderSignaturesCount);
339
+ }
340
+ if (result.validValidatorSignaturesCount > 0) {
341
+ preVerifyMetrics.validSignatures.inc({type: "validator"}, result.validValidatorSignaturesCount);
342
+ }
343
+ if (result.invalidBuilderSignaturesCount > 0) {
344
+ preVerifyMetrics.invalidSignatures.inc({type: "builder"}, result.invalidBuilderSignaturesCount);
345
+ }
346
+ if (result.invalidValidatorSignaturesCount > 0) {
347
+ preVerifyMetrics.invalidSignatures.inc({type: "validator"}, result.invalidValidatorSignaturesCount);
348
+ }
349
+ }
350
+
351
+ // Always log (once per slot, only within the pre-fork window) so devnets show the
352
+ // pre-verify draining even on all-cache-hit ticks.
353
+ this.logger.verbose("PrepareNextSlotScheduler pre-verified deposit signatures", {
354
+ clockSlot,
355
+ validBuilderSignaturesCount: result.validBuilderSignaturesCount,
356
+ invalidBuilderSignaturesCount: result.invalidBuilderSignaturesCount,
357
+ validValidatorSignaturesCount: result.validValidatorSignaturesCount,
358
+ invalidValidatorSignaturesCount: result.invalidValidatorSignaturesCount,
359
+ scannedPendingDeposits: result.scannedPendingDeposits,
360
+ totalCachedDeposits: result.totalCachedDeposits,
361
+ totalBuilderPubkeys: result.totalBuilderPubkeys,
362
+ pendingDepositsCount: result.pendingDepositsCount,
363
+ });
364
+ }
365
+ }
265
366
  }
266
367
  } catch (e) {
267
368
  if (!isErrorAborted(e) && !isQueueErrorAborted(e)) {
@@ -1,6 +1,11 @@
1
1
  import {BitArray} from "@chainsafe/ssz";
2
2
  import {ChainForkConfig} from "@lodestar/config";
3
- import {IForkChoice, ProtoBlock, getSafeExecutionBlockHash} from "@lodestar/fork-choice";
3
+ import {
4
+ IForkChoice,
5
+ ProtoBlock,
6
+ getFinalizedExecutionBlockHash,
7
+ getSafeExecutionBlockHash,
8
+ } from "@lodestar/fork-choice";
4
9
  import {
5
10
  BUILDER_INDEX_SELF_BUILD,
6
11
  ForkName,
@@ -265,8 +270,8 @@ export async function produceBlockBody<T extends BlockType>(
265
270
  // TODO GLOAS: support non self-building here, the block type differentiation between
266
271
  // full and blinded no longer makes sense in gloas, it might be a good idea to move
267
272
  // this into a completely separate function and have pre/post gloas more separated
268
- const safeBlockHash = getSafeExecutionBlockHash(this.forkChoice);
269
- const finalizedBlockHash = this.forkChoice.getFinalizedBlock().executionPayloadBlockHash ?? ZERO_HASH_HEX;
273
+ const safeBlockHash = getSafeExecutionBlockHash(this.forkChoice, this.logger);
274
+ const finalizedBlockHash = getFinalizedExecutionBlockHash(this.forkChoice, this.logger);
270
275
  // TODO GLOAS: post-Gloas, proposer feeRecipient is also carried (signed) in
271
276
  // ProposerPreferencesPool. Consider using this unified cache instead
272
277
  // see https://github.com/ChainSafe/lodestar/issues/9379
@@ -411,8 +416,8 @@ export async function produceBlockBody<T extends BlockType>(
411
416
  throw new Error("Expected Bellatrix state for execution block production");
412
417
  }
413
418
 
414
- const safeBlockHash = getSafeExecutionBlockHash(this.forkChoice);
415
- const finalizedBlockHash = this.forkChoice.getFinalizedBlock().executionPayloadBlockHash ?? ZERO_HASH_HEX;
419
+ const safeBlockHash = getSafeExecutionBlockHash(this.forkChoice, this.logger);
420
+ const finalizedBlockHash = getFinalizedExecutionBlockHash(this.forkChoice, this.logger);
416
421
  const feeRecipient = requestedFeeRecipient ?? this.beaconProposerCache.getOrDefault(proposerIndex);
417
422
  const feeRecipientType = requestedFeeRecipient
418
423
  ? "requested"
@@ -1037,7 +1042,7 @@ export async function produceCommonBlockBody<T extends BlockType>(
1037
1042
  graffiti,
1038
1043
  // Eth1 data voting is no longer required since electra
1039
1044
  eth1Data: currentState.eth1Data,
1040
- proposerSlashings,
1045
+ proposerSlashings: this.opts.disableProposerSlashings === true ? [] : proposerSlashings,
1041
1046
  attesterSlashings,
1042
1047
  attestations,
1043
1048
  // Since electra, deposits are processed by the execution layer,
@@ -1,21 +1,83 @@
1
1
  import {computeStartSlotAtEpoch} from "@lodestar/state-transition";
2
- import {Epoch, Slot, ValidatorIndex} from "@lodestar/types";
2
+ import {Epoch, RootHex, Slot, ValidatorIndex, phase0} from "@lodestar/types";
3
3
  import {MapDef} from "@lodestar/utils";
4
4
 
5
+ /** Two distinct block roots signed by the same proposer for the same slot are sufficient to establish an equivocation */
6
+ const MAX_BLOCK_ROOTS_PER_PROPOSAL = 2;
7
+
5
8
  /**
6
9
  * Keeps a cache to filter block proposals from the same validator in the same slot.
7
10
  *
8
- * This cache is not bounded and for extremely long periods of non-finality it can grow a lot. However it's practically
9
- * limited by the possible shufflings in those epochs, and the stored data is very cheap
11
+ * Block roots with a signature verified against the block's proposer index are tracked separately from proposals
12
+ * accepted by gossip validation or block import. A root from a block signed by its declared proposer is potential
13
+ * equivocation evidence, but the block may still fail other validation. Such a block must not mark the proposal as
14
+ * known, since that would cause a later valid block for the same slot and proposer to be ignored as a repeat proposal.
15
+ *
16
+ * The signed block header of each observed root is kept so a proposer slashing can be produced from the two
17
+ * conflicting headers once an equivocation is established.
18
+ *
19
+ * The cache is pruned on finalization and bounds the number of roots stored per proposer and slot
10
20
  */
11
21
  export class SeenBlockProposers {
12
22
  private readonly proposerIndexesBySlot = new MapDef<Slot, Set<ValidatorIndex>>(() => new Set<ValidatorIndex>());
13
- private finalizedSlot: Epoch = 0;
23
+ private readonly signedBlockHeadersBySlot = new MapDef<
24
+ Slot,
25
+ MapDef<ValidatorIndex, Map<RootHex, phase0.SignedBeaconBlockHeader>>
26
+ >(() => new MapDef<ValidatorIndex, Map<RootHex, phase0.SignedBeaconBlockHeader>>(() => new Map()));
27
+ private finalizedSlot: Slot = 0;
14
28
 
15
29
  isKnown(blockSlot: Slot, proposerIndex: ValidatorIndex): boolean {
16
30
  return this.proposerIndexesBySlot.get(blockSlot)?.has(proposerIndex) === true;
17
31
  }
18
32
 
33
+ hasBlockRoot(blockSlot: Slot, proposerIndex: ValidatorIndex, blockRoot: RootHex): boolean {
34
+ return this.signedBlockHeadersBySlot.get(blockSlot)?.get(proposerIndex)?.has(blockRoot) === true;
35
+ }
36
+
37
+ isEquivocating(blockSlot: Slot, proposerIndex: ValidatorIndex): boolean {
38
+ return (
39
+ (this.signedBlockHeadersBySlot.get(blockSlot)?.get(proposerIndex)?.size ?? 0) >= MAX_BLOCK_ROOTS_PER_PROPOSAL
40
+ );
41
+ }
42
+
43
+ getConflictingBlockRoots(blockSlot: Slot, proposerIndex: ValidatorIndex, blockRoot: RootHex): RootHex[] {
44
+ const signedBlockHeaderByRoot = this.signedBlockHeadersBySlot.get(blockSlot)?.get(proposerIndex);
45
+ return signedBlockHeaderByRoot === undefined
46
+ ? []
47
+ : Array.from(signedBlockHeaderByRoot.keys()).filter((root) => root !== blockRoot);
48
+ }
49
+
50
+ /** Return the two signed block headers that establish an equivocation, or null if there is none */
51
+ getEquivocationHeaders(
52
+ blockSlot: Slot,
53
+ proposerIndex: ValidatorIndex
54
+ ): [phase0.SignedBeaconBlockHeader, phase0.SignedBeaconBlockHeader] | null {
55
+ const signedBlockHeaderByRoot = this.signedBlockHeadersBySlot.get(blockSlot)?.get(proposerIndex);
56
+ if (signedBlockHeaderByRoot === undefined || signedBlockHeaderByRoot.size < MAX_BLOCK_ROOTS_PER_PROPOSAL) {
57
+ return null;
58
+ }
59
+ const [signedHeader1, signedHeader2] = signedBlockHeaderByRoot.values();
60
+ return [signedHeader1, signedHeader2];
61
+ }
62
+
63
+ /** Record a block only after its proposer signature has been verified */
64
+ observeBlockRoot(
65
+ blockSlot: Slot,
66
+ proposerIndex: ValidatorIndex,
67
+ blockRoot: RootHex,
68
+ signedBlockHeader: phase0.SignedBeaconBlockHeader
69
+ ): void {
70
+ if (blockSlot < this.finalizedSlot) {
71
+ throw Error(`blockSlot ${blockSlot} < finalizedSlot ${this.finalizedSlot}`);
72
+ }
73
+
74
+ const signedBlockHeaderByRoot = this.signedBlockHeadersBySlot.getOrDefault(blockSlot).getOrDefault(proposerIndex);
75
+ if (signedBlockHeaderByRoot.size < MAX_BLOCK_ROOTS_PER_PROPOSAL && !signedBlockHeaderByRoot.has(blockRoot)) {
76
+ signedBlockHeaderByRoot.set(blockRoot, signedBlockHeader);
77
+ }
78
+ }
79
+
80
+ /** Mark a block as known from gossip or another block import path */
19
81
  add(blockSlot: Slot, proposerIndex: ValidatorIndex): void {
20
82
  if (blockSlot < this.finalizedSlot) {
21
83
  throw Error(`blockSlot ${blockSlot} < finalizedSlot ${this.finalizedSlot}`);
@@ -31,9 +93,14 @@ export class SeenBlockProposers {
31
93
  this.proposerIndexesBySlot.delete(slot);
32
94
  }
33
95
  }
96
+ for (const slot of this.signedBlockHeadersBySlot.keys()) {
97
+ if (slot < finalizedSlot) {
98
+ this.signedBlockHeadersBySlot.delete(slot);
99
+ }
100
+ }
34
101
  }
35
102
 
36
- seenAtEpoch(epoch: Slot, index: ValidatorIndex): boolean {
103
+ seenAtEpoch(epoch: Epoch, index: ValidatorIndex): boolean {
37
104
  const fromSlot = computeStartSlotAtEpoch(epoch);
38
105
  const toSlot = computeStartSlotAtEpoch(epoch + 1);
39
106
 
@@ -23,8 +23,9 @@ import {
23
23
  getBlockProposerSignatureSet,
24
24
  isExecutionBlockBodyType,
25
25
  isStatePostBellatrix,
26
+ signedBlockToSignedHeader,
26
27
  } from "@lodestar/state-transition";
27
- import {SignedBeaconBlock, deneb, gloas, isGloasBeaconBlock} from "@lodestar/types";
28
+ import {RootHex, SignedBeaconBlock, deneb, gloas, isGloasBeaconBlock, ssz} from "@lodestar/types";
28
29
  import {byteArrayEquals, sleep, toRootHex} from "@lodestar/utils";
29
30
  import {BlockErrorCode, BlockGossipError, GossipAction} from "../errors/index.js";
30
31
  import {IBeaconChain} from "../interface.js";
@@ -75,7 +76,10 @@ export async function validateGossipBlock(
75
76
  // reboot if the `observed_block_producers` cache is empty. In that case, without this
76
77
  // check, we will load the parent and state from disk only to find out later that we
77
78
  // already know this block.
78
- const blockRoot = toRootHex(config.getForkTypes(blockSlot).BeaconBlock.hashTreeRoot(block));
79
+ // A block's hash tree root is identical to its header's, so the root is derived from the header
80
+ // which is also used as potential equivocation evidence
81
+ const signedBlockHeader = signedBlockToSignedHeader(config, signedBlock);
82
+ const blockRoot = toRootHex(ssz.phase0.BeaconBlockHeader.hashTreeRoot(signedBlockHeader.message));
79
83
  if (chain.forkChoice.getBlockHexDefaultStatus(blockRoot) !== null) {
80
84
  throw new BlockGossipError(GossipAction.IGNORE, {code: BlockErrorCode.ALREADY_KNOWN, root: blockRoot});
81
85
  }
@@ -85,7 +89,12 @@ export async function validateGossipBlock(
85
89
 
86
90
  // [IGNORE] The block is the first block with valid signature received for the proposer for the slot, signed_beacon_block.message.slot.
87
91
  const proposerIndex = block.proposerIndex;
92
+ const hasBlockRoot = chain.seenBlockProposers.hasBlockRoot(blockSlot, proposerIndex, blockRoot);
88
93
  if (chain.seenBlockProposers.isKnown(blockSlot, proposerIndex)) {
94
+ if (!hasBlockRoot && !chain.seenBlockProposers.isEquivocating(blockSlot, proposerIndex)) {
95
+ await verifyBlockProposerSignature(chain, signedBlock, blockRoot, {verifyOnMainThread: false});
96
+ chain.seenBlockProposers.observeBlockRoot(blockSlot, proposerIndex, blockRoot, signedBlockHeader);
97
+ }
89
98
  throw new BlockGossipError(GossipAction.IGNORE, {code: BlockErrorCode.REPEAT_PROPOSAL, proposerIndex});
90
99
  }
91
100
 
@@ -270,18 +279,8 @@ export async function validateGossipBlock(
270
279
  }
271
280
 
272
281
  // [REJECT] The proposer signature, signed_beacon_block.signature, is valid with respect to the proposer_index pubkey.
273
- if (!chain.seenBlockInputCache.isVerifiedProposerSignature(blockSlot, blockRoot, signedBlock.signature)) {
274
- const signatureSet = getBlockProposerSignatureSet(chain.config, signedBlock);
275
- // Don't batch so verification is not delayed
276
- if (!(await chain.bls.verifySignatureSets([signatureSet], {verifyOnMainThread: true}))) {
277
- throw new BlockGossipError(GossipAction.REJECT, {
278
- code: BlockErrorCode.PROPOSAL_SIGNATURE_INVALID,
279
- blockSlot,
280
- });
281
- }
282
-
283
- chain.seenBlockInputCache.markVerifiedProposerSignature(blockSlot, blockRoot, signedBlock.signature);
284
- }
282
+ await verifyBlockProposerSignature(chain, signedBlock, blockRoot);
283
+ chain.seenBlockProposers.observeBlockRoot(blockSlot, proposerIndex, blockRoot, signedBlockHeader);
285
284
 
286
285
  // [REJECT] The block is proposed by the expected proposer_index for the block's slot in the context of the current
287
286
  // shuffling (defined by parent_root/slot). If the proposer_index cannot immediately be verified against the expected
@@ -291,11 +290,6 @@ export async function validateGossipBlock(
291
290
  throw new BlockGossipError(GossipAction.REJECT, {code: BlockErrorCode.INCORRECT_PROPOSER, proposerIndex});
292
291
  }
293
292
 
294
- // Check again in case there two blocks are processed concurrently
295
- if (chain.seenBlockProposers.isKnown(blockSlot, proposerIndex)) {
296
- throw new BlockGossipError(GossipAction.IGNORE, {code: BlockErrorCode.REPEAT_PROPOSAL, proposerIndex});
297
- }
298
-
299
293
  // Simple implementation of a pending block queue. Keeping the block here recycles the queue logic, and keeps the
300
294
  // gossip validation promise without any extra infrastructure.
301
295
  // Do the sleep at the end, since regen and signature validation can already take longer than `msToBlockSlot`.
@@ -305,7 +299,35 @@ export async function validateGossipBlock(
305
299
  await sleep(msToBlockSlot);
306
300
  }
307
301
 
302
+ // Check again after all async validation and the early-block delay so concurrent proposals cannot both pass
303
+ if (chain.seenBlockProposers.isKnown(blockSlot, proposerIndex)) {
304
+ throw new BlockGossipError(GossipAction.IGNORE, {code: BlockErrorCode.REPEAT_PROPOSAL, proposerIndex});
305
+ }
306
+
308
307
  chain.seenBlockProposers.add(blockSlot, proposerIndex);
309
308
 
310
309
  return {skippedSlots};
311
310
  }
311
+
312
+ export async function verifyBlockProposerSignature(
313
+ chain: IBeaconChain,
314
+ signedBlock: SignedBeaconBlock,
315
+ blockRoot: RootHex,
316
+ opts: {verifyOnMainThread?: boolean} = {}
317
+ ): Promise<void> {
318
+ const blockSlot = signedBlock.message.slot;
319
+ if (chain.seenBlockInputCache.isVerifiedProposerSignature(blockSlot, blockRoot, signedBlock.signature)) {
320
+ return;
321
+ }
322
+
323
+ const signatureSet = getBlockProposerSignatureSet(chain.config, signedBlock);
324
+ // Don't batch so verification is not delayed
325
+ if (!(await chain.bls.verifySignatureSets([signatureSet], {verifyOnMainThread: opts.verifyOnMainThread ?? true}))) {
326
+ throw new BlockGossipError(GossipAction.REJECT, {
327
+ code: BlockErrorCode.PROPOSAL_SIGNATURE_INVALID,
328
+ blockSlot,
329
+ });
330
+ }
331
+
332
+ chain.seenBlockInputCache.markVerifiedProposerSignature(blockSlot, blockRoot, signedBlock.signature);
333
+ }
@@ -2,15 +2,16 @@ import {ChainConfig, ChainForkConfig} from "@lodestar/config";
2
2
  import {
3
3
  KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH,
4
4
  KZG_COMMITMENTS_SUBTREE_INDEX,
5
+ MIN_SEED_LOOKAHEAD,
5
6
  NUMBER_OF_COLUMNS,
7
+ isForkPostFulu,
6
8
  } from "@lodestar/params";
7
9
  import {
8
10
  computeEpochAtSlot,
9
11
  computeStartSlotAtEpoch,
10
12
  getBlockHeaderProposerSignatureSetByHeaderSlot,
11
- getBlockHeaderProposerSignatureSetByParentStateSlot,
12
13
  } from "@lodestar/state-transition";
13
- import {DataColumnSidecar, Root, Slot, SubnetID, fulu, gloas, ssz} from "@lodestar/types";
14
+ import {DataColumnSidecar, Root, Slot, SubnetID, ValidatorIndex, fulu, gloas, ssz} from "@lodestar/types";
14
15
  import {byteArrayEquals, toRootHex, verifyMerkleBranch} from "@lodestar/utils";
15
16
  import {BeaconMetrics} from "../../metrics/metrics/beacon.js";
16
17
  import {Metrics} from "../../metrics/metrics.js";
@@ -104,19 +105,23 @@ export async function validateGossipFuluDataColumnSidecar(
104
105
  });
105
106
  }
106
107
 
107
- // getBlockSlotState also checks for whether the current finalized checkpoint is an ancestor of the block.
108
- // As a result, we throw an IGNORE (whereas the spec says we should REJECT for this scenario).
109
- // this is something we should change this in the future to make the code airtight to the spec.
110
108
  // 7) [REJECT] The sidecar's block's parent passes validation.
111
- const blockState = await chain.regen
112
- .getBlockSlotState(parentBlock, blockHeader.slot, {dontTransferCache: true}, RegenCaller.validateGossipDataColumn)
113
- .catch(() => {
114
- throw new DataColumnSidecarGossipError(GossipAction.IGNORE, {
115
- code: DataColumnSidecarErrorCode.PARENT_UNKNOWN,
116
- parentRoot,
117
- slot: blockHeader.slot,
118
- });
119
- });
109
+ // Post-fulu, we can use parent state to get expected proposer index thanks to proposer lookahead
110
+ const parentEpoch = computeEpochAtSlot(parentBlock.slot);
111
+ const blockEpoch = computeEpochAtSlot(blockHeader.slot);
112
+ const getProposerIndex = async (): Promise<ValidatorIndex> => {
113
+ if (isForkPostFulu(chain.config.getForkName(parentBlock.slot)) && blockEpoch - parentEpoch <= MIN_SEED_LOOKAHEAD) {
114
+ const parentState = await chain.regen.getState(parentBlock.stateRoot, RegenCaller.validateGossipDataColumn);
115
+ return parentState.getBeaconProposer(blockHeader.slot);
116
+ }
117
+ const blockState = await chain.regen.getBlockSlotState(
118
+ parentBlock,
119
+ blockHeader.slot,
120
+ {dontTransferCache: true},
121
+ RegenCaller.validateGossipDataColumn
122
+ );
123
+ return blockState.getBeaconProposer(blockHeader.slot);
124
+ };
120
125
 
121
126
  // 13) [REJECT] The sidecar is proposed by the expected proposer_index for the block's slot in the context of the current
122
127
  // shuffling (defined by block_header.parent_root/block_header.slot). If the proposer_index cannot
@@ -124,7 +129,13 @@ export async function validateGossipFuluDataColumnSidecar(
124
129
  // while proposers for the block's branch are calculated -- in such a case do not REJECT, instead IGNORE
125
130
  // this message.
126
131
  const proposerIndex = blockHeader.proposerIndex;
127
- const expectedProposerIndex = blockState.getBeaconProposer(blockHeader.slot);
132
+ const expectedProposerIndex = await getProposerIndex().catch(() => {
133
+ throw new DataColumnSidecarGossipError(GossipAction.IGNORE, {
134
+ code: DataColumnSidecarErrorCode.PARENT_UNKNOWN,
135
+ parentRoot,
136
+ slot: blockHeader.slot,
137
+ });
138
+ });
128
139
 
129
140
  if (proposerIndex !== expectedProposerIndex) {
130
141
  throw new DataColumnSidecarGossipError(GossipAction.REJECT, {
@@ -137,9 +148,8 @@ export async function validateGossipFuluDataColumnSidecar(
137
148
  // 5) [REJECT] The proposer signature of sidecar.signed_block_header, is valid with respect to the block_header.proposer_index pubkey.
138
149
  const signature = dataColumnSidecar.signedBlockHeader.signature;
139
150
  if (!chain.seenBlockInputCache.isVerifiedProposerSignature(blockHeader.slot, blockRootHex, signature)) {
140
- const signatureSet = getBlockHeaderProposerSignatureSetByParentStateSlot(
151
+ const signatureSet = getBlockHeaderProposerSignatureSetByHeaderSlot(
141
152
  chain.config,
142
- blockState.slot,
143
153
  dataColumnSidecar.signedBlockHeader
144
154
  );
145
155
 
@@ -1179,6 +1179,10 @@ export function createLodestarMetrics(
1179
1179
  name: "lodestar_oppool_proposer_slashing_pool_size",
1180
1180
  help: "Current size of the ProposerSlashingPool",
1181
1181
  }),
1182
+ proposerSlashingsProduced: register.counter({
1183
+ name: "lodestar_oppool_proposer_slashings_produced_total",
1184
+ help: "Total number of proposer slashings produced from observed equivocations",
1185
+ }),
1182
1186
  voluntaryExitPoolSize: register.gauge({
1183
1187
  name: "lodestar_oppool_voluntary_exit_pool_size",
1184
1188
  help: "Current size of the VoluntaryExitPool",
@@ -1708,6 +1712,41 @@ export function createLodestarMetrics(
1708
1712
  }),
1709
1713
  },
1710
1714
 
1715
+ // Pre-verify builder-deposit signatures in the epochs before the Gloas fork (prepareNextSlot).
1716
+ builderDepositPreVerify: {
1717
+ duration: register.histogram({
1718
+ name: "lodestar_builder_deposit_preverify_duration_seconds",
1719
+ help: "Duration of one preVerifyBuilderDepositsPreGloas call (BLS batch verify + scan)",
1720
+ buckets: [1, 2, 3, 4],
1721
+ }),
1722
+ pendingDeposits: register.gauge({
1723
+ name: "lodestar_builder_deposit_preverify_pending_deposits",
1724
+ help: "Current state.pendingDeposits length (backlog to drain before the fork)",
1725
+ }),
1726
+ cachedDeposits: register.gauge({
1727
+ name: "lodestar_builder_deposit_preverify_cached_deposits",
1728
+ help: "Deposit signatures verified & cached so far this pre-fork window (valid + invalid)",
1729
+ }),
1730
+ scannedDeposits: register.gauge({
1731
+ name: "lodestar_builder_deposit_preverify_scanned_deposits",
1732
+ help: "Pending deposits examined by the last pre-verify call (< pending_deposits ⇒ cap hit)",
1733
+ }),
1734
+ builderPubkeys: register.gauge({
1735
+ name: "lodestar_builder_deposit_preverify_builder_pubkeys",
1736
+ help: "Distinct builder pubkeys tracked this window (validator deposits for these are pre-verified)",
1737
+ }),
1738
+ validSignatures: register.counter<{type: "builder" | "validator"}>({
1739
+ name: "lodestar_builder_deposit_preverify_valid_signatures_total",
1740
+ help: "Cumulative deposit signatures whose BLS verification passed (VALID), by credential type",
1741
+ labelNames: ["type"],
1742
+ }),
1743
+ invalidSignatures: register.counter<{type: "builder" | "validator"}>({
1744
+ name: "lodestar_builder_deposit_preverify_invalid_signatures_total",
1745
+ help: "Cumulative deposit signatures whose BLS verification failed (INVALID), by credential type",
1746
+ labelNames: ["type"],
1747
+ }),
1748
+ },
1749
+
1711
1750
  // reprocess attestations
1712
1751
  reprocessApiAttestations: {
1713
1752
  total: register.gauge({