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

View File

@ -1225,26 +1225,34 @@ WalletService.prototype.startScan = function(opts, cb) {
var self = this;
function scanFinished(err) {
var data = {};
if (err) {
data.result = 'error';
data.error = err;
} else {
data.result = 'success';
}
self._notify('ScanFinished', data, true);
var result = err ? 'error' : 'success';
var data = {
result: result,
};
if (err) data.error = err;
self.getWallet({}, function(err, wallet) {
wallet.scanStatus = result;
self.storage.storeWallet(wallet, function() {
self._notify('ScanFinished', data, true);
});
});
};
self.getWallet({}, function(err, wallet) {
if (err) return cb(err);
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() {
self.scan(opts, scanFinished);
}, 100);
setTimeout(function() {
self.scan(opts, scanFinished);
}, 100);
return cb(null, {
started: true
return cb(null, {
started: true
});
});
});
};

View File

@ -2669,18 +2669,42 @@ describe('Wallet service', function() {
];
WalletService.onNotification(function(n) {
if (n.type == 'ScanFinished') {
should.not.exist(n.creatorId);
server.storage.fetchAddresses(wallet.id, function(err, addresses) {
should.exist(addresses);
addresses.length.should.equal(expectedPaths.length);
var paths = _.pluck(addresses, 'path');
_.difference(paths, expectedPaths).length.should.equal(0);
server.createAddress({}, function(err, address) {
should.not.exist(err);
address.path.should.equal('m/2147483647/0/4');
done();
});
})
server.getWallet({}, function(err, wallet) {
should.exist(wallet.scanStatus);
wallet.scanStatus.should.equal('success');
should.not.exist(n.creatorId);
server.storage.fetchAddresses(wallet.id, function(err, addresses) {
should.exist(addresses);
addresses.length.should.equal(expectedPaths.length);
var paths = _.pluck(addresses, 'path');
_.difference(paths, expectedPaths).length.should.equal(0);
server.createAddress({}, function(err, address) {
should.not.exist(err);
address.path.should.equal('m/2147483647/0/4');
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) {