blocks: cache block header info with next block with 6 confirmations

This commit is contained in:
Braydon Fuller 2016-04-20 10:54:10 -04:00
parent b686c5bea9
commit e1df171f95
2 changed files with 14 additions and 13 deletions

View File

@ -13,7 +13,7 @@ function BlockController(node) {
this.node = node; this.node = node;
this.blockSummaryCache = LRU(1000000); this.blockSummaryCache = LRU(1000000);
this.blockSummaryCacheConfirmations = 6; this.blockCacheConfirmations = 6;
this.blockCache = LRU(1000); this.blockCache = LRU(1000);
this.poolStrings = {}; this.poolStrings = {};
@ -35,16 +35,12 @@ var BLOCK_LIMIT = 200;
BlockController.prototype.block = function(req, res, next, hash) { BlockController.prototype.block = function(req, res, next, hash) {
var self = this; var self = this;
function finish(blockResult) { var blockCached = self.blockCache.get(hash);
blockResult.confirmations = self.node.services.bitcoind.height - blockResult.height + 1;
req.block = blockResult; if (blockCached) {
blockCached.confirmations = self.node.services.bitcoind.height - blockCached.height + 1;
req.block = blockCached;
next(); next();
}
var block = self.blockCache.get(hash);
if (block) {
finish(block);
} else { } else {
self.node.getBlock(hash, function(err, block) { self.node.getBlock(hash, function(err, block) {
if(err && err.code === -5) { if(err && err.code === -5) {
@ -57,8 +53,11 @@ BlockController.prototype.block = function(req, res, next, hash) {
return common.handleErrors(err, res); return common.handleErrors(err, res);
} }
var blockResult = self.transformBlock(block, info); var blockResult = self.transformBlock(block, info);
self.blockCache.set(hash, blockResult); if (blockResult.confirmations >= self.blockCacheConfirmations) {
finish(blockResult); 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), bits: blockObj.header.bits.toString(16),
difficulty: block.header.getDifficulty(), difficulty: block.header.getDifficulty(),
chainwork: info.chainwork, chainwork: info.chainwork,
confirmations: info.confirmations,
previousblockhash: this._normalizePrevHash(blockObj.header.prevHash), previousblockhash: this._normalizePrevHash(blockObj.header.prevHash),
nextblockhash: info.nextblockhash, nextblockhash: info.nextblockhash,
reward: this.getBlockReward(info.height) / 1e8, 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; var confirmations = self.node.services.bitcoind.height - height + 1;
if (confirmations >= self.blockSummaryCacheConfirmations) { if (confirmations >= self.blockCacheConfirmations) {
self.blockSummaryCache.set(hash, summary); self.blockSummaryCache.set(hash, summary);
} }

View File

@ -14,6 +14,7 @@ var blockIndexes = {
chainwork: '0000000000000000000000000000000000000000000000054626b1839ade284a', chainwork: '0000000000000000000000000000000000000000000000054626b1839ade284a',
previousblockhash: '00000000000001a55f3214e9172eb34b20e0bc5bd6b8007f3f149fca2c8991a4', previousblockhash: '00000000000001a55f3214e9172eb34b20e0bc5bd6b8007f3f149fca2c8991a4',
nextblockhash: '000000000001e866a8057cde0c650796cb8a59e0e6038dc31c69d7ca6649627d', nextblockhash: '000000000001e866a8057cde0c650796cb8a59e0e6038dc31c69d7ca6649627d',
confirmations: 119,
height: 533974 height: 533974
}, },
'000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7': { '000000000008fbb2e358e382a6f6948b2da24563bba183af447e6e2542e8efc7': {