@lodestar/prover 1.35.0-dev.e18102ed8c → 1.35.0-dev.f45a2be721

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/bin/lodestar-prover.js +3 -0
  2. package/lib/browser/index.d.ts.map +1 -0
  3. package/lib/cli/applyPreset.d.ts.map +1 -0
  4. package/lib/cli/cli.d.ts.map +1 -0
  5. package/lib/cli/cmds/index.d.ts.map +1 -0
  6. package/lib/cli/cmds/start/handler.d.ts.map +1 -0
  7. package/lib/cli/cmds/start/index.d.ts.map +1 -0
  8. package/lib/cli/cmds/start/options.d.ts.map +1 -0
  9. package/lib/cli/index.d.ts.map +1 -0
  10. package/lib/cli/options.d.ts.map +1 -0
  11. package/lib/constants.d.ts.map +1 -0
  12. package/lib/index.d.ts.map +1 -0
  13. package/lib/interfaces.d.ts.map +1 -0
  14. package/lib/proof_provider/index.d.ts.map +1 -0
  15. package/lib/proof_provider/ordered_map.d.ts.map +1 -0
  16. package/lib/proof_provider/payload_store.d.ts.map +1 -0
  17. package/lib/proof_provider/proof_provider.d.ts.map +1 -0
  18. package/lib/provider_types/eip1193_provider_type.d.ts.map +1 -0
  19. package/lib/provider_types/ethers_provider_type.d.ts.map +1 -0
  20. package/lib/provider_types/legacy_provider_type.d.ts.map +1 -0
  21. package/lib/provider_types/web3_js_provider_type.d.ts.map +1 -0
  22. package/lib/types.d.ts.map +1 -0
  23. package/lib/utils/assertion.d.ts.map +1 -0
  24. package/lib/utils/consensus.d.ts.map +1 -0
  25. package/lib/utils/conversion.d.ts.map +1 -0
  26. package/lib/utils/errors.d.ts.map +1 -0
  27. package/lib/utils/evm.d.ts.map +1 -0
  28. package/lib/utils/execution.d.ts.map +1 -0
  29. package/lib/utils/file.d.ts.map +1 -0
  30. package/lib/utils/gitData/gitDataPath.d.ts.map +1 -0
  31. package/lib/utils/gitData/index.d.ts.map +1 -0
  32. package/lib/utils/gitData/writeGitData.d.ts.map +1 -0
  33. package/lib/utils/json_rpc.d.ts.map +1 -0
  34. package/lib/utils/process.d.ts.map +1 -0
  35. package/lib/utils/req_resp.d.ts.map +1 -0
  36. package/lib/utils/rpc_provider.d.ts.map +1 -0
  37. package/lib/utils/validation.d.ts.map +1 -0
  38. package/lib/utils/verification.d.ts.map +1 -0
  39. package/lib/utils/version.d.ts.map +1 -0
  40. package/lib/verified_requests/eth_call.d.ts.map +1 -0
  41. package/lib/verified_requests/eth_estimateGas.d.ts.map +1 -0
  42. package/lib/verified_requests/eth_getBalance.d.ts.map +1 -0
  43. package/lib/verified_requests/eth_getBlockByHash.d.ts.map +1 -0
  44. package/lib/verified_requests/eth_getBlockByNumber.d.ts.map +1 -0
  45. package/lib/verified_requests/eth_getCode.d.ts.map +1 -0
  46. package/lib/verified_requests/eth_getTransactionCount.d.ts.map +1 -0
  47. package/lib/web3_provider.d.ts.map +1 -0
  48. package/lib/web3_provider_inspector.d.ts.map +1 -0
  49. package/lib/web3_proxy.d.ts.map +1 -0
  50. package/package.json +16 -15
  51. package/src/browser/index.ts +3 -0
  52. package/src/cli/applyPreset.ts +83 -0
  53. package/src/cli/cli.ts +58 -0
  54. package/src/cli/cmds/index.ts +7 -0
  55. package/src/cli/cmds/start/handler.ts +27 -0
  56. package/src/cli/cmds/start/index.ts +18 -0
  57. package/src/cli/cmds/start/options.ts +85 -0
  58. package/src/cli/index.ts +30 -0
  59. package/src/cli/options.ts +73 -0
  60. package/src/constants.ts +6 -0
  61. package/src/index.ts +5 -0
  62. package/src/interfaces.ts +90 -0
  63. package/src/proof_provider/index.ts +1 -0
  64. package/src/proof_provider/ordered_map.ts +25 -0
  65. package/src/proof_provider/payload_store.ts +223 -0
  66. package/src/proof_provider/proof_provider.ts +210 -0
  67. package/src/provider_types/eip1193_provider_type.ts +32 -0
  68. package/src/provider_types/ethers_provider_type.ts +44 -0
  69. package/src/provider_types/legacy_provider_type.ts +123 -0
  70. package/src/provider_types/web3_js_provider_type.ts +35 -0
  71. package/src/types.ts +163 -0
  72. package/src/utils/assertion.ts +11 -0
  73. package/src/utils/consensus.ts +122 -0
  74. package/src/utils/conversion.ts +107 -0
  75. package/src/utils/errors.ts +4 -0
  76. package/src/utils/evm.ts +284 -0
  77. package/src/utils/execution.ts +76 -0
  78. package/src/utils/file.ts +51 -0
  79. package/src/utils/gitData/gitDataPath.ts +48 -0
  80. package/src/utils/gitData/index.ts +70 -0
  81. package/src/utils/gitData/writeGitData.ts +10 -0
  82. package/src/utils/json_rpc.ts +170 -0
  83. package/src/utils/process.ts +111 -0
  84. package/src/utils/req_resp.ts +34 -0
  85. package/src/utils/rpc_provider.ts +117 -0
  86. package/src/utils/validation.ts +161 -0
  87. package/src/utils/verification.ts +112 -0
  88. package/src/utils/version.ts +74 -0
  89. package/src/verified_requests/eth_call.ts +50 -0
  90. package/src/verified_requests/eth_estimateGas.ts +49 -0
  91. package/src/verified_requests/eth_getBalance.ts +26 -0
  92. package/src/verified_requests/eth_getBlockByHash.ts +24 -0
  93. package/src/verified_requests/eth_getBlockByNumber.ts +25 -0
  94. package/src/verified_requests/eth_getCode.ts +50 -0
  95. package/src/verified_requests/eth_getTransactionCount.ts +26 -0
  96. package/src/web3_provider.ts +58 -0
  97. package/src/web3_provider_inspector.ts +88 -0
  98. package/src/web3_proxy.ts +175 -0
