block: only cache block summary with height if confirmed >= 6

- update api with changes in bitcore node
- add block event
This commit is contained in:
Braydon Fuller 2016-04-11 10:39:46 -04:00
parent 9d78188f64
commit 19c5b617d1
3 changed files with 21 additions and 17 deletions

View File

@ -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);
});
});

View File

@ -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);

View File

@ -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) {