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.

This commit is contained in:
Brendon Duncan 2018-09-26 18:03:14 +12:00
commit d68870f743

View file

@ -6,7 +6,7 @@
.module('bitcoincom.services') .module('bitcoincom.services')
.factory('walletHistoryService', walletHistoryService); .factory('walletHistoryService', walletHistoryService);
function walletHistoryService(configService, storageService, lodash, $log, txFormatService) { function walletHistoryService(storageService, lodash, $log, txFormatService) {
var PAGE_SIZE = 50; var PAGE_SIZE = 50;
//var PAGE_SIZE = 20; // For dev only //var PAGE_SIZE = 20; // For dev only
// How much to overlap on each end of the page, for mitigating inconsistent sort order. // 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); var txsAreContinuous = false;
console.log('overlappingTxFraction:', overlappingTxFraction); if (cachedTxs.length > 0) {
console.log('overlappingTxsCount:', overlappingTxsCount); var overlappingTxFraction = overlappingTxsCount / Math.min(cachedTxs.length, PAGE_OVERLAP);
console.log('overlappingTxsCount:', overlappingTxsCount);
if (overlappingTxFraction >= MIN_KNOWN_TX_OVERLAP_FRACTION || (someTransactionsWereNew && overlappingTxsCount === 0)) { // We are good console.log('overlappingTxFraction:', overlappingTxFraction);
txsAreContinuous = overlappingTxFraction >= MIN_KNOWN_TX_OVERLAP_FRACTION;
} else {
txsAreContinuous = true;
}
if (txsAreContinuous) {
if (someTransactionsWereNew) { if (someTransactionsWereNew) {
saveTxHistory(walletId, cachedTxs); saveTxHistory(walletId, cachedTxs);
} else if (confirmationsUpdated) { } else if (confirmationsUpdated) {
@ -62,7 +68,7 @@
return cachedTxs; return cachedTxs;
} else { } else {
// We might be missing some txs. // 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. // Our history is wrong, so remove it - we could instead, try to fetch data that was not so early.
storageService.removeTxHistory(walletId, function onRemoveTxHistory(){}); storageService.removeTxHistory(walletId, function onRemoveTxHistory(){});
return []; 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) { if (someTransactionsWereNew) {
var allTxs = uniqueNewTxs.concat(cachedTxs); var allTxs = uniqueNewTxs.concat(cachedTxs);
saveTxHistory(walletId, allTxs); saveTxHistory(walletId, allTxs);
@ -107,6 +121,7 @@
} }
} else { } else {
// We might be missing some txs. // 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 // Our history is wrong, so just include the latest ones
saveTxHistory(walletId, newTxs); saveTxHistory(walletId, newTxs);
return newTxs; return newTxs;