-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathhttp.test.ts
206 lines (169 loc) · 7.55 KB
/
http.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { Blob, makeEncodedBlob, makeUnencodedBlob } from '@aztec/foundation/blob';
import { Fr } from '@aztec/foundation/fields';
import { jest } from '@jest/globals';
import http from 'http';
import { type AddressInfo } from 'net';
import { BlobSinkServer } from '../server/server.js';
import { runBlobSinkClientTests } from './blob-sink-client-tests.js';
import { HttpBlobSinkClient } from './http.js';
describe('HttpBlobSinkClient', () => {
runBlobSinkClientTests(async () => {
const server = new BlobSinkServer({
port: 0,
});
await server.start();
const client = new HttpBlobSinkClient({
blobSinkUrl: `http://localhost:${server.port}`,
});
return {
client,
cleanup: async () => {
await server.stop();
},
};
});
it('should handle server connection errors gracefully', async () => {
const client = new HttpBlobSinkClient({ blobSinkUrl: 'http://localhost:12345' }); // Invalid port
const blob = await Blob.fromFields([Fr.random()]);
const blobHash = blob.getEthVersionedBlobHash();
const success = await client.sendBlobsToBlobSink('0x1234', [blob]);
expect(success).toBe(false);
const retrievedBlobs = await client.getBlobSidecar('0x1234', [blobHash]);
expect(retrievedBlobs).toEqual([]);
});
describe('Mock Ethereum Clients', () => {
let blobSinkServer: BlobSinkServer;
let testEncodedBlob: Blob;
let testEncodedBlobHash: Buffer;
let testNonEncodedBlob: Blob;
let testNonEncodedBlobHash: Buffer;
// A blob to be ignored when requesting blobs
// - we do not include it's blobHash in our queries
let testBlobIgnore: Blob;
let executionHostServer: http.Server | undefined = undefined;
let executionHostPort: number | undefined = undefined;
let consensusHostServer: http.Server | undefined = undefined;
let consensusHostPort: number | undefined = undefined;
const MOCK_SLOT_NUMBER = 1;
beforeEach(async () => {
testEncodedBlob = await makeEncodedBlob(3);
testEncodedBlobHash = testEncodedBlob.getEthVersionedBlobHash();
testBlobIgnore = await makeEncodedBlob(3);
testNonEncodedBlob = await makeUnencodedBlob(3);
testNonEncodedBlobHash = testNonEncodedBlob.getEthVersionedBlobHash();
});
const startExecutionHostServer = (): Promise<void> => {
executionHostServer = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ result: { parentBeaconBlockRoot: '0x1234' } }));
});
return new Promise((resolve, _reject) => {
executionHostServer?.listen(0, () => {
executionHostPort = (executionHostServer?.address() as AddressInfo).port;
resolve();
});
});
};
const startConsensusHostServer = (): Promise<void> => {
consensusHostServer = http.createServer((req, res) => {
if (req.url?.includes('/eth/v1/beacon/headers/')) {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ data: { header: { message: { slot: MOCK_SLOT_NUMBER } } } }));
} else if (req.url?.includes('/eth/v1/beacon/blob_sidecars/')) {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
data: [
// Correctly encoded blob
{
index: 0,
blob: `0x${Buffer.from(testEncodedBlob.data).toString('hex')}`,
// eslint-disable-next-line camelcase
kzg_commitment: `0x${testEncodedBlob.commitment.toString('hex')}`,
// eslint-disable-next-line camelcase
kzg_proof: `0x${testEncodedBlob.proof.toString('hex')}`,
},
// Correctly encoded blob, but we do not ask for it in the client
{
index: 1,
blob: `0x${Buffer.from(testBlobIgnore.data).toString('hex')}`,
// eslint-disable-next-line camelcase
kzg_commitment: `0x${testBlobIgnore.commitment.toString('hex')}`,
// eslint-disable-next-line camelcase
kzg_proof: `0x${testBlobIgnore.proof.toString('hex')}`,
},
// Incorrectly encoded blob
{
index: 2,
blob: `0x${Buffer.from(testNonEncodedBlob.data).toString('hex')}`,
// eslint-disable-next-line camelcase
kzg_commitment: `0x${testNonEncodedBlob.commitment.toString('hex')}`,
// eslint-disable-next-line camelcase
kzg_proof: `0x${testNonEncodedBlob.proof.toString('hex')}`,
},
],
}),
);
} else {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Not Found' }));
}
});
return new Promise((resolve, _reject) => {
consensusHostServer?.listen(0, () => {
consensusHostPort = (consensusHostServer?.address() as AddressInfo).port;
resolve();
});
});
};
afterEach(async () => {
await blobSinkServer?.stop();
executionHostServer?.close();
consensusHostServer?.close();
executionHostPort = undefined;
consensusHostPort = undefined;
});
// When the consensus host is not responding, we should still be able to request blobs with the block hash
it('should handle no consensus host', async () => {
blobSinkServer = new BlobSinkServer({
port: 0,
});
await blobSinkServer.start();
const blobSinkSpy = jest.spyOn((blobSinkServer as any).blobStore, 'getBlobSidecars');
await startExecutionHostServer();
const client = new HttpBlobSinkClient({
blobSinkUrl: `http://localhost:${blobSinkServer.port}`,
l1RpcUrl: `http://localhost:${executionHostPort}`,
});
const success = await client.sendBlobsToBlobSink('0x1234', [testEncodedBlob]);
expect(success).toBe(true);
const retrievedBlobs = await client.getBlobSidecar('0x1234', [testEncodedBlobHash]);
expect(retrievedBlobs).toEqual([testEncodedBlob]);
// Check that the blob sink was called with the correct block hash and no index
expect(blobSinkSpy).toHaveBeenCalledWith('0x1234', undefined);
});
// When the consensus host is responding, we should request blobs from the consensus host
// based on the slot number
it('should request based on slot where consensus host is provided', async () => {
await startExecutionHostServer();
await startConsensusHostServer();
const client = new HttpBlobSinkClient({
l1RpcUrl: `http://localhost:${executionHostPort}`,
l1ConsensusHostUrl: `http://localhost:${consensusHostPort}`,
});
const retrievedBlobs = await client.getBlobSidecar('0x1234', [testEncodedBlobHash]);
expect(retrievedBlobs).toEqual([testEncodedBlob]);
});
it('Even if we ask for non-encoded blobs, we should only get encoded blobs', async () => {
await startExecutionHostServer();
await startConsensusHostServer();
const client = new HttpBlobSinkClient({
l1RpcUrl: `http://localhost:${executionHostPort}`,
l1ConsensusHostUrl: `http://localhost:${consensusHostPort}`,
});
const retrievedBlobs = await client.getBlobSidecar('0x1234', [testEncodedBlobHash, testNonEncodedBlobHash]);
// We should only get the correctly encoded blob
expect(retrievedBlobs).toEqual([testEncodedBlob]);
});
});
});