From adb81616aa6f6bdb5c4640a94b8573e78c379685 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Tue, 18 Apr 2023 04:23:41 +0530 Subject: [PATCH] Run cache summary run cache storage for address summary in background when queried result is incomplete --- lib/services/address/index.js | 61 +++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/lib/services/address/index.js b/lib/services/address/index.js index e0087f09..8337410b 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -357,11 +357,11 @@ AddressService.prototype.getAddressSummary = function(address, options, streamer if(tx) { count++; self._aggregateAddressSummaryResult(tx, address, result, options); + result.lastItem = tx.txid(); } if(count >= MAX_TX_QUERY_LIMIT) {//stop quering db when limit reached options.flag_stop = true; - result.lastItem = tx.txid(); result.incomplete = true; } @@ -388,8 +388,12 @@ AddressService.prototype.getAddressSummary = function(address, options, streamer callback(null, result); //store in cache if needed - if(useCache && result.incomplete) - this._storeCache(address, result.lastItem, result); + if(useCache) { + if(result.incomplete) //full summary needs to be calculated in background + self._cacheSummaryInBackground(address, result.lastItem, result); + else if (result.lastItem != lastCachedTx) + self._storeCache(address, result.lastItem, result); + } }); @@ -397,6 +401,57 @@ AddressService.prototype.getAddressSummary = function(address, options, streamer } +AddressService.prototype._cacheSummaryInBackground_runningInstance = new Set(); +AddressService.prototype._cacheSummaryInBackground = function(address, lastTx, result){ + const self = this; + + if(self._cacheSummaryInBackground_runningInstance.has(address)) + return; + + self._cacheSummaryInBackground_runningInstance.add(address); + + const cache = { + balanceSat: result.balanceSat, + totalReceivedSat: result.totalReceivedSat, + totalSentSat: result.totalSentSat, + txApperances: result.txApperances, + unconfirmedBalanceSat: result.unconfirmedBalanceSat, + unconfirmedTxApperances: result.unconfirmedTxApperances + }; + const options = { queryMempool: false, after: lastTx, noTxList: true }; + var lastItem; + + self._streamAddressSummary(address, options, function(err, tx) { + + if(err) + return log.error(err); + + if(tx) { + self._aggregateAddressSummaryResult(tx, address, cache, options); + lastItem = tx.txid(); + } + + }, function(err) { + + if (err) + return log.error(err); + + cache.balanceSat = parseInt(cache.balanceSat.toFixed()); + cache.totalReceivedSat = parseInt(cache.totalReceivedSat.toFixed()); + cache.totalSentSat = parseInt(cache.totalSentSat.toFixed()); + cache.txApperances = parseInt(cache.txApperances.toFixed()); + cache.unconfirmedBalanceSat = parseInt(cache.unconfirmedBalanceSat.toFixed()); + cache.unconfirmedTxApperances = parseInt(cache.unconfirmedTxApperances.toFixed()); + + if(_.isUndefined(lastItem)) + self._storeCache(address, lastItem, cache); + + self._cacheSummaryInBackground_runningInstance.delete(address); //remove from running instance + + }); + +} + AddressService.prototype._storeCache = function(address, lastCacheTx, result, callback) { var key = self._encoding.encodeAddressCacheKey(address); var value = self._encoding.encodeAddressCacheValue(lastCacheTx, result.balanceSat, result.totalReceivedSat, result.totalSentSat, result.txApperances, result.unconfirmedBalanceSat, result.unconfirmedTxApperances)