From d3366197b3b65ea13b1bff54f196eb70821285f1 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Mon, 9 Jul 2018 16:50:38 -0400 Subject: [PATCH 1/2] add interval in case deployment gets stuck --- lib/contracts/blockchain.js | 50 ++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/lib/contracts/blockchain.js b/lib/contracts/blockchain.js index 38cfe9276..838a5d7ee 100644 --- a/lib/contracts/blockchain.js +++ b/lib/contracts/blockchain.js @@ -240,13 +240,55 @@ class Blockchain { } deployContractFromObject(deployContractObject, params, cb) { + const self = this; + let hash; + let calledBacked = false; + + function callback() { + if (calledBacked) { + return; + } + if (interval) { + clearInterval(interval); + } + calledBacked = true; + cb(...arguments); + } + + // This interval is there to compensate for the event that sometimes doesn't get triggered when using WebSocket + // FIXME The issue somehow only happens when the blockchain node is started in the same terminal + const interval = setInterval(() => { + if (!hash) { + return; // Wait until we receive the hash + } + self.web3.eth.getTransactionReceipt(hash, (err, receipt) => { + if (!err && !receipt) { + return; // Transaction is not yet complete + } + callback(err, receipt); + }); + }, 500); + deployContractObject.send({ from: params.from, gas: params.gas, gasPrice: params.gasPrice - }).on('receipt', function(receipt) { - if (receipt.contractAddress !== undefined) { - cb(null, receipt); + }, function (err, transactionHash) { + if (err) { + return callback(err); } - }).on('error', cb); + hash = transactionHash; + }) + .on('receipt', function (receipt) { + if (receipt.contractAddress !== undefined) { + callback(null, receipt); + } + }) + .then(function (_contract) { + if (!hash) { + return; // Somehow we didn't get the receipt yet... Interval will catch it + } + self.web3.eth.getTransactionReceipt(hash, callback); + }) + .catch(callback); } assertNodeConnection(noLogs, cb) { From cc3839382cda9084e2000a09f48f213e47e59f69 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 11 Jul 2018 08:47:12 -0400 Subject: [PATCH 2/2] check for receipt address before calling back --- lib/contracts/blockchain.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/contracts/blockchain.js b/lib/contracts/blockchain.js index 838a5d7ee..ed7972e00 100644 --- a/lib/contracts/blockchain.js +++ b/lib/contracts/blockchain.js @@ -244,15 +244,18 @@ class Blockchain { let hash; let calledBacked = false; - function callback() { + function callback(err, receipt) { if (calledBacked) { return; } + if (!err && !receipt.contractAddress) { + return; // Not deployed yet. Need to wait + } if (interval) { clearInterval(interval); } calledBacked = true; - cb(...arguments); + cb(err, receipt); } // This interval is there to compensate for the event that sometimes doesn't get triggered when using WebSocket