From 614ed087c78704a0a4e61c9be15c1e86ffb3208b Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 5 Jan 2015 13:56:09 -0300 Subject: [PATCH 1/6] better sync for incomplete wallets after close/open --- js/controllers/createProfile.js | 3 +-- js/models/Async.js | 20 ++++++++++++++++---- js/models/Wallet.js | 27 ++++++++++++++++++--------- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/js/controllers/createProfile.js b/js/controllers/createProfile.js index 71061cac2..cb16a0559 100644 --- a/js/controllers/createProfile.js +++ b/js/controllers/createProfile.js @@ -150,13 +150,12 @@ angular.module('copayApp.controllers').controller('CreateProfileController', fun $scope.error = null; if (err) { var msg = err.toString(); + $scope.createStep = 'email'; if (msg.indexOf('EEXIST') >= 0 || msg.indexOf('BADC') >= 0) { msg = 'This profile already exists' - $scope.createStep = 'email'; } if (msg.indexOf('EMAILERROR') >= 0) { msg = 'Could not send verification email. Please check your email address.'; - $scope.createStep = 'email'; } $scope.error = msg; $scope.passwordStrength = null; diff --git a/js/models/Async.js b/js/models/Async.js index 505b7826e..33e8e23f5 100644 --- a/js/models/Async.js +++ b/js/models/Async.js @@ -106,10 +106,14 @@ Network.prototype._deletePeer = function(peerId) { this.connectedPeers = Network._arrayRemove(peerId, this.connectedPeers); }; -Network.prototype._addConnectedCopayer = function(copayerId) { +Network.prototype._addCopayer = function(copayerId) { var peerId = this.peerFromCopayer(copayerId); this._addCopayerMap(peerId, copayerId); Network._arrayPushOnce(peerId, this.connectedPeers); +}; + +Network.prototype._addConnectedCopayer = function(copayerId) { + this._addCopayer(copayerId); this.emit('connect', copayerId); }; @@ -255,7 +259,7 @@ Network.prototype._setupSocketHandlers = function(opts, cb) { // We ask for this message, and then ignore it, only to see if the // server has erased our old messages. - + if (fromTs) { self.ignoreMessageFromTs = fromTs; } @@ -272,8 +276,8 @@ Network.prototype._setupSocketHandlers = function(opts, cb) { }, 1); }); self.socket.on('error', self._onError.bind(self)); - self.socket.on('no_messages', self.emit.bind(self, 'no_messages')); + self.socket.on('no messages', self.emit.bind(self, 'no_messages')); self.socket.on('connect', function() { var pubkey = self.getKey().public.toString('hex'); log.debug('Async subscribing to pubkey:', pubkey); @@ -331,7 +335,6 @@ Network.prototype.start = function(opts, openCallback) { preconditions.checkArgument(opts); preconditions.checkArgument(opts.privkey); preconditions.checkArgument(opts.copayerId); - preconditions.checkState(this.connectedPeers && this.connectedPeers.length === 0); if (this.started) { log.debug('Async: Networing already started for this wallet.') @@ -368,6 +371,7 @@ Network.prototype.getCopayerIds = function() { for (var peerId in this.copayerForPeer) { copayerIds.push(this.copayerForPeer[peerId]); } + console.log('[Async.js.373]', copayerIds); //TODO return copayerIds; } }; @@ -380,6 +384,7 @@ Network.prototype.send = function(dest, payload, cb) { var self = this; if (!dest) { dest = this.getCopayerIds(); + console.log('[Async.js.383:dest:]', dest); //TODO payload.isBroadcast = 1; } @@ -388,6 +393,7 @@ Network.prototype.send = function(dest, payload, cb) { var l = dest.length; var i = 0; + for (var ii in dest) { var to = dest[ii]; if (to == this.copayerId) @@ -423,4 +429,10 @@ Network.prototype.lockIncommingConnections = function(allowedCopayerIdsArray) { } }; +Network.prototype.setCopayers = function(copayersIdsArray) { + for (var i in copayersIdsArray) { + this._addCopayer(copayersIdsArray[i]); + } +}; + module.exports = Network; diff --git a/js/models/Wallet.js b/js/models/Wallet.js index d65391bcc..349739fea 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -687,8 +687,13 @@ Wallet.prototype.updateSyncedTimestamp = function(ts) { * Triggers a call to {@link Wallet#sendWalletReady} */ Wallet.prototype._onNoMessages = function() { - log.debug('Wallet:' + this.id + ' No messages at the server. Requesting peer sync from: ' + (this.syncedTimestamp + 1)); - this.sendWalletReady(null, parseInt((this.syncedTimestamp + 1) / 1000000)); + if (this.isComplete()) { + log.debug('Wallet:' + this.getName() + ' No messages at the server. Requesting peer sync from: ' + (this.syncedTimestamp + 1)); + this.sendWalletReady(null, parseInt((this.syncedTimestamp + 1) / 1000000)); + } else { + log.debug('Incomplete wallet:' + this.getName() + ' No messages at the server. Requesting forced peer sync from 0'); + this.sendWalletReady(null, 0, true); + } }; /** @@ -723,7 +728,7 @@ Wallet.prototype._onData = function(senderId, data, ts) { this.sendWalletReady(senderId); break; case 'walletReady': - if (this.lastMessageFrom[senderId] !== 'walletReady') { + if (this.lastMessageFrom[senderId] !== 'walletReady' || data.force) { log.debug('Wallet:' + this.id + ' peer Sync received. since: ' + (data.sinceTs || 0)); this.sendPublicKeyRing(senderId); this.sendAddressBook(senderId); @@ -986,7 +991,10 @@ Wallet.prototype.netStart = function() { if (this.publicKeyRing.isComplete()) { this._lockIncomming(this.publicKeyRing.getAllCopayerIds()); + } else { + this.network.setCopayers(this.publicKeyRing.getAllCopayerIds()); } + log.debug('Wallet:' + self.id + ' Starting network.'); this.network.start(startOpts, function() { self.emitAndKeepAlive(self.isComplete() ? 'ready' : 'waitingCopayers'); @@ -1191,7 +1199,7 @@ Wallet.fromObj = function(o, readOpts) { }); } - opts.syncedTimestamp = o.syncedTimestamp || 0; + opts.syncedTimestamp = opts.publicKeyRing.isComplete() ? o.syncedTimestamp || 0 : 0; opts.blockchainOpts = readOpts.blockchainOpts; opts.networkOpts = readOpts.networkOpts; @@ -1208,7 +1216,7 @@ Wallet.fromObj = function(o, readOpts) { Wallet.prototype._sendToPeers = function(recipients, obj) { if (!this.isShared()) return; log.info('Wallet:' + this.getName() + ' ### Sending ' + obj.type); - log.debug('Sending obj', obj); + log.debug('Sending:', recipients, obj); this.network.send(recipients, obj); }; @@ -1293,11 +1301,12 @@ Wallet.prototype.sendSignature = function(ntxid) { * @desc Notify other peers that a wallet has been backed up and it's ready to be used * @param {string[]} [recipients] - the pubkeys of the recipients */ -Wallet.prototype.sendWalletReady = function(recipients, sinceTs) { +Wallet.prototype.sendWalletReady = function(recipients, sinceTs, force) { this._sendToPeers(recipients, { type: 'walletReady', walletId: this.id, sinceTs: sinceTs, + force: force, }); }; @@ -1442,7 +1451,7 @@ Wallet.prototype.getPendingTxProposalsCount = function() { _.each(txps, function(inTxp, ntxid) { if (!inTxp.isPending(maxRejectCount)) return; -// TODO: are the uxtos availables? + // TODO: are the uxtos availables? // pending++; @@ -1631,7 +1640,7 @@ Wallet.prototype.broadcastToBitcoinNetwork = function(ntxid, cb) { this.blockchain.broadcast(txHex, function(err, txid) { if (err || !txid) { - log.debug('Wallet:' + self.getName() + ' Send failed:' + err ); + log.debug('Wallet:' + self.getName() + ' Send failed:' + err); self._checkIfTxIsSent(ntxid, function(err, txid) { return cb(err, txid); @@ -2205,7 +2214,7 @@ Wallet.prototype.getUnspent = function(cb) { if (self.cache.unspent != null) { log.debug('Wallet ' + this.getName() + ': Get unspent cache hit'); return self.computeUnspent(self.cache.unspent, cb); - return + return } var addresses = this.getAddresses(); From b46a05fe4af5a4c281cc15678f9f0802bdb00d9b Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 5 Jan 2015 14:01:16 -0300 Subject: [PATCH 2/6] rm console.log --- js/models/Async.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/js/models/Async.js b/js/models/Async.js index 33e8e23f5..00ec189d8 100644 --- a/js/models/Async.js +++ b/js/models/Async.js @@ -371,7 +371,6 @@ Network.prototype.getCopayerIds = function() { for (var peerId in this.copayerForPeer) { copayerIds.push(this.copayerForPeer[peerId]); } - console.log('[Async.js.373]', copayerIds); //TODO return copayerIds; } }; @@ -384,7 +383,6 @@ Network.prototype.send = function(dest, payload, cb) { var self = this; if (!dest) { dest = this.getCopayerIds(); - console.log('[Async.js.383:dest:]', dest); //TODO payload.isBroadcast = 1; } From 85d73e9bb4370b4110e4e01c22c8950186bc86fd Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 5 Jan 2015 14:04:26 -0300 Subject: [PATCH 3/6] rm return --- js/models/Wallet.js | 1 - 1 file changed, 1 deletion(-) diff --git a/js/models/Wallet.js b/js/models/Wallet.js index 349739fea..42ad94aeb 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -2214,7 +2214,6 @@ Wallet.prototype.getUnspent = function(cb) { if (self.cache.unspent != null) { log.debug('Wallet ' + this.getName() + ': Get unspent cache hit'); return self.computeUnspent(self.cache.unspent, cb); - return } var addresses = this.getAddresses(); From 3bdf35c6abf0083d7c46e097381e8d74aec6db12 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 5 Jan 2015 14:30:25 -0300 Subject: [PATCH 4/6] send walletReady always for incomplete wallets --- js/models/Wallet.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/js/models/Wallet.js b/js/models/Wallet.js index 42ad94aeb..53a7690e8 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -690,9 +690,6 @@ Wallet.prototype._onNoMessages = function() { if (this.isComplete()) { log.debug('Wallet:' + this.getName() + ' No messages at the server. Requesting peer sync from: ' + (this.syncedTimestamp + 1)); this.sendWalletReady(null, parseInt((this.syncedTimestamp + 1) / 1000000)); - } else { - log.debug('Incomplete wallet:' + this.getName() + ' No messages at the server. Requesting forced peer sync from 0'); - this.sendWalletReady(null, 0, true); } }; @@ -992,7 +989,12 @@ Wallet.prototype.netStart = function() { if (this.publicKeyRing.isComplete()) { this._lockIncomming(this.publicKeyRing.getAllCopayerIds()); } else { - this.network.setCopayers(this.publicKeyRing.getAllCopayerIds()); + //Partially complete wallet. + if (this.publicKeyRing.getAllCopayerIds() > 1) { + this.network.setCopayers(this.publicKeyRing.getAllCopayerIds()); + log.debug('Incomplete wallet opened:' + this.getName() + '. forced peer sync from 0'); + this.sendWalletReady(null, 0, true); + } } log.debug('Wallet:' + self.id + ' Starting network.'); @@ -1216,7 +1218,7 @@ Wallet.fromObj = function(o, readOpts) { Wallet.prototype._sendToPeers = function(recipients, obj) { if (!this.isShared()) return; log.info('Wallet:' + this.getName() + ' ### Sending ' + obj.type); - log.debug('Sending:', recipients, obj); + log.debug('Sending:', recipients, obj); this.network.send(recipients, obj); }; From 0963d9133ba1abfb1e224676d435978db1512d3a Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 5 Jan 2015 14:48:50 -0300 Subject: [PATCH 5/6] rm force --- js/models/Wallet.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/js/models/Wallet.js b/js/models/Wallet.js index 53a7690e8..f333fdca8 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -725,7 +725,7 @@ Wallet.prototype._onData = function(senderId, data, ts) { this.sendWalletReady(senderId); break; case 'walletReady': - if (this.lastMessageFrom[senderId] !== 'walletReady' || data.force) { + if (this.lastMessageFrom[senderId] !== 'walletReady') { log.debug('Wallet:' + this.id + ' peer Sync received. since: ' + (data.sinceTs || 0)); this.sendPublicKeyRing(senderId); this.sendAddressBook(senderId); @@ -990,10 +990,10 @@ Wallet.prototype.netStart = function() { this._lockIncomming(this.publicKeyRing.getAllCopayerIds()); } else { //Partially complete wallet. - if (this.publicKeyRing.getAllCopayerIds() > 1) { + if (this.publicKeyRing.getAllCopayerIds().length > 1) { this.network.setCopayers(this.publicKeyRing.getAllCopayerIds()); log.debug('Incomplete wallet opened:' + this.getName() + '. forced peer sync from 0'); - this.sendWalletReady(null, 0, true); + this.sendWalletReady(null, 0); } } @@ -1303,12 +1303,11 @@ Wallet.prototype.sendSignature = function(ntxid) { * @desc Notify other peers that a wallet has been backed up and it's ready to be used * @param {string[]} [recipients] - the pubkeys of the recipients */ -Wallet.prototype.sendWalletReady = function(recipients, sinceTs, force) { +Wallet.prototype.sendWalletReady = function(recipients, sinceTs) { this._sendToPeers(recipients, { type: 'walletReady', walletId: this.id, - sinceTs: sinceTs, - force: force, + sinceTs: sinceTs }); }; From f65bc7b9e580b138a1ceb013f68eb420e0fe28eb Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 5 Jan 2015 14:54:09 -0300 Subject: [PATCH 6/6] fix send --- js/models/Wallet.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/js/models/Wallet.js b/js/models/Wallet.js index f333fdca8..2e712e4e4 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -992,13 +992,16 @@ Wallet.prototype.netStart = function() { //Partially complete wallet. if (this.publicKeyRing.getAllCopayerIds().length > 1) { this.network.setCopayers(this.publicKeyRing.getAllCopayerIds()); - log.debug('Incomplete wallet opened:' + this.getName() + '. forced peer sync from 0'); - this.sendWalletReady(null, 0); } } log.debug('Wallet:' + self.id + ' Starting network.'); this.network.start(startOpts, function() { + //Partially complete wallet. + if (self.publicKeyRing.getAllCopayerIds().length > 1) { + log.debug('Incomplete wallet opened:' + self.getName() + '. forced peer sync from 0'); + self.sendWalletReady(null, 0); + } self.emitAndKeepAlive(self.isComplete() ? 'ready' : 'waitingCopayers'); }); };