From e1ab87c17370fe71a7b0c5331e0f05c6a898f6ae Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sat, 7 Feb 2015 14:15:04 -0300 Subject: [PATCH] txproposal id = time + uuid --- lib/model/txproposal.js | 2 +- lib/storage.js | 27 ++++++++++----------------- test/integration.js | 11 +++++++---- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/lib/model/txproposal.js b/lib/model/txproposal.js index 54936ed..3a9d277 100644 --- a/lib/model/txproposal.js +++ b/lib/model/txproposal.js @@ -14,7 +14,7 @@ function TxProposal(opts) { this.version = VERSION; this.createdOn = Math.floor(Date.now() / 1000); - this.id = Uuid.v4(); + this.id = ('000000000000' + this.createdOn).slice(-12) + Uuid.v4(); this.creatorId = opts.creatorId; this.toAddress = opts.toAddress; this.amount = opts.amount; diff --git a/lib/storage.js b/lib/storage.js index 47d16eb..275d4a6 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -39,11 +39,8 @@ var KEY = { 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); + PENDING_TXP: function(walletId, txProposalId) { + return 'pending-txp-ts!' + walletId + opKey(txProposalId); }, ADDRESS: function(walletId, address) { return 'address!' + walletId + opKey(address); @@ -107,7 +104,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(walletId); this.db.createReadStream({ gte: key, lt: key + '~' @@ -135,12 +132,12 @@ Storage.prototype.fetchPendingTxs = function(walletId, cb) { Storage.prototype.fetchTxs = function(walletId, opts, cb) { var txs = []; opts = opts || {}; - opts.limit = opts.limit || -1; - opts.minTs = opts.minTs || 0; - opts.maxTs = opts.maxTs || MAX_TS; + opts.limit = _.isNumber(opts.limit) ? parseInt(opts.limit) : -1; + opts.minTs = _.isNumber(opts.minTs) ? ('000000000000' + parseInt(opts.minTs)).slice(-12) : 0; + opts.maxTs = _.isNumber(opts.maxTs) ? ('000000000000' + parseInt(opts.maxTs)).slice(-12) : MAX_TS; - var key = KEY.TXP_BY_TS(walletId, opts.minTs); - var endkey = KEY.TXP_BY_TS(walletId, opts.maxTs); + var key = KEY.TXP(walletId, opts.minTs); + var endkey = KEY.TXP(walletId, opts.maxTs); this.db.createReadStream({ gt: key, @@ -168,22 +165,18 @@ Storage.prototype.storeTx = function(walletId, txp, cb) { 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), + key: KEY.PENDING_TXP(walletId, txp.id), value: txp, }); } else { ops.push({ type: 'del', - key: KEY.PENDING_TXP_BY_TS(walletId, txp.createdOn, txp.id), + key: KEY.PENDING_TXP(walletId, txp.id), }); } this.db.batch(ops, cb); diff --git a/test/integration.js b/test/integration.js index 1fa8ff1..479fa93 100644 --- a/test/integration.js +++ b/test/integration.js @@ -710,8 +710,6 @@ describe('Copay server', function() { it.skip('should fail to create tx when wallet is not complete', function(done) {}); - it.skip('should fail to create tx when wallet is not complete', function(done) {}); - it('should fail to create tx when insufficient funds', function(done) { helpers.createUtxos(server, wallet, helpers.toSatoshi([100]), function(utxos) { helpers.stubBlockExplorer(server, utxos); @@ -1009,6 +1007,10 @@ describe('Copay server', function() { var server, wallet, clock; beforeEach(function(done) { + if (server) + return done(); + + this.timeout(5000); console.log('\tCreating TXS...'); clock = sinon.useFakeTimers(); helpers.createAndJoinWallet(1, 1, function(s, w) { @@ -1028,8 +1030,9 @@ describe('Copay server', function() { server.createTx(txOpts, function(err, tx) { next(); }); - }, - done + }, function(err) { + return done(err); + } ); }); });