From ea51e035abd3fe8066669c1e13faed876b04404b Mon Sep 17 00:00:00 2001 From: Brendon Duncan Date: Fri, 21 Sep 2018 03:56:51 -0700 Subject: [PATCH] Fixed updating of confirmations of cached transactions. Cache now properly cleared on first fetch from Wallet Details screen. --- .../controllers/wallet-details.controller.js | 4 +- src/js/services/wallet-history.service.js | 50 +++++++++++++------ 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/js/controllers/wallet-details.controller.js b/src/js/controllers/wallet-details.controller.js index f3109db8b..cef57925e 100644 --- a/src/js/controllers/wallet-details.controller.js +++ b/src/js/controllers/wallet-details.controller.js @@ -400,8 +400,8 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun var refreshInterval; - $scope.$on("$ionicView.afterEnter", function(event, data) { - $scope.updateAll(); + $scope.$on("$ionicView.afterEnter", function onAfterEnter(event, data) { + $scope.updateAll(true, true); // refreshAmountSection(); refreshInterval = $interval($scope.onRefresh, 10 * 1000); $timeout(function() { diff --git a/src/js/services/wallet-history.service.js b/src/js/services/wallet-history.service.js index 0738d4348..13fc8d636 100644 --- a/src/js/services/wallet-history.service.js +++ b/src/js/services/wallet-history.service.js @@ -27,20 +27,23 @@ function addEarlyTransactions(walletId, cachedTxs, newTxs) { - var cachedTxIds = {}; + var cachedTxIndexFromId = {}; cachedTxs.forEach(function forCachedTx(tx){ - cachedTxIds[tx.txid] = true; + cachedTxIndexFromId[tx.txid] = true; }); + var confirmationsUpdated = false; var someTransactionsWereNew = false; var overlappingTxsCount = 0; newTxs.forEach(function forNewTx(tx){ - if (cachedTxIds[tx.txid]) { - overlappingTxsCount++; - } else { + if (typeof cachedTxIndexFromId[tx.txid] === "undefined") { someTransactionsWereNew = true; cachedTxs.push(tx); + } else { + var txUpdated = updateCachedTx(cachedTxs, cachedTxIndexFromId, tx); + confirmationsUpdated = confirmationsUpdated || txUpdated; + overlappingTxsCount++; } }); @@ -50,6 +53,8 @@ if (overlappingTxFraction >= MIN_KNOWN_TX_OVERLAP_FRACTION) { // We are good if (someTransactionsWereNew) { saveTxHistory(walletId, cachedTxs); + } else if (confirmationsUpdated) { + saveTxHistory(walletId, cachedTxs); } else if (overlappingTxsCount === newTxs.length) { allTransactionsFetched = true; } @@ -65,9 +70,9 @@ } function addLatestTransactions(walletId, cachedTxs, newTxs) { - var cachedTxIds = {}; + var cachedTxIndexFromId = {}; cachedTxs.forEach(function forCachedTx(tx, txIndex){ - cachedTxIds[tx.txid] = txIndex; + cachedTxIndexFromId[tx.txid] = txIndex; }); var someTransactionsWereNew = false; @@ -76,15 +81,13 @@ var uniqueNewTxs = []; newTxs.forEach(function forNewTx(tx){ - 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++; - } else { + if (typeof cachedTxIndexFromId[tx.txid] === "undefined") { someTransactionsWereNew = true; uniqueNewTxs.push(tx); + } else { + var txUpdated = updateCachedTx(cachedTxs, cachedTxIndexFromId, tx); + confirmationsUpdated = confirmationsUpdated || txUpdated; + overlappingTxsCount++; } }); @@ -207,8 +210,25 @@ }); } - function updateLocalTxHistoryByPage(wallet, getLatest, flushCacheOnNew, cb) { + /** + * Returns true if the cached tx was updated + * @param {*} cachedTxs + * @param {*} cachedTxIndexFromId - Indices for cachedTxs, based on txid + * @param {*} tx - The most recent tx info + */ + function updateCachedTx(cachedTxs, cachedTxIndexFromId, tx) { + var updated = false; + var txIndex = cachedTxIndexFromId[tx.txid]; + var cachedTx = cachedTxs[txIndex]; + if (cachedTx.confirmations < SAFE_CONFIRMATIONS && tx.confirmations > cachedTx.confirmations) { + cachedTxs[txIndex].confirmations = tx.confirmations; + updated = true; + } + return updated; + } + + function updateLocalTxHistoryByPage(wallet, getLatest, flushCacheOnNew, cb) { if (flushCacheOnNew) { fetchTxHistoryByPage(wallet, 0, function onFetchTxHistory(err, txs){ if (err) {