WIP changes for next version
This commit is contained in:
parent
95bcad836c
commit
c61490ed62
@ -27,6 +27,9 @@ function BlockController(options) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.common = new Common({log: this.node.log});
|
this.common = new Common({log: this.node.log});
|
||||||
|
this._block = this.node.services.block;
|
||||||
|
this._header = this.node.services.header;
|
||||||
|
this._timestamp = this.node.services.timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
var BLOCK_LIMIT = 200;
|
var BLOCK_LIMIT = 200;
|
||||||
@ -59,24 +62,31 @@ BlockController.prototype.block = function(req, res, next) {
|
|||||||
var blockCached = self.blockCache.get(hash);
|
var blockCached = self.blockCache.get(hash);
|
||||||
|
|
||||||
if (blockCached) {
|
if (blockCached) {
|
||||||
blockCached.confirmations = self.node.services.bitcoind.height - blockCached.height + 1;
|
var height = self._block.getTip().height;
|
||||||
|
blockCached.confirmations = height - blockCached.height + 1;
|
||||||
req.block = blockCached;
|
req.block = blockCached;
|
||||||
next();
|
next();
|
||||||
} else {
|
} else {
|
||||||
self.node.getBlock(hash, function(err, block) {
|
self._block.getBlock(hash, function(err, block) {
|
||||||
if((err && err.code === -5) || (err && err.code === -8)) {
|
if (err) {
|
||||||
return self.common.handleErrors(null, res);
|
|
||||||
} else if(err) {
|
|
||||||
return self.common.handleErrors(err, res);
|
return self.common.handleErrors(err, res);
|
||||||
}
|
}
|
||||||
self.node.services.bitcoind.getBlockHeader(hash, function(err, info) {
|
|
||||||
|
if (!block) {
|
||||||
|
return self.common.handleErrors(null, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
self._header.getBlockHeader(hash, function(err, info) {
|
||||||
|
console.log(info);
|
||||||
if (err) {
|
if (err) {
|
||||||
return self.common.handleErrors(err, res);
|
return self.common.handleErrors(err, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
var blockResult = self.transformBlock(block, info);
|
var blockResult = self.transformBlock(block, info);
|
||||||
if (blockResult.confirmations >= self.blockCacheConfirmations) {
|
if (blockResult.confirmations >= self.blockCacheConfirmations) {
|
||||||
self.blockCache.set(hash, blockResult);
|
self.blockCache.set(hash, blockResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
req.block = blockResult;
|
req.block = blockResult;
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
@ -158,7 +168,7 @@ BlockController.prototype.showRaw = function(req, res) {
|
|||||||
BlockController.prototype.blockIndex = function(req, res) {
|
BlockController.prototype.blockIndex = function(req, res) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var height = req.params.height;
|
var height = req.params.height;
|
||||||
this.node.services.bitcoind.getBlockHeader(parseInt(height), function(err, info) {
|
self._header.getBlockHeader(parseInt(height), function(err, info) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return self.common.handleErrors(err, res);
|
return self.common.handleErrors(err, res);
|
||||||
}
|
}
|
||||||
@ -183,7 +193,7 @@ BlockController.prototype._getBlockSummary = function(hash, moreTimestamp, next)
|
|||||||
if (summaryCache) {
|
if (summaryCache) {
|
||||||
finish(summaryCache);
|
finish(summaryCache);
|
||||||
} else {
|
} else {
|
||||||
self.node.services.bitcoind.getRawBlock(hash, function(err, blockBuffer) {
|
self._block.getRawBlock(hash, function(err, blockBuffer) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
@ -201,7 +211,7 @@ BlockController.prototype._getBlockSummary = function(hash, moreTimestamp, next)
|
|||||||
var txlength = br.readVarintNum();
|
var txlength = br.readVarintNum();
|
||||||
info.transactions = [bitcore.Transaction().fromBufferReader(br)];
|
info.transactions = [bitcore.Transaction().fromBufferReader(br)];
|
||||||
|
|
||||||
self.node.services.bitcoind.getBlockHeader(hash, function(err, blockHeader) {
|
self._header.getBlockHeader(hash, function(err, blockHeader) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
@ -216,7 +226,8 @@ BlockController.prototype._getBlockSummary = function(hash, moreTimestamp, next)
|
|||||||
poolInfo: self.getPoolInfo(info)
|
poolInfo: self.getPoolInfo(info)
|
||||||
};
|
};
|
||||||
|
|
||||||
var confirmations = self.node.services.bitcoind.height - height + 1;
|
var _height = self._block.getTip().height;
|
||||||
|
var confirmations = _height - height + 1;
|
||||||
if (confirmations >= self.blockCacheConfirmations) {
|
if (confirmations >= self.blockCacheConfirmations) {
|
||||||
self.blockSummaryCache.set(hash, summary);
|
self.blockSummaryCache.set(hash, summary);
|
||||||
}
|
}
|
||||||
@ -259,7 +270,7 @@ BlockController.prototype.list = function(req, res) {
|
|||||||
var more = false;
|
var more = false;
|
||||||
var moreTimestamp = lte;
|
var moreTimestamp = lte;
|
||||||
|
|
||||||
self.node.services.bitcoind.getBlockHashesByTimestamp(lte, gte, function(err, hashes) {
|
self._timestamp.getBlockHashesByTimestamp(lte, gte, function(err, hashes) {
|
||||||
if(err) {
|
if(err) {
|
||||||
return self.common.handleErrors(err, res);
|
return self.common.handleErrors(err, res);
|
||||||
}
|
}
|
||||||
|
|||||||
24
lib/index.js
24
lib/index.js
@ -61,7 +61,7 @@ var InsightAPI = function(options) {
|
|||||||
this.txController = new TxController(this.node);
|
this.txController = new TxController(this.node);
|
||||||
};
|
};
|
||||||
|
|
||||||
InsightAPI.dependencies = ['bitcoind', 'web'];
|
InsightAPI.dependencies = ['header', 'block', 'transaction', 'address', 'web', 'mempool', 'timestamp', 'fee'];
|
||||||
|
|
||||||
inherits(InsightAPI, BaseService);
|
inherits(InsightAPI, BaseService);
|
||||||
|
|
||||||
@ -90,9 +90,25 @@ InsightAPI.prototype.getRoutePrefix = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
InsightAPI.prototype.start = function(callback) {
|
InsightAPI.prototype.start = function(callback) {
|
||||||
this.node.services.bitcoind.on('tx', this.transactionEventHandler.bind(this));
|
|
||||||
this.node.services.bitcoind.on('block', this.blockEventHandler.bind(this));
|
if (this._subscribed) {
|
||||||
setImmediate(callback);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._subscribed = true;
|
||||||
|
|
||||||
|
if (!this._bus) {
|
||||||
|
this._bus = this.node.openBus({remoteAddress: 'localhost-insight-api'});
|
||||||
|
}
|
||||||
|
|
||||||
|
this._bus.on('block/block', this.transactionEventHandler.bind(this));
|
||||||
|
this._bus.subscribe('block/block');
|
||||||
|
|
||||||
|
this._bus.on('mempool/transaction', this.blockEventHandler.bind(this));
|
||||||
|
this._bus.subscribe('mempool/transaction');
|
||||||
|
|
||||||
|
callback();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
InsightAPI.prototype.createLogInfoStream = function() {
|
InsightAPI.prototype.createLogInfoStream = function() {
|
||||||
|
|||||||
@ -61,7 +61,7 @@
|
|||||||
"bitcoreNode": "lib",
|
"bitcoreNode": "lib",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"async": "*",
|
"async": "*",
|
||||||
"bitcore-lib": "^0.13.7",
|
"bitcore-lib": "bitpay/bitcore-lib#transitional",
|
||||||
"bitcore-message": "^1.0.1",
|
"bitcore-message": "^1.0.1",
|
||||||
"body-parser": "^1.13.3",
|
"body-parser": "^1.13.3",
|
||||||
"compression": "^1.6.1",
|
"compression": "^1.6.1",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user