@lodestar/beacon-node 1.49.0-dev.c9061c6942 → 1.49.0-dev.c9d60c8080

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 (128) hide show
  1. package/lib/api/impl/beacon/blocks/index.js +2 -2
  2. package/lib/api/impl/beacon/blocks/index.js.map +1 -1
  3. package/lib/api/impl/validator/index.d.ts.map +1 -1
  4. package/lib/api/impl/validator/index.js +9 -7
  5. package/lib/api/impl/validator/index.js.map +1 -1
  6. package/lib/chain/archiveStore/historicalState/metrics.d.ts.map +1 -1
  7. package/lib/chain/archiveStore/historicalState/metrics.js +4 -0
  8. package/lib/chain/archiveStore/historicalState/metrics.js.map +1 -1
  9. package/lib/chain/archiveStore/utils/pruneHistory.d.ts.map +1 -1
  10. package/lib/chain/archiveStore/utils/pruneHistory.js +14 -3
  11. package/lib/chain/archiveStore/utils/pruneHistory.js.map +1 -1
  12. package/lib/chain/blocks/index.js +2 -2
  13. package/lib/chain/blocks/index.js.map +1 -1
  14. package/lib/chain/blocks/payloadEnvelopeInput/types.d.ts +1 -1
  15. package/lib/chain/blocks/payloadEnvelopeInput/types.d.ts.map +1 -1
  16. package/lib/chain/emitter.d.ts +10 -0
  17. package/lib/chain/emitter.d.ts.map +1 -1
  18. package/lib/chain/emitter.js +4 -0
  19. package/lib/chain/emitter.js.map +1 -1
  20. package/lib/chain/errors/attesterSlashingError.d.ts +4 -1
  21. package/lib/chain/errors/attesterSlashingError.d.ts.map +1 -1
  22. package/lib/chain/errors/attesterSlashingError.js +1 -0
  23. package/lib/chain/errors/attesterSlashingError.js.map +1 -1
  24. package/lib/chain/errors/payloadAttestation.d.ts +4 -0
  25. package/lib/chain/errors/payloadAttestation.d.ts.map +1 -1
  26. package/lib/chain/errors/payloadAttestation.js +1 -0
  27. package/lib/chain/errors/payloadAttestation.js.map +1 -1
  28. package/lib/chain/errors/proposerSlashingError.d.ts +4 -1
  29. package/lib/chain/errors/proposerSlashingError.d.ts.map +1 -1
  30. package/lib/chain/errors/proposerSlashingError.js +1 -0
  31. package/lib/chain/errors/proposerSlashingError.js.map +1 -1
  32. package/lib/chain/options.d.ts +0 -4
  33. package/lib/chain/options.d.ts.map +1 -1
  34. package/lib/chain/options.js +0 -1
  35. package/lib/chain/options.js.map +1 -1
  36. package/lib/chain/produceBlock/produceBlockBody.d.ts.map +1 -1
  37. package/lib/chain/produceBlock/produceBlockBody.js +6 -6
  38. package/lib/chain/produceBlock/produceBlockBody.js.map +1 -1
  39. package/lib/chain/seenCache/seenPayloadEnvelopeInput.d.ts +2 -1
  40. package/lib/chain/seenCache/seenPayloadEnvelopeInput.d.ts.map +1 -1
  41. package/lib/chain/seenCache/seenPayloadEnvelopeInput.js +3 -2
  42. package/lib/chain/seenCache/seenPayloadEnvelopeInput.js.map +1 -1
  43. package/lib/chain/validation/attesterSlashing.d.ts.map +1 -1
  44. package/lib/chain/validation/attesterSlashing.js +1 -2
  45. package/lib/chain/validation/attesterSlashing.js.map +1 -1
  46. package/lib/chain/validation/payloadAttestationMessage.d.ts.map +1 -1
  47. package/lib/chain/validation/payloadAttestationMessage.js +8 -1
  48. package/lib/chain/validation/payloadAttestationMessage.js.map +1 -1
  49. package/lib/chain/validation/proposerSlashing.js +1 -2
  50. package/lib/chain/validation/proposerSlashing.js.map +1 -1
  51. package/lib/chain/validatorMonitor.d.ts.map +1 -1
  52. package/lib/chain/validatorMonitor.js +2 -2
  53. package/lib/chain/validatorMonitor.js.map +1 -1
  54. package/lib/db/repositories/blockArchive.d.ts +5 -0
  55. package/lib/db/repositories/blockArchive.d.ts.map +1 -1
  56. package/lib/db/repositories/blockArchive.js +36 -1
  57. package/lib/db/repositories/blockArchive.js.map +1 -1
  58. package/lib/metrics/metrics/lodestar.d.ts +5 -1
  59. package/lib/metrics/metrics/lodestar.d.ts.map +1 -1
  60. package/lib/metrics/metrics/lodestar.js +10 -0
  61. package/lib/metrics/metrics/lodestar.js.map +1 -1
  62. package/lib/network/gossip/encoding.d.ts +1 -2
  63. package/lib/network/gossip/encoding.d.ts.map +1 -1
  64. package/lib/network/gossip/encoding.js +5 -10
  65. package/lib/network/gossip/encoding.js.map +1 -1
  66. package/lib/network/gossip/gossipsub.d.ts.map +1 -1
  67. package/lib/network/gossip/gossipsub.js +16 -1
  68. package/lib/network/gossip/gossipsub.js.map +1 -1
  69. package/lib/network/gossip/topic.d.ts +9 -6
  70. package/lib/network/gossip/topic.d.ts.map +1 -1
  71. package/lib/network/gossip/topic.js +23 -8
  72. package/lib/network/gossip/topic.js.map +1 -1
  73. package/lib/network/processor/gossipHandlers.d.ts.map +1 -1
  74. package/lib/network/processor/gossipHandlers.js +16 -5
  75. package/lib/network/processor/gossipHandlers.js.map +1 -1
  76. package/lib/network/processor/gossipValidatorFn.d.ts +2 -0
  77. package/lib/network/processor/gossipValidatorFn.d.ts.map +1 -1
  78. package/lib/network/processor/gossipValidatorFn.js +88 -7
  79. package/lib/network/processor/gossipValidatorFn.js.map +1 -1
  80. package/lib/network/processor/index.d.ts +6 -2
  81. package/lib/network/processor/index.d.ts.map +1 -1
  82. package/lib/network/processor/index.js +62 -24
  83. package/lib/network/processor/index.js.map +1 -1
  84. package/lib/network/reqresp/protocols.d.ts.map +1 -1
  85. package/lib/network/reqresp/protocols.js +19 -5
  86. package/lib/network/reqresp/protocols.js.map +1 -1
  87. package/lib/sync/types.d.ts +21 -0
  88. package/lib/sync/types.d.ts.map +1 -1
  89. package/lib/sync/types.js +22 -0
  90. package/lib/sync/types.js.map +1 -1
  91. package/lib/sync/unknownBlock.d.ts +20 -0
  92. package/lib/sync/unknownBlock.d.ts.map +1 -1
  93. package/lib/sync/unknownBlock.js +158 -2
  94. package/lib/sync/unknownBlock.js.map +1 -1
  95. package/lib/util/sszBytes.d.ts +5 -0
  96. package/lib/util/sszBytes.d.ts.map +1 -1
  97. package/lib/util/sszBytes.js +13 -1
  98. package/lib/util/sszBytes.js.map +1 -1
  99. package/package.json +14 -14
  100. package/src/api/impl/beacon/blocks/index.ts +2 -2
  101. package/src/api/impl/validator/index.ts +9 -7
  102. package/src/chain/archiveStore/historicalState/metrics.ts +4 -0
  103. package/src/chain/archiveStore/utils/pruneHistory.ts +14 -4
  104. package/src/chain/blocks/index.ts +2 -2
  105. package/src/chain/blocks/payloadEnvelopeInput/types.ts +1 -1
  106. package/src/chain/emitter.ts +7 -2
  107. package/src/chain/errors/attesterSlashingError.ts +3 -1
  108. package/src/chain/errors/payloadAttestation.ts +2 -0
  109. package/src/chain/errors/proposerSlashingError.ts +3 -1
  110. package/src/chain/options.ts +0 -5
  111. package/src/chain/produceBlock/produceBlockBody.ts +17 -6
  112. package/src/chain/seenCache/seenPayloadEnvelopeInput.ts +3 -2
  113. package/src/chain/validation/attesterSlashing.ts +1 -2
  114. package/src/chain/validation/payloadAttestationMessage.ts +9 -0
  115. package/src/chain/validation/proposerSlashing.ts +1 -2
  116. package/src/chain/validatorMonitor.ts +12 -2
  117. package/src/db/repositories/blockArchive.ts +38 -1
  118. package/src/metrics/metrics/lodestar.ts +17 -1
  119. package/src/network/gossip/encoding.ts +5 -8
  120. package/src/network/gossip/gossipsub.ts +19 -2
  121. package/src/network/gossip/topic.ts +25 -7
  122. package/src/network/processor/gossipHandlers.ts +18 -5
  123. package/src/network/processor/gossipValidatorFn.ts +111 -7
  124. package/src/network/processor/index.ts +81 -24
  125. package/src/network/reqresp/protocols.ts +31 -6
  126. package/src/sync/types.ts +24 -3
  127. package/src/sync/unknownBlock.ts +166 -1
  128. package/src/util/sszBytes.ts +18 -0
