@aztec/blob-client 0.0.1-commit.001888fc

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 (96) hide show
  1. package/README.md +71 -0
  2. package/dest/archive/blobscan_archive_client.d.ts +146 -0
  3. package/dest/archive/blobscan_archive_client.d.ts.map +1 -0
  4. package/dest/archive/blobscan_archive_client.js +138 -0
  5. package/dest/archive/config.d.ts +7 -0
  6. package/dest/archive/config.d.ts.map +1 -0
  7. package/dest/archive/config.js +11 -0
  8. package/dest/archive/factory.d.ts +4 -0
  9. package/dest/archive/factory.d.ts.map +1 -0
  10. package/dest/archive/factory.js +7 -0
  11. package/dest/archive/index.d.ts +3 -0
  12. package/dest/archive/index.d.ts.map +1 -0
  13. package/dest/archive/index.js +2 -0
  14. package/dest/archive/instrumentation.d.ts +11 -0
  15. package/dest/archive/instrumentation.d.ts.map +1 -0
  16. package/dest/archive/instrumentation.js +33 -0
  17. package/dest/archive/interface.d.ts +13 -0
  18. package/dest/archive/interface.d.ts.map +1 -0
  19. package/dest/archive/interface.js +1 -0
  20. package/dest/blobstore/blob_store_test_suite.d.ts +3 -0
  21. package/dest/blobstore/blob_store_test_suite.d.ts.map +1 -0
  22. package/dest/blobstore/blob_store_test_suite.js +133 -0
  23. package/dest/blobstore/index.d.ts +3 -0
  24. package/dest/blobstore/index.d.ts.map +1 -0
  25. package/dest/blobstore/index.js +2 -0
  26. package/dest/blobstore/interface.d.ts +12 -0
  27. package/dest/blobstore/interface.d.ts.map +1 -0
  28. package/dest/blobstore/interface.js +1 -0
  29. package/dest/blobstore/memory_blob_store.d.ts +8 -0
  30. package/dest/blobstore/memory_blob_store.d.ts.map +1 -0
  31. package/dest/blobstore/memory_blob_store.js +24 -0
  32. package/dest/client/bin/index.d.ts +3 -0
  33. package/dest/client/bin/index.d.ts.map +1 -0
  34. package/dest/client/bin/index.js +30 -0
  35. package/dest/client/config.d.ts +56 -0
  36. package/dest/client/config.d.ts.map +1 -0
  37. package/dest/client/config.js +65 -0
  38. package/dest/client/factory.d.ts +40 -0
  39. package/dest/client/factory.d.ts.map +1 -0
  40. package/dest/client/factory.js +56 -0
  41. package/dest/client/http.d.ts +85 -0
  42. package/dest/client/http.d.ts.map +1 -0
  43. package/dest/client/http.js +620 -0
  44. package/dest/client/index.d.ts +6 -0
  45. package/dest/client/index.d.ts.map +1 -0
  46. package/dest/client/index.js +5 -0
  47. package/dest/client/interface.d.ts +39 -0
  48. package/dest/client/interface.d.ts.map +1 -0
  49. package/dest/client/interface.js +1 -0
  50. package/dest/client/local.d.ts +13 -0
  51. package/dest/client/local.d.ts.map +1 -0
  52. package/dest/client/local.js +19 -0
  53. package/dest/client/tests.d.ts +11 -0
  54. package/dest/client/tests.d.ts.map +1 -0
  55. package/dest/client/tests.js +51 -0
  56. package/dest/encoding/index.d.ts +15 -0
  57. package/dest/encoding/index.d.ts.map +1 -0
  58. package/dest/encoding/index.js +19 -0
  59. package/dest/filestore/factory.d.ts +50 -0
  60. package/dest/filestore/factory.d.ts.map +1 -0
  61. package/dest/filestore/factory.js +67 -0
  62. package/dest/filestore/filestore_blob_client.d.ts +67 -0
  63. package/dest/filestore/filestore_blob_client.d.ts.map +1 -0
  64. package/dest/filestore/filestore_blob_client.js +115 -0
  65. package/dest/filestore/healthcheck.d.ts +5 -0
  66. package/dest/filestore/healthcheck.d.ts.map +1 -0
  67. package/dest/filestore/healthcheck.js +3 -0
  68. package/dest/filestore/index.d.ts +3 -0
  69. package/dest/filestore/index.d.ts.map +1 -0
  70. package/dest/filestore/index.js +2 -0
  71. package/package.json +94 -0
  72. package/src/archive/blobscan_archive_client.ts +176 -0
  73. package/src/archive/config.ts +14 -0
  74. package/src/archive/factory.ts +11 -0
  75. package/src/archive/fixtures/blobscan_get_blob_data.json +1 -0
  76. package/src/archive/fixtures/blobscan_get_block.json +56 -0
  77. package/src/archive/index.ts +2 -0
  78. package/src/archive/instrumentation.ts +50 -0
  79. package/src/archive/interface.ts +9 -0
  80. package/src/blobstore/blob_store_test_suite.ts +110 -0
  81. package/src/blobstore/index.ts +2 -0
  82. package/src/blobstore/interface.ts +12 -0
  83. package/src/blobstore/memory_blob_store.ts +31 -0
  84. package/src/client/bin/index.ts +35 -0
  85. package/src/client/config.ts +136 -0
  86. package/src/client/factory.ts +93 -0
  87. package/src/client/http.ts +751 -0
  88. package/src/client/index.ts +5 -0
  89. package/src/client/interface.ts +40 -0
  90. package/src/client/local.ts +30 -0
  91. package/src/client/tests.ts +62 -0
  92. package/src/encoding/index.ts +21 -0
  93. package/src/filestore/factory.ts +145 -0
  94. package/src/filestore/filestore_blob_client.ts +149 -0
  95. package/src/filestore/healthcheck.ts +5 -0
  96. package/src/filestore/index.ts +2 -0
