Skip to content

Commit

Permalink
Merge pull request #1123 from matiu/bug/backup1
Browse files Browse the repository at this point in the history
Bug/backup1
  • Loading branch information
maraoz committed Aug 18, 2014
2 parents 563da59 + c7d0441 commit 1a65435
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
21 changes: 13 additions & 8 deletions js/models/core/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1611,12 +1611,12 @@ Wallet.prototype.updateIndexes = function(callback) {
Wallet.prototype.updateIndex = function(index, callback) {
var self = this;
var SCANN_WINDOW = 20;
self.indexDiscovery(index.changeIndex, true, index.cosigner, SCANN_WINDOW, function(err, changeIndex) {
self.indexDiscovery(index.changeIndex, true, index.copayerIndex, SCANN_WINDOW, function(err, changeIndex) {
if (err) return callback(err);
if (changeIndex != -1)
index.changeIndex = changeIndex + 1;

self.indexDiscovery(index.receiveIndex, false, index.cosigner, SCANN_WINDOW, function(err, receiveIndex) {
self.indexDiscovery(index.receiveIndex, false, index.copayerIndex, SCANN_WINDOW, function(err, receiveIndex) {
if (err) return callback(err);
if (receiveIndex != -1)
index.receiveIndex = receiveIndex + 1;
Expand All @@ -1625,18 +1625,23 @@ Wallet.prototype.updateIndex = function(index, callback) {
});
}

Wallet.prototype.deriveAddresses = function(index, amout, isChange, cosigner) {
var ret = new Array(amout);
for (var i = 0; i < amout; i++) {
ret[i] = this.publicKeyRing.getAddress(index + i, isChange, cosigner).toString();
Wallet.prototype.deriveAddresses = function(index, amount, isChange, copayerIndex) {
preconditions.checkArgument(amount);
preconditions.shouldBeDefined(copayerIndex);

var ret = new Array(amount);
for (var i = 0; i < amount; i++) {
ret[i] = this.publicKeyRing.getAddress(index + i, isChange, copayerIndex).toString();
}
return ret;
}

// This function scans the publicKeyRing branch starting at index @start and reports the index with last activity,
// using a scan window of @gap. The argument @change defines the branch to scan: internal or external.
// Returns -1 if no activity is found in range.
Wallet.prototype.indexDiscovery = function(start, change, cosigner, gap, cb) {
Wallet.prototype.indexDiscovery = function(start, change, copayerIndex, gap, cb) {
preconditions.shouldBeDefined(copayerIndex);
preconditions.checkArgument(gap);
var scanIndex = start;
var lastActive = -1;
var hasActivity = false;
Expand All @@ -1646,7 +1651,7 @@ Wallet.prototype.indexDiscovery = function(start, change, cosigner, gap, cb) {
function _do(next) {
// Optimize window to minimize the derivations.
var scanWindow = (lastActive == -1) ? gap : gap - (scanIndex - lastActive) + 1;
var addresses = self.deriveAddresses(scanIndex, scanWindow, change, cosigner);
var addresses = self.deriveAddresses(scanIndex, scanWindow, change, copayerIndex);
self.blockchain.checkActivity(addresses, function(err, actives) {
if (err) throw err;

Expand Down
5 changes: 5 additions & 0 deletions test/test.Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,9 @@ describe('Wallet model', function() {
});

sinon.assert.callCount(updateIndex, 4);
sinon.assert.calledWith(updateIndex, w.publicKeyRing.indexes[0] );
sinon.assert.calledWith(updateIndex, w.publicKeyRing.indexes[1] );
sinon.assert.calledWith(updateIndex, w.publicKeyRing.indexes[2] );
w.updateIndex.restore();
done();
});
Expand All @@ -837,6 +840,8 @@ describe('Wallet model', function() {
index.receiveIndex.should.equal(9);
index.changeIndex.should.equal(9);
indexDiscovery.callCount.should.equal(2);
sinon.assert.calledWith(indexDiscovery, 1, true, 2, 20 );
sinon.assert.calledWith(indexDiscovery, 2, false, 2, 20 );
w.indexDiscovery.restore();
done();
});
Expand Down

0 comments on commit 1a65435

Please sign in to comment.