From 04dbbc0b45575494bb4177014da49d21b467b339 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 9 Sep 2014 23:00:24 -0300 Subject: [PATCH] add ts to p2p sync --- js/models/core/TxProposals.js | 16 ++++++++++++++++ js/models/core/Wallet.js | 21 ++++++++++++--------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/js/models/core/TxProposals.js b/js/models/core/TxProposals.js index f1021be14..c5ecd49f0 100644 --- a/js/models/core/TxProposals.js +++ b/js/models/core/TxProposals.js @@ -38,6 +38,22 @@ TxProposals.prototype.length = function() { return Object.keys(this.txps).length; }; + +TxProposals.prototype.getNtxidsSince = function(sinceTs) { + preconditions.checkArgument(sinceTs); + var ret = []; + + for(var ii in this.txps){ + var txp = this.txps[ii]; + if (txp.createdTs >= sinceTs) + ret.push(ii); + } +console.log('[TxProposals.js.52:ret:]',ret); //TODO + return ret; +}; + + + TxProposals.prototype.getNtxids = function() { return Object.keys(this.txps); }; diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 47fdbd0f3..fcae0c43f 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -330,7 +330,7 @@ Wallet.prototype._onTxProposal = function(senderId, data) { m.newCopayer = m.txp.setCopayers(senderId, keyMap); } catch (e) { log.error('Corrupt TX proposal received from:', senderId, e.toString()); - m=null; + m = null; } if (m) { @@ -462,8 +462,8 @@ Wallet.prototype.updateTimestamp = function(ts) { * Triggers a call to {@link Wallet#sendWalletReady} */ Wallet.prototype._onNoMessages = function() { - log.debug('No messages at the server. Requesting sync'); //TODO - this.sendWalletReady(); + log.debug('No messages at the server. Requesting peer sync from: ' + this.lastTimestamp + 1); //TODO + this.sendWalletReady(null, parseInt((this.lastTimestamp + 1)/1000) ) ; }; /** @@ -498,9 +498,10 @@ Wallet.prototype._onData = function(senderId, data, ts) { break; case 'walletReady': if (this.lastMessageFrom[senderId] !== 'walletReady') { + log.debug('peer Sync received. since: ' + (data.sinceTs||0)); this.sendPublicKeyRing(senderId); this.sendAddressBook(senderId); - this.sendAllTxProposals(senderId); // send old txps + this.sendAllTxProposals(senderId, data.sinceTs); // send old txps } break; case 'publicKeyRing': @@ -892,11 +893,12 @@ Wallet.prototype.send = function(recipients, obj) { * @desc Send the set of TxProposals to some peers * @param {string[]} recipients - the pubkeys of the recipients */ -Wallet.prototype.sendAllTxProposals = function(recipients) { - var ntxids = this.txProposals.getNtxids(), - that = this; +Wallet.prototype.sendAllTxProposals = function(recipients, sinceTs) { + var ntxids = sinceTs ? this.txProposals.getNtxidsSince(sinceTs) : this.txProposals.getNtxids(); + var self = this; + _.each(ntxids, function(ntxid, key) { - that.sendTxProposal(ntxid, recipients); + self.sendTxProposal(ntxid, recipients); }); }; @@ -947,12 +949,13 @@ Wallet.prototype.sendReject = 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) { +Wallet.prototype.sendWalletReady = function(recipients, sinceTs) { log.debug('### SENDING WalletReady TO:', recipients || 'All'); this.send(recipients, { type: 'walletReady', walletId: this.id, + sinceTs: sinceTs, }); };