From f68750991ded7daf975b8a383f8a027c4c16ee0b Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 23 Dec 2015 05:33:21 -0800 Subject: [PATCH] add block.reward. add coinbase data to into. failsafe for getFee. --- lib/bcoin/block.js | 21 +++++++++++++++++++++ lib/bcoin/input.js | 15 +++++++++------ lib/bcoin/output.js | 12 ++++++------ lib/bcoin/protocol/network.js | 4 ++++ lib/bcoin/tx.js | 3 +++ 5 files changed, 43 insertions(+), 12 deletions(-) diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index d213dbe6..1e3b38b6 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -7,6 +7,7 @@ var bcoin = require('../bcoin'); var utils = bcoin.utils; var constants = bcoin.protocol.constants; +var network = bcoin.protocol.network; /** * Block @@ -299,6 +300,26 @@ Block.prototype.__defineGetter__('rhash', function() { return utils.revHex(this.hash('hex')); }); +Block.reward = function reward(height) { + var halvings = height / network.halvingInterval | 0; + var reward; + + if (height < 0) + return utils.satoshi('0.0'); + + if (halvings >= 64) + return utils.satoshi('0.0'); + + reward = utils.satoshi('50.0'); + reward.iushrn(halvings); + + return reward; +}; + +Block.prototype.__defineGetter__('reward', function() { + return Block.reward(this.height); +}); + Block.prototype.inspect = function inspect() { var copy = bcoin.block(this, this.subtype); copy.__proto__ = null; diff --git a/lib/bcoin/input.js b/lib/bcoin/input.js index ae69247f..b4d393d6 100644 --- a/lib/bcoin/input.js +++ b/lib/bcoin/input.js @@ -115,7 +115,7 @@ Input.getData = function getData(input) { addr: '[unknown]', multisig: null, redeem: null, - data: null, + flags: null, text: null, value: new bn(0), script: s, @@ -125,17 +125,20 @@ Input.getData = function getData(input) { } if (+input.out.hash === 0) { + data = bcoin.script.coinbase(input.script); return { type: 'coinbase', side: 'input', + coinbase: data, + height: data.height || -1, sig: null, pub: null, hash: '[coinbase]', addr: '[coinbase]', multisig: null, redeem: null, - data: null, - text: null, + flags: data.flags, + text: data.text, value: new bn(0), script: s, seq: input.seq, @@ -173,7 +176,7 @@ Input.getData = function getData(input) { addr: '[unknown]', multisig: null, redeem: null, - data: null, + flags: null, text: null, value: new bn(0), script: s, @@ -195,7 +198,7 @@ Input.getData = function getData(input) { addr: addr, multisig: null, redeem: null, - data: null, + flags: null, text: null, value: new bn(0), script: s, @@ -260,7 +263,7 @@ Input.getData = function getData(input) { addr: '[unknown]', multisig: null, redeem: null, - data: null, + flags: null, text: null, value: new bn(0), script: s, diff --git a/lib/bcoin/output.js b/lib/bcoin/output.js index 89297864..364b445e 100644 --- a/lib/bcoin/output.js +++ b/lib/bcoin/output.js @@ -88,7 +88,7 @@ Output.getData = function getData(output) { addr: addr, multisig: null, redeem: null, - data: null, + flags: null, text: null, value: output.value, script: s, @@ -109,7 +109,7 @@ Output.getData = function getData(output) { addr: addr, multisig: null, redeem: null, - data: null, + flags: null, text: null, value: output.value, script: s, @@ -143,7 +143,7 @@ Output.getData = function getData(output) { }) }, redeem: null, - data: null, + flags: null, text: null, value: output.value, script: s, @@ -171,7 +171,7 @@ Output.getData = function getData(output) { addrs: null }, redeem: null, - data: null, + flags: null, text: null, value: output.value, script: s, @@ -191,7 +191,7 @@ Output.getData = function getData(output) { addr: '[colored]', multisig: null, redeem: null, - data: ret, + flags: ret, text: utils.array2utf8(ret), value: output.value, script: s, @@ -209,7 +209,7 @@ Output.getData = function getData(output) { addr: '[unknown]', multisig: null, redeem: null, - data: null, + flags: null, text: null, value: new bn(0), script: s, diff --git a/lib/bcoin/protocol/network.js b/lib/bcoin/protocol/network.js index d6c9413b..9d2d146b 100644 --- a/lib/bcoin/protocol/network.js +++ b/lib/bcoin/protocol/network.js @@ -80,6 +80,8 @@ main.checkpoints.tsLastCheckpoint = 1397080064; main.checkpoints.txsLastCheckpoint = 36544669; main.checkpoints.txsPerDay = 60000.0; +main.halvingInterval = 210000; + // http://blockexplorer.com/b/0 // http://blockexplorer.com/rawblock/000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f main.genesis = { @@ -153,6 +155,8 @@ testnet.checkpoints.tsLastCheckpoint = 1338180505; testnet.checkpoints.txsLastCheckpoint = 16341; testnet.checkpoints.txsPerDay = 300; +testnet.halvingInterval = 210000; + // http://blockexplorer.com/testnet/b/0 // http://blockexplorer.com/testnet/rawblock/000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943 testnet.genesis = { diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 025fbe5c..a769e2d2 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -795,6 +795,9 @@ TX.prototype._recalculateFee = function recalculateFee() { }; TX.prototype.getFee = function getFee() { + if (this.funds('in').cmp(this.funds('out')) < 0) + return new bn(0); + return this.funds('in').sub(this.funds('out')); };