@@ -1,6 +1,7 @@
1
1
  import {BeaconConfig} from "@lodestar/config";
2
- import {ForkName} from "@lodestar/params";
2
+ import {ForkName, isForkPostGloas} from "@lodestar/params";
3
3
  import {ContextBytesFactory, ContextBytesType, Encoding, TypeSizes} from "@lodestar/reqresp";
4
+ import {computeMaxGloasDataColumnSidecarSize} from "../../util/sszBytes.js";
4
5
  import {rateLimitQuotas} from "./rateLimit.js";
5
6
  import {ProtocolNoHandler, ReqRespMethod, Version, requestSszTypeByMethod, responseSszTypeByMethod} from "./types.js";
6
7
 
@@ -151,18 +152,42 @@ function toProtocol(protocol: ProtocolSummary) {
151
152
  encoding: Encoding.SSZ_SNAPPY,
152
153
  contextBytes: toContextBytes(protocol.contextBytesType, config),
153
154
  inboundRateLimits: rateLimitQuotas(fork, config)[protocol.method],
154
- requestSizes: requestType === null ? null : clampTypeSizes(requestType, config),
155
- responseSizes: (fork) => clampTypeSizes(responseSszTypeByMethod[protocol.method](fork, protocol.version), config),
155
+ requestSizes: requestType === null ? null : clampTypeSizes(requestType, config.MAX_PAYLOAD_SIZE),
156
+ responseSizes: (fork) =>
157
+ clampResponseTypeSizes(
158
+ responseSszTypeByMethod[protocol.method](fork, protocol.version),
159
+ protocol.method,
160
+ fork,
161
+ config
162
+ ),
156
163
  };
