-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathhttp.ts
191 lines (169 loc) · 6.25 KB
/
http.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
import { Blob, type BlobJson } from '@aztec/foundation/blob';
import { type Logger, createLogger } from '@aztec/foundation/log';
import { outboundTransform } from '../encoding/index.js';
import { type BlobSinkConfig, getBlobSinkConfigFromEnv } from './config.js';
import { type BlobSinkClientInterface } from './interface.js';
export class HttpBlobSinkClient implements BlobSinkClientInterface {
private readonly log: Logger;
private readonly config: BlobSinkConfig;
constructor(config?: BlobSinkConfig) {
this.config = config ?? getBlobSinkConfigFromEnv();
this.log = createLogger('aztec:blob-sink-client');
}
public async sendBlobsToBlobSink(blockHash: string, blobs: Blob[]): Promise<boolean> {
// TODO(md): for now we are assuming the indexes of the blobs will be 0, 1, 2
// When in reality they will not, but for testing purposes this is fine
if (!this.config.blobSinkUrl) {
this.log.verbose('No blob sink url configured');
return false;
}
this.log.verbose(`Sending ${blobs.length} blobs to blob sink`);
try {
const res = await fetch(`${this.config.blobSinkUrl}/blob_sidecar`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
// eslint-disable-next-line camelcase
block_id: blockHash,
// Snappy compress the blob buffer
blobs: blobs.map((b, i) => ({ blob: outboundTransform(b.toBuffer()), index: i })),
}),
});
if (res.ok) {
return true;
}
this.log.error('Failed to send blobs to blob sink', res.status);
return false;
} catch (err) {
this.log.error(`Error sending blobs to blob sink`, err);
return false;
}
}
/**
* Get the blob sidecar
*
* If requesting from the blob sink, we send the blobkHash
* If requesting from the beacon node, we send the slot number
*
* 1. First atttempts to get blobs from a configured blob sink
* 2. If no blob sink is configured, attempts to get blobs from a configured consensus host
*
* 3. If none configured, fails
*
* @param blockHash - The block hash
* @param indices - The indices of the blobs to get
* @returns The blobs
*/
public async getBlobSidecar(blockHash: string, indices?: number[]): Promise<Blob[]> {
if (this.config.blobSinkUrl) {
this.log.debug('Getting blob sidecar from blob sink');
const blobs = await this.getBlobSidecarFrom(this.config.blobSinkUrl, blockHash, indices);
if (blobs.length > 0) {
this.log.debug(`Got ${blobs.length} blobs from blob sink`);
return blobs;
}
}
if (this.config.l1ConsensusHostUrl) {
// The beacon api can query by slot number, so we get that first
this.log.debug('Getting slot number from consensus host');
const slotNumber = await this.getSlotNumber(blockHash);
if (slotNumber) {
const blobs = await this.getBlobSidecarFrom(this.config.l1ConsensusHostUrl, slotNumber, indices);
if (blobs.length > 0) {
this.log.debug(`Got ${blobs.length} blobs from consensus host`);
return blobs;
}
}
}
this.log.verbose('No blob sources available');
return [];
}
public async getBlobSidecarFrom(
hostUrl: string,
blockHashOrSlot: string | number,
indices?: number[],
): Promise<Blob[]> {
// TODO(md): right now we assume all blobs are ours, this will not yet work on sepolia
try {
let url = `${hostUrl}/eth/v1/beacon/blob_sidecars/${blockHashOrSlot}`;
if (indices && indices.length > 0) {
url += `?indices=${indices.join(',')}`;
}
const res = await fetch(url);
if (res.ok) {
const body = await res.json();
const blobs = body.data.map((b: BlobJson) => Blob.fromJson(b));
return blobs;
}
this.log.debug(`Unable to get blob sidecar`, res.status);
return [];
} catch (err: any) {
this.log.error(`Unable to get blob sidecar`, err.message);
return [];
}
}
/**
* Get the slot number from the consensus host
* As of eip-4788, the parentBeaconBlockRoot is included in the execution layer.
* This allows us to query the consensus layer for the slot number of the parent block, which we will then use
* to request blobs from the consensus layer.
*
* If this returns undefined, it means that we are not connected to a real consensus host, and we should
* query blobs with the blockHash.
*
* If this returns a number, then we should query blobs with the slot number
*
* @param blockHash - The block hash
* @returns The slot number
*/
private async getSlotNumber(blockHash: string): Promise<number | undefined> {
if (!this.config.l1ConsensusHostUrl) {
this.log.debug('No consensus host url configured');
return undefined;
}
if (!this.config.l1RpcUrl) {
this.log.debug('No execution host url configured');
return undefined;
}
// Ping execution node to get the parentBeaconBlockRoot for this block
let parentBeaconBlockRoot: string | undefined;
try {
const res = await fetch(`${this.config.l1RpcUrl}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
method: 'eth_getBlockByHash',
params: [blockHash, /*tx flag*/ false],
id: 1,
}),
});
if (res.ok) {
const body = await res.json();
parentBeaconBlockRoot = body.result.parentBeaconBlockRoot;
}
} catch (err) {
this.log.error(`Error getting parent beacon block root`, err);
}
if (!parentBeaconBlockRoot) {
this.log.error(`No parent beacon block root found for block ${blockHash}`);
return undefined;
}
// Query beacon chain to get the slot number for that block root
try {
const res = await fetch(`${this.config.l1ConsensusHostUrl}/eth/v1/beacon/headers/${parentBeaconBlockRoot}`);
if (res.ok) {
const body = await res.json();
// Add one to get the slot number of the original block hash
return Number(body.data.header.message.slot) + 1;
}
} catch (err) {
this.log.error(`Error getting slot number`, err);
}
return undefined;
}
}