From 312bc2eece7bbf737c1699d3f9a9d17d6b6bd8e7 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Fri, 17 Apr 2015 17:39:57 -0300 Subject: [PATCH 1/3] test scan not saving last address block --- test/integration/server.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/test/integration/server.js b/test/integration/server.js index 0db5f9e..4bc9621 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -2519,7 +2519,7 @@ describe('Wallet service', function() { }); }); - describe('#scan', function() { + describe.only('#scan', function() { var server, wallet; var scanConfigOld = WalletService.scanConfig; beforeEach(function(done) { @@ -2538,7 +2538,11 @@ describe('Wallet service', function() { }); it('should scan main addresses', function(done) { - helpers.stubAddressActivity(['3K2VWMXheGZ4qG35DyGjA2dLeKfaSr534A']); + helpers.stubAddressActivity( + ['3K2VWMXheGZ4qG35DyGjA2dLeKfaSr534A', // 'm/2147483647/0/0' + '3NezgtNbuDzL2sFhnfxyVy8bHp4v6ud252', // 'm/2147483647/0/2' + '3CQ2hCMUu1SCPVPMpfCCuT3nAfHGiHV1o7', // 'm/2147483647/1/0' + ]); var expectedPaths = [ 'm/2147483647/0/0', 'm/2147483647/0/1', @@ -2563,18 +2567,20 @@ describe('Wallet service', function() { }); }); it('should scan main addresses & copayer addresses', function(done) { - helpers.stubAddressActivity(['3K2VWMXheGZ4qG35DyGjA2dLeKfaSr534A']); + helpers.stubAddressActivity( + ['3K2VWMXheGZ4qG35DyGjA2dLeKfaSr534A', // 'm/2147483647/0/0' + '3CQ2hCMUu1SCPVPMpfCCuT3nAfHGiHV1o7', // 'm/2147483647/1/0' + '3BYHznBmosYxUj1NWcjdFKX2tdsH7UT1YG', // 'm/0/0/1' + '3Eg1uPkGnwyU42bRiaDuo6Cu9bjjhoG7Sh', // 'm/1/1/0' + '3AYmZ63tMd2AHN8QLfu5D2nfRzCH66psWx', // 'm/1/0/0' + ]); var expectedPaths = [ 'm/2147483647/0/0', 'm/2147483647/0/1', - 'm/2147483647/0/2', - 'm/2147483647/0/3', 'm/2147483647/1/0', 'm/2147483647/1/1', 'm/0/0/0', 'm/0/0/1', - 'm/0/1/0', - 'm/0/1/1', 'm/1/0/0', 'm/1/0/1', 'm/1/1/0', From 1a615e5bb5d0d22e81d777da71be566bd271b4db Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Fri, 17 Apr 2015 18:25:41 -0300 Subject: [PATCH 2/3] discard blocks without activity --- lib/model/addressmanager.js | 9 +++++++++ lib/server.js | 22 ++++++++++++++++------ test/integration/server.js | 24 ++++++++++++++---------- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/lib/model/addressmanager.js b/lib/model/addressmanager.js index bca94f6..48161d1 100644 --- a/lib/model/addressmanager.js +++ b/lib/model/addressmanager.js @@ -38,6 +38,15 @@ AddressManager.prototype._incrementIndex = function(isChange) { } }; +AddressManager.prototype.rewindIndex = function(isChange, n) { + n = _.isUndefined(n) ? 1 : n; + if (isChange) { + this.changeAddressIndex -= n; + } else { + this.receiveAddressIndex -= n; + } +}; + AddressManager.prototype.getCurrentAddressPath = function(isChange) { return 'm/' + this.copayerIndex + '/' + diff --git a/lib/server.js b/lib/server.js index cf9bc16..0125b63 100644 --- a/lib/server.js +++ b/lib/server.js @@ -432,9 +432,9 @@ WalletService.prototype.verifyMessageSignature = function(opts, cb) { WalletService.prototype._getBlockchainExplorer = function(provider, network) { if (!this.blockchainExplorer) { - var opts = {}; + var opts = {}; if (this.blockchainExplorerOpts && this.blockchainExplorerOpts[network]) { - opts = this.blockchainExplorerOpts[network]; + opts = this.blockchainExplorerOpts[network]; } opts.provider = provider; opts.network = network; @@ -1164,7 +1164,7 @@ WalletService.prototype.scan = function(opts, cb) { function deriveAddresses(size, derivator, cb) { async.mapSeries(_.range(size), function(i, next) { setTimeout(function() { - next(null, derivator()); + next(null, derivator.derive()); }, WalletService.scanConfig.DERIVATION_DELAY) }, cb); }; @@ -1184,10 +1184,14 @@ WalletService.prototype.scan = function(opts, cb) { deriveAddresses(WalletService.scanConfig.SCAN_WINDOW, derivator, function(err, addresses) { if (err) return next(err); networkName = networkName || Bitcore.Address(addresses[0].address).toObject().network; - allAddresses.push(addresses); checkActivity(_.pluck(addresses, 'address'), networkName, function(err, thereIsActivity) { if (err) return next(new Error('Could not fetch TX activity:' + err)); activity = thereIsActivity; + if (thereIsActivity) { + allAddresses.push(addresses); + } else { + derivator.rewind(WalletService.scanConfig.SCAN_WINDOW); + } next(); }); }); @@ -1208,10 +1212,16 @@ WalletService.prototype.scan = function(opts, cb) { var derivators = []; _.each([false, true], function(isChange) { - derivators.push(_.bind(wallet.createAddress, wallet, isChange)); + derivators.push({ + derive: _.bind(wallet.createAddress, wallet, isChange), + rewind: _.bind(wallet.addressManager.rewindIndex, wallet.addressManager, isChange), + }); if (opts.includeCopayerBranches) { _.each(wallet.copayers, function(copayer) { - derivators.push(_.bind(copayer.createAddress, copayer, wallet, isChange)); + derivators.push({ + derive: _.bind(copayer.createAddress, copayer, wallet, isChange), + rewind: _.bind(copayer.addressManager.rewindIndex, copayer.addressManager, isChange), + }); }); } }); diff --git a/test/integration/server.js b/test/integration/server.js index 4bc9621..9e3c728 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -2519,7 +2519,7 @@ describe('Wallet service', function() { }); }); - describe.only('#scan', function() { + describe('#scan', function() { var server, wallet; var scanConfigOld = WalletService.scanConfig; beforeEach(function(done) { @@ -2539,9 +2539,9 @@ describe('Wallet service', function() { it('should scan main addresses', function(done) { helpers.stubAddressActivity( - ['3K2VWMXheGZ4qG35DyGjA2dLeKfaSr534A', // 'm/2147483647/0/0' - '3NezgtNbuDzL2sFhnfxyVy8bHp4v6ud252', // 'm/2147483647/0/2' - '3CQ2hCMUu1SCPVPMpfCCuT3nAfHGiHV1o7', // 'm/2147483647/1/0' + ['3K2VWMXheGZ4qG35DyGjA2dLeKfaSr534A', // m/2147483647/0/0 + '3NezgtNbuDzL2sFhnfxyVy8bHp4v6ud252', // m/2147483647/0/2 + '3CQ2hCMUu1SCPVPMpfCCuT3nAfHGiHV1o7', // m/2147483647/1/0 ]); var expectedPaths = [ 'm/2147483647/0/0', @@ -2568,11 +2568,11 @@ describe('Wallet service', function() { }); it('should scan main addresses & copayer addresses', function(done) { helpers.stubAddressActivity( - ['3K2VWMXheGZ4qG35DyGjA2dLeKfaSr534A', // 'm/2147483647/0/0' - '3CQ2hCMUu1SCPVPMpfCCuT3nAfHGiHV1o7', // 'm/2147483647/1/0' - '3BYHznBmosYxUj1NWcjdFKX2tdsH7UT1YG', // 'm/0/0/1' - '3Eg1uPkGnwyU42bRiaDuo6Cu9bjjhoG7Sh', // 'm/1/1/0' - '3AYmZ63tMd2AHN8QLfu5D2nfRzCH66psWx', // 'm/1/0/0' + ['3K2VWMXheGZ4qG35DyGjA2dLeKfaSr534A', // m/2147483647/0/0 + '3CQ2hCMUu1SCPVPMpfCCuT3nAfHGiHV1o7', // m/2147483647/1/0 + '3BYHznBmosYxUj1NWcjdFKX2tdsH7UT1YG', // m/0/0/1 + '3Eg1uPkGnwyU42bRiaDuo6Cu9bjjhoG7Sh', // m/1/1/0 + '3AYmZ63tMd2AHN8QLfu5D2nfRzCH66psWx', // m/1/0/0 ]); var expectedPaths = [ 'm/2147483647/0/0', @@ -2664,7 +2664,11 @@ describe('Wallet service', function() { }); it('should start an asynchronous scan', function(done) { - helpers.stubAddressActivity(['3K2VWMXheGZ4qG35DyGjA2dLeKfaSr534A']); + helpers.stubAddressActivity([ + '3K2VWMXheGZ4qG35DyGjA2dLeKfaSr534A', // m/2147483647/0/0 + '3NezgtNbuDzL2sFhnfxyVy8bHp4v6ud252', // m/2147483647/0/2 + '3CQ2hCMUu1SCPVPMpfCCuT3nAfHGiHV1o7', // m/2147483647/1/1 + ]); var expectedPaths = [ 'm/2147483647/0/0', 'm/2147483647/0/1', From be0565fca05fd232ce8ab04adeedf5dd8ca6c91d Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Fri, 17 Apr 2015 18:31:47 -0300 Subject: [PATCH 3/3] test rewind --- lib/model/addressmanager.js | 4 ++-- test/models/addressmanager.js | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/model/addressmanager.js b/lib/model/addressmanager.js index 48161d1..6d9d66f 100644 --- a/lib/model/addressmanager.js +++ b/lib/model/addressmanager.js @@ -41,9 +41,9 @@ AddressManager.prototype._incrementIndex = function(isChange) { AddressManager.prototype.rewindIndex = function(isChange, n) { n = _.isUndefined(n) ? 1 : n; if (isChange) { - this.changeAddressIndex -= n; + this.changeAddressIndex = Math.max(0, this.changeAddressIndex - n); } else { - this.receiveAddressIndex -= n; + this.receiveAddressIndex = Math.max(0, this.receiveAddressIndex - n); } }; diff --git a/test/models/addressmanager.js b/test/models/addressmanager.js index 31236d3..eea0286 100644 --- a/test/models/addressmanager.js +++ b/test/models/addressmanager.js @@ -33,4 +33,28 @@ describe('AddressManager', function() { am.getNewAddressPath(true).should.equal('m/2/1/0'); }); }); + describe('#rewindIndex', function() { + it('should rewind main index', function() { + var am = AddressManager.create({}); + am.getNewAddressPath(false).should.equal('m/2147483647/0/0'); + am.getNewAddressPath(false).should.equal('m/2147483647/0/1'); + am.getNewAddressPath(false).should.equal('m/2147483647/0/2'); + am.rewindIndex(false, 2); + am.getNewAddressPath(false).should.equal('m/2147483647/0/1'); + }); + it('should rewind change index', function() { + var am = AddressManager.create({}); + am.getNewAddressPath(true).should.equal('m/2147483647/1/0'); + am.rewindIndex(false, 1); + am.getNewAddressPath(true).should.equal('m/2147483647/1/1'); + am.rewindIndex(true, 2); + am.getNewAddressPath(true).should.equal('m/2147483647/1/0'); + }); + it('should stop at 0', function() { + var am = AddressManager.create({}); + am.getNewAddressPath(false).should.equal('m/2147483647/0/0'); + am.rewindIndex(false, 20); + am.getNewAddressPath(false).should.equal('m/2147483647/0/0'); + }); + }); });