Minor fixes.

This commit is contained in:
Chris Kleeschulte 2017-10-06 14:50:22 -04:00
parent fbef2d85fa
commit 3923f260e8
No known key found for this signature in database
GPG Key ID: 33195D27EF6BDB7F
2 changed files with 28 additions and 29 deletions

View File

@ -6,7 +6,6 @@ var util = require('util');
var bitcore = require('bitcore-lib'); var bitcore = require('bitcore-lib');
var _ = bitcore.deps._; var _ = bitcore.deps._;
var pools = require('../pools.json'); var pools = require('../pools.json');
var BN = bitcore.crypto.BN;
var LRU = require('lru-cache'); var LRU = require('lru-cache');
var Common = require('./common'); var Common = require('./common');
var bcoin = require('bcoin'); var bcoin = require('bcoin');
@ -110,6 +109,9 @@ BlockController.prototype.rawBlock = function(req, res, next) {
} else if(err) { } else if(err) {
return self.common.handleErrors(err, res); return self.common.handleErrors(err, res);
} }
if (!blockBuffer) {
return next();
}
req.rawBlock = { req.rawBlock = {
rawblock: blockBuffer.toString('hex') rawblock: blockBuffer.toString('hex')
}; };
@ -147,9 +149,9 @@ BlockController.prototype.transformBlock = function(block, info) {
chainwork: info.chainwork, chainwork: info.chainwork,
confirmations: this._block.getTip().height - info.height + 1, confirmations: this._block.getTip().height - info.height + 1,
previousblockhash: info.prevHash, previousblockhash: info.prevHash,
nextblockhash: null, nextblockhash: info.nextHash,
reward: null, reward: this.getBlockReward(block.txs[0]),
isMainChain: true, isMainChain: block.isMainChain,
poolInfo: this.getPoolInfo(block.txs[0]) poolInfo: this.getPoolInfo(block.txs[0])
}; };
}; };
@ -203,6 +205,10 @@ BlockController.prototype._getBlockSummary = function(hash, moreTimestamp, next)
return next(err); return next(err);
} }
if (!blockBuffer) {
return next();
}
var block = bcoin.block.fromRaw(blockBuffer, 'hex'); var block = bcoin.block.fromRaw(blockBuffer, 'hex');
self._header.getBlockHeader(hash, function(err, header) { self._header.getBlockHeader(hash, function(err, header) {
@ -214,8 +220,8 @@ BlockController.prototype._getBlockSummary = function(hash, moreTimestamp, next)
var summary = { var summary = {
height: header.height, height: header.height,
size: block.size, size: block.getSize(),
virtualSize: block.virtualSize, virtualSize: block.getVirtualSize(),
hash: hash, hash: hash,
time: header.timestamp, time: header.timestamp,
txlength: block.txs.length, 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) { BlockController.prototype.getPoolInfo = function(tx) {
if (!tx) { if (!tx) {
return {}; 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 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; module.exports = BlockController;

View File

@ -141,9 +141,9 @@ TxController.prototype.transformInput = function(options, input, index) {
transformed.valueSat = options.inputValues[index]; transformed.valueSat = options.inputValues[index];
transformed.value = transformed.valueSat / 1e8; transformed.value = transformed.valueSat / 1e8;
transformed.doubleSpentTxID = null; // TODO transformed.doubleSpentTxID = null; // TODO
//transformed.isConfirmed = null; // TODO transformed.isConfirmed = null; // TODO
//transformed.confirmations = null; // TODO transformed.confirmations = null; // TODO
//transformed.unconfirmedInput = null; // TODO transformed.unconfirmedInput = null; // TODO
return transformed; return transformed;
}; };
@ -161,12 +161,11 @@ TxController.prototype.transformOutput = function(options, output, index) {
transformed.scriptPubKey.asm = output.script.toASM(); transformed.scriptPubKey.asm = output.script.toASM();
} }
//if (!options.noSpent) { if (!options.noSpent) {
// These aren't implemented in the new api transformed.spentTxId = output.spentTxId || null; // TODO
//transformed.spentTxId = output.spentTxId || null; // we aren't tracking this with the bcoin implementation transformed.spentIndex = _.isUndefined(output.spentIndex) ? null : output.spentIndex; // TODO
//transformed.spentIndex = _.isUndefined(output.spentIndex) ? null : output.spentIndex; transformed.spentHeight = output.spentHeight || null; // TODO
//transformed.spentHeight = output.spentHeight || null; }
//}
var address = output.getAddress(); var address = output.getAddress();
if (address) { if (address) {