Merge pull request #1227 from matiu/bug/networking2

Bug/networking2
This commit is contained in:
Gustavo Maximiliano Cortez 2014-08-29 11:30:35 -03:00
commit b2f4d4d870
8 changed files with 47 additions and 85 deletions

2
.gitignore vendored
View File

@ -7,7 +7,7 @@ lib-cov
*.out *.out
*.pid *.pid
*.gz *.gz
*.swp *.sw*
*.sig *.sig
tags tags
pids pids

View File

@ -16,7 +16,6 @@ angular.module('copayApp.controllers').controller('MoreController',
$scope.deleteWallet = function() { $scope.deleteWallet = function() {
var w = $rootScope.wallet; var w = $rootScope.wallet;
w.disconnect();
walletFactory.delete(w.id, function() { walletFactory.delete(w.id, function() {
controllerUtils.logout(); controllerUtils.logout();
}); });

View File

@ -36,7 +36,7 @@ angular.module('copayApp.controllers').controller('SidebarController', function(
$scope.refresh = function() { $scope.refresh = function() {
var w = $rootScope.wallet; var w = $rootScope.wallet;
w.connectToAll(); w.sendWalletReady();
if ($rootScope.addrInfos.length > 0) { if ($rootScope.addrInfos.length > 0) {
controllerUtils.updateBalance(function() { controllerUtils.updateBalance(function() {
$rootScope.$digest(); $rootScope.$digest();
@ -49,11 +49,7 @@ angular.module('copayApp.controllers').controller('SidebarController', function(
}; };
function logout() { function logout() {
var w = $rootScope.wallet; controllerUtils.logout();
if (w) {
w.disconnect();
controllerUtils.logout();
}
} }
// ng-repeat defined number of times instead of repeating over array? // ng-repeat defined number of times instead of repeating over array?

View File

@ -53,6 +53,7 @@ function Wallet(opts) {
this.addressBook = opts.addressBook || {}; this.addressBook = opts.addressBook || {};
this.publicKey = this.privateKey.publicHex; this.publicKey = this.privateKey.publicHex;
this.lastTimestamp = opts.lastTimestamp || undefined; this.lastTimestamp = opts.lastTimestamp || undefined;
this.lastMessageFrom = {};
this.paymentRequests = opts.paymentRequests || {}; this.paymentRequests = opts.paymentRequests || {};
@ -86,19 +87,6 @@ Wallet.prototype.seedCopayer = function(pubKey) {
this.seededCopayerId = pubKey; this.seededCopayerId = pubKey;
}; };
// not being used now
Wallet.prototype.connectToAll = function() {
// not being used now
return;
var all = this.publicKeyRing.getAllCopayerIds();
this.network.connectToCopayers(all);
if (this.seededCopayerId) {
this.sendWalletReady(this.seededCopayerId);
this.seededCopayerId = null;
}
};
Wallet.prototype._onIndexes = function(senderId, data) { Wallet.prototype._onIndexes = function(senderId, data) {
this.log('RECV INDEXES:', data); this.log('RECV INDEXES:', data);
var inIndexes = HDParams.fromList(data.indexes); var inIndexes = HDParams.fromList(data.indexes);
@ -323,6 +311,12 @@ Wallet.prototype.updateTimestamp = function(ts) {
this.store(); this.store();
}; };
Wallet.prototype._onNoMessages = function() {
console.log('No messages at the server. Requesting sync'); //TODO
this.sendWalletReady();
};
Wallet.prototype._onData = function(senderId, data, ts) { Wallet.prototype._onData = function(senderId, data, ts) {
preconditions.checkArgument(senderId); preconditions.checkArgument(senderId);
preconditions.checkArgument(data); preconditions.checkArgument(data);
@ -330,22 +324,26 @@ Wallet.prototype._onData = function(senderId, data, ts) {
preconditions.checkArgument(ts); preconditions.checkArgument(ts);
preconditions.checkArgument(typeof ts === 'number'); preconditions.checkArgument(typeof ts === 'number');
this.updateTimestamp(ts); console.log('RECV', senderId, data);
if (data.type !== 'walletId' && this.id !== data.walletId) { if (data.type !== 'walletId' && this.id !== data.walletId) {
this.emit('corrupt', senderId); this.emit('corrupt', senderId);
this.updateTimestamp(ts);
return; return;
} }
switch (data.type) { switch (data.type) {
// This handler is repeaded on WalletFactory (#join). TODO // This handler is repeaded on WalletFactory (#join). TODO
case 'walletId': case 'walletId':
this.sendWalletReady(senderId); this.sendWalletReady(senderId);
break; break;
case 'walletReady': case 'walletReady':
this.sendPublicKeyRing(senderId); if (this.lastMessageFrom[senderId] !== 'walletReady') {
this.sendAddressBook(senderId); this.sendPublicKeyRing(senderId);
this.sendAllTxProposals(senderId); // send old txps this.sendAddressBook(senderId);
this.sendAllTxProposals(senderId); // send old txps
}
break; break;
case 'publicKeyRing': case 'publicKeyRing':
this._onPublicKeyRing(senderId, data); this._onPublicKeyRing(senderId, data);
@ -365,11 +363,16 @@ Wallet.prototype._onData = function(senderId, data, ts) {
case 'addressbook': case 'addressbook':
this._onAddressBook(senderId, data); this._onAddressBook(senderId, data);
break; break;
// unused messages
case 'disconnect': case 'disconnect':
this._onDisconnect(senderId, data); //case 'an other unused message':
break; break;
default:
throw new Error('unknown message type received: ' + data.type + ' from: ' + senderId)
} }
this.lastMessageFrom[senderId] = data.type;
this.updateTimestamp(ts);
}; };
Wallet.prototype._onConnect = function(newCopayerId) { Wallet.prototype._onConnect = function(newCopayerId) {
@ -381,12 +384,6 @@ Wallet.prototype._onConnect = function(newCopayerId) {
this.emit('connect', peerID); this.emit('connect', peerID);
}; };
Wallet.prototype._onDisconnect = function(peerID) {
this.currentDelay = null;
this.emit('disconnect', peerID);
};
Wallet.prototype.getNetworkName = function() { Wallet.prototype.getNetworkName = function() {
return this.publicKeyRing.network.name; return this.publicKeyRing.network.name;
}; };
@ -445,6 +442,7 @@ Wallet.prototype.netStart = function(callback) {
net.removeAllListeners(); net.removeAllListeners();
net.on('connect', self._onConnect.bind(self)); net.on('connect', self._onConnect.bind(self));
net.on('data', self._onData.bind(self)); net.on('data', self._onData.bind(self));
net.on('no messages', self._onNoMessages.bind(self));
var myId = self.getMyCopayerId(); var myId = self.getMyCopayerId();
var myIdPriv = self.getMyCopayerIdPriv(); var myIdPriv = self.getMyCopayerIdPriv();
@ -647,8 +645,7 @@ Wallet.prototype.sendReject = function(ntxid) {
Wallet.prototype.sendWalletReady = function(recipients) { Wallet.prototype.sendWalletReady = function(recipients) {
preconditions.checkArgument(recipients); this.log('### SENDING WalletReady TO:', recipients || 'All');
this.log('### SENDING WalletReady TO:', recipients);
this.send(recipients, { this.send(recipients, {
type: 'walletReady', type: 'walletReady',
@ -1751,15 +1748,10 @@ Wallet.prototype.indexDiscovery = function(start, change, copayerIndex, gap, cb)
} }
Wallet.prototype.disconnect = function() { Wallet.prototype.close = function() {
this.log('## DISCONNECTING'); this.log('## CLOSING');
this.lock.release(); this.lock.release();
var self = this; this.network.cleanUp();
self.send(null, {
type: 'disconnect',
walletId: this.id,
});
self.network.cleanUp();
}; };
Wallet.prototype.getNetwork = function() { Wallet.prototype.getNetwork = function() {

View File

@ -220,6 +220,8 @@ Network.prototype._setupConnectionHandlers = function(cb) {
}); });
self.socket.on('error', self._onError.bind(self)); self.socket.on('error', self._onError.bind(self));
self.socket.on('no messages', self.emit.bind(self, 'no messages'));
self.socket.on('connect', function() { self.socket.on('connect', function() {
self.socket.on('disconnect', function() { self.socket.on('disconnect', function() {
@ -288,22 +290,12 @@ Network.prototype.start = function(opts, openCallback) {
this._setupConnectionHandlers(openCallback); this._setupConnectionHandlers(openCallback);
this.socket.emit('subscribe', pubkey); this.socket.emit('subscribe', pubkey);
var self = this, var fromTs = opts.lastTimestamp + 1;
tries = 0; var self = this;
self.socket.on('insight-error', function(m) { self.socket.on('subscribed', function(m) {
self.socket.emit('sync', fromTs);
console.log('Retrying to sync...'); self.started = true;
setTimeout(function() {
if (tries++ > 5) {
self.emit('serverError');
} else {
self.socket.emit('sync', opts.lastTimestamp);
}
}, 500);
}); });
self.socket.emit('sync', opts.lastTimestamp);
self.started = true;
}; };
Network.prototype.createSocket = function() { Network.prototype.createSocket = function() {
@ -345,18 +337,21 @@ Network.prototype.send = function(dest, payload, cb) {
dest = this.getCopayerIds(); dest = this.getCopayerIds();
payload.isBroadcast = 1; payload.isBroadcast = 1;
} }
if (typeof dest === 'string') if (typeof dest === 'string')
dest = [dest]; dest = [dest];
var l = dest.length; var l = dest.length;
var i = 0; var i = 0;
//console.log('sending ' + JSON.stringify(payload)); for (var ii in dest) {
dest.forEach(function(to) { var to = dest[ii];
//console.log('\t to ' + to); if (to == this.copayerId)
var message = self.encode(to, payload); continue;
//console.log('SEND to: ' + to, this.copayerId, payload);
var message = this.encode(to, payload);
this.socket.emit('message', message);
}
self.socket.emit('message', message);
});
if (typeof cb === 'function') cb(); if (typeof cb === 'function') cb();
}; };

View File

@ -22,7 +22,7 @@ angular.module('copayApp.services')
root.logout = function() { root.logout = function() {
if ($rootScope.wallet) if ($rootScope.wallet)
$rootScope.wallet.disconnect(); $rootScope.wallet.close();
Socket.removeAllListeners(); Socket.removeAllListeners();
@ -179,9 +179,6 @@ angular.module('copayApp.services')
} }
$rootScope.$digest(); $rootScope.$digest();
}); });
w.on('disconnect', function(peerID) {
$rootScope.$digest();
});
w.on('close', root.onErrorDigest); w.on('close', root.onErrorDigest);
w.on('locked', root.onErrorDigest.bind(this)); w.on('locked', root.onErrorDigest.bind(this));
w.netStart(); w.netStart();

View File

@ -102,8 +102,7 @@ FakeWallet.prototype.toEncryptedObj = function() {
return this.enc; return this.enc;
}; };
FakeWallet.prototype.disconnect = function() { FakeWallet.prototype.close = function() {
this.disconnectCalled = 1;
}; };
// TODO a try catch was here // TODO a try catch was here

View File

@ -530,15 +530,6 @@ describe('Wallet model', function() {
w._onConnect(newId); w._onConnect(newId);
}); });
it('handle disconnections', function(done) {
var w = createW();
w.on('disconnect', function(id) {
id.should.equal(newId);
done();
});
w._onDisconnect(newId);
});
it('should register new copayers correctly', function() { it('should register new copayers correctly', function() {
var w = createW(); var w = createW();
var r = w.getRegisteredCopayerIds(); var r = w.getRegisteredCopayerIds();
@ -1429,11 +1420,4 @@ describe('Wallet model', function() {
should.exist(n.networkNonce); should.exist(n.networkNonce);
}); });
it('#disconnect', function() {
var w = cachedCreateW();
var spy1 = sinon.spy(w, 'send');
w.disconnect();
spy1.calledOnce.should.be.true;
});
}); });