forked from paradigmxyz/zk-eth-rng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateBlockInfo.ts
65 lines (52 loc) · 2.12 KB
/
generateBlockInfo.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
import fs from "fs";
import _ from "lodash";
import { ethers } from "ethers";
import minimist from "minimist";
import { BLOCK_HEADER_FIELDS, cleanHeaderFields, getBlockHeaderFields } from "./utils";
async function generateBlockInfo(rpcUrl: string, blockNum: number) {
// Get block data fields from RPC.
console.log("Fetching block data from RPC...", {
rpcUrl,
blockNum,
})
const block = await getBlockHeaderFields(rpcUrl, blockNum);
const expectedBlockhash = block.hash;
// Clean required block header fields for RLP format spec.
console.log("Cleaning block header fields");
const cleanedHeaderFields = cleanHeaderFields(block);
// RLP encode header fields.
console.log("RLP encoding header fields");
const rlp = ethers.utils.RLP.encode(_.at(cleanedHeaderFields, BLOCK_HEADER_FIELDS));
// Derive blockhash from RLP encoded header.
const derivedBlockhash = ethers.utils.keccak256(rlp);
console.log("Derived blockhash from RLP encoded header", derivedBlockhash);
// Sanity check derived blockhash matches blockhash from RPC.
if (derivedBlockhash !== expectedBlockhash) {
throw new Error("Blockhash mismatch, might be computing blockhash for pre-1559 blocks!");
}
// Construct block info object
const blockInfo = {
cleanedHeaderFields,
rlp,
goldenHash: block.hash,
calculatedHash: ethers.utils.keccak256(rlp),
};
console.log("Block info:", blockInfo);
// Write object
fs.writeFileSync(
`../contracts/testdata/blockheaderinfo/${blockNum}.json`,
JSON.stringify(blockInfo, null, 2)
);
}
const argv = minimist(process.argv.slice(2));
const blockNum = parseInt(argv.blockNum || process.env.BLOCK_NUM, 10);
const rpcUrl = argv.rpc || process.env.RPC_URL;
console.log("Parsed inputs", { blockNum, rpcUrl });
if (!blockNum) {
throw new Error("CLI arg 'blockNum' is required!")
}
if (!rpcUrl) {
throw new Error("CLI arg 'rpc' is required!")
}
// usage: $ yarn ts-node generateBlockInfo.ts --blockNum 15539395 --rpc https://ethereum-mainnet-rpc.allthatnode.com
generateBlockInfo(rpcUrl, blockNum);