refactor retrieving normalized txs
Conflicts: lib/server.js
This commit is contained in:
parent
b2731e59f1
commit
251c743e16
115
lib/server.js
115
lib/server.js
|
@ -2771,70 +2771,75 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
|
|||
});
|
||||
};
|
||||
|
||||
function getNormalizedTxs(addresses, from, to, cb) {
|
||||
var txs, fromCache, totalItems;
|
||||
var useCache = addresses.length >= Defaults.HISTORY_CACHE_ADDRESS_THRESOLD;
|
||||
|
||||
async.series([
|
||||
|
||||
function(next) {
|
||||
if (!useCache) return next();
|
||||
|
||||
self.storage.getTxHistoryCache(self.walletId, from, to, function(err, res) {
|
||||
if (err) return next(err);
|
||||
if (!res || !res[0]) return next();
|
||||
|
||||
txs = res;
|
||||
fromCache = true;
|
||||
|
||||
return next()
|
||||
});
|
||||
},
|
||||
function(next) {
|
||||
if (txs) return next();
|
||||
|
||||
var addressStrs = _.pluck(addresses, 'address');
|
||||
var network = Bitcore.Address(addressStrs[0]).toObject().network;
|
||||
var bc = self._getBlockchainExplorer(network);
|
||||
bc.getTransactions(addressStrs, from, to, function(err, rawTxs, total) {
|
||||
if (err) return cb(err);
|
||||
txs = self._normalizeTxHistory(rawTxs);
|
||||
totalItems = total;
|
||||
return next();
|
||||
});
|
||||
},
|
||||
function(next) {
|
||||
if (!useCache || fromCache) return next();
|
||||
|
||||
var txsToCache = _.filter(txs, function(i) {
|
||||
return i.confirmations >= Defaults.CONFIRMATIONS_TO_START_CACHING;
|
||||
}).reverse();
|
||||
|
||||
if (!txsToCache.length) return next();
|
||||
|
||||
var fwdIndex = totalItems - to;
|
||||
if (fwdIndex < 0) fwdIndex = 0;
|
||||
self.storage.storeTxHistoryCache(self.walletId, totalItems, fwdIndex, txsToCache, next);
|
||||
}
|
||||
], function(err) {
|
||||
if (err) return cb(err);
|
||||
return cb(null, {
|
||||
items: txs,
|
||||
fromCache: fromCache
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// Get addresses for this wallet
|
||||
self.storage.fetchAddresses(self.walletId, function(err, addresses) {
|
||||
if (err) return cb(err);
|
||||
if (addresses.length == 0) return cb(null, []);
|
||||
|
||||
var addressStrs = _.pluck(addresses, 'address');
|
||||
var networkName = Bitcore.Address(addressStrs[0]).toObject().network;
|
||||
var useCache = addresses.length >= Defaults.HISTORY_CACHE_ADDRESS_THRESOLD;
|
||||
|
||||
var bc = self._getBlockchainExplorer(networkName);
|
||||
var from = opts.skip || 0;
|
||||
var to = from + opts.limit;
|
||||
var normalizedTxs, fromCache;
|
||||
|
||||
async.parallel([
|
||||
|
||||
function(next) {
|
||||
self.storage.fetchTxs(self.walletId, {}, next);
|
||||
getNormalizedTxs(addresses, from, to, next);
|
||||
},
|
||||
function(next) {
|
||||
var totalItems;
|
||||
|
||||
async.series([
|
||||
|
||||
function(nextSerie) {
|
||||
if (!useCache) return nextSerie();
|
||||
|
||||
self.storage.getTxHistoryCache(self.walletId, from, to, function(err, res) {
|
||||
if (err) return nextSerie(err);
|
||||
if (!res || !res[0]) return nextSerie();
|
||||
|
||||
normalizedTxs = res;
|
||||
fromCache = true;
|
||||
|
||||
return nextSerie()
|
||||
});
|
||||
},
|
||||
function(nextSerie) {
|
||||
if (normalizedTxs) return nextSerie();
|
||||
bc.getTransactions(addressStrs, from, to, function(err, rawTxs, total) {
|
||||
if (err) return cb(err);
|
||||
normalizedTxs = self._normalizeTxHistory(rawTxs);
|
||||
totalItems = total;
|
||||
return nextSerie();
|
||||
});
|
||||
},
|
||||
function(nextSerie) {
|
||||
if (!useCache || fromCache) return nextSerie();
|
||||
|
||||
var txsToCache = _.filter(normalizedTxs, function(i) {
|
||||
return i.confirmations >= Defaults.CONFIRMATIONS_TO_START_CACHING;
|
||||
});
|
||||
|
||||
if (!txsToCache.length)
|
||||
return nextSerie(err);
|
||||
self.storage.storeTxHistoryCache(self.walletId, totalItems, to, txsToCache, function(err) {
|
||||
nextSerie(err);
|
||||
})
|
||||
}
|
||||
],
|
||||
function(err) {
|
||||
if (err) return next(err);
|
||||
return next();
|
||||
});
|
||||
self.storage.fetchTxs(self.walletId, {}, next);
|
||||
},
|
||||
function(next) {
|
||||
self.storage.fetchTxNotes(self.walletId, {}, next);
|
||||
|
@ -2842,14 +2847,12 @@ WalletService.prototype.getTxHistory = function(opts, cb) {
|
|||
], function(err, res) {
|
||||
if (err) return cb(err);
|
||||
|
||||
var proposals = res[0];
|
||||
var notes = res[2];
|
||||
var finalTxs = decorate(normalizedTxs, addresses, proposals, notes);
|
||||
var finalTxs = decorate(res[0].items, addresses, res[1], res[2]);
|
||||
|
||||
if (fromCache)
|
||||
if (res[0].fromCache)
|
||||
log.debug("History from cache for:", self.walletId, from, to);
|
||||
|
||||
return cb(null, finalTxs, !!fromCache);
|
||||
return cb(null, finalTxs, !!res[0].fromCache);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue