add ts to p2p sync

This commit is contained in:
Matias Alejo Garcia 2014-09-09 23:00:24 -03:00
parent 3aca85e268
commit 04dbbc0b45
2 changed files with 28 additions and 9 deletions

View File

@ -38,6 +38,22 @@ TxProposals.prototype.length = function() {
return Object.keys(this.txps).length; 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() { TxProposals.prototype.getNtxids = function() {
return Object.keys(this.txps); return Object.keys(this.txps);
}; };

View File

@ -330,7 +330,7 @@ Wallet.prototype._onTxProposal = function(senderId, data) {
m.newCopayer = m.txp.setCopayers(senderId, keyMap); m.newCopayer = m.txp.setCopayers(senderId, keyMap);
} catch (e) { } catch (e) {
log.error('Corrupt TX proposal received from:', senderId, e.toString()); log.error('Corrupt TX proposal received from:', senderId, e.toString());
m=null; m = null;
} }
if (m) { if (m) {
@ -462,8 +462,8 @@ Wallet.prototype.updateTimestamp = function(ts) {
* Triggers a call to {@link Wallet#sendWalletReady} * Triggers a call to {@link Wallet#sendWalletReady}
*/ */
Wallet.prototype._onNoMessages = function() { Wallet.prototype._onNoMessages = function() {
log.debug('No messages at the server. Requesting sync'); //TODO log.debug('No messages at the server. Requesting peer sync from: ' + this.lastTimestamp + 1); //TODO
this.sendWalletReady(); this.sendWalletReady(null, parseInt((this.lastTimestamp + 1)/1000) ) ;
}; };
/** /**
@ -498,9 +498,10 @@ Wallet.prototype._onData = function(senderId, data, ts) {
break; break;
case 'walletReady': case 'walletReady':
if (this.lastMessageFrom[senderId] !== 'walletReady') { if (this.lastMessageFrom[senderId] !== 'walletReady') {
log.debug('peer Sync received. since: ' + (data.sinceTs||0));
this.sendPublicKeyRing(senderId); this.sendPublicKeyRing(senderId);
this.sendAddressBook(senderId); this.sendAddressBook(senderId);
this.sendAllTxProposals(senderId); // send old txps this.sendAllTxProposals(senderId, data.sinceTs); // send old txps
} }
break; break;
case 'publicKeyRing': case 'publicKeyRing':
@ -892,11 +893,12 @@ Wallet.prototype.send = function(recipients, obj) {
* @desc Send the set of TxProposals to some peers * @desc Send the set of TxProposals to some peers
* @param {string[]} recipients - the pubkeys of the recipients * @param {string[]} recipients - the pubkeys of the recipients
*/ */
Wallet.prototype.sendAllTxProposals = function(recipients) { Wallet.prototype.sendAllTxProposals = function(recipients, sinceTs) {
var ntxids = this.txProposals.getNtxids(), var ntxids = sinceTs ? this.txProposals.getNtxidsSince(sinceTs) : this.txProposals.getNtxids();
that = this; var self = this;
_.each(ntxids, function(ntxid, key) { _.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 * @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 * @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'); log.debug('### SENDING WalletReady TO:', recipients || 'All');
this.send(recipients, { this.send(recipients, {
type: 'walletReady', type: 'walletReady',
walletId: this.id, walletId: this.id,
sinceTs: sinceTs,
}); });
}; };