Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

fix: handle eth_gasEstimate reverts correctly to prevent potential process crash #4056

Merged
merged 2 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/chains/ethereum/ethereum/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
davidmurdoch marked this conversation as resolved.
Show resolved Hide resolved
resolve(Quantity.from(result.gasEstimate));
}
);
Expand Down
45 changes: 45 additions & 0 deletions src/chains/ethereum/ethereum/tests/api/eth/estimateGas.test.ts
Original file line number Diff line number Diff line change
@@ -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(
davidmurdoch marked this conversation as resolved.
Show resolved Hide resolved
didRaiseUnhandledRejection === false,
"Shouldn't have raised an unhandledRejection"
);
} finally {
process.off("unhandledRejection", unhandledRejectionHandler);
}
});
});
});
});