diff --git a/lib/blocks.js b/lib/blocks.js index 565965b..71eb7de 100644 --- a/lib/blocks.js +++ b/lib/blocks.js @@ -13,7 +13,7 @@ function BlockController(node) { this.node = node; this.blockSummaryCache = LRU(1000000); - this.blockSummaryCacheConfirmations = 6; + this.blockCacheConfirmations = 6; this.blockCache = LRU(1000); this.poolStrings = {}; @@ -35,16 +35,12 @@ var BLOCK_LIMIT = 200; BlockController.prototype.block = function(req, res, next, hash) { var self = this; - function finish(blockResult) { - blockResult.confirmations = self.node.services.bitcoind.height - blockResult.height + 1; - req.block = blockResult; + var blockCached = self.blockCache.get(hash); + + if (blockCached) { + blockCached.confirmations = self.node.services.bitcoind.height - blockCached.height + 1; + req.block = blockCached; next(); - } - - var block = self.blockCache.get(hash); - - if (block) { - finish(block); } else { self.node.getBlock(hash, function(err, block) { if(err && err.code === -5) { @@ -57,8 +53,11 @@ BlockController.prototype.block = function(req, res, next, hash) { return common.handleErrors(err, res); } var blockResult = self.transformBlock(block, info); - self.blockCache.set(hash, blockResult); - finish(blockResult); + if (blockResult.confirmations >= self.blockCacheConfirmations) { + self.blockCache.set(hash, blockResult); + } + req.block = blockResult; + next(); }); }); } @@ -110,6 +109,7 @@ BlockController.prototype.transformBlock = function(block, info) { bits: blockObj.header.bits.toString(16), difficulty: block.header.getDifficulty(), chainwork: info.chainwork, + confirmations: info.confirmations, previousblockhash: this._normalizePrevHash(blockObj.header.prevHash), nextblockhash: info.nextblockhash, reward: this.getBlockReward(info.height) / 1e8, @@ -193,7 +193,7 @@ BlockController.prototype._getBlockSummary = function(hash, moreTs, next) { }; var confirmations = self.node.services.bitcoind.height - height + 1; - if (confirmations >= self.blockSummaryCacheConfirmations) { + if (confirmations >= self.blockCacheConfirmations) { self.blockSummaryCache.set(hash, summary); } diff --git a/test/blocks.js b/test/blocks.js index e1ee5d2..d670597 100644 --- a/test/blocks.js +++ b/test/blocks.js @@ -14,6 +14,7 @@ var blockIndexes = { chainwork: '0000000000000000000000000000000000000000000000054626b1839ade284a', previousblockhash: '00000000000001a55f3214e9172eb34b20e0bc5bd6b8007f3f149fca2c8991a4', nextblockhash: '000000000001e866a8057cde0c650796cb8a59e0e6038dc31c69d7ca6649627d', + confirmations: 119, height: 533974 }, '000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7': {