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,8 +203,36 @@ AddressService.prototype.getAddressUnspentOutputs = function(address, options, c
lt: end lt: end
}; };
var utxoStream = self._db.createReadStream(criteria); async.waterfall([
// query the mempool if necessary
function(next) {
if (!options.queryMempool) {
return next(null, []);
}
self._mempool.getTxsByAddress(address, next);
},
// 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; var streamErr;
utxoStream.on('end', function() { utxoStream.on('end', function() {
@ -213,7 +241,8 @@ AddressService.prototype.getAddressUnspentOutputs = function(address, options, c
return callback(streamErr); return callback(streamErr);
} }
callback(null, results); results = utils.orderByConfirmations(results);
next(null, results);
}); });
@ -239,9 +268,38 @@ AddressService.prototype.getAddressUnspentOutputs = function(address, options, c
}); });
}); });
}
], callback);
}; };
AddressService.prototype._getMempoolUtxos = function(tx, address) {
var results = [];
for(var i = 0; i < tx.outputs.length; i++) {
var output = tx.outputs[i];
if (utils.getAddress(output, this._network) !== address) {
continue;
}
results.push({
address: address,
txid: tx.txid(),
vout: i,
scriptPubKey: output.script.toRaw().toString('hex'),
amount: Unit.fromSatoshis(output.value).toBTC(),
height: null,
satoshis: output.value,
confirmations: 0
});
}
return results;
};
AddressService.prototype.getAPIMethods = function() { AddressService.prototype.getAPIMethods = function() {
return [ return [
['getAddressHistory', this, this.getAddressHistory, 2], ['getAddressHistory', this, this.getAddressHistory, 2],

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 };