From 9cb47a680a900ea1924e35dcec8107613efc7094 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 6 Feb 2015 17:51:21 -0300 Subject: [PATCH] index implemented for fetch pending --- lib/server.js | 2 +- lib/storage.js | 36 +++++++++++++++++++++--------------- test/integration.js | 3 ++- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/lib/server.js b/lib/server.js index f91144b..f76de07 100644 --- a/lib/server.js +++ b/lib/server.js @@ -566,7 +566,7 @@ CopayServer.prototype.rejectTx = function(opts, cb) { CopayServer.prototype.getPendingTxs = function(opts, cb) { var self = this; - self.storage.fetchTxs(self.walletId, function(err, txps) { + self.storage.fetchPendingTxs(self.walletId, function(err, txps) { if (err) return cb(err); var pending = _.filter(txps, function(txp) { diff --git a/lib/storage.js b/lib/storage.js index 6b197ac..8015d2d 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -20,6 +20,12 @@ var Storage = function(opts) { }; +// WARN: Ts is supposed to have fixed with. Y33 problem :) + +var opKey = function(key) { + return key ? '!' + key : ''; +}; + var KEY = { WALLET: function(id) { return 'wallet::' + id; @@ -27,17 +33,17 @@ var KEY = { COPAYER: function(id) { return 'copayer::' + id; }, - TXP_BY_ID: function(walletId, txProposalId) { - return 'txp::' + walletId + '::' + txProposalId; + TXP: function(walletId, txProposalId) { + return 'txp!' + walletId + opKey(txProposalId); }, - TXP_BY_TS: function(walletId, ts) { - return 'txp-ts::' + walletId + '::' + ts.toFixed(12); + TXP_BY_TS: function(walletId, ts, txProposalId) { + return 'txp-ts!' + walletId + opKey(ts) + opKey(txProposalId); }, - PENDING_TXP_BY_TS: function(walletId, ts) { - return 'pending-txp-ts::' + walletId + '::' + ts.toFixed(12); + PENDING_TXP_BY_TS: function(walletId, ts, txProposalId) { + return 'pending-txp-ts!' + walletId + opKey(ts) + opKey(txProposalId); }, ADDRESS: function(walletId, address) { - return 'address::' + walletId + '::' + address; + return 'address!' + walletId + opKey(address); }, }; @@ -87,7 +93,7 @@ Storage.prototype.fetchCopayerLookup = function(copayerId, cb) { }; Storage.prototype.fetchTx = function(walletId, txProposalId, cb) { - this.db.get(KEY.TXP_BY_ID(walletId, txProposalId), function(err, data) { + this.db.get(KEY.TXP(walletId, txProposalId), function(err, data) { if (err) { if (err.notFound) return cb(); return cb(err); @@ -99,7 +105,7 @@ Storage.prototype.fetchTx = function(walletId, txProposalId, cb) { Storage.prototype.fetchPendingTxs = function(walletId, cb) { var txs = []; - var key = KEY.PENDING_TXP_BY_TS(walletId,''); + var key = KEY.PENDING_TXP_BY_TS(walletId); this.db.createReadStream({ gte: key, lt: key + '~' @@ -118,7 +124,7 @@ Storage.prototype.fetchPendingTxs = function(walletId, cb) { Storage.prototype.fetchTxs = function(walletId, cb) { var txs = []; - var key = KEY.TXP_BY_ID(walletId,''); + var key = KEY.TXP(walletId); this.db.createReadStream({ gte: key, lt: key + '~' @@ -144,23 +150,23 @@ Storage.prototype.storeTx = function(walletId, txp, cb) { async.series([ function(next) { - self.db.put(KEY.TXP_BY_ID(walletId, txp.id), txp, next); + self.db.put(KEY.TXP(walletId, txp.id), txp, next); }, function(next) { - self.db.put(KEY.TXP_BY_TS(walletId, txp.createdOn), txp, next); + self.db.put(KEY.TXP_BY_TS(walletId, txp.createdOn, txp.id), txp, next); }, function(next) { if (txp.isPending()) - self.db.put(KEY.PENDING_TXP_BY_TS(walletId, txp.createdOn), txp, next); + self.db.put(KEY.PENDING_TXP_BY_TS(walletId, txp.createdOn, txp.id), txp, next); else - self.db.del(KEY.PENDING_TXP_BY_TS(walletId, txp.createdOn), next); + self.db.del(KEY.PENDING_TXP_BY_TS(walletId, txp.createdOn, txp.id), next); } ], cb); }; Storage.prototype.fetchAddresses = function(walletId, cb) { var addresses = []; - var key = KEY.ADDRESS(walletId,''); + var key = KEY.ADDRESS(walletId); this.db.createReadStream({ gte: key, lt: key + '~' diff --git a/test/integration.js b/test/integration.js index c4c7a79..cded530 100644 --- a/test/integration.js +++ b/test/integration.js @@ -697,7 +697,8 @@ describe('Copay server', function() { }); }); - it('should create tx', function(done) { + it('should create a tx', function(done) { + helpers.createUtxos(server, wallet, helpers.toSatoshi([100, 200]), function(utxos) { helpers.stubBlockExplorer(server, utxos); var txOpts = {