Improve locking

This commit is contained in:
Ivan Socolsky 2015-01-28 10:36:49 -03:00
parent b0e23e6952
commit 635b2d8343
6 changed files with 33 additions and 14 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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);
});
});
});
};

View File

@ -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-';