index implemented for fetch pending

This commit is contained in:
Matias Alejo Garcia 2015-02-06 17:51:21 -03:00
parent 0a7edd9523
commit 9cb47a680a
3 changed files with 24 additions and 17 deletions

View File

@ -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) {

View File

@ -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 + '~'

View File

@ -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 = {