@@ -0,0 +1,751 @@
1
+ import { Blob, type BlobJson, computeEthVersionedBlobHash } from '@aztec/blob-lib';
2
+ import { makeL1HttpTransport } from '@aztec/ethereum/client';
3
+ import { shuffle } from '@aztec/foundation/array';
4
+ import { type Logger, createLogger } from '@aztec/foundation/log';
5
+ import { makeBackoff, retry } from '@aztec/foundation/retry';
6
+ import { bufferToHex, hexToBuffer } from '@aztec/foundation/string';
7
+
8
+ import { type RpcBlock, createPublicClient } from 'viem';
9
+
10
+ import { createBlobArchiveClient } from '../archive/factory.js';
11
+ import type { BlobArchiveClient } from '../archive/interface.js';
12
+ import type { FileStoreBlobClient } from '../filestore/filestore_blob_client.js';
13
+ import { DEFAULT_HEALTHCHECK_UPLOAD_INTERVAL_MINUTES } from '../filestore/healthcheck.js';
14
+ import { type BlobClientConfig, getBlobClientConfigFromEnv } from './config.js';
15
+ import type { BlobClientInterface, GetBlobSidecarOptions } from './interface.js';
16
+
17
+ export class HttpBlobClient implements BlobClientInterface {
18
+ protected readonly log: Logger;
19
+ protected readonly config: BlobClientConfig;
20
+ protected readonly archiveClient: BlobArchiveClient | undefined;
21
+ protected readonly fetch: typeof fetch;
22
+ protected readonly fileStoreClients: FileStoreBlobClient[];
23
+ protected readonly fileStoreUploadClient: FileStoreBlobClient | undefined;
24
+
25
+ private disabled = false;
26
+ private healthcheckUploadIntervalId?: NodeJS.Timeout;
27
+
28
+ /** Cached beacon genesis time (seconds since Unix epoch). Fetched once at startup. */
29
+ private beaconGenesisTime?: bigint;
30
+ /** Cached beacon slot duration in seconds. Fetched once at startup. */
31
+ private beaconSecondsPerSlot?: number;
32
+
33
+ constructor(
34
+ config?: BlobClientConfig,
35
+ private readonly opts: {
36
+ logger?: Logger;
37
+ archiveClient?: BlobArchiveClient;
38
+ fileStoreClients?: FileStoreBlobClient[];
39
+ fileStoreUploadClient?: FileStoreBlobClient;
40
+ /** Callback fired when blobs are successfully fetched from any source */
41
+ onBlobsFetched?: (blobs: Blob[]) => void;
42
+ } = {},
43
+ ) {
44
+ this.config = config ?? getBlobClientConfigFromEnv();
45
+ this.archiveClient = opts.archiveClient ?? createBlobArchiveClient(this.config);
46
+ this.log = opts.logger ?? createLogger('blob-client:client');
47
+ this.fileStoreClients = opts.fileStoreClients ?? [];
48
+ this.fileStoreUploadClient = opts.fileStoreUploadClient;
49
+
50
+ if (this.fileStoreUploadClient && !opts.onBlobsFetched) {
51
+ this.opts.onBlobsFetched = blobs => {
52
+ this.uploadBlobsToFileStore(blobs);
53
+ };
54
+ }
55
+
56
+ this.fetch = async (...args: Parameters<typeof fetch>): Promise<Response> => {
57
+ return await retry(
58
+ () => fetch(...args),
59
+ // eslint-disable-next-line @typescript-eslint/no-base-to-string
60
+ `Fetching ${args[0]}`,
61
+ makeBackoff([1, 1, 3]),
62
+ this.log,
63
+ /*failSilently=*/ true,
64
+ );
65
+ };
66
+ }
67
+
68
+ /**
69
+ * Upload fetched blobs to filestore (fire-and-forget).
70
+ * Called automatically when blobs are fetched from any source.
71
+ */
72
+ private uploadBlobsToFileStore(blobs: Blob[]): void {
73
+ if (!this.fileStoreUploadClient) {
74
+ return;
75
+ }
76
+
77
+ void this.fileStoreUploadClient.saveBlobs(blobs, true).catch(err => {
78
+ this.log.warn(`Failed to upload ${blobs.length} blobs to filestore`, err);
79
+ });
80
+ }
81
+
82
+ /**
83
+ * Disables or enables blob storage operations.
84
+ * When disabled, getBlobSidecar returns empty arrays and sendBlobsToFilestore returns false.
85
+ * Useful for testing scenarios where blob storage failure needs to be simulated.
86
+ * @param value - True to disable blob storage, false to enable
87
+ */
88
+ public setDisabled(value: boolean): void {
89
+ this.disabled = value;
90
+ this.log.info(`Blob storage ${value ? 'disabled' : 'enabled'}`);
91
+ }
92
+
93
+ public async testSources() {
94
+ const { l1ConsensusHostUrls } = this.config;
95
+ const archiveUrl = this.archiveClient?.getBaseUrl();
96
+ this.log.info(`Testing configured blob sources`, { l1ConsensusHostUrls, archiveUrl });
97
+
98
+ let successfulSourceCount = 0;
99
+
100
+ if (l1ConsensusHostUrls && l1ConsensusHostUrls.length > 0) {
101
+ for (let l1ConsensusHostIndex = 0; l1ConsensusHostIndex < l1ConsensusHostUrls.length; l1ConsensusHostIndex++) {
102
+ const l1ConsensusHostUrl = l1ConsensusHostUrls[l1ConsensusHostIndex];
103
+ try {
104
+ const { url, ...options } = getBeaconNodeFetchOptions(
105
+ `${l1ConsensusHostUrl}/eth/v1/beacon/headers`,
106
+ this.config,
107
+ l1ConsensusHostIndex,
108
+ );
109
+ const res = await this.fetch(url, options);
110
+ if (res.ok) {
111
+ this.log.info(`L1 consensus host is reachable`, { l1ConsensusHostUrl });
112
+ successfulSourceCount++;
113
+ } else {
114
+ this.log.error(`Failure reaching L1 consensus host: ${res.statusText} (${res.status})`, {
115
+ l1ConsensusHostUrl,
116
+ });
117
+ }
118
+ } catch (err) {
119
+ this.log.error(`Error reaching L1 consensus host`, err, { l1ConsensusHostUrl });
120
+ }
121
+ }
122
+ } else {
123
+ this.log.warn('No L1 consensus host urls configured');
124
+ }
125
+
126
+ if (this.archiveClient) {
127
+ try {
128
+ const latest = await this.archiveClient.getLatestBlock();
129
+ this.log.info(`Archive client is reachable and synced to L1 block ${latest.number}`, { latest, archiveUrl });
130
+ successfulSourceCount++;
131
+ } catch (err) {
132
+ this.log.error(`Error reaching archive client`, err, { archiveUrl });
133
+ }
134
+ } else {
135
+ this.log.warn('No archive client configured');
136
+ }
137
+
138
+ if (this.fileStoreClients.length > 0) {
139
+ for (const fileStoreClient of this.fileStoreClients) {
140
+ try {
141
+ const accessible = await fileStoreClient.testConnection();
142
+ if (accessible) {
143
+ this.log.info(`FileStore is reachable`, { url: fileStoreClient.getBaseUrl() });
144
+ successfulSourceCount++;
145
+ } else {
146
+ this.log.warn(`FileStore is not accessible`, { url: fileStoreClient.getBaseUrl() });
147
+ }
148
+ } catch (err) {
149
+ this.log.error(`Error reaching filestore`, err, { url: fileStoreClient.getBaseUrl() });
150
+ }
151
+ }
152
+ }
153
+
154
+ if (successfulSourceCount === 0) {
155
+ if (this.config.blobAllowEmptySources) {
156
+ this.log.warn('No blob sources are reachable');
157
+ } else {
158
+ throw new Error('No blob sources are reachable');
159
+ }
160
+ }
161
+ }
162
+
163
+ public async sendBlobsToFilestore(blobs: Blob[]): Promise<boolean> {
164
+ if (this.disabled) {
165
+ this.log.warn('Blob storage is disabled, not uploading blobs');
166
+ return false;
167
+ }
168
+
169
+ if (!this.fileStoreUploadClient) {
170
+ this.log.verbose('No filestore upload configured');
171
+ return false;
172
+ }
173
+
174
+ this.log.verbose(`Uploading ${blobs.length} blobs to filestore`);
175
+ try {
176
+ await this.fileStoreUploadClient.saveBlobs(blobs, true);
177
+ return true;
178
+ } catch (err) {
179
+ this.log.error('Failed to upload blobs to filestore', err);
180
+ return false;
181
+ }
182
+ }
183
+
184
+ /**
185
+ * Get the blob sidecar
186
+ *
187
+ * If requesting from the blob client, we send the blobkHash
188
+ * If requesting from the beacon node, we send the slot number
189
+ *
190
+ * Source ordering depends on sync state:
191
+ * - Historical sync: blob client → FileStore → L1 consensus → Archive
192
+ * - Near tip sync: blob client → FileStore → L1 consensus → FileStore (with retries) → Archive (eg blobscan)
193
+ *
194
+ * @param blockHash - The block hash
195
+ * @param blobHashes - The blob hashes to fetch
196
+ * @param opts - Options including isHistoricalSync flag
197
+ * @returns The blobs
198
+ */
199
+ public async getBlobSidecar(
200
+ blockHash: `0x${string}`,
201
+ blobHashes: Buffer[],
202
+ opts?: GetBlobSidecarOptions,
203
+ ): Promise<Blob[]> {
204
+ if (this.disabled) {
205
+ this.log.warn('Blob storage is disabled, returning empty blob sidecar');
206
+ return [];
207
+ }
208
+
209
+ const isHistoricalSync = opts?.isHistoricalSync ?? false;
210
+ // Accumulate blobs across sources, preserving order and handling duplicates
211
+ // resultBlobs[i] will contain the blob for blobHashes[i], or undefined if not yet found
212
+ const resultBlobs: (Blob | undefined)[] = new Array(blobHashes.length).fill(undefined);
213
+
214
+ // Helper to get missing blob hashes that we still need to fetch
215
+ const getMissingBlobHashes = (): Buffer[] =>
216
+ blobHashes
217
+ .map((bh, i) => (resultBlobs[i] === undefined ? bh : undefined))
218
+ .filter((bh): bh is Buffer => bh !== undefined);
219
+
220
+ // Return the result, ignoring any undefined ones
221
+ const getFilledBlobs = (): Blob[] => resultBlobs.filter((b): b is Blob => b !== undefined);
222
+
223
+ // Helper to fill in results from fetched blobs
224
+ const fillResults = async (fetchedBlobs: BlobJson[]): Promise<Blob[]> => {
225
+ const blobs = await processFetchedBlobs(fetchedBlobs, blobHashes, this.log);
226
+ // Fill in any missing positions with matching blobs
227
+ for (let i = 0; i < blobHashes.length; i++) {
228
+ if (resultBlobs[i] === undefined) {
229
+ resultBlobs[i] = blobs[i];
230
+ }
231
+ }
232
+ return getFilledBlobs();
233
+ };
234
+
235
+ // Fire callback when returning blobs (fire-and-forget)
236
+ const returnWithCallback = (blobs: Blob[]): Blob[] => {
237
+ if (blobs.length > 0 && this.opts.onBlobsFetched) {
238
+ void Promise.resolve().then(() => this.opts.onBlobsFetched!(blobs));
239
+ }
240
+ return blobs;
241
+ };
242
+
243
+ const { l1ConsensusHostUrls } = this.config;
244
+
245
+ const ctx = { blockHash, blobHashes: blobHashes.map(bufferToHex) };
246
+
247
+ // Try filestore (quick, no retries) - useful for both historical and near-tip sync
248
+ if (this.fileStoreClients.length > 0 && getMissingBlobHashes().length > 0) {
249
+ await this.tryFileStores(getMissingBlobHashes, fillResults, ctx);
250
+ if (getMissingBlobHashes().length === 0) {
251
+ return returnWithCallback(getFilledBlobs());
252
+ }
253
+ }
254
+
255
+ const missingAfterSink = getMissingBlobHashes();
256
+ if (missingAfterSink.length > 0 && l1ConsensusHostUrls && l1ConsensusHostUrls.length > 0) {
257
+ // The beacon api can query by slot number, so we get that first
258
+ const consensusCtx = { l1ConsensusHostUrls, ...ctx };
259
+ this.log.trace(`Attempting to get slot number for block hash`, consensusCtx);
260
+ const slotNumber = await this.getSlotNumber(blockHash, opts?.parentBeaconBlockRoot, opts?.l1BlockTimestamp);
261
+ this.log.debug(`Got slot number ${slotNumber} from consensus host for querying blobs`, consensusCtx);
262
+
263
+ if (slotNumber) {
264
+ let l1ConsensusHostUrl: string;
265
+ for (let l1ConsensusHostIndex = 0; l1ConsensusHostIndex < l1ConsensusHostUrls.length; l1ConsensusHostIndex++) {
266
+ const missingHashes = getMissingBlobHashes();
267
+ if (missingHashes.length === 0) {
268
+ break;
269
+ }
270
+
271
+ l1ConsensusHostUrl = l1ConsensusHostUrls[l1ConsensusHostIndex];
272
+ this.log.trace(`Attempting to get ${missingHashes.length} blobs from consensus host`, {
273
+ slotNumber,
274
+ l1ConsensusHostUrl,
275
+ ...ctx,
276
+ });
277
+ const blobs = await this.getBlobsFromHost(
278
+ l1ConsensusHostUrl,
279
+ slotNumber,
280
+ l1ConsensusHostIndex,
281
+ getMissingBlobHashes(),
282
+ );
283
+ const result = await fillResults(blobs);
284
+ this.log.debug(
285
+ `Got ${blobs.length} blobs from consensus host (total: ${result.length}/${blobHashes.length})`,
286
+ { slotNumber, l1ConsensusHostUrl, ...ctx },
287
+ );
288
+ if (result.length === blobHashes.length) {
289
+ return returnWithCallback(result);
290
+ }
291
+ }
292
+ }
293
+ }
294
+
295
+ // For near-tip sync, retry filestores with backoff (eventual consistency)
296
+ // This handles the case where blobs are still being uploaded by other validators
297
+ if (!isHistoricalSync && this.fileStoreClients.length > 0 && getMissingBlobHashes().length > 0) {
298
+ try {
299
+ await retry(
300
+ async () => {
301
+ await this.tryFileStores(getMissingBlobHashes, fillResults, ctx);
302
+ if (getMissingBlobHashes().length > 0) {
303
+ throw new Error('Still missing blobs from filestores');
304
+ }
305
+ },
306
+ 'filestore blob retrieval',
307
+ makeBackoff([1, 1, 2]),
308
+ this.log,
309
+ true, // failSilently - expected to fail during eventual consistency
310
+ );
311
+ return returnWithCallback(getFilledBlobs());
312
+ } catch {
313
+ // Exhausted retries, continue to archive fallback
314
+ }
315
+ }
316
+
317
+ const missingAfterConsensus = getMissingBlobHashes();
318
+ if (missingAfterConsensus.length > 0 && this.archiveClient) {
319
+ const archiveCtx = { archiveUrl: this.archiveClient.getBaseUrl(), ...ctx };
320
+ this.log.trace(`Attempting to get ${missingAfterConsensus.length} blobs from archive`, archiveCtx);
321
+ const allBlobs = await this.archiveClient.getBlobsFromBlock(blockHash);
322
+ if (!allBlobs) {
323
+ this.log.debug('No blobs found from archive client', archiveCtx);
324
+ } else {
325
+ this.log.trace(`Got ${allBlobs.length} blobs from archive client before filtering`, archiveCtx);
326
+ const result = await fillResults(allBlobs);
327
+ this.log.debug(
328
+ `Got ${allBlobs.length} blobs from archive client (total: ${result.length}/${blobHashes.length})`,
329
+ archiveCtx,
330
+ );
331
+ if (result.length === blobHashes.length) {
332
+ return returnWithCallback(result);
333
+ }
334
+ }
335
+ }
336
+
337
+ const result = getFilledBlobs();
338
+ if (result.length < blobHashes.length) {
339
+ this.log.warn(
340
+ `Failed to fetch all blobs for ${blockHash} from all blob sources (got ${result.length}/${blobHashes.length})`,
341
+ {
342
+ l1ConsensusHostUrls,
343
+ archiveUrl: this.archiveClient?.getBaseUrl(),
344
+ fileStoreUrls: this.fileStoreClients.map(c => c.getBaseUrl()),
345
+ },
346
+ );
347
+ }
348
+ return returnWithCallback(result);
349
+ }
350
+
351
+ /**
352
+ * Try all filestores once (shuffled for load distribution).
353
+ * @param getMissingBlobHashes - Function to get remaining blob hashes to fetch
354
+ * @param fillResults - Callback to fill in results
355
+ * @param ctx - Logging context
356
+ */
357
+ private async tryFileStores(
358
+ getMissingBlobHashes: () => Buffer[],
359
+ fillResults: (blobs: BlobJson[]) => Promise<Blob[]>,
360
+ ctx: { blockHash: string; blobHashes: string[] },
361
+ ): Promise<void> {
362
+ // Shuffle clients for load distribution
363
+ const shuffledClients = [...this.fileStoreClients];
364
+ shuffle(shuffledClients);
365
+
366
+ for (const client of shuffledClients) {
367
+ const blobHashes = getMissingBlobHashes();
368
+ if (blobHashes.length === 0) {
369
+ return; // All blobs found, no need to try more filestores
370
+ }
371
+
372
+ try {
373
+ const blobHashStrings = blobHashes.map(h => `0x${h.toString('hex')}`);
374
+ this.log.trace(`Attempting to get ${blobHashStrings.length} blobs from filestore`, {
375
+ url: client.getBaseUrl(),
376
+ ...ctx,
377
+ });
378
+ const blobs = await client.getBlobsByHashes(blobHashStrings);
379
+ if (blobs.length > 0) {
380
+ const result = await fillResults(blobs);
381
+ this.log.debug(
382
+ `Got ${blobs.length} blobs from filestore (total: ${result.length}/${ctx.blobHashes.length})`,
383
+ {
384
+ url: client.getBaseUrl(),
385
+ ...ctx,
386
+ },
387
+ );
388
+ }
389
+ } catch (err) {
390
+ this.log.warn(`Failed to fetch from filestore: ${err}`, { url: client.getBaseUrl() });
391
+ }
392
+ }
393
+ }
394
+
395
+ public async getBlobSidecarFrom(
396
+ hostUrl: string,
397
+ blockHashOrSlot: string | number,
398
+ blobHashes: Buffer[] = [],
399
+ l1ConsensusHostIndex?: number,
400
+ ): Promise<Blob[]> {
401
+ const blobs = await this.getBlobsFromHost(hostUrl, blockHashOrSlot, l1ConsensusHostIndex, blobHashes);
402
+ return (await processFetchedBlobs(blobs, blobHashes, this.log)).filter((b): b is Blob => b !== undefined);
403
+ }
404
+
405
+ public async getBlobsFromHost(
406
+ hostUrl: string,
407
+ blockHashOrSlot: string | number,
408
+ l1ConsensusHostIndex?: number,
409
+ blobHashes?: Buffer[],
410
+ ): Promise<BlobJson[]> {
411
+ try {
412
+ let res = await this.fetchBlobSidecars(hostUrl, blockHashOrSlot, l1ConsensusHostIndex, blobHashes);
413
+ if (res.ok) {
414
+ return await parseBlobJsonsFromResponse(await res.json(), this.log);
415
+ }
416
+
417
+ if (res.status === 404 && typeof blockHashOrSlot === 'number') {
418
+ const latestSlot = await this.getLatestSlotNumber(hostUrl, l1ConsensusHostIndex);
419
+ this.log.debug(`Requested L1 slot ${blockHashOrSlot} not found, trying out slots up to ${latestSlot}`, {
420
+ hostUrl,
421
+ status: res.status,
422
+ statusText: res.statusText,
423
+ });
424
+
425
+ let maxRetries = 10;
426
+ let currentSlot = blockHashOrSlot + 1;
427
+ while (res.status === 404 && maxRetries > 0 && latestSlot !== undefined && currentSlot <= latestSlot) {
428
+ this.log.debug(`Trying slot ${currentSlot}`);
429
+ res = await this.fetchBlobSidecars(hostUrl, currentSlot, l1ConsensusHostIndex, blobHashes);
430
+ if (res.ok) {
431
+ return await parseBlobJsonsFromResponse(await res.json(), this.log);
432
+ }
433
+ currentSlot++;
434
+ maxRetries--;
435
+ }
436
+ }
437
+
438
+ this.log.warn(`Unable to get blob sidecar for ${blockHashOrSlot}: ${res.statusText} (${res.status})`, {
439
+ status: res.status,
440
+ statusText: res.statusText,
441
+ body: await res.text().catch(() => 'Failed to read response body'),
442
+ });
443
+ return [];
444
+ } catch (error: any) {
445
+ this.log.warn(`Error getting blob sidecar from ${hostUrl}: ${error.message ?? error}`);
446
+ return [];
447
+ }
448
+ }
449
+
450
+ private fetchBlobSidecars(
451
+ hostUrl: string,
452
+ blockHashOrSlot: string | number,
453
+ l1ConsensusHostIndex?: number,
454
+ blobHashes?: Buffer[],
455
+ ): Promise<Response> {
456
+ let baseUrl = `${hostUrl}/eth/v1/beacon/blobs/${blockHashOrSlot}`;
457
+
458
+ if (blobHashes && blobHashes.length > 0) {
459
+ const params = new URLSearchParams();
460
+ for (const hash of blobHashes) {
461
+ params.append('versioned_hashes', `0x${hash.toString('hex')}`);
462
+ }
463
+ baseUrl += `?${params.toString()}`;
464
+ }
465
+
466
+ const { url, ...options } = getBeaconNodeFetchOptions(baseUrl, this.config, l1ConsensusHostIndex);
467
+ this.log.debug(`Fetching blob sidecar for ${blockHashOrSlot}`, { url, ...options });
468
+ return this.fetch(url, options);
469
+ }
470
+
471
+ private async getLatestSlotNumber(hostUrl: string, l1ConsensusHostIndex?: number): Promise<number | undefined> {
472
+ try {
473
+ const baseUrl = `${hostUrl}/eth/v1/beacon/headers/head`;
474
+ const { url, ...options } = getBeaconNodeFetchOptions(baseUrl, this.config, l1ConsensusHostIndex);
475
+ this.log.debug(`Fetching latest slot number`, { url, ...options });
476
+ const res = await this.fetch(url, options);
477
+ if (res.ok) {
478
+ const body = await res.json();
479
+ const slot = parseInt(body.data.header.message.slot);
480
+ if (Number.isNaN(slot)) {
481
+ this.log.error(`Failed to parse slot number from response from ${hostUrl}`, { body });
482
+ return undefined;
483
+ }
484
+ return slot;
485
+ }
486
+ } catch (err) {
487
+ this.log.error(`Error getting latest slot number from ${hostUrl}`, err);
488
+ return undefined;
489
+ }
490
+ }
491
+
492
+ /**
493
+ * Get the slot number from the consensus host
494
+ * As of eip-4788, the parentBeaconBlockRoot is included in the execution layer.
495
+ * This allows us to query the consensus layer for the slot number of the parent block, which we will then use
496
+ * to request blobs from the consensus layer.
497
+ *
498
+ * If this returns undefined, it means that we are not connected to a real consensus host, and we should
499
+ * query blobs with the blockHash.
500
+ *
501
+ * If this returns a number, then we should query blobs with the slot number
502
+ *
503
+ * @param blockHash - The block hash
504
+ * @returns The slot number
505
+ */
506
+ private async getSlotNumber(
507
+ blockHash: `0x${string}`,
508
+ parentBeaconBlockRoot?: string,
509
+ l1BlockTimestamp?: bigint,
510
+ ): Promise<number | undefined> {
511
+ const { l1ConsensusHostUrls, l1RpcUrls } = this.config;
512
+ if (!l1ConsensusHostUrls || l1ConsensusHostUrls.length === 0) {
513
+ this.log.debug('No consensus host url configured');
514
+ return undefined;
515
+ }
516
+
517
+ // Primary path: compute slot from timestamp if genesis config is cached (no network call needed)
518
+ if (
519
+ l1BlockTimestamp !== undefined &&
520
+ this.beaconGenesisTime !== undefined &&
521
+ this.beaconSecondsPerSlot !== undefined
522
+ ) {
523
+ const slot = Number((l1BlockTimestamp - this.beaconGenesisTime) / BigInt(this.beaconSecondsPerSlot));
524
+ this.log.debug(`Computed slot ${slot} from L1 block timestamp`, { l1BlockTimestamp });
525
+ return slot;
526
+ }
527
+
528
+ if (!parentBeaconBlockRoot) {
529
+ // parentBeaconBlockRoot not provided by caller — fetch it from the execution RPC
530
+ if (!l1RpcUrls || l1RpcUrls.length === 0) {
531
+ this.log.debug('No execution host url configured');
532
+ return undefined;
533
+ }
534
+
535
+ const client = createPublicClient({
536
+ transport: makeL1HttpTransport(l1RpcUrls, { timeout: this.config.l1HttpTimeoutMS }),
537
+ });
538
+ try {
539
+ const res: RpcBlock = await client.request({
540
+ method: 'eth_getBlockByHash',
541
+ params: [blockHash, /*tx flag*/ false],
542
+ });
543
+
544
+ if (res.parentBeaconBlockRoot) {
545
+ parentBeaconBlockRoot = res.parentBeaconBlockRoot;
546
+ }
547
+ } catch (err) {
548
+ this.log.error(`Error getting parent beacon block root`, err);
549
+ }
550
+ }
551
+
552
+ if (!parentBeaconBlockRoot) {
553
+ this.log.error(`No parent beacon block root found for block ${blockHash}`);
554
+ return undefined;
555
+ }
556
+
557
+ // Query beacon chain to get the slot number for that block root
558
+ let l1ConsensusHostUrl: string;
559
+ for (let l1ConsensusHostIndex = 0; l1ConsensusHostIndex < l1ConsensusHostUrls.length; l1ConsensusHostIndex++) {
560
+ l1ConsensusHostUrl = l1ConsensusHostUrls[l1ConsensusHostIndex];
561
+ try {
562
+ const { url, ...options } = getBeaconNodeFetchOptions(
563
+ `${l1ConsensusHostUrl}/eth/v1/beacon/headers/${parentBeaconBlockRoot}`,
564
+ this.config,
565
+ l1ConsensusHostIndex,
566
+ );
567
+ const res = await this.fetch(url, options);
568
+
569
+ if (res.ok) {
570
+ const body = await res.json();
571
+
572
+ // Add one to get the slot number of the original block hash
573
+ return Number(body.data.header.message.slot) + 1;
574
+ }
575
+ } catch (err) {
576
+ this.log.error(`Error getting slot number`, err);
577
+ }
578
+ }
579
+
580
+ return undefined;
581
+ }
582
+
583
+ /** @internal - exposed for testing */
584
+ public getArchiveClient(): BlobArchiveClient | undefined {
585
+ return this.archiveClient;
586
+ }
587
+
588
+ /** Returns true if this client can upload blobs to filestore. */
589
+ public canUpload(): boolean {
590
+ return this.fileStoreUploadClient !== undefined;
591
+ }
592
+
593
+ /**
594
+ * Start the blob client.
595
+ * Fetches and caches beacon genesis config for timestamp-based slot resolution,
596
+ * then uploads the initial healthcheck file (awaited) and starts periodic uploads.
597
+ */
598
+ public async start(): Promise<void> {
599
+ await this.fetchBeaconConfig();
600
+
601
+ if (!this.fileStoreUploadClient) {
602
+ return;
603
+ }
604
+
605
+ await this.fileStoreUploadClient.uploadHealthcheck();
606
+ this.log.debug('Initial healthcheck file uploaded');
607
+
608
+ this.startPeriodicHealthcheckUpload();
609
+ }
610
+
611
+ /**
612
+ * Start periodic healthcheck upload to the file store to ensure it remains available even if accidentally deleted.
613
+ */
614
+ private startPeriodicHealthcheckUpload(): void {
615
+ const intervalMs =
616
+ (this.config.blobHealthcheckUploadIntervalMinutes ?? DEFAULT_HEALTHCHECK_UPLOAD_INTERVAL_MINUTES) * 60 * 1000;
617
+
618
+ this.healthcheckUploadIntervalId = setInterval(() => {
619
+ void this.fileStoreUploadClient!.uploadHealthcheck().catch(err => {
620
+ this.log.warn('Failed to upload periodic healthcheck file', err);
621
+ });
622
+ }, intervalMs);
623
+ }
624
+
625
+ /**
626
+ * Fetches and caches beacon genesis time and slot duration from the first available consensus host.
627
+ * These static values enable timestamp-based slot resolution, eliminating the per-fetch headers call.
628
+ * Logs a warning and leaves fields undefined if all hosts fail, callers fall back gracefully.
629
+ */
630
+ private async fetchBeaconConfig(): Promise<void> {
631
+ const { l1ConsensusHostUrls } = this.config;
632
+ if (!l1ConsensusHostUrls || l1ConsensusHostUrls.length === 0) {
633
+ return;
634
+ }
635
+
636
+ for (let i = 0; i < l1ConsensusHostUrls.length; i++) {
637
+ try {
638
+ const { url: genesisUrl, ...genesisOptions } = getBeaconNodeFetchOptions(
639
+ `${l1ConsensusHostUrls[i]}/eth/v1/config/genesis`,
640
+ this.config,
641
+ i,
642
+ );
643
+ const { url: specUrl, ...specOptions } = getBeaconNodeFetchOptions(
644
+ `${l1ConsensusHostUrls[i]}/eth/v1/config/spec`,
645
+ this.config,
646
+ i,
647
+ );
648
+
649
+ const [genesisRes, specRes] = await Promise.all([
650
+ this.fetch(genesisUrl, genesisOptions),
651
+ this.fetch(specUrl, specOptions),
652
+ ]);
653
+
654
+ if (genesisRes.ok && specRes.ok) {
655
+ const genesis = await genesisRes.json();
656
+ const spec = await specRes.json();
657
+ this.beaconGenesisTime = BigInt(genesis.data.genesisTime);
658
+ this.beaconSecondsPerSlot = parseInt(spec.data.secondsPerSlot);
659
+ this.log.debug(`Fetched beacon genesis config`, {
660
+ genesisTime: this.beaconGenesisTime,
661
+ secondsPerSlot: this.beaconSecondsPerSlot,
662
+ });
663
+ return;
664
+ }
665
+ } catch (err) {
666
+ this.log.warn(`Failed to fetch beacon config from host ${l1ConsensusHostUrls[i]}`, err);
667
+ }
668
+ }
669
+ this.log.warn('Could not fetch beacon genesis config from any consensus host — will use headers call fallback');
670
+ }
671
+
672
+ /**
673
+ * Stop the blob client, clearing any periodic tasks.
674
+ */
675
+ public stop(): void {
676
+ if (this.healthcheckUploadIntervalId) {
677
+ clearInterval(this.healthcheckUploadIntervalId);
678
+ this.healthcheckUploadIntervalId = undefined;
679
+ }
680
+ }
681
+ }
682
+
683
+ async function parseBlobJsonsFromResponse(response: any, logger: Logger): Promise<BlobJson[]> {
684
+ try {
685
+ return await Promise.all((response.data as string[]).map(parseBlobJson));
686
+ } catch (err) {
687
+ logger.error(`Error parsing blob json from response`, err);
688
+ return [];
689
+ }
690
+ }
691
+
692
+ // Blobs will be in this form when requested from the blob client, or from the beacon chain via `getBlobSidecars`:
693
+ // https://ethereum.github.io/beacon-APIs/?urls.primaryName=dev#/Beacon/getBlobSidecars
694
+ // Here we attempt to parse the response data to Buffer, and check the lengths (via Blob's constructor), to avoid
695
+ // throwing an error down the line when calling Blob.fromJson().
696
+ async function parseBlobJson(rawHex: string): Promise<BlobJson> {
697
+ const blobBuffer = Buffer.from(rawHex.slice(2), 'hex');
698
+ const blob = await Blob.fromBlobBuffer(blobBuffer);
699
+ return blob.toJSON();
700
+ }
701
+
702
+ // Returns an array that maps each blob hash to the corresponding blob, or undefined if the blob is not found
703
+ // or the data does not match the commitment.
704
+ async function processFetchedBlobs(
705
+ blobs: BlobJson[],
706
+ blobHashes: Buffer[],
707
+ logger: Logger,
708
+ ): Promise<(Blob | undefined)[]> {
709
+ const requestedBlobHashes = new Set<string>(blobHashes.map(bufferToHex));
710
+ const hashToBlob = new Map<string, Blob>();
711
+ for (const blobJson of blobs) {
712
+ const hashHex = bufferToHex(computeEthVersionedBlobHash(hexToBuffer(blobJson.kzg_commitment)));
713
+ if (!requestedBlobHashes.has(hashHex) || hashToBlob.has(hashHex)) {
714
+ continue;
715
+ }
716
+
717
+ try {
718
+ const blob = await Blob.fromJson(blobJson);
719
+ hashToBlob.set(hashHex, blob);
720
+ } catch (err) {
721
+ // If the above throws, it's likely that the blob commitment does not match the hash of the blob data.
722
+ logger.error(`Error converting blob from json`, err);
723
+ }
724
+ }
725
+ return blobHashes.map(h => hashToBlob.get(bufferToHex(h)));
726
+ }
727
+
728
+ function getBeaconNodeFetchOptions(url: string, config: BlobClientConfig, l1ConsensusHostIndex?: number) {
729
+ const { l1ConsensusHostApiKeys, l1ConsensusHostApiKeyHeaders } = config;
730
+ const l1ConsensusHostApiKey =
731
+ l1ConsensusHostIndex !== undefined && l1ConsensusHostApiKeys && l1ConsensusHostApiKeys[l1ConsensusHostIndex];
732
+ const l1ConsensusHostApiKeyHeader =
733
+ l1ConsensusHostIndex !== undefined &&
734
+ l1ConsensusHostApiKeyHeaders &&
735
+ l1ConsensusHostApiKeyHeaders[l1ConsensusHostIndex];
736
+
737
+ let formattedUrl = url;
738
+ if (l1ConsensusHostApiKey && l1ConsensusHostApiKey.getValue() !== '' && !l1ConsensusHostApiKeyHeader) {
739
+ formattedUrl += `${formattedUrl.includes('?') ? '&' : '?'}key=${l1ConsensusHostApiKey.getValue()}`;
740
+ }
741
+
742
+ return {
743
+ url: formattedUrl,
744
+ ...(l1ConsensusHostApiKey &&
745
+ l1ConsensusHostApiKeyHeader && {
746
+ headers: {
747
+ [l1ConsensusHostApiKeyHeader]: l1ConsensusHostApiKey.getValue(),
748
+ },
749
+ }),
750
+ };
751
+ }