Commit cd9dd507 authored by Alex Beregszaszi's avatar Alex Beregszaszi Committed by GitHub

Merge pull request #361 from ethereum/checkgasblocklimit

Check the current block gas limit before sending the tx
parents 293b12fa 2a703772
......@@ -65,19 +65,24 @@ TxRunner.prototype.execute = function () {
})
})
} else {
self.web3.eth.estimateGas(tx, function (err, resp) {
self.web3.eth.estimateGas(tx, function (err, gasEstimation) {
if (err) {
return callback(err, resp)
return callback(err, gasEstimation)
}
self.web3.eth.getBlock('latest', function (err, block) {
if (err) {
return callback(err)
} else {
// NOTE: estimateGas very likely will return a large limit if execution of the code failed
// we want to be able to run the code in order to debug and find the cause for the failure
var blockGasLimit = Math.floor(block.gasLimit - block.gasLimit / 1024)
tx.gas = blockGasLimit < gasEstimation ? blockGasLimit : gasEstimation
if (resp > gasLimit) {
return callback('Gas required exceeds limit: ' + resp)
if (tx.gas > gasLimit) {
return callback('Gas required exceeds limit: ' + tx.gas)
}
tx.gas = resp
var sendTransaction = self.personalMode ? self.web3.personal.sendTransaction : self.web3.eth.sendTransaction
sendTransaction(tx, function (err, resp) {
if (err) {
return callback(err, resp)
......@@ -85,6 +90,8 @@ TxRunner.prototype.execute = function () {
tryTillResponse(self.web3, resp, callback)
})
}
})
})
}
} else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment