From 65089302bbec7d68b4fe21f272813a686ff0a218 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Fri, 31 Mar 2017 16:05:07 -0400 Subject: [PATCH] Fixed wallet getTransactions. --- lib/services/address/index.js | 14 +++----- lib/services/wallet-api/index.js | 61 ++++++++++++++++++++------------ 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/lib/services/address/index.js b/lib/services/address/index.js index 593d07e4..f78f3c16 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -317,7 +317,7 @@ AddressService.prototype.blockHandler = function(block, connectBlock, callback) }; AddressService.prototype.getAddressString = function(script, output) { - var address = script.toAddress(); + var address = script.toAddress(this.node.network.name); if(address) { return address.toString(); } @@ -1149,24 +1149,18 @@ AddressService.prototype.getAddressTxids = function(address, options, callback) var streamErr = null; stream.on('close', function() { -console.log('close'); }); - stream.on('data', function(buffer) { - -console.log(buffer); var key = self._encoding.decodeAddressIndexKey(buffer); txids[key.txid] = true; }); stream.on('end', function() { -console.log(txids); callback(streamErr, Object.keys(txids)); }); stream.on('error', function(err) { -console.log(err); streamErr = err; }); }; @@ -1174,11 +1168,11 @@ console.log(err); AddressService.prototype.getAddressTxidsWithHeights = function(address, options, callback) { var self = this; - var opts = options || { start: 0, end: 0xffffffff, txid: new Array(65).join('0') }; + var opts = options || {}; var txids = {}; - var start = self._encoding.encodeAddressIndexKey(address, opts.start, opts.txid); - var end = self._encoding.encodeAddressIndexKey(address, opts.end, opts.txid); + var start = self._encoding.encodeAddressIndexKey(address, opts.start || 0, '00'); + var end = self._encoding.encodeAddressIndexKey(address, opts.end || 0xffffffff); var stream = self.store.createKeyStream({ gte: start, diff --git a/lib/services/wallet-api/index.js b/lib/services/wallet-api/index.js index b616cf13..c13e7582 100644 --- a/lib/services/wallet-api/index.js +++ b/lib/services/wallet-api/index.js @@ -15,6 +15,7 @@ var _ = require('lodash'); var bodyParser = require('body-parser'); var LRU = require('lru-cache'); var Encoding = require('./encoding'); +var Readable = require('stream').Readable; var WalletService = function(options) { BaseService.call(this, options); @@ -593,19 +594,35 @@ WalletService.prototype._endpointGetTransactions = function() { from: req.query.from, to: req.query.to }; - self._getTransactions(walletId, options, function(err, transactions, totalCount) { + self._getTransactions(walletId, options, function(err, transactions) { if(err) { return utils.sendError(err, res); } - res.status(200).jsonp({ - transactions: transactions, - totalCount: totalCount + var rs = new Readable; + transactions.forEach(function(transaction) { + rs.push(JSON.stringify(self._formatTransaction(transaction))); }); + rs.push(null); + rs.pipe(res); }); }); }; }; +WalletService.prototype._formatTransactions = function(txs) { + return txs.forEach(this._formatTransaction); +}; + +WalletService.prototype._formatTransaction = function(tx) { + var obj = tx.toObject(); + for(var i = 0; i < tx.inputs.length; i++) { + obj.inputs[i].inputSatoshis = tx.__inputValues[i]; + } + obj.height = tx.__height; + obj.timestamp = tx.__timestamp; + return obj; +}; + WalletService.prototype._endpointPutAddresses = function() { var self = this; return function(req, res) { @@ -730,6 +747,18 @@ WalletService.prototype._getTransactions = function(walletId, options, callback) }); } + function mapTxids(txids) { + async.mapLimit(txids, 10, function(txid, next) { + self.node.services.transaction.getTransaction(txid, options, next); + }, function(err, transactions) { + if(err) { + return callback(err); + } + self._cache.set(key, JSON.stringify(self._formatTransactions(transactions))); + finish(transactions); + }); + } + if (!self._cache.peek(key)) { var stream = self.store.createReadStream({ gte: self._encoding.encodeWalletTransactionKey(walletId, opts.start), @@ -737,10 +766,6 @@ WalletService.prototype._getTransactions = function(walletId, options, callback) }); var streamErr; - stream.on('close', function() { - finish(txids); - }); - stream.on('error', function(err) { streamErr = err; }); @@ -750,15 +775,7 @@ WalletService.prototype._getTransactions = function(walletId, options, callback) }); stream.on('end', function() { - async.mapLimit(txids, function(txid, next) { - self.node.services.transaction.getTransaction(txid, options, next); - }, function(err, transactions) { - if(err) { - return callback(err); - } - self._cache.set(key, JSON.stringify(transactions)); - finish(transactions); - }); + mapTxids(txids); }); } else { try { @@ -1041,13 +1058,13 @@ WalletService.prototype._storeBalance = function(walletId, balance, callback) { WalletService.prototype._processStartEndOptions = function(req, callback) { var self = this; - var heights = []; if (!(req.query.start && req.query.start < (500 * 1E6))) { - var times = []; var heights = []; - times.push(utils.normalizeTimeStamp(req.query.start)); - times.push(utils.normalizeTimeStamp(req.query.end)); - self.node.services.timestamp.getBlockHeights(times, function(err, hashTuple) { + self.node.services.timestamp.getBlockHeights([ + utils.normalizeTimeStamp(req.query.start), + utils.normalizeTimeStamp(req.query.end) + ], + function(err, hashTuple) { hashTuple.forEach(function(hash) { self.node.services.bitcoind._tryAllClients(function(client, done) { client.getBlock(hash, function(err, response) {