From d68870f7435d3ed9b40b1ef91026325051ecfbb3 Mon Sep 17 00:00:00 2001 From: Brendon Duncan Date: Wed, 26 Sep 2018 18:03:14 +1200 Subject: [PATCH] Bugfix for getting wallet history when cache is empty, while still enforcing overlap in transactions when cache contains transactions, to ensure no transactions are missed. --- src/js/services/wallet-history.service.js | 33 ++++++++++++++++------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/js/services/wallet-history.service.js b/src/js/services/wallet-history.service.js index a9792c170..f114a396d 100644 --- a/src/js/services/wallet-history.service.js +++ b/src/js/services/wallet-history.service.js @@ -6,7 +6,7 @@ .module('bitcoincom.services') .factory('walletHistoryService', walletHistoryService); - function walletHistoryService(configService, storageService, lodash, $log, txFormatService) { + function walletHistoryService(storageService, lodash, $log, txFormatService) { var PAGE_SIZE = 50; //var PAGE_SIZE = 20; // For dev only // How much to overlap on each end of the page, for mitigating inconsistent sort order. @@ -47,11 +47,17 @@ } }); - var overlappingTxFraction = overlappingTxsCount / Math.min(cachedTxs.length, PAGE_OVERLAP); - console.log('overlappingTxFraction:', overlappingTxFraction); - console.log('overlappingTxsCount:', overlappingTxsCount); - - if (overlappingTxFraction >= MIN_KNOWN_TX_OVERLAP_FRACTION || (someTransactionsWereNew && overlappingTxsCount === 0)) { // We are good + var txsAreContinuous = false; + if (cachedTxs.length > 0) { + var overlappingTxFraction = overlappingTxsCount / Math.min(cachedTxs.length, PAGE_OVERLAP); + console.log('overlappingTxsCount:', overlappingTxsCount); + console.log('overlappingTxFraction:', overlappingTxFraction); + txsAreContinuous = overlappingTxFraction >= MIN_KNOWN_TX_OVERLAP_FRACTION; + } else { + txsAreContinuous = true; + } + + if (txsAreContinuous) { if (someTransactionsWereNew) { saveTxHistory(walletId, cachedTxs); } else if (confirmationsUpdated) { @@ -62,7 +68,7 @@ return cachedTxs; } else { // We might be missing some txs. - console.error('We might be missing some txs in the history.'); + $log.error('We might be missing some txs in the history.'); // Our history is wrong, so remove it - we could instead, try to fetch data that was not so early. storageService.removeTxHistory(walletId, function onRemoveTxHistory(){}); return []; @@ -92,9 +98,17 @@ } }); - var overlappingTxFraction = overlappingTxsCount / Math.min(cachedTxs.length, PAGE_OVERLAP); + var txsAreContinuous = false; + if (cachedTxs.length > 0) { + var overlappingTxFraction = overlappingTxsCount / Math.min(cachedTxs.length, PAGE_OVERLAP); + console.log('overlappingTxsCount:', overlappingTxsCount); + console.log('overlappingTxFraction:', overlappingTxFraction); + txsAreContinuous = overlappingTxFraction >= MIN_KNOWN_TX_OVERLAP_FRACTION; + } else { + txsAreContinuous = true; + } - if (overlappingTxFraction >= MIN_KNOWN_TX_OVERLAP_FRACTION || (someTransactionsWereNew && overlappingTxsCount === 0)) { // We are good + if (txsAreContinuous) { if (someTransactionsWereNew) { var allTxs = uniqueNewTxs.concat(cachedTxs); saveTxHistory(walletId, allTxs); @@ -107,6 +121,7 @@ } } else { // We might be missing some txs. + $log.error('We might be missing some txs in the history.'); // Our history is wrong, so just include the latest ones saveTxHistory(walletId, newTxs); return newTxs;