-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathserver.test.ts
141 lines (111 loc) · 5.5 KB
/
server.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { Blob, makeEncodedBlob } from '@aztec/blob-lib';
import request from 'supertest';
import { outboundTransform } from '../encoding/index.js';
import { BlobSinkServer } from './server.js';
describe('BlobSinkService', () => {
let service: BlobSinkServer;
beforeEach(async () => {
service = new BlobSinkServer({
port: 0, // Using port 0 lets the OS assign a random available port
});
await service.start();
});
afterEach(async () => {
await service.stop();
});
describe('should store and retrieve a blob sidecar', () => {
const blockId = '0x1234';
let blob: Blob;
let blob2: Blob;
beforeEach(async () => {
blob = await makeEncodedBlob(3);
blob2 = await makeEncodedBlob(3);
// Post the blob
const postResponse = await request(service.getApp())
.post('/blob_sidecar')
.send({
// eslint-disable-next-line camelcase
block_id: blockId,
blobs: [
{
index: 0,
blob: outboundTransform(blob.toBuffer()),
},
{
index: 1,
blob: outboundTransform(blob2.toBuffer()),
},
],
});
expect(postResponse.status).toBe(200);
});
it('should retrieve the blob', async () => {
// Retrieve the blob
const getResponse = await request(service.getApp()).get(`/eth/v1/beacon/blob_sidecars/${blockId}`);
expect(getResponse.status).toBe(200);
// Convert the response blob back to a Blob object and verify it matches
const retrievedBlobs = getResponse.body.data;
const retrievedBlob = await Blob.fromEncodedBlobBuffer(Buffer.from(retrievedBlobs[0].blob.slice(2), 'hex'));
const retrievedBlob2 = await Blob.fromEncodedBlobBuffer(Buffer.from(retrievedBlobs[1].blob.slice(2), 'hex'));
expect(retrievedBlob.fieldsHash.toString()).toBe(blob.fieldsHash.toString());
expect(retrievedBlob.commitment.toString('hex')).toBe(blob.commitment.toString('hex'));
expect(retrievedBlob.proof.toString('hex')).toBe(blob.proof.toString('hex'));
expect(retrievedBlob2.fieldsHash.toString()).toBe(blob2.fieldsHash.toString());
expect(retrievedBlob2.commitment.toString('hex')).toBe(blob2.commitment.toString('hex'));
expect(retrievedBlob2.proof.toString('hex')).toBe(blob2.proof.toString('hex'));
});
it('should retrieve specific indicies', async () => {
// We can also request specific indicies
const getWithIndicies = await request(service.getApp()).get(
`/eth/v1/beacon/blob_sidecars/${blockId}?indices=0,1`,
);
expect(getWithIndicies.status).toBe(200);
expect(getWithIndicies.body.data.length).toBe(2);
const retrievedBlobs = getWithIndicies.body.data;
const retrievedBlob = await Blob.fromEncodedBlobBuffer(Buffer.from(retrievedBlobs[0].blob.slice(2), 'hex'));
const retrievedBlob2 = await Blob.fromEncodedBlobBuffer(Buffer.from(retrievedBlobs[1].blob.slice(2), 'hex'));
expect(retrievedBlob.fieldsHash.toString()).toBe(blob.fieldsHash.toString());
expect(retrievedBlob.commitment.toString('hex')).toBe(blob.commitment.toString('hex'));
expect(retrievedBlob.proof.toString('hex')).toBe(blob.proof.toString('hex'));
expect(retrievedBlob2.fieldsHash.toString()).toBe(blob2.fieldsHash.toString());
expect(retrievedBlob2.commitment.toString('hex')).toBe(blob2.commitment.toString('hex'));
expect(retrievedBlob2.proof.toString('hex')).toBe(blob2.proof.toString('hex'));
});
it('should retrieve a single index', async () => {
const getWithIndicies = await request(service.getApp()).get(`/eth/v1/beacon/blob_sidecars/${blockId}?indices=1`);
expect(getWithIndicies.status).toBe(200);
expect(getWithIndicies.body.data.length).toBe(1);
const retrievedBlobs = getWithIndicies.body.data;
const retrievedBlob = await Blob.fromEncodedBlobBuffer(Buffer.from(retrievedBlobs[0].blob.slice(2), 'hex'));
expect(retrievedBlob.fieldsHash.toString()).toBe(blob2.fieldsHash.toString());
expect(retrievedBlob.commitment.toString('hex')).toBe(blob2.commitment.toString('hex'));
expect(retrievedBlob.proof.toString('hex')).toBe(blob2.proof.toString('hex'));
});
});
it('should return an error if invalid indicies are provided', async () => {
const blockId = '0x1234';
const response = await request(service.getApp()).get(`/eth/v1/beacon/blob_sidecars/${blockId}?indices=word`);
expect(response.status).toBe(400);
expect(response.body.error).toBe('Invalid indices parameter');
});
it('should return an error if the block ID is invalid (POST)', async () => {
const response = await request(service.getApp()).post('/blob_sidecar').send({
// eslint-disable-next-line camelcase
block_id: undefined,
});
expect(response.status).toBe(400);
});
it('should return an error if the block ID is invalid (GET)', async () => {
const response = await request(service.getApp()).get('/eth/v1/beacon/blob_sidecars/invalid-id');
expect(response.status).toBe(400);
});
it('should return 404 for non-existent blob', async () => {
const response = await request(service.getApp()).get('/eth/v1/beacon/blob_sidecars/0x999999');
expect(response.status).toBe(404);
});
it('should reject negative block IDs', async () => {
const response = await request(service.getApp()).get('/eth/v1/beacon/blob_sidecars/-123');
expect(response.status).toBe(400);
expect(response.body.error).toBe('Invalid block_id parameter');
});
});