From 3d7a12f3ab06a9292ff4e475c14cb10e76f40e10 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Tue, 24 May 2016 11:07:30 -0300 Subject: [PATCH] filter txs broadcasted in the last 24hs only --- lib/server.js | 7 ++----- lib/storage.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/lib/server.js b/lib/server.js index c85a061..51361ac 100644 --- a/lib/server.js +++ b/lib/server.js @@ -943,15 +943,12 @@ WalletService.prototype._getUtxosForCurrentWallet = function(addresses, cb) { }, function(next) { var fromTs = Math.floor(Date.now() / 1000) - 24 * 3600; - self.storage.fetchTxs(self.walletId, { + self.storage.fetchBroadcastedTxs(self.walletId, { minTs: fromTs, limit: 100 }, function(err, txs) { if (err) return next(err); - var broadcasted = _.filter(txs, { - status: 'broadcasted' - }); - var spentInputs = _.map(_.flatten(_.pluck(broadcasted, 'inputs')), utxoKey); + var spentInputs = _.map(_.flatten(_.pluck(txs, 'inputs')), utxoKey); _.each(spentInputs, function(input) { if (utxoIndex[input]) { utxoIndex[input].spent = true; diff --git a/lib/storage.js b/lib/storage.js index 5ea50c2..5b571bc 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -296,6 +296,43 @@ Storage.prototype.fetchTxs = function(walletId, opts, cb) { }); }; +/** + * fetchBroadcastedTxs. Times are in UNIX EPOCH (seconds) + * + * @param walletId + * @param opts.minTs + * @param opts.maxTs + * @param opts.limit + */ +Storage.prototype.fetchBroadcastedTxs = function(walletId, opts, cb) { + var self = this; + + opts = opts || {}; + + var tsFilter = {}; + if (_.isNumber(opts.minTs)) tsFilter.$gte = opts.minTs; + if (_.isNumber(opts.maxTs)) tsFilter.$lte = opts.maxTs; + + var filter = { + walletId: walletId, + status: 'broadcasted', + }; + if (!_.isEmpty(tsFilter)) filter.broadcastedOn = tsFilter; + + var mods = {}; + if (_.isNumber(opts.limit)) mods.limit = opts.limit; + + this.db.collection(collections.TXS).find(filter, mods).sort({ + createdOn: -1 + }).toArray(function(err, result) { + if (err) return cb(err); + if (!result) return cb(); + var txs = _.map(result, function(tx) { + return Model.TxProposal.fromObj(tx); + }); + return self._completeTxData(walletId, txs, cb); + }); +}; /** * Retrieves notifications after a specific id or from a given ts (whichever is more recent).