diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index b7287e99..2a488bb3 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -456,7 +456,7 @@ Block.prototype.getClaimed = function getClaimed() { * @returns {BN} */ -Block.reward = function reward(height, network) { +Block.reward = function _reward(height, network) { var halvings, reward; network = bcoin.network.get(network); @@ -468,11 +468,25 @@ Block.reward = function reward(height, network) { if (halvings >= 64) return 0; + // We need to shift right by `halvings`, + // but 50 btc is a 33 bit number, so we + // cheat. We only start halving once the + // halvings are at least 1. if (halvings === 0) return 5000000000; + // We can't shift right by 32 bits. + // 25m bitcoin is 32 bits, so we + // can safely return zero if the + // shift will be 32. + if (halvings >= 33) + return 0; + reward = 2500000000 >>> (halvings - 1); + // We could do this to keep this more (or + // less?) accurate, but this is infinitely + // slower. // reward = 5000000000; // reward = Math.floor(reward / Math.pow(2, halvings)); diff --git a/lib/bcoin/protocol/constants.js b/lib/bcoin/protocol/constants.js index f50c28d2..426ab9fb 100644 --- a/lib/bcoin/protocol/constants.js +++ b/lib/bcoin/protocol/constants.js @@ -9,7 +9,6 @@ * @module constants */ -var bn = require('bn.js'); var utils = require('../utils'); /** @@ -413,7 +412,7 @@ exports.tx = { MAX_FEE: exports.COIN / 10, MIN_RELAY: 10000, BARE_MULTISIG: true, - FREE_THRESHOLD: Math.floor(exports.COIN * 144 / 250), + FREE_THRESHOLD: exports.COIN * 144 / 250, MAX_SIGOPS: exports.block.MAX_SIGOPS / 5, MAX_SIGOPS_COST: exports.block.MAX_SIGOPS_COST / 5, COINBASE_MATURITY: 100 diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index c22dfda0..45115db6 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -80,6 +80,8 @@ function TX(data, block, index) { this._raw = data._raw || null; this._size = data._size || 0; this._witnessSize = data._witnessSize || 0; + this._outputValue = null; + this._inputValue = null; for (i = 0; i < data.inputs.length; i++) this.inputs.push(new bcoin.input(data.inputs[i])); @@ -665,12 +667,18 @@ TX.prototype.getInputValue = function getInputValue() { var total = 0; var i; + if (this._inputValue != null) + return this._inputValue; + if (!this.hasCoins()) return total; for (i = 0; i < this.inputs.length; i++) total += this.inputs[i].coin.value; + if (!this.mutable) + this._inputValue = total; + return total; }; @@ -683,9 +691,15 @@ TX.prototype.getOutputValue = function getOutputValue() { var total = 0; var i; + if (this._outputValue != null) + return this._outputValue; + for (i = 0; i < this.outputs.length; i++) total += this.outputs[i].value; + if (!this.mutable) + this._outputValue = total; + return total; };