@lodestar/beacon-node 1.49.0-dev.b27f498c2c → 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 (57) hide show
  1. package/lib/api/impl/validator/index.d.ts.map +1 -1
  2. package/lib/api/impl/validator/index.js +3 -1
  3. package/lib/api/impl/validator/index.js.map +1 -1
  4. package/lib/chain/archiveStore/historicalState/metrics.d.ts.map +1 -1
  5. package/lib/chain/archiveStore/historicalState/metrics.js +4 -0
  6. package/lib/chain/archiveStore/historicalState/metrics.js.map +1 -1
  7. package/lib/chain/archiveStore/utils/pruneHistory.d.ts.map +1 -1
  8. package/lib/chain/archiveStore/utils/pruneHistory.js +5 -1
  9. package/lib/chain/archiveStore/utils/pruneHistory.js.map +1 -1
  10. package/lib/chain/emitter.d.ts +10 -0
  11. package/lib/chain/emitter.d.ts.map +1 -1
  12. package/lib/chain/emitter.js +4 -0
  13. package/lib/chain/emitter.js.map +1 -1
  14. package/lib/chain/errors/payloadAttestation.d.ts +4 -0
  15. package/lib/chain/errors/payloadAttestation.d.ts.map +1 -1
  16. package/lib/chain/errors/payloadAttestation.js +1 -0
  17. package/lib/chain/errors/payloadAttestation.js.map +1 -1
  18. package/lib/chain/options.d.ts +0 -4
  19. package/lib/chain/options.d.ts.map +1 -1
  20. package/lib/chain/options.js +0 -1
  21. package/lib/chain/options.js.map +1 -1
  22. package/lib/chain/validation/payloadAttestationMessage.d.ts.map +1 -1
  23. package/lib/chain/validation/payloadAttestationMessage.js +8 -1
  24. package/lib/chain/validation/payloadAttestationMessage.js.map +1 -1
  25. package/lib/db/repositories/blockArchive.d.ts +5 -0
  26. package/lib/db/repositories/blockArchive.d.ts.map +1 -1
  27. package/lib/db/repositories/blockArchive.js +36 -1
  28. package/lib/db/repositories/blockArchive.js.map +1 -1
  29. package/lib/metrics/metrics/lodestar.d.ts +5 -1
  30. package/lib/metrics/metrics/lodestar.d.ts.map +1 -1
  31. package/lib/metrics/metrics/lodestar.js +10 -0
  32. package/lib/metrics/metrics/lodestar.js.map +1 -1
  33. package/lib/network/processor/index.d.ts +6 -2
  34. package/lib/network/processor/index.d.ts.map +1 -1
  35. package/lib/network/processor/index.js +62 -24
  36. package/lib/network/processor/index.js.map +1 -1
  37. package/lib/sync/types.d.ts +21 -0
  38. package/lib/sync/types.d.ts.map +1 -1
  39. package/lib/sync/types.js +22 -0
  40. package/lib/sync/types.js.map +1 -1
  41. package/lib/sync/unknownBlock.d.ts +20 -0
  42. package/lib/sync/unknownBlock.d.ts.map +1 -1
  43. package/lib/sync/unknownBlock.js +158 -2
  44. package/lib/sync/unknownBlock.js.map +1 -1
  45. package/package.json +14 -14
  46. package/src/api/impl/validator/index.ts +3 -1
  47. package/src/chain/archiveStore/historicalState/metrics.ts +4 -0
  48. package/src/chain/archiveStore/utils/pruneHistory.ts +5 -2
  49. package/src/chain/emitter.ts +7 -2
  50. package/src/chain/errors/payloadAttestation.ts +2 -0
  51. package/src/chain/options.ts +0 -5
  52. package/src/chain/validation/payloadAttestationMessage.ts +9 -0
  53. package/src/db/repositories/blockArchive.ts +38 -1
  54. package/src/metrics/metrics/lodestar.ts +17 -1
  55. package/src/network/processor/index.ts +81 -24
  56. package/src/sync/types.ts +24 -3
  57. package/src/sync/unknownBlock.ts +166 -1
