Added search memory pool for txs with certain addresses.

This commit is contained in:
Chris Kleeschulte 2017-10-06 10:02:38 -04:00
parent c7c268f00a
commit 1e04e08411
No known key found for this signature in database
GPG Key ID: 33195D27EF6BDB7F
2 changed files with 36 additions and 45 deletions

View File

@ -11,7 +11,6 @@ var _ = bitcore.deps._;
var Encoding = require('./encoding'); var Encoding = require('./encoding');
var Transform = require('stream').Transform; var Transform = require('stream').Transform;
var assert = require('assert'); var assert = require('assert');
var Stream = require('stream');
var AddressService = function(options) { var AddressService = function(options) {
@ -409,37 +408,28 @@ AddressService.prototype._getAddressHistory = function(address, options, callbac
async.waterfall([ async.waterfall([
// get all the txs from the mempool that involve our address
function(next) { function(next) {
if (!options.queryMempool) { if (!options.queryMempool) {
return next(); 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); var txStream = self._getTxStream(address, options);
txStream.on('end', function() { txStream.on('end', function() {
return callback(null, options.results); 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); var txidStream = self._getTxidStream(address, options);
txidStream.pipe(txStream); txidStream.pipe(txStream);
next(); next();

View File

@ -137,10 +137,9 @@ MempoolService.prototype.getMempoolTransaction = function(txid, callback) {
}; };
MempoolService.prototype.getTxidsByAddress = function(address, callback) { // TODO optimize this using another index?
// given an address, give me all the tx ids for txs that involve this address MempoolService.prototype.getTxsByAddress = function(address, callback) {
// perceivably, our mempool will remain a managable size, so cycling over the whole
// pool won't be too spendy?
var self = this; var self = this;
var results = []; var results = [];
var start = self._encoding.encodeMempoolTransactionKey(new Array(65).join('0')); 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); var stream = self._db.createReadStream(criteria);
stream.on('error', function() {
return [];
});
stream.on('end', function() { stream.on('end', function() {
return callback(null, results); return callback(null, results);
}); });
stream.on('data', function(data) { stream.on('data', function(data) {
var tx = self._encoding.decodeMempoolTransactionValue(data.value); var tx = self._encoding.decodeMempoolTransactionValue(data.value);
var txid = self._involvesAddress(tx, address); tx = self._involvesAddress(tx, address);
if (txid) { if (tx) {
results.push(txid); results.push(tx);
} }
}); });
@ -169,30 +172,28 @@ MempoolService.prototype.getTxidsByAddress = function(address, callback) {
MempoolService.prototype._involvesAddress = function(tx, address) { MempoolService.prototype._involvesAddress = function(tx, address) {
var _address; function contains(collection, network) {
for(var i = 0; i < tx.inputs.length; i++) { var _address;
var input = tx.inputs[i]; for(var i = 0; i < collection.length; i++) {
_address = input.getAddress(); var item = collection[i];
if (!_address) { _address = item.getAddress();
continue; if (!_address) {
} continue;
_address.network = this._network; }
_address = _address.toString(); _address.network = network;
if (address === _address) { _address = _address.toString();
return tx.txid(); if (address === _address) {
return true;
}
} }
} }
for(i = 0; i < tx.outputs.length; i++) { var collections = [ tx.outputs, tx.inputs ];
var output = tx.outputs[i];
_address = output.getAddress(); for(var i = 0; i < collections.length; i++) {
if (!_address) { var hasAddress = contains(collections[i], this._network);
continue; if (hasAddress) {
} return tx;
_address.network = this._network;
_address = _address.toString();
if (address === _address) {
return tx.txid();
} }
} }