From e71c1848cee34d4eb17f1c168f452c142abaa80e Mon Sep 17 00:00:00 2001 From: David Murdoch <187813+davidmurdoch@users.noreply.github.com> Date: Mon, 19 Dec 2022 12:40:28 -0500 Subject: [PATCH] fix: handle `eth_gasEstimate` reverts correctly to prevent potential process crash (#4056) Co-authored-by: Jeff Smale <6363749+jeffsmale90@users.noreply.github.com> --- src/chains/ethereum/ethereum/src/api.ts | 2 +- .../tests/api/eth/estimateGas.test.ts | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/chains/ethereum/ethereum/tests/api/eth/estimateGas.test.ts diff --git a/src/chains/ethereum/ethereum/src/api.ts b/src/chains/ethereum/ethereum/src/api.ts index e47a39f6de..879269f93a 100644 --- a/src/chains/ethereum/ethereum/src/api.ts +++ b/src/chains/ethereum/ethereum/src/api.ts @@ -960,7 +960,7 @@ export default class EthereumApi implements Api { generateVM, runArgs, (err: Error, result: EstimateGasResult) => { - if (err) reject(err); + if (err) return void reject(err); resolve(Quantity.from(result.gasEstimate)); } ); diff --git a/src/chains/ethereum/ethereum/tests/api/eth/estimateGas.test.ts b/src/chains/ethereum/ethereum/tests/api/eth/estimateGas.test.ts new file mode 100644 index 0000000000..9c7f3f3d2a --- /dev/null +++ b/src/chains/ethereum/ethereum/tests/api/eth/estimateGas.test.ts @@ -0,0 +1,45 @@ +import assert from "assert"; +import getProvider from "../../helpers/getProvider"; + +describe("api", () => { + describe("eth", () => { + describe("estimateGas", () => { + it("shouldn't raise an unhandled rejection when the transaction fails", async () => { + // see https://github.com/trufflesuite/ganache/pull/4056 + const provider = await getProvider(); + const [from] = await provider.request({ + method: "eth_accounts", + params: [] + }); + + const transaction = { + from, + // invalid bytecode + input: "0x1234" + }; + + let didRaiseUnhandledRejection = false; + const unhandledRejectionHandler = () => + (didRaiseUnhandledRejection = true); + process.once("unhandledRejection", unhandledRejectionHandler); + + try { + const estimatingGas = provider.request({ + method: "eth_estimateGas", + params: [transaction] + }); + + await assert.rejects(estimatingGas); + await new Promise(resolve => setImmediate(resolve)); + + assert( + didRaiseUnhandledRejection === false, + "Shouldn't have raised an unhandledRejection" + ); + } finally { + process.off("unhandledRejection", unhandledRejectionHandler); + } + }); + }); + }); +});