Fixed getting txs from memory pool from calls to getAddressHistory.

This commit is contained in:
Chris Kleeschulte 2017-10-08 14:14:22 -04:00
parent f10106f9a0
commit ce653b5a12
No known key found for this signature in database
GPG Key ID: 33195D27EF6BDB7F
4 changed files with 36 additions and 68 deletions

View File

@ -70,7 +70,7 @@ AddressService.prototype.getAddressHistory = function(addresses, options, callba
} }
txList = utils.dedupByTxid(txList); txList = utils.dedupByTxid(txList);
txList = utils.orderByConfirmationsDesc(txList); txList = utils.orderByConfirmations(txList);
var results = { var results = {
totalCount: txList.length, totalCount: txList.length,
@ -313,9 +313,10 @@ AddressService.prototype._getTxidStream = function(address, options) {
AddressService.prototype._transformTxForAddressHistory = function(opts, chunk, enc, callback) { AddressService.prototype._transformTxForAddressHistory = function(opts, chunk, enc, callback) {
var self = this; var self = this;
var key = self._encoding.decodeAddressIndexKey(chunk);
self._tx.getTransaction(key.txid, opts, function(err, tx) { var txid = _.isString(chunk) ? chunk : self._encoding.decodeAddressIndexKey(chunk).txid;
self._tx.getTransaction(txid, opts, function(err, tx) {
if (err) { if (err) {
log.error('Address Service: gettransaction ' + err); log.error('Address Service: gettransaction ' + err);
@ -324,7 +325,7 @@ AddressService.prototype._transformTxForAddressHistory = function(opts, chunk, e
} }
if (!tx) { if (!tx) {
log.error('Address Service: Could not find tx for txid: ' + key.txid + '. This should not be possible, check indexes.'); log.error('Address Service: Could not find tx for txid: ' + txid + '. This should not be possible, check indexes.');
opts.stream.emit('error', err); opts.stream.emit('error', err);
return callback(); return callback();
} }
@ -353,6 +354,7 @@ AddressService.prototype._getTxStream = function(address, options) {
}; };
// main api function for insight-api/bws
AddressService.prototype._getAddressHistory = function(address, options, callback) { AddressService.prototype._getAddressHistory = function(address, options, callback) {
var self = this; var self = this;
@ -371,7 +373,6 @@ 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) {
@ -396,12 +397,16 @@ AddressService.prototype._getAddressHistory = function(address, options, callbac
}); });
if (mempoolTxids.length > 0) { if (mempoolTxids.length > 0) {
var mempoolxidStream = new Stream.Readable({ objectMode: true });
mempoolxidStream.pipe(txStream); var mempoolTxidStream = new Stream.Readable({ objectMode: true });
mempoolTxidStream.pipe(txStream);
mempoolTxids.forEach(function(txid) { mempoolTxids.forEach(function(txid) {
mempoolxidStream.push(txid); mempoolTxidStream.push(txid);
}); });
mempoolxidStream.unpipe();
mempoolTxidStream.push(null);
} }
var txidStream = self._getTxidStream(address, options); var txidStream = self._getTxidStream(address, options);

View File

@ -386,17 +386,24 @@ BlockService.prototype.stop = function(callback) {
setImmediate(callback); setImmediate(callback);
}; };
BlockService.prototype._getTimeSinceLastBlock = function(blockHash, prevBlockHash, callback) { BlockService.prototype._getTimeSinceLastBlock = function(callback) {
var self = this; var self = this;
async.map([ blockHash, prevBlockHash ], function(hash, next) { self._header.getBlockHeader(Math.max(self._tip.height - 1, 0), function(err, header) {
self._timestamp.getTimestamp(hash, next);
}, function(err, times) { if(err || !header) {
if (err) { return callback(err || new Error('Block Service: we should have a header in order to get time since last block.'));
return callback(err);
} }
return callback(null, utils.convertMillisecondsToHumanReadable((times[0] * 1000) - (times[1] * 1000)));
async.map([ self._tip.hash, header.hash ], function(hash, next) {
self._timestamp.getTimestamp(hash, next);
}, function(err, times) {
if (err) {
return callback(err);
}
return callback(null, utils.convertMillisecondsToHumanReadable((times[0] * 1000) - (times[1] * 1000)));
});
}); });
}; };
@ -413,7 +420,7 @@ BlockService.prototype._queueBlock = function(block) {
return self._handleError(err); return self._handleError(err);
} }
self._logSynced(block.rhash(), true); self._logSynced(block.rhash());
self._blocksInQueue--; self._blocksInQueue--;
}); });
@ -837,7 +844,7 @@ BlockService.prototype._setTip = function(tip, callback) {
this._saveTip(tip, callback); this._saveTip(tip, callback);
}; };
BlockService.prototype._logSynced = function(blockHash, noHeight) { BlockService.prototype._logSynced = function() {
var self = this; var self = this;
@ -845,58 +852,14 @@ BlockService.prototype._logSynced = function(blockHash, noHeight) {
return; return;
} }
var blockHeight; self._getTimeSinceLastBlock(function(err, diff) {
var timeDiff;
async.waterfall([
function(next) {
if (noHeight) {
return next(null, null);
}
self._header.getBlockHeader(blockHash, function(err, header) {
if (err) {
return next(err);
}
if (!header) {
return next(null, null);
}
blockHeight = header.height;
next(null, header.prevHash);
});
},
function(prevBlockHash, next) {
if (!prevBlockHash) {
return next();
}
self._getTimeSinceLastBlock(blockHash, prevBlockHash, function(err, diff) {
if (err) {
return self._handleError(err);
}
timeDiff = diff;
next();
});
}
], function(err) {
if (err) { if (err) {
return self._handleError(err); return self._handleError(err);
} }
var supplementals = ''; log.info('Block Service: The best block hash is: ' + self._tip.hash +
if (blockHeight && timeDiff) { ' at height: ' + self._tip.height + '. Time between the last 2 blocks (adjusted): ' + diff);
supplementals += ' at height: ' + blockHeight + '. Time between the last 2 blocks (adjusted): ' + timeDiff;
}
log.info('Block Service: The best block hash is: ' + blockHash + supplementals);
}); });

View File

@ -163,7 +163,7 @@ MempoolService.prototype.getTxidsByAddress = function(address, callback) {
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); var txid = self._involvesAddress(tx, address);
if (tx) { if (txid) {
results.push(txid); results.push(txid);
} }
}); });

View File

@ -142,8 +142,8 @@ TransactionService.prototype._getSupplementaryTransactionInfo = function(txid, t
if (header) { if (header) {
// Do we need both of these? // Do we need both of these?
tx.blockHash = header.hash; tx.blockhash = header.hash;
tx.__blockHash = header.hash; tx.__blockhash = header.hash;
} }
callback(null, txid, tx, options); callback(null, txid, tx, options);