@lodestar/beacon-node 1.49.0-dev.46b983948a → 1.49.0-dev.57ffc39074

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 (79) 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/attesterSlashingError.d.ts +4 -1
  15. package/lib/chain/errors/attesterSlashingError.d.ts.map +1 -1
  16. package/lib/chain/errors/attesterSlashingError.js +1 -0
  17. package/lib/chain/errors/attesterSlashingError.js.map +1 -1
  18. package/lib/chain/errors/payloadAttestation.d.ts +4 -0
  19. package/lib/chain/errors/payloadAttestation.d.ts.map +1 -1
  20. package/lib/chain/errors/payloadAttestation.js +1 -0
  21. package/lib/chain/errors/payloadAttestation.js.map +1 -1
  22. package/lib/chain/errors/proposerSlashingError.d.ts +4 -1
  23. package/lib/chain/errors/proposerSlashingError.d.ts.map +1 -1
  24. package/lib/chain/errors/proposerSlashingError.js +1 -0
  25. package/lib/chain/errors/proposerSlashingError.js.map +1 -1
  26. package/lib/chain/options.d.ts +0 -4
  27. package/lib/chain/options.d.ts.map +1 -1
  28. package/lib/chain/options.js +0 -1
  29. package/lib/chain/options.js.map +1 -1
  30. package/lib/chain/validation/attesterSlashing.d.ts.map +1 -1
  31. package/lib/chain/validation/attesterSlashing.js +1 -2
  32. package/lib/chain/validation/attesterSlashing.js.map +1 -1
  33. package/lib/chain/validation/payloadAttestationMessage.d.ts.map +1 -1
  34. package/lib/chain/validation/payloadAttestationMessage.js +8 -1
  35. package/lib/chain/validation/payloadAttestationMessage.js.map +1 -1
  36. package/lib/chain/validation/proposerSlashing.js +1 -2
  37. package/lib/chain/validation/proposerSlashing.js.map +1 -1
  38. package/lib/db/repositories/blockArchive.d.ts +5 -0
  39. package/lib/db/repositories/blockArchive.d.ts.map +1 -1
  40. package/lib/db/repositories/blockArchive.js +36 -1
  41. package/lib/db/repositories/blockArchive.js.map +1 -1
  42. package/lib/metrics/metrics/lodestar.d.ts +5 -1
  43. package/lib/metrics/metrics/lodestar.d.ts.map +1 -1
  44. package/lib/metrics/metrics/lodestar.js +10 -0
  45. package/lib/metrics/metrics/lodestar.js.map +1 -1
  46. package/lib/network/processor/gossipValidatorFn.d.ts +2 -0
  47. package/lib/network/processor/gossipValidatorFn.d.ts.map +1 -1
  48. package/lib/network/processor/gossipValidatorFn.js +88 -7
  49. package/lib/network/processor/gossipValidatorFn.js.map +1 -1
  50. package/lib/network/processor/index.d.ts +6 -2
  51. package/lib/network/processor/index.d.ts.map +1 -1
  52. package/lib/network/processor/index.js +62 -24
  53. package/lib/network/processor/index.js.map +1 -1
  54. package/lib/sync/types.d.ts +21 -0
  55. package/lib/sync/types.d.ts.map +1 -1
  56. package/lib/sync/types.js +22 -0
  57. package/lib/sync/types.js.map +1 -1
  58. package/lib/sync/unknownBlock.d.ts +16 -0
  59. package/lib/sync/unknownBlock.d.ts.map +1 -1
  60. package/lib/sync/unknownBlock.js +154 -2
  61. package/lib/sync/unknownBlock.js.map +1 -1
  62. package/package.json +14 -14
  63. package/src/api/impl/validator/index.ts +3 -1
  64. package/src/chain/archiveStore/historicalState/metrics.ts +4 -0
  65. package/src/chain/archiveStore/utils/pruneHistory.ts +5 -2
  66. package/src/chain/emitter.ts +7 -2
  67. package/src/chain/errors/attesterSlashingError.ts +3 -1
  68. package/src/chain/errors/payloadAttestation.ts +2 -0
  69. package/src/chain/errors/proposerSlashingError.ts +3 -1
  70. package/src/chain/options.ts +0 -5
  71. package/src/chain/validation/attesterSlashing.ts +1 -2
  72. package/src/chain/validation/payloadAttestationMessage.ts +9 -0
  73. package/src/chain/validation/proposerSlashing.ts +1 -2
  74. package/src/db/repositories/blockArchive.ts +38 -1
  75. package/src/metrics/metrics/lodestar.ts +17 -1
  76. package/src/network/processor/gossipValidatorFn.ts +111 -7
  77. package/src/network/processor/index.ts +81 -24
  78. package/src/sync/types.ts +24 -3
  79. package/src/sync/unknownBlock.ts +162 -1
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.onClockSlot);
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.onClockSlot(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.onClockSlot);
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 onClockSlot 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,118 @@ export class BlockInputSync {
785
834
  }
786
835
  }
787
836
 
