@aztec/blob-client 0.0.1-commit.0208eb9

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 +54 -0
  36. package/dest/client/config.d.ts.map +1 -0
  37. package/dest/client/config.js +60 -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 +79 -0
  42. package/dest/client/http.d.ts.map +1 -0
  43. package/dest/client/http.js +567 -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 +28 -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 +127 -0
  86. package/src/client/factory.ts +93 -0
  87. package/src/client/http.ts +662 -0
  88. package/src/client/index.ts +5 -0
  89. package/src/client/interface.ts +29 -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,567 @@
1
+ import { Blob, computeEthVersionedBlobHash } from '@aztec/blob-lib';
2
+ import { shuffle } from '@aztec/foundation/array';
3
+ import { createLogger } from '@aztec/foundation/log';
4
+ import { makeBackoff, retry } from '@aztec/foundation/retry';
5
+ import { bufferToHex, hexToBuffer } from '@aztec/foundation/string';
6
+ import { createPublicClient, fallback, http } from 'viem';
7
+ import { createBlobArchiveClient } from '../archive/factory.js';
8
+ import { DEFAULT_HEALTHCHECK_UPLOAD_INTERVAL_MINUTES } from '../filestore/healthcheck.js';
9
+ import { getBlobClientConfigFromEnv } from './config.js';
10
+ export class HttpBlobClient {
11
+ opts;
12
+ log;
13
+ config;
14
+ archiveClient;
15
+ fetch;
16
+ fileStoreClients;
17
+ fileStoreUploadClient;
18
+ disabled;
19
+ healthcheckUploadIntervalId;
20
+ constructor(config, opts = {}){
21
+ this.opts = opts;
22
+ this.disabled = false;
23
+ this.config = config ?? getBlobClientConfigFromEnv();
24
+ this.archiveClient = opts.archiveClient ?? createBlobArchiveClient(this.config);
25
+ this.log = opts.logger ?? createLogger('blob-client:client');
26
+ this.fileStoreClients = opts.fileStoreClients ?? [];
27
+ this.fileStoreUploadClient = opts.fileStoreUploadClient;
28
+ if (this.fileStoreUploadClient && !opts.onBlobsFetched) {
29
+ this.opts.onBlobsFetched = (blobs)=>{
30
+ this.uploadBlobsToFileStore(blobs);
31
+ };
32
+ }
33
+ this.fetch = async (...args)=>{
34
+ return await retry(()=>fetch(...args), // eslint-disable-next-line @typescript-eslint/no-base-to-string
35
+ `Fetching ${args[0]}`, makeBackoff([
36
+ 1,
37
+ 1,
38
+ 3
39
+ ]), this.log, /*failSilently=*/ true);
40
+ };
41
+ }
42
+ /**
43
+ * Upload fetched blobs to filestore (fire-and-forget).
44
+ * Called automatically when blobs are fetched from any source.
45
+ */ uploadBlobsToFileStore(blobs) {
46
+ if (!this.fileStoreUploadClient) {
47
+ return;
48
+ }
49
+ void this.fileStoreUploadClient.saveBlobs(blobs, true).catch((err)=>{
50
+ this.log.warn(`Failed to upload ${blobs.length} blobs to filestore`, err);
51
+ });
52
+ }
53
+ /**
54
+ * Disables or enables blob storage operations.
55
+ * When disabled, getBlobSidecar returns empty arrays and sendBlobsToFilestore returns false.
56
+ * Useful for testing scenarios where blob storage failure needs to be simulated.
57
+ * @param value - True to disable blob storage, false to enable
58
+ */ setDisabled(value) {
59
+ this.disabled = value;
60
+ this.log.info(`Blob storage ${value ? 'disabled' : 'enabled'}`);
61
+ }
62
+ async testSources() {
63
+ const { l1ConsensusHostUrls } = this.config;
64
+ const archiveUrl = this.archiveClient?.getBaseUrl();
65
+ this.log.info(`Testing configured blob sources`, {
66
+ l1ConsensusHostUrls,
67
+ archiveUrl
68
+ });
69
+ let successfulSourceCount = 0;
70
+ if (l1ConsensusHostUrls && l1ConsensusHostUrls.length > 0) {
71
+ for(let l1ConsensusHostIndex = 0; l1ConsensusHostIndex < l1ConsensusHostUrls.length; l1ConsensusHostIndex++){
72
+ const l1ConsensusHostUrl = l1ConsensusHostUrls[l1ConsensusHostIndex];
73
+ try {
74
+ const { url, ...options } = getBeaconNodeFetchOptions(`${l1ConsensusHostUrl}/eth/v1/beacon/headers`, this.config, l1ConsensusHostIndex);
75
+ const res = await this.fetch(url, options);
76
+ if (res.ok) {
77
+ this.log.info(`L1 consensus host is reachable`, {
78
+ l1ConsensusHostUrl
79
+ });
80
+ successfulSourceCount++;
81
+ } else {
82
+ this.log.error(`Failure reaching L1 consensus host: ${res.statusText} (${res.status})`, {
83
+ l1ConsensusHostUrl
84
+ });
85
+ }
86
+ } catch (err) {
87
+ this.log.error(`Error reaching L1 consensus host`, err, {
88
+ l1ConsensusHostUrl
89
+ });
90
+ }
91
+ }
92
+ } else {
93
+ this.log.warn('No L1 consensus host urls configured');
94
+ }
95
+ if (this.archiveClient) {
96
+ try {
97
+ const latest = await this.archiveClient.getLatestBlock();
98
+ this.log.info(`Archive client is reachable and synced to L1 block ${latest.number}`, {
99
+ latest,
100
+ archiveUrl
101
+ });
102
+ successfulSourceCount++;
103
+ } catch (err) {
104
+ this.log.error(`Error reaching archive client`, err, {
105
+ archiveUrl
106
+ });
107
+ }
108
+ } else {
109
+ this.log.warn('No archive client configured');
110
+ }
111
+ if (this.fileStoreClients.length > 0) {
112
+ for (const fileStoreClient of this.fileStoreClients){
113
+ try {
114
+ const accessible = await fileStoreClient.testConnection();
115
+ if (accessible) {
116
+ this.log.info(`FileStore is reachable`, {
117
+ url: fileStoreClient.getBaseUrl()
118
+ });
119
+ successfulSourceCount++;
120
+ } else {
121
+ this.log.warn(`FileStore is not accessible`, {
122
+ url: fileStoreClient.getBaseUrl()
123
+ });
124
+ }
125
+ } catch (err) {
126
+ this.log.error(`Error reaching filestore`, err, {
127
+ url: fileStoreClient.getBaseUrl()
128
+ });
129
+ }
130
+ }
131
+ }
132
+ if (successfulSourceCount === 0) {
133
+ if (this.config.blobAllowEmptySources) {
134
+ this.log.warn('No blob sources are reachable');
135
+ } else {
136
+ throw new Error('No blob sources are reachable');
137
+ }
138
+ }
139
+ }
140
+ async sendBlobsToFilestore(blobs) {
141
+ if (this.disabled) {
142
+ this.log.warn('Blob storage is disabled, not uploading blobs');
143
+ return false;
144
+ }
145
+ if (!this.fileStoreUploadClient) {
146
+ this.log.verbose('No filestore upload configured');
147
+ return false;
148
+ }
149
+ this.log.verbose(`Uploading ${blobs.length} blobs to filestore`);
150
+ try {
151
+ await this.fileStoreUploadClient.saveBlobs(blobs, true);
152
+ return true;
153
+ } catch (err) {
154
+ this.log.error('Failed to upload blobs to filestore', err);
155
+ return false;
156
+ }
157
+ }
158
+ /**
159
+ * Get the blob sidecar
160
+ *
161
+ * If requesting from the blob client, we send the blobkHash
162
+ * If requesting from the beacon node, we send the slot number
163
+ *
164
+ * Source ordering depends on sync state:
165
+ * - Historical sync: blob client → FileStore → L1 consensus → Archive
166
+ * - Near tip sync: blob client → FileStore → L1 consensus → FileStore (with retries) → Archive (eg blobscan)
167
+ *
168
+ * @param blockHash - The block hash
169
+ * @param blobHashes - The blob hashes to fetch
170
+ * @param opts - Options including isHistoricalSync flag
171
+ * @returns The blobs
172
+ */ async getBlobSidecar(blockHash, blobHashes, opts) {
173
+ if (this.disabled) {
174
+ this.log.warn('Blob storage is disabled, returning empty blob sidecar');
175
+ return [];
176
+ }
177
+ const isHistoricalSync = opts?.isHistoricalSync ?? false;
178
+ // Accumulate blobs across sources, preserving order and handling duplicates
179
+ // resultBlobs[i] will contain the blob for blobHashes[i], or undefined if not yet found
180
+ const resultBlobs = new Array(blobHashes.length).fill(undefined);
181
+ // Helper to get missing blob hashes that we still need to fetch
182
+ const getMissingBlobHashes = ()=>blobHashes.map((bh, i)=>resultBlobs[i] === undefined ? bh : undefined).filter((bh)=>bh !== undefined);
183
+ // Return the result, ignoring any undefined ones
184
+ const getFilledBlobs = ()=>resultBlobs.filter((b)=>b !== undefined);
185
+ // Helper to fill in results from fetched blobs
186
+ const fillResults = (fetchedBlobs)=>{
187
+ const blobs = processFetchedBlobs(fetchedBlobs, blobHashes, this.log);
188
+ // Fill in any missing positions with matching blobs
189
+ for(let i = 0; i < blobHashes.length; i++){
190
+ if (resultBlobs[i] === undefined) {
191
+ resultBlobs[i] = blobs[i];
192
+ }
193
+ }
194
+ return getFilledBlobs();
195
+ };
196
+ // Fire callback when returning blobs (fire-and-forget)
197
+ const returnWithCallback = (blobs)=>{
198
+ if (blobs.length > 0 && this.opts.onBlobsFetched) {
199
+ void Promise.resolve().then(()=>this.opts.onBlobsFetched(blobs));
200
+ }
201
+ return blobs;
202
+ };
203
+ const { l1ConsensusHostUrls } = this.config;
204
+ const ctx = {
205
+ blockHash,
206
+ blobHashes: blobHashes.map(bufferToHex)
207
+ };
208
+ // Try filestore (quick, no retries) - useful for both historical and near-tip sync
209
+ if (this.fileStoreClients.length > 0 && getMissingBlobHashes().length > 0) {
210
+ await this.tryFileStores(getMissingBlobHashes, fillResults, ctx);
211
+ if (getMissingBlobHashes().length === 0) {
212
+ return returnWithCallback(getFilledBlobs());
213
+ }
214
+ }
215
+ const missingAfterSink = getMissingBlobHashes();
216
+ if (missingAfterSink.length > 0 && l1ConsensusHostUrls && l1ConsensusHostUrls.length > 0) {
217
+ // The beacon api can query by slot number, so we get that first
218
+ const consensusCtx = {
219
+ l1ConsensusHostUrls,
220
+ ...ctx
221
+ };
222
+ this.log.trace(`Attempting to get slot number for block hash`, consensusCtx);
223
+ const slotNumber = await this.getSlotNumber(blockHash);
224
+ this.log.debug(`Got slot number ${slotNumber} from consensus host for querying blobs`, consensusCtx);
225
+ if (slotNumber) {
226
+ let l1ConsensusHostUrl;
227
+ for(let l1ConsensusHostIndex = 0; l1ConsensusHostIndex < l1ConsensusHostUrls.length; l1ConsensusHostIndex++){
228
+ const missingHashes = getMissingBlobHashes();
229
+ if (missingHashes.length === 0) {
230
+ break;
231
+ }
232
+ l1ConsensusHostUrl = l1ConsensusHostUrls[l1ConsensusHostIndex];
233
+ this.log.trace(`Attempting to get ${missingHashes.length} blobs from consensus host`, {
234
+ slotNumber,
235
+ l1ConsensusHostUrl,
236
+ ...ctx
237
+ });
238
+ const blobs = await this.getBlobsFromHost(l1ConsensusHostUrl, slotNumber, l1ConsensusHostIndex);
239
+ const result = fillResults(blobs);
240
+ this.log.debug(`Got ${blobs.length} blobs from consensus host (total: ${result.length}/${blobHashes.length})`, {
241
+ slotNumber,
242
+ l1ConsensusHostUrl,
243
+ ...ctx
244
+ });
245
+ if (result.length === blobHashes.length) {
246
+ return returnWithCallback(result);
247
+ }
248
+ }
249
+ }
250
+ }
251
+ // For near-tip sync, retry filestores with backoff (eventual consistency)
252
+ // This handles the case where blobs are still being uploaded by other validators
253
+ if (!isHistoricalSync && this.fileStoreClients.length > 0 && getMissingBlobHashes().length > 0) {
254
+ try {
255
+ await retry(async ()=>{
256
+ await this.tryFileStores(getMissingBlobHashes, fillResults, ctx);
257
+ if (getMissingBlobHashes().length > 0) {
258
+ throw new Error('Still missing blobs from filestores');
259
+ }
260
+ }, 'filestore blob retrieval', makeBackoff([
261
+ 1,
262
+ 1,
263
+ 2
264
+ ]), this.log, true);
265
+ return returnWithCallback(getFilledBlobs());
266
+ } catch {
267
+ // Exhausted retries, continue to archive fallback
268
+ }
269
+ }
270
+ const missingAfterConsensus = getMissingBlobHashes();
271
+ if (missingAfterConsensus.length > 0 && this.archiveClient) {
272
+ const archiveCtx = {
273
+ archiveUrl: this.archiveClient.getBaseUrl(),
274
+ ...ctx
275
+ };
276
+ this.log.trace(`Attempting to get ${missingAfterConsensus.length} blobs from archive`, archiveCtx);
277
+ const allBlobs = await this.archiveClient.getBlobsFromBlock(blockHash);
278
+ if (!allBlobs) {
279
+ this.log.debug('No blobs found from archive client', archiveCtx);
280
+ } else {
281
+ this.log.trace(`Got ${allBlobs.length} blobs from archive client before filtering`, archiveCtx);
282
+ const result = fillResults(allBlobs);
283
+ this.log.debug(`Got ${allBlobs.length} blobs from archive client (total: ${result.length}/${blobHashes.length})`, archiveCtx);
284
+ if (result.length === blobHashes.length) {
285
+ return returnWithCallback(result);
286
+ }
287
+ }
288
+ }
289
+ const result = getFilledBlobs();
290
+ if (result.length < blobHashes.length) {
291
+ this.log.warn(`Failed to fetch all blobs for ${blockHash} from all blob sources (got ${result.length}/${blobHashes.length})`, {
292
+ l1ConsensusHostUrls,
293
+ archiveUrl: this.archiveClient?.getBaseUrl(),
294
+ fileStoreUrls: this.fileStoreClients.map((c)=>c.getBaseUrl())
295
+ });
296
+ }
297
+ return returnWithCallback(result);
298
+ }
299
+ /**
300
+ * Try all filestores once (shuffled for load distribution).
301
+ * @param getMissingBlobHashes - Function to get remaining blob hashes to fetch
302
+ * @param fillResults - Callback to fill in results
303
+ * @param ctx - Logging context
304
+ */ async tryFileStores(getMissingBlobHashes, fillResults, ctx) {
305
+ // Shuffle clients for load distribution
306
+ const shuffledClients = [
307
+ ...this.fileStoreClients
308
+ ];
309
+ shuffle(shuffledClients);
310
+ for (const client of shuffledClients){
311
+ const blobHashes = getMissingBlobHashes();
312
+ if (blobHashes.length === 0) {
313
+ return; // All blobs found, no need to try more filestores
314
+ }
315
+ try {
316
+ const blobHashStrings = blobHashes.map((h)=>`0x${h.toString('hex')}`);
317
+ this.log.trace(`Attempting to get ${blobHashStrings.length} blobs from filestore`, {
318
+ url: client.getBaseUrl(),
319
+ ...ctx
320
+ });
321
+ const blobs = await client.getBlobsByHashes(blobHashStrings);
322
+ if (blobs.length > 0) {
323
+ const result = fillResults(blobs);
324
+ this.log.debug(`Got ${blobs.length} blobs from filestore (total: ${result.length}/${ctx.blobHashes.length})`, {
325
+ url: client.getBaseUrl(),
326
+ ...ctx
327
+ });
328
+ }
329
+ } catch (err) {
330
+ this.log.warn(`Failed to fetch from filestore: ${err}`, {
331
+ url: client.getBaseUrl()
332
+ });
333
+ }
334
+ }
335
+ }
336
+ async getBlobSidecarFrom(hostUrl, blockHashOrSlot, blobHashes = [], l1ConsensusHostIndex) {
337
+ const blobs = await this.getBlobsFromHost(hostUrl, blockHashOrSlot, l1ConsensusHostIndex);
338
+ return processFetchedBlobs(blobs, blobHashes, this.log).filter((b)=>b !== undefined);
339
+ }
340
+ async getBlobsFromHost(hostUrl, blockHashOrSlot, l1ConsensusHostIndex) {
341
+ try {
342
+ let res = await this.fetchBlobSidecars(hostUrl, blockHashOrSlot, l1ConsensusHostIndex);
343
+ if (res.ok) {
344
+ return parseBlobJsonsFromResponse(await res.json(), this.log);
345
+ }
346
+ if (res.status === 404 && typeof blockHashOrSlot === 'number') {
347
+ const latestSlot = await this.getLatestSlotNumber(hostUrl, l1ConsensusHostIndex);
348
+ this.log.debug(`Requested L1 slot ${blockHashOrSlot} not found, trying out slots up to ${latestSlot}`, {
349
+ hostUrl,
350
+ status: res.status,
351
+ statusText: res.statusText
352
+ });
353
+ let maxRetries = 10;
354
+ let currentSlot = blockHashOrSlot + 1;
355
+ while(res.status === 404 && maxRetries > 0 && latestSlot !== undefined && currentSlot <= latestSlot){
356
+ this.log.debug(`Trying slot ${currentSlot}`);
357
+ res = await this.fetchBlobSidecars(hostUrl, currentSlot, l1ConsensusHostIndex);
358
+ if (res.ok) {
359
+ return parseBlobJsonsFromResponse(await res.json(), this.log);
360
+ }
361
+ currentSlot++;
362
+ maxRetries--;
363
+ }
364
+ }
365
+ this.log.warn(`Unable to get blob sidecar for ${blockHashOrSlot}: ${res.statusText} (${res.status})`, {
366
+ status: res.status,
367
+ statusText: res.statusText,
368
+ body: await res.text().catch(()=>'Failed to read response body')
369
+ });
370
+ return [];
371
+ } catch (error) {
372
+ this.log.warn(`Error getting blob sidecar from ${hostUrl}: ${error.message ?? error}`);
373
+ return [];
374
+ }
375
+ }
376
+ fetchBlobSidecars(hostUrl, blockHashOrSlot, l1ConsensusHostIndex) {
377
+ const baseUrl = `${hostUrl}/eth/v1/beacon/blob_sidecars/${blockHashOrSlot}`;
378
+ const { url, ...options } = getBeaconNodeFetchOptions(baseUrl, this.config, l1ConsensusHostIndex);
379
+ this.log.debug(`Fetching blob sidecar for ${blockHashOrSlot}`, {
380
+ url,
381
+ ...options
382
+ });
383
+ return this.fetch(url, options);
384
+ }
385
+ async getLatestSlotNumber(hostUrl, l1ConsensusHostIndex) {
386
+ try {
387
+ const baseUrl = `${hostUrl}/eth/v1/beacon/headers/head`;
388
+ const { url, ...options } = getBeaconNodeFetchOptions(baseUrl, this.config, l1ConsensusHostIndex);
389
+ this.log.debug(`Fetching latest slot number`, {
390
+ url,
391
+ ...options
392
+ });
393
+ const res = await this.fetch(url, options);
394
+ if (res.ok) {
395
+ const body = await res.json();
396
+ const slot = parseInt(body.data.header.message.slot);
397
+ if (Number.isNaN(slot)) {
398
+ this.log.error(`Failed to parse slot number from response from ${hostUrl}`, {
399
+ body
400
+ });
401
+ return undefined;
402
+ }
403
+ return slot;
404
+ }
405
+ } catch (err) {
406
+ this.log.error(`Error getting latest slot number from ${hostUrl}`, err);
407
+ return undefined;
408
+ }
409
+ }
410
+ /**
411
+ * Get the slot number from the consensus host
412
+ * As of eip-4788, the parentBeaconBlockRoot is included in the execution layer.
413
+ * This allows us to query the consensus layer for the slot number of the parent block, which we will then use
414
+ * to request blobs from the consensus layer.
415
+ *
416
+ * If this returns undefined, it means that we are not connected to a real consensus host, and we should
417
+ * query blobs with the blockHash.
418
+ *
419
+ * If this returns a number, then we should query blobs with the slot number
420
+ *
421
+ * @param blockHash - The block hash
422
+ * @returns The slot number
423
+ */ async getSlotNumber(blockHash) {
424
+ const { l1ConsensusHostUrls, l1RpcUrls } = this.config;
425
+ if (!l1ConsensusHostUrls || l1ConsensusHostUrls.length === 0) {
426
+ this.log.debug('No consensus host url configured');
427
+ return undefined;
428
+ }
429
+ if (!l1RpcUrls || l1RpcUrls.length === 0) {
430
+ this.log.debug('No execution host url configured');
431
+ return undefined;
432
+ }
433
+ // Ping execution node to get the parentBeaconBlockRoot for this block
434
+ let parentBeaconBlockRoot;
435
+ const client = createPublicClient({
436
+ transport: fallback(l1RpcUrls.map((url)=>http(url, {
437
+ batch: false
438
+ })))
439
+ });
440
+ try {
441
+ const res = await client.request({
442
+ method: 'eth_getBlockByHash',
443
+ params: [
444
+ blockHash,
445
+ /*tx flag*/ false
446
+ ]
447
+ });
448
+ if (res.parentBeaconBlockRoot) {
449
+ parentBeaconBlockRoot = res.parentBeaconBlockRoot;
450
+ }
451
+ } catch (err) {
452
+ this.log.error(`Error getting parent beacon block root`, err);
453
+ }
454
+ if (!parentBeaconBlockRoot) {
455
+ this.log.error(`No parent beacon block root found for block ${blockHash}`);
456
+ return undefined;
457
+ }
458
+ // Query beacon chain to get the slot number for that block root
459
+ let l1ConsensusHostUrl;
460
+ for(let l1ConsensusHostIndex = 0; l1ConsensusHostIndex < l1ConsensusHostUrls.length; l1ConsensusHostIndex++){
461
+ l1ConsensusHostUrl = l1ConsensusHostUrls[l1ConsensusHostIndex];
462
+ try {
463
+ const { url, ...options } = getBeaconNodeFetchOptions(`${l1ConsensusHostUrl}/eth/v1/beacon/headers/${parentBeaconBlockRoot}`, this.config, l1ConsensusHostIndex);
464
+ const res = await this.fetch(url, options);
465
+ if (res.ok) {
466
+ const body = await res.json();
467
+ // Add one to get the slot number of the original block hash
468
+ return Number(body.data.header.message.slot) + 1;
469
+ }
470
+ } catch (err) {
471
+ this.log.error(`Error getting slot number`, err);
472
+ }
473
+ }
474
+ return undefined;
475
+ }
476
+ /** @internal - exposed for testing */ getArchiveClient() {
477
+ return this.archiveClient;
478
+ }
479
+ /** Returns true if this client can upload blobs to filestore. */ canUpload() {
480
+ return this.fileStoreUploadClient !== undefined;
481
+ }
482
+ /**
483
+ * Start the blob client.
484
+ * Uploads the initial healthcheck file (awaited) and starts periodic uploads.
485
+ */ async start() {
486
+ if (!this.fileStoreUploadClient) {
487
+ return;
488
+ }
489
+ await this.fileStoreUploadClient.uploadHealthcheck();
490
+ this.log.debug('Initial healthcheck file uploaded');
491
+ this.startPeriodicHealthcheckUpload();
492
+ }
493
+ /**
494
+ * Start periodic healthcheck upload to the file store to ensure it remains available even if accidentally deleted.
495
+ */ startPeriodicHealthcheckUpload() {
496
+ const intervalMs = (this.config.blobHealthcheckUploadIntervalMinutes ?? DEFAULT_HEALTHCHECK_UPLOAD_INTERVAL_MINUTES) * 60 * 1000;
497
+ this.healthcheckUploadIntervalId = setInterval(()=>{
498
+ void this.fileStoreUploadClient.uploadHealthcheck().catch((err)=>{
499
+ this.log.warn('Failed to upload periodic healthcheck file', err);
500
+ });
501
+ }, intervalMs);
502
+ }
503
+ /**
504
+ * Stop the blob client, clearing any periodic tasks.
505
+ */ stop() {
506
+ if (this.healthcheckUploadIntervalId) {
507
+ clearInterval(this.healthcheckUploadIntervalId);
508
+ this.healthcheckUploadIntervalId = undefined;
509
+ }
510
+ }
511
+ }
512
+ function parseBlobJsonsFromResponse(response, logger) {
513
+ try {
514
+ const blobs = response.data.map(parseBlobJson);
515
+ return blobs;
516
+ } catch (err) {
517
+ logger.error(`Error parsing blob json from response`, err);
518
+ return [];
519
+ }
520
+ }
521
+ // Blobs will be in this form when requested from the blob client, or from the beacon chain via `getBlobSidecars`:
522
+ // https://ethereum.github.io/beacon-APIs/?urls.primaryName=dev#/Beacon/getBlobSidecars
523
+ // Here we attempt to parse the response data to Buffer, and check the lengths (via Blob's constructor), to avoid
524
+ // throwing an error down the line when calling Blob.fromJson().
525
+ function parseBlobJson(data) {
526
+ const blobBuffer = Buffer.from(data.blob.slice(2), 'hex');
527
+ const commitmentBuffer = Buffer.from(data.kzg_commitment.slice(2), 'hex');
528
+ const blob = new Blob(blobBuffer, commitmentBuffer);
529
+ return blob.toJSON();
530
+ }
531
+ // Returns an array that maps each blob hash to the corresponding blob, or undefined if the blob is not found
532
+ // or the data does not match the commitment.
533
+ function processFetchedBlobs(blobs, blobHashes, logger) {
534
+ const requestedBlobHashes = new Set(blobHashes.map(bufferToHex));
535
+ const hashToBlob = new Map();
536
+ for (const blobJson of blobs){
537
+ const hashHex = bufferToHex(computeEthVersionedBlobHash(hexToBuffer(blobJson.kzg_commitment)));
538
+ if (!requestedBlobHashes.has(hashHex) || hashToBlob.has(hashHex)) {
539
+ continue;
540
+ }
541
+ try {
542
+ const blob = Blob.fromJson(blobJson);
543
+ hashToBlob.set(hashHex, blob);
544
+ } catch (err) {
545
+ // If the above throws, it's likely that the blob commitment does not match the hash of the blob data.
546
+ logger.error(`Error converting blob from json`, err);
547
+ }
548
+ }
549
+ return blobHashes.map((h)=>hashToBlob.get(bufferToHex(h)));
550
+ }
551
+ function getBeaconNodeFetchOptions(url, config, l1ConsensusHostIndex) {
552
+ const { l1ConsensusHostApiKeys, l1ConsensusHostApiKeyHeaders } = config;
553
+ const l1ConsensusHostApiKey = l1ConsensusHostIndex !== undefined && l1ConsensusHostApiKeys && l1ConsensusHostApiKeys[l1ConsensusHostIndex];
554
+ const l1ConsensusHostApiKeyHeader = l1ConsensusHostIndex !== undefined && l1ConsensusHostApiKeyHeaders && l1ConsensusHostApiKeyHeaders[l1ConsensusHostIndex];
555
+ let formattedUrl = url;
556
+ if (l1ConsensusHostApiKey && l1ConsensusHostApiKey.getValue() !== '' && !l1ConsensusHostApiKeyHeader) {
557
+ formattedUrl += `${formattedUrl.includes('?') ? '&' : '?'}key=${l1ConsensusHostApiKey.getValue()}`;
558
+ }
559
+ return {
560
+ url: formattedUrl,
561
+ ...l1ConsensusHostApiKey && l1ConsensusHostApiKeyHeader && {
562
+ headers: {
563
+ [l1ConsensusHostApiKeyHeader]: l1ConsensusHostApiKey.getValue()
564
+ }
565
+ }
566
+ };
567
+ }
@@ -0,0 +1,6 @@
1
+ export * from './http.js';
2
+ export * from './local.js';
3
+ export * from './interface.js';
4
+ export * from './factory.js';
5
+ export * from './config.js';
6
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9jbGllbnQvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsY0FBYyxXQUFXLENBQUM7QUFDMUIsY0FBYyxZQUFZLENBQUM7QUFDM0IsY0FBYyxnQkFBZ0IsQ0FBQztBQUMvQixjQUFjLGNBQWMsQ0FBQztBQUM3QixjQUFjLGFBQWEsQ0FBQyJ9
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC"}
@@ -0,0 +1,5 @@
1
+ export * from './http.js';
2
+ export * from './local.js';
3
+ export * from './interface.js';
4
+ export * from './factory.js';
5
+ export * from './config.js';
@@ -0,0 +1,28 @@
1
+ import type { Blob } from '@aztec/blob-lib';
2
+ /**
3
+ * Options for getBlobSidecar method.
4
+ */
5
+ export interface GetBlobSidecarOptions {
6
+ /**
7
+ * True if the archiver is catching up (historical sync), false if near tip.
8
+ * This affects source ordering:
9
+ * - Historical: FileStore first (data should exist), then L1 consensus, then archive (eg. blobscan)
10
+ * - Near tip: FileStore first with no retries (data should exist), L1 consensus second (freshest data), then FileStore with retries, then archive (eg. blobscan)
11
+ */
12
+ isHistoricalSync?: boolean;
13
+ }
14
+ export interface BlobClientInterface {
15
+ /** Sends the given blobs to the filestore, to be indexed by blob hash. */
16
+ sendBlobsToFilestore(blobs: Blob[]): Promise<boolean>;
17
+ /** Fetches the given blob sidecars by block hash and blob hashes. */
18
+ getBlobSidecar(blockId: string, blobHashes?: Buffer[], opts?: GetBlobSidecarOptions): Promise<Blob[]>;
19
+ /** Starts the blob client (e.g., uploads healthcheck file if not exists). */
20
+ start?(): Promise<void>;
21
+ /** Tests all configured blob sources and logs whether they are reachable or not. */
22
+ testSources(): Promise<void>;
23
+ /** Stops the blob client, clearing any periodic tasks. */
24
+ stop?(): void;
25
+ /** Returns true if this client can upload blobs to filestore. */
26
+ canUpload(): boolean;
27
+ }
28
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW50ZXJmYWNlLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvY2xpZW50L2ludGVyZmFjZS50cyJdLCJuYW1lcyI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AoB,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACtD,qEAAqE;IACrE,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,EAAE,EAAE,IAAI,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;IACtG,6EAA6E;IAC7E,KAAK,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACxB,oFAAoF;IACpF,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,0DAA0D;IAC1D,IAAI,CAAC,IAAI,IAAI,CAAC;IACd,iEAAiE;IACjE,SAAS,IAAI,OAAO,CAAC;CACtB"}
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1,13 @@
1
+ import type { Blob } from '@aztec/blob-lib';
2
+ import type { BlobStore } from '../blobstore/index.js';
3
+ import type { BlobClientInterface, GetBlobSidecarOptions } from './interface.js';
4
+ export declare class LocalBlobClient implements BlobClientInterface {
5
+ private readonly blobStore;
6
+ constructor(blobStore: BlobStore);
7
+ testSources(): Promise<void>;
8
+ sendBlobsToFilestore(blobs: Blob[]): Promise<boolean>;
9
+ getBlobSidecar(_blockId: string, blobHashes: Buffer[], _opts?: GetBlobSidecarOptions): Promise<Blob[]>;
10
+ /** Returns true if this client can upload blobs. Always true for local client. */
11
+ canUpload(): boolean;
12
+ }
13
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibG9jYWwuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9jbGllbnQvbG9jYWwudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxLQUFLLEVBQUUsSUFBSSxFQUFFLE1BQU0saUJBQWlCLENBQUM7QUFFNUMsT0FBTyxLQUFLLEVBQUUsU0FBUyxFQUFFLE1BQU0sdUJBQ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IAEM,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,EAAE,KAAK,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,CAE5G;IAED,kFAAkF;IAC3E,SAAS,IAAI,OAAO,CAE1B;CACF"}
@@ -0,0 +1,19 @@
1
+ export class LocalBlobClient {
2
+ blobStore;
3
+ constructor(blobStore){
4
+ this.blobStore = blobStore;
5
+ }
6
+ testSources() {
7
+ return Promise.resolve();
8
+ }
9
+ async sendBlobsToFilestore(blobs) {
10
+ await this.blobStore.addBlobs(blobs);
11
+ return true;
12
+ }
13
+ getBlobSidecar(_blockId, blobHashes, _opts) {
14
+ return this.blobStore.getBlobsByHashes(blobHashes);
15
+ }
16
+ /** Returns true if this client can upload blobs. Always true for local client. */ canUpload() {
17
+ return true;
18
+ }
19
+ }
@@ -0,0 +1,11 @@
1
+ import type { BlobClientInterface } from './interface.js';
2
+ /**
3
+ * Shared test suite for blob clients
4
+ * @param createClient - Function that creates a client instance for testing
5
+ * @param cleanup - Optional cleanup function to run after each test
6
+ */
7
+ export declare function runBlobClientTests(createClient: () => Promise<{
8
+ client: BlobClientInterface;
9
+ cleanup: () => Promise<void>;
10
+ }>): void;
11
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdHMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9jbGllbnQvdGVzdHMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBSUEsT0FBTyxLQUFLLEVBQUUsbUJBQW1CLEVBQUUsTUFBTSxnQkFBZ0IsQ0FBQztBQUUxRDs7OztHQUlHO0FBQ0gsd0JBQWdCLGtCQUFrQixDQUNoQyxZQUFZLEVBQUUsTUFBTSxPQUFPLENBQUM7SUFBRSxNQUFNLEVBQUUsbUJBQW1CLENBQUM7SUFBQyxPQUFPLEVBQUUsTUFBTSxPQUFPLENBQUMsSUFBSSxDQUFDLENBQUE7Q0FBRSxDQUFDLFFBaUQzRiJ9