cache values.

This commit is contained in:
Christopher Jeffrey 2016-05-15 00:59:29 -07:00
parent a7f3d2aa8f
commit 022de4a91d
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 30 additions and 3 deletions

View File

@ -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));

View File

@ -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

View File

@ -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;
};