Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('blobscan_archive_client', () => {
});

describe('getBlobData', () => {
const expectedUrl = `https://api.blobscan.dev/blobs/${blobId}/data`;
const expectedUrl = new URL(`https://api.blobscan.dev/blobs/${blobId}/data`);

it('fetches blob data', async () => {
await loadResponse('blobscan_get_blob_data.json');
Expand All @@ -60,7 +60,7 @@ describe('blobscan_archive_client', () => {
});

describe('getBlobsFromBlock', () => {
const expectedUrl = `https://api.blobscan.dev/blocks/${blockId}?type=canonical&expand=blob%2Cblob_data`;
const expectedUrl = new URL(`https://api.blobscan.dev/blocks/${blockId}?type=canonical&expand=blob%2Cblob_data`);

it('fetches blobs from block', async () => {
await loadResponse('blobscan_get_block.json');
Expand Down
31 changes: 22 additions & 9 deletions yarn-project/blob-sink/src/archive/blobscan_archive_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,30 @@ export class BlobscanArchiveClient implements BlobArchiveClient {
);
};

private readonly baseUrl;
private readonly baseUrl: URL;

private instrumentation: BlobArchiveClientInstrumentation;

constructor(baseUrl: string, telemetry: TelemetryClient = getTelemetryClient()) {
this.baseUrl = baseUrl.replace(/^https?:\/\//, '');
this.baseUrl = new URL(baseUrl);
if (this.baseUrl.protocol !== 'https:') {
throw new TypeError('BaseURL must be secure: ' + baseUrl);
}
this.instrumentation = new BlobArchiveClientInstrumentation(
telemetry,
new URL(baseUrl).host,
this.baseUrl.origin,
'BlobscanArchiveClient',
);
}

public async getLatestBlock(): Promise<{ hash: string; number: number; slot: number }> {
const url = `https://${this.baseUrl}/blocks?sort=desc&type=canonical&p=1&ps=1`;
this.logger.trace(`Fetching latest block from ${url}`);
const url = new URL('blocks', this.baseUrl);
url.searchParams.set('sort', 'desc');
url.searchParams.set('type', 'canonical');
url.searchParams.set('p', '1');
url.searchParams.set('ps', '1');

this.logger.trace(`Fetching latest block from ${url.href}`);
const response = await this.fetch(url, this.fetchOpts);

if (response.status !== 200) {
Expand All @@ -106,12 +114,15 @@ export class BlobscanArchiveClient implements BlobArchiveClient {
}

public getBaseUrl(): string {
return this.baseUrl;
return this.baseUrl.href;
}

public async getBlobsFromBlock(blockId: string): Promise<BlobJson[] | undefined> {
const url = `https://${this.baseUrl}/blocks/${blockId}?type=canonical&expand=blob%2Cblob_data`;
this.logger.trace(`Fetching blobs for block ${blockId} from ${url}`);
const url = new URL(`blocks/${blockId}`, this.baseUrl);
url.searchParams.set('type', 'canonical');
url.searchParams.set('expand', 'blob,blob_data');

this.logger.trace(`Fetching blobs for block ${blockId} from ${url.href}`);
const response = await this.fetch(url, this.fetchOpts);

this.instrumentation.incRequest('blocks', response.status);
Expand All @@ -137,7 +148,9 @@ export class BlobscanArchiveClient implements BlobArchiveClient {
}

public async getBlobData(id: string): Promise<Buffer | undefined> {
const response = await this.fetch(`https://${this.baseUrl}/blobs/${id}/data`, this.fetchOpts);
const url = new URL(`blobs/${id}/data`, this.baseUrl);

const response = await this.fetch(url, this.fetchOpts);
this.instrumentation.incRequest('blobs', response.status);
if (response.status === 404) {
return undefined;
Expand Down
27 changes: 27 additions & 0 deletions yarn-project/blob-sink/src/archive/factory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { BlobSinkArchiveApiConfig } from './config.js';
import { createBlobArchiveClient } from './factory.js';

describe('BlobscanArchiveClient factory', () => {
it.each<[string, BlobSinkArchiveApiConfig, boolean, string]>([
['empty config', {}, false, ''],
['random chain, no custom URL', { l1ChainId: 23478 }, false, ''],
[
'random chain, custom URL',
{ l1ChainId: 23478, archiveApiUrl: 'https://example.com' },
true,
'https://example.com/',
],
['ETH mainnet default URL', { l1ChainId: 1 }, true, 'https://api.blobscan.com/'],
['ETH mainnet custom URL', { l1ChainId: 1, archiveApiUrl: 'https://example.com' }, true, 'https://example.com/'],
['Sepolia default URL', { l1ChainId: 11155111 }, true, 'https://api.sepolia.blobscan.com/'],
['Seplia custom URL', { l1ChainId: 11155111, archiveApiUrl: 'https://example.com' }, true, 'https://example.com/'],
])('can instantiate a client: %s', (_, cfg, clientExpected, expectedBaseUrl) => {
const client = createBlobArchiveClient(cfg);
if (clientExpected) {
expect(client).toBeDefined();
expect(client!.getBaseUrl()).toEqual(expectedBaseUrl);
} else {
expect(client).not.toBeDefined();
}
});
});
2 changes: 1 addition & 1 deletion yarn-project/blob-sink/src/client/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ describe('HttpBlobSinkClient', () => {
});

it('should fall back to archive client', async () => {
const client = new TestHttpBlobSinkClient({ archiveApiUrl: `http://api.blobscan.com` });
const client = new TestHttpBlobSinkClient({ archiveApiUrl: `https://api.blobscan.com` });
const archiveSpy = jest.spyOn(client.getArchiveClient(), 'getBlobsFromBlock').mockResolvedValue(blobData);

const retrievedBlobs = await client.getBlobSidecar('0x1234', [testEncodedBlobHash]);
Expand Down