From 83b65193afc0e853732b40560c88a0a8cd0efe5a Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Thu, 28 May 2015 12:51:41 -0300 Subject: [PATCH] check blockchain if broadcast fails --- lib/server.js | 47 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/lib/server.js b/lib/server.js index ebd8833..1951b36 100644 --- a/lib/server.js +++ b/lib/server.js @@ -920,6 +920,18 @@ WalletService.prototype._broadcastTx = function(txp, cb) { }) }; +WalletService.prototype._checkTxInBlockchain = function(txp, cb) { + var tx = txp.getBitcoreTx(); + var bc = this._getBlockchainExplorer('insight', txp.getNetworkName()); + bc.getTransaction(tx.id, function(err, tx) { + if (err) { + log.error('Could not get transaction info', err); + return cb(new ClientError('BLOCKCHAINERROR', 'Could not get transaction info')); + } + return cb(null, tx); + }) +}; + /** * Sign a transaction proposal. * @param {Object} opts @@ -993,6 +1005,20 @@ WalletService.prototype.broadcastTx = function(opts, cb) { if (!Utils.checkRequired(opts, ['txProposalId'])) return cb(new ClientError('Required argument missing')); + function setBroadcasted(txp, txid, cb) { + txp.setBroadcasted(txid); + self.storage.storeTx(self.walletId, txp, function(err) { + if (err) return cb(err); + + self._notify('NewOutgoingTx', { + txProposalId: opts.txProposalId, + txid: txid + }, function() { + return cb(null, txp); + }); + }); + }; + self.getWallet({}, function(err, wallet) { if (err) return cb(err); @@ -1008,19 +1034,18 @@ WalletService.prototype.broadcastTx = function(opts, cb) { return cb(new ClientError('TXNOTACCEPTED', 'The transaction proposal is not accepted')); self._broadcastTx(txp, function(err, txid) { - if (err) return cb(err); + if (err) { + var broadcastErr = err; + // Check if tx already in blockchain + self._checkTxInBlockchain(txp, function(err, tx) { + if (err) return cb(err); + if (!tx) return cb(broadcastErr); - txp.setBroadcasted(txid); - self.storage.storeTx(self.walletId, txp, function(err) { - if (err) return cb(err); - - self._notify('NewOutgoingTx', { - txProposalId: opts.txProposalId, - txid: txid - }, function() { - return cb(null, txp); + setBroadcasted(txp, tx.txid, cb); }); - }); + } else { + setBroadcasted(txp, txid, cb); + } }); }); });