157
164
  };
158
165
  }
159
166
 
167
+ function clampResponseTypeSizes(
168
+ type: TypeSizes,
169
+ method: ReqRespMethod,
170
+ fork: ForkName,
171
+ config: BeaconConfig
172
+ ): TypeSizes {
173
+ let maxPayloadSize = config.MAX_PAYLOAD_SIZE;
174
+ if (isForkPostGloas(fork)) {
175
+ switch (method) {
176
+ case ReqRespMethod.DataColumnSidecarsByRange:
177
+ case ReqRespMethod.DataColumnSidecarsByRoot:
178
+ maxPayloadSize = Math.min(maxPayloadSize, computeMaxGloasDataColumnSidecarSize(config));
179
+ break;
180
+ }
181
+ }
182
+ return clampTypeSizes(type, maxPayloadSize);
183
+ }
184
+
160
185
  /**
161
- * Length-prefix must be within the SSZ type bounds or MAX_PAYLOAD_SIZE, whichever is smaller
186
+ * Length-prefix must be within the SSZ type bounds or the configured payload limit, whichever is smaller.
162
187
  * https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.12/specs/phase0/p2p-interface.md#encoding-strategies
163
188
  */
164
- function clampTypeSizes(type: TypeSizes, config: BeaconConfig): TypeSizes {
165
- return {minSize: type.minSize, maxSize: Math.min(type.maxSize, config.MAX_PAYLOAD_SIZE)};
189
+ function clampTypeSizes(type: TypeSizes, maxPayloadSize: number): TypeSizes {
190
+ return {minSize: type.minSize, maxSize: Math.min(type.maxSize, maxPayloadSize)};
166
191
  }
167
192
 
168
193
  function toContextBytes(type: ContextBytesType, config: BeaconConfig): ContextBytesFactory {
package/src/sync/types.ts CHANGED
@@ -23,6 +23,14 @@ export enum PendingBlockType {
23
23
  * Payload analog of UNKNOWN_BLOCK_ROOT: we have a beacon block root but not its execution payload envelope.
24
24
  */
25
25
  UNKNOWN_PAYLOAD_BLOCK_ROOT = "unknown_payload_block_root",
26
+ /**
27
+ * Same as UNKNOWN_PAYLOAD_BLOCK_ROOT, but the trigger also carries the payload's slot
28
+ */
29
+ UNKNOWN_PAYLOAD_BLOCK_ROOT_SLOT = "unknown_payload_block_root_slot",
30
+ /**
31
+ * Same as UNKNOWN_PAYLOAD_BLOCK_ROOT_SLOT for the current slot
32
+ */
33
+ DEFERRED_PAYLOAD_BLOCK_ROOT = "deferred_payload_block_root",
26
34
  /**
27
35
  * Payload analog of INCOMPLETE_BLOCK_INPUT: we have a partial payload input that did not complete in time.
28
36
  */
@@ -49,6 +57,20 @@ export enum FetchResult {
49
57
  FailureMaxAttempts = "failure_max_attempts",
50
58
  }
51
59
 
60
+ /**
61
+ * Outcome of a slot's deferred optimistic payload searches, recorded once when its poll loop ends.
62
+ */
63
+ export enum DeferredPayloadResult {
64
+ /** gossip delivered every watched payload before PAYLOAD_DUE, no search was needed */
65
+ ImportedBeforeSearch = "imported_before_search",
66
+ /** at least one was missing at PAYLOAD_DUE, we searched, and all were imported afterwards */
67
+ ImportedAfterSearch = "imported_after_search",
68
+ /** something was still not in fork choice when the slot ended */
69
+ Unresolved = "unresolved",
70
+ /** the slot had no optimistic search and no payload import at all, eg a skipped slot */
71
+ ResolvedNoRoot = "resolved_no_root",
72
+ }
73
+
52
74
  export enum PendingBlockInputStatus {
53
75
  pending = "pending",
54
76
  fetching = "fetching",
@@ -130,9 +152,8 @@ export type PendingPayloadInput = {
130
152
  export type PendingPayloadRootHex = {
131
153
  status: PendingPayloadInputStatus.pending | PendingPayloadInputStatus.fetching;
132
154
  rootHex: RootHex;
133
- // Trusted slot only (fork choice / validated data), may be missing until resolved. NOT the gossip
134
- // message slot from ChainEvent.unknownEnvelopeBlockRoot, which is untrusted and not necessarily the
135
- // payload/block slot. See BlockInputSync.resolvePayloadSlot.
155
+ // slot could be via fork choice or ChainEvent.unknownEnvelopeBlockRootSlot
156
+ // while ChainEvent.unknownEnvelopeBlockRoot does not provide one
136
157
  slot?: Slot;
137
158
  timeAddedSec: number;
138
159
  timeSyncedSec?: number;
@@ -5,7 +5,7 @@ import {ForkSeq, SLOTS_PER_EPOCH, isForkPostGloas} from "@lodestar/params";
5
5
  import {RequestError, RequestErrorCode} from "@lodestar/reqresp";
6
6
  import {computeTimeAtSlot} from "@lodestar/state-transition";
7
7
  import {RootHex, Slot, gloas} from "@lodestar/types";
8
- import {Logger, fromHex, prettyPrintIndices, pruneSetToMax, sleep, toRootHex} from "@lodestar/utils";
8
+ import {Logger, fromHex, isErrorAborted, prettyPrintIndices, pruneSetToMax, sleep, toRootHex} from "@lodestar/utils";
9
9
  import {isBlockInputBlobs, isBlockInputColumns} from "../chain/blocks/blockInput/blockInput.js";
10
10
  import {BlockInputSource, IBlockInput} from "../chain/blocks/blockInput/types.js";
11
11
  import {PayloadError, PayloadErrorCode} from "../chain/blocks/importExecutionPayload.js";
@@ -18,6 +18,7 @@ import {Metrics} from "../metrics/index.js";
18
18
  import {INetwork, NetworkEvent, NetworkEventData, prettyPrintPeerIdStr} from "../network/index.js";
19
19
  import {PeerSyncMeta} from "../network/peers/peersData.js";
20
20
  import {MAX_PEERS_PER_ROOT} from "../network/processor/constants.js";
21
+ import {ClockEvent} from "../util/clock.js";
21
22
  import {PeerIdStr} from "../util/peerId.js";
22
23
  import {shuffle} from "../util/shuffle.js";
23
24
  import {sortBy} from "../util/sortBy.js";
@@ -26,6 +27,7 @@ import {MAX_CONCURRENT_REQUESTS} from "./constants.js";
26
27
  import {SyncOptions} from "./options.js";
27
28
  import {
28
29
  BlockInputSyncCacheItem,
30
+ DeferredPayloadResult,
29
31
  DownloadResult,
30
32
  DroppedItemReason,
31
33
  FetchResult,
@@ -57,6 +59,11 @@ const MAX_PENDING_BLOCKS = 100;
57
59
  */
58
60
  const PRUNE_UNRESOLVED_SLOT_EPOCHS = 2;
59
61
 
62
+ /**
63
+ * Poll pending payloads of the current slot this often, between PAYLOAD_DUE and the end of the slot.
64
+ */
65
+ export const PAYLOAD_POLL_INTERVAL_MS = 1_000;
66
+
60
67
  type AdvancePendingBlockResult =
61
68
  | "ready"
62
69
  | "queued_block"
@@ -116,6 +123,11 @@ export class BlockInputSync {
116
123
  private subscribedToNetworkEvents = false;
117
124
  private peerBalancer: UnknownBlockPeerBalancer;
118
125
  private rateLimitBackoffTimeout: NodeJS.Timeout | undefined;
126
+ /**
127
+ * Search unknown payload starting at PAYLOAD_DUE_BPS.
128
+ */
129
+ private readonly deferredPayloadRoots = new Map<RootHex, {slot: Slot; source: BlockInputSource; peer?: PeerIdStr}>();
130
+ private payloadPollAbortController: AbortController | undefined;
119
131
 
120
132
  constructor(
121
133
  private readonly config: ChainForkConfig,
@@ -152,6 +164,7 @@ export class BlockInputSync {
152
164
  this.logger.verbose("BlockInputSync enabled.");
153
165
  this.chain.emitter.on(ChainEvent.unknownBlockRoot, this.onUnknownBlockRoot);
154
166
  this.chain.emitter.on(ChainEvent.unknownEnvelopeBlockRoot, this.onUnknownEnvelopeBlockRoot);
167
+ this.chain.emitter.on(ChainEvent.unknownEnvelopeBlockRootSlot, this.onUnknownEnvelopeBlockRootSlot);
155
168
  this.chain.emitter.on(ChainEvent.incompleteBlockInput, this.onIncompleteBlockInput);
156
169
  this.chain.emitter.on(ChainEvent.incompletePayloadEnvelope, this.onIncompletePayloadEnvelope);
157
170
  this.chain.emitter.on(ChainEvent.blockUnknownParent, this.onUnknownParent);
@@ -160,6 +173,7 @@ export class BlockInputSync {
160
173
  this.chain.emitter.on(routes.events.EventType.executionPayload, this.onPayloadImported);
161
174
  this.network.events.on(NetworkEvent.peerConnected, this.onPeerConnected);
162
175
  this.network.events.on(NetworkEvent.peerDisconnected, this.onPeerDisconnected);
176
+ this.chain.clock.on(ClockEvent.slot, this.startPayloadPollAtSlot);
163
177
 
164
178
  // Seed the balancer with peers that connected before we subscribed
165
179
  for (const peerId of this.network.getConnectedPeers()) {
@@ -171,14 +185,22 @@ export class BlockInputSync {
171
185
  }
172
186
 
173
187
  this.subscribedToNetworkEvents = true;
188
+ // we subscribe mid-slot, and a root can be deferred at any point in the slot, so start this
189
+ // slot's loop now instead of waiting for the next ClockEvent.slot
190
+ this.startPayloadPollAtSlot(this.chain.clock.currentSlot);
174
191
  }
175
192
  }
176
193
 
177
194
  unsubscribeFromNetwork(): void {
178
195
  this.logger.verbose("BlockInputSync disabled.");
179
196
  this.clearRateLimitBackoffTimer();
197
+ this.chain.clock.off(ClockEvent.slot, this.startPayloadPollAtSlot);
198
+ this.payloadPollAbortController?.abort();
199
+ this.payloadPollAbortController = undefined;
200
+ this.deferredPayloadRoots.clear();
180
201
  this.chain.emitter.off(ChainEvent.unknownBlockRoot, this.onUnknownBlockRoot);
181
202
  this.chain.emitter.off(ChainEvent.unknownEnvelopeBlockRoot, this.onUnknownEnvelopeBlockRoot);
203
+ this.chain.emitter.off(ChainEvent.unknownEnvelopeBlockRootSlot, this.onUnknownEnvelopeBlockRootSlot);
182
204
  this.chain.emitter.off(ChainEvent.incompleteBlockInput, this.onIncompleteBlockInput);
183
205
  this.chain.emitter.off(ChainEvent.incompletePayloadEnvelope, this.onIncompletePayloadEnvelope);
184
206
  this.chain.emitter.off(ChainEvent.blockUnknownParent, this.onUnknownParent);
@@ -242,6 +264,33 @@ export class BlockInputSync {
242
264
  }
243
265
  };
244
266
 
267
+ private onUnknownEnvelopeBlockRootSlot = (data: ChainEventData[ChainEvent.unknownEnvelopeBlockRootSlot]): void => {
268
+ try {
269
+ if (data.slot === this.chain.clock.currentSlot) {
270
+ // we receive the optimistic search, the poll loop searches it at PAYLOAD_DUE or on its next
271
+ // tick. If that loop already returned - every root it watched was imported - this root waits
272
+ // for the next slot, where startPayloadPollAtSlot moves it to pendingPayloads. That is not the normal
273
+ // scenario, and in a forking condition it only delays the search of the extra root
274
+ const deferred = this.deferredPayloadRoots.get(data.rootHex);
275
+ if (deferred === undefined) {
276
+ this.deferredPayloadRoots.set(data.rootHex, {slot: data.slot, source: data.source, peer: data.peer});
277
+ } else if (deferred.peer === undefined) {
278
+ deferred.peer = data.peer;
279
+ }
280
+ return;
281
+ }
282
+
283
+ const isNewRoot = this.addByPayloadRootHex(data.rootHex, data.peer, data.slot);
284
+ if (isNewRoot) {
285
+ this.triggerUnknownBlockSearch();
286
+ this.metrics?.blockInputSync.requests.inc({type: PendingBlockType.UNKNOWN_PAYLOAD_BLOCK_ROOT_SLOT});
287
+ this.metrics?.blockInputSync.payloadSource.inc({source: data.source});
288
+ }
289
+ } catch (e) {
290
+ this.logger.debug("Error handling unknownEnvelopeBlockRootSlot event", {}, e as Error);
291
+ }
292
+ };
293
+
245
294
  private onIncompletePayloadEnvelope = (data: ChainEventData[ChainEvent.incompletePayloadEnvelope]): void => {
246
295
  try {
247
296
  this.addByPayloadInput(data.payloadInput, data.peer);
@@ -785,6 +834,122 @@ export class BlockInputSync {
785
834
  }
786
835
  }
787
836
 
837
+ /**
838
+ * Start this slot's payload poll: end the previous slot's loop, move roots it never got to
839
+ * into pendingPayloads, then poll from PAYLOAD_DUE to the end of the slot.
840
+ */
841
+ private startPayloadPollAtSlot = (slot: Slot): void => {
842
+ if (!isForkPostGloas(this.config.getForkName(slot))) {
843
+ return;
844
+ }
845
+ // end the previous slot's loop, wherever it is: its sleep is bound to this same controller
846
+ this.payloadPollAbortController?.abort();
847
+ this.payloadPollAbortController = new AbortController();
848
+ // entries from an earlier slot is not polled
849
+ for (const [rootHex, {slot: deferredSlot, source, peer}] of this.deferredPayloadRoots) {
850
+ if (deferredSlot < slot) {
851
+ if (!this.chain.forkChoice.hasPayloadHexUnsafe(rootHex)) {
852
+ this.addDeferredPayloadRoot(rootHex, deferredSlot, source, peer);
853
+ }
854
+ this.deferredPayloadRoots.delete(rootHex);
855
+ }
856
+ }
857
+ this.pollPayloadsAtSlot(slot, this.payloadPollAbortController.signal).catch((e) => {
858
+ if (!isErrorAborted(e)) {
859
+ this.logger.debug("Error polling payloads", {slot}, e as Error);
860
+ }
861
+ });
862
+ };
863
+
864
+ /**
865
+ * Poll unknown payloads of the current slot.
866
+ */
867
+ private async pollPayloadsAtSlot(slot: Slot, signal: AbortSignal): Promise<void> {
868
+ // fixed tick count so the loop cannot outlive its slot: for mainnet, it's 6 ticks
869
+ const tickCount = Math.floor(
870
+ (this.config.SLOT_DURATION_MS - this.config.getPayloadDueMs()) / PAYLOAD_POLL_INTERVAL_MS
871
+ );
872
+ // a root still missing on several ticks is searched on each of them, recorded once
873
+ const searchedRoots = new Set<RootHex>();
874
+ let polls = 0;
875
+
876
+ try {
877
+ await sleep(Math.max(0, this.config.getPayloadDueMs() - this.chain.clock.msFromSlot(slot)), signal);
878
+
879
+ for (polls = 0; polls < tickCount; polls++) {
880
+ let importedCount = 0;
881
+ for (const [rootHex, {slot: deferredSlot, source, peer}] of this.deferredPayloadRoots) {
882
+ if (this.chain.forkChoice.hasPayloadHexUnsafe(rootHex)) {
883
+ importedCount++;
884
+ continue;
885
+ }
886
+ // copy to the regular pendingPayloads so that we can search
887
+ this.addDeferredPayloadRoot(rootHex, deferredSlot, source, peer);
888
+ searchedRoots.add(rootHex);
889
+ }
890
+
891
+ // an empty deferredPayloadRoots means there is no optimistic search comes before PAYLOAD_DUE
892
+ // we have to poll until the payload of current slot is imported or slot end
893
+ if (this.deferredPayloadRoots.size > 0 && importedCount === this.deferredPayloadRoots.size) {
894
+ if (searchedRoots.size > 0) {
895
+ // the backup search was needed
896
+ this.metrics?.blockInputSync.deferredPayloadResult.inc({
897
+ result: DeferredPayloadResult.ImportedAfterSearch,
898
+ });
899
+ this.metrics?.blockInputSync.deferredPayloadPolls.observe(polls);
900
+ this.logger.verbose("Payload imported after unknown sync search", {
901
+ slot,
902
+ roots: Array.from(searchedRoots).join(","),
903
+ polls,
904
+ });
905
+ } else {
906
+ // this happens most of the time on devnet
907
+ // gossip delivered everything before PAYLOAD_DUE
908
+ this.metrics?.blockInputSync.deferredPayloadResult.inc({
909
+ result: DeferredPayloadResult.ImportedBeforeSearch,
910
+ });
911
+ }
912
+ return;
913
+ }
914
+
915
+ this.triggerUnknownBlockSearch();
916
+ if (polls < tickCount - 1) {
917
+ // no sleep after the last tick: the loop would otherwise end right at the slot boundary,
918
+ // where startPayloadPollAtSlot aborts it
919
+ await sleep(PAYLOAD_POLL_INTERVAL_MS, signal);
920
+ }
921
+ }
922
+
923
+ // reaching here means the early return never fired
924
+ if (this.deferredPayloadRoots.size > 0) {
925
+ this.metrics?.blockInputSync.deferredPayloadResult.inc({result: DeferredPayloadResult.Unresolved});
926
+ this.logger.debug("Deferred payloads still missing at end of slot", {
927
+ slot,
928
+ roots: Array.from(this.deferredPayloadRoots.keys()).join(","),
929
+ });
930
+ } else {
931
+ // no optimistic search and no payload import ended the loop, eg a skipped slot
932
+ this.metrics?.blockInputSync.deferredPayloadResult.inc({result: DeferredPayloadResult.ResolvedNoRoot});
933
+ this.logger.debug("No deferred payloads at slot", {
934
+ slot,
935
+ });
936
+ }
937
+ } catch (e) {
938
+ if (!isErrorAborted(e)) {
939
+ throw e;
940
+ }
941
+ this.logger.verbose("Payload poll aborted", {slot, polls, msIntoSlot: this.chain.clock.msFromSlot(slot)});
942
+ }
943
+ }
944
+
945
+ private addDeferredPayloadRoot(rootHex: RootHex, slot: Slot, source: BlockInputSource, peer?: PeerIdStr): void {
946
+ const isNewRoot = this.addByPayloadRootHex(rootHex, peer, slot);
947
+ if (isNewRoot) {
948
+ this.metrics?.blockInputSync.requests.inc({type: PendingBlockType.DEFERRED_PAYLOAD_BLOCK_ROOT});
949
+ this.metrics?.blockInputSync.payloadSource.inc({source});
950
+ }
951
+ }
952
+
788
953
  private async downloadBlock(block: BlockInputSyncCacheItem): Promise<void> {
789
954
  if (block.status !== PendingBlockInputStatus.pending) {
790
955
  return;
@@ -1,6 +1,7 @@
1
1
  import {BitArray, deserializeUint8ArrayBitListFromBytes} from "@chainsafe/ssz";
2
2
  import {ChainForkConfig} from "@lodestar/config";
3
3
  import {
4
+ BYTES_PER_CELL,
4
5
  BYTES_PER_FIELD_ELEMENT,
5
6
  FIELD_ELEMENTS_PER_BLOB,
6
7
  ForkName,
@@ -551,6 +552,23 @@ export function getBeaconBlockRootFromDataColumnSidecarSerialized(data: Uint8Arr
551
552
  return "0x" + blockRootBuf.toString("hex");
552
553
  }
553
554
 
555
+ // beaconBlockRoot is the last fixed field in the Gloas layout
556
+ const DATA_COLUMN_SIDECAR_GLOAS_FIXED_SIZE = BEACON_BLOCK_ROOT_POSITION_IN_GLOAS_DATA_COLUMN_SIDECAR + ROOT_SIZE; // 56 bytes
557
+ const KZG_PROOF_SIZE = 48; // Bytes48
558
+ const DATA_COLUMN_SIDECAR_GLOAS_PER_BLOB_SIZE = BYTES_PER_CELL + KZG_PROOF_SIZE; // 2048 + 48 = 2096 bytes
559
+
560
+ /**
561
+ * Gloas sidecar size bound uses the largest blob limit across the entire schedule, including future entries.
562
+ * https://github.com/ethereum/consensus-specs/pull/5613
563
+ */
564
+ export function computeMaxGloasDataColumnSidecarSize(config: ChainForkConfig): number {
565
+ const maxBlobs = config.BLOB_SCHEDULE.reduce(
566
+ (max, entry) => Math.max(max, entry.MAX_BLOBS_PER_BLOCK),
567
+ config.MAX_BLOBS_PER_BLOCK_ELECTRA
568
+ );
569
+ return DATA_COLUMN_SIDECAR_GLOAS_FIXED_SIZE + maxBlobs * DATA_COLUMN_SIDECAR_GLOAS_PER_BLOB_SIZE;
570
+ }
571
+
554
572
  /**
555
573
  * SignedExecutionPayloadEnvelope SSZ Layout:
556
574
  * ├─ 4 bytes: message offset (points to byte 100)