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

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