Skip to content

Commit 7b16791

Browse files
authored
test: blob archive client factory code (#13648)
1 parent 694537d commit 7b16791

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

yarn-project/blob-sink/src/archive/blobscan_archive_client.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('blobscan_archive_client', () => {
3939
});
4040

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

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

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

6565
it('fetches blobs from block', async () => {
6666
await loadResponse('blobscan_get_block.json');

yarn-project/blob-sink/src/archive/blobscan_archive_client.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,30 @@ export class BlobscanArchiveClient implements BlobArchiveClient {
6868
);
6969
};
7070

71-
private readonly baseUrl;
71+
private readonly baseUrl: URL;
7272

7373
private instrumentation: BlobArchiveClientInstrumentation;
7474

7575
constructor(baseUrl: string, telemetry: TelemetryClient = getTelemetryClient()) {
76-
this.baseUrl = baseUrl.replace(/^https?:\/\//, '');
76+
this.baseUrl = new URL(baseUrl);
77+
if (this.baseUrl.protocol !== 'https:') {
78+
throw new TypeError('BaseURL must be secure: ' + baseUrl);
79+
}
7780
this.instrumentation = new BlobArchiveClientInstrumentation(
7881
telemetry,
79-
new URL(baseUrl).host,
82+
this.baseUrl.origin,
8083
'BlobscanArchiveClient',
8184
);
8285
}
8386

8487
public async getLatestBlock(): Promise<{ hash: string; number: number; slot: number }> {
85-
const url = `https://${this.baseUrl}/blocks?sort=desc&type=canonical&p=1&ps=1`;
86-
this.logger.trace(`Fetching latest block from ${url}`);
88+
const url = new URL('blocks', this.baseUrl);
89+
url.searchParams.set('sort', 'desc');
90+
url.searchParams.set('type', 'canonical');
91+
url.searchParams.set('p', '1');
92+
url.searchParams.set('ps', '1');
93+
94+
this.logger.trace(`Fetching latest block from ${url.href}`);
8795
const response = await this.fetch(url, this.fetchOpts);
8896

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

108116
public getBaseUrl(): string {
109-
return this.baseUrl;
117+
return this.baseUrl.href;
110118
}
111119

112120
public async getBlobsFromBlock(blockId: string): Promise<BlobJson[] | undefined> {
113-
const url = `https://${this.baseUrl}/blocks/${blockId}?type=canonical&expand=blob%2Cblob_data`;
114-
this.logger.trace(`Fetching blobs for block ${blockId} from ${url}`);
121+
const url = new URL(`blocks/${blockId}`, this.baseUrl);
122+
url.searchParams.set('type', 'canonical');
123+
url.searchParams.set('expand', 'blob,blob_data');
124+
125+
this.logger.trace(`Fetching blobs for block ${blockId} from ${url.href}`);
115126
const response = await this.fetch(url, this.fetchOpts);
116127

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

139150
public async getBlobData(id: string): Promise<Buffer | undefined> {
140-
const response = await this.fetch(`https://${this.baseUrl}/blobs/${id}/data`, this.fetchOpts);
151+
const url = new URL(`blobs/${id}/data`, this.baseUrl);
152+
153+
const response = await this.fetch(url, this.fetchOpts);
141154
this.instrumentation.incRequest('blobs', response.status);
142155
if (response.status === 404) {
143156
return undefined;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { BlobSinkArchiveApiConfig } from './config.js';
2+
import { createBlobArchiveClient } from './factory.js';
3+
4+
describe('BlobscanArchiveClient factory', () => {
5+
it.each<[string, BlobSinkArchiveApiConfig, boolean, string]>([
6+
['empty config', {}, false, ''],
7+
['random chain, no custom URL', { l1ChainId: 23478 }, false, ''],
8+
[
9+
'random chain, custom URL',
10+
{ l1ChainId: 23478, archiveApiUrl: 'https://example.com' },
11+
true,
12+
'https://example.com/',
13+
],
14+
['ETH mainnet default URL', { l1ChainId: 1 }, true, 'https://api.blobscan.com/'],
15+
['ETH mainnet custom URL', { l1ChainId: 1, archiveApiUrl: 'https://example.com' }, true, 'https://example.com/'],
16+
['Sepolia default URL', { l1ChainId: 11155111 }, true, 'https://api.sepolia.blobscan.com/'],
17+
['Seplia custom URL', { l1ChainId: 11155111, archiveApiUrl: 'https://example.com' }, true, 'https://example.com/'],
18+
])('can instantiate a client: %s', (_, cfg, clientExpected, expectedBaseUrl) => {
19+
const client = createBlobArchiveClient(cfg);
20+
if (clientExpected) {
21+
expect(client).toBeDefined();
22+
expect(client!.getBaseUrl()).toEqual(expectedBaseUrl);
23+
} else {
24+
expect(client).not.toBeDefined();
25+
}
26+
});
27+
});

yarn-project/blob-sink/src/client/http.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ describe('HttpBlobSinkClient', () => {
411411
});
412412

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

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

0 commit comments

Comments
 (0)