better sync for incomplete wallets after close/open

This commit is contained in:
Matias Alejo Garcia 2015-01-05 13:56:09 -03:00
commit 614ed087c7
3 changed files with 35 additions and 15 deletions

View file

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

View file

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

View file

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