Fixed get utxos to retrieve mempool txs.
This commit is contained in:
parent
929041b9b6
commit
e15695cfa6
@ -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() {
|
||||||
|
|||||||
@ -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) {
|
||||||
|
|||||||
@ -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 };
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user