@lodestar/beacon-node 1.48.0-dev.d55b13cf5e → 1.48.0-dev.df6a2aa429

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 (127) hide show
  1. package/lib/api/impl/beacon/blocks/index.d.ts.map +1 -1
  2. package/lib/api/impl/beacon/blocks/index.js +0 -7
  3. package/lib/api/impl/beacon/blocks/index.js.map +1 -1
  4. package/lib/api/impl/beacon/state/utils.d.ts.map +1 -1
  5. package/lib/api/impl/beacon/state/utils.js +14 -0
  6. package/lib/api/impl/beacon/state/utils.js.map +1 -1
  7. package/lib/api/impl/config/index.d.ts.map +1 -1
  8. package/lib/api/impl/config/index.js +4 -1
  9. package/lib/api/impl/config/index.js.map +1 -1
  10. package/lib/api/impl/validator/index.d.ts.map +1 -1
  11. package/lib/api/impl/validator/index.js +39 -39
  12. package/lib/api/impl/validator/index.js.map +1 -1
  13. package/lib/chain/archiveStore/archiveStore.d.ts.map +1 -1
  14. package/lib/chain/archiveStore/archiveStore.js +4 -1
  15. package/lib/chain/archiveStore/archiveStore.js.map +1 -1
  16. package/lib/chain/archiveStore/utils/archiveBlocks.d.ts +9 -1
  17. package/lib/chain/archiveStore/utils/archiveBlocks.d.ts.map +1 -1
  18. package/lib/chain/archiveStore/utils/archiveBlocks.js +37 -4
  19. package/lib/chain/archiveStore/utils/archiveBlocks.js.map +1 -1
  20. package/lib/chain/blocks/importBlock.d.ts +5 -1
  21. package/lib/chain/blocks/importBlock.d.ts.map +1 -1
  22. package/lib/chain/blocks/importBlock.js +12 -18
  23. package/lib/chain/blocks/importBlock.js.map +1 -1
  24. package/lib/chain/blocks/index.d.ts +8 -4
  25. package/lib/chain/blocks/index.d.ts.map +1 -1
  26. package/lib/chain/blocks/index.js +80 -15
  27. package/lib/chain/blocks/index.js.map +1 -1
  28. package/lib/chain/blocks/types.d.ts +8 -0
  29. package/lib/chain/blocks/types.d.ts.map +1 -1
  30. package/lib/chain/blocks/types.js.map +1 -1
  31. package/lib/chain/blocks/utils/chainSegment.d.ts +1 -1
  32. package/lib/chain/blocks/utils/chainSegment.d.ts.map +1 -1
  33. package/lib/chain/blocks/utils/chainSegment.js +26 -11
  34. package/lib/chain/blocks/utils/chainSegment.js.map +1 -1
  35. package/lib/chain/chain.d.ts +7 -2
  36. package/lib/chain/chain.d.ts.map +1 -1
  37. package/lib/chain/chain.js +69 -40
  38. package/lib/chain/chain.js.map +1 -1
  39. package/lib/chain/emitter.d.ts +1 -1
  40. package/lib/chain/emitter.js +1 -1
  41. package/lib/chain/errors/executionPayloadBid.d.ts +4 -0
  42. package/lib/chain/errors/executionPayloadBid.d.ts.map +1 -1
  43. package/lib/chain/errors/executionPayloadBid.js +1 -0
  44. package/lib/chain/errors/executionPayloadBid.js.map +1 -1
  45. package/lib/chain/errors/gossipValidation.d.ts +2 -1
  46. package/lib/chain/errors/gossipValidation.d.ts.map +1 -1
  47. package/lib/chain/errors/gossipValidation.js +1 -0
  48. package/lib/chain/errors/gossipValidation.js.map +1 -1
  49. package/lib/chain/forkChoice/index.d.ts.map +1 -1
  50. package/lib/chain/forkChoice/index.js +2 -0
  51. package/lib/chain/forkChoice/index.js.map +1 -1
  52. package/lib/chain/interface.d.ts +2 -2
  53. package/lib/chain/interface.d.ts.map +1 -1
  54. package/lib/chain/lightClient/index.d.ts +3 -3
  55. package/lib/chain/lightClient/index.d.ts.map +1 -1
  56. package/lib/chain/lightClient/index.js +21 -23
  57. package/lib/chain/lightClient/index.js.map +1 -1
  58. package/lib/chain/lightClient/proofs.d.ts +2 -1
  59. package/lib/chain/lightClient/proofs.d.ts.map +1 -1
  60. package/lib/chain/lightClient/proofs.js +5 -1
  61. package/lib/chain/lightClient/proofs.js.map +1 -1
  62. package/lib/chain/opPools/aggregatedAttestationPool.js +1 -1
  63. package/lib/chain/opPools/aggregatedAttestationPool.js.map +1 -1
  64. package/lib/chain/validation/executionPayloadBid.d.ts.map +1 -1
  65. package/lib/chain/validation/executionPayloadBid.js +16 -0
  66. package/lib/chain/validation/executionPayloadBid.js.map +1 -1
  67. package/lib/chain/validation/payloadAttestationMessage.d.ts.map +1 -1
  68. package/lib/chain/validation/payloadAttestationMessage.js +5 -6
  69. package/lib/chain/validation/payloadAttestationMessage.js.map +1 -1
  70. package/lib/chain/validatorMonitor.d.ts +7 -1
  71. package/lib/chain/validatorMonitor.d.ts.map +1 -1
  72. package/lib/chain/validatorMonitor.js +22 -5
  73. package/lib/chain/validatorMonitor.js.map +1 -1
  74. package/lib/execution/builder/validateBid.d.ts.map +1 -1
  75. package/lib/execution/builder/validateBid.js +3 -0
  76. package/lib/execution/builder/validateBid.js.map +1 -1
  77. package/lib/execution/engine/mock.d.ts.map +1 -1
  78. package/lib/execution/engine/mock.js +4 -1
  79. package/lib/execution/engine/mock.js.map +1 -1
  80. package/lib/metrics/metrics/lodestar.d.ts +4 -3
  81. package/lib/metrics/metrics/lodestar.d.ts.map +1 -1
  82. package/lib/metrics/metrics/lodestar.js +5 -5
  83. package/lib/metrics/metrics/lodestar.js.map +1 -1
  84. package/lib/network/network.js +2 -2
  85. package/lib/network/network.js.map +1 -1
  86. package/lib/network/processor/gossipHandlers.d.ts.map +1 -1
  87. package/lib/network/processor/gossipHandlers.js +382 -278
  88. package/lib/network/processor/gossipHandlers.js.map +1 -1
  89. package/lib/network/processor/index.d.ts.map +1 -1
  90. package/lib/network/processor/index.js +22 -16
  91. package/lib/network/processor/index.js.map +1 -1
  92. package/lib/network/reqresp/rateLimit.js +2 -1
  93. package/lib/network/reqresp/rateLimit.js.map +1 -1
  94. package/lib/sync/range/range.d.ts.map +1 -1
  95. package/lib/sync/range/range.js +24 -1
  96. package/lib/sync/range/range.js.map +1 -1
  97. package/package.json +14 -14
  98. package/src/api/impl/beacon/blocks/index.ts +0 -10
  99. package/src/api/impl/beacon/state/utils.ts +15 -0
  100. package/src/api/impl/config/index.ts +4 -1
  101. package/src/api/impl/validator/index.ts +44 -43
  102. package/src/chain/archiveStore/archiveStore.ts +6 -0
  103. package/src/chain/archiveStore/utils/archiveBlocks.ts +39 -3
  104. package/src/chain/blocks/importBlock.ts +25 -20
  105. package/src/chain/blocks/index.ts +107 -21
  106. package/src/chain/blocks/types.ts +9 -0
  107. package/src/chain/blocks/utils/chainSegment.ts +26 -12
  108. package/src/chain/chain.ts +78 -50
  109. package/src/chain/emitter.ts +1 -1
  110. package/src/chain/errors/executionPayloadBid.ts +2 -0
  111. package/src/chain/errors/gossipValidation.ts +1 -0
  112. package/src/chain/forkChoice/index.ts +2 -0
  113. package/src/chain/interface.ts +2 -2
  114. package/src/chain/lightClient/index.ts +32 -36
  115. package/src/chain/lightClient/proofs.ts +10 -0
  116. package/src/chain/opPools/aggregatedAttestationPool.ts +1 -1
  117. package/src/chain/validation/executionPayloadBid.ts +18 -0
  118. package/src/chain/validation/payloadAttestationMessage.ts +8 -9
  119. package/src/chain/validatorMonitor.ts +24 -7
  120. package/src/execution/builder/validateBid.ts +4 -0
  121. package/src/execution/engine/mock.ts +4 -1
  122. package/src/metrics/metrics/lodestar.ts +6 -5
  123. package/src/network/network.ts +2 -2
  124. package/src/network/processor/gossipHandlers.ts +489 -344
  125. package/src/network/processor/index.ts +24 -17
  126. package/src/network/reqresp/rateLimit.ts +2 -1
  127. package/src/sync/range/range.ts +23 -1
