From 7a2c8a933fa7b879c97eb3fbc6e3d5f8c61e591e Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Sat, 10 May 2014 20:32:37 +0400 Subject: [PATCH] chain: set height properly --- lib/bcoin/chain.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index ec1c5e62..839a8c39 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -94,8 +94,6 @@ Chain.prototype._getRange = function _getRange(hash, ts, futureOnly) { Chain.prototype._probeIndex = function _probeIndex(hash, ts) { if (!this.index.bloom.test(hash, 'hex')) return false; - if (!this.strict) - return true; var start = 0; var end = this.index.ts.length; @@ -107,7 +105,7 @@ Chain.prototype._probeIndex = function _probeIndex(hash, ts) { for (var i = start; i <= end; i++) if (this.index.hashes[i] === hash) - return true; + return { height: this.index.heights[i], ts: this.index.ts[i] }; return false; }; @@ -127,11 +125,8 @@ Chain.prototype._addIndex = function _addIndex(hash, ts, height) { this.index.ts.splice(pos, 0, ts); this.index.hashes.splice(pos, 0, hash); - this.index.bloom.add(hash, 'hex'); - assert(pos > 0); - if (!height) - height = this.index.heights[pos - 1] + 1; this.index.heights.splice(pos, 0, height); + this.index.bloom.add(hash, 'hex'); if (!this.storage) return; @@ -166,9 +161,10 @@ Chain.prototype.add = function add(block) { if (this.orphan.map[prev]) break; + var prevProbe = this._probeIndex(prev, block.ts); + // If previous block wasn't ever seen - add current to orphans - if (!this._probeIndex(hash, block.ts) && - !this._probeIndex(prev, block.ts)) { + if (!this._probeIndex(hash, block.ts) && !prevProbe) { this.orphan.count++; this.orphan.map[prev] = block; @@ -179,7 +175,8 @@ Chain.prototype.add = function add(block) { } // Validated known block at this point - add it to index - this._addIndex(hash, block.ts); + if (prevProbe) + this._addIndex(hash, block.ts, prevProbe.height + 1); // At least one block was added res = true;