Fixed wallet getTransactions.

This commit is contained in:
Chris Kleeschulte 2017-03-31 16:05:07 -04:00
parent 06ffa05ba2
commit 65089302bb
2 changed files with 43 additions and 32 deletions

View File

@ -317,7 +317,7 @@ AddressService.prototype.blockHandler = function(block, connectBlock, callback)
}; };
AddressService.prototype.getAddressString = function(script, output) { AddressService.prototype.getAddressString = function(script, output) {
var address = script.toAddress(); var address = script.toAddress(this.node.network.name);
if(address) { if(address) {
return address.toString(); return address.toString();
} }
@ -1149,24 +1149,18 @@ AddressService.prototype.getAddressTxids = function(address, options, callback)
var streamErr = null; var streamErr = null;
stream.on('close', function() { stream.on('close', function() {
console.log('close');
}); });
stream.on('data', function(buffer) { stream.on('data', function(buffer) {
console.log(buffer);
var key = self._encoding.decodeAddressIndexKey(buffer); var key = self._encoding.decodeAddressIndexKey(buffer);
txids[key.txid] = true; txids[key.txid] = true;
}); });
stream.on('end', function() { stream.on('end', function() {
console.log(txids);
callback(streamErr, Object.keys(txids)); callback(streamErr, Object.keys(txids));
}); });
stream.on('error', function(err) { stream.on('error', function(err) {
console.log(err);
streamErr = err; streamErr = err;
}); });
}; };
@ -1174,11 +1168,11 @@ console.log(err);
AddressService.prototype.getAddressTxidsWithHeights = function(address, options, callback) { AddressService.prototype.getAddressTxidsWithHeights = function(address, options, callback) {
var self = this; var self = this;
var opts = options || { start: 0, end: 0xffffffff, txid: new Array(65).join('0') }; var opts = options || {};
var txids = {}; var txids = {};
var start = self._encoding.encodeAddressIndexKey(address, opts.start, opts.txid); var start = self._encoding.encodeAddressIndexKey(address, opts.start || 0, '00');
var end = self._encoding.encodeAddressIndexKey(address, opts.end, opts.txid); var end = self._encoding.encodeAddressIndexKey(address, opts.end || 0xffffffff);
var stream = self.store.createKeyStream({ var stream = self.store.createKeyStream({
gte: start, gte: start,

View File

@ -15,6 +15,7 @@ var _ = require('lodash');
var bodyParser = require('body-parser'); var bodyParser = require('body-parser');
var LRU = require('lru-cache'); var LRU = require('lru-cache');
var Encoding = require('./encoding'); var Encoding = require('./encoding');
var Readable = require('stream').Readable;
var WalletService = function(options) { var WalletService = function(options) {
BaseService.call(this, options); BaseService.call(this, options);
@ -593,19 +594,35 @@ WalletService.prototype._endpointGetTransactions = function() {
from: req.query.from, from: req.query.from,
to: req.query.to to: req.query.to
}; };
self._getTransactions(walletId, options, function(err, transactions, totalCount) { self._getTransactions(walletId, options, function(err, transactions) {
if(err) { if(err) {
return utils.sendError(err, res); return utils.sendError(err, res);
} }
res.status(200).jsonp({ var rs = new Readable;
transactions: transactions, transactions.forEach(function(transaction) {
totalCount: totalCount 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() { WalletService.prototype._endpointPutAddresses = function() {
var self = this; var self = this;
return function(req, res) { 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)) { if (!self._cache.peek(key)) {
var stream = self.store.createReadStream({ var stream = self.store.createReadStream({
gte: self._encoding.encodeWalletTransactionKey(walletId, opts.start), gte: self._encoding.encodeWalletTransactionKey(walletId, opts.start),
@ -737,10 +766,6 @@ WalletService.prototype._getTransactions = function(walletId, options, callback)
}); });
var streamErr; var streamErr;
stream.on('close', function() {
finish(txids);
});
stream.on('error', function(err) { stream.on('error', function(err) {
streamErr = err; streamErr = err;
}); });
@ -750,15 +775,7 @@ WalletService.prototype._getTransactions = function(walletId, options, callback)
}); });
stream.on('end', function() { stream.on('end', function() {
async.mapLimit(txids, function(txid, next) { mapTxids(txids);
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);
});
}); });
} else { } else {
try { try {
@ -1041,13 +1058,13 @@ WalletService.prototype._storeBalance = function(walletId, balance, callback) {
WalletService.prototype._processStartEndOptions = function(req, callback) { WalletService.prototype._processStartEndOptions = function(req, callback) {
var self = this; var self = this;
var heights = [];
if (!(req.query.start && req.query.start < (500 * 1E6))) { if (!(req.query.start && req.query.start < (500 * 1E6))) {
var times = [];
var heights = []; var heights = [];
times.push(utils.normalizeTimeStamp(req.query.start)); self.node.services.timestamp.getBlockHeights([
times.push(utils.normalizeTimeStamp(req.query.end)); utils.normalizeTimeStamp(req.query.start),
self.node.services.timestamp.getBlockHeights(times, function(err, hashTuple) { utils.normalizeTimeStamp(req.query.end)
],
function(err, hashTuple) {
hashTuple.forEach(function(hash) { hashTuple.forEach(function(hash) {
self.node.services.bitcoind._tryAllClients(function(client, done) { self.node.services.bitcoind._tryAllClients(function(client, done) {
client.getBlock(hash, function(err, response) { client.getBlock(hash, function(err, response) {