From 196610f2ebc89546bb8707180d5b5a052f13548f Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 6 Feb 2015 17:04:10 -0300 Subject: [PATCH] txp indexes at storage --- lib/model/txproposal.js | 4 ++ lib/server.js | 4 +- lib/storage.js | 97 ++++++++++++++++++++++++++++------------- 3 files changed, 72 insertions(+), 33 deletions(-) diff --git a/lib/model/txproposal.js b/lib/model/txproposal.js index 6f13900..2f157b7 100644 --- a/lib/model/txproposal.js +++ b/lib/model/txproposal.js @@ -146,6 +146,10 @@ TxProposal.prototype.reject = function(copayerId) { this.addAction(copayerId, 'reject'); }; +TxProposal.prototype.isPending = function() { + return this.status === 'pending'; +}; + TxProposal.prototype.isAccepted = function() { var votes = _.countBy(_.values(this.actions), 'type'); return votes['accept'] >= this.requiredSignatures; diff --git a/lib/server.js b/lib/server.js index b7eb952..f91144b 100644 --- a/lib/server.js +++ b/lib/server.js @@ -569,8 +569,8 @@ CopayServer.prototype.getPendingTxs = function(opts, cb) { self.storage.fetchTxs(self.walletId, function(err, txps) { if (err) return cb(err); - var pending = _.filter(txps, { - status: 'pending' + var pending = _.filter(txps, function(txp) { + return txp.isPending(); }); return cb(null, pending); }); diff --git a/lib/storage.js b/lib/storage.js index 8c1610d..87411a2 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -12,14 +12,16 @@ var Copayer = require('./model/copayer'); var Address = require('./model/address'); var TxProposal = require('./model/txproposal'); -var Storage = function (opts) { +var Storage = function(opts) { opts = opts || {}; - this.db = opts.db || levelup(opts.dbPath || './db/copay.db', { valueEncoding: 'json' }); + this.db = opts.db || levelup(opts.dbPath || './db/copay.db', { + valueEncoding: 'json' + }); }; -Storage.prototype.fetchWallet = function (id, cb) { - this.db.get('wallet-' + id, function (err, data) { +Storage.prototype.fetchWallet = function(id, cb) { + this.db.get('wallet-' + id, function(err, data) { if (err) { if (err.notFound) return cb(); return cb(err); @@ -28,25 +30,34 @@ Storage.prototype.fetchWallet = function (id, cb) { }); }; -Storage.prototype.storeWallet = function (wallet, cb) { +Storage.prototype.storeWallet = function(wallet, cb) { this.db.put('wallet-' + wallet.id, wallet, cb); }; -Storage.prototype.storeWalletAndUpdateCopayersLookup = function (wallet, cb) { +<< << << < HEAD +Storage.prototype.storeWalletAndUpdateCopayersLookup = function(wallet, cb) { var ops = []; - ops.push({ type: 'put', key: 'wallet-' + wallet.id, value: wallet }); - _.each(wallet.copayers, function (copayer) { - var value = { - walletId: wallet.id, + ops.push({ + type: 'put', + key: 'wallet-' + wallet.id, + value: wallet + }); + _.each(wallet.copayers, function(copayer) { + var value = { + walletId: wallet.id, signingPubKey: copayer.signingPubKey, }; - ops.push({ type: 'put', key: 'copayer-' + copayer.id, value: value }); + ops.push({ + type: 'put', + key: 'copayer-' + copayer.id, + value: value + }); }); this.db.batch(ops, cb); }; -Storage.prototype.fetchCopayerLookup = function (copayerId, cb) { - this.db.get('copayer-' + copayerId, function (err, data) { +Storage.prototype.fetchCopayerLookup = function(copayerId, cb) { + this.db.get('copayer-' + copayerId, function(err, data) { if (err) { if (err.notFound) return cb(); return cb(err); @@ -55,8 +66,8 @@ Storage.prototype.fetchCopayerLookup = function (copayerId, cb) { }); }; -Storage.prototype.fetchTx = function (walletId, txProposalId, cb) { - this.db.get('wallet-' + walletId + '-txp-' + txProposalId, function (err, data) { +Storage.prototype.fetchTx = function(walletId, txProposalId, cb) { + this.db.get('wallet-' + walletId + '-txp-' + txProposalId, function(err, data) { if (err) { if (err.notFound) return cb(); return cb(err); @@ -65,55 +76,79 @@ Storage.prototype.fetchTx = function (walletId, txProposalId, cb) { }); }; -Storage.prototype.fetchTxs = function (walletId, cb) { +Storage.prototype.fetchTxs = function(walletId, cb) { var txs = []; var key = 'wallet-' + walletId + '-txp-'; - this.db.createReadStream({ gte: key, lt: key + '~' }) - .on('data', function (data) { + this.db.createReadStream({ + gte: key, + lt: key + '~' + }) + .on('data', function(data) { txs.push(TxProposal.fromObj(data.value)); }) - .on('error', function (err) { + .on('error', function(err) { if (err.notFound) return cb(); return cb(err); }) - .on('end', function () { + .on('end', function() { return cb(null, txs); }); }; -Storage.prototype.storeTx = function (walletId, txp, cb) { - this.db.put('wallet-' + walletId + '-txp-' + txp.id, txp, cb); +Storage.prototype.storeTx = function(walletId, txp, cb) { + var self = this; + + async.series([ + + function(next) { + self.db.put('wallet-' + walletId + '-txp-' + txp.id, txp, next); + }, + function(next) { + self.db.put('wallet-' + walletId + '-txp-ts-' + txp.createdOn, txp.id, next); + }, + function(next) { + if (txp.isPending()) + self.db.put('wallet-' + walletId + '-txp-p-' + txp.createdOn, txp.id, next); + else + self.db.del('wallet-' + walletId + '-txp-p-' + txp.createdOn, next); + } + ], cb); }; -Storage.prototype.fetchAddresses = function (walletId, cb) { +Storage.prototype.fetchAddresses = function(walletId, cb) { var addresses = []; var key = 'wallet-' + walletId + '-address-'; - this.db.createReadStream({ gte: key, lt: key + '~' }) - .on('data', function (data) { + this.db.createReadStream({ + gte: key, + lt: key + '~' + }) + .on('data', function(data) { addresses.push(Address.fromObj(data.value)); }) - .on('error', function (err) { + .on('error', function(err) { if (err.notFound) return cb(); return cb(err); }) - .on('end', function () { + .on('end', function() { return cb(null, addresses); }); }; -Storage.prototype.storeAddress = function (walletId, address, cb) { +Storage.prototype.storeAddress = function(walletId, address, cb) { this.db.put('wallet-' + walletId + '-address-' + address.address, address, cb); }; -Storage.prototype.removeAddress = function (walletId, address, cb) { +Storage.prototype.removeAddress = function(walletId, address, cb) { this.db.del('wallet-' + walletId + '-address-' + address.address, cb); }; -Storage.prototype._dump = function (cb) { +Storage.prototype._dump = function(cb) { this.db.readStream() .on('data', console.log) - .on('end', function () { if (cb) return cb(); }); + .on('end', function() { + if (cb) return cb(); + }); }; module.exports = Storage;