network.height.

This commit is contained in:
Christopher Jeffrey 2016-03-28 15:26:02 -07:00
parent 46a47b5bf2
commit 2e0948faa5
7 changed files with 53 additions and 80 deletions

View File

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

View File

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

View File

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

View File

@ -180,6 +180,7 @@ Input.prototype.inspect = function inspect() {
hash: this.prevout.hash,
index: this.prevout.index,
spent: false,
age: 0,
address: null
};
}

View File

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

View File

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

View File

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