@@ -0,0 +1,284 @@
1
+ import {Block, BlockHeader} from "@ethereumjs/block";
2
+ import {Blockchain} from "@ethereumjs/blockchain";
3
+ import {TransactionFactory} from "@ethereumjs/tx";
4
+ import {Account, Address} from "@ethereumjs/util";
5
+ import {RunTxResult, VM} from "@ethereumjs/vm";
6
+ import {NetworkName} from "@lodestar/config/networks";
7
+ import {ExecutionPayload} from "@lodestar/types";
8
+ import {Logger} from "@lodestar/utils";
9
+ import {ZERO_ADDRESS} from "../constants.js";
10
+ import {ProofProvider} from "../proof_provider/proof_provider.js";
11
+ import {ELBlock, ELProof, ELTransaction, JsonRpcVersion} from "../types.js";
12
+ import {bufferToHex, chunkIntoN, cleanObject, hexToBigInt, hexToBuffer, numberToHex, padLeft} from "./conversion.js";
13
+ import {getChainCommon, getTxType} from "./execution.js";
14
+ import {isValidResponse} from "./json_rpc.js";
15
+ import {ELRpcProvider} from "./rpc_provider.js";
16
+ import {isNullish, isValidAccount, isValidCodeHash, isValidStorageKeys} from "./validation.js";
17
+
18
+ export async function createVM({proofProvider}: {proofProvider: ProofProvider}): Promise<VM> {
19
+ const common = getChainCommon(proofProvider.config.PRESET_BASE as string);
20
+ const blockchain = await Blockchain.create({common});
21
+
22
+ // Connect blockchain object with existing proof provider for block history
23
+ // biome-ignore lint/suspicious/noExplicitAny: We need to use `any` type here
24
+ (blockchain as any).getBlock = async (blockId: number) => {
25
+ const payload = await proofProvider.getExecutionPayload(blockId);
26
+ return {
27
+ hash: () => payload.blockHash,
28
+ };
29
+ };
30
+ const vm = await VM.create({common, blockchain});
31
+
32
+ return vm;
33
+ }
34
+
35
+ export async function getVMWithState({
36
+ rpc,
37
+ executionPayload,
38
+ tx,
39
+ vm,
40
+ logger,
41
+ }: {
42
+ rpc: ELRpcProvider;
43
+ vm: VM;
44
+ executionPayload: ExecutionPayload;
45
+ tx: ELTransaction;
46
+ logger: Logger;
47
+ }): Promise<VM> {
48
+ const {stateRoot, blockHash, gasLimit} = executionPayload;
49
+ const blockHashHex = bufferToHex(blockHash);
50
+
51
+ // If tx does not have a from address then it must be initiated via zero address
52
+ const from = tx.from ?? ZERO_ADDRESS;
53
+ const to = tx.to;
54
+
55
+ // Create Access List for the contract call
56
+ const accessListTx = cleanObject({
57
+ to,
58
+ from,
59
+ data: tx.input ? tx.input : tx.data,
60
+ value: tx.value,
61
+ gas: tx.gas ? tx.gas : numberToHex(gasLimit),
62
+ gasPrice: "0x0",
63
+ }) as ELTransaction;
64
+ const response = await rpc.request("eth_createAccessList", [accessListTx, blockHashHex], {raiseError: false});
65
+
66
+ if (!isValidResponse(response) || response.result.error) {
67
+ throw new Error(`Invalid response from RPC. method: eth_createAccessList, params: ${JSON.stringify(tx)}`);
68
+ }
69
+
70
+ const storageKeysMap: Record<string, string[]> = {};
71
+ for (const {address, storageKeys} of response.result.accessList) {
72
+ storageKeysMap[address] = storageKeys;
73
+ }
74
+
75
+ // If from address is not present then we have to fetch it for all keys
76
+ if (isNullish(storageKeysMap[from])) {
77
+ storageKeysMap[from] = [];
78
+ }
79
+
80
+ // If to address is not present then we have to fetch it with for all keys
81
+ if (to && isNullish(storageKeysMap[to])) {
82
+ storageKeysMap[to] = [];
83
+ }
84
+
85
+ const batchRequests = [];
86
+ for (const [address, storageKeys] of Object.entries(storageKeysMap)) {
87
+ batchRequests.push({
88
+ jsonrpc: "2.0" as JsonRpcVersion,
89
+ id: rpc.getRequestId(),
90
+ method: "eth_getProof",
91
+ params: [address, storageKeys, blockHashHex],
92
+ });
93
+
94
+ batchRequests.push({
95
+ jsonrpc: "2.0" as JsonRpcVersion,
96
+ id: rpc.getRequestId(),
97
+ method: "eth_getCode",
98
+ params: [address, blockHashHex],
99
+ });
100
+ }
101
+
102
+ // If all responses are valid then we will have even number of responses
103
+ // For each address, one response for eth_getProof and one for eth_getCode
104
+ const batchResponse = await rpc.batchRequest(batchRequests, {raiseError: true});
105
+ const batchResponseInChunks = chunkIntoN(batchResponse, 2);
106
+
107
+ const vmState: VMState = {};
108
+ for (const [proofResponse, codeResponse] of batchResponseInChunks) {
109
+ const addressHex = proofResponse.request.params[0] as string;
110
+ if (!isNullish(vmState[addressHex])) continue;
111
+
112
+ const proof = proofResponse.response.result as ELProof;
113
+ const storageKeys = proofResponse.request.params[1] as string[];
114
+ const code = codeResponse.response.result as string;
115
+
116
+ const validAccount = await isValidAccount({address: addressHex, proof, logger, stateRoot});
117
+ const validStorage = validAccount && (await isValidStorageKeys({storageKeys, proof, logger}));
118
+ if (!validAccount || !validStorage) {
119
+ throw new Error(`Invalid account: ${addressHex}`);
120
+ }
121
+
122
+ if (!(await isValidCodeHash({codeResponse: code, logger, codeHash: proof.codeHash}))) {
123
+ throw new Error(`Invalid code hash: ${addressHex}`);
124
+ }
125
+
126
+ vmState[addressHex] = {code, proof};
127
+ }
128
+
129
+ return updateVMWithState({vm, state: vmState, logger});
130
+ }
131
+ type VMState = Record<string, {code: string; proof: ELProof}>;
132
+ export async function updateVMWithState({vm, state}: {logger: Logger; state: VMState; vm: VM}): Promise<VM> {
133
+ await vm.stateManager.checkpoint();
134
+ for (const [addressHex, {proof, code}] of Object.entries(state)) {
135
+ const address = Address.fromString(addressHex);
136
+ const codeBuffer = hexToBuffer(code);
137
+
138
+ const account = Account.fromAccountData({
139
+ nonce: BigInt(proof.nonce),
140
+ balance: BigInt(proof.balance),
141
+ codeHash: proof.codeHash,
142
+ });
143
+
144
+ await vm.stateManager.putAccount(address, account);
145
+
146
+ for (const {key, value} of proof.storageProof) {
147
+ await vm.stateManager.putContractStorage(address, padLeft(hexToBuffer(key), 32), padLeft(hexToBuffer(value), 32));
148
+ }
149
+ if (codeBuffer.byteLength !== 0) await vm.stateManager.putContractCode(address, codeBuffer);
150
+ }
151
+
152
+ await vm.stateManager.commit();
153
+ return vm;
154
+ }
155
+
156
+ export async function executeVMCall({
157
+ rpc,
158
+ tx,
159
+ vm,
160
+ executionPayload,
161
+ network,
162
+ }: {
163
+ rpc: ELRpcProvider;
164
+ tx: ELTransaction;
165
+ vm: VM;
166
+ executionPayload: ExecutionPayload;
167
+ network: NetworkName;
168
+ }): Promise<RunTxResult["execResult"]> {
169
+ const {from, to, gas, gasPrice, maxPriorityFeePerGas, value, data, input} = tx;
170
+ const blockHash = bufferToHex(executionPayload.blockHash);
171
+ const {result: block} = await rpc.request("eth_getBlockByHash", [blockHash, true], {
172
+ raiseError: true,
173
+ });
174
+
175
+ if (!block) {
176
+ throw new Error(`Block not found: ${blockHash}`);
177
+ }
178
+
179
+ const {execResult} = await vm.evm.runCall({
180
+ caller: from ? Address.fromString(from) : undefined,
181
+ to: to ? Address.fromString(to) : undefined,
182
+ gasLimit: hexToBigInt(gas ?? block.gasLimit),
183
+ gasPrice: hexToBigInt(gasPrice ?? maxPriorityFeePerGas ?? "0x0"),
184
+ value: hexToBigInt(value ?? "0x0"),
185
+ data: input ? hexToBuffer(input) : data ? hexToBuffer(data) : undefined,
186
+ block: {
187
+ header: getVMBlockHeaderFromELBlock(block, executionPayload, network),
188
+ },
189
+ });
190
+
191
+ if (execResult.exceptionError) {
192
+ throw new Error(execResult.exceptionError.error);
193
+ }
194
+
195
+ return execResult;
196
+ }
197
+
198
+ export async function executeVMTx({
199
+ rpc,
200
+ tx,
201
+ vm,
202
+ executionPayload,
203
+ network,
204
+ }: {
205
+ rpc: ELRpcProvider;
206
+ tx: ELTransaction;
207
+ vm: VM;
208
+ executionPayload: ExecutionPayload;
209
+ network: NetworkName;
210
+ }): Promise<RunTxResult> {
211
+ const {result: block} = await rpc.request("eth_getBlockByHash", [bufferToHex(executionPayload.blockHash), true], {
212
+ raiseError: true,
213
+ });
214
+
215
+ if (!block) {
216
+ throw new Error(`Block not found: ${bufferToHex(executionPayload.blockHash)}`);
217
+ }
218
+ const txType = getTxType(tx);
219
+ const from = tx.from ? Address.fromString(tx.from) : Address.zero();
220
+ const to = tx.to ? Address.fromString(tx.to) : undefined;
221
+
222
+ const txData = {
223
+ ...tx,
224
+ from,
225
+ to,
226
+ type: txType,
227
+ // If no gas limit is specified use the last block gas limit as an upper bound.
228
+ gasLimit: hexToBigInt(tx.gas ?? block.gasLimit),
229
+ };
230
+
231
+ if (txType === 2) {
232
+ // Handle EIP-1559 transactions
233
+ // To fix the vm error: Transaction's maxFeePerGas (0) is less than the block's baseFeePerGas
234
+ txData.maxFeePerGas = txData.maxFeePerGas ?? block.baseFeePerGas;
235
+ } else {
236
+ // Legacy transaction
237
+ txData.gasPrice = isNullish(txData.gasPrice) || txData.gasPrice === "0x0" ? block.baseFeePerGas : txData.gasPrice;
238
+ }
239
+
240
+ const txObject = TransactionFactory.fromTxData(txData, {common: getChainCommon(network), freeze: false});
241
+
242
+ // Override to avoid tx signature verification
243
+ txObject.getSenderAddress = () => (tx.from ? Address.fromString(tx.from) : Address.zero());
244
+
245
+ const result = await vm.runTx({
246
+ tx: txObject,
247
+ skipNonce: true,
248
+ skipBalance: true,
249
+ skipBlockGasLimitValidation: true,
250
+ skipHardForkValidation: true,
251
+ block: {
252
+ header: getVMBlockHeaderFromELBlock(block, executionPayload, network),
253
+ } as Block,
254
+ });
255
+
256
+ return result;
257
+ }
258
+
259
+ export function getVMBlockHeaderFromELBlock(
260
+ block: ELBlock,
261
+ executionPayload: ExecutionPayload,
262
+ network: NetworkName
263
+ ): BlockHeader {
264
+ const blockHeaderData = {
265
+ number: hexToBigInt(block.number),
266
+ cliqueSigner: () => Address.fromString(block.miner),
267
+ timestamp: hexToBigInt(block.timestamp),
268
+ difficulty: hexToBigInt(block.difficulty),
269
+ gasLimit: hexToBigInt(block.gasLimit),
270
+ baseFeePerGas: block.baseFeePerGas ? hexToBigInt(block.baseFeePerGas) : undefined,
271
+
272
+ // Use these values from the execution payload
273
+ // instead of the block values to ensure that
274
+ // the VM is using the verified values from the lightclient
275
+ prevRandao: Buffer.from(executionPayload.prevRandao),
276
+ stateRoot: Buffer.from(executionPayload.stateRoot),
277
+ parentHash: Buffer.from(executionPayload.parentHash),
278
+
279
+ // TODO: Fix the coinbase address
280
+ coinbase: Address.fromString(block.miner),
281
+ };
282
+
283
+ return BlockHeader.fromHeaderData(blockHeaderData, {common: getChainCommon(network)});
284
+ }
@@ -0,0 +1,76 @@
1
+ import {Common, CustomChain, Hardfork} from "@ethereumjs/common";
2
+ import {ELApiParams, ELApiReturn, ELTransaction} from "../types.js";
3
+ import {isValidResponse} from "./json_rpc.js";
4
+ import {ELRpcProvider} from "./rpc_provider.js";
5
+ import {isBlockNumber, isPresent} from "./validation.js";
6
+
7
+ export type Optional<T, K extends keyof T> = Omit<T, K> & {[P in keyof T]?: T[P] | undefined};
8
+
9
+ export async function getELCode(
10
+ rpc: ELRpcProvider,
11
+ args: ELApiParams["eth_getCode"]
12
+ ): Promise<ELApiReturn["eth_getCode"]> {
13
+ const codeResult = await rpc.request("eth_getCode", args, {raiseError: false});
14
+
15
+ if (!isValidResponse(codeResult)) {
16
+ throw new Error(`Can not find code for address=${args[0]}`);
17
+ }
18
+
19
+ return codeResult.result;
20
+ }
21
+
22
+ export async function getELProof(
23
+ rpc: ELRpcProvider,
24
+ args: ELApiParams["eth_getProof"]
25
+ ): Promise<ELApiReturn["eth_getProof"]> {
26
+ const proof = await rpc.request("eth_getProof", args, {raiseError: false});
27
+ if (!isValidResponse(proof)) {
28
+ throw new Error(`Can not find proof for address=${args[0]}`);
29
+ }
30
+ return proof.result;
31
+ }
32
+
33
+ export async function getELBlock(
34
+ rpc: ELRpcProvider,
35
+ args: ELApiParams["eth_getBlockByNumber"]
36
+ ): Promise<ELApiReturn["eth_getBlockByNumber"]> {
37
+ const block = await rpc.request(isBlockNumber(args[0]) ? "eth_getBlockByNumber" : "eth_getBlockByHash", args, {
38
+ raiseError: false,
39
+ });
40
+
41
+ if (!isValidResponse(block)) {
42
+ throw new Error(`Can not find block. id=${args[0]}`);
43
+ }
44
+
45
+ return block.result;
46
+ }
47
+
48
+ export function getChainCommon(network: string): Common {
49
+ switch (network) {
50
+ case "mainnet":
51
+ case "sepolia":
52
+ case "holesky":
53
+ case "ephemery":
54
+ // TODO: Not sure how to detect the fork during runtime
55
+ return new Common({chain: network, hardfork: Hardfork.Shanghai});
56
+ case "minimal":
57
+ // TODO: Not sure how to detect the fork during runtime
58
+ return new Common({chain: "mainnet", hardfork: Hardfork.Shanghai});
59
+ case "gnosis":
60
+ return new Common({chain: CustomChain.xDaiChain});
61
+ default:
62
+ throw new Error(`Non supported network "${network}"`);
63
+ }
64
+ }
65
+
66
+ export function getTxType(tx: ELTransaction): number {
67
+ if (isPresent(tx.maxFeePerGas) || isPresent(tx.maxPriorityFeePerGas)) {
68
+ return 2;
69
+ }
70
+
71
+ if (isPresent(tx.accessList)) {
72
+ return 1;
73
+ }
74
+
75
+ return 0;
76
+ }
@@ -0,0 +1,51 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import yaml from "js-yaml";
4
+
5
+ const {load, FAILSAFE_SCHEMA, Type} = yaml;
6
+
7
+ enum FileFormat {
8
+ json = "json",
9
+ yaml = "yaml",
10
+ yml = "yml",
11
+ toml = "toml",
12
+ }
13
+
14
+ const yamlSchema = FAILSAFE_SCHEMA.extend({
15
+ implicit: [
16
+ new Type("tag:yaml.org,2002:str", {
17
+ kind: "scalar",
18
+ construct: function construct(data) {
19
+ return data !== null ? data : "";
20
+ },
21
+ }),
22
+ ],
23
+ });
24
+
25
+ /**
26
+ * Parse file contents as Json.
27
+ */
28
+ function parse<T>(contents: string, fileFormat: FileFormat): T {
29
+ switch (fileFormat) {
30
+ case FileFormat.json:
31
+ return JSON.parse(contents) as T;
32
+ case FileFormat.yaml:
33
+ case FileFormat.yml:
34
+ return load(contents, {schema: yamlSchema}) as T;
35
+ default:
36
+ return contents as unknown as T;
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Read a JSON serializable object from a file
42
+ *
43
+ * Parse either from json, yaml, or toml
44
+ * Optional acceptedFormats object can be passed which can be an array of accepted formats, in future can be extended to include parseFn for the accepted formats
45
+ */
46
+ export function readFile<T>(filepath: string, acceptedFormats?: string[]): T {
47
+ const fileFormat = path.extname(filepath).substr(1);
48
+ if (acceptedFormats && !acceptedFormats.includes(fileFormat)) throw new Error(`UnsupportedFileFormat: ${filepath}`);
49
+ const contents = fs.readFileSync(filepath, "utf-8");
50
+ return parse(contents, fileFormat as FileFormat);
51
+ }
@@ -0,0 +1,48 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import {fileURLToPath} from "node:url";
4
+
5
+ // Global variable __dirname no longer available in ES6 modules.
6
+ // Solutions: https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-js-when-using-es6-modules
7
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
8
+
9
+ // Persist git data and distribute through NPM so CLI consumers can know exactly
10
+ // at what commit was this src build. This is used in the metrics and to log initially.
11
+ //
12
+ // - For NPM release (stable): Only the version is persisted. Once must then track the version's tag
13
+ // in Github to resolve that version to a specific commit. While this is okay, git-data.json gives
14
+ // a gurantee of the exact commit at build time.
15
+ //
16
+ // - For NPM release (dev): canary commits include the commit, so this feature is not really
17
+ // necessary. However, it's more cumbersome to have conditional logic on stable / dev.
18
+ //
19
+ // - For build from source: .git folder is available in the context of the built code, so it can extract
20
+ // branch and commit directly without the need for .git-data.json.
21
+ //
22
+ // - For build from source dockerized: This feature is required to know the branch and commit, since
23
+ // git data is not persisted past the build. However, .dockerignore prevents .git folder from being
24
+ // copied into the container's context, so .git-data.json can't be generated.
25
+
26
+ /**
27
+ * WARNING!! If you change this path make sure to update:
28
+ * - 'packages/cli/package.json' -> .files -> `".git-data.json"`
29
+ */
30
+ export const gitDataPath = path.resolve(__dirname, "../../../.git-data.json");
31
+
32
+ /** Git data type used to construct version information string and persistence. */
33
+ export type GitData = {
34
+ /** "developer-feature" */
35
+ branch: string;
36
+ /** "80c248bb392f512cc115d95059e22239a17bbd7d" */
37
+ commit: string;
38
+ };
39
+
40
+ /** Writes a persistent git data file. */
41
+ export function writeGitDataFile(gitData: GitData): void {
42
+ fs.writeFileSync(gitDataPath, JSON.stringify(gitData, null, 2));
43
+ }
44
+
45
+ /** Reads the persistent git data file. */
46
+ export function readGitDataFile(): GitData {
47
+ return JSON.parse(fs.readFileSync(gitDataPath, "utf8")) as GitData;
48
+ }
@@ -0,0 +1,70 @@
1
+ import {execSync} from "node:child_process";
2
+ // This file is created in the build step and is distributed through NPM
3
+ // MUST be in sync with `-/gitDataPath.ts` and `package.json` files.
4
+ import {GitData, readGitDataFile} from "./gitDataPath.js";
5
+
6
+ /** Reads git data from a persisted file or local git data at build time. */
7
+ export function readAndGetGitData(): GitData {
8
+ try {
9
+ // Gets git data containing current branch and commit info from persistent file.
10
+ let persistedGitData: Partial<GitData>;
11
+ try {
12
+ persistedGitData = readGitDataFile();
13
+ } catch (_e) {
14
+ persistedGitData = {};
15
+ }
16
+
17
+ const currentGitData = getGitData();
18
+
19
+ return {
20
+ // If the CLI is run from source, prioritze current git data
21
+ // over `.git-data.json` file, which might be stale here.
22
+ branch:
23
+ currentGitData.branch && currentGitData.branch.length > 0
24
+ ? currentGitData.branch
25
+ : (persistedGitData.branch ?? ""),
26
+ commit:
27
+ currentGitData.commit && currentGitData.commit.length > 0
28
+ ? currentGitData.commit
29
+ : (persistedGitData.commit ?? ""),
30
+ };
31
+ } catch (_e) {
32
+ return {
33
+ branch: "",
34
+ commit: "",
35
+ };
36
+ }
37
+ }
38
+
39
+ /** Gets git data containing current branch and commit info from CLI. */
40
+ export function getGitData(): GitData {
41
+ return {
42
+ branch: process.env.GIT_BRANCH ?? getBranch(),
43
+ commit: process.env.GIT_COMMIT ?? getCommit(),
44
+ };
45
+ }
46
+
47
+ /** Tries to get branch from git CLI. */
48
+ function getBranch(): string {
49
+ try {
50
+ return shellSilent("git rev-parse --abbrev-ref HEAD");
51
+ } catch (_e) {
52
+ return "";
53
+ }
54
+ }
55
+
56
+ /** Tries to get commit from git from git CLI. */
57
+ function getCommit(): string {
58
+ try {
59
+ return shellSilent("git rev-parse --verify HEAD");
60
+ } catch (_e) {
61
+ return "";
62
+ }
63
+ }
64
+
65
+ /** Silent shell that won't pollute stdout, or stderr */
66
+ function shellSilent(cmd: string): string {
67
+ return execSync(cmd, {stdio: ["ignore", "pipe", "ignore"]})
68
+ .toString()
69
+ .trim();
70
+ }
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env node
2
+
3
+ // For RATIONALE of this file, check packages/cli/src/util/gitData/gitDataPath.ts
4
+ // Persist exact commit in NPM distributions for easier tracking of the build
5
+
6
+ import {writeGitDataFile} from "./gitDataPath.js";
7
+ import {getGitData} from "./index.js";
8
+
9
+ // Script to write the git data file (json) used by the build procedures to persist git data.
10
+ writeGitDataFile(getGitData());