|
|
@ -2771,70 +2771,75 @@ WalletService.prototype.getTxHistory = function(opts, cb) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
// 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; |
|
|
|
function getNormalizedTxs(addresses, from, to, cb) { |
|
|
|
var txs, fromCache, totalItems; |
|
|
|
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.series([ |
|
|
|
|
|
|
|
async.parallel([ |
|
|
|
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) { |
|
|
|
self.storage.fetchTxs(self.walletId, {}, 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) { |
|
|
|
var totalItems; |
|
|
|
if (!useCache || fromCache) return next(); |
|
|
|
|
|
|
|
async.series([ |
|
|
|
var txsToCache = _.filter(txs, function(i) { |
|
|
|
return i.confirmations >= Defaults.CONFIRMATIONS_TO_START_CACHING; |
|
|
|
}).reverse(); |
|
|
|
|
|
|
|
function(nextSerie) { |
|
|
|
if (!useCache) return nextSerie(); |
|
|
|
if (!txsToCache.length) return next(); |
|
|
|
|
|
|
|
self.storage.getTxHistoryCache(self.walletId, from, to, function(err, res) { |
|
|
|
if (err) return nextSerie(err); |
|
|
|
if (!res || !res[0]) return nextSerie(); |
|
|
|
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 |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
normalizedTxs = res; |
|
|
|
fromCache = true; |
|
|
|
// 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, []); |
|
|
|
|
|
|
|
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 from = opts.skip || 0; |
|
|
|
var to = from + opts.limit; |
|
|
|
|
|
|
|
var txsToCache = _.filter(normalizedTxs, function(i) { |
|
|
|
return i.confirmations >= Defaults.CONFIRMATIONS_TO_START_CACHING; |
|
|
|
}); |
|
|
|
async.parallel([ |
|
|
|
|
|
|
|
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(); |
|
|
|
}); |
|
|
|
function(next) { |
|
|
|
getNormalizedTxs(addresses, from, to, next); |
|
|
|
}, |
|
|
|
function(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); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|