split ts, better syncing

This commit is contained in:
Matias Alejo Garcia 2014-10-31 12:27:22 -03:00
commit b49661ebd8
4 changed files with 50 additions and 24 deletions

View file

@ -204,6 +204,11 @@ Network.prototype._onMessage = function(enc) {
} }
log.debug('Async: receiving ' + JSON.stringify(payload)); log.debug('Async: receiving ' + JSON.stringify(payload));
if (this.ignoreMessageFromTs && this.ignoreMessageFromTs === enc.ts) {
log.debug('Ignoring message from ', enc.ts);
return;
}
var self = this; var self = this;
switch (payload.type) { switch (payload.type) {
case 'hello': case 'hello':
@ -247,7 +252,15 @@ Network.prototype._setupConnectionHandlers = function(opts, cb) {
}); });
self.socket.on('subscribed', function(m) { self.socket.on('subscribed', function(m) {
var fromTs = (opts.lastTimestamp || 0) + 1; var fromTs = opts.syncedTimestamp || 0;
// 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;
}
log.info('Async: synchronizing from: ',fromTs);
self.socket.emit('sync', fromTs); self.socket.emit('sync', fromTs);
self.started = true; self.started = true;
}); });

View file

@ -566,7 +566,7 @@ Identity.prototype.decodeSecret = function(secret) {
Identity.prototype.getLastFocusedWallet = function() { Identity.prototype.getLastFocusedWallet = function() {
if (_.keys(this.wallets).length == 0) return; if (_.keys(this.wallets).length == 0) return;
return _.max(this.wallets, function(wallet) { return _.max(this.wallets, function(wallet) {
return wallet.lastTimestamp || 0; return wallet.focusedTimestamp || 0;
}); });
}; };

View file

@ -92,7 +92,9 @@ function Wallet(opts) {
this.registeredPeerIds = []; this.registeredPeerIds = [];
this.addressBook = opts.addressBook || {}; this.addressBook = opts.addressBook || {};
this.publicKey = this.privateKey.publicHex; this.publicKey = this.privateKey.publicHex;
this.lastTimestamp = opts.lastTimestamp || 0; this.focusedTimestamp = opts.focusedTimestamp || 0;
this.syncedTimestamp = opts.syncedTimestamp || 0;
console.log('[Wallet.js.96:syncedTimestamp:]',this.syncedTimestamp); //TODO
this.lastMessageFrom = {}; this.lastMessageFrom = {};
this.paymentRequests = opts.paymentRequests || {}; this.paymentRequests = opts.paymentRequests || {};
@ -147,7 +149,8 @@ Wallet.PERSISTED_PROPERTIES = [
'txProposals', 'txProposals',
'privateKey', 'privateKey',
'addressBook', 'addressBook',
'lastTimestamp', 'focusedTimestamp',
'syncedTimestamp',
'secretNumber', 'secretNumber',
]; ];
@ -571,24 +574,31 @@ Wallet.prototype._onAddressBook = function(senderId, data) {
* @desc Updates the wallet's last modified timestamp and triggers a save * @desc Updates the wallet's last modified timestamp and triggers a save
* @param {number} ts - the timestamp * @param {number} ts - the timestamp
*/ */
Wallet.prototype.updateTimestamp = function(ts, callback) { Wallet.prototype.updateFocusedTimestamp = function(ts) {
preconditions.checkArgument(ts); preconditions.checkArgument(ts);
preconditions.checkArgument(_.isNumber(ts)); preconditions.checkArgument(_.isNumber(ts));
this.lastTimestamp = ts; preconditions.checkArgument(ts > 2999999999, 'use miliseconds');
// we dont store here this.focusedTimestamp = ts;
if (callback) {
return callback(null);
}
}; };
Wallet.prototype.updateSyncedTimestamp = function(ts) {
preconditions.checkArgument(ts);
preconditions.checkArgument(_.isNumber(ts));
preconditions.checkArgument(ts > 2999999999999, 'use microseconds');
this.syncedTimestamp = ts;
};
/** /**
* @desc Called when there are no messages in the server * @desc Called when there are no messages in the server
* Triggers a call to {@link Wallet#sendWalletReady} * Triggers a call to {@link Wallet#sendWalletReady}
*/ */
Wallet.prototype._onNoMessages = function() { Wallet.prototype._onNoMessages = function() {
log.debug('Wallet:' + this.id + ' No messages at the server. Requesting peer sync from: ' + (this.lastTimestamp + 1)); if (!this.isShared()) return;
this.sendWalletReady(null, parseInt((this.lastTimestamp + 1) / 1000));
this.updateTimestamp(parseInt(Date.now() / 1000)); 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));
}; };
/** /**
@ -608,7 +618,7 @@ Wallet.prototype._onData = function(senderId, data, ts) {
preconditions.checkArgument(_.isNumber(ts)); preconditions.checkArgument(_.isNumber(ts));
log.debug('Wallet:' + this.id + ' RECV', senderId, data); log.debug('Wallet:' + this.id + ' RECV', senderId, data);
this.updateTimestamp(ts); this.updateSyncedTimestamp(ts);
if (data.type !== 'walletId' && this.id !== data.walletId) { if (data.type !== 'walletId' && this.id !== data.walletId) {
log.debug('Wallet:' + this.id + ' Received corrupt message:', data) log.debug('Wallet:' + this.id + ' Received corrupt message:', data)
@ -872,7 +882,7 @@ Wallet.prototype.netStart = function() {
copayerId: myId, copayerId: myId,
privkey: myIdPriv, privkey: myIdPriv,
maxPeers: self.totalCopayers, maxPeers: self.totalCopayers,
lastTimestamp: this.lastTimestamp || 0, syncedTimestamp: this.syncedTimestamp || 0,
secretNumber: self.secretNumber, secretNumber: self.secretNumber,
}; };
@ -967,7 +977,8 @@ Wallet.prototype.toObj = function() {
txProposals: this.txProposals.toObj(), txProposals: this.txProposals.toObj(),
privateKey: this.privateKey ? this.privateKey.toObj() : undefined, privateKey: this.privateKey ? this.privateKey.toObj() : undefined,
addressBook: this.addressBook, addressBook: this.addressBook,
lastTimestamp: this.lastTimestamp || 0, syncedTimestamp: this.syncedTimestamp || 0,
focusedTimestamp: this.focusedTimestamp || 0,
secretNumber: this.secretNumber, secretNumber: this.secretNumber,
}; };
@ -993,7 +1004,7 @@ Wallet.fromUntrustedObj = function(obj, readOpts) {
* @param {Object} o.privateKey - Private key to be deserialized by {@link PrivateKey#fromObj} * @param {Object} o.privateKey - Private key to be deserialized by {@link PrivateKey#fromObj}
* @param {string} o.networkName - 'livenet' or 'testnet' * @param {string} o.networkName - 'livenet' or 'testnet'
* @param {Object} o.publicKeyRing - PublicKeyRing to be deserialized by {@link PublicKeyRing#fromObj} * @param {Object} o.publicKeyRing - PublicKeyRing to be deserialized by {@link PublicKeyRing#fromObj}
* @param {number} o.lastTimestamp - last time this wallet object was deserialized * @param {number} o.Timestamp - last time this wallet object was deserialized
* @param {Object} o.txProposals - TxProposals to be deserialized by {@link TxProposals#fromObj} * @param {Object} o.txProposals - TxProposals to be deserialized by {@link TxProposals#fromObj}
* @param {string} o.nickname - user's nickname * @param {string} o.nickname - user's nickname
* *
@ -1065,7 +1076,8 @@ Wallet.fromObj = function(o, readOpts) {
}); });
} }
opts.lastTimestamp = o.lastTimestamp || 0; opts.syncedTimestamp = o.syncedTimestamp || 0;
opts.focusedTimestamp = o.focusedTimestamp || 0;
opts.blockchainOpts = readOpts.blockchainOpts; opts.blockchainOpts = readOpts.blockchainOpts;
opts.networkOpts = readOpts.networkOpts; opts.networkOpts = readOpts.networkOpts;
@ -1073,6 +1085,7 @@ Wallet.fromObj = function(o, readOpts) {
return new Wallet(opts); return new Wallet(opts);
}; };
/** /**
* @desc Send a message to other peers * @desc Send a message to other peers
* @param {string[]} recipients - the pubkey of the recipients of the message * @param {string[]} recipients - the pubkey of the recipients of the message

View file

@ -205,15 +205,15 @@ angular.module('copayApp.services')
root.setFocusedWallet = function(w) { root.setFocusedWallet = function(w) {
if (!_.isObject(w)) if (!_.isObject(w))
w = $rootScope.iden.getWalletById(w); w = $rootScope.iden.getWalletById(w);
preconditions.checkState(w && _.isObject(w)); preconditions.checkState(w && _.isObject(w));
$rootScope.wallet = w; $rootScope.wallet = w;
w.updateTimestamp(new Date().getTime(), function() { w.updateFocusedTimestamp(Date.now());
root.redirIfLogged(); root.redirIfLogged();
root.updateBalance(w, function() { root.updateBalance(w, function() {
$rootScope.$digest(); $rootScope.$digest();
}) })
});
}; };
root.bindProfile = function($scope, iden, w) { root.bindProfile = function($scope, iden, w) {