From 105e814bc72fff68b071d08d6048d695ac1bf29d Mon Sep 17 00:00:00 2001 From: Andrew Donley Date: Fri, 2 Mar 2018 04:13:03 -0800 Subject: [PATCH] decoupled pub/sub and polling paradigms in contract deployment timeout --- packages/web3-core-method/src/index.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/web3-core-method/src/index.js b/packages/web3-core-method/src/index.js index 7dc285cd518..8c296d87e2e 100644 --- a/packages/web3-core-method/src/index.js +++ b/packages/web3-core-method/src/index.js @@ -31,6 +31,7 @@ var promiEvent = require('web3-core-promievent'); var Subscriptions = require('web3-core-subscriptions').subscriptions; var TIMEOUTBLOCK = 50; +var POLLINGTIMEOUT = 15 * TIMEOUTBLOCK; // ~average block time (seconds) * TIMEOUTBLOCK var CONFIRMATIONBLOCKS = 24; var Method = function Method(options) { @@ -240,7 +241,7 @@ Method.prototype._confirmTransaction = function (defer, result, payload) { // fire "receipt" and confirmation events and resolve after - var checkConfirmation = function (err, blockHeader, sub, existingReceipt) { + var checkConfirmation = function (err, blockHeader, sub, existingReceipt, isPolling) { if (!err) { // create fake unsubscribe if (!sub) { @@ -369,10 +370,20 @@ Method.prototype._confirmTransaction = function (defer, result, payload) { .catch(function () { timeoutCount++; - if (timeoutCount - 1 >= TIMEOUTBLOCK) { - sub.unsubscribe(); - promiseResolved = true; - return utils._fireError(new Error('Transaction was not mined within 50 blocks, please make sure your transaction was properly sent. Be aware that it might still be mined!'), defer.eventEmitter, defer.reject); + // check to see if we are http polling + if(!!isPolling) { + // polling timeout is different than TIMEOUTBLOCK blocks since we are triggering every second + if (timeoutCount - 1 >= POLLINGTIMEOUT) { + sub.unsubscribe(); + promiseResolved = true; + return utils._fireError(new Error('Transaction was not mined within' + POLLINGTIMEOUT + ' seconds, please make sure your transaction was properly sent. Be aware that it might still be mined!'), defer.eventEmitter, defer.reject); + } + } else { + if (timeoutCount - 1 >= TIMEOUTBLOCK) { + sub.unsubscribe(); + promiseResolved = true; + return utils._fireError(new Error('Transaction was not mined within 50 blocks, please make sure your transaction was properly sent. Be aware that it might still be mined!'), defer.eventEmitter, defer.reject); + } } }); @@ -390,7 +401,10 @@ Method.prototype._confirmTransaction = function (defer, result, payload) { if (_.isFunction(this.requestManager.provider.on)) { _ethereumCall.subscribe('newBlockHeaders', checkConfirmation); } else { - intervalId = setInterval(checkConfirmation, 1000); + // if provider is http and polling + intervalId = setInterval(function() { + checkConfirmation(null, null, null, null, true); + }, 1000); } }.bind(this);