-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblob-submission.test.ts
370 lines (308 loc) · 11.2 KB
/
blob-submission.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import {
parseGwei,
bytesToHex,
Hex,
Abi,
commitmentToVersionedHash,
getContractAddress,
parseEventLogs,
encodeFunctionData,
SendRawTransactionErrorType,
} from "viem";
import { client, account } from "./client.js";
import { cKzg, viemKzg } from "./ckzg.js";
const { BYTES_PER_BLOB } = cKzg;
import * as path from "path";
import { readJsonFile } from "./utils/json-io.js";
import { fileURLToPath } from "url";
import { dirname } from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
let abi: Abi;
let bytecode: Hex;
let deployedContractAddress: `0x${string}`;
// Deploy the blob submission contract (Blob.sol):
async function deployBlobSubmissionContract() {
const artifactPath = path.join(
__dirname,
"..",
"contracts",
"out",
"Blob.sol",
"Blob.json"
);
let data: any;
try {
data = await readJsonFile(artifactPath);
} catch (error) {
console.error("Error reading JSON file:", error);
}
abi = data.abi as Abi;
bytecode = data.bytecode.object;
const myAddress = (await client.getAddresses())[0];
const deploymentHash = await client.deployContract({
account,
abi,
bytecode,
});
let nonce = BigInt(
(await client.getTransactionCount({
address: myAddress,
})) - 1
);
deployedContractAddress = getContractAddress({
from: myAddress,
nonce,
});
}
let blob: Uint8Array;
let versionedHash: Uint8Array;
let inputBytes: Buffer;
let input: `0x${string}`;
function createBlobData() {
// Construct a blob:
// Let's design the blob so that we know some of the evaluations of p(X), so that we can validate this is all working correctly.
/**
* Not to scale ;)
*
* |
* 0xabcd | *
* |
* 0x1234 | *
* |
* 0x69 | *
* |_______________________
* 0 1 2
*
* p(X) would roughly interpolate these values.
* Except the x-coords aren't ascending integers; they're roots of unity.
* Not only that, but they're arranged in bit-reversal permutation, which needs
* a codebase of its own. I played with the python functions in the eth consensus
* specs to deduce a mapping from {0, 1, 2} above to the bit-reversal-permutation
* roots of unity:
* 0 -> 1
* 1 -> 52435875175126190479447740508185965837690552500527637822603658699938581184512
* = 0x73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000000
* (which is actually `-1` in the scalar field)
* 2 -> ok I haven't computed this yet.
*
*
* So to test the point evaluation precompile:
*
* C = commitment to the blob
*
* p(X) interpolation of the blob
*
* z0 = 1
* z1 = 0x73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000000
* z2 = ?
*
* y0 = p(z0) = 0x1234
* y1 = p(z1) = 0xabcd
* y2 = p(z2) = 0x69
*
* proof0 = commitment to q0(X) = (p(X) - p(z0)) / (X - z0)
* proof1 = commitment to q1(X) = (p(X) - p(z1)) / (X - z1)
*/
blob = Buffer.alloc(BYTES_PER_BLOB);
(blob as Buffer).write("1234", 32 - 2, 2, "hex");
(blob as Buffer).write("abcd", 2 * 32 - 2, 2, "hex"); // each value is offset by a 32-byte 'Field' (as per the definition of "Field" in the eip-4844 spec).
(blob as Buffer).write("69", 3 * 32 - 1, 1, "hex");
const commitment = viemKzg.blobToKzgCommitment(blob);
{
// These are the 0th and 1th roots of unity, taken from the eth consensus specs python lib.
let z0 = Buffer.alloc(32);
(z0 as Buffer).write("01", 31, "hex");
let z1 = Buffer.alloc(32);
(z1 as Buffer).write(
"73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000000",
0,
"hex"
);
// Notice: we have to move away from viem's restricted kzg methods and access the underlying
// cKzg methods, to compute a point at a z value that we can specify.
const [proof0, y0] = cKzg.computeKzgProof(blob, z0);
expect(bytesToHex(y0)).toBe(bytesToHex(blob.slice(0, 32)));
const [proof1, y1] = cKzg.computeKzgProof(blob, z1);
expect(bytesToHex(y1)).toBe(bytesToHex(blob.slice(32, 64)));
}
let z = Buffer.alloc(32);
(z as Buffer).write("02", 31, "hex");
const [proof, y] = cKzg.computeKzgProof(blob, z);
expect(bytesToHex(y)).toBe(
"0x049a902c5b4968d755b6f49c0231e15af80c62e352a428e8e9842eadc1c106bd"
);
versionedHash = commitmentToVersionedHash({ commitment });
inputBytes = Buffer.concat([versionedHash, z, y, commitment, proof]);
expect(inputBytes.length).toBe(192);
input = bytesToHex(inputBytes);
}
beforeEach(async () => {
await deployBlobSubmissionContract();
createBlobData();
});
test("Test basic blob submission to nowhere", async () => {
// First send just a blob to no particular contract or function:
const basicBlobRequest = await client.prepareTransactionRequest({
blobs: [blob],
kzg: viemKzg,
maxFeePerBlobGas: parseGwei("30"),
to: "0x0000000000000000000000000000000000000000",
maxPriorityFeePerGas: 1_000_000_000n,
maxFeePerGas: 2_051_413_230n,
gas: 10_000_000n,
});
const basicBlobSerializedTransaction = await client.signTransaction(
basicBlobRequest
);
// A Hardhat Network Node can only cope with blob transactions which are sent as raw transactions.
// An Anvil node seemingly can't cope with blob transactions.
// So let's send it as a raw transaction for the hardhat node's benefit.
const basicBlobHash = await client.sendRawTransaction({
serializedTransaction: basicBlobSerializedTransaction,
});
});
test("Test blob submission", async () => {
// Now the actual tx I want: sending a blob AND calling a particular function.
const submitBlobsFunctionData = encodeFunctionData({
abi,
functionName: "submitBlobs",
});
const submitBlobsRequest = await client.prepareTransactionRequest({
blobs: [blob],
kzg: viemKzg,
maxFeePerBlobGas: parseGwei("30"),
to: deployedContractAddress,
data: submitBlobsFunctionData,
// Interestingly, specifying these gas amounts enables hardhat to cope with this. Otherwise it makes an estimateGas call, and when it makes that call, it doesn't do a `send RAW Transaction` (it does a normal `sendTransaction`, so that breaks the hardhat node (it can't deal with non-raw blobby txs https://github.com/NomicFoundation/hardhat/issues/5182).
maxPriorityFeePerGas: 1_000_000_000n,
maxFeePerGas: 2_051_413_230n,
gas: 10_000_000n,
});
const serializedTransaction = await client.signTransaction(
submitBlobsRequest
);
// A Hardhat Network Node can only cope with blob transactions which are sent as raw transactions.
// An Anvil node seemingly can't cope with blob transactions.
// So let's send it as a raw transaction for the hardhat node's benefit.
const submitBlobsHash = await client.sendRawTransaction({
serializedTransaction,
});
await new Promise((f) => setTimeout(f, 1000));
const submitBlobsReceipt = await client.getTransactionReceipt({
hash: submitBlobsHash,
});
const submitBlobsLogs = parseEventLogs({
abi,
logs: submitBlobsReceipt.logs,
});
expect(submitBlobsLogs[0].eventName).toBe("BlobHash");
expect(submitBlobsLogs[0].args.hasOwnProperty("_blobhash")).toBe(true);
if ("_blobhash" in submitBlobsLogs[0].args) {
expect(submitBlobsLogs[0].args?._blobhash).toBe(bytesToHex(versionedHash));
}
//*************************************** */
// Now let's verify the kzg proof on-chain:
const verifyKzgProofFunctionData = encodeFunctionData({
abi,
functionName: "verifyKzgProof",
args: [input, 0],
});
const verifyKzgProofRequest = await client.prepareTransactionRequest({
to: deployedContractAddress,
data: verifyKzgProofFunctionData,
});
const verifyKzgProofSerializedTransaction = await client.signTransaction(
verifyKzgProofRequest
);
const verifyKzgProofHash = await client.sendRawTransaction({
serializedTransaction: verifyKzgProofSerializedTransaction,
});
await new Promise((f) => setTimeout(f, 1000));
const verifyKzgProofReceipt = await client.getTransactionReceipt({
hash: verifyKzgProofHash,
});
const verifyKzgProofLogs = parseEventLogs({
abi,
logs: verifyKzgProofReceipt.logs,
});
expect(verifyKzgProofLogs[0].eventName).toBe("PointEvaluationSuccess");
expect(verifyKzgProofLogs[0].args.hasOwnProperty("_success")).toBe(true);
if ("_success" in verifyKzgProofLogs[0].args) {
expect(verifyKzgProofLogs[0].args?._success).toBe(true);
}
//*************************************** */
// Now let's validate than an invalid input to `verifyKzgProof` will fail:
let badInputBytes = inputBytes;
badInputBytes.write("00", 100, "hex");
const badInput = bytesToHex(badInputBytes);
const badVerifyKzgProofFunctionData = encodeFunctionData({
abi,
functionName: "verifyKzgProof",
args: [badInput, 0],
});
const badVerifyKzgProofRequest = await client.prepareTransactionRequest({
to: deployedContractAddress,
data: badVerifyKzgProofFunctionData,
// Interestingly, specifying these gas amounts enables hardhat to cope with this:
maxPriorityFeePerGas: 1_000_000_000n,
maxFeePerGas: 2_051_413_230n,
gas: 10_000_000n,
});
const badVerifyKzgProofSerializedTransaction = await client.signTransaction(
badVerifyKzgProofRequest
);
let badVerifyKzgProofHash: Hex;
try {
badVerifyKzgProofHash = await client.sendRawTransaction({
serializedTransaction: badVerifyKzgProofSerializedTransaction,
});
} catch (e) {
const error = e as SendRawTransactionErrorType;
expect(error.name).toBe("InvalidInputRpcError");
if (error.name === "InvalidInputRpcError") {
expect(error.details).toBe(
"reverted with reason string 'Point evaluation precompile failed'"
);
}
}
//*************************************** */
});
test("Test point evaluation for non-existent blob (for batching)", async () => {
// Ok, in this test, the blob _hasn't_ been submitted, but this is still a
// valid kzg opening proof, so it should still work.
const verifyUnrelatedKzgProofFunctionData = encodeFunctionData({
abi,
functionName: "verifyUnrelatedKzgProof",
args: [input],
});
const verifyUnrelatedKzgProofRequest = await client.prepareTransactionRequest(
{
to: deployedContractAddress,
data: verifyUnrelatedKzgProofFunctionData,
}
);
const verifyUnrelatedKzgProofSerializedTransaction =
await client.signTransaction(verifyUnrelatedKzgProofRequest);
const verifyUnrelatedKzgProofHash = await client.sendRawTransaction({
serializedTransaction: verifyUnrelatedKzgProofSerializedTransaction,
});
await new Promise((f) => setTimeout(f, 1000));
const verifyUnrelatedKzgProofReceipt = await client.getTransactionReceipt({
hash: verifyUnrelatedKzgProofHash,
});
const verifyUnrelatedKzgProofLogs = parseEventLogs({
abi,
logs: verifyUnrelatedKzgProofReceipt.logs,
});
expect(verifyUnrelatedKzgProofLogs[0].eventName).toBe(
"PointEvaluationSuccess"
);
expect(verifyUnrelatedKzgProofLogs[0].args.hasOwnProperty("_success")).toBe(
true
);
if ("_success" in verifyUnrelatedKzgProofLogs[0].args) {
expect(verifyUnrelatedKzgProofLogs[0].args?._success).toBe(true);
}
//*************************************** */
});