@@ -1,7 +1,7 @@
1
1
  import {ChainConfig} from "@lodestar/config";
2
2
  import {computeStartSlotAtEpoch} from "@lodestar/state-transition";
3
3
  import {Epoch} from "@lodestar/types";
4
- import {Logger} from "@lodestar/utils";
4
+ import {Logger, prettyPrintIndices} from "@lodestar/utils";
5
5
  import {IBeaconDb} from "../../../db/interface.js";
6
6
  import {Metrics} from "../../../metrics/index.js";
7
7
 
@@ -44,12 +44,15 @@ export async function pruneHistory(
44
44
  blocksToPrune: blocks.length,
45
45
  envelopesToPrune: envelopes.length,
46
46
  statesToPrune: states.length,
47
+ blockSlots: prettyPrintIndices(blocks),
48
+ envelopeSlots: prettyPrintIndices(envelopes),
49
+ stateSlots: prettyPrintIndices(states),
47
50
  });
48
51
 
49
52
  const step1 = metrics?.pruneHistory.pruneKeys.startTimer();
50
53
  await Promise.all([
51
54
  // ->
52
- db.blockArchive.batchDelete(blocks),
55
+ db.blockArchive.batchDeleteRange(blocks),
53
56
  db.executionPayloadEnvelopeArchive.batchDelete(envelopes),
54
57
  db.stateArchive.batchDelete(states),
55
58
  ]);
