From 230f3681bd5e4d2e0c878de8a6b8ac789c85d038 Mon Sep 17 00:00:00 2001 From: k Date: Sat, 1 Apr 2017 13:27:57 -0400 Subject: [PATCH] Cleaned code a bit. --- lib/services/timestamp/index.js | 6 +++--- lib/services/wallet-api/index.js | 25 ++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/services/timestamp/index.js b/lib/services/timestamp/index.js index c670a97c..ed25284f 100644 --- a/lib/services/timestamp/index.js +++ b/lib/services/timestamp/index.js @@ -115,6 +115,7 @@ TimestampService.prototype.getBlockHeights = function(timestamps, callback) { }); var hashes = []; + var hashTuple = []; var streamErr = null; stream.on('data', function(data) { @@ -127,10 +128,9 @@ TimestampService.prototype.getBlockHeights = function(timestamps, callback) { stream.on('end', function() { if (!streamErr && hashes.length > 1) { - var hashTuple = [ hashes[0], hashes[hashes.length - 1] ]; - return callback(null, hashTuple); + hashTuple = [ hashes[0], hashes[hashes.length - 1] ]; } - callback(streamErr); + callback(streamErr, hashTuple); }); }; diff --git a/lib/services/wallet-api/index.js b/lib/services/wallet-api/index.js index c13e7582..e017c02a 100644 --- a/lib/services/wallet-api/index.js +++ b/lib/services/wallet-api/index.js @@ -585,25 +585,36 @@ WalletService.prototype._endpointPostAddresses = function() { WalletService.prototype._endpointGetTransactions = function() { var self = this; return function(req, res) { - req.setTimeout(600000); + var walletId = req.params.walletId; + self._processStartEndOptions(req, function(err, heights) { + + if(err) { + return utils.sendError(err, res); + } + var options = { - start: heights[0], - end : heights[1], + start: heights[0] || 0, + end : heights[1] || 0xffffffff, from: req.query.from, to: req.query.to }; self._getTransactions(walletId, options, function(err, transactions) { + if(err) { return utils.sendError(err, res); } + var rs = new Readable; + transactions.forEach(function(transaction) { rs.push(JSON.stringify(self._formatTransaction(transaction))); }); + rs.push(null); rs.pipe(res); + }); }); }; @@ -1058,13 +1069,21 @@ WalletService.prototype._storeBalance = function(walletId, balance, callback) { WalletService.prototype._processStartEndOptions = function(req, callback) { var self = this; + if (!(req.query.start && req.query.start < (500 * 1E6))) { + var heights = []; self.node.services.timestamp.getBlockHeights([ utils.normalizeTimeStamp(req.query.start), utils.normalizeTimeStamp(req.query.end) ], + function(err, hashTuple) { + + if(err) { + return callback(err); + } + hashTuple.forEach(function(hash) { self.node.services.bitcoind._tryAllClients(function(client, done) { client.getBlock(hash, function(err, response) {