837
+ private onClockSlot = (slot: Slot): void => {
838
+ if (!isForkPostGloas(this.config.getForkName(slot))) {
839
+ return;
840
+ }
841
+ // end the previous slot's loop, wherever it is: its sleep is bound to this same controller
842
+ this.payloadPollAbortController?.abort();
843
+ this.payloadPollAbortController = new AbortController();
844
+ // entries from an earlier slot is not polled
845
+ for (const [rootHex, {slot: deferredSlot, source, peer}] of this.deferredPayloadRoots) {
846
+ if (deferredSlot < slot) {
847
+ if (!this.chain.forkChoice.hasPayloadHexUnsafe(rootHex)) {
848
+ this.addDeferredPayloadRoot(rootHex, deferredSlot, source, peer);
849
+ }
850
+ this.deferredPayloadRoots.delete(rootHex);
851
+ }
852
+ }
853
+ this.pollPayloadsAtSlot(slot, this.payloadPollAbortController.signal).catch((e) => {
854
+ if (!isErrorAborted(e)) {
855
+ this.logger.debug("Error polling payloads", {slot}, e as Error);
856
+ }
857
+ });
858
+ };
859
+
860
+ /**
861
+ * Poll unknown payloads of the current slot.
862
+ */
863
+ private async pollPayloadsAtSlot(slot: Slot, signal: AbortSignal): Promise<void> {
864
+ // fixed tick count so the loop cannot outlive its slot: for mainnet, it's 6 ticks
865
+ const tickCount = Math.floor(
866
+ (this.config.SLOT_DURATION_MS - this.config.getPayloadDueMs()) / PAYLOAD_POLL_INTERVAL_MS
867
+ );
868
+ // a root still missing on several ticks is searched on each of them, recorded once
869
+ const searchedRoots = new Set<RootHex>();
870
+ let polls = 0;
871
+
872
+ try {
873
+ await sleep(Math.max(0, this.config.getPayloadDueMs() - this.chain.clock.msFromSlot(slot)), signal);
874
+
875
+ for (polls = 0; polls < tickCount; polls++) {
876
+ let importedCount = 0;
877
+ for (const [rootHex, {slot: deferredSlot, source, peer}] of this.deferredPayloadRoots) {
878
+ if (this.chain.forkChoice.hasPayloadHexUnsafe(rootHex)) {
879
+ importedCount++;
880
+ continue;
881
+ }
882
+ // copy to the regular pendingPayloads so that we can search
883
+ this.addDeferredPayloadRoot(rootHex, deferredSlot, source, peer);
884
+ searchedRoots.add(rootHex);
885
+ }
886
+
887
+ // an empty deferredPayloadRoots means there is no optimistic search comes before PAYLOAD_DUE
888
+ // we have to poll until the payload of current slot is imported or slot end
889
+ if (this.deferredPayloadRoots.size > 0 && importedCount === this.deferredPayloadRoots.size) {
890
+ if (searchedRoots.size > 0) {
891
+ // the backup search was needed
892
+ this.metrics?.blockInputSync.deferredPayloadResult.inc({
893
+ result: DeferredPayloadResult.ImportedAfterSearch,
894
+ });
895
+ this.metrics?.blockInputSync.deferredPayloadPolls.observe(polls);
896
+ this.logger.verbose("Payload imported after unknown sync search", {
897
+ slot,
898
+ roots: Array.from(searchedRoots).join(","),
899
+ polls,
900
+ });
901
+ } else {
902
+ // this happens most of the time on devnet
903
+ // gossip delivered everything before PAYLOAD_DUE
904
+ this.metrics?.blockInputSync.deferredPayloadResult.inc({
905
+ result: DeferredPayloadResult.ImportedBeforeSearch,
906
+ });
907
+ }
908
+ return;
909
+ }
910
+
911
+ this.triggerUnknownBlockSearch();
912
+ if (polls < tickCount - 1) {
913
+ // no sleep after the last tick: the loop would otherwise end right at the slot boundary,
914
+ // where onClockSlot aborts it
915
+ await sleep(PAYLOAD_POLL_INTERVAL_MS, signal);
916
+ }
917
+ }
918
+
919
+ // reaching here means the early return never fired
920
+ if (this.deferredPayloadRoots.size > 0) {
921
+ this.metrics?.blockInputSync.deferredPayloadResult.inc({result: DeferredPayloadResult.Unresolved});
922
+ this.logger.debug("Deferred payloads still missing at end of slot", {
923
+ slot,
924
+ roots: Array.from(this.deferredPayloadRoots.keys()).join(","),
925
+ });
926
+ } else {
927
+ // no optimistic search and no payload import ended the loop, eg a skipped slot
928
+ this.metrics?.blockInputSync.deferredPayloadResult.inc({result: DeferredPayloadResult.ResolvedNoRoot});
929
+ this.logger.debug("No deferred payloads at slot", {
930
+ slot,
931
+ });
932
+ }
933
+ } catch (e) {
934
+ if (!isErrorAborted(e)) {
935
+ throw e;
936
+ }
937
+ this.logger.verbose("Payload poll aborted", {slot, polls, msIntoSlot: this.chain.clock.msFromSlot(slot)});
938
+ }
939
+ }
940
+
941
+ private addDeferredPayloadRoot(rootHex: RootHex, slot: Slot, source: BlockInputSource, peer?: PeerIdStr): void {
942
+ const isNewRoot = this.addByPayloadRootHex(rootHex, peer, slot);
943
+ if (isNewRoot) {
944
+ this.metrics?.blockInputSync.requests.inc({type: PendingBlockType.DEFERRED_PAYLOAD_BLOCK_ROOT});
945
+ this.metrics?.blockInputSync.payloadSource.inc({source});
946
+ }
947
+ }
948
+
788
949
  private async downloadBlock(block: BlockInputSyncCacheItem): Promise<void> {
789
950
  if (block.status !== PendingBlockInputStatus.pending) {
790
951
  return;