@@ -72,6 +72,10 @@ export enum ChainEvent {
72
72
  * Trigger BlockInputSync to find a SignedExecutionPayloadEnvelope with specified block root.
73
73
  */
74
74
  unknownEnvelopeBlockRoot = "unknownEnvelopeBlockRoot",
75
+ /**
76
+ * Same as unknownEnvelopeBlockRoot, but with slot
77
+ */
78
+ unknownEnvelopeBlockRootSlot = "unknownEnvelopeBlockRootSlot",
75
79
  /**
76
80
  * Trigger BlockInputSync for blocks that are partially received via gossip but are not complete by time the
77
81
  * cut-off window passes for waiting on gossip
@@ -99,8 +103,8 @@ export type ChainEventData = {
99
103
  peer: PeerIdStr;
100
104
  source: BlockInputSource;
101
105
  };
102
- // slot is the message slot, not necessarily the envelope's slot, but useful as a logging/prune hint
103
- [ChainEvent.unknownEnvelopeBlockRoot]: {rootHex: RootHex; slot: Slot; peer?: PeerIdStr; source: BlockInputSource};
106
+ [ChainEvent.unknownEnvelopeBlockRoot]: {rootHex: RootHex; peer?: PeerIdStr; source: BlockInputSource};
107
+ [ChainEvent.unknownEnvelopeBlockRootSlot]: {rootHex: RootHex; slot: Slot; peer?: PeerIdStr; source: BlockInputSource};
104
108
  };
105
109
 
106
110
  export type IChainEvents = ApiEvents & {
@@ -126,6 +130,7 @@ export type IChainEvents = ApiEvents & {
126
130
  [ChainEvent.incompleteBlockInput]: (data: ChainEventData[ChainEvent.incompleteBlockInput]) => void;
127
131
  [ChainEvent.incompletePayloadEnvelope]: (data: ChainEventData[ChainEvent.incompletePayloadEnvelope]) => void;
128
132
  [ChainEvent.unknownEnvelopeBlockRoot]: (data: ChainEventData[ChainEvent.unknownEnvelopeBlockRoot]) => void;
133
+ [ChainEvent.unknownEnvelopeBlockRootSlot]: (data: ChainEventData[ChainEvent.unknownEnvelopeBlockRootSlot]) => void;
129
134
  };
130
135
 
131
136
  /**
@@ -6,6 +6,7 @@ export enum PayloadAttestationErrorCode {
6
6
  PAYLOAD_ATTESTATION_ALREADY_KNOWN = "PAYLOAD_ATTESTATION_ERROR_PAYLOAD_ATTESTATION_ALREADY_KNOWN",
7
7
  UNKNOWN_BLOCK_ROOT = "PAYLOAD_ATTESTATION_ERROR_UNKNOWN_BLOCK_ROOT",
8
8
  INVALID_BLOCK_SLOT = "PAYLOAD_ATTESTATION_ERROR_INVALID_BLOCK_SLOT",
9
+ PRE_GLOAS_SLOT = "PAYLOAD_ATTESTATION_ERROR_PRE_GLOAS_SLOT",
9
10
  INVALID_BLOCK = "PAYLOAD_ATTESTATION_ERROR_INVALID_BLOCK",
10
11
  INVALID_ATTESTER = "PAYLOAD_ATTESTATION_ERROR_INVALID_ATTESTER",
11
12
  INVALID_SIGNATURE = "PAYLOAD_ATTESTATION_ERROR_INVALID_SIGNATURE",
@@ -20,6 +21,7 @@ export type PayloadAttestationErrorType =
20
21
  }
21
22
  | {code: PayloadAttestationErrorCode.UNKNOWN_BLOCK_ROOT; blockRoot: RootHex}
22
23
  | {code: PayloadAttestationErrorCode.INVALID_BLOCK_SLOT; blockRoot: RootHex; blockSlot: Slot; slot: Slot}
24
+ | {code: PayloadAttestationErrorCode.PRE_GLOAS_SLOT; slot: Slot}
23
25
  | {code: PayloadAttestationErrorCode.INVALID_BLOCK; blockRoot: RootHex}
24
26
  | {code: PayloadAttestationErrorCode.INVALID_ATTESTER; attesterIndex: ValidatorIndex}
25
27
  | {code: PayloadAttestationErrorCode.INVALID_SIGNATURE};
@@ -63,10 +63,6 @@ export type BlockProcessOpts = {
63
63
  * Will double processing times. Use only for debugging purposes.
64
64
  */
65
65
  disableBlsBatchVerify?: boolean;
66
- /**
67
- * Assert progressive balances the same to EpochTransitionCache
68
- */
69
- assertCorrectProgressiveBalances?: boolean;
70
66
  /** Used for fork_choice spec tests */
71
67
  disableOnBlockError?: boolean;
72
68
  /** Used for fork_choice spec tests */
@@ -114,7 +110,6 @@ export const defaultChainOptions: IChainOptions = {
114
110
  suggestedFeeRecipient: defaultValidatorOptions.suggestedFeeRecipient,
115
111
  graffitiAppend: true,
116
112
  serveHistoricalState: false,
117
- assertCorrectProgressiveBalances: false,
118
113
  archiveStateEpochFrequency: 1024,
119
114
  archiveMode: DEFAULT_ARCHIVE_MODE,
120
115
  pruneHistory: false,
@@ -1,4 +1,5 @@
1
1
  import {
2
+ computeEpochAtSlot,
2
3
  createIndexedSignatureSetFromComponents,
3
4
  getPayloadAttestationDataSigningRoot,
4
5
  isStatePostGloas,
@@ -35,6 +36,14 @@ async function validatePayloadAttestationMessage(
35
36
  ): Promise<PayloadAttestationValidationResult> {
36
37
  const {data, validatorIndex} = payloadAttestationMessage;
37
38
 
39
+ // [REJECT] The payload attestation slot is at or after the Gloas fork.
40
+ if (computeEpochAtSlot(data.slot) < chain.config.GLOAS_FORK_EPOCH) {
41
+ throw new PayloadAttestationError(GossipAction.REJECT, {
42
+ code: PayloadAttestationErrorCode.PRE_GLOAS_SLOT,
43
+ slot: data.slot,
44
+ });
45
+ }
46
+
38
47
  // [IGNORE] The message's slot is for the current slot (with a `MAXIMUM_GOSSIP_CLOCK_DISPARITY` allowance), i.e. `data.slot == current_slot`.
39
48
  if (!chain.clock.isCurrentSlotGivenGossipDisparity(data.slot)) {
40
49
  throw new PayloadAttestationError(GossipAction.IGNORE, {
@@ -1,8 +1,9 @@
1
1
  import {ChainForkConfig} from "@lodestar/config";
2
2
  import {Db, DbBatch, FilterOptions, KeyValue, Repository} from "@lodestar/db";
3
3
  import {Root, SignedBeaconBlock, Slot, ssz} from "@lodestar/types";
4
- import {bytesToInt, intToBytes} from "@lodestar/utils";
4
+ import {bytesToInt, fromHex, intToBytes} from "@lodestar/utils";
5
5
  import {getSignedBlockTypeFromBytes} from "../../util/multifork.js";
6
+ import {getParentRootFromSignedBeaconBlockSerialized} from "../../util/sszBytes.js";
6
7
  import {Bucket, getBucketNameByValue} from "../buckets.js";
7
8
  import {
8
9
  deleteParentRootIndex,
@@ -24,6 +25,10 @@ export type BlockArchiveBatchPutBinaryItem = KeyValue<Slot, Uint8Array> & {
24
25
  parentRoot: Root;
25
26
  };
26
27
 
28
+ // The initial prune can span millions of slots. This bounds the slot index reads in flight and keeps each
29
+ // delete batch at a few thousand keys, both tiny next to the block value batches in archiveBlocks
30
+ const DELETE_RANGE_CHUNK_SIZE = 1000;
31
+
27
32
  /**
28
33
  * Stores finalized blocks. Block slot is identifier.
29
34
  */
@@ -103,6 +108,38 @@ export class BlockArchiveRepository extends Repository<Slot, SignedBeaconBlock>
103
108
  );
104
109
  }
105
110
 
111
+ /**
112
+ * Delete a contiguous range of archived blocks together with their index entries.
113
+ * `slots` must be every archived slot in the range, so the parent of each block is the entry before it.
114
+ */
115
+ async batchDeleteRange(slots: Slot[]): Promise<void> {
116
+ const sorted = [...slots].sort((a, b) => a - b);
117
+ let prevRoot: Root | null = null;
118
+
119
+ for (let i = 0; i < sorted.length; i += DELETE_RANGE_CHUNK_SIZE) {
120
+ const chunk = sorted.slice(i, i + DELETE_RANGE_CHUNK_SIZE);
121
+ const roots = await Promise.all(chunk.map((slot) => this.getRootBySlot(slot)));
122
+
123
+ const keys: Uint8Array[] = [];
124
+ for (let j = 0; j < chunk.length; j++) {
125
+ keys.push(this.encodeKey(chunk[j]), getSlotIndexKey(chunk[j]));
126
+ const root = roots[j];
127
+ if (root) keys.push(getRootIndexKey(root));
128
+ // The parent index entry pointing at this block is keyed by its parent root, which is the
129
+ // previous block's root unless that block is unindexed, then it is read from the block itself
130
+ let parentRoot = prevRoot;
131
+ if (parentRoot === null) {
132
+ const block = await this.getBinary(chunk[j]);
133
+ const parentRootHex = block ? getParentRootFromSignedBeaconBlockSerialized(block) : null;
134
+ parentRoot = parentRootHex ? fromHex(parentRootHex) : null;
135
+ }
136
+ if (parentRoot) keys.push(getParentRootIndexKey(parentRoot));
137
+ prevRoot = root;
138
+ }
139
+ await this.db.batchDelete(keys, this.dbReqOpts);
140
+ }
141
+ }
142
+
106
143
  async batch(operations: DbBatch<Slot, SignedBeaconBlock>): Promise<void> {
107
144
  await this.batchBinary(
108
145
  operations.map((operation) =>
@@ -26,7 +26,13 @@ import {ExecutionPayloadStatus} from "../../execution/index.js";
26
26
  import {GossipType} from "../../network/index.js";
27
27
  import {CannotAcceptWorkReason, ReprocessRejectReason} from "../../network/processor/index.js";
28
28
  import {BackfillSyncMethod} from "../../sync/backfill/backfill.js";
29
- import {DownloadResult, DroppedItemReason, FetchResult, PendingBlockType} from "../../sync/types.js";
29
+ import {
30
+ DeferredPayloadResult,
31
+ DownloadResult,
32
+ DroppedItemReason,
33
+ FetchResult,
34
+ PendingBlockType,
35
+ } from "../../sync/types.js";
30
36
  import {PeerSyncType, RangeSyncType} from "../../sync/utils/remoteSyncType.js";
31
37
  import {AllocSource} from "../../util/bufferPool.js";
32
38
  import {DataColumnReconstructionCode} from "../../util/dataColumns.js";
@@ -655,6 +661,16 @@ export function createLodestarMetrics(
655
661
  help: "Count of payload (execution payload envelope) sync triggers, labeled by their source",
656
662
  labelNames: ["source"],
657
663
  }),
664
+ deferredPayloadResult: register.counter<{result: DeferredPayloadResult}>({
665
+ name: "lodestar_sync_deferred_payload_result_total",
666
+ help: "Outcome of a slot's deferred optimistic payload searches, by result",
667
+ labelNames: ["result"],
668
+ }),
669
+ deferredPayloadPolls: register.histogram({
670
+ name: "lodestar_sync_deferred_payload_polls_count",
671
+ help: "Poll ticks after PAYLOAD_DUE until a searched payload was imported",
672
+ buckets: [0, 1, 2, 3, 6],
673
+ }),
658
674
  pendingBlocks: register.gauge({
659
675
  name: "lodestar_sync_unknown_block_pending_blocks_size",
660
676
  help: "Current size of UnknownBlockSync pending blocks cache",
@@ -86,8 +86,13 @@ export const MAX_AWAITING_MESSAGES_PER_ROOT: Partial<Record<GossipType, number>>
86
86
 
87
87
  /**
88
88
  * Track the forwarded peers we already emit to UnknownBlockInput sync.
89
+ * envelopeSlotEmitted: whether the unknownEnvelopeBlockRootSlot event was already emitted for this root.
89
90
  */
90
- type SearchedRootEntry = {blockPeerIds?: Set<PeerIdStr>; envelopePeerIds?: Set<PeerIdStr>};
91
+ type SearchedRootEntry = {
92
+ blockPeerIds?: Set<PeerIdStr>;
93
+ envelopePeerIds?: Set<PeerIdStr>;
94
+ envelopeSlotEmitted?: boolean;
95
+ };
91
96
 
92
97
  /**
93
98
  * This is respective to gossipsub seenTTL (which is 550 * 0.7 = 385s), also it's respective
@@ -192,6 +197,19 @@ type PreprocessResult =
192
197
  | {action: PreprocessAction.AwaitBlock; root: RootHex}
193
198
  | {action: PreprocessAction.AwaitEnvelope; root: RootHex};
194
199
 
200
+ enum SearchTarget {
201
+ Block,
202
+ PayloadEnvelope,
203
+ }
204
+
205
+ type SearchUnknownRootTarget =
206
+ | {target: SearchTarget.Block}
207
+ | {
208
+ target: SearchTarget.PayloadEnvelope;
209
+ /** The message slot is the payload's slot, so it can be used for UnknownBlockInput search */
210
+ slotIsPayloadSlot: boolean;
211
+ };
212
+
195
213
  /**
196
214
  * Network processor handles the gossip queues and throtles processing to not overload the main thread
197
215
  * - Decides when to process work and what to process
@@ -347,12 +365,17 @@ export class NetworkProcessor {
347
365
  }
348
366
 
349
367
  /**
350
- * Search envelope via `ChainEvent.unknownEnvelopeBlockRoot` event
368
+ * Search envelope via `ChainEvent.unknownEnvelopeBlockRoot` / `ChainEvent.unknownEnvelopeBlockRootSlot` event
351
369
  * Slot is the message slot, which is not necessarily the same as the envelope's slot, but it can be used for a good prune strategy.
352
370
  * In the rare case, if 2 messages on 2 slots search for the same root (for example beacon_attestation) we may emit the same root twice but BlockInputSync should handle it well.
353
371
  * Same peer-forwarding + dedup behavior as searchUnknownBlock.
354
372
  */
355
- searchUnknownEnvelope({slot, root}: SlotRootHex, source: BlockInputSource, peer?: PeerIdStr): void {
373
+ searchUnknownEnvelope(
374
+ {slot, root}: SlotRootHex,
375
+ source: BlockInputSource,
376
+ peer?: PeerIdStr,
377
+ slotIsPayloadSlot = false
378
+ ): void {
356
379
  if (this.chain.seenPayloadEnvelope(root)) {
357
380
  return;
358
381
  }
@@ -369,8 +392,17 @@ export class NetworkProcessor {
369
392
  forwardedPeers.add(peer);
370
393
  shouldEmit = true;
371
394
  }
395
+ // the payload-slot seed must reach the sync once, even if a slot-less trigger started the search first
396
+ if (slotIsPayloadSlot && !entry.envelopeSlotEmitted) {
397
+ shouldEmit = true;
398
+ }
372
399
  if (shouldEmit) {
373
- this.chain.emitter.emit(ChainEvent.unknownEnvelopeBlockRoot, {rootHex: root, slot, peer, source});
400
+ if (slotIsPayloadSlot) {
401
+ entry.envelopeSlotEmitted = true;
402
+ this.chain.emitter.emit(ChainEvent.unknownEnvelopeBlockRootSlot, {rootHex: root, slot, peer, source});
403
+ } else {
404
+ this.chain.emitter.emit(ChainEvent.unknownEnvelopeBlockRoot, {rootHex: root, peer, source});
405
+ }
374
406
  }
375
407
  }
376
408
 
@@ -431,7 +463,7 @@ export class NetworkProcessor {
431
463
  // no need to check if root is a descendant of the current finalized block, it will be checked once we validate the message if needed
432
464
  if (root && !this.chain.forkChoice.hasBlockHexUnsafe(root)) {
433
465
  // starting from GLOAS, unknown root from data_column_sidecar also falls into this case
434
- this.searchUnknownRoot({slot, root}, true, false, peerId);
466
+ this.searchUnknownRoot({slot, root}, peerId, {target: SearchTarget.Block});
435
467
  // for beacon_attestation and beacon_aggregate_and_proof messages, this is only temporary.
436
468
  // if "index = 1" we need to await for the Envelope instead
437
469
  preprocessResult = {action: PreprocessAction.AwaitBlock, root};
@@ -446,27 +478,28 @@ export class NetworkProcessor {
446
478
  if (topicType === GossipType.beacon_block) {
447
479
  const parentRoot = getParentRootFromSignedBeaconBlockSerialized(message.msg.data);
448
480
  if (parentRoot) {
449
- let searchParentBlock = false;
450
- let searchParentEnvelope = false;
481
+ let search: SearchUnknownRootTarget | null = null;
451
482
  if (ForkSeq[fork] >= ForkSeq.gloas) {
452
483
  // GLOAS: also check parent envelope, same logic as execution_payload_bid
453
484
  const parentBlockHash = getParentBlockHashFromGloasSignedBeaconBlockSerialized(message.msg.data);
454
485
  if (parentBlockHash && !this.chain.forkChoice.getBlockHexAndBlockHash(parentRoot, parentBlockHash)) {
455
486
  const protoBlock = this.chain.forkChoice.getBlockHexDefaultStatus(parentRoot);
456
487
  if (protoBlock === null) {
457
- searchParentBlock = true;
488
+ search = {target: SearchTarget.Block};
458
489
  } else if (
459
490
  protoBlock.executionPayloadBlockHash &&
460
491
  protoBlock.executionPayloadBlockHash !== parentBlockHash
461
492
  ) {
462
493
  // only search for the envelope by block root if we're sure there is one. Otherwise UnknownBlockSync will penalize the peer.
463
- searchParentEnvelope = true;
494
+ search = {target: SearchTarget.PayloadEnvelope, slotIsPayloadSlot: false};
464
495
  }
465
496
  }
466
497
  } else if (!this.chain.forkChoice.hasBlockHexUnsafe(parentRoot)) {
467
- searchParentBlock = true;
498
+ search = {target: SearchTarget.Block};
499
+ }
500
+ if (search !== null) {
501
+ this.searchUnknownRoot({slot, root: parentRoot}, peerId, search);
468
502
  }
469
- this.searchUnknownRoot({slot, root: parentRoot}, searchParentBlock, searchParentEnvelope, peerId);
470
503
  }
471
504
  preprocessResult = {action: PreprocessAction.PushToQueue};
472
505
  }
@@ -485,7 +518,10 @@ export class NetworkProcessor {
485
518
  : getDataIndexFromSignedAggregateAndProofSerialized(message.msg.data);
486
519
  if (attIndex === 1 && !this.chain.forkChoice.hasPayloadHexUnsafe(root)) {
487
520
  // attestation votes that the payload is available but it is not yet known
488
- this.searchUnknownRoot({slot, root}, false, true, peerId);
521
+ this.searchUnknownRoot({slot, root}, peerId, {
522
+ target: SearchTarget.PayloadEnvelope,
523
+ slotIsPayloadSlot: false,
524
+ });
489
525
  preprocessResult = {action: PreprocessAction.AwaitEnvelope, root};
490
526
  }
491
527
  break;
@@ -494,8 +530,13 @@ export class NetworkProcessor {
494
530
  if (root == null) break;
495
531
  const payloadPresent = getPayloadPresentFromPayloadAttestationMessageSerialized(message.msg.data);
496
532
  if (payloadPresent && !this.chain.forkChoice.hasPayloadHexUnsafe(root)) {
497
- // payload attestation votes that the payload is available but it is not yet known
498
- this.searchUnknownRoot({slot, root}, false, true, peerId);
533
+ // payload attestation votes that the payload is available but it is not yet known.
534
+ // this is optimistic search, the peer may not have the payload (only the ptc committee had).
535
+ // the PTC vote's slot is the payload's slot
536
+ this.searchUnknownRoot({slot, root}, undefined, {
537
+ target: SearchTarget.PayloadEnvelope,
538
+ slotIsPayloadSlot: true,
539
+ });
499
540
  // do not await the envelope, payload attestation processing only requires that the block is known
500
541
  // also do not reset preprocessResult, we may already await for the block
501
542
  }
@@ -504,7 +545,12 @@ export class NetworkProcessor {
504
545
  case GossipType.data_column_sidecar: {
505
546
  if (root == null) break;
506
547
  if (!this.chain.forkChoice.hasPayloadHexUnsafe(root)) {
507
- this.searchUnknownRoot({slot, root}, false, true, peerId);
548
+ // this is optimistic search, the peer may not have the payload.
549
+ // the sidecar's slot is the block's (and so the payload's) slot
550
+ this.searchUnknownRoot({slot, root}, undefined, {
551
+ target: SearchTarget.PayloadEnvelope,
552
+ slotIsPayloadSlot: true,
553
+ });
508
554
  // do not await the envelope, we can do gossip validation
509
555
  // also do not reset preprocessResult, we may already await for the block
510
556
  }
@@ -515,7 +561,7 @@ export class NetworkProcessor {
515
561
  // Extract beacon_block_root directly
516
562
  const blockRoot = getBeaconBlockRootFromExecutionPayloadEnvelopeSerialized(message.msg.data);
517
563
  if (blockRoot && !this.chain.forkChoice.hasBlockHexUnsafe(blockRoot)) {
518
- this.searchUnknownRoot({slot, root: blockRoot}, true, false, peerId);
564
+ this.searchUnknownRoot({slot, root: blockRoot}, peerId, {target: SearchTarget.Block});
519
565
  // We always want to await the block
520
566
  // This allows us to properly forward the payload envelope
521
567
  preprocessResult = {action: PreprocessAction.AwaitBlock, root: blockRoot};
@@ -534,13 +580,16 @@ export class NetworkProcessor {
534
580
  ) {
535
581
  const protoBlock = this.chain.forkChoice.getBlockHexDefaultStatus(parentBlockRoot);
536
582
  if (protoBlock === null) {
537
- this.searchUnknownRoot({slot, root: parentBlockRoot}, true, false, peerId);
583
+ this.searchUnknownRoot({slot, root: parentBlockRoot}, peerId, {target: SearchTarget.Block});
538
584
  preprocessResult = {action: PreprocessAction.AwaitBlock, root: parentBlockRoot};
539
585
  } else if (
540
586
  protoBlock.executionPayloadBlockHash &&
541
587
  protoBlock.executionPayloadBlockHash !== parentBlockHash
542
588
  ) {
543
- this.searchUnknownRoot({slot, root: parentBlockRoot}, false, true, peerId);
589
+ this.searchUnknownRoot({slot, root: parentBlockRoot}, peerId, {
590
+ target: SearchTarget.PayloadEnvelope,
591
+ slotIsPayloadSlot: false,
592
+ });
544
593
  preprocessResult = {action: PreprocessAction.AwaitEnvelope, root: parentBlockRoot};
545
594
  }
546
595
  }
@@ -641,16 +690,24 @@ export class NetworkProcessor {
641
690
  return true;
642
691
  }
643
692
 
693
+ /**
694
+ * Search block/envelope given a SlotRootHex
695
+ * undefined peer id means optimistic search
696
+ */
644
697
  private searchUnknownRoot(
645
698
  slotRoot: SlotRootHex,
646
- searchBlock: boolean,
647
- searchEnvelope: boolean,
648
- peerId: PeerIdStr
699
+ peerId: PeerIdStr | undefined,
700
+ search: SearchUnknownRootTarget
649
701
  ): void {
650
- if (!searchBlock && !searchEnvelope) return;
651
702
  if (this.tooManySearchedRoots(slotRoot.slot, slotRoot.root)) return;
652
- if (searchBlock) this.searchUnknownBlock(slotRoot, BlockInputSource.network_processor, peerId);
653
- if (searchEnvelope) this.searchUnknownEnvelope(slotRoot, BlockInputSource.network_processor, peerId);
703
+ switch (search.target) {
704
+ case SearchTarget.Block:
705
+ this.searchUnknownBlock(slotRoot, BlockInputSource.network_processor, peerId);
706
+ break;
707
+ case SearchTarget.PayloadEnvelope:
708
+ this.searchUnknownEnvelope(slotRoot, BlockInputSource.network_processor, peerId, search.slotIsPayloadSlot);
709
+ break;
710
+ }
654
711
  }
655
712
 
656
713
  private pushPendingGossipsubMessageToQueue(message: PendingGossipsubMessage): void {
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;