Merge pull request #180 from isocolsky/scanning

Scanning
This commit is contained in:
Matias Alejo Garcia 2015-04-14 16:04:50 -03:00
commit 949b45fc9d
3 changed files with 59 additions and 25 deletions

View File

@ -32,6 +32,7 @@ Wallet.create = function(opts) {
x.pubKey = opts.pubKey; x.pubKey = opts.pubKey;
x.network = opts.network; x.network = opts.network;
x.addressManager = AddressManager.create(); x.addressManager = AddressManager.create();
x.scanStatus = null;
return x; return x;
}; };
@ -53,6 +54,7 @@ Wallet.fromObj = function(obj) {
x.pubKey = obj.pubKey; x.pubKey = obj.pubKey;
x.network = obj.network; x.network = obj.network;
x.addressManager = AddressManager.fromObj(obj.addressManager); x.addressManager = AddressManager.fromObj(obj.addressManager);
x.scanStatus = obj.scanStatus;
return x; return x;
}; };

View File

@ -1225,19 +1225,26 @@ WalletService.prototype.startScan = function(opts, cb) {
var self = this; var self = this;
function scanFinished(err) { function scanFinished(err) {
var data = {}; var result = err ? 'error' : 'success';
if (err) { var data = {
data.result = 'error'; result: result,
data.error = err; };
} else { if (err) data.error = err;
data.result = 'success';
} self.getWallet({}, function(err, wallet) {
wallet.scanStatus = result;
self.storage.storeWallet(wallet, function() {
self._notify('ScanFinished', data, true); self._notify('ScanFinished', data, true);
});
});
}; };
self.getWallet({}, function(err, wallet) { self.getWallet({}, function(err, wallet) {
if (err) return cb(err); if (err) return cb(err);
if (!wallet.isComplete()) return cb(new ClientError('Wallet is not complete')); if (!wallet.isComplete()) return cb(new ClientError('Wallet is not complete'));
wallet.scanStatus = 'running';
self.storage.storeWallet(wallet, function(err) {
if (err) return cb(err);
setTimeout(function() { setTimeout(function() {
self.scan(opts, scanFinished); self.scan(opts, scanFinished);
@ -1247,6 +1254,7 @@ WalletService.prototype.startScan = function(opts, cb) {
started: true started: true
}); });
}); });
});
}; };

View File

@ -2669,6 +2669,9 @@ describe('Wallet service', function() {
]; ];
WalletService.onNotification(function(n) { WalletService.onNotification(function(n) {
if (n.type == 'ScanFinished') { if (n.type == 'ScanFinished') {
server.getWallet({}, function(err, wallet) {
should.exist(wallet.scanStatus);
wallet.scanStatus.should.equal('success');
should.not.exist(n.creatorId); should.not.exist(n.creatorId);
server.storage.fetchAddresses(wallet.id, function(err, addresses) { server.storage.fetchAddresses(wallet.id, function(err, addresses) {
should.exist(addresses); should.exist(addresses);
@ -2681,6 +2684,27 @@ describe('Wallet service', function() {
done(); done();
}); });
}) })
});
}
});
server.startScan({}, function(err) {
should.not.exist(err);
server.getWallet({}, function(err, wallet) {
should.exist(wallet.scanStatus);
wallet.scanStatus.should.equal('running');
});
});
});
it('should set scan status error when unable to reach blockchain', function(done) {
blockchainExplorer.getAddressActivity = sinon.stub().yields('dummy error');
WalletService.onNotification(function(n) {
if (n.type == 'ScanFinished') {
should.exist(n.data.error);
server.getWallet({}, function(err, wallet) {
should.exist(wallet.scanStatus);
wallet.scanStatus.should.equal('error');
done();
});
} }
}); });
server.startScan({}, function(err) { server.startScan({}, function(err) {