From 635b2d8343c59b5be732017e55afc339b39b874f Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Wed, 28 Jan 2015 10:36:49 -0300 Subject: [PATCH] Improve locking --- lib/model/address.js | 2 ++ lib/model/copayer.js | 2 ++ lib/model/txproposal.js | 2 ++ lib/model/wallet.js | 2 ++ lib/server.js | 35 +++++++++++++++++++++-------------- lib/storage.js | 4 ++++ 6 files changed, 33 insertions(+), 14 deletions(-) diff --git a/lib/model/address.js b/lib/model/address.js index 6521c87..4b51137 100644 --- a/lib/model/address.js +++ b/lib/model/address.js @@ -3,6 +3,7 @@ function Address(opts) { opts = opts || {}; + this.createdOn = Math.floor(Date.now() / 1000); this.address = opts.address; this.path = opts.path; }; @@ -10,6 +11,7 @@ function Address(opts) { Address.fromObj = function (obj) { var x = new Address(); + x.createdOn = obj.createdOn; x.address = obj.address; x.path = obj.path; return x; diff --git a/lib/model/copayer.js b/lib/model/copayer.js index 1c5c051..fb5d504 100644 --- a/lib/model/copayer.js +++ b/lib/model/copayer.js @@ -5,6 +5,7 @@ var _ = require('lodash'); function Copayer(opts) { opts = opts || {}; + this.createdOn = Math.floor(Date.now() / 1000); this.id = opts.id; this.name = opts.name; this.xPubKey = opts.xPubKey; @@ -14,6 +15,7 @@ function Copayer(opts) { Copayer.fromObj = function (obj) { var x = new Copayer(); + x.createdOn = obj.createdOn; x.id = obj.id; x.name = obj.name; x.xPubKey = obj.xPubKey; diff --git a/lib/model/txproposal.js b/lib/model/txproposal.js index deb81c3..3350d60 100644 --- a/lib/model/txproposal.js +++ b/lib/model/txproposal.js @@ -3,6 +3,7 @@ function TxProposal(opts) { opts = opts || {}; + this.createdOn = Math.floor(Date.now() / 1000); this.creatorId = opts.creatorId; this.toAddress = opts.toAddress; this.amount = opts.amount; @@ -13,6 +14,7 @@ function TxProposal(opts) { TxProposal.fromObj = function (obj) { var x = new TxProposal(); + x.createdOn = obj.createdOn; x.creatorId = obj.creatorId; x.toAddress = obj.toAddress; x.amount = obj.amount; diff --git a/lib/model/wallet.js b/lib/model/wallet.js index 5dee7ee..b629309 100644 --- a/lib/model/wallet.js +++ b/lib/model/wallet.js @@ -5,6 +5,7 @@ var _ = require('lodash'); function Wallet(opts) { opts = opts || {}; + this.createdOn = Math.floor(Date.now() / 1000); this.id = opts.id; this.name = opts.name; this.m = opts.m; @@ -17,6 +18,7 @@ function Wallet(opts) { Wallet.fromObj = function (obj) { var x = new Wallet(); + x.createdOn = obj.createdOn; x.id = obj.id; x.name = obj.name; x.m = obj.m; diff --git a/lib/server.js b/lib/server.js index cd12e92..2c9079d 100644 --- a/lib/server.js +++ b/lib/server.js @@ -80,6 +80,19 @@ CopayServer.prototype.createWallet = function (opts, cb) { }); }; + +CopayServer.prototype.runLocked = function (walletId, cb, task) { + var self = this; + + Lock.get(walletId, function (lock) { + var _cb = function () { + cb.apply(null, arguments); + lock.free(); + }; + task(_cb); + }); +}; + /** * Joins a wallet in creation. * @param {Object} opts @@ -92,17 +105,12 @@ CopayServer.prototype.createWallet = function (opts, cb) { CopayServer.prototype.joinWallet = function (opts, cb) { var self = this; - Lock.get(opts.walletId, function (lock) { - var _cb = function (err, res) { - cb(err, res); - lock.free(); - }; - + self.runLocked(opts.walletId, cb, function (cb) { self.getWallet({ id: opts.walletId, includeCopayers: true }, function (err, wallet) { - if (err) return _cb(err); - if (!wallet) return _cb('Wallet not found'); - if (_.find(wallet.copayers, { xPubKey: opts.xPubKey })) return _cb('Copayer already in wallet'); - if (wallet.copayers.length == wallet.n) return _cb('Wallet full'); + if (err) return cb(err); + if (!wallet) return cb('Wallet not found'); + if (_.find(wallet.copayers, { xPubKey: opts.xPubKey })) return cb('Copayer already in wallet'); + if (wallet.copayers.length == wallet.n) return cb('Wallet full'); // TODO: validate copayer's extended public key using the public key from this wallet // Note: use Bitcore.crypto.ecdsa .verify() @@ -115,16 +123,15 @@ CopayServer.prototype.createWallet = function (opts, cb) { }); self.storage.storeCopayer(wallet.id, copayer, function (err) { - if (err) return _cb(err); - if ((wallet.copayers.length + 1) < wallet.n) return _cb(); + if (err) return cb(err); + if ((wallet.copayers.length + 1) < wallet.n) return cb(); wallet.status = 'complete'; wallet.publicKeyRing = _.pluck(wallet.copayers, 'xPubKey'); wallet.publicKeyRing.push(copayer.xPubKey); - self.storage.storeWallet(wallet, _cb); + self.storage.storeWallet(wallet, cb); }); }); - }); }; diff --git a/lib/storage.js b/lib/storage.js index a04c8ac..577039d 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -56,6 +56,10 @@ Storage.prototype.storeAddress = function (walletId, address, cb) { this.db.put('wallet-' + walletId + '-address-' + address.address, address, cb); }; +Storage.prototype.storeTx = function (walletId, tx, cb) { + this.db.put('wallet-' + walletId + '-tx-' + tx.ntxid, tx, cb); +}; + Storage.prototype.getAddresses = function (walletId, cb) { var addresses = []; var key = 'wallet-' + walletId + '-address-';