@lodestar/beacon-node 1.42.0-dev.5f2fffc2ce → 1.42.0-dev.7552832620

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 (142) hide show
  1. package/lib/api/impl/beacon/blocks/index.d.ts.map +1 -1
  2. package/lib/api/impl/beacon/blocks/index.js +35 -16
  3. package/lib/api/impl/beacon/blocks/index.js.map +1 -1
  4. package/lib/chain/blocks/blockInput/types.d.ts +3 -3
  5. package/lib/chain/blocks/blockInput/types.d.ts.map +1 -1
  6. package/lib/chain/blocks/importBlock.d.ts.map +1 -1
  7. package/lib/chain/blocks/importBlock.js +18 -2
  8. package/lib/chain/blocks/importBlock.js.map +1 -1
  9. package/lib/chain/blocks/importExecutionPayload.d.ts +48 -0
  10. package/lib/chain/blocks/importExecutionPayload.d.ts.map +1 -0
  11. package/lib/chain/blocks/importExecutionPayload.js +159 -0
  12. package/lib/chain/blocks/importExecutionPayload.js.map +1 -0
  13. package/lib/chain/blocks/payloadEnvelopeInput/index.d.ts +3 -0
  14. package/lib/chain/blocks/payloadEnvelopeInput/index.d.ts.map +1 -0
  15. package/lib/chain/blocks/payloadEnvelopeInput/index.js +3 -0
  16. package/lib/chain/blocks/payloadEnvelopeInput/index.js.map +1 -0
  17. package/lib/chain/blocks/payloadEnvelopeInput/payloadEnvelopeInput.d.ts +80 -0
  18. package/lib/chain/blocks/payloadEnvelopeInput/payloadEnvelopeInput.d.ts.map +1 -0
  19. package/lib/chain/blocks/payloadEnvelopeInput/payloadEnvelopeInput.js +248 -0
  20. package/lib/chain/blocks/payloadEnvelopeInput/payloadEnvelopeInput.js.map +1 -0
  21. package/lib/chain/blocks/payloadEnvelopeInput/types.d.ts +29 -0
  22. package/lib/chain/blocks/payloadEnvelopeInput/types.d.ts.map +1 -0
  23. package/lib/chain/blocks/payloadEnvelopeInput/types.js +11 -0
  24. package/lib/chain/blocks/payloadEnvelopeInput/types.js.map +1 -0
  25. package/lib/chain/blocks/payloadEnvelopeProcessor.d.ts +15 -0
  26. package/lib/chain/blocks/payloadEnvelopeProcessor.d.ts.map +1 -0
  27. package/lib/chain/blocks/payloadEnvelopeProcessor.js +46 -0
  28. package/lib/chain/blocks/payloadEnvelopeProcessor.js.map +1 -0
  29. package/lib/chain/blocks/types.d.ts +7 -0
  30. package/lib/chain/blocks/types.d.ts.map +1 -1
  31. package/lib/chain/blocks/writePayloadEnvelopeInputToDb.d.ts +12 -0
  32. package/lib/chain/blocks/writePayloadEnvelopeInputToDb.d.ts.map +1 -0
  33. package/lib/chain/blocks/writePayloadEnvelopeInputToDb.js +40 -0
  34. package/lib/chain/blocks/writePayloadEnvelopeInputToDb.js.map +1 -0
  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 +28 -3
  38. package/lib/chain/chain.js.map +1 -1
  39. package/lib/chain/errors/executionPayloadEnvelope.d.ts +12 -2
  40. package/lib/chain/errors/executionPayloadEnvelope.d.ts.map +1 -1
  41. package/lib/chain/errors/executionPayloadEnvelope.js +3 -1
  42. package/lib/chain/errors/executionPayloadEnvelope.js.map +1 -1
  43. package/lib/chain/forkChoice/index.d.ts.map +1 -1
  44. package/lib/chain/forkChoice/index.js +0 -10
  45. package/lib/chain/forkChoice/index.js.map +1 -1
  46. package/lib/chain/interface.d.ts +6 -3
  47. package/lib/chain/interface.d.ts.map +1 -1
  48. package/lib/chain/opPools/utils.js +1 -1
  49. package/lib/chain/opPools/utils.js.map +1 -1
  50. package/lib/chain/produceBlock/computeNewStateRoot.d.ts.map +1 -1
  51. package/lib/chain/produceBlock/computeNewStateRoot.js +6 -1
  52. package/lib/chain/produceBlock/computeNewStateRoot.js.map +1 -1
  53. package/lib/chain/produceBlock/produceBlockBody.js +1 -1
  54. package/lib/chain/produceBlock/produceBlockBody.js.map +1 -1
  55. package/lib/chain/regen/interface.d.ts +2 -0
  56. package/lib/chain/regen/interface.d.ts.map +1 -1
  57. package/lib/chain/regen/interface.js +2 -0
  58. package/lib/chain/regen/interface.js.map +1 -1
  59. package/lib/chain/seenCache/index.d.ts +1 -1
  60. package/lib/chain/seenCache/index.d.ts.map +1 -1
  61. package/lib/chain/seenCache/index.js +1 -1
  62. package/lib/chain/seenCache/index.js.map +1 -1
  63. package/lib/chain/seenCache/seenGossipBlockInput.js +2 -2
  64. package/lib/chain/seenCache/seenGossipBlockInput.js.map +1 -1
  65. package/lib/chain/seenCache/seenPayloadEnvelopeInput.d.ts +38 -0
  66. package/lib/chain/seenCache/seenPayloadEnvelopeInput.d.ts.map +1 -0
  67. package/lib/chain/seenCache/seenPayloadEnvelopeInput.js +76 -0
  68. package/lib/chain/seenCache/seenPayloadEnvelopeInput.js.map +1 -0
  69. package/lib/chain/validation/executionPayloadEnvelope.d.ts.map +1 -1
  70. package/lib/chain/validation/executionPayloadEnvelope.js +30 -19
  71. package/lib/chain/validation/executionPayloadEnvelope.js.map +1 -1
  72. package/lib/chain/validation/lightClientFinalityUpdate.js +1 -1
  73. package/lib/chain/validation/lightClientFinalityUpdate.js.map +1 -1
  74. package/lib/chain/validation/lightClientOptimisticUpdate.js +1 -1
  75. package/lib/chain/validation/lightClientOptimisticUpdate.js.map +1 -1
  76. package/lib/chain/validatorMonitor.d.ts +2 -1
  77. package/lib/chain/validatorMonitor.d.ts.map +1 -1
  78. package/lib/chain/validatorMonitor.js +4 -1
  79. package/lib/chain/validatorMonitor.js.map +1 -1
  80. package/lib/execution/engine/interface.d.ts +2 -2
  81. package/lib/metrics/metrics/lodestar.d.ts +28 -0
  82. package/lib/metrics/metrics/lodestar.d.ts.map +1 -1
  83. package/lib/metrics/metrics/lodestar.js +74 -0
  84. package/lib/metrics/metrics/lodestar.js.map +1 -1
  85. package/lib/network/network.js +2 -2
  86. package/lib/network/network.js.map +1 -1
  87. package/lib/network/processor/extractSlotRootFns.d.ts.map +1 -1
  88. package/lib/network/processor/extractSlotRootFns.js +14 -4
  89. package/lib/network/processor/extractSlotRootFns.js.map +1 -1
  90. package/lib/network/processor/gossipHandlers.d.ts.map +1 -1
  91. package/lib/network/processor/gossipHandlers.js +31 -3
  92. package/lib/network/processor/gossipHandlers.js.map +1 -1
  93. package/lib/network/reqresp/ReqRespBeaconNode.d.ts +1 -1
  94. package/lib/network/reqresp/ReqRespBeaconNode.js +1 -1
  95. package/lib/sync/backfill/backfill.d.ts +1 -1
  96. package/lib/sync/backfill/backfill.js +1 -1
  97. package/lib/sync/constants.d.ts +1 -1
  98. package/lib/sync/constants.js +1 -1
  99. package/lib/util/sszBytes.d.ts +4 -1
  100. package/lib/util/sszBytes.d.ts.map +1 -1
  101. package/lib/util/sszBytes.js +69 -12
  102. package/lib/util/sszBytes.js.map +1 -1
  103. package/package.json +15 -15
  104. package/src/api/impl/beacon/blocks/index.ts +36 -17
  105. package/src/chain/blocks/blockInput/types.ts +3 -3
  106. package/src/chain/blocks/importBlock.ts +36 -2
  107. package/src/chain/blocks/importExecutionPayload.ts +241 -0
  108. package/src/chain/blocks/payloadEnvelopeInput/index.ts +2 -0
  109. package/src/chain/blocks/payloadEnvelopeInput/payloadEnvelopeInput.ts +336 -0
  110. package/src/chain/blocks/payloadEnvelopeInput/types.ts +33 -0
  111. package/src/chain/blocks/payloadEnvelopeProcessor.ts +61 -0
  112. package/src/chain/blocks/types.ts +8 -0
  113. package/src/chain/blocks/writePayloadEnvelopeInputToDb.ts +55 -0
  114. package/src/chain/chain.ts +37 -3
  115. package/src/chain/errors/executionPayloadEnvelope.ts +6 -2
  116. package/src/chain/forkChoice/index.ts +0 -10
  117. package/src/chain/interface.ts +6 -3
  118. package/src/chain/opPools/utils.ts +1 -1
  119. package/src/chain/produceBlock/computeNewStateRoot.ts +6 -1
  120. package/src/chain/produceBlock/produceBlockBody.ts +1 -1
  121. package/src/chain/regen/interface.ts +2 -0
  122. package/src/chain/seenCache/index.ts +1 -1
  123. package/src/chain/seenCache/seenGossipBlockInput.ts +2 -2
  124. package/src/chain/seenCache/seenPayloadEnvelopeInput.ts +106 -0
  125. package/src/chain/validation/executionPayloadEnvelope.ts +38 -25
  126. package/src/chain/validation/lightClientFinalityUpdate.ts +1 -1
  127. package/src/chain/validation/lightClientOptimisticUpdate.ts +1 -1
  128. package/src/chain/validatorMonitor.ts +11 -1
  129. package/src/execution/engine/interface.ts +2 -2
  130. package/src/metrics/metrics/lodestar.ts +77 -0
  131. package/src/network/network.ts +2 -2
  132. package/src/network/processor/extractSlotRootFns.ts +18 -5
  133. package/src/network/processor/gossipHandlers.ts +37 -1
  134. package/src/network/reqresp/ReqRespBeaconNode.ts +1 -1
  135. package/src/sync/backfill/backfill.ts +1 -1
  136. package/src/sync/constants.ts +1 -1
  137. package/src/util/sszBytes.ts +90 -10
  138. package/lib/chain/seenCache/seenExecutionPayloadEnvelope.d.ts +0 -15
  139. package/lib/chain/seenCache/seenExecutionPayloadEnvelope.d.ts.map +0 -1
  140. package/lib/chain/seenCache/seenExecutionPayloadEnvelope.js +0 -28
  141. package/lib/chain/seenCache/seenExecutionPayloadEnvelope.js.map +0 -1
  142. package/src/chain/seenCache/seenExecutionPayloadEnvelope.ts +0 -34
