diff --git a/lib/services/address/index.js b/lib/services/address/index.js index d4caff8e..92e9ccab 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -11,7 +11,6 @@ var _ = bitcore.deps._; var Encoding = require('./encoding'); var Transform = require('stream').Transform; var assert = require('assert'); -var Stream = require('stream'); var AddressService = function(options) { @@ -409,37 +408,28 @@ AddressService.prototype._getAddressHistory = function(address, options, callbac async.waterfall([ + // get all the txs from the mempool that involve our address function(next) { if (!options.queryMempool) { return next(); } - self._mempool.getTxidsByAddress(address, next); + self._mempool.getTxsByAddress(address, next); }, - function(mempoolTxids, next) { + // stream the rest of the confirmed txids out of the address index + function(mempoolTxs, next) { - console.log(mempoolTxids); + if (mempoolTxs.length > 0) { + options.results = options.results.concat(mempoolTxs); + } var txStream = self._getTxStream(address, options); txStream.on('end', function() { return callback(null, options.results); }); - if (mempoolTxids) { - - var mempoolTxidStream = new Stream.Readable({ objectMode: true }); - mempoolTxidStream.pipe(txStream); - - mempoolTxids.forEach(function(txid) { - mempoolTxidStream.push(txid); - }); - - mempoolTxidStream.unpipe(); - - } - var txidStream = self._getTxidStream(address, options); txidStream.pipe(txStream); next(); diff --git a/lib/services/mempool/index.js b/lib/services/mempool/index.js index a9b53e47..3188405a 100644 --- a/lib/services/mempool/index.js +++ b/lib/services/mempool/index.js @@ -137,10 +137,9 @@ MempoolService.prototype.getMempoolTransaction = function(txid, callback) { }; -MempoolService.prototype.getTxidsByAddress = function(address, callback) { - // given an address, give me all the tx ids for txs that involve this address - // perceivably, our mempool will remain a managable size, so cycling over the whole - // pool won't be too spendy? +// TODO optimize this using another index? +MempoolService.prototype.getTxsByAddress = function(address, callback) { + var self = this; var results = []; var start = self._encoding.encodeMempoolTransactionKey(new Array(65).join('0')); @@ -153,15 +152,19 @@ MempoolService.prototype.getTxidsByAddress = function(address, callback) { var stream = self._db.createReadStream(criteria); + stream.on('error', function() { + return []; + }); + stream.on('end', function() { return callback(null, results); }); stream.on('data', function(data) { var tx = self._encoding.decodeMempoolTransactionValue(data.value); - var txid = self._involvesAddress(tx, address); - if (txid) { - results.push(txid); + tx = self._involvesAddress(tx, address); + if (tx) { + results.push(tx); } }); @@ -169,30 +172,28 @@ MempoolService.prototype.getTxidsByAddress = function(address, callback) { MempoolService.prototype._involvesAddress = function(tx, address) { - var _address; - for(var i = 0; i < tx.inputs.length; i++) { - var input = tx.inputs[i]; - _address = input.getAddress(); - if (!_address) { - continue; - } - _address.network = this._network; - _address = _address.toString(); - if (address === _address) { - return tx.txid(); + function contains(collection, network) { + var _address; + for(var i = 0; i < collection.length; i++) { + var item = collection[i]; + _address = item.getAddress(); + if (!_address) { + continue; + } + _address.network = network; + _address = _address.toString(); + if (address === _address) { + return true; + } } } - for(i = 0; i < tx.outputs.length; i++) { - var output = tx.outputs[i]; - _address = output.getAddress(); - if (!_address) { - continue; - } - _address.network = this._network; - _address = _address.toString(); - if (address === _address) { - return tx.txid(); + var collections = [ tx.outputs, tx.inputs ]; + + for(var i = 0; i < collections.length; i++) { + var hasAddress = contains(collections[i], this._network); + if (hasAddress) { + return tx; } }