From 2e0948faa59d4fc0f372c19e3972ae30b5e61296 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 28 Mar 2016 15:26:02 -0700 Subject: [PATCH] network.height. --- lib/bcoin/abstractblock.js | 6 ----- lib/bcoin/chain.js | 11 +++++++- lib/bcoin/coin.js | 34 +++++++----------------- lib/bcoin/input.js | 1 + lib/bcoin/mtx.js | 23 +++++++--------- lib/bcoin/protocol/network.js | 8 ++++++ lib/bcoin/tx.js | 50 ++++++++++------------------------- 7 files changed, 53 insertions(+), 80 deletions(-) diff --git a/lib/bcoin/abstractblock.js b/lib/bcoin/abstractblock.js index ee6658a5..69788998 100644 --- a/lib/bcoin/abstractblock.js +++ b/lib/bcoin/abstractblock.js @@ -30,8 +30,6 @@ function AbstractBlock(data) { this._raw = data._raw || null; this._size = data._size || 0; - this._chain = data.chain; - this.valid = null; this._hash = null; } @@ -98,10 +96,6 @@ AbstractBlock.prototype.isGenesis = function isGenesis() { return this.hash('hex') === network.genesis.hash; }; -AbstractBlock.prototype.__defineGetter__('chain', function() { - return this._chain || bcoin.chain.global; -}); - AbstractBlock.prototype.__defineGetter__('rhash', function() { return utils.revHex(this.hash('hex')); }); diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 71a28a54..f79eb917 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -167,6 +167,9 @@ Chain.prototype._init = function _init() { self.tip = tip; self.height = tip.height; + if (self.bestHeight === -1) + network.height = tip.height; + self.loaded = true; self.emit('open'); self.emit('tip', tip); @@ -889,6 +892,10 @@ Chain.prototype._setBestChain = function _setBestChain(entry, block, callback) { self.tip = entry; self.height = entry.height; + + if (self.bestHeight === -1) + network.height = entry.height; + self.emit('tip', entry); return callback(); @@ -1062,8 +1069,10 @@ Chain.prototype.add = function add(initial, peer, callback, force) { // We do this even for orphans (peers will send // us their highest block during the initial // getblocks sync, making it an orphan). - if (block.getCoinbaseHeight() > self.bestHeight) + if (block.getCoinbaseHeight() > self.bestHeight) { self.bestHeight = block.getCoinbaseHeight(); + network.height = self.bestHeight; + } // If previous block wasn't ever seen, // add it current to orphans and break. diff --git a/lib/bcoin/coin.js b/lib/bcoin/coin.js index abb15466..dd93dabc 100644 --- a/lib/bcoin/coin.js +++ b/lib/bcoin/coin.js @@ -8,6 +8,7 @@ var bn = require('bn.js'); var bcoin = require('../bcoin'); var utils = require('./utils'); var assert = utils.assert; +var network = bcoin.protocol.network; /** * Coin @@ -63,39 +64,19 @@ function Coin(tx, index) { utils.inherits(Coin, bcoin.output); -Coin.prototype.__defineGetter__('chain', function() { - return this._chain || bcoin.chain.global; -}); - -Coin.prototype.getSize = function getSize() { - return 4 + 4 + 8 + this.script.getSize() + 32 + 4 + 1; -}; - Coin.prototype.getConfirmations = function getConfirmations(height) { - var top; - - if (height == null) { - if (!this.chain) - return 0; - - top = this.chain.height; - } else { - top = height; - } + if (height == null) + height = network.height; if (this.height === -1) return 0; - if (top < this.height) + if (height < this.height) return 1; - return top - this.height + 1; + return height - this.height + 1; }; -Coin.prototype.__defineGetter__('confirmations', function() { - return this.getConfirmations(); -}); - Coin.prototype.getAge = function getAge(height) { var age = this.getConfirmations(height); @@ -108,6 +89,10 @@ Coin.prototype.getAge = function getAge(height) { return age; }; +Coin.prototype.__defineGetter__('confirmations', function() { + return this.getConfirmations(); +}); + Coin.prototype.__defineGetter__('age', function() { return this.getAge(); }); @@ -122,6 +107,7 @@ Coin.prototype.inspect = function inspect() { coinbase: this.coinbase, hash: this.hash ? utils.revHex(this.hash) : null, index: this.index, + age: this.age, address: this.getAddress() }; }; diff --git a/lib/bcoin/input.js b/lib/bcoin/input.js index 270139f3..37fab8e7 100644 --- a/lib/bcoin/input.js +++ b/lib/bcoin/input.js @@ -180,6 +180,7 @@ Input.prototype.inspect = function inspect() { hash: this.prevout.hash, index: this.prevout.index, spent: false, + age: 0, address: null }; } diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index a4470a75..d62bb3f0 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -10,6 +10,7 @@ var bcoin = require('../bcoin'); var utils = require('./utils'); var assert = utils.assert; var constants = bcoin.protocol.constants; +var network = bcoin.protocol.network; var Script = bcoin.script; var Witness = bcoin.script.witness; @@ -45,8 +46,6 @@ function MTX(options) { this.height = -1; - this._chain = options.chain; - if (options.inputs) { options.inputs.forEach(function(input) { this.addInput(input); @@ -882,10 +881,12 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) { // Calculate max possible size after signing. size = tx.maxSize(options.m, options.n); - // if (newkb == null && tx.isFree(this.chain.height + 1, size)) { - // fee = new bn(0); - // break; - // } + if (options.free) { + if (newkb == null && tx.isFree(null, size)) { + fee = new bn(0); + break; + } + } if (options.accurate) { newkb = size / 1024; @@ -960,7 +961,7 @@ MTX.prototype.fill = function fill(coins, options) { result = this.selectCoins(coins, options); if (!result.coins) { - err = new Error('Could not fill transaction'); + err = new Error('Could not fill transaction.'); err.requiredFunds = result.total; throw err; } @@ -1022,12 +1023,8 @@ MTX.prototype.sortMembers = function sortMembers() { }; MTX.prototype.avoidFeeSniping = function avoidFeeSniping(height) { - if (height == null) { - if (!this.chain) - return; - - height = this.chain.height; - } + if (height == null) + height = network.height; if (height === -1) height = 0; diff --git a/lib/bcoin/protocol/network.js b/lib/bcoin/protocol/network.js index 108a2cd5..d93ba49f 100644 --- a/lib/bcoin/protocol/network.js +++ b/lib/bcoin/protocol/network.js @@ -153,6 +153,8 @@ main.deployments = { } }; +main.height = -1; + /** * Testnet (v3) * https://en.bitcoin.it/wiki/Testnet @@ -272,6 +274,8 @@ testnet.deployments = { } }; +testnet.height = -1; + /** * Regtest */ @@ -373,6 +377,8 @@ regtest.deployments = { } }; +regtest.height = -1; + /** * Segnet */ @@ -481,6 +487,8 @@ segnet.ruleChangeActivationThreshold = 108; segnet.minerConfirmationWindow = 144; segnet.deployments = {}; +segnet.height = -1; + network.xprivkeys = { '76066276': 'main', '70615956': 'testnet', diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 98cfd511..e5b62028 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -10,6 +10,7 @@ var bcoin = require('../bcoin'); var utils = require('./utils'); var assert = utils.assert; var constants = bcoin.protocol.constants; +var network = bcoin.protocol.network; var Script = bcoin.script; var BufferReader = require('./reader'); var BufferWriter = require('./writer'); @@ -44,8 +45,6 @@ function TX(data, block, index) { this.height = data.height != null ? data.height : -1; - this._chain = data.chain; - // assert(data.inputs.length !== 0); // assert(data.outputs.length !== 0); @@ -913,11 +912,11 @@ TX.prototype.maxSize = function maxSize() { TX.prototype.getPriority = function getPriority(height, size) { var sum, i, input, age; - if (height == null) + if (height == null) { height = this.height; - - if (height === -1) - height = null; + if (height === -1) + height = network.height + 1; + } if (!this.hasCoins()) return new bn(0); @@ -953,8 +952,11 @@ TX.prototype.isFree = function isFree(height, size) { if (!this.hasCoins()) return false; - if (height == null) + if (height == null) { height = this.height; + if (height === -1) + height = network.height + 1; + } if (size == null) size = this.maxSize(); @@ -981,37 +983,17 @@ TX.prototype.getMinFee = function getMinFee(size) { return fee; }; -TX.prototype.getHeight = function getHeight() { - if (this.height !== -1) - return this.height; - - if (!this.chain) - return -1; - - return this.block ? this.chain.getHeight(this.block) : -1; -}; - TX.prototype.getConfirmations = function getConfirmations(height) { - var top; + if (height == null) + height = network.height; - if (height == null) { - if (!this.chain) - return 0; - - top = this.chain.height; - } else { - top = height; - } - - height = this.height; - - if (height === -1) + if (this.height === -1) return 0; - if (top < height) + if (height < this.height) return 1; - return top - height + 1; + return height - this.height + 1; }; TX.prototype.getValue = function getValue() { @@ -1034,10 +1016,6 @@ TX.prototype.hasType = function hasType(type) { return false; }; -TX.prototype.__defineGetter__('chain', function() { - return this._chain || bcoin.chain.global; -}); - TX.prototype.__defineGetter__('rblock', function() { return this.block ? utils.revHex(this.block)