fix local transaction sync with more than 6 confirmation

This commit is contained in:
Javier 2015-10-22 11:43:30 -03:00
commit c34873c177

View file

@ -734,13 +734,20 @@ angular.module('copayApp.controllers').controller('indexController', function($r
}); });
}; };
self.areEqualsTxs = function(firstTx, secondTx) { self.stopSync = function(remoteTx, localTx) {
if (firstTx.txid == secondTx.txid) if (remoteTx.txid == localTx.txid)
return true; return true;
else else
return false; return false;
} }
self.removeLessThanSixConfirmations = function(txs) {
return lodash.map(txs, function(tx) {
if (tx.confirmations >= 6)
return tx;
});
}
self.updateLocalTxHistory = function(cb) { self.updateLocalTxHistory = function(cb) {
var fc = profileService.focusedClient; var fc = profileService.focusedClient;
var c = fc.credentials; var c = fc.credentials;
@ -749,23 +756,25 @@ angular.module('copayApp.controllers').controller('indexController', function($r
storageService.getTxHistory(c.walletId, function(err, txs) { storageService.getTxHistory(c.walletId, function(err, txs) {
if (err) return cb(err); if (err) return cb(err);
var txsFromLocal; var localTxs;
try { try {
txsFromLocal = JSON.parse(txs); localTxs = JSON.parse(txs);
} catch (ex) { } catch (ex) {
return cb(ex); return cb(ex);
} }
if (!txsFromLocal) if (!localTxs)
txsFromLocal = []; localTxs = [];
var txsFromLocal = self.removeLessThanSixConfirmations(localTxs);
var count = 0; var count = 0;
fillTxsObject(txsToPush); fillTxsObject(txsToPush);
function fillTxsObject(txsToPush) { function fillTxsObject(txsToPush) {
self.makeTxHistoryRequest(txsToPush, txsFromLocal, function(err, skipLoop, txsResult) { self.makeTxHistoryRequest(txsToPush, txsFromLocal, function(err, exitLoop, txsResult) {
if (err) return cb(err); if (err) return cb(err);
if (skipLoop) { if (exitLoop) {
self.txHistory = []; self.txHistory = [];
self.setTxHistory(lodash.compact(txsResult.concat(txsFromLocal))); self.setTxHistory(lodash.compact(txsResult.concat(txsFromLocal)));
storageService.setTxHistory(JSON.stringify(self.txHistory), c.walletId, function() { storageService.setTxHistory(JSON.stringify(self.txHistory), c.walletId, function() {
@ -781,7 +790,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.makeTxHistoryRequest = function(txsToPush, localTx, cb) { self.makeTxHistoryRequest = function(txsToPush, localTx, cb) {
var fc = profileService.focusedClient; var fc = profileService.focusedClient;
var c = fc.credentials; var c = fc.credentials;
var skipLoop = false; var exitLoop = false;
fc.getTxHistory({ fc.getTxHistory({
skip: self.skipHistory, skip: self.skipHistory,
@ -790,28 +799,28 @@ angular.module('copayApp.controllers').controller('indexController', function($r
if (err) return cb(err); if (err) return cb(err);
if (!txsFromBWC[0]) { if (!txsFromBWC[0]) {
skipLoop = true; exitLoop = true;
$log.debug('There is not transactions stored'); $log.debug('There is not transactions stored');
} }
lodash.each(txsFromBWC, function(t) { lodash.each(txsFromBWC, function(t) {
if (!localTx[0]) txsToPush.push(t); if (!localTx[0]) txsToPush.push(t);
else { else {
if (!self.areEqualsTxs(t, localTx[0]) && !skipLoop) { if (!self.stopSync(t, localTx[0]) && !exitLoop) {
txsToPush.push(t); txsToPush.push(t);
} else { } else {
skipLoop = true; exitLoop = true;
} }
} }
}); });
self.skipHistory = self.skipHistory + self.limitHistory; self.skipHistory = self.skipHistory + self.limitHistory;
return cb(null, skipLoop, txsToPush); return cb(null, exitLoop, txsToPush);
}); });
} }
self.updateTxHistory = function(skip) { self.updateTxHistory = function() {
$log.debug('Updating Transaction History'); $log.debug('Updating Transaction History');
self.skipHistory = skip || 0; self.skipHistory = 0;
self.txHistoryError = false; self.txHistoryError = false;
self.updatingTxHistory = true; self.updatingTxHistory = true;
self.txHistoryPaging = false; self.txHistoryPaging = false;