diff --git a/lib/blocks.js b/lib/blocks.js index 4bb4ede..1a4dfbb 100644 --- a/lib/blocks.js +++ b/lib/blocks.js @@ -125,9 +125,11 @@ BlockController.prototype._normalizePrevHash = function(hash) { }; BlockController.prototype.transformBlock = function(block, info) { + var transactionIds = block.txs.map(function(tx) { return tx.txid(); }); + return { hash: block.rhash(), size: block.size, @@ -139,14 +141,14 @@ BlockController.prototype.transformBlock = function(block, info) { time: block.ts, nonce: block.nonce, bits: block.bits, - difficulty: null, + difficulty: this._header.getDifficulty(), chainwork: info.chainwork, - confirmations: null, - previousblockhash: bcoin.util.revHex(block.prevBlock), + confirmations: this._block.getTip().height - info.height + 1, + previousblockhash: info.prevHash, nextblockhash: null, reward: null, - isMainChain: null, - poolInfo: this.getPoolInfo(block) + isMainChain: true, + poolInfo: this.getPoolInfo(block.txs[0]) }; }; @@ -194,36 +196,28 @@ BlockController.prototype._getBlockSummary = function(hash, moreTimestamp, next) finish(summaryCache); } else { self._block.getRawBlock(hash, function(err, blockBuffer) { + if (err) { return next(err); } - var br = new bitcore.encoding.BufferReader(blockBuffer); + var block = bcoin.block.fromRaw(blockBuffer, 'hex'); - // TODO: 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 - // them all back together to get the binary size of the block. - // FIXME: This code might still read the whole block. Fixing that - // would require changes in bitcore-node. - var header = bitcore.BlockHeader.fromBufferReader(br); - var info = {}; - var txlength = br.readVarintNum(); - info.transactions = [bitcore.Transaction().fromBufferReader(br)]; + self._header.getBlockHeader(hash, function(err, header) { - self._header.getBlockHeader(hash, function(err, blockHeader) { if (err) { return next(err); } - var height = blockHeader.height; + var height = header.height; var summary = { - height: height, - size: blockBuffer.length, + height: header.height, + size: block.size, + virtualSize: block.virtualSize, hash: hash, - time: header.time, - txlength: txlength, - poolInfo: self.getPoolInfo(info) + time: header.timestamp, + txlength: block.txs.length, + poolInfo: self.getPoolInfo(block.txs[0]) }; var _height = self._block.getTip().height; @@ -319,8 +313,11 @@ BlockController.prototype.list = function(req, res) { }); }; -BlockController.prototype.getPoolInfo = function(block) { - var coinbaseBuffer = block.txs[0].inputs[0].script.raw; +BlockController.prototype.getPoolInfo = function(tx) { + if (!tx) { + return {}; + } + var coinbaseBuffer = tx.inputs[0].script.raw; for(var k in this.poolStrings) { if (coinbaseBuffer.toString('utf-8').match(k)) { diff --git a/lib/status.js b/lib/status.js index eb6029f..4aead19 100644 --- a/lib/status.js +++ b/lib/status.js @@ -5,6 +5,7 @@ var Common = require('./common'); function StatusController(node) { this.node = node; this.common = new Common({log: this.node.log}); + this._header = this.node.services.header; } StatusController.prototype.show = function(req, res) { @@ -45,7 +46,7 @@ StatusController.prototype.show = function(req, res) { }; StatusController.prototype.getInfo = function(callback) { - this.node.services.bitcoind.getInfo(function(err, result) { + this._header.getInfo(function(err, result) { if (err) { return callback(err); } diff --git a/lib/utils.js b/lib/utils.js index e4ddeec..063d02b 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -6,6 +6,7 @@ var Common = require('./common'); function UtilsController(node) { this.node = node; + this._fee = this.node.services.fee; this.common = new Common({log: this.node.log}); } @@ -17,7 +18,7 @@ UtilsController.prototype.estimateFee = function(req, res) { async.map(nbBlocks, function(n, next) { var num = parseInt(n); // Insight and Bitcoin JSON-RPC return bitcoin for this value (instead of satoshis). - self.node.services.bitcoind.estimateFee(num, function(err, fee) { + self._fee.estimateFee(num, function(err, fee) { if (err) { return next(err); }