Fixed get utxos to retrieve mempool txs.

This commit is contained in:
Chris Kleeschulte 2017-10-12 17:53:24 -04:00
parent 929041b9b6
commit e15695cfa6
No known key found for this signature in database
GPG Key ID: 33195D27EF6BDB7F
3 changed files with 83 additions and 23 deletions

View File

@ -203,43 +203,101 @@ AddressService.prototype.getAddressUnspentOutputs = function(address, options, c
lt: end lt: end
}; };
var utxoStream = self._db.createReadStream(criteria); async.waterfall([
var streamErr; // query the mempool if necessary
function(next) {
utxoStream.on('end', function() { if (!options.queryMempool) {
return next(null, []);
}
if (streamErr) { self._mempool.getTxsByAddress(address, next);
return callback(streamErr); },
// if mempool utxos, then add them first
function(mempoolTxs, next) {
if (mempoolTxs.length <= 0) {
return next();
}
mempoolTxs.forEach(function(tx) {
results = results.concat(self._getMempoolUtxos(tx, address));
});
next();
},
function(next) {
var utxoStream = self._db.createReadStream(criteria);
var streamErr;
utxoStream.on('end', function() {
if (streamErr) {
return callback(streamErr);
}
results = utils.orderByConfirmations(results);
next(null, results);
});
utxoStream.on('error', function(err) {
streamErr = err;
});
utxoStream.on('data', function(data) {
var key = self._encoding.decodeUtxoIndexKey(data.key);
var value = self._encoding.decodeUtxoIndexValue(data.value);
results.push({
address: address,
txid: key.txid,
vout: key.outputIndex,
ts: value.timestamp,
scriptPubKey: value.script.toString('hex'),
amount: Unit.fromSatoshis(value.satoshis).toBTC(),
height: value.height,
satoshis: value.satoshis,
confirmationsFromCache: true
});
});
} }
], callback);
callback(null, results); };
}); AddressService.prototype._getMempoolUtxos = function(tx, address) {
utxoStream.on('error', function(err) { var results = [];
streamErr = err;
});
utxoStream.on('data', function(data) { for(var i = 0; i < tx.outputs.length; i++) {
var key = self._encoding.decodeUtxoIndexKey(data.key); var output = tx.outputs[i];
var value = self._encoding.decodeUtxoIndexValue(data.value);
if (utils.getAddress(output, this._network) !== address) {
continue;
}
results.push({ results.push({
address: address, address: address,
txid: key.txid, txid: tx.txid(),
vout: key.outputIndex, vout: i,
ts: value.timestamp, scriptPubKey: output.script.toRaw().toString('hex'),
scriptPubKey: value.script.toString('hex'), amount: Unit.fromSatoshis(output.value).toBTC(),
amount: Unit.fromSatoshis(value.satoshis).toBTC(), height: null,
height: value.height, satoshis: output.value,
satoshis: value.satoshis, confirmations: 0
confirmationsFromCache: true
}); });
}
}); return results;
}; };
AddressService.prototype.getAPIMethods = function() { AddressService.prototype.getAPIMethods = function() {

View File

@ -162,6 +162,7 @@ TransactionService.prototype.setTxMetaInfo = function(tx, options, callback) {
if (tx.__inputValues) { if (tx.__inputValues) {
return next(null, tx); return next(null, tx);
} }
// the tx's that contain these input values could, themselves be unconfirmed // the tx's that contain these input values could, themselves be unconfirmed
// we are also assuming that this tx is from thet mempool // we are also assuming that this tx is from thet mempool
self._getInputValues(tx, options, function(err, inputValues) { self._getInputValues(tx, options, function(err, inputValues) {

View File

@ -180,6 +180,7 @@ describe('Address Service', function() {
var txidStream = new EventEmitter(); var txidStream = new EventEmitter();
addressService._mempool = { getTxsByAddress: sinon.stub().callsArgWith(1, null, []) };
var createReadStream = sandbox.stub().returns(txidStream); var createReadStream = sandbox.stub().returns(txidStream);
addressService._db = { createReadStream: createReadStream }; addressService._db = { createReadStream: createReadStream };