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

Commit

Permalink
Merge pull request #457 from trufflesuite/fix-estimation-error
Browse files Browse the repository at this point in the history
Return errors when a tx fails in estimation
  • Loading branch information
davidmurdoch authored Aug 22, 2019
2 parents 640d59c + 4643722 commit 2e63045
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[![npm](https://img.shields.io/npm/v/ganache-core.svg)]()
[![npm](https://img.shields.io/npm/dm/ganache-core.svg)]()
[![npm Version](https://img.shields.io/npm/v/ganache-core.svg)](https://www.npmjs.com/package/ganache-core)
[![npm Downloads](https://img.shields.io/npm/dm/ganache-core.svg)](https://www.npmjs.com/package/ganache-core)
[![Build Status](https://travis-ci.org/trufflesuite/ganache-core.svg?branch=master)](https://travis-ci.org/trufflesuite/ganache-core)
[![Coverage Status](https://coveralls.io/repos/github/trufflesuite/ganache-core/badge.svg?branch=develop)](https://coveralls.io/github/trufflesuite/ganache-core?branch=develop)
# Ganache Core
Expand Down
2 changes: 1 addition & 1 deletion lib/blockchain_double.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ BlockchainDouble.prototype.estimateGas = function(tx, blockNumber, callback) {
return;
}

estimateGas(vm, runArgs, err, callback);
estimateGas(vm, runArgs, callback);
});
};

Expand Down
5 changes: 4 additions & 1 deletion lib/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ Provider.prototype.cleanUpErrorObject = function(err, response) {
errorObject.error.message = err.message;
errorObject.error.data.stack = err.stack;
errorObject.error.data.name = err.name;
if ("code" in err) {
errorObject.error.code = err.code;
}
} else if (!response.error) {
errorObject.error = {
message: err.toString()
Expand Down Expand Up @@ -242,7 +245,7 @@ Provider.prototype.reportErrorInResponse = function(request, err, response) {
}
}

if (request.method === "eth_call") {
if (request.method === "eth_call" || request.method === "eth_estimateGas") {
if (err instanceof RuntimeError) {
if (self.options.vmErrorsOnRPCResponse) {
if (!response.error.data) {
Expand Down
10 changes: 8 additions & 2 deletions lib/utils/gasEstimation.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
const STIPEND = 2300;
module.exports = (vm, runArgs, err, callback) => {
module.exports = (vm, runArgs, callback) => {
const steps = stepTracker();

vm.on("step", steps.collect);

vm.runTx(runArgs, function(vmerr, result) {
if (vmerr) {
return callback(vmerr, err);
return callback(vmerr);
} else if (result.vm.exception === 0) {
const error = result.vm.exceptionError
? new Error(`execution error: ${result.vm.exceptionError.error}`)
: new Error("execution error");
error.code = -32000;
return callback(error);
} else if (steps.done()) {
let estimate = result.gasUsed;
result.gasEstimate = estimate;
Expand Down
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"scripts": {
"_mocha": "mocha --check-leaks --recursive --globals _scratch --opts ./test/.mocharc",
"nyc_mocha": "nyc mocha --check-leaks --recursive --globals _scratch --opts ./test/.mocharc",
"nyc_mocha": "nyc npm run _mocha",
"_lint": "eslint --ignore-path .gitignore .",
"build": "webpack-cli --config ./webpack/node/core.webpack.config.js",
"build-web": "webpack-cli --config ./webpack/web-experimental/core.webpack.config.js",
Expand Down
16 changes: 16 additions & 0 deletions test/runTimeErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ function tests(ganacheProviderOptions) {
});
});

it("Should fail to estimate gas when the transaction is invalid", async() => {
const { accounts, instance, send } = context;
const txParams = {
from: accounts[0],
// this errors:
to: instance.options.address
};
const result = await send("eth_estimateGas", txParams);
assert.deepStrictEqual(result.response.error.code, -32000, "Gas estimation error code is not as expected");
assert.deepStrictEqual(
result.response.error.message,
"execution error: revert",
"Gas estimation error message is not as expected"
);
});

it("should output the transaction hash even if a (out of gas) runtime error occurred", async function() {
const { accounts, bytecode, provider, send } = context;

Expand Down

0 comments on commit 2e63045

Please sign in to comment.