@aztec/blob-client 0.0.1-commit.03f7ef2

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