From 7f17dd4a4c1402f92d948c07c66790723b829234 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 22 Apr 2016 12:09:57 -0400 Subject: [PATCH] bitcoind: fixed issue with cache mempool updates --- lib/services/bitcoind.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/services/bitcoind.js b/lib/services/bitcoind.js index 59f20cf1..c952f9b3 100644 --- a/lib/services/bitcoind.js +++ b/lib/services/bitcoind.js @@ -832,23 +832,25 @@ Bitcoin.prototype.getAddressUnspentOutputs = function(addressArg, options, callb return { address: delta.address, txid: delta.txid, - height: -1, // unconfirmed outputIndex: delta.index, script: script.toHex(), - satoshis: delta.satoshis + satoshis: delta.satoshis, + timestamp: delta.timestamp }; } - function updateWithMempool(utxos, mempoolDeltas) { + function updateWithMempool(confirmedUtxos, mempoolDeltas) { if (!mempoolDeltas || !mempoolDeltas.length) { - return utxos; + return confirmedUtxos; } var isSpentOutputs = false; + var mempoolUnspentOutputs = []; var spentOutputs = []; + for (var i = 0; i < mempoolDeltas.length; i++) { var delta = mempoolDeltas[i]; if (delta.satoshis > 0) { - utxos.push(transformUnspentOutput(delta)); + mempoolUnspentOutputs.push(transformUnspentOutput(delta)); } else if (delta.satoshis < 0) { if (!spentOutputs[delta.prevtxid]) { spentOutputs[delta.prevtxid] = [delta.prevout]; @@ -858,6 +860,9 @@ Bitcoin.prototype.getAddressUnspentOutputs = function(addressArg, options, callb isSpentOutputs = true; } } + + var utxos = mempoolUnspentOutputs.reverse().concat(confirmedUtxos); + if (isSpentOutputs) { return utxos.filter(function(utxo) { if (!spentOutputs[utxo.txid]) { @@ -867,21 +872,23 @@ Bitcoin.prototype.getAddressUnspentOutputs = function(addressArg, options, callb } }); } + return utxos; } function finish(mempoolDeltas) { if (utxos) { return setImmediate(function() { - callback(null, updateWithMempool(utxos, mempoolDeltas).reverse()); + callback(null, updateWithMempool(utxos, mempoolDeltas)); }); } else { self.client.getAddressUtxos({addresses: addresses}, function(err, response) { if (err) { return callback(self._wrapRPCError(err)); } - self.utxosCache.set(cacheKey, response.result); - callback(null, updateWithMempool(response.result, mempoolDeltas).reverse()); + var utxos = response.result.reverse(); + self.utxosCache.set(cacheKey, utxos); + callback(null, updateWithMempool(utxos, mempoolDeltas)); }); } }