mirror of https://github.com/BTCPrivate/copay.git
Merge pull request #1666 from matiu/bug/more-sync-fixes
Bug/more sync fixes
This commit is contained in:
commit
bd26b23fad
|
@ -202,12 +202,12 @@ Network.prototype._onMessage = function(enc) {
|
|||
this._deletePeer(sender);
|
||||
return;
|
||||
}
|
||||
log.debug('Async: receiving ' + JSON.stringify(payload));
|
||||
|
||||
if (this.ignoreMessageFromTs && this.ignoreMessageFromTs === enc.ts) {
|
||||
log.debug('Ignoring message from ', enc.ts);
|
||||
log.debug('Ignoring trailing message. Ts:', enc.ts);
|
||||
return;
|
||||
}
|
||||
log.debug('Async: receiving ' + JSON.stringify(payload));
|
||||
|
||||
var self = this;
|
||||
switch (payload.type) {
|
||||
|
@ -352,7 +352,7 @@ Network.prototype.start = function(opts, openCallback) {
|
|||
};
|
||||
|
||||
Network.prototype.createSocket = function() {
|
||||
log.debug('Async: Connecting to socket:', this.url, this.socketOptions);
|
||||
log.debug('Async: Connecting to socket:', this.url);
|
||||
return io.connect(this.url, this.socketOptions);
|
||||
};
|
||||
|
||||
|
|
|
@ -158,7 +158,7 @@ Identity.prototype.retrieveWalletFromStorage = function(walletId, opts, callback
|
|||
return callback(error);
|
||||
}
|
||||
try {
|
||||
log.debug('## OPENING Wallet: ' + walletId);
|
||||
log.info('## OPENING Wallet:', walletId);
|
||||
if (_.isString(walletData)) {
|
||||
walletData = JSON.parse(walletData);
|
||||
}
|
||||
|
@ -209,7 +209,7 @@ Identity.prototype.storeWallet = function(wallet, cb) {
|
|||
* @param {Function} cb
|
||||
*/
|
||||
Identity.storeWalletDebounced = _.debounce(function(identity, wallet, cb) {
|
||||
identity.storeWallet(wallet,cb);
|
||||
identity.storeWallet(wallet, cb);
|
||||
}, 3000);
|
||||
|
||||
|
||||
|
@ -252,10 +252,13 @@ Identity.prototype.store = function(opts, cb) {
|
|||
storeFunction.call(self.storage, this.getId(), this.toObj(), function(err) {
|
||||
if (err) return cb(err);
|
||||
|
||||
console.log('[Identity.js.255:opts:]', opts); //TODO
|
||||
if (opts.noWallets)
|
||||
return cb();
|
||||
|
||||
async.map(self.wallets, self.storeWallet, cb);
|
||||
async.each(_.values(self.wallets), function(wallet, in_cb) {
|
||||
self.storeWallet(wallet, in_cb);
|
||||
}, cb);
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -312,11 +315,14 @@ Identity.prototype.importWalletFromObj = function(obj, opts, cb) {
|
|||
if (!w) return cb(new Error('Could not decrypt'));
|
||||
|
||||
this._checkVersion(w.version);
|
||||
this.addWallet(w, function(err) {
|
||||
if (err) return cb(err, null);
|
||||
self.wallets[w.getId()] = w;
|
||||
self.bindWallet(w);
|
||||
self.store(null, function(err) {
|
||||
this.addWallet(w);
|
||||
self.bindWallet(w);
|
||||
self.storeWallet(w, function(err) {
|
||||
if (err) return cb(err);
|
||||
|
||||
self.store({
|
||||
noWallets: true
|
||||
}, function(err) {
|
||||
return cb(err, w);
|
||||
});
|
||||
});
|
||||
|
@ -376,7 +382,7 @@ Identity.prototype.bindWallet = function(w) {
|
|||
var self = this;
|
||||
|
||||
self.wallets[w.getId()] = w;
|
||||
log.debug('Binding wallet ' + w.getName());
|
||||
log.debug('Binding wallet:' + w.getName());
|
||||
|
||||
w.on('txProposalsUpdated', function() {
|
||||
Identity.storeWalletDebounced(self, w);
|
||||
|
@ -390,13 +396,6 @@ Identity.prototype.bindWallet = function(w) {
|
|||
w.on('txProposalEvent', function() {
|
||||
Identity.storeWalletDebounced(self, w);
|
||||
});
|
||||
w.on('ready', function() {
|
||||
self.store({
|
||||
noWallets: true
|
||||
}, function() {
|
||||
Identity.storeWalletDebounced(self, w);
|
||||
});
|
||||
});
|
||||
w.on('addressBookUpdated', function() {
|
||||
Identity.storeWalletDebounced(self, w);
|
||||
});
|
||||
|
@ -476,23 +475,24 @@ Identity.prototype.createWallet = function(opts, cb) {
|
|||
var self = this;
|
||||
|
||||
var w = new walletClass(opts);
|
||||
this.addWallet(w, function(err) {
|
||||
this.addWallet(w);
|
||||
self.bindWallet(w);
|
||||
w.netStart();
|
||||
self.storeWallet(w, function(err) {
|
||||
if (err) return cb(err);
|
||||
self.bindWallet(w);
|
||||
w.netStart();
|
||||
return cb(err, w);
|
||||
|
||||
self.store({
|
||||
noWallets: true
|
||||
}, function(err) {
|
||||
return cb(err, w);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Identity.prototype.addWallet = function(wallet, cb) {
|
||||
Identity.prototype.addWallet = function(wallet) {
|
||||
preconditions.checkArgument(wallet);
|
||||
preconditions.checkArgument(wallet.getId);
|
||||
preconditions.checkArgument(cb);
|
||||
|
||||
this.wallets[wallet.getId()] = wallet;
|
||||
|
||||
// TODO (eordano): Consider not saving automatically after this
|
||||
this.storage.setItem(wallet.getStorageKey(), wallet.toObj(), cb);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -662,14 +662,7 @@ Identity.prototype.joinWallet = function(opts, cb) {
|
|||
err = 'walletFull';
|
||||
}
|
||||
}
|
||||
if (err)
|
||||
return cb(err);
|
||||
|
||||
self.store({
|
||||
noWallets: true
|
||||
}, function(err) {
|
||||
return cb(err, w);
|
||||
});
|
||||
return cb(err, w);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
|
@ -124,7 +124,7 @@ Insight.prototype.subscribeToBlocks = function() {
|
|||
|
||||
/** @private */
|
||||
Insight.prototype._getSocketIO = function(url, opts) {
|
||||
log.debug('Insight: Connecting to socket:', this.url, this.opts);
|
||||
log.debug('Insight: Connecting to socket:', this.url);
|
||||
return io(this.url, this.opts);
|
||||
};
|
||||
|
||||
|
@ -200,7 +200,6 @@ Insight.prototype.subscribe = function(addresses) {
|
|||
var self = this;
|
||||
|
||||
function handlerFor(self, address) {
|
||||
console.log('HANDLER [Insight.js.150:address:]',address); //TODO
|
||||
return function(txid) {
|
||||
// verify the address is still subscribed
|
||||
if (!self.subscribed[address]) return;
|
||||
|
|
|
@ -116,7 +116,8 @@ function Wallet(opts) {
|
|||
inherits(Wallet, events.EventEmitter);
|
||||
|
||||
Wallet.prototype.emitAndKeepAlive = function(args) {
|
||||
log.debug('Wallet Emitting:', arguments);
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
log.debug('Wallet Emitting:', args);
|
||||
this.keepAlive();
|
||||
this.emit.apply(this, arguments);
|
||||
};
|
||||
|
@ -810,7 +811,7 @@ Wallet.prototype._setBlockchainListeners = function() {
|
|||
var self = this;
|
||||
self.blockchain.removeAllListeners();
|
||||
|
||||
log.debug('Setting Blockchain listeners for', this.getId());
|
||||
log.debug('Setting Blockchain listeners for', this.getName());
|
||||
self.blockchain.on('reconnect', function(attempts) {
|
||||
log.debug('Wallet:' + self.id + 'blockchain reconnect event');
|
||||
self.emitAndKeepAlive('insightReconnected');
|
||||
|
@ -867,7 +868,7 @@ Wallet.prototype.netStart = function() {
|
|||
|
||||
|
||||
if (net.started) {
|
||||
log.debug('Wallet:' + self.id + ' Wallet networking was ready')
|
||||
log.debug('Wallet:' + self.getName() + ': Wallet networking was ready')
|
||||
self.emitAndKeepAlive('ready', net.getPeer());
|
||||
return;
|
||||
}
|
||||
|
@ -888,12 +889,8 @@ Wallet.prototype.netStart = function() {
|
|||
|
||||
log.debug('Wallet:' + self.id + ' Starting networking: ' + startOpts.copayerId);
|
||||
net.start(startOpts, function() {
|
||||
log.debug('Wallet:' + self.id + ' Networking ready:', net.copayerId);
|
||||
self._setBlockchainListeners();
|
||||
self.emitAndKeepAlive('ready', net.getPeer());
|
||||
setTimeout(function() {
|
||||
self._newAddresses(true);
|
||||
}, 0);
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -0,0 +1,116 @@
|
|||
'use strict';
|
||||
|
||||
var preconditions = require('preconditions').singleton();
|
||||
|
||||
function WalletLock(storage, walletId, timeoutMin) {
|
||||
preconditions.checkArgument(storage);
|
||||
preconditions.checkArgument(walletId);
|
||||
|
||||
this.storage = storage;
|
||||
this.timeoutMin = timeoutMin || 5;
|
||||
this.key = WalletLock._keyFor(walletId);
|
||||
}
|
||||
|
||||
WalletLock.prototype.getSessionId = function(cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
var self = this;
|
||||
|
||||
self.sessionStorage.getItem('sessionId', function(sessionId) {
|
||||
if (sessionId)
|
||||
return cb(sessionId);
|
||||
|
||||
sessionId = bitcore.SecureRandom.getRandomBuffer(8).toString('hex');
|
||||
self.sessionStorage.setItem('sessionId', sessionId, function() {
|
||||
return cb(sessionId);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
WalletLock.prototype.init = function(cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
var self = this;
|
||||
|
||||
self.storage.getSessionId(function(sid) {
|
||||
preconditions.checkState(sid);
|
||||
|
||||
self.sessionId = sid;
|
||||
cb();
|
||||
});
|
||||
};
|
||||
|
||||
WalletLock._keyFor = function(walletId) {
|
||||
return 'lock' + '::' + walletId;
|
||||
};
|
||||
|
||||
WalletLock.prototype._isLockedByOther = function(cb) {
|
||||
var self = this;
|
||||
|
||||
this.storage.getGlobal(this.key, function(json) {
|
||||
var wl = json ? JSON.parse(json) : null;
|
||||
if (!wl || !wl.expireTs)
|
||||
return cb(false);
|
||||
|
||||
var expiredSince = Date.now() - wl.expireTs;
|
||||
if (expiredSince >= 0)
|
||||
return cb(false);
|
||||
|
||||
var isMyself = wl.sessionId === self.sessionId;
|
||||
|
||||
if (isMyself)
|
||||
return cb(false);
|
||||
|
||||
// Seconds remainding
|
||||
return cb(parseInt(-expiredSince / 1000));
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
WalletLock.prototype._setLock = function(cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
preconditions.checkState(this.sessionId);
|
||||
var self = this;
|
||||
|
||||
this.storage.setGlobal(this.key, {
|
||||
sessionId: this.sessionId,
|
||||
expireTs: Date.now() + this.timeoutMin * 60 * 1000,
|
||||
}, function() {
|
||||
|
||||
cb(null);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
WalletLock.prototype._doKeepAlive = function(cb) {
|
||||
preconditions.checkArgument(cb);
|
||||
preconditions.checkState(this.sessionId);
|
||||
|
||||
var self = this;
|
||||
|
||||
this._isLockedByOther(function(t) {
|
||||
if (t)
|
||||
return cb(new Error('LOCKED: Wallet is locked for ' + t + ' srcs'));
|
||||
|
||||
self._setLock(cb);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
WalletLock.prototype.keepAlive = function(cb) {
|
||||
var self = this;
|
||||
|
||||
if (!self.sessionId) {
|
||||
return self.init(self._doKeepAlive.bind(self, cb));
|
||||
};
|
||||
|
||||
return this._doKeepAlive(cb);
|
||||
};
|
||||
|
||||
|
||||
WalletLock.prototype.release = function(cb) {
|
||||
this.storage.removeGlobal(this.key, cb);
|
||||
};
|
||||
|
||||
|
||||
module.exports = WalletLock;
|
|
@ -121,14 +121,15 @@ describe('Identity model', function() {
|
|||
});
|
||||
|
||||
describe('Identity.create()', function() {
|
||||
it('should create', function() {
|
||||
it('should create and store identity', function() {
|
||||
var args = createIdentity();
|
||||
args.blockchain.on = sinon.stub();
|
||||
var old = Identity.prototype.createWallet;
|
||||
sinon.stub(Identity.prototype, 'store').yields(null);
|
||||
Identity.create(args.params, function(err, iden) {
|
||||
should.not.exist(err);
|
||||
should.exist(iden);
|
||||
should.exist(iden.wallets);
|
||||
Identity.prototype.store.calledOnce.should.be.true;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue