diff --git a/lib/blocks.js b/lib/blocks.js index 96a7edf..3463748 100644 --- a/lib/blocks.js +++ b/lib/blocks.js @@ -13,6 +13,7 @@ function BlockController(node) { this.node = node; this.blockSummaryCache = LRU(1000000); + this.blockSummaryCacheConfirmations = 6; this.blockCache = LRU(1000); this.poolStrings = {}; @@ -125,11 +126,12 @@ BlockController.prototype._getBlockSummary = function(hash, moreTs, next) { finish(summaryCache); } else { self.node.services.bitcoind.getRawBlock(hash, function(err, blockBuffer) { - if(err) { + if (err) { return next(err); } var br = new bitcore.encoding.BufferReader(blockBuffer); + // take a shortcut to get number of transactions and the blocksize. // Also reads the coinbase transaction and only that. // Old code parsed all transactions in every block _and_ then encoded @@ -142,9 +144,13 @@ BlockController.prototype._getBlockSummary = function(hash, moreTs, next) { info.transactions = [bitcore.Transaction().fromBufferReader(br)]; self.node.services.bitcoind.getBlockHeader(hash, function(err, blockHeader) { + if (err) { + return next(err); + } + var height = blockHeader.height; - var block = { - height: blockHeader.height, + var summary = { + height: height, size: blockBuffer.length, hash: hash, time: header.time, @@ -152,9 +158,12 @@ BlockController.prototype._getBlockSummary = function(hash, moreTs, next) { poolInfo: self.getPoolInfo(info) }; - self.blockSummaryCache.set(hash, summary); + var confirmations = self.node.services.bitcoind.height - height + 1; + if (confirmations >= self.blockSummaryCacheConfirmations) { + self.blockSummaryCache.set(hash, summary); + } - finish(block); + finish(summary); }); }); diff --git a/lib/index.js b/lib/index.js index a5d82a1..5a915e4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -84,8 +84,8 @@ InsightAPI.prototype.getRoutePrefix = function() { }; InsightAPI.prototype.start = function(callback) { - this.node.services.bitcoind.on('tx', this.transactionHandler.bind(this)); - + this.node.services.bitcoind.on('tx', this.transactionEventHandler.bind(this)); + this.node.services.bitcoind.on('block', this.blockEventHandler.bind(this)); setImmediate(callback); }; @@ -213,18 +213,13 @@ InsightAPI.prototype.getPublishEvents = function() { ]; }; -InsightAPI.prototype.blockHandler = function(block, add, callback) { +InsightAPI.prototype.blockEventHandler = function(hashBuffer) { // Notify inv subscribers for (var i = 0; i < this.subscriptions.inv.length; i++) { - this.subscriptions.inv[i].emit('block', block.hash); + this.subscriptions.inv[i].emit('block', hashBuffer.toString('hex')); } - - setImmediate(function() { - callback(null, []); - }); }; - -InsightAPI.prototype.transactionHandler = function(txBuffer) { +InsightAPI.prototype.transactionEventHandler = function(txBuffer) { var tx = new Transaction().fromBuffer(txBuffer); var result = this.txController.transformInvTransaction(tx); diff --git a/lib/transactions.js b/lib/transactions.js index dbcf5a1..657caa6 100644 --- a/lib/transactions.js +++ b/lib/transactions.js @@ -24,7 +24,7 @@ TxController.prototype.show = function(req, res) { TxController.prototype.transaction = function(req, res, next, txid) { var self = this; - this.node.getTransactionWithBlockInfo(txid, true, function(err, transaction) { + this.node.getTransactionWithBlockInfo(txid, function(err, transaction) { if (err && err instanceof self.node.errors.Transaction.NotFound) { return common.handleErrors(null, res); } else if(err) { @@ -231,7 +231,7 @@ TxController.prototype.transformInvTransaction = function(transaction) { TxController.prototype.rawTransaction = function(req, res, next, txid) { var self = this; - this.node.getTransaction(txid, true, function(err, transaction) { + this.node.getTransaction(txid, function(err, transaction) { if (err && err instanceof self.node.errors.Transaction.NotFound) { return common.handleErrors(null, res); } else if(err) {