Adding latest transactions.

This commit is contained in:
Brendon Duncan 2018-08-22 19:28:24 +12:00
commit ad6a1fbe8d
2 changed files with 50 additions and 34 deletions

View file

@ -191,7 +191,7 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun
if (err) return; if (err) return;
$timeout(function() { $timeout(function() {
walletService.startScan($scope.wallet, function() { walletService.startScan($scope.wallet, function() {
$scope.updateAll(); $scope.updateAll(true, true);
$scope.$apply(); $scope.$apply();
}); });
}); });
@ -287,6 +287,7 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun
} }
function fetchAndShowTxHistory(getLatest, flushCacheOnNew) { function fetchAndShowTxHistory(getLatest, flushCacheOnNew) {
console.log('pagination fetchAndShowTxHistory() getLatest:', getLatest, ', flushCacheOnNew:', flushCacheOnNew);
$scope.vm.updatingTxHistory = true; $scope.vm.updatingTxHistory = true;
walletHistoryService.updateLocalTxHistoryByPage($scope.wallet, getLatest, flushCacheOnNew, function onUpdateLocalTxHistoryByPage(err, txHistory) { walletHistoryService.updateLocalTxHistoryByPage($scope.wallet, getLatest, flushCacheOnNew, function onUpdateLocalTxHistoryByPage(err, txHistory) {
@ -374,17 +375,6 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun
} }
fetchAndShowTxHistory(false, false); 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()" // on-refresh="onRefresh()"
@ -392,14 +382,14 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun
$timeout(function() { $timeout(function() {
$scope.$broadcast('scroll.refreshComplete'); $scope.$broadcast('scroll.refreshComplete');
}, 300); }, 300);
$scope.updateAll(true); $scope.updateAll(true, false);
}; };
$scope.updateAll = function(forceStatusUpdate, getLatestTx, flushTxCacheOnNew)  { $scope.updateAll = function(forceStatusUpdate, flushTxCacheOnNew)  {
console.log('pagination updateAll()'); console.log('pagination updateAll()');
updateStatus(forceStatusUpdate); updateStatus(forceStatusUpdate);
//updateTxHistory(cb); //updateTxHistory(cb);
fetchAndShowTxHistory(getLatestTx, flushTxCacheOnNew); fetchAndShowTxHistory(true, flushTxCacheOnNew);
}; };
$scope.hideToggle = function() { $scope.hideToggle = function() {
@ -529,11 +519,11 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun
listeners = [ listeners = [
$rootScope.$on('bwsEvent', function(e, walletId) { $rootScope.$on('bwsEvent', function(e, walletId) {
if (walletId == $scope.wallet.id && e.type != 'NewAddress') if (walletId == $scope.wallet.id && e.type != 'NewAddress')
$scope.updateAll(); $scope.updateAll(false, false);
}), }),
$rootScope.$on('Local/TxAction', function(e, walletId) { $rootScope.$on('Local/TxAction', function(e, walletId) {
if (walletId == $scope.wallet.id) 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) { $scope.$on("$ionicView.afterEnter", function(event, data) {
updateTxHistoryFromCachedData(); updateTxHistoryFromCachedData();
$scope.updateAll(false, true, true); $scope.updateAll(false, true);
refreshAmountSection(); refreshAmountSection();
//refreshInterval = $interval($scope.onRefresh, 10 * 1000); //refreshInterval = $interval($scope.onRefresh, 10 * 1000);
//refreshInterval = $interval($scope.onRefresh, 120 * 1000); // For testing //refreshInterval = $interval($scope.onRefresh, 120 * 1000); // For testing

View file

@ -31,21 +31,21 @@
cachedTxIds[tx.txid] = true; cachedTxIds[tx.txid] = true;
}); });
var someTransactionWereNew = false; var someTransactionsWereNew = false;
var overlappingTxsCount = 0; var overlappingTxsCount = 0;
newTxs.forEach(function forNewTx(tx){ newTxs.forEach(function forNewTx(tx){
if (cachedTxIds[tx.txid]) { if (cachedTxIds[tx.txid]) {
overlappingTxsCount++; overlappingTxsCount++;
} else { } else {
someTransactionWereNew = true; someTransactionsWereNew = true;
cachedTxs.push(tx); 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 (overlappingTxsCount >= MIN_KNOWN_TX_OVERLAP) { // We are good
if (someTransactionWereNew) { if (someTransactionsWereNew) {
console.log('pagination someTransactionsWereNew'); console.log('pagination someTransactionsWereNew');
saveTxHistory(walletId, cachedTxs); 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. // Only clear the cache once we have received new transactions from the server.
@ -87,13 +122,6 @@
var processedTxs = processNewTxs(wallet, txsFromServer); var processedTxs = processNewTxs(wallet, txsFromServer);
/*
if (getLatest) {
console.log('pagination Saving retrieved txs.');
saveTxHistory(wallet, processedTxs);
}
*/
return cb(null, processedTxs); return cb(null, processedTxs);
}); });
} }
@ -154,8 +182,6 @@
$log.debug('Ignoring duplicate TX in history: ' + tx.txid) $log.debug('Ignoring duplicate TX in history: ' + tx.txid)
} }
}); });
// Update notes?
return processedTxs; return processedTxs;
}; };
@ -175,7 +201,7 @@
function updateLocalTxHistoryByPage(wallet, getLatest, flushCacheOnNew, cb) { function updateLocalTxHistoryByPage(wallet, getLatest, flushCacheOnNew, cb) {
if (flushCacheOnNew) { 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){ fetchTxHistoryByPage(wallet, 0, function onFetchTxHistory(err, txs){
if (err) { if (err) {
return cb(err, txs); return cb(err, txs);
@ -184,7 +210,7 @@
return cb(null, txs); return cb(null, txs);
}); });
} else { } else {
console.log('pagination Getting early txs.'); console.log('pagination Getting txs to add to cache.');
getCachedTxHistory(wallet.id, function onCachedHistory(err, cachedTxs){ getCachedTxHistory(wallet.id, function onCachedHistory(err, cachedTxs){
if (err) { if (err) {
$log.error('Failed to get cached tx history.', err); $log.error('Failed to get cached tx history.', err);