diff --git a/lib/wallet/account.js b/lib/wallet/account.js index 865673b4..333b290d 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -218,7 +218,7 @@ Account.fromOptions = function fromOptions(db, options) { * @const {Number} */ -Account.MAX_LOOKAHEAD = 50; +Account.MAX_LOOKAHEAD = 40; /** * Attempt to intialize the account (generating @@ -726,15 +726,36 @@ Account.prototype.syncDepth = co(function* syncDepth(receive, change, nested) { */ Account.prototype.setLookahead = co(function* setLookahead(lookahead) { - var i, key, depth, target; + var i, diff, key, depth, target; - if (lookahead <= this.lookahead) { + if (lookahead === this.lookahead) { this.db.logger.warning( - 'Lookahead is not increasing for: %s/%s.', + 'Lookahead is not changing for: %s/%s.', this.id, this.name); return; } + if (lookahead < this.lookahead) { + diff = this.lookahead - lookahead; + + this.receiveDepth += diff; + this.receive = this.deriveReceive(this.receiveDepth - 1); + + this.changeDepth += diff; + this.change = this.deriveChange(this.changeDepth - 1); + + if (this.witness) { + this.nestedDepth += diff; + this.nested = this.deriveNested(this.nestedDepth - 1); + } + + this.lookahead = lookahead; + + this.save(); + + return; + } + depth = this.receiveDepth + this.lookahead target = this.receiveDepth + lookahead;