@lodestar/prover 1.35.0-dev.8ea34e52ba → 1.35.0-dev.91dadf81de

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 (111) 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/cli.js +1 -1
  6. package/lib/cli/cli.js.map +1 -1
  7. package/lib/cli/cmds/index.d.ts.map +1 -0
  8. package/lib/cli/cmds/index.js.map +1 -1
  9. package/lib/cli/cmds/start/handler.d.ts.map +1 -0
  10. package/lib/cli/cmds/start/index.d.ts.map +1 -0
  11. package/lib/cli/cmds/start/options.d.ts.map +1 -0
  12. package/lib/cli/index.d.ts.map +1 -0
  13. package/lib/cli/index.js.map +1 -1
  14. package/lib/cli/options.d.ts.map +1 -0
  15. package/lib/constants.d.ts.map +1 -0
  16. package/lib/index.d.ts +1 -1
  17. package/lib/index.d.ts.map +1 -0
  18. package/lib/index.js +1 -1
  19. package/lib/index.js.map +1 -1
  20. package/lib/interfaces.d.ts.map +1 -0
  21. package/lib/proof_provider/index.d.ts.map +1 -0
  22. package/lib/proof_provider/ordered_map.d.ts.map +1 -0
  23. package/lib/proof_provider/payload_store.d.ts.map +1 -0
  24. package/lib/proof_provider/proof_provider.d.ts.map +1 -0
  25. package/lib/provider_types/eip1193_provider_type.d.ts.map +1 -0
  26. package/lib/provider_types/ethers_provider_type.d.ts.map +1 -0
  27. package/lib/provider_types/legacy_provider_type.d.ts.map +1 -0
  28. package/lib/provider_types/web3_js_provider_type.d.ts.map +1 -0
  29. package/lib/types.d.ts.map +1 -0
  30. package/lib/utils/assertion.d.ts.map +1 -0
  31. package/lib/utils/consensus.d.ts.map +1 -0
  32. package/lib/utils/conversion.d.ts.map +1 -0
  33. package/lib/utils/errors.d.ts.map +1 -0
  34. package/lib/utils/evm.d.ts.map +1 -0
  35. package/lib/utils/execution.d.ts.map +1 -0
  36. package/lib/utils/file.d.ts.map +1 -0
  37. package/lib/utils/file.js.map +1 -1
  38. package/lib/utils/gitData/gitDataPath.d.ts.map +1 -0
  39. package/lib/utils/gitData/index.d.ts.map +1 -0
  40. package/lib/utils/gitData/index.js.map +1 -1
  41. package/lib/utils/gitData/writeGitData.d.ts.map +1 -0
  42. package/lib/utils/json_rpc.d.ts.map +1 -0
  43. package/lib/utils/process.d.ts.map +1 -0
  44. package/lib/utils/req_resp.d.ts.map +1 -0
  45. package/lib/utils/rpc_provider.d.ts.map +1 -0
  46. package/lib/utils/validation.d.ts.map +1 -0
  47. package/lib/utils/validation.js.map +1 -1
  48. package/lib/utils/verification.d.ts.map +1 -0
  49. package/lib/utils/version.d.ts.map +1 -0
  50. package/lib/verified_requests/eth_call.d.ts.map +1 -0
  51. package/lib/verified_requests/eth_estimateGas.d.ts.map +1 -0
  52. package/lib/verified_requests/eth_getBalance.d.ts.map +1 -0
  53. package/lib/verified_requests/eth_getBlockByHash.d.ts.map +1 -0
  54. package/lib/verified_requests/eth_getBlockByNumber.d.ts.map +1 -0
  55. package/lib/verified_requests/eth_getCode.d.ts.map +1 -0
  56. package/lib/verified_requests/eth_getTransactionCount.d.ts.map +1 -0
  57. package/lib/web3_provider.d.ts.map +1 -0
  58. package/lib/web3_provider_inspector.d.ts.map +1 -0
  59. package/lib/web3_provider_inspector.js.map +1 -1
  60. package/lib/web3_proxy.d.ts.map +1 -0
  61. package/lib/web3_proxy.js +1 -1
  62. package/lib/web3_proxy.js.map +1 -1
  63. package/package.json +16 -15
  64. package/src/browser/index.ts +3 -0
  65. package/src/cli/applyPreset.ts +83 -0
  66. package/src/cli/cli.ts +58 -0
  67. package/src/cli/cmds/index.ts +7 -0
  68. package/src/cli/cmds/start/handler.ts +27 -0
  69. package/src/cli/cmds/start/index.ts +18 -0
  70. package/src/cli/cmds/start/options.ts +85 -0
  71. package/src/cli/index.ts +30 -0
  72. package/src/cli/options.ts +73 -0
  73. package/src/constants.ts +6 -0
  74. package/src/index.ts +5 -0
  75. package/src/interfaces.ts +90 -0
  76. package/src/proof_provider/index.ts +1 -0
  77. package/src/proof_provider/ordered_map.ts +25 -0
  78. package/src/proof_provider/payload_store.ts +223 -0
  79. package/src/proof_provider/proof_provider.ts +210 -0
  80. package/src/provider_types/eip1193_provider_type.ts +32 -0
  81. package/src/provider_types/ethers_provider_type.ts +44 -0
  82. package/src/provider_types/legacy_provider_type.ts +123 -0
  83. package/src/provider_types/web3_js_provider_type.ts +35 -0
  84. package/src/types.ts +163 -0
  85. package/src/utils/assertion.ts +11 -0
  86. package/src/utils/consensus.ts +122 -0
  87. package/src/utils/conversion.ts +107 -0
  88. package/src/utils/errors.ts +4 -0
  89. package/src/utils/evm.ts +284 -0
  90. package/src/utils/execution.ts +76 -0
  91. package/src/utils/file.ts +51 -0
  92. package/src/utils/gitData/gitDataPath.ts +48 -0
  93. package/src/utils/gitData/index.ts +70 -0
  94. package/src/utils/gitData/writeGitData.ts +10 -0
  95. package/src/utils/json_rpc.ts +170 -0
  96. package/src/utils/process.ts +111 -0
  97. package/src/utils/req_resp.ts +34 -0
  98. package/src/utils/rpc_provider.ts +117 -0
  99. package/src/utils/validation.ts +161 -0
  100. package/src/utils/verification.ts +112 -0
  101. package/src/utils/version.ts +74 -0
  102. package/src/verified_requests/eth_call.ts +50 -0
  103. package/src/verified_requests/eth_estimateGas.ts +49 -0
  104. package/src/verified_requests/eth_getBalance.ts +26 -0
  105. package/src/verified_requests/eth_getBlockByHash.ts +24 -0
  106. package/src/verified_requests/eth_getBlockByNumber.ts +25 -0
  107. package/src/verified_requests/eth_getCode.ts +50 -0
  108. package/src/verified_requests/eth_getTransactionCount.ts +26 -0
  109. package/src/web3_provider.ts +58 -0
  110. package/src/web3_provider_inspector.ts +88 -0
  111. package/src/web3_proxy.ts +175 -0
