txp indexes at storage
This commit is contained in:
parent
0ee2abf841
commit
196610f2eb
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
|
|
@ -14,7 +14,9 @@ var TxProposal = require('./model/txproposal');
|
|||
|
||||
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'
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
@ -32,15 +34,24 @@ Storage.prototype.storeWallet = function (wallet, cb) {
|
|||
this.db.put('wallet-' + wallet.id, wallet, cb);
|
||||
};
|
||||
|
||||
<< << << < HEAD
|
||||
Storage.prototype.storeWalletAndUpdateCopayersLookup = function(wallet, cb) {
|
||||
var ops = [];
|
||||
ops.push({ type: 'put', key: 'wallet-' + wallet.id, value: wallet });
|
||||
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);
|
||||
};
|
||||
|
@ -68,7 +79,10 @@ Storage.prototype.fetchTx = function (walletId, txProposalId, cb) {
|
|||
Storage.prototype.fetchTxs = function(walletId, cb) {
|
||||
var txs = [];
|
||||
var key = 'wallet-' + walletId + '-txp-';
|
||||
this.db.createReadStream({ gte: key, lt: key + '~' })
|
||||
this.db.createReadStream({
|
||||
gte: key,
|
||||
lt: key + '~'
|
||||
})
|
||||
.on('data', function(data) {
|
||||
txs.push(TxProposal.fromObj(data.value));
|
||||
})
|
||||
|
@ -82,13 +96,32 @@ Storage.prototype.fetchTxs = function (walletId, cb) {
|
|||
};
|
||||
|
||||
Storage.prototype.storeTx = function(walletId, txp, cb) {
|
||||
this.db.put('wallet-' + walletId + '-txp-' + txp.id, 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) {
|
||||
var addresses = [];
|
||||
var key = 'wallet-' + walletId + '-address-';
|
||||
this.db.createReadStream({ gte: key, lt: key + '~' })
|
||||
this.db.createReadStream({
|
||||
gte: key,
|
||||
lt: key + '~'
|
||||
})
|
||||
.on('data', function(data) {
|
||||
addresses.push(Address.fromObj(data.value));
|
||||
})
|
||||
|
@ -113,7 +146,9 @@ Storage.prototype.removeAddress = function (walletId, address, 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;
|
||||
|
|
Loading…
Reference in New Issue