check blockchain if broadcast fails

This commit is contained in:
Ivan Socolsky 2015-05-28 12:51:41 -03:00
parent 38d6ee83d3
commit 83b65193af
1 changed files with 36 additions and 11 deletions

View File

@ -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);
}
});
});
});