@@ -0,0 +1,175 @@
1
+ import http from "node:http";
2
+ import https from "node:https";
3
+ import url from "node:url";
4
+ import httpProxy from "http-proxy";
5
+ import {LogLevel} from "@lodestar/logger";
6
+ import {getNodeLogger} from "@lodestar/logger/node";
7
+ import {ELRequestHandler, VerifiedExecutionInitOptions} from "./interfaces.js";
8
+ import {ProofProvider} from "./proof_provider/proof_provider.js";
9
+ import {JsonRpcRequestOrBatch, JsonRpcRequestPayload, JsonRpcResponseOrBatch} from "./types.js";
10
+ import {getResponseForRequest, isBatchRequest} from "./utils/json_rpc.js";
11
+ import {processAndVerifyRequest} from "./utils/process.js";
12
+ import {fetchRequestPayload, fetchResponseBody} from "./utils/req_resp.js";
13
+ import {ELRpcProvider} from "./utils/rpc_provider.js";
14
+
15
+ export type VerifiedProxyOptions = Exclude<VerifiedExecutionInitOptions<false>, "mutateProvider" | "providerTypes"> & {
16
+ executionRpcUrl: string;
17
+ requestTimeout: number;
18
+ };
19
+
20
+ function createHttpHandler({
21
+ info,
22
+ signal,
23
+ }: {
24
+ signal: AbortSignal;
25
+ info: () => {port: number; host: string; timeout: number} | string;
26
+ }): ELRequestHandler {
27
+ return function handler(payload: JsonRpcRequestOrBatch): Promise<JsonRpcResponseOrBatch | undefined> {
28
+ return new Promise((resolve, reject) => {
29
+ const serverInfo = info();
30
+ if (typeof serverInfo === "string") {
31
+ return reject(new Error(serverInfo));
32
+ }
33
+
34
+ const req = http.request(
35
+ {
36
+ method: "POST",
37
+ path: "/proxy",
38
+ port: serverInfo.port,
39
+ host: serverInfo.host,
40
+ timeout: serverInfo.timeout,
41
+ signal,
42
+ headers: {
43
+ "Content-Type": "application/json",
44
+ },
45
+ },
46
+ (res) => {
47
+ fetchResponseBody(res)
48
+ .then((response) => {
49
+ resolve(response);
50
+ })
51
+ .catch(reject);
52
+ }
53
+ );
54
+ req.on("timeout", () => {
55
+ req.destroy();
56
+ reject(new Error("Request timeout"));
57
+ });
58
+ req.write(JSON.stringify(payload));
59
+ req.end();
60
+ });
61
+ };
62
+ }
63
+
64
+ export function createVerifiedExecutionProxy(opts: VerifiedProxyOptions): {
65
+ server: http.Server;
66
+ proofProvider: ProofProvider;
67
+ } {
68
+ const {executionRpcUrl, requestTimeout} = opts;
69
+ const signal = opts.signal ?? new AbortController().signal;
70
+ const logger = opts.logger ?? getNodeLogger({level: opts.logLevel ?? LogLevel.info, module: "prover"});
71
+
72
+ const proofProvider = ProofProvider.init({
73
+ ...opts,
74
+ signal,
75
+ logger,
76
+ });
77
+
78
+ logger.info("Creating http proxy", {url: executionRpcUrl});
79
+ const proxy = httpProxy.createProxy({
80
+ target: executionRpcUrl,
81
+ ws: executionRpcUrl.startsWith("ws"),
82
+ agent: executionRpcUrl.startsWith("https") ? https.globalAgent : http.globalAgent,
83
+ xfwd: true,
84
+ ignorePath: true,
85
+ changeOrigin: true,
86
+ });
87
+
88
+ let proxyServerListeningAddress: {host: string; port: number} | undefined;
89
+ const rpc = new ELRpcProvider(
90
+ createHttpHandler({
91
+ signal,
92
+ info: () => {
93
+ if (!proxyServerListeningAddress) {
94
+ return "Proxy server not listening";
95
+ }
96
+
97
+ return {
98
+ port: proxyServerListeningAddress.port,
99
+ host: proxyServerListeningAddress.host,
100
+ timeout: requestTimeout,
101
+ };
102
+ },
103
+ }),
104
+ logger
105
+ );
106
+
107
+ logger.info("Creating http server");
108
+ const proxyServer = http.createServer(function proxyRequestHandler(req, res) {
109
+ if (req.url === "/proxy") {
110
+ logger.debug("Forwarding request to execution layer");
111
+ proxy.web(req, res);
112
+ return;
113
+ }
114
+
115
+ let payload: JsonRpcRequestPayload;
116
+ fetchRequestPayload(req)
117
+ .then((data) => {
118
+ payload = data;
119
+ return processAndVerifyRequest({payload, proofProvider, rpc, logger});
120
+ })
121
+ .then((response) => {
122
+ res.write(JSON.stringify(response));
123
+ res.end();
124
+ })
125
+ .catch((err) => {
126
+ logger.error("Error processing request", err);
127
+ const message = (err as Error).message;
128
+ if (isBatchRequest(payload)) {
129
+ res.write(JSON.stringify(payload.map((req) => getResponseForRequest(req, {message}))));
130
+ } else {
131
+ res.write(JSON.stringify(getResponseForRequest(payload, undefined, {message})));
132
+ }
133
+
134
+ res.end();
135
+ });
136
+ });
137
+
138
+ proxyServer.on("listening", () => {
139
+ const address = proxyServer.address();
140
+
141
+ if (address === null) {
142
+ throw new Error("Invalid proxy server address");
143
+ }
144
+
145
+ if (typeof address === "string") {
146
+ const rawUrl = url.parse(address);
147
+ if (!rawUrl.host || !rawUrl.port || !rawUrl.protocol) {
148
+ throw new Error(`Invalid proxy server address: ${address}`);
149
+ }
150
+ proxyServerListeningAddress = {host: rawUrl.host, port: parseInt(rawUrl.port)};
151
+ } else {
152
+ proxyServerListeningAddress = {host: address.address, port: address.port};
153
+ }
154
+
155
+ logger.info(
156
+ `Lodestar Prover Proxy listening on ${proxyServerListeningAddress.host}:${proxyServerListeningAddress.port}`
157
+ );
158
+
159
+ rpc.verifyCompatibility().catch((err) => {
160
+ logger.error(err);
161
+ process.exit(1);
162
+ });
163
+ });
164
+
165
+ proxyServer.on("upgrade", function proxyRequestUpgrade(req, socket, head) {
166
+ logger.debug("Upgrading the ws connection");
167
+ proxy.ws(req, socket, head);
168
+ });
169
+
170
+ signal.addEventListener("abort", () => {
171
+ proxyServer.close();
172
+ });
173
+
174
+ return {server: proxyServer, proofProvider};
175
+ }