From 7d3eb8f3fa67580bd125beac83e3fc6cf5089d75 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 2 Nov 2016 16:46:25 -0700 Subject: [PATCH] account: lookahead option. --- lib/wallet/account.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/wallet/account.js b/lib/wallet/account.js index e90bbd9f..79c1f864 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -49,7 +49,6 @@ function Account(db, options) { this.db = db; this.network = db.network; this.wallet = null; - this.lookahead = Account.MAX_LOOKAHEAD; this.receive = null; this.change = null; @@ -68,6 +67,7 @@ function Account(db, options) { this.receiveDepth = 0; this.changeDepth = 0; this.nestedDepth = 0; + this.lookahead = 5; this.accountKey = null; this.keys = []; @@ -175,6 +175,13 @@ Account.prototype.fromOptions = function fromOptions(options) { this.nestedDepth = options.nestedDepth; } + if (options.lookahead != null) { + assert(utils.isNumber(options.lookahead)); + assert(options.lookahead >= 0); + assert(options.lookahead <= Account.MAX_LOOKAHEAD); + this.lookahead = options.lookahead; + } + this.accountKey = options.accountKey; if (this.n > 1) @@ -211,7 +218,7 @@ Account.fromOptions = function fromOptions(db, options) { * @const {Number} */ -Account.MAX_LOOKAHEAD = 10; +Account.MAX_LOOKAHEAD = 20; /** * Attempt to intialize the account (generating @@ -670,7 +677,7 @@ Account.prototype.syncDepth = co(function* syncDepth(receive, change, nested) { if (receive > this.receiveDepth) { depth = this.receiveDepth + this.lookahead; - assert(receive < depth); + assert(receive <= depth); for (i = depth; i < receive + this.lookahead; i++) { key = this.deriveReceive(i); @@ -687,7 +694,7 @@ Account.prototype.syncDepth = co(function* syncDepth(receive, change, nested) { if (change > this.changeDepth) { depth = this.changeDepth + this.lookahead; - assert(change < depth); + assert(change <= depth); for (i = depth; i < change + this.lookahead; i++) { key = this.deriveChange(i); @@ -703,7 +710,7 @@ Account.prototype.syncDepth = co(function* syncDepth(receive, change, nested) { if (this.witness && nested > this.nestedDepth) { depth = this.nestedDepth + this.lookahead; - assert(nested < depth); + assert(nested <= depth); for (i = depth; i < nested + this.lookahead; i++) { key = this.deriveNested(i); @@ -781,6 +788,7 @@ Account.prototype.inspect = function inspect() { receiveDepth: this.receiveDepth, changeDepth: this.changeDepth, nestedDepth: this.nestedDepth, + lookahead: this.lookahead, address: this.initialized ? this.receive.getAddress() : null, @@ -815,6 +823,7 @@ Account.prototype.toJSON = function toJSON(minimal) { receiveDepth: this.receiveDepth, changeDepth: this.changeDepth, nestedDepth: this.nestedDepth, + lookahead: this.lookahead, receiveAddress: this.receive ? this.receive.getAddress('base58') : null, @@ -850,6 +859,7 @@ Account.prototype.toRaw = function toRaw(writer) { p.writeU32(this.receiveDepth); p.writeU32(this.changeDepth); p.writeU32(this.nestedDepth); + p.writeU8(this.lookahead); p.writeBytes(this.accountKey.toRaw()); p.writeU8(this.keys.length); @@ -885,6 +895,7 @@ Account.prototype.fromRaw = function fromRaw(data) { this.receiveDepth = p.readU32(); this.changeDepth = p.readU32(); this.nestedDepth = p.readU32(); + this.lookahead = p.readU8(); this.accountKey = HD.fromRaw(p.readBytes(82)); assert(Account.typesByVal[this.type]);