Browse Source

check blockchain if broadcast fails

activeAddress
Ivan Socolsky 10 years ago
parent
commit
83b65193af
  1. 47
      lib/server.js

47
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. * Sign a transaction proposal.
* @param {Object} opts * @param {Object} opts
@ -993,6 +1005,20 @@ WalletService.prototype.broadcastTx = function(opts, cb) {
if (!Utils.checkRequired(opts, ['txProposalId'])) if (!Utils.checkRequired(opts, ['txProposalId']))
return cb(new ClientError('Required argument missing')); 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) { self.getWallet({}, function(err, wallet) {
if (err) return cb(err); 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')); return cb(new ClientError('TXNOTACCEPTED', 'The transaction proposal is not accepted'));
self._broadcastTx(txp, function(err, txid) { self._broadcastTx(txp, function(err, txid) {
if (err) return cb(err); if (err) {
var broadcastErr = err;
txp.setBroadcasted(txid); // Check if tx already in blockchain
self.storage.storeTx(self.walletId, txp, function(err) { self._checkTxInBlockchain(txp, function(err, tx) {
if (err) return cb(err); if (err) return cb(err);
if (!tx) return cb(broadcastErr);
self._notify('NewOutgoingTx', { setBroadcasted(txp, tx.txid, cb);
txProposalId: opts.txProposalId,
txid: txid
}, function() {
return cb(null, txp);
}); });
}); } else {
setBroadcasted(txp, txid, cb);
}
}); });
}); });
}); });

Loading…
Cancel
Save