@@ -0,0 +1,241 @@
1
+ import {routes} from "@lodestar/api";
2
+ import {ForkName} from "@lodestar/params";
3
+ import {
4
+ BeaconStateView,
5
+ CachedBeaconStateGloas,
6
+ getExecutionPayloadEnvelopeSignatureSet,
7
+ } from "@lodestar/state-transition";
8
+ import {processExecutionPayloadEnvelope} from "@lodestar/state-transition/block";
9
+ import {byteArrayEquals, fromHex, toRootHex} from "@lodestar/utils";
10
+ import {ExecutionPayloadStatus} from "../../execution/index.js";
11
+ import {isQueueErrorAborted} from "../../util/queue/index.js";
12
+ import {BeaconChain} from "../chain.js";
13
+ import {RegenCaller} from "../regen/interface.js";
14
+ import {PayloadEnvelopeInput} from "../seenCache/seenPayloadEnvelopeInput.js";
15
+ import {ImportPayloadOpts} from "./types.js";
16
+
17
+ const EVENTSTREAM_EMIT_RECENT_EXECUTION_PAYLOAD_SLOTS = 64;
18
+
19
+ export enum PayloadErrorCode {
20
+ EXECUTION_ENGINE_INVALID = "PAYLOAD_ERROR_EXECUTION_ENGINE_INVALID",
21
+ EXECUTION_ENGINE_ERROR = "PAYLOAD_ERROR_EXECUTION_ENGINE_ERROR",
22
+ BLOCK_NOT_IN_FORK_CHOICE = "PAYLOAD_ERROR_BLOCK_NOT_IN_FORK_CHOICE",
23
+ STATE_TRANSITION_ERROR = "PAYLOAD_ERROR_STATE_TRANSITION_ERROR",
24
+ INVALID_SIGNATURE = "PAYLOAD_ERROR_INVALID_SIGNATURE",
25
+ }
26
+
27
+ export type PayloadErrorType =
28
+ | {
29
+ code: PayloadErrorCode.EXECUTION_ENGINE_INVALID;
30
+ execStatus: ExecutionPayloadStatus;
31
+ errorMessage: string;
32
+ }
33
+ | {
34
+ code: PayloadErrorCode.EXECUTION_ENGINE_ERROR;
35
+ execStatus: ExecutionPayloadStatus;
36
+ errorMessage: string;
37
+ }
38
+ | {
39
+ code: PayloadErrorCode.BLOCK_NOT_IN_FORK_CHOICE;
40
+ blockRootHex: string;
41
+ }
42
+ | {
43
+ code: PayloadErrorCode.STATE_TRANSITION_ERROR;
44
+ message: string;
45
+ }
46
+ | {
47
+ code: PayloadErrorCode.INVALID_SIGNATURE;
48
+ };
49
+
50
+ export class PayloadError extends Error {
51
+ type: PayloadErrorType;
52
+
53
+ constructor(type: PayloadErrorType, message?: string) {
54
+ super(message ?? type.code);
55
+ this.type = type;
56
+ }
57
+ }
58
+
59
+ /**
60
+ * Import an execution payload envelope after all data is available.
61
+ *
62
+ * This function:
63
+ * 1. Gets the ProtoBlock from fork choice
64
+ * 2. Applies write-queue backpressure (waitForSpace) early, before verification
65
+ * 3. Regenerates the block state
66
+ * 4. Runs EL verification (notifyNewPayload) in parallel with signature verification and processExecutionPayloadEnvelope
67
+ * 5. Persists verified payload envelope to hot DB
68
+ * 6. Updates fork choice
69
+ * 7. Caches the post-execution payload state
70
+ * 8. Records metrics for column sources
71
+ *
72
+ */
73
+ export async function importExecutionPayload(
74
+ this: BeaconChain,
75
+ payloadInput: PayloadEnvelopeInput,
76
+ opts: ImportPayloadOpts = {}
77
+ ): Promise<void> {
78
+ const envelope = payloadInput.getPayloadEnvelope();
79
+ const blockRootHex = payloadInput.blockRootHex;
80
+
81
+ // 1. Get ProtoBlock for parent root lookup
82
+ const protoBlock = this.forkChoice.getBlockHexDefaultStatus(blockRootHex);
83
+ if (!protoBlock) {
84
+ throw new PayloadError({
85
+ code: PayloadErrorCode.BLOCK_NOT_IN_FORK_CHOICE,
86
+ blockRootHex,
87
+ });
88
+ }
89
+
90
+ // 2. Apply backpressure from the write queue early, before doing verification work.
91
+ // The actual DB write is deferred until after verification succeeds.
92
+ await this.unfinalizedPayloadEnvelopeWrites.waitForSpace();
93
+
94
+ // 3. Get pre-state for processExecutionPayloadEnvelope
95
+ // We need the block state (post-block, pre-payload) to process the envelope
96
+ const blockState = (await this.regen.getBlockSlotState(
97
+ protoBlock,
98
+ protoBlock.slot,
99
+ {dontTransferCache: true},
100
+ RegenCaller.processBlock
101
+ )) as CachedBeaconStateGloas;
102
+
103
+ // 4. Run verification steps in parallel
104
+ // Note: No data availability check needed here - importExecutionPayload is only
105
+ // called when payloadInput.isComplete() is true, so all data is already available.
106
+ const [execResult, signatureValid, postPayloadResult] = await Promise.all([
107
+ this.executionEngine.notifyNewPayload(
108
+ ForkName.gloas,
109
+ envelope.message.payload,
110
+ payloadInput.getVersionedHashes(),
111
+ fromHex(protoBlock.parentRoot),
112
+ envelope.message.executionRequests
113
+ ),
114
+
115
+ opts.validSignature === true
116
+ ? Promise.resolve(true)
117
+ : (async () => {
118
+ const signatureSet = getExecutionPayloadEnvelopeSignatureSet(
119
+ this.config,
120
+ blockState.epochCtx.pubkeyCache,
121
+ new BeaconStateView(blockState),
122
+ envelope,
123
+ payloadInput.proposerIndex
124
+ );
125
+ return this.bls.verifySignatureSets([signatureSet]);
126
+ })(),
127
+
128
+ // Signature verified separately above.
129
+ // State root check is done separately below with better error typing (matching block pipeline pattern).
130
+ (async () => {
131
+ try {
132
+ return {
133
+ postPayloadState: processExecutionPayloadEnvelope(blockState, envelope, {
134
+ verifySignature: false,
135
+ verifyStateRoot: false,
136
+ }),
137
+ };
138
+ } catch (e) {
139
+ throw new PayloadError(
140
+ {
141
+ code: PayloadErrorCode.STATE_TRANSITION_ERROR,
142
+ message: (e as Error).message,
143
+ },
144
+ `State transition error: ${(e as Error).message}`
145
+ );
146
+ }
147
+ })(),
148
+ ]);
149
+
150
+ // 4b. Check signature verification result
151
+ if (!signatureValid) {
152
+ throw new PayloadError({code: PayloadErrorCode.INVALID_SIGNATURE});
153
+ }
154
+
155
+ // 5. Handle EL response
156
+ switch (execResult.status) {
157
+ case ExecutionPayloadStatus.VALID:
158
+ break;
159
+
160
+ case ExecutionPayloadStatus.INVALID:
161
+ throw new PayloadError({
162
+ code: PayloadErrorCode.EXECUTION_ENGINE_INVALID,
163
+ execStatus: execResult.status,
164
+ errorMessage: execResult.validationError ?? "",
165
+ });
166
+
167
+ case ExecutionPayloadStatus.ACCEPTED:
168
+ case ExecutionPayloadStatus.SYNCING:
169
+ // TODO GLOAS: Handle optimistic import for payload - for now treat as error
170
+ throw new PayloadError({
171
+ code: PayloadErrorCode.EXECUTION_ENGINE_ERROR,
172
+ execStatus: execResult.status,
173
+ errorMessage: execResult.validationError ?? "EL syncing, payload not yet validated",
174
+ });
175
+
176
+ case ExecutionPayloadStatus.INVALID_BLOCK_HASH:
177
+ case ExecutionPayloadStatus.ELERROR:
178
+ case ExecutionPayloadStatus.UNAVAILABLE:
179
+ throw new PayloadError({
180
+ code: PayloadErrorCode.EXECUTION_ENGINE_ERROR,
181
+ execStatus: execResult.status,
182
+ errorMessage: execResult.validationError ?? "",
183
+ });
184
+ }
185
+
186
+ // 5b. Verify envelope state root matches post-state
187
+ const postPayloadState = postPayloadResult.postPayloadState;
188
+ const postPayloadStateRoot = postPayloadState.hashTreeRoot();
189
+ if (!byteArrayEquals(envelope.message.stateRoot, postPayloadStateRoot)) {
190
+ throw new PayloadError({
191
+ code: PayloadErrorCode.STATE_TRANSITION_ERROR,
192
+ message: `Envelope state root mismatch expected=${toRootHex(envelope.message.stateRoot)} actual=${toRootHex(postPayloadStateRoot)}`,
193
+ });
194
+ }
195
+
196
+ // 5c. Persist payload envelope to hot DB (performed asynchronously to avoid blocking)
197
+ this.unfinalizedPayloadEnvelopeWrites.push(payloadInput).catch((e) => {
198
+ if (!isQueueErrorAborted(e)) {
199
+ this.logger.error(
200
+ "Error pushing payload envelope to unfinalized write queue",
201
+ {slot: payloadInput.slot, root: blockRootHex},
202
+ e as Error
203
+ );
204
+ }
205
+ });
206
+
207
+ // 6. Update fork choice
208
+ this.forkChoice.onExecutionPayload(
209
+ blockRootHex,
210
+ payloadInput.getBlockHashHex(),
211
+ envelope.message.payload.blockNumber,
212
+ toRootHex(postPayloadStateRoot)
213
+ );
214
+
215
+ // 7. Cache payload state
216
+ // TODO GLOAS: Enable when PR #8868 merged (adds processPayloadState)
217
+ // this.regen.processPayloadState(postPayloadState);
218
+ // if epoch boundary also call
219
+ // this.regen.addCheckpointState(cp, checkpointState, true);
220
+
221
+ // 8. Record metrics for payload envelope and column sources
222
+ this.metrics?.importPayload.bySource.inc({source: payloadInput.getPayloadEnvelopeSource().source});
223
+ for (const {source} of payloadInput.getSampledColumnsWithSource()) {
224
+ this.metrics?.importPayload.columnsBySource.inc({source});
225
+ }
226
+
227
+ this.logger.verbose("Execution payload imported", {
228
+ slot: payloadInput.slot,
229
+ root: blockRootHex,
230
+ blockHash: payloadInput.getBlockHashHex(),
231
+ });
232
+
233
+ // 9. Emit event after payload is fully verified and imported to fork choice, only for recent enough payloads
234
+ const currentSlot = this.clock.currentSlot;
235
+ if (currentSlot - payloadInput.slot < EVENTSTREAM_EMIT_RECENT_EXECUTION_PAYLOAD_SLOTS) {
236
+ this.emitter.emit(routes.events.EventType.executionPayloadAvailable, {
237
+ slot: payloadInput.slot,
238
+ blockRoot: blockRootHex,
239
+ });
240
+ }
241
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./payloadEnvelopeInput.js";
2
+ export * from "./types.js";
@@ -0,0 +1,336 @@
1
+ import {NUMBER_OF_COLUMNS} from "@lodestar/params";
2
+ import {ColumnIndex, DataColumnSidecars, RootHex, Slot, ValidatorIndex, deneb, gloas} from "@lodestar/types";
3
+ import {toRootHex, withTimeout} from "@lodestar/utils";
4
+ import {VersionedHashes} from "../../../execution/index.js";
5
+ import {kzgCommitmentToVersionedHash} from "../../../util/blobs.js";
6
+ import {AddPayloadEnvelopeProps, ColumnWithSource, CreateFromBlockProps, SourceMeta} from "./types.js";
7
+
8
+ export type PayloadEnvelopeInputState =
9
+ | {
10
+ hasPayload: false;
11
+ hasAllData: false;
12
+ hasComputedAllData: false;
13
+ }
14
+ | {
15
+ hasPayload: false;
16
+ hasAllData: true;
17
+ hasComputedAllData: boolean;
18
+ }
19
+ | {
20
+ hasPayload: true;
21
+ hasAllData: false;
22
+ hasComputedAllData: false;
23
+ payloadEnvelope: gloas.SignedExecutionPayloadEnvelope;
24
+ payloadEnvelopeSource: SourceMeta;
25
+ }
26
+ | {
27
+ hasPayload: true;
28
+ hasAllData: true;
29
+ hasComputedAllData: boolean;
30
+ payloadEnvelope: gloas.SignedExecutionPayloadEnvelope;
31
+ payloadEnvelopeSource: SourceMeta;
32
+ timeCompleteSec: number;
33
+ };
34
+
35
+ type PromiseParts<T> = {
36
+ promise: Promise<T>;
37
+ resolve: (value: T) => void;
38
+ reject: (e: Error) => void;
39
+ };
40
+
41
+ function createPromise<T>(): PromiseParts<T> {
42
+ let resolve!: (value: T) => void;
43
+ let reject!: (e: Error) => void;
44
+ const promise = new Promise<T>((_resolve, _reject) => {
45
+ resolve = _resolve;
46
+ reject = _reject;
47
+ });
48
+ return {promise, resolve, reject};
49
+ }
50
+
51
+ /**
52
+ * Tracks bid + payload envelope + data columns for a Gloas block.
53
+ *
54
+ * Created during block import from signedExecutionPayloadBid in block body.
55
+ * Always has bid (required for creation).
56
+ *
57
+ * Completion requires: payload envelope + all sampled columns
58
+ */
59
+ export class PayloadEnvelopeInput {
60
+ readonly blockRootHex: RootHex;
61
+ readonly slot: Slot;
62
+ readonly proposerIndex: ValidatorIndex;
63
+ readonly bid: gloas.ExecutionPayloadBid;
64
+ readonly versionedHashes: VersionedHashes;
65
+
66
+ private columnsCache = new Map<ColumnIndex, ColumnWithSource>();
67
+
68
+ private readonly sampledColumns: ColumnIndex[];
69
+ private readonly custodyColumns: ColumnIndex[];
70
+
71
+ private timeCreatedSec: number;
72
+
73
+ private readonly payloadEnvelopeDataPromise: PromiseParts<gloas.SignedExecutionPayloadEnvelope>;
74
+ private readonly columnsDataPromise: PromiseParts<DataColumnSidecars>;
75
+
76
+ state: PayloadEnvelopeInputState;
77
+
78
+ private constructor(props: {
79
+ blockRootHex: RootHex;
80
+ slot: Slot;
81
+ proposerIndex: ValidatorIndex;
82
+ bid: gloas.ExecutionPayloadBid;
83
+ sampledColumns: ColumnIndex[];
84
+ custodyColumns: ColumnIndex[];
85
+ timeCreatedSec: number;
86
+ }) {
87
+ this.blockRootHex = props.blockRootHex;
88
+ this.slot = props.slot;
89
+ this.proposerIndex = props.proposerIndex;
90
+ this.bid = props.bid;
91
+ this.versionedHashes = props.bid.blobKzgCommitments.map(kzgCommitmentToVersionedHash);
92
+ this.sampledColumns = props.sampledColumns;
93
+ this.custodyColumns = props.custodyColumns;
94
+ this.timeCreatedSec = props.timeCreatedSec;
95
+ this.payloadEnvelopeDataPromise = createPromise();
96
+ this.columnsDataPromise = createPromise();
97
+
98
+ const noBlobs = props.bid.blobKzgCommitments.length === 0;
99
+ const noSampledColumns = props.sampledColumns.length === 0;
100
+ const hasAllData = noBlobs || noSampledColumns;
101
+
102
+ if (hasAllData) {
103
+ this.state = {hasPayload: false, hasAllData: true, hasComputedAllData: true};
104
+ this.columnsDataPromise.resolve(this.getSampledColumns());
105
+ } else {
106
+ this.state = {hasPayload: false, hasAllData: false, hasComputedAllData: false};
107
+ }
108
+ }
109
+
110
+ static createFromBlock(props: CreateFromBlockProps): PayloadEnvelopeInput {
111
+ const bid = (props.block.message.body as gloas.BeaconBlockBody).signedExecutionPayloadBid.message;
112
+ return new PayloadEnvelopeInput({
113
+ blockRootHex: props.blockRootHex,
114
+ slot: props.block.message.slot,
115
+ proposerIndex: props.block.message.proposerIndex,
116
+ bid,
117
+ sampledColumns: props.sampledColumns,
118
+ custodyColumns: props.custodyColumns,
119
+ timeCreatedSec: props.timeCreatedSec,
120
+ });
121
+ }
122
+
123
+ getBid(): gloas.ExecutionPayloadBid {
124
+ return this.bid;
125
+ }
126
+
127
+ getBuilderIndex(): ValidatorIndex {
128
+ return this.bid.builderIndex;
129
+ }
130
+
131
+ getBlockHashHex(): RootHex {
132
+ return toRootHex(this.bid.blockHash);
133
+ }
134
+
135
+ getBlobKzgCommitments(): deneb.BlobKzgCommitments {
136
+ return this.bid.blobKzgCommitments;
137
+ }
138
+
139
+ addPayloadEnvelope(props: AddPayloadEnvelopeProps): void {
140
+ if (this.state.hasPayload) {
141
+ throw new Error(`Payload envelope already set for block ${this.blockRootHex}`);
142
+ }
143
+ if (toRootHex(props.envelope.message.beaconBlockRoot) !== this.blockRootHex) {
144
+ throw new Error("Payload envelope beacon_block_root mismatch");
145
+ }
146
+
147
+ const source: SourceMeta = {
148
+ source: props.source,
149
+ seenTimestampSec: props.seenTimestampSec,
150
+ peerIdStr: props.peerIdStr,
151
+ };
152
+
153
+ if (this.state.hasAllData) {
154
+ // Complete state
155
+ this.state = {
156
+ hasPayload: true,
157
+ hasAllData: true,
158
+ hasComputedAllData: this.state.hasComputedAllData,
159
+ payloadEnvelope: props.envelope,
160
+ payloadEnvelopeSource: source,
161
+ timeCompleteSec: props.seenTimestampSec,
162
+ };
163
+ this.payloadEnvelopeDataPromise.resolve(props.envelope);
164
+ } else {
165
+ // Has payload, waiting for columns
166
+ this.state = {
167
+ hasPayload: true,
168
+ hasAllData: false,
169
+ hasComputedAllData: false,
170
+ payloadEnvelope: props.envelope,
171
+ payloadEnvelopeSource: source,
172
+ };
173
+ }
174
+ }
175
+
176
+ addColumn(columnWithSource: ColumnWithSource): void {
177
+ const {columnSidecar, seenTimestampSec} = columnWithSource;
178
+ this.columnsCache.set(columnSidecar.index, columnWithSource);
179
+
180
+ const sampledColumns = this.getSampledColumns();
181
+ const hasAllData =
182
+ // already hasAllData
183
+ this.state.hasAllData ||
184
+ // has all sampled columns
185
+ sampledColumns.length === this.sampledColumns.length ||
186
+ // has enough columns to reconstruct the rest
187
+ this.columnsCache.size >= NUMBER_OF_COLUMNS / 2;
188
+
189
+ const hasComputedAllData =
190
+ // has all sampled columns
191
+ sampledColumns.length === this.sampledColumns.length;
192
+
193
+ if (!hasAllData) {
194
+ return;
195
+ }
196
+
197
+ if (hasComputedAllData) {
198
+ this.columnsDataPromise.resolve(sampledColumns);
199
+ }
200
+
201
+ if (this.state.hasPayload) {
202
+ // Complete state
203
+ this.state = {
204
+ hasPayload: true,
205
+ hasAllData: true,
206
+ hasComputedAllData: hasComputedAllData || this.state.hasComputedAllData,
207
+ payloadEnvelope: this.state.payloadEnvelope,
208
+ payloadEnvelopeSource: this.state.payloadEnvelopeSource,
209
+ timeCompleteSec: seenTimestampSec,
210
+ };
211
+ this.payloadEnvelopeDataPromise.resolve(this.state.payloadEnvelope);
212
+ } else {
213
+ // No payload yet, all data ready
214
+ this.state = {
215
+ hasPayload: false,
216
+ hasAllData: true,
217
+ hasComputedAllData: hasComputedAllData || this.state.hasComputedAllData,
218
+ };
219
+ }
220
+ }
221
+
222
+ getVersionedHashes(): VersionedHashes {
223
+ return this.versionedHashes;
224
+ }
225
+
226
+ hasPayloadEnvelope(): boolean {
227
+ return this.state.hasPayload;
228
+ }
229
+
230
+ getPayloadEnvelope(): gloas.SignedExecutionPayloadEnvelope {
231
+ if (!this.state.hasPayload) throw new Error("Payload envelope not set");
232
+ return this.state.payloadEnvelope;
233
+ }
234
+
235
+ getPayloadEnvelopeSource(): SourceMeta {
236
+ if (!this.state.hasPayload) throw new Error("Payload envelope source not set");
237
+ return this.state.payloadEnvelopeSource;
238
+ }
239
+
240
+ getSampledColumns(): gloas.DataColumnSidecars {
241
+ const columns: gloas.DataColumnSidecars = [];
242
+ for (const index of this.sampledColumns) {
243
+ const column = this.columnsCache.get(index);
244
+ if (column) {
245
+ columns.push(column.columnSidecar);
246
+ }
247
+ }
248
+ return columns;
249
+ }
250
+
251
+ getSampledColumnsWithSource(): ColumnWithSource[] {
252
+ const columns: ColumnWithSource[] = [];
253
+ for (const index of this.sampledColumns) {
254
+ const column = this.columnsCache.get(index);
255
+ if (column) {
256
+ columns.push(column);
257
+ }
258
+ }
259
+ return columns;
260
+ }
261
+
262
+ getCustodyColumns(): gloas.DataColumnSidecars {
263
+ const columns: gloas.DataColumnSidecars = [];
264
+ for (const index of this.custodyColumns) {
265
+ const column = this.columnsCache.get(index);
266
+ if (column) {
267
+ columns.push(column.columnSidecar);
268
+ }
269
+ }
270
+ return columns;
271
+ }
272
+
273
+ hasComputedAllData(): boolean {
274
+ return this.state.hasComputedAllData;
275
+ }
276
+
277
+ waitForComputedAllData(timeout: number, signal?: AbortSignal): Promise<DataColumnSidecars> {
278
+ if (this.state.hasComputedAllData) {
279
+ return Promise.resolve(this.getSampledColumns());
280
+ }
281
+ return withTimeout(() => this.columnsDataPromise.promise, timeout, signal);
282
+ }
283
+
284
+ getTimeCreated(): number {
285
+ return this.timeCreatedSec;
286
+ }
287
+
288
+ getTimeComplete(): number {
289
+ if (!this.state.hasPayload || !this.state.hasAllData) throw new Error("Not yet complete");
290
+ return this.state.timeCompleteSec;
291
+ }
292
+
293
+ isComplete(): boolean {
294
+ return this.state.hasPayload && this.state.hasAllData;
295
+ }
296
+
297
+ async waitForData(): Promise<gloas.SignedExecutionPayloadEnvelope> {
298
+ return this.payloadEnvelopeDataPromise.promise;
299
+ }
300
+
301
+ getSerializedCacheKeys(): object[] {
302
+ const objects: object[] = [];
303
+
304
+ if (this.state.hasPayload) {
305
+ objects.push(this.state.payloadEnvelope);
306
+ }
307
+
308
+ for (const {columnSidecar} of this.columnsCache.values()) {
309
+ objects.push(columnSidecar);
310
+ }
311
+
312
+ return objects;
313
+ }
314
+
315
+ getLogMeta(): {
316
+ slot: number;
317
+ blockRoot: string;
318
+ hasPayload: boolean;
319
+ hasAllData: boolean;
320
+ hasComputedAllData: boolean;
321
+ isComplete: boolean;
322
+ columnsCount: number;
323
+ sampledColumnsCount: number;
324
+ } {
325
+ return {
326
+ slot: this.slot,
327
+ blockRoot: this.blockRootHex,
328
+ hasPayload: this.state.hasPayload,
329
+ hasAllData: this.state.hasAllData,
330
+ hasComputedAllData: this.state.hasComputedAllData,
331
+ isComplete: this.isComplete(),
332
+ columnsCount: this.columnsCache.size,
333
+ sampledColumnsCount: this.sampledColumns.length,
334
+ };
335
+ }
336
+ }
@@ -0,0 +1,33 @@
1
+ import {ForkPostGloas} from "@lodestar/params";
2
+ import {ColumnIndex, RootHex, SignedBeaconBlock, gloas} from "@lodestar/types";
3
+
4
+ export enum PayloadEnvelopeInputSource {
5
+ gossip = "gossip",
6
+ api = "api",
7
+ engine = "engine",
8
+ byRange = "req_resp_by_range",
9
+ byRoot = "req_resp_by_root",
10
+ recovery = "recovery",
11
+ }
12
+
13
+ export type SourceMeta = {
14
+ source: PayloadEnvelopeInputSource;
15
+ seenTimestampSec: number;
16
+ peerIdStr?: string;
17
+ };
18
+
19
+ export type ColumnWithSource = SourceMeta & {
20
+ columnSidecar: gloas.DataColumnSidecar;
21
+ };
22
+
23
+ export type CreateFromBlockProps = {
24
+ blockRootHex: RootHex;
25
+ block: SignedBeaconBlock<ForkPostGloas>;
26
+ sampledColumns: ColumnIndex[];
27
+ custodyColumns: ColumnIndex[];
28
+ timeCreatedSec: number;
29
+ };
30
+
31
+ export type AddPayloadEnvelopeProps = SourceMeta & {
32
+ envelope: gloas.SignedExecutionPayloadEnvelope;
33
+ };
@@ -0,0 +1,61 @@
1
+ import {Metrics} from "../../metrics/metrics.js";
2
+ import {JobItemQueue} from "../../util/queue/index.js";
3
+ import type {BeaconChain} from "../chain.js";
4
+ import {PayloadEnvelopeInput} from "../seenCache/seenPayloadEnvelopeInput.js";
5
+ import {importExecutionPayload} from "./importExecutionPayload.js";
6
+ import {ImportPayloadOpts} from "./types.js";
7
+
8
+ // TODO GLOAS: Set to be equal to DEFAULT_MAX_PENDING_UNFINALIZED_PAYLOAD_ENVELOPE_WRITES for now
9
+ const QUEUE_MAX_LENGTH = 16;
10
+
11
+ enum PayloadEnvelopeImportStatus {
12
+ queued = "queued",
13
+ importing = "importing",
14
+ imported = "imported",
15
+ }
16
+
17
+ /**
18
+ * PayloadEnvelopeProcessor processes payload envelope jobs in a queued fashion, one after the other.
19
+ */
20
+ export class PayloadEnvelopeProcessor {
21
+ readonly jobQueue: JobItemQueue<[PayloadEnvelopeInput, ImportPayloadOpts], void>;
22
+ private readonly importStatus = new WeakMap<PayloadEnvelopeInput, PayloadEnvelopeImportStatus>();
23
+
24
+ constructor(chain: BeaconChain, metrics: Metrics | null, signal: AbortSignal) {
25
+ this.jobQueue = new JobItemQueue<[PayloadEnvelopeInput, ImportPayloadOpts], void>(
26
+ (payloadInput, opts) => {
27
+ this.importStatus.set(payloadInput, PayloadEnvelopeImportStatus.importing);
28
+ return importExecutionPayload.call(chain, payloadInput, opts);
29
+ },
30
+ {maxLength: QUEUE_MAX_LENGTH, noYieldIfOneItem: true, signal},
31
+ metrics?.payloadEnvelopeProcessorQueue ?? undefined
32
+ );
33
+ }
34
+
35
+ async processPayloadEnvelopeJob(payloadInput: PayloadEnvelopeInput, opts: ImportPayloadOpts = {}): Promise<void> {
36
+ if (!payloadInput.isComplete()) {
37
+ return;
38
+ }
39
+
40
+ if (this.importStatus.get(payloadInput) !== undefined) {
41
+ return;
42
+ }
43
+
44
+ await this.jobQueue.waitForSpace();
45
+
46
+ // Re-check after await, as another call may have queued this payload.
47
+ if (this.importStatus.get(payloadInput) !== undefined) {
48
+ return;
49
+ }
50
+
51
+ this.importStatus.set(payloadInput, PayloadEnvelopeImportStatus.queued);
52
+
53
+ try {
54
+ await this.jobQueue.push(payloadInput, opts);
55
+ this.importStatus.set(payloadInput, PayloadEnvelopeImportStatus.imported);
56
+ } catch (e) {
57
+ this.importStatus.delete(payloadInput);
58
+ throw e;
59
+ }
60
+ }
61
+ }
@@ -41,6 +41,14 @@ export enum BlobSidecarValidation {
41
41
  Full,
42
42
  }
43
43
 
44
+ export type ImportPayloadOpts = {
45
+ /**
46
+ * Set to true if envelope signature was already verified (e.g., during gossip/API validation).
47
+ * When false/undefined, signature will be verified during import.
48
+ */
49
+ validSignature?: boolean;
50
+ };
51
+
44
52
  export type ImportBlockOpts = {
45
53
  /**
46
54
  * TEMP: Review if this is safe, Lighthouse always imports attestations even in finalized sync.