diff --git a/lib/services/bitcoind.js b/lib/services/bitcoind.js index 695482b9..216c72ae 100644 --- a/lib/services/bitcoind.js +++ b/lib/services/bitcoind.js @@ -996,14 +996,16 @@ Bitcoin.prototype.getRawBlock = function(blockArg, callback) { var self = this; function queryBlock(blockhash) { - self.client.getBlock(blockhash, false, function(err, response) { - if (err) { - return callback(self._wrapRPCError(err)); - } - var buffer = new Buffer(response.result, 'hex'); - self.rawBlockCache.set(blockhash, buffer); - callback(null, buffer); - }); + self._tryAll(function(done) { + self.client.getBlock(blockhash, false, function(err, response) { + if (err) { + return done(self._wrapRPCError(err)); + } + var buffer = new Buffer(response.result, 'hex'); + self.rawBlockCache.set(blockhash, buffer); + done(null, buffer); + }); + }, callback); } var cachedBlock = self.rawBlockCache.get(blockArg); @@ -1013,11 +1015,17 @@ Bitcoin.prototype.getRawBlock = function(blockArg, callback) { }); } else { if (_.isNumber(blockArg)) { - self.client.getBlockHash(blockArg, function(err, response) { + self._tryAll(function(done) { + self.client.getBlockHash(blockArg, function(err, response) { + if (err) { + return callback(self._wrapRPCError(err)); + } + done(null, response.result); + }); + }, function(err, blockhash) { if (err) { - return callback(self._wrapRPCError(err)); + return callback(err); } - var blockhash = response.result; queryBlock(blockhash); }); } else { @@ -1107,25 +1115,29 @@ Bitcoin.prototype.getBlockHeader = function(block, callback) { var self = this; function queryHeader(blockhash) { - self.client.getBlockHeader(blockhash, function(err, response) { - if (err && response.error.code === -5) { - return callback(null, null); - } else if (err) { - return callback(self._wrapRPCError(err)); - } - // TODO format response prevHash instead of previousblockhash, etc. - callback(null, response.result); - }); + self._tryAll(function(done) { + self.client.getBlockHeader(blockhash, function(err, response) { + if (err) { + return done(self._wrapRPCError(err)); + } + // TODO format response prevHash instead of previousblockhash, etc. + done(null, response.result); + }); + }, callback); } if (_.isNumber(block)) { - self.client.getBlockHash(block, function(err, response) { - if (err && response.error.code === -8) { - return callback(null, null); - } else if (err) { - return callback(self._wrapRPCError(err)); + self._tryAll(function(done) { + self.client.getBlockHash(block, function(err, response) { + if (err) { + return callback(self._wrapRPCError(err)); + } + done(null, response.result); + }); + }, function(err, blockhash) { + if (err) { + return callback(err); } - var blockhash = response.result; queryHeader(blockhash); }); } else { @@ -1192,16 +1204,16 @@ Bitcoin.prototype.getRawTransaction = function(txid, callback) { callback(null, tx); }); } else { - self.client.getRawTransaction(txid, function(err, response) { - if (err && response.error.code === -5) { - return callback(null, null); - } else if (err) { - return callback(self._wrapRPCError(err)); - } - var buffer = new Buffer(response.result, 'hex'); - self.rawTransactionCache.set(txid, buffer); - callback(null, buffer); - }); + self._tryAll(function(done) { + self.client.getRawTransaction(txid, function(err, response) { + if (err) { + return done(self._wrapRPCError(err)); + } + var buffer = new Buffer(response.result, 'hex'); + self.rawTransactionCache.set(txid, buffer); + done(null, buffer); + }); + }, callback); } }; @@ -1219,17 +1231,17 @@ Bitcoin.prototype.getTransaction = function(txid, callback) { callback(null, tx); }); } else { - self.client.getRawTransaction(txid, function(err, response) { - if (err && response.error.code === -5) { - return callback(null, null); - } else if (err) { - return callback(self._wrapRPCError(err)); - } - var tx = Transaction(); - tx.fromString(response.result); - self.transactionCache.set(txid, tx); - callback(null, tx); - }); + self._tryAll(function(done) { + self.client.getRawTransaction(txid, function(err, response) { + if (err) { + return done(self._wrapRPCError(err)); + } + var tx = Transaction(); + tx.fromString(response.result); + self.transactionCache.set(txid, tx); + done(null, tx); + }); + }, callback); } }; @@ -1251,23 +1263,23 @@ Bitcoin.prototype.getTransactionWithBlockInfo = function(txid, callback) { callback(null, tx); }); } else { - self.client.getRawTransaction(txid, 1, function(err, response) { - if (err && response.error.code === -5) { - return callback(null, null); - } else if (err) { - return callback(self._wrapRPCError(err)); - } - var tx = Transaction(); - tx.fromString(response.result.hex); - tx.__blockHash = response.result.blockhash; - tx.__height = response.result.height ? response.result.height : -1; - tx.__timestamp = response.result.time; - var confirmations = self._getConfirmationsDetail(tx); - if (confirmations >= self.transactionInfoCacheConfirmations) { - self.transactionInfoCache.set(txid, tx); - } - callback(null, tx); - }); + self._tryAll(function(done) { + self.client.getRawTransaction(txid, 1, function(err, response) { + if (err) { + return done(self._wrapRPCError(err)); + } + var tx = Transaction(); + tx.fromString(response.result.hex); + tx.__blockHash = response.result.blockhash; + tx.__height = response.result.height ? response.result.height : -1; + tx.__timestamp = response.result.time; + var confirmations = self._getConfirmationsDetail(tx); + if (confirmations >= self.transactionInfoCacheConfirmations) { + self.transactionInfoCache.set(txid, tx); + } + done(null, tx); + }); + }, callback); } }; diff --git a/regtest/bitcoind.js b/regtest/bitcoind.js index 8aa4d71e..485b33dc 100644 --- a/regtest/bitcoind.js +++ b/regtest/bitcoind.js @@ -222,13 +222,10 @@ describe('Bitcoind Functionality', function() { }); }); - it('will return null if the transaction does not exist', function(done) { + it('will return error if the transaction does not exist', function(done) { var txid = '6226c407d0e9705bdd7158e60983e37d0f5d23529086d6672b07d9238d5aa618'; bitcoind.getTransaction(txid, function(err, response) { - if (err) { - throw err; - } - should.not.exist(response); + should.exist(err); done(); }); }); @@ -250,13 +247,10 @@ describe('Bitcoind Functionality', function() { }); }); - it('will return null if the transaction does not exist', function(done) { + it('will return error if the transaction does not exist', function(done) { var txid = '6226c407d0e9705bdd7158e60983e37d0f5d23529086d6672b07d9238d5aa618'; bitcoind.getRawTransaction(txid, function(err, response) { - if (err) { - throw err; - } - should.not.exist(response); + should.exist(err); done(); }); }); @@ -293,12 +287,9 @@ describe('Bitcoind Functionality', function() { done(); }); }); - it('will get null for block not found', function(done) { + it('will get error for block not found', function(done) { bitcoind.getBlockHeader('notahash', function(err, header) { - if(err) { - return done(err); - } - should.equal(header, null); + should.exist(err); done(); }); }); @@ -321,12 +312,9 @@ describe('Bitcoind Functionality', function() { }); }); }); - it('will get null with number greater than tip', function(done) { + it('will get error with number greater than tip', function(done) { bitcoind.getBlockHeader(100000, function(err, header) { - if (err) { - return done(err); - } - should.equal(header, null); + should.exist(err); done(); }); });