From 3923f260e889b5a1fc32dbf480c408b12ad68672 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Fri, 6 Oct 2017 14:50:22 -0400 Subject: [PATCH] Minor fixes. --- lib/blocks.js | 40 ++++++++++++++++++++-------------------- lib/transactions.js | 17 ++++++++--------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/lib/blocks.js b/lib/blocks.js index 2e5c84f..82da845 100644 --- a/lib/blocks.js +++ b/lib/blocks.js @@ -6,7 +6,6 @@ var util = require('util'); var bitcore = require('bitcore-lib'); var _ = bitcore.deps._; var pools = require('../pools.json'); -var BN = bitcore.crypto.BN; var LRU = require('lru-cache'); var Common = require('./common'); var bcoin = require('bcoin'); @@ -110,6 +109,9 @@ BlockController.prototype.rawBlock = function(req, res, next) { } else if(err) { return self.common.handleErrors(err, res); } + if (!blockBuffer) { + return next(); + } req.rawBlock = { rawblock: blockBuffer.toString('hex') }; @@ -147,9 +149,9 @@ BlockController.prototype.transformBlock = function(block, info) { chainwork: info.chainwork, confirmations: this._block.getTip().height - info.height + 1, previousblockhash: info.prevHash, - nextblockhash: null, - reward: null, - isMainChain: true, + nextblockhash: info.nextHash, + reward: this.getBlockReward(block.txs[0]), + isMainChain: block.isMainChain, poolInfo: this.getPoolInfo(block.txs[0]) }; }; @@ -203,6 +205,10 @@ BlockController.prototype._getBlockSummary = function(hash, moreTimestamp, next) return next(err); } + if (!blockBuffer) { + return next(); + } + var block = bcoin.block.fromRaw(blockBuffer, 'hex'); self._header.getBlockHeader(hash, function(err, header) { @@ -214,8 +220,8 @@ BlockController.prototype._getBlockSummary = function(hash, moreTimestamp, next) var summary = { height: header.height, - size: block.size, - virtualSize: block.virtualSize, + size: block.getSize(), + virtualSize: block.getVirtualSize(), hash: hash, time: header.timestamp, txlength: block.txs.length, @@ -315,6 +321,14 @@ BlockController.prototype.list = function(req, res) { }); }; +BlockController.prototype.getBlockReward = function(tx) { + var amt = 0; + tx.outputs.forEach(function(output) { + amt += output.value; + }); + return bitcore.Unit.fromSatoshis(amt).toBTC(); +}; + BlockController.prototype.getPoolInfo = function(tx) { if (!tx) { return {}; @@ -339,18 +353,4 @@ BlockController.prototype.formatTimestamp = function(date) { return yyyy + '-' + (mm[1] ? mm : '0' + mm[0]) + '-' + (dd[1] ? dd : '0' + dd[0]); //padding }; -BlockController.prototype.getBlockReward = function(height) { - var halvings = Math.floor(height / 210000); - // Force block reward to zero when right shift is undefined. - if (halvings >= 64) { - return 0; - } - - // Subsidy is cut in half every 210,000 blocks which will occur approximately every 4 years. - var subsidy = new BN(50 * 1e8); - subsidy = subsidy.shrn(halvings); - - return parseInt(subsidy.toString(10)); -}; - module.exports = BlockController; diff --git a/lib/transactions.js b/lib/transactions.js index cb05e26..950895d 100644 --- a/lib/transactions.js +++ b/lib/transactions.js @@ -141,9 +141,9 @@ TxController.prototype.transformInput = function(options, input, index) { transformed.valueSat = options.inputValues[index]; transformed.value = transformed.valueSat / 1e8; transformed.doubleSpentTxID = null; // TODO - //transformed.isConfirmed = null; // TODO - //transformed.confirmations = null; // TODO - //transformed.unconfirmedInput = null; // TODO + transformed.isConfirmed = null; // TODO + transformed.confirmations = null; // TODO + transformed.unconfirmedInput = null; // TODO return transformed; }; @@ -161,12 +161,11 @@ TxController.prototype.transformOutput = function(options, output, index) { transformed.scriptPubKey.asm = output.script.toASM(); } - //if (!options.noSpent) { - // These aren't implemented in the new api - //transformed.spentTxId = output.spentTxId || null; // we aren't tracking this with the bcoin implementation - //transformed.spentIndex = _.isUndefined(output.spentIndex) ? null : output.spentIndex; - //transformed.spentHeight = output.spentHeight || null; - //} + if (!options.noSpent) { + transformed.spentTxId = output.spentTxId || null; // TODO + transformed.spentIndex = _.isUndefined(output.spentIndex) ? null : output.spentIndex; // TODO + transformed.spentHeight = output.spentHeight || null; // TODO + } var address = output.getAddress(); if (address) {