bitcoind: fixed issue with cache mempool updates
This commit is contained in:
parent
0272b17f0e
commit
7f17dd4a4c
@ -832,23 +832,25 @@ Bitcoin.prototype.getAddressUnspentOutputs = function(addressArg, options, callb
|
|||||||
return {
|
return {
|
||||||
address: delta.address,
|
address: delta.address,
|
||||||
txid: delta.txid,
|
txid: delta.txid,
|
||||||
height: -1, // unconfirmed
|
|
||||||
outputIndex: delta.index,
|
outputIndex: delta.index,
|
||||||
script: script.toHex(),
|
script: script.toHex(),
|
||||||
satoshis: delta.satoshis
|
satoshis: delta.satoshis,
|
||||||
|
timestamp: delta.timestamp
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateWithMempool(utxos, mempoolDeltas) {
|
function updateWithMempool(confirmedUtxos, mempoolDeltas) {
|
||||||
if (!mempoolDeltas || !mempoolDeltas.length) {
|
if (!mempoolDeltas || !mempoolDeltas.length) {
|
||||||
return utxos;
|
return confirmedUtxos;
|
||||||
}
|
}
|
||||||
var isSpentOutputs = false;
|
var isSpentOutputs = false;
|
||||||
|
var mempoolUnspentOutputs = [];
|
||||||
var spentOutputs = [];
|
var spentOutputs = [];
|
||||||
|
|
||||||
for (var i = 0; i < mempoolDeltas.length; i++) {
|
for (var i = 0; i < mempoolDeltas.length; i++) {
|
||||||
var delta = mempoolDeltas[i];
|
var delta = mempoolDeltas[i];
|
||||||
if (delta.satoshis > 0) {
|
if (delta.satoshis > 0) {
|
||||||
utxos.push(transformUnspentOutput(delta));
|
mempoolUnspentOutputs.push(transformUnspentOutput(delta));
|
||||||
} else if (delta.satoshis < 0) {
|
} else if (delta.satoshis < 0) {
|
||||||
if (!spentOutputs[delta.prevtxid]) {
|
if (!spentOutputs[delta.prevtxid]) {
|
||||||
spentOutputs[delta.prevtxid] = [delta.prevout];
|
spentOutputs[delta.prevtxid] = [delta.prevout];
|
||||||
@ -858,6 +860,9 @@ Bitcoin.prototype.getAddressUnspentOutputs = function(addressArg, options, callb
|
|||||||
isSpentOutputs = true;
|
isSpentOutputs = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var utxos = mempoolUnspentOutputs.reverse().concat(confirmedUtxos);
|
||||||
|
|
||||||
if (isSpentOutputs) {
|
if (isSpentOutputs) {
|
||||||
return utxos.filter(function(utxo) {
|
return utxos.filter(function(utxo) {
|
||||||
if (!spentOutputs[utxo.txid]) {
|
if (!spentOutputs[utxo.txid]) {
|
||||||
@ -867,21 +872,23 @@ Bitcoin.prototype.getAddressUnspentOutputs = function(addressArg, options, callb
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return utxos;
|
return utxos;
|
||||||
}
|
}
|
||||||
|
|
||||||
function finish(mempoolDeltas) {
|
function finish(mempoolDeltas) {
|
||||||
if (utxos) {
|
if (utxos) {
|
||||||
return setImmediate(function() {
|
return setImmediate(function() {
|
||||||
callback(null, updateWithMempool(utxos, mempoolDeltas).reverse());
|
callback(null, updateWithMempool(utxos, mempoolDeltas));
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
self.client.getAddressUtxos({addresses: addresses}, function(err, response) {
|
self.client.getAddressUtxos({addresses: addresses}, function(err, response) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(self._wrapRPCError(err));
|
return callback(self._wrapRPCError(err));
|
||||||
}
|
}
|
||||||
self.utxosCache.set(cacheKey, response.result);
|
var utxos = response.result.reverse();
|
||||||
callback(null, updateWithMempool(response.result, mempoolDeltas).reverse());
|
self.utxosCache.set(cacheKey, utxos);
|
||||||
|
callback(null, updateWithMempool(utxos, mempoolDeltas));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user