diff --git a/lib/services/block/index.js b/lib/services/block/index.js index 072721d1..b174aabf 100644 --- a/lib/services/block/index.js +++ b/lib/services/block/index.js @@ -209,13 +209,13 @@ BlockService.prototype._detectInitialChainState = function(headers) { } var index = this._tip.height - 1; - var record = Array.from(headers)[index]; + var record = headers.getIndex(index); - if (record[0] !== this._tip.hash) { + if (record.hash !== this._tip.hash) { // reorg! we don't yet have the blocks to reorg to, so we'll rewind the chain back to // to common ancestor, set the tip to the common ancestor and start the sync - this._chainTips.push(record[0]); - this._handleReorg(record[0]); + this._chainTips.push(record.hash); + this._handleReorg(record.hash); } }; @@ -413,7 +413,7 @@ BlockService.prototype._getHash = function(blockArg) { var headers = this._header.getAllHeaders(); if (utils.isHeight(blockArg)) { - return Array.from(headers)[blockArg]; + return headers.getIndex(blockArg).hash; } }; @@ -439,7 +439,7 @@ BlockService.prototype._getOldBlocks = function(currentHash, commonAncestorHash) var oldBlocks; var headers = this._header.getAllHeaders(); for(var i = headers.length - 1; i > 0; --i) { - var item = headers[i]; + var item = headers.getIndex(i); if (item.hash === currentHash) { oldBlocks = [this._blockQueue.get(currentHash)]; continue; @@ -695,7 +695,7 @@ BlockService.prototype._sync = function() { log.info('Blocks download progress: ' + this._tip.height + '/' + this._numNeeded + ' (' + (this._tip.height / this._numNeeded*100).toFixed(2) + '%)'); - var endHash = Array.from(headers)[ Math.min(this._tip.height + BlockService.MAX_BLOCKS, size) ][0]; + var endHash = headers.getIndex( Math.min(this._tip.height + BlockService.MAX_BLOCKS, size) ).hash; this._p2p.getBlocks({ startHash: this._tip.hash, endHash: endHash }); return; diff --git a/lib/services/header/index.js b/lib/services/header/index.js index 6630fb5b..ea2840ce 100644 --- a/lib/services/header/index.js +++ b/lib/services/header/index.js @@ -17,7 +17,7 @@ var HeaderService = function(options) { this._tip = null; this._p2p = this.node.services.p2p; this._db = this.node.services.db; - this._headers = new Map(); + this._headers = new utils.SimpleMap(); }; inherits(HeaderService, BaseService); @@ -43,8 +43,8 @@ HeaderService.prototype.getAPIMethods = function() { HeaderService.prototype.getBlockHeader = function(arg) { if (utils.isHeight(arg)) { - var header = Array.from(this._headers)[arg]; - return header ? header[1] : null; + var header = this._headers.getIndex(arg); + return header ? header : null; } return this._headers.get(arg); @@ -133,7 +133,7 @@ HeaderService.prototype._onHeaders = function(headers, convert) { } var runningHeight = this._tip.height; - var prevHeader = Array.from(this._headers)[this._headers.length - 1]; + var prevHeader = this._headers.getLastIndex(); for(var i = 0; i < headers.length; i++) { var header = headers[i]; diff --git a/lib/utils.js b/lib/utils.js index 1c15a30b..70a37c15 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -199,9 +199,36 @@ utils.encodeTip = function(tip, name) { }; -utils.isHeight = function(arg) { +utils.isHeight = function(blockArg) { return _.isNumber(blockArg) || (blockArg.length < 40 && /^[0-9]+$/.test(blockArg)) }; +utils.SimpleMap = function SimpleMap() { + var object = {}; + var array = []; + + this.size = 0; + this.length = 0; + + this.get = function (key) { + return array[object[key]]; + }; + + this.set = function (key, value) { + object[key] = array.length; + array.push(value); + + this.size = array.length; + this.length = array.length; + }; + + this.getIndex = function (index) { + return array[index]; + }; + + this.getLastIndex = function () { + return array[array.length - 1]; + }; +}; module.exports = utils;