Browse Source

Merge pull request #17 from matiu/feat/history

Feat/history
activeAddress
Ivan Socolsky 10 years ago
parent
commit
cf56d627d4
  1. 4
      lib/model/txproposal.js
  2. 24
      lib/server.js
  3. 180
      lib/storage.js
  4. 130
      test/integration.js

4
lib/model/txproposal.js

@ -146,6 +146,10 @@ TxProposal.prototype.reject = function(copayerId) {
this.addAction(copayerId, 'reject'); this.addAction(copayerId, 'reject');
}; };
TxProposal.prototype.isPending = function() {
return this.status === 'pending';
};
TxProposal.prototype.isAccepted = function() { TxProposal.prototype.isAccepted = function() {
var votes = _.countBy(_.values(this.actions), 'type'); var votes = _.countBy(_.values(this.actions), 'type');
return votes['accept'] >= this.requiredSignatures; return votes['accept'] >= this.requiredSignatures;

24
lib/server.js

@ -400,7 +400,7 @@ CopayServer.prototype._selectUtxos = function(txp, utxos) {
CopayServer.prototype.createTx = function(opts, cb) { CopayServer.prototype.createTx = function(opts, cb) {
var self = this; var self = this;
Utils.checkRequired(opts, ['toAddress', 'amount', 'message']); Utils.checkRequired(opts, ['toAddress', 'amount']);
// TODO? // TODO?
@ -566,15 +566,29 @@ CopayServer.prototype.rejectTx = function(opts, cb) {
CopayServer.prototype.getPendingTxs = function(opts, cb) { CopayServer.prototype.getPendingTxs = function(opts, cb) {
var self = this; var self = this;
self.storage.fetchTxs(self.walletId, function(err, txps) { self.storage.fetchPendingTxs(self.walletId, function(err, txps) {
if (err) return cb(err); if (err) return cb(err);
var pending = _.filter(txps, { return cb(null, txps);
status: 'pending'
}); });
return cb(null, pending); };
/**
* Retrieves pending transaction proposals in the range (maxTs-minTs)
* @param {Object} opts.minTs (defaults to 0)
* @param {Object} opts.maxTs (defaults to now)
* @param {Object} opts.limit
* @returns {TxProposal[]} Transaction proposal.
*/
CopayServer.prototype.getTxs = function(opts, cb) {
var self = this;
self.storage.fetchTxs(self.walletId, opts, function(err, txps) {
if (err) return cb(err);
return cb(null, txps);
}); });
}; };
module.exports = CopayServer; module.exports = CopayServer;

180
lib/storage.js

@ -3,7 +3,6 @@
var _ = require('lodash'); var _ = require('lodash');
var levelup = require('levelup'); var levelup = require('levelup');
var $ = require('preconditions').singleton(); var $ = require('preconditions').singleton();
var async = require('async');
var log = require('npmlog'); var log = require('npmlog');
log.debug = log.verbose; log.debug = log.verbose;
@ -12,14 +11,48 @@ var Copayer = require('./model/copayer');
var Address = require('./model/address'); var Address = require('./model/address');
var TxProposal = require('./model/txproposal'); var TxProposal = require('./model/txproposal');
var Storage = function (opts) { var Storage = function(opts) {
opts = 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'
});
};
var opKey = function(key) {
return key ? '!' + key : '';
};
var MAX_TS = '999999999999';
var opKeyTs = function(key) {
return key ? '!' + ('000000000000'+key).slice(-12) : '';
}; };
Storage.prototype.fetchWallet = function (id, cb) {
this.db.get('wallet-' + id, function (err, data) { var KEY = {
WALLET: function(id) {
return 'wallet!' + id;
},
COPAYER: function(id) {
return 'copayer!' + id;
},
TXP: function(walletId, txProposalId) {
return 'txp!' + walletId + opKey(txProposalId);
},
TXP_BY_TS: function(walletId, ts, txProposalId) {
return 'txp-ts!' + walletId + opKeyTs(ts) + opKey(txProposalId);
},
PENDING_TXP_BY_TS: function(walletId, ts, txProposalId) {
return 'pending-txp-ts!' + walletId + opKey(ts) + opKey(txProposalId);
},
ADDRESS: function(walletId, address) {
return 'address!' + walletId + opKey(address);
},
};
Storage.prototype.fetchWallet = function(id, cb) {
this.db.get(KEY.WALLET(id), function(err, data) {
if (err) { if (err) {
if (err.notFound) return cb(); if (err.notFound) return cb();
return cb(err); return cb(err);
@ -28,25 +61,33 @@ 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); this.db.put(KEY.WALLET(wallet.id), wallet, cb);
}; };
Storage.prototype.storeWalletAndUpdateCopayersLookup = function (wallet, cb) { Storage.prototype.storeWalletAndUpdateCopayersLookup = function(wallet, cb) {
var ops = []; var ops = [];
ops.push({ type: 'put', key: 'wallet-' + wallet.id, value: wallet }); ops.push({
_.each(wallet.copayers, function (copayer) { type: 'put',
key: KEY.WALLET(wallet.id),
value: wallet
});
_.each(wallet.copayers, function(copayer) {
var value = { var value = {
walletId: wallet.id, walletId: wallet.id,
signingPubKey: copayer.signingPubKey, signingPubKey: copayer.signingPubKey,
}; };
ops.push({ type: 'put', key: 'copayer-' + copayer.id, value: value }); ops.push({
type: 'put',
key: KEY.COPAYER(copayer.id),
value: value
});
}); });
this.db.batch(ops, cb); this.db.batch(ops, cb);
}; };
Storage.prototype.fetchCopayerLookup = function (copayerId, cb) { Storage.prototype.fetchCopayerLookup = function(copayerId, cb) {
this.db.get('copayer-' + copayerId, function (err, data) { this.db.get(KEY.COPAYER(copayerId), function(err, data) {
if (err) { if (err) {
if (err.notFound) return cb(); if (err.notFound) return cb();
return cb(err); return cb(err);
@ -55,8 +96,8 @@ Storage.prototype.fetchCopayerLookup = function (copayerId, cb) {
}); });
}; };
Storage.prototype.fetchTx = function (walletId, txProposalId, cb) { Storage.prototype.fetchTx = function(walletId, txProposalId, cb) {
this.db.get('wallet-' + walletId + '-txp-' + txProposalId, function (err, data) { this.db.get(KEY.TXP(walletId, txProposalId), function(err, data) {
if (err) { if (err) {
if (err.notFound) return cb(); if (err.notFound) return cb();
return cb(err); return cb(err);
@ -65,55 +106,124 @@ Storage.prototype.fetchTx = function (walletId, txProposalId, cb) {
}); });
}; };
Storage.prototype.fetchTxs = function (walletId, cb) { Storage.prototype.fetchPendingTxs = function(walletId, cb) {
var txs = [];
var key = KEY.PENDING_TXP_BY_TS(walletId);
this.db.createReadStream({
gte: key,
lt: key + '~'
})
.on('data', function(data) {
txs.push(TxProposal.fromObj(data.value));
})
.on('error', function(err) {
if (err.notFound) return cb();
return cb(err);
})
.on('end', function() {
return cb(null, txs);
});
};
/**
* fetchTxs
*
* @param walletId
* @param opts.minTs
* @param opts.maxTs
* @param opts.limit
*/
Storage.prototype.fetchTxs = function(walletId, opts, cb) {
var txs = []; var txs = [];
var key = 'wallet-' + walletId + '-txp-'; opts = opts || {};
this.db.createReadStream({ gte: key, lt: key + '~' }) opts.limit = opts.limit || -1;
.on('data', function (data) { opts.minTs = opts.minTs || 0;
opts.maxTs = opts.maxTs || MAX_TS;
var key = KEY.TXP_BY_TS(walletId, opts.minTs);
var endkey = KEY.TXP_BY_TS(walletId, opts.maxTs);
this.db.createReadStream({
gt: key,
lt: endkey + '~',
reverse: true,
limit: opts.limit,
})
.on('data', function(data) {
txs.push(TxProposal.fromObj(data.value)); txs.push(TxProposal.fromObj(data.value));
}) })
.on('error', function (err) { .on('error', function(err) {
if (err.notFound) return cb(); if (err.notFound) return cb();
return cb(err); return cb(err);
}) })
.on('end', function () { .on('end', function() {
return cb(null, txs); return cb(null, txs);
}); });
}; };
Storage.prototype.storeTx = function (walletId, txp, cb) { // TODO should we store only txp.id on keys for indexing
this.db.put('wallet-' + walletId + '-txp-' + txp.id, txp, cb); // or the whole txp? For now, the entire record makes sense
// (faster + easier to access)
Storage.prototype.storeTx = function(walletId, txp, cb) {
var ops = [{
type: 'put',
key: KEY.TXP(walletId, txp.id),
value: txp,
}, {
type: 'put',
key: KEY.TXP_BY_TS(walletId, txp.createdOn, txp.id),
value: txp,
}];
if (txp.isPending()) {
ops.push({
type: 'put',
key: KEY.PENDING_TXP_BY_TS(walletId, txp.createdOn, txp.id),
value: txp,
});
} else {
ops.push({
type: 'del',
key: KEY.PENDING_TXP_BY_TS(walletId, txp.createdOn, txp.id),
});
}
this.db.batch(ops, cb);
}; };
Storage.prototype.fetchAddresses = function (walletId, cb) { Storage.prototype.fetchAddresses = function(walletId, cb) {
var addresses = []; var addresses = [];
var key = 'wallet-' + walletId + '-address-'; var key = KEY.ADDRESS(walletId);
this.db.createReadStream({ gte: key, lt: key + '~' }) this.db.createReadStream({
.on('data', function (data) { gte: key,
lt: key + '~'
})
.on('data', function(data) {
addresses.push(Address.fromObj(data.value)); addresses.push(Address.fromObj(data.value));
}) })
.on('error', function (err) { .on('error', function(err) {
if (err.notFound) return cb(); if (err.notFound) return cb();
return cb(err); return cb(err);
}) })
.on('end', function () { .on('end', function() {
return cb(null, addresses); 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); this.db.put(KEY.ADDRESS(walletId, 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); this.db.del(KEY.ADDRESS(walletId, address.address), cb);
}; };
Storage.prototype._dump = function (cb) { Storage.prototype._dump = function(cb) {
this.db.readStream() this.db.readStream()
.on('data', console.log) .on('data', console.log)
.on('end', function () { if (cb) return cb(); }); .on('end', function() {
if (cb) return cb();
});
}; };
module.exports = Storage; module.exports = Storage;

130
test/integration.js

@ -60,14 +60,14 @@ var aTextSignature = '3045022100addd20e5413865d65d561ad2979f2289a40d52594b1f8048
var helpers = {}; var helpers = {};
helpers.getAuthServer = function (copayerId, cb) { helpers.getAuthServer = function(copayerId, cb) {
var signatureStub = sinon.stub(CopayServer.prototype, '_verifySignature'); var signatureStub = sinon.stub(CopayServer.prototype, '_verifySignature');
signatureStub.returns(true); signatureStub.returns(true);
CopayServer.getInstanceWithAuth({ CopayServer.getInstanceWithAuth({
copayerId: copayerId, copayerId: copayerId,
message: 'dummy', message: 'dummy',
signature: 'dummy', signature: 'dummy',
}, function (err, server) { }, function(err, server) {
signatureStub.restore(); signatureStub.restore();
return cb(server); return cb(server);
}); });
@ -99,11 +99,11 @@ helpers.createAndJoinWallet = function(id, m, n, cb) {
server.joinWallet(copayerOpts, function(err) { server.joinWallet(copayerOpts, function(err) {
return cb(err); return cb(err);
}); });
}, function (err) { }, function(err) {
if (err) return new Error('Could not generate wallet'); if (err) return new Error('Could not generate wallet');
helpers.getAuthServer('1', function (s) { helpers.getAuthServer('1', function(s) {
s.getWallet({}, function (err, w) { s.getWallet({}, function(err, w) {
cb(s, w); cb(s, w);
}); });
}); });
@ -213,21 +213,19 @@ describe('Copay server', function() {
storage = new Storage({ storage = new Storage({
db: db db: db
}); });
CopayServer.initialize({ storage: storage }); CopayServer.initialize({
storage: storage
});
}); });
describe.skip('#getInstanceWithAuth', function() { describe.skip('#getInstanceWithAuth', function() {
beforeEach(function() { beforeEach(function() {});
});
it('should get server instance for existing copayer', function(done) { it('should get server instance for existing copayer', function(done) {});
});
it('should fail when requesting for non-existent copayer', function(done) { it('should fail when requesting for non-existent copayer', function(done) {});
});
it('should fail when message signature cannot be verified', function(done) { it('should fail when message signature cannot be verified', function(done) {});
});
}); });
describe('#createWallet', function() { describe('#createWallet', function() {
@ -342,7 +340,7 @@ describe('Copay server', function() {
}; };
server.joinWallet(copayerOpts, function(err) { server.joinWallet(copayerOpts, function(err) {
should.not.exist(err); should.not.exist(err);
helpers.getAuthServer('999', function (server) { helpers.getAuthServer('999', function(server) {
server.getWallet({}, function(err, wallet) { server.getWallet({}, function(err, wallet) {
wallet.id.should.equal('123'); wallet.id.should.equal('123');
wallet.copayers.length.should.equal(1); wallet.copayers.length.should.equal(1);
@ -406,7 +404,7 @@ describe('Copay server', function() {
}; };
server.joinWallet(copayer1Opts, function(err) { server.joinWallet(copayer1Opts, function(err) {
should.not.exist(err); should.not.exist(err);
helpers.getAuthServer('111', function (server) { helpers.getAuthServer('111', function(server) {
server.getWallet({}, function(err, wallet) { server.getWallet({}, function(err, wallet) {
wallet.status.should.equal('complete'); wallet.status.should.equal('complete');
server.joinWallet(copayer2Opts, function(err) { server.joinWallet(copayer2Opts, function(err) {
@ -697,7 +695,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.createUtxos(server, wallet, helpers.toSatoshi([100, 200]), function(utxos) {
helpers.stubBlockExplorer(server, utxos); helpers.stubBlockExplorer(server, utxos);
var txOpts = { var txOpts = {
@ -1002,7 +1001,9 @@ describe('Copay server', function() {
server.getPendingTxs({}, function(err, txps) { server.getPendingTxs({}, function(err, txps) {
should.not.exist(err); should.not.exist(err);
txps.length.should.equal(0); txps.length.should.equal(0);
server.getTx({ id: txpid }, function (err, txp) { server.getTx({
id: txpid
}, function(err, txp) {
txp.status.should.equal('accepted'); txp.status.should.equal('accepted');
should.not.exist(txp.txid); should.not.exist(txp.txid);
done(); done();
@ -1027,4 +1028,97 @@ describe('Copay server', function() {
it.skip('proposal creator should be able to delete proposal if there are no other signatures', function (done) { it.skip('proposal creator should be able to delete proposal if there are no other signatures', function (done) {
}); });
}); });
describe('#getTxs', function() {
var server, wallet, clock;
beforeEach(function(done) {
console.log('\tCreating TXS...');
clock = sinon.useFakeTimers();
helpers.createAndJoinWallet('123', 1, 1, function(s, w) {
server = s;
wallet = w;
server.createAddress({
isChange: false,
}, function(err, address) {
helpers.createUtxos(server, wallet, helpers.toSatoshi(_.range(10)), function(utxos) {
helpers.stubBlockExplorer(server, utxos);
var txOpts = {
toAddress: '18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7',
amount: helpers.toSatoshi(0.1),
};
async.eachSeries(_.range(10), function(i, next) {
clock.tick(10000);
server.createTx(txOpts, function(err, tx) {
next();
});
},
done
);
});
});
});
});
afterEach(function() {
clock.restore();
});
it('should pull 4 txs, down to to time 60', function(done) {
server.getTxs({
minTs: 60,
limit: 8
}, function(err, txps) {
should.not.exist(err);
var times = _.pluck(txps,'createdOn');
times.should.deep.equal([90,80,70,60]);
done();
});
});
it('should pull the first 5 txs', function(done) {
server.getTxs({
maxTs: 50,
limit: 5
}, function(err, txps) {
should.not.exist(err);
var times = _.pluck(txps,'createdOn');
times.should.deep.equal([50,40,30,20,10]);
done();
});
});
it('should pull the last 4 txs', function(done) {
server.getTxs({
limit: 4
}, function(err, txps) {
should.not.exist(err);
var times = _.pluck(txps,'createdOn');
times.should.deep.equal([90,80,70,60]);
done();
});
});
it('should pull all txs', function(done) {
server.getTxs({
}, function(err, txps) {
should.not.exist(err);
var times = _.pluck(txps,'createdOn');
times.should.deep.equal([90,80,70,60,50,40,30,20,10]);
done();
});
});
it('should txs from times 50 to 70', function(done) {
server.getTxs({
minTs: 50,
maxTs: 70,
}, function(err, txps) {
should.not.exist(err);
var times = _.pluck(txps,'createdOn');
times.should.deep.equal([70,60,50]);
done();
});
});
});
}); });

Loading…
Cancel
Save