Update confirmations on cached transactions

This commit is contained in:
Sebastiaan Pasma 2018-09-20 15:58:42 +02:00
commit 3cad7d7ad8
No known key found for this signature in database
GPG key ID: 9A2B0C8B95A1D26F

View file

@ -66,16 +66,21 @@
function addLatestTransactions(walletId, cachedTxs, newTxs) { function addLatestTransactions(walletId, cachedTxs, newTxs) {
var cachedTxIds = {}; var cachedTxIds = {};
cachedTxs.forEach(function forCachedTx(tx){ cachedTxs.forEach(function forCachedTx(tx, txIndex){
cachedTxIds[tx.txid] = true; cachedTxIds[tx.txid] = txIndex;
}); });
var someTransactionsWereNew = false; var someTransactionsWereNew = false;
var confirmationsUpdated = false;
var overlappingTxsCount = 0; var overlappingTxsCount = 0;
var uniqueNewTxs = []; var uniqueNewTxs = [];
newTxs.forEach(function forNewTx(tx){ newTxs.forEach(function forNewTx(tx){
if (cachedTxIds[tx.txid]) { if (typeof cachedTxIds[tx.txid] !== "undefined") {
if (cachedTxs[cachedTxIds[tx.txid]].confirmations < SAFE_CONFIRMATIONS && tx.confirmations >= SAFE_CONFIRMATIONS) {
cachedTxs[cachedTxIds[tx.txid]].confirmations = tx.confirmations;
confirmationsUpdated = true;
}
overlappingTxsCount++; overlappingTxsCount++;
} else { } else {
someTransactionsWereNew = true; someTransactionsWereNew = true;
@ -91,6 +96,9 @@
saveTxHistory(walletId, allTxs); saveTxHistory(walletId, allTxs);
return allTxs; return allTxs;
} else { } else {
if (confirmationsUpdated) {
saveTxHistory(walletId, cachedTxs);
}
return cachedTxs; return cachedTxs;
} }
} else { } else {
@ -104,6 +112,8 @@
// Only clear the cache once we have received new transactions from the server. // Only clear the cache once we have received new transactions from the server.
/** /**
* @param wallet
* @param start
* @param {function(err, txs)} cb - transactions is always an array, may be empty * @param {function(err, txs)} cb - transactions is always an array, may be empty
*/ */
function fetchTxHistoryByPage(wallet, start, cb) { function fetchTxHistoryByPage(wallet, start, cb) {
@ -187,7 +197,7 @@
}); });
return processedTxs; return processedTxs;
}; }
function saveTxHistory(walletId, processedTxs) { function saveTxHistory(walletId, processedTxs) {
storageService.setTxHistory(processedTxs, walletId, function onSetTxHistory(error){ storageService.setTxHistory(processedTxs, walletId, function onSetTxHistory(error){
@ -197,7 +207,6 @@
}); });
} }
function updateLocalTxHistoryByPage(wallet, getLatest, flushCacheOnNew, cb) { function updateLocalTxHistoryByPage(wallet, getLatest, flushCacheOnNew, cb) {
if (flushCacheOnNew) { if (flushCacheOnNew) {
@ -240,10 +249,5 @@
}); });
} }
} }
} }
})(); })();