diff --git a/src/js/controllers/walletDetails.js b/src/js/controllers/walletDetails.js index d22ab388f..fc45d70f6 100644 --- a/src/js/controllers/walletDetails.js +++ b/src/js/controllers/walletDetails.js @@ -191,7 +191,7 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun if (err) return; $timeout(function() { walletService.startScan($scope.wallet, function() { - $scope.updateAll(); + $scope.updateAll(true, true); $scope.$apply(); }); }); @@ -287,6 +287,7 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun } function fetchAndShowTxHistory(getLatest, flushCacheOnNew) { + console.log('pagination fetchAndShowTxHistory() getLatest:', getLatest, ', flushCacheOnNew:', flushCacheOnNew); $scope.vm.updatingTxHistory = true; walletHistoryService.updateLocalTxHistoryByPage($scope.wallet, getLatest, flushCacheOnNew, function onUpdateLocalTxHistoryByPage(err, txHistory) { @@ -374,17 +375,6 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun } fetchAndShowTxHistory(false, false); - /* - $scope.vm.updatingTxHistory = true; - $timeout(function() { - walletService.getMoreTxs($scope.wallet, function onMoreTxs() { - currentTxHistoryDisplayPage++; - //$scope.showHistory(); - $scope.$broadcast('scroll.infiniteScrollComplete'); - $scope.vm.updatingTxHistory = false; - }); - }, 100); - */ }; // on-refresh="onRefresh()" @@ -392,14 +382,14 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun $timeout(function() { $scope.$broadcast('scroll.refreshComplete'); }, 300); - $scope.updateAll(true); + $scope.updateAll(true, false); }; - $scope.updateAll = function(forceStatusUpdate, getLatestTx, flushTxCacheOnNew)  { + $scope.updateAll = function(forceStatusUpdate, flushTxCacheOnNew)  { console.log('pagination updateAll()'); updateStatus(forceStatusUpdate); //updateTxHistory(cb); - fetchAndShowTxHistory(getLatestTx, flushTxCacheOnNew); + fetchAndShowTxHistory(true, flushTxCacheOnNew); }; $scope.hideToggle = function() { @@ -529,11 +519,11 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun listeners = [ $rootScope.$on('bwsEvent', function(e, walletId) { if (walletId == $scope.wallet.id && e.type != 'NewAddress') - $scope.updateAll(); + $scope.updateAll(false, false); }), $rootScope.$on('Local/TxAction', function(e, walletId) { if (walletId == $scope.wallet.id) - $scope.updateAll(); + $scope.updateAll(false, false); }), ]; }); @@ -542,7 +532,7 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun $scope.$on("$ionicView.afterEnter", function(event, data) { updateTxHistoryFromCachedData(); - $scope.updateAll(false, true, true); + $scope.updateAll(false, true); refreshAmountSection(); //refreshInterval = $interval($scope.onRefresh, 10 * 1000); //refreshInterval = $interval($scope.onRefresh, 120 * 1000); // For testing diff --git a/src/js/services/wallet-history.service.js b/src/js/services/wallet-history.service.js index 00847cbbf..8d6345e1d 100644 --- a/src/js/services/wallet-history.service.js +++ b/src/js/services/wallet-history.service.js @@ -31,21 +31,21 @@ cachedTxIds[tx.txid] = true; }); - var someTransactionWereNew = false; + var someTransactionsWereNew = false; var overlappingTxsCount = 0; newTxs.forEach(function forNewTx(tx){ if (cachedTxIds[tx.txid]) { overlappingTxsCount++; } else { - someTransactionWereNew = true; + someTransactionsWereNew = true; cachedTxs.push(tx); } }); - console.log('pagination Overlapping transactions:', overlappingTxsCount); + console.log('pagination Early transactions overlapping:', overlappingTxsCount); if (overlappingTxsCount >= MIN_KNOWN_TX_OVERLAP) { // We are good - if (someTransactionWereNew) { + if (someTransactionsWereNew) { console.log('pagination someTransactionsWereNew'); saveTxHistory(walletId, cachedTxs); } @@ -60,7 +60,42 @@ } - function addLatestTransactions(cachedTxs, newTxs) { + function addLatestTransactions(walletId, cachedTxs, newTxs) { + var cachedTxIds = {}; + cachedTxs.forEach(function forCachedTx(tx){ + cachedTxIds[tx.txid] = true; + }); + + var someTransactionsWereNew = false; + var overlappingTxsCount = 0; + var uniqueNewTxs = []; + + newTxs.forEach(function forNewTx(tx){ + if (cachedTxIds[tx.txid]) { + overlappingTxsCount++; + } else { + someTransactionWereNew = true; + uniqueNewTxs.push(tx); + } + }); + + console.log('pagination Latest transactions overlapping:', overlappingTxsCount); + if (overlappingTxsCount >= MIN_KNOWN_TX_OVERLAP) { // We are good + if (someTransactionsWereNew) { + console.log('pagination someTransactionsWereNew'); + var allTxs = uniqueNewTxs.concat(cachedTxs); + saveTxHistory(walletId, allTxs); + return allTxs; + } else { + return cachedTxs; + } + } else { + // We might be missing some txs. + // Our history is wrong, so just include the latest ones + saveTxHistory(walletId, newTxs); + return newTxs; + } + } // Only clear the cache once we have received new transactions from the server. @@ -87,13 +122,6 @@ var processedTxs = processNewTxs(wallet, txsFromServer); - /* - if (getLatest) { - console.log('pagination Saving retrieved txs.'); - saveTxHistory(wallet, processedTxs); - } - */ - return cb(null, processedTxs); }); } @@ -154,8 +182,6 @@ $log.debug('Ignoring duplicate TX in history: ' + tx.txid) } }); - - // Update notes? return processedTxs; }; @@ -175,7 +201,7 @@ function updateLocalTxHistoryByPage(wallet, getLatest, flushCacheOnNew, cb) { if (flushCacheOnNew) { - console.log('pagination Getting latest txs.'); + console.log('pagination Getting latest txs, will then flush cache.'); fetchTxHistoryByPage(wallet, 0, function onFetchTxHistory(err, txs){ if (err) { return cb(err, txs); @@ -184,7 +210,7 @@ return cb(null, txs); }); } else { - console.log('pagination Getting early txs.'); + console.log('pagination Getting txs to add to cache.'); getCachedTxHistory(wallet.id, function onCachedHistory(err, cachedTxs){ if (err) { $log.error('Failed to get cached tx history.', err);