@@ -15,7 +15,9 @@ import {
15
15
  } from "@lodestar/types";
16
16
  import {byteArrayEquals, fromHex} from "@lodestar/utils";
17
17
  import {IBeaconChain} from "../../../../chain/index.js";
18
+ import {GENESIS_EPOCH} from "../../../../constants/index.js";
18
19
  import {IBeaconSync} from "../../../../sync/index.js";
20
+ import {isOptimisticBlock} from "../../../../util/forkChoice.js";
19
21
  import {ApiError, ValidationError} from "../../errors.js";
20
22
  import {notWhileSyncing} from "../../utils.js";
21
23
 
@@ -65,6 +67,19 @@ export async function getStateResponseWithRegen(
65
67
  notWhileSyncing(chain, sync.state);
66
68
  }
67
69
 
70
+ if (inStateId === "head") {
71
+ const head = chain.forkChoice.getHead();
72
+ // Head block's post-state is not cached after init from an anchor state at a skipped boundary slot,
73
+ // it does not exist anywhere and regen has no seed for it, serve the closest head state instead
74
+ const state = chain.regen.getStateSync(head.stateRoot) ?? chain.getHeadState();
75
+ const finalizedEpoch = chain.forkChoice.getFinalizedCheckpoint().epoch;
76
+ return {
77
+ state,
78
+ executionOptimistic: isOptimisticBlock(head),
79
+ finalized: state.epoch <= finalizedEpoch && finalizedEpoch !== GENESIS_EPOCH,
80
+ };
81
+ }
82
+
68
83
  const stateId = resolveStateId(chain.forkChoice, inStateId);
69
84
 
70
85
  const res =
@@ -20,11 +20,14 @@ export function renderJsonSpec(config: ChainConfig): routes.config.Spec {
20
20
  const presetJson = presetToJson(activePreset);
21
21
  const constantsJson = specValuesToJson(specConstants);
22
22
 
23
- // TODO Fulu: remove this check once interop issues are resolved
23
+ // TODO Fulu/Gloas: remove these checks once interop issues are resolved
24
24
  // see https://github.com/attestantio/go-eth2-client/issues/230
25
25
  if (config.FULU_FORK_EPOCH === Infinity) {
26
26
  delete configJson.BLOB_SCHEDULE;
27
27
  }
28
+ if (config.GLOAS_FORK_EPOCH === Infinity) {
29
+ delete configJson.GAS_LIMIT_SCHEDULE;
30
+ }
28
31
 
29
32
  return {...configJson, ...presetJson, ...constantsJson};
30
33
  }
@@ -133,40 +133,37 @@ type BidCandidate = {
133
133
  };
134
134
 
135
135
  /**
136
- * Return the best bid by boosted total payment. A candidate with the max boost factor is
137
- * preferred over any other regardless of value, ties prefer a builder api bid over the
138
- * p2p bid, and the earlier received bid between builder api bids.
136
+ * Ranking value of a bid, the boosted total payment scaled by 100 as `boostFactor` is a percentage.
137
+ * Kept scaled so bids that differ by less than a percent are not truncated into a tie.
139
138
  */
140
- function selectBestBid(candidates: BidCandidate[]): BidCandidate | null {
141
- const boostedValue = ({totalGwei, boostFactor}: BidCandidate): bigint => boostFactor * totalGwei;
142
- let best: BidCandidate | null = null;
143
- for (const candidate of candidates) {
144
- if (best === null) {
145
- best = candidate;
146
- continue;
147
- }
148
- // Preserve max boost preference before comparing bid values
149
- const candidateIsMaxBoost = candidate.boostFactor === MAX_BUILDER_BOOST_FACTOR;
150
- const bestIsMaxBoost = best.boostFactor === MAX_BUILDER_BOOST_FACTOR;
151
- if (candidateIsMaxBoost !== bestIsMaxBoost) {
152
- if (candidateIsMaxBoost) {
153
- best = candidate;
154
- }
155
- continue;
156
- }
157
- const candidateValue = boostedValue(candidate);
158
- const bestValue = boostedValue(best);
159
- if (candidateValue > bestValue) {
160
- best = candidate;
161
- } else if (
162
- candidateValue === bestValue &&
163
- // A tie prefers a builder api bid over the p2p bid, and the earlier received bid otherwise
164
- (best.url === undefined || (candidate.url !== undefined && candidate.receivedMs < best.receivedMs))
165
- ) {
166
- best = candidate;
167
- }
139
+ function getBoostedTotalScaled({totalGwei, boostFactor}: BidCandidate): bigint {
140
+ return boostFactor * totalGwei;
141
+ }
142
+
143
+ /** Order bid candidates by preference, best first, used to select a bid and to log the ranking */
144
+ function compareBidCandidates(a: BidCandidate, b: BidCandidate): number {
145
+ // Preserve max boost preference before comparing bid values
146
+ const aIsMaxBoost = a.boostFactor === MAX_BUILDER_BOOST_FACTOR;
147
+ const bIsMaxBoost = b.boostFactor === MAX_BUILDER_BOOST_FACTOR;
148
+ if (aIsMaxBoost !== bIsMaxBoost) {
149
+ return aIsMaxBoost ? -1 : 1;
168
150
  }
169
- return best;
151
+
152
+ const aBoostedTotal = getBoostedTotalScaled(a);
153
+ const bBoostedTotal = getBoostedTotalScaled(b);
154
+ if (aBoostedTotal !== bBoostedTotal) {
155
+ return aBoostedTotal > bBoostedTotal ? -1 : 1;
156
+ }
157
+
158
+ // A tie prefers a builder api bid over the p2p bid
159
+ const aIsBuilderApi = a.url !== undefined;
160
+ const bIsBuilderApi = b.url !== undefined;
161
+ if (aIsBuilderApi !== bIsBuilderApi) {
162
+ return aIsBuilderApi ? -1 : 1;
163
+ }
164
+
165
+ // and the earlier received bid between builder api bids
166
+ return a.receivedMs - b.receivedMs;
170
167
  }
171
168
 
172
169
  type ProduceBlockContentsRes = {executionPayloadValue: Wei; consensusBlockValue: Wei} & {
@@ -1049,20 +1046,24 @@ export function getValidatorApi(
1049
1046
  });
1050
1047
  }
1051
1048
 
1052
- const best = selectBestBid(candidates);
1053
- if (candidates.length > 0) {
1054
- logger.debug("Ranked builder bid candidates", {
1049
+ if (candidates.length === 0) {
1050
+ return null;
1051
+ }
1052
+
1053
+ const rankedCandidates = candidates.toSorted(compareBidCandidates);
1054
+ for (const [index, candidate] of rankedCandidates.entries()) {
1055
+ logger.debug("Builder bid candidate", {
1055
1056
  slot,
1056
- candidates: candidates
1057
- .map(
1058
- (candidate) =>
1059
- `${candidate.url ?? "p2p"}:total=${prettyGweiToEth(candidate.totalGwei)}:boost=${candidate.boostFactor}:received=${candidate.receivedMs}ms`
1060
- )
1061
- .join(","),
1062
- bidSource: best?.url ?? "p2p",
1057
+ rank: index + 1,
1058
+ source: candidate.url !== undefined ? toPrintableUrl(candidate.url) : "p2p",
1059
+ builder: candidate.signedBid.message.builderIndex,
1060
+ total: prettyGweiToEth(candidate.totalGwei),
1061
+ boost: candidate.boostFactor,
1062
+ boosted: prettyGweiToEth(getBoostedTotalScaled(candidate) / 100n),
1063
+ receivedMs: candidate.receivedMs,
1063
1064
  });
1064
1065
  }
1065
- return best;
1066
+ return rankedCandidates[0];
1066
1067
  })();
1067
1068
 
1068
1069
  const commonBlockBodyPromise = chain.produceCommonBlockBody({
@@ -1,5 +1,6 @@
1
1
  import {CheckpointWithHex} from "@lodestar/fork-choice";
2
2
  import {LoggerNode} from "@lodestar/logger/node";
3
+ import {SLOTS_PER_EPOCH} from "@lodestar/params";
3
4
  import {Checkpoint} from "@lodestar/types/phase0";
4
5
  import {callFnWhenAwait} from "@lodestar/utils";
5
6
  import {IBeaconDb} from "../../db/index.js";
@@ -208,6 +209,9 @@ export class ArchiveStore {
208
209
  const finalizedEpoch = finalized.epoch;
209
210
  this.logger.verbose("Start processing finalized checkpoint", {epoch: finalizedEpoch, rootHex: finalized.rootHex});
210
211
 
212
+ // we want to track late imported canonical blocks, but it's not nice to do it at syncig time
213
+ const isNodeSynced = this.chain.clock.currentSlot - this.chain.forkChoice.getHead().slot <= SLOTS_PER_EPOCH;
214
+
211
215
  let timer = this.metrics?.processFinalizedCheckpoint.durationByTask.startTimer();
212
216
  await archiveBlocks(
213
217
  this.chain.config,
@@ -217,6 +221,8 @@ export class ArchiveStore {
217
221
  this.logger,
218
222
  finalized,
219
223
  this.chain.clock.currentEpoch,
224
+ this.metrics,
225
+ isNodeSynced,
220
226
  this.archiveDataEpochs,
221
227
  this.chain.opts.persistOrphanedBlocks,
222
228
  this.chain.opts.persistOrphanedBlocksDir
@@ -8,6 +8,7 @@ import {Epoch, Slot} from "@lodestar/types";
8
8
  import {Logger, fromAsync, fromHex, prettyPrintIndices, toRootHex} from "@lodestar/utils";
9
9
  import {IBeaconDb} from "../../../db/index.js";
10
10
  import {BlockArchiveBatchPutBinaryItem} from "../../../db/repositories/index.js";
11
+ import {Metrics} from "../../../metrics/metrics.js";
11
12
  import {ensureDir, writeIfNotExist} from "../../../util/file.js";
12
13
  import {BlockRootHex} from "../../../util/sszBytes.js";
13
14
  import {LightClientServer} from "../../lightClient/index.js";
@@ -20,6 +21,16 @@ const BLOB_SIDECAR_BATCH_SIZE = 32;
20
21
 
21
22
  type BlockRootSlot = {slot: Slot; root: Uint8Array};
22
23
 
24
+ /**
25
+ * Why a finalized-canonical block was imported after the attestation cutoff.
26
+ */
27
+ export enum LateCanonicalBlockReason {
28
+ // received on time, but finished importing after the attestation cutoff
29
+ SlowImport = "slow_import",
30
+ // block reached this node late on the network thread
31
+ LateReceive = "late_receive",
32
+ }
33
+
23
34
  /**
24
35
  * Persist orphaned block to disk
25
36
  */
@@ -54,6 +65,8 @@ export async function archiveBlocks(
54
65
  logger: Logger,
55
66
  finalizedCheckpoint: CheckpointWithHex,
56
67
  currentEpoch: Epoch,
68
+ metrics: Metrics | null,
69
+ isNodeSynced: boolean,
57
70
  archiveDataEpochs?: number,
58
71
  persistOrphanedBlocks?: boolean,
59
72
  persistOrphanedBlocksDir?: string
@@ -76,6 +89,29 @@ export async function archiveBlocks(
76
89
 
77
90
  const logCtx = {currentEpoch, finalizedEpoch: finalizedCheckpoint.epoch, finalizedRoot: finalizedCheckpoint.rootHex};
78
91
 
92
+ // `finalizedCanonicalBlocks` is newest -> oldest; its LAST element is the previous-finalized
93
+ // boundary block, already audited on the prior run, so exclude it to avoid double counting.
94
+ if (isNodeSynced && finalizedCanonicalBlocks.length > 1) {
95
+ const lateBlocks = finalizedCanonicalBlocks.slice(0, -1).filter((block) => !block.importedTimely);
96
+ if (lateBlocks.length > 0) {
97
+ let slowImport = 0;
98
+ for (const block of lateBlocks) {
99
+ // received late => late_receive; otherwise we had it on time but were slow to import => slow_import
100
+ const reason = block.timeliness ? LateCanonicalBlockReason.SlowImport : LateCanonicalBlockReason.LateReceive;
101
+ if (block.timeliness) slowImport++;
102
+ metrics?.importBlock.lateCanonicalBlock.inc({reason});
103
+ }
104
+ const lateSlots = lateBlocks.map((block) => block.slot).sort((a, b) => a - b);
105
+ logger.verbose("Late imported canonical blocks", {
106
+ ...logCtx,
107
+ count: lateBlocks.length,
108
+ slowImport,
109
+ lateReceive: lateBlocks.length - slowImport,
110
+ slotRange: prettyPrintIndices(lateSlots),
111
+ });
112
+ }
113
+ }
114
+
79
115
  if (finalizedCanonicalBlockRoots.length > 0) {
80
116
  const migratedSlots = await migrateBlocksFromHotToColdDb(db, logger, finalizedCanonicalBlockRoots);
81
117
  logger.verbose("Migrated blocks from hot DB to cold DB", {
@@ -298,7 +334,7 @@ async function migrateBlocksFromHotToColdDb(db: IBeaconDb, logger: Logger, block
298
334
  ]);
299
335
  for (const entry of canonicalBlockEntries) migratedSlots.push(entry.slot);
300
336
  }
301
- // Ancestor walk is newest → oldest; sort ascending so `prettyPrintIndices` renders cleanly.
337
+ // Ancestor walk is newest to oldest; sort ascending so `prettyPrintIndices` renders cleanly.
302
338
  return migratedSlots.sort((a, b) => a - b);
303
339
  }
304
340
 
@@ -437,7 +473,7 @@ async function migrateDataColumnSidecarsFromHotToColdDb(
437
473
  await Promise.all(promises);
438
474
  }
439
475
 
440
- // Ancestor walk is newest → oldest; sort ascending so `prettyPrintIndices` renders cleanly.
476
+ // Ancestor walk is newest to oldest; sort ascending so `prettyPrintIndices` renders cleanly.
441
477
  return migratedSlots.sort((a, b) => a - b);
442
478
  }
443
479
 
@@ -485,7 +521,7 @@ async function migrateExecutionPayloadEnvelopesFromHotToColdDb(
485
521
  ]);
486
522
 
487
523
  // Slots are ascending in hot-db key order — sort to guarantee `prettyPrintIndices` output is clean
488
- // regardless of ancestor-walk order (newest → oldest).
524
+ // regardless of ancestor-walk order (newest to oldest).
489
525
  return envelopeEntries.map((entry) => entry.key).sort((a, b) => a - b);
490
526
  }
491
527
 
@@ -25,7 +25,17 @@ import {
25
25
  isStatePostAltair,
26
26
  isStatePostBellatrix,
27
27
  } from "@lodestar/state-transition";
28
- import {Attestation, BeaconBlock, altair, capella, electra, isGloasBeaconBlock, phase0, ssz} from "@lodestar/types";
28
+ import {
29
+ Attestation,
30
+ BeaconBlock,
31
+ Epoch,
32
+ altair,
33
+ capella,
34
+ electra,
35
+ isGloasBeaconBlock,
36
+ phase0,
37
+ ssz,
38
+ } from "@lodestar/types";
29
39
  import {isErrorAborted, toRootHex} from "@lodestar/utils";
30
40
  import {GENESIS_SLOT, ZERO_HASH_HEX} from "../../constants/index.js";
31
41
  import {callInNextEventLoop} from "../../util/eventLoop.js";
@@ -44,6 +54,15 @@ import {getCheckpointFromState} from "./utils/checkpoint.js";
44
54
  * Fork-choice allows to import attestations from current (0) or past (1) epoch.
45
55
  */
46
56
  const FORK_CHOICE_ATT_EPOCH_LIMIT = 1;
57
+ /**
58
+ * Whether the attestations of a block at `blockEpoch` are imported into fork choice
59
+ */
60
+ export function importsBlockAttestations(opts: ImportBlockOpts, blockEpoch: Epoch, currentEpoch: Epoch): boolean {
61
+ return (
62
+ opts.importAttestations === AttestationImportOpt.Force ||
63
+ (opts.importAttestations !== AttestationImportOpt.Skip && blockEpoch >= currentEpoch - FORK_CHOICE_ATT_EPOCH_LIMIT)
64
+ );
65
+ }
47
66
  /**
48
67
  * Emit eventstream events for block contents events only for blocks that are recent enough to clock
49
68
  */
@@ -85,7 +104,7 @@ export async function importBlock(
85
104
  const currentEpoch = computeEpochAtSlot(currentSlot);
86
105
  const blockEpoch = computeEpochAtSlot(blockSlot);
87
106
  const prevFinalizedEpoch = this.forkChoice.getFinalizedCheckpoint().epoch;
88
- const blockDelaySec =
107
+ const receiveDelaySec =
89
108
  fullyVerifiedBlock.seenTimestampSec - computeTimeAtSlot(this.config, blockSlot, postState.genesisTime);
90
109
  const recvToValLatency = Date.now() / 1000 - (opts.seenTimestampSec ?? Date.now() / 1000);
91
110
  const fork = this.config.getForkSeq(blockSlot);
@@ -122,10 +141,12 @@ export async function importBlock(
122
141
  executionStatus = parentBlock.executionStatus;
123
142
  }
124
143
 
144
+ const importDelaySec = this.clock.secFromSlot(blockSlot);
125
145
  const blockSummary = this.forkChoice.onBlock(
126
146
  block.message,
127
147
  postState,
128
- blockDelaySec,
148
+ receiveDelaySec,
149
+ importDelaySec,
129
150
  currentSlot,
130
151
  executionStatus,
131
152
  dataAvailabilityStatus
@@ -147,10 +168,7 @@ export async function importBlock(
147
168
  // Only process attestations of blocks with relevant attestations for the fork-choice:
148
169
  // If current epoch is N, and block is epoch X, block may include attestations for epoch X or X - 1.
149
170
  // The latest block that is useful is at epoch N - 1 which may include attestations for epoch N - 1 or N - 2.
150
- if (
151
- opts.importAttestations === AttestationImportOpt.Force ||
152
- (opts.importAttestations !== AttestationImportOpt.Skip && blockEpoch >= currentEpoch - FORK_CHOICE_ATT_EPOCH_LIMIT)
153
- ) {
171
+ if (importsBlockAttestations(opts, blockEpoch, currentEpoch)) {
154
172
  const attestations = block.message.body.attestations;
155
173
  const rootCache = new RootCache(postState);
156
174
  const invalidAttestationErrorsByCode = new Map<string, {error: Error; count: number}>();
@@ -506,19 +524,6 @@ export async function importBlock(
506
524
  this.metrics?.previousJustifiedEpoch.set(checkpointState.previousJustifiedCheckpoint.epoch);
507
525
  this.metrics?.currentJustifiedEpoch.set(justifiedCheckpoint.epoch);
508
526
  }
509
- const finalizedCheckpoint = checkpointState.finalizedCheckpoint;
510
- const finalizedEpoch = finalizedCheckpoint.epoch;
511
- const preFinalizedEpoch = parentBlockSummary.finalizedEpoch;
512
- if (finalizedEpoch > preFinalizedEpoch) {
513
- this.emitter.emit(routes.events.EventType.finalizedCheckpoint, {
514
- block: toRootHex(finalizedCheckpoint.root),
515
- epoch: finalizedCheckpoint.epoch,
516
- state: toRootHex(checkpointState.hashTreeRoot()),
517
- executionOptimistic: false,
518
- });
519
- this.logger.verbose("Checkpoint finalized", toCheckpointHex(finalizedCheckpoint));
520
- this.metrics?.finalizedEpoch.set(finalizedCheckpoint.epoch);
521
- }
522
527
  }
523
528
  }
524
529
 
@@ -1,3 +1,5 @@
1
+ import {PayloadStatus} from "@lodestar/fork-choice";
2
+ import {computeEpochAtSlot} from "@lodestar/state-transition";
1
3
  import {SignedBeaconBlock, Slot} from "@lodestar/types";
2
4
  import {isErrorAborted, toRootHex} from "@lodestar/utils";
3
5
  import {Metrics} from "../../metrics/metrics.js";
@@ -5,15 +7,17 @@ import {nextEventLoop} from "../../util/eventLoop.js";
5
7
  import {JobItemQueue, isQueueErrorAborted} from "../../util/queue/index.js";
6
8
  import type {BeaconChain} from "../chain.js";
7
9
  import {BlockError, BlockErrorCode, isBlockErrorAborted} from "../errors/index.js";
10
+ import {ForkchoiceCaller} from "../forkChoice/index.js";
8
11
  import {BlockProcessOpts} from "../options.js";
9
12
  import {IBlockInput} from "./blockInput/types.js";
10
- import {importBlock} from "./importBlock.js";
13
+ import {importBlock, importsBlockAttestations} from "./importBlock.js";
11
14
  import {PayloadError, importExecutionPayload} from "./importExecutionPayload.js";
12
15
  import {PayloadEnvelopeInput} from "./payloadEnvelopeInput/payloadEnvelopeInput.js";
13
- import {FullyVerifiedBlock, ImportBlockOpts} from "./types.js";
14
- import {assertLinearChainSegment} from "./utils/chainSegment.js";
16
+ import {FullyVerifiedBlock, ImportBlockOpts, ProcessBlocksResult} from "./types.js";
17
+ import {OrphanedPayloadEnvelope, assertLinearChainSegment} from "./utils/chainSegment.js";
15
18
  import {verifyBlocksInEpoch} from "./verifyBlock.js";
16
19
  import {verifyBlocksSanityChecks} from "./verifyBlocksSanityChecks.js";
20
+ import {verifyPayloadsDataAvailability} from "./verifyPayloadsDataAvailability.js";
17
21
 
18
22
  export {AttestationImportOpt, type ImportBlockOpts} from "./types.js";
19
23
 
@@ -23,10 +27,16 @@ const QUEUE_MAX_LENGTH = 256;
23
27
  * BlockProcessor processes block jobs in a queued fashion, one after the other.
24
28
  */
25
29
  export class BlockProcessor {
26
- readonly jobQueue: JobItemQueue<[IBlockInput[], Map<Slot, PayloadEnvelopeInput> | null, ImportBlockOpts], void>;
30
+ readonly jobQueue: JobItemQueue<
31
+ [IBlockInput[], Map<Slot, PayloadEnvelopeInput> | null, ImportBlockOpts],
32
+ ProcessBlocksResult
33
+ >;
27
34
 
28
35
  constructor(chain: BeaconChain, metrics: Metrics | null, opts: BlockProcessOpts, signal: AbortSignal) {
29
- this.jobQueue = new JobItemQueue<[IBlockInput[], Map<Slot, PayloadEnvelopeInput> | null, ImportBlockOpts], void>(
36
+ this.jobQueue = new JobItemQueue<
37
+ [IBlockInput[], Map<Slot, PayloadEnvelopeInput> | null, ImportBlockOpts],
38
+ ProcessBlocksResult
39
+ >(
30
40
  (job, payloadEnvelopes, importOpts) => {
31
41
  return processBlocks.call(chain, job, payloadEnvelopes, {...opts, ...importOpts});
32
42
  },
@@ -39,8 +49,8 @@ export class BlockProcessor {
39
49
  job: IBlockInput[],
40
50
  payloadEnvelopes: Map<Slot, PayloadEnvelopeInput> | null,
41
51
  opts: ImportBlockOpts = {}
42
- ): Promise<void> {
43
- await this.jobQueue.push(job, payloadEnvelopes, opts);
52
+ ): Promise<ProcessBlocksResult> {
53
+ return this.jobQueue.push(job, payloadEnvelopes, opts);
44
54
  }
45
55
  }
46
56
 
@@ -59,9 +69,10 @@ export async function processBlocks(
59
69
  blocks: IBlockInput[],
60
70
  payloadEnvelopes: Map<Slot, PayloadEnvelopeInput> | null,
61
71
  opts: BlockProcessOpts & ImportBlockOpts
62
- ): Promise<void> {
72
+ ): Promise<ProcessBlocksResult> {
73
+ const emptyResult: ProcessBlocksResult = {orphaned: [], skipped: false};
63
74
  if (blocks.length === 0) {
64
- return; // TODO: or throw?
75
+ return emptyResult; // TODO: or throw?
65
76
  }
66
77
 
67
78
  try {
@@ -70,7 +81,7 @@ export async function processBlocks(
70
81
  // No relevant blocks, skip verifyBlocksInEpoch()
71
82
  if (relevantBlocks.length === 0 || parentBlock === null) {
72
83
  // parentBlock can only be null if relevantBlocks are empty
73
- return;
84
+ return await importPayloadEnvelopesOfKnownBlocks.call(this, payloadEnvelopes, opts);
74
85
  }
75
86
 
76
87
  const {warnings: orphanedPayloads} = assertLinearChainSegment(
@@ -79,12 +90,21 @@ export async function processBlocks(
79
90
  payloadEnvelopes,
80
91
  parentBlock
81
92
  );
82
- if (orphanedPayloads != null) {
83
- for (const orphaned of orphanedPayloads) {
84
- this.logger.debug("Orphaned payload envelope in chain segment", {
85
- slot: orphaned.slot,
86
- blockRoot: orphaned.payloadEnvelopeInput.blockRootHex,
87
- });
93
+ // Orphaned payloads of recent blocks are still imported, a reorg of the child could make the payload canonical
94
+ // again and later blocks build on it. Older ones are skipped: without the block attestations the orphaned FULL
95
+ // variant has no descendants and ties at zero weight with the EMPTY variant carrying the chain, the payload
96
+ // status tiebreaker then picks FULL and parks the head there.
97
+ const blockEpoch = computeEpochAtSlot(relevantBlocks[0].getBlock().message.slot);
98
+ const importsAttestations = importsBlockAttestations(opts, blockEpoch, this.clock.currentEpoch);
99
+ const skipped = orphanedPayloads != null && payloadEnvelopes !== null && !importsAttestations;
100
+ let payloadEnvelopesToImport = payloadEnvelopes;
101
+ if (skipped) {
102
+ payloadEnvelopesToImport = new Map(payloadEnvelopes);
103
+ for (const orphaned of orphanedPayloads ?? []) {
104
+ payloadEnvelopesToImport.delete(orphaned.slot);
105
+ // never validated, drop it from the shared cache so it is not served to peers, by-root sync reloads the
106
+ // entry from db if the payload is ever needed
107
+ this.seenPayloadEnvelopeInputCache.prune(orphaned.payloadEnvelopeInput.blockRootHex);
88
108
  }
89
109
  }
90
110
 
@@ -97,7 +117,7 @@ export async function processBlocks(
97
117
  proposerBalanceDeltas,
98
118
  segmentExecStatus,
99
119
  indexedAttestationsByBlock,
100
- } = await verifyBlocksInEpoch.call(this, parentBlock, relevantBlocks, payloadEnvelopes, opts);
120
+ } = await verifyBlocksInEpoch.call(this, parentBlock, relevantBlocks, payloadEnvelopesToImport, opts);
101
121
 
102
122
  // If segmentExecStatus has lvhForkchoice then, the entire segment should be invalid
103
123
  // and we need to further propagate
@@ -132,8 +152,8 @@ export async function processBlocks(
132
152
  }
133
153
 
134
154
  const slotSet = new Set<Slot>(blocks.map((b) => b.getBlock().message.slot));
135
- if (payloadEnvelopes) {
136
- for (const slot of payloadEnvelopes.keys()) slotSet.add(slot);
155
+ if (payloadEnvelopesToImport) {
156
+ for (const slot of payloadEnvelopesToImport.keys()) slotSet.add(slot);
137
157
  }
138
158
  const slots = Array.from(slotSet).sort((a, b) => a - b);
139
159
  for (const slot of slots) {
@@ -146,7 +166,7 @@ export async function processBlocks(
146
166
  // PayloadEnvelopeInput is shared and may receive an envelope after the DA snapshot was taken.
147
167
  const payloadDA = payloadDAStatuses.get(slot);
148
168
  if (payloadDA !== undefined) {
149
- const payloadInput = payloadEnvelopes?.get(slot);
169
+ const payloadInput = payloadEnvelopesToImport?.get(slot);
150
170
  if (payloadInput === undefined) {
151
171
  throw new Error(`Missing payload input for slot ${slot} after DA verification`);
152
172
  }
@@ -159,9 +179,11 @@ export async function processBlocks(
159
179
 
160
180
  await nextEventLoop();
161
181
  }
182
+
183
+ return {orphaned: orphanedPayloads ?? [], skipped};
162
184
  } catch (e) {
163
185
  if (isErrorAborted(e) || isQueueErrorAborted(e) || isBlockErrorAborted(e)) {
164
- return; // Ignore
186
+ return emptyResult; // Ignore
165
187
  }
166
188
 
167
189
  // above functions should only throw BlockError, or PayloadError from the gloas payload import
@@ -209,6 +231,70 @@ export async function processBlocks(
209
231
  }
210
232
  }
211
233
 
234
+ /**
235
+ * A peer can serve a block without its envelope, the block is then imported with a PENDING payload. Later batches
236
+ * carry the envelope but all their blocks are known, without importing it here every child building on the FULL
237
+ * variant fails with PARENT_PAYLOAD_UNKNOWN and range sync never progresses. If our head already descends from the
238
+ * block's EMPTY variant the chain did not build on the payload, importing it would only add a dead FULL leaf. As for
239
+ * orphaned payloads in a segment this only applies while the block attestations are not imported, a recent payload
240
+ * is imported anyway so a reorg building on it does not have to fetch it through unknown block sync.
241
+ */
242
+ async function importPayloadEnvelopesOfKnownBlocks(
243
+ this: BeaconChain,
244
+ payloadEnvelopes: Map<Slot, PayloadEnvelopeInput> | null,
245
+ opts: ImportBlockOpts
246
+ ): Promise<ProcessBlocksResult> {
247
+ if (payloadEnvelopes === null) {
248
+ return {orphaned: [], skipped: false};
249
+ }
250
+
251
+ const head = this.forkChoice.getHead();
252
+ const currentEpoch = this.clock.currentEpoch;
253
+ const payloadInputs: PayloadEnvelopeInput[] = [];
254
+ const orphaned: OrphanedPayloadEnvelope[] = [];
255
+ for (const payloadInput of payloadEnvelopes.values()) {
256
+ const {blockRootHex, slot} = payloadInput;
257
+ if (
258
+ !payloadInput.hasPayloadEnvelope() ||
259
+ !this.forkChoice.hasBlockHex(blockRootHex) ||
260
+ this.forkChoice.getBlockHexAndBlockHash(blockRootHex, payloadInput.getBlockHashHex()) !== null
261
+ ) {
262
+ continue;
263
+ }
264
+ if (
265
+ head.blockRoot !== blockRootHex &&
266
+ !importsBlockAttestations(opts, computeEpochAtSlot(slot), currentEpoch) &&
267
+ this.forkChoice.isDescendant(blockRootHex, PayloadStatus.EMPTY, head.blockRoot, head.payloadStatus)
268
+ ) {
269
+ // never validated, see processBlocks()
270
+ this.seenPayloadEnvelopeInputCache.prune(blockRootHex);
271
+ orphaned.push({slot, payloadEnvelopeInput: payloadInput});
272
+ continue;
273
+ }
274
+ payloadInputs.push(payloadInput);
275
+ }
276
+
277
+ // orphaned envelopes on this path are always skipped, range sync logs them with peer/client context
278
+ const result: ProcessBlocksResult = {orphaned, skipped: orphaned.length > 0};
279
+
280
+ if (payloadInputs.length === 0) {
281
+ return result;
282
+ }
283
+
284
+ const {dataAvailabilityStatuses} = await verifyPayloadsDataAvailability(payloadInputs, new AbortController().signal);
285
+ for (let i = 0; i < payloadInputs.length; i++) {
286
+ this.logger.debug("Importing payload envelope of known block", {
287
+ slot: payloadInputs[i].slot,
288
+ blockRoot: payloadInputs[i].blockRootHex,
289
+ });
290
+ await importExecutionPayload.call(this, payloadInputs[i], dataAvailabilityStatuses[i], {validSignature: false});
291
+ }
292
+ // the new FULL variants are not seen by the head cached from the last block import
293
+ this.recomputeForkChoiceHead(ForkchoiceCaller.importBlock);
294
+
295
+ return result;
296
+ }
297
+
212
298
  function getBlockOrPayloadError(e: unknown, block: SignedBeaconBlock): BlockError | PayloadError {
213
299
  if (e instanceof BlockError) {
214
300
  return e;
@@ -4,6 +4,7 @@ import {ForkSeq} from "@lodestar/params";
4
4
  import {DataAvailabilityStatus, IBeaconStateView, computeEpochAtSlot} from "@lodestar/state-transition";
5
5
  import type {IndexedAttestation, Slot, fulu} from "@lodestar/types";
6
6
  import {IBlockInput} from "./blockInput/types.js";
7
+ import {OrphanedPayloadEnvelope} from "./utils/chainSegment.js";
7
8
 
8
9
  export enum GossipedInputType {
9
10
  block = "block",
@@ -110,3 +111,11 @@ export type FullyVerifiedBlock = {
110
111
  /** If the execution payload couldn't be verified because of EL syncing status, used in optimistic sync */
111
112
  executionStatus: BlockExecutionStatus | PayloadExecutionStatus;
112
113
  };
114
+
115
+ /**
116
+ * Result of processing a chain segment.
117
+ */
118
+ export type ProcessBlocksResult = {
119
+ orphaned: OrphanedPayloadEnvelope[];
120
+ skipped: boolean;
121
+ };
@@ -22,7 +22,7 @@ export type ChainSegmentResult = {warnings: OrphanedPayloadEnvelope[] | null};
22
22
  * - Verifies parent root + slot linearity
23
23
  * - For gloas: verifies bid.parentBlockHash matches the tracked execution hash; if not, the
24
24
  * previous FULL envelope is treated as orphaned (segment continues as if previous slot was EMPTY)
25
- * - If an envelope exists for this slot: verifies it references this block's root
25
+ * - If an envelope exists for this slot (or the parent's slot): verifies it references this block's root
26
26
  * - Advances the tracked execution hash (FULL if envelope present, EMPTY if not)
27
27
  */
28
28
  export function assertLinearChainSegment(
@@ -37,17 +37,6 @@ export function assertLinearChainSegment(
37
37
 
38
38
  const warnings: OrphanedPayloadEnvelope[] = [];
39
39
 
40
- let parentExecHash: string | null = parentBlock?.executionPayloadBlockHash ?? null;
41
- if (parentBlock !== null) {
42
- const parentPayloadInput = payloadEnvelopes?.get(parentBlock.slot);
43
- if (parentPayloadInput?.hasPayloadEnvelope()) {
44
- // Checkpoint-sync first batch: the anchor is stored PENDING with the inherited (grandparent)
45
- // hash, not its own payload. Its payload envelope is in this batch, so use that blockHash as
46
- // the real parent EL head.
47
- parentExecHash = parentPayloadInput.getBlockHashHex();
48
- }
49
- }
50
-
51
40
  // Track the expected execution payload block hash through the segment, seeded from the FIRST
52
41
  // block's own bid
53
42
  const firstBlockMessage = blocks[0].getBlock().message;
@@ -55,6 +44,31 @@ export function assertLinearChainSegment(
55
44
  ? toRootHex(firstBlockMessage.body.signedExecutionPayloadBid.message.parentBlockHash)
56
45
  : null;
57
46
 
47
+ let parentExecHash: string | null = parentBlock?.executionPayloadBlockHash ?? null;
48
+ if (parentBlock !== null) {
49
+ const parentPayloadInput = payloadEnvelopes?.get(parentBlock.slot);
50
+ if (parentPayloadInput?.hasPayloadEnvelope()) {
51
+ // Envelopes are served by slot, the one at the parent's slot must reference the parent block. Without this
52
+ // check an envelope of a sibling block is taken as orphaned and only fails downstream in the payload import
53
+ if (parentPayloadInput.blockRootHex !== parentBlock.blockRoot) {
54
+ throw new BlockError(blocks[0].getBlock(), {
55
+ code: BlockErrorCode.ENVELOPE_BLOCK_ROOT_MISMATCH,
56
+ envelopeBlockRoot: parentPayloadInput.blockRootHex,
57
+ blockRoot: parentBlock.blockRoot,
58
+ });
59
+ }
60
+ const parentPayloadBlockHash = parentPayloadInput.getBlockHashHex();
61
+ if (currentExecHash === parentPayloadBlockHash) {
62
+ // Checkpoint-sync first batch: the anchor is stored PENDING with the inherited (grandparent)
63
+ // hash, its own payload is in this batch and is the real parent EL head
64
+ parentExecHash = parentPayloadBlockHash;
65
+ } else {
66
+ // First block builds on the parent's EMPTY variant, the parent's payload in this batch is orphaned
67
+ warnings.push({slot: parentBlock.slot, payloadEnvelopeInput: parentPayloadInput});
68
+ }
69
+ }
70
+ }
71
+
58
72
  // First block must build on the parent's EL head. PARENT_PAYLOAD_UNKNOWN (vs the in-segment
59
73
  // NON_LINEAR_PAYLOAD_ROOTS below) tells range sync this is a parent-link problem, not an in-batch one.
60
74
  if (parentExecHash !== null && currentExecHash !== null && currentExecHash !== parentExecHash) {