refactor.

This commit is contained in:
Christopher Jeffrey 2016-03-31 00:45:30 -07:00
parent 1dc15922f6
commit 82ee87c0f5
3 changed files with 29 additions and 27 deletions

View File

@ -254,6 +254,24 @@ Block.prototype.getCoinbaseHeight = function getCoinbaseHeight() {
return height;
};
Block.prototype.getReward = function getReward() {
var reward = Block.reward(this.height);
var i;
assert(this.height !== -1);
for (i = 1; i < this.txs.length; i++)
reward.iadd(this.txs[i].getFee());
return reward;
};
Block.prototype.getClaimed = function getClaimed() {
assert(this.txs[0]);
assert(this.txs[0].isCoinbase());
return this.txs[0].getOutputValue();
};
Block.reward = function reward(height) {
var halvings = height / network.halvingInterval | 0;
var reward;

View File

@ -521,7 +521,7 @@ Chain.prototype._verify = function _verify(block, prev, callback) {
flags |= constants.flags.VERIFY_CHECKLOCKTIMEVERIFY;
// Segregrated witness is now usable
if (network.segwitHeight !== -1 && height >= network.segwitHeight) {
if (network.type === 'segnet3' && height >= network.segwitHeight) {
if (block.version >= 5 && prev.isUpgraded(5)) {
flags |= constants.flags.VERIFY_WITNESS;
segwit = true;
@ -565,7 +565,7 @@ Chain.prototype._verify = function _verify(block, prev, callback) {
return done(new VerifyError(block, 'invalid', 'bad-cb-height', 100));
}
if (block.version >= 5 && segwit) {
if (segwit) {
if (block.commitmentHash !== block.getCommitmentHash('hex')) {
return done(new VerifyError(block,
'invalid',
@ -582,7 +582,7 @@ Chain.prototype._verify = function _verify(block, prev, callback) {
}
// Get timestamp for tx.isFinal().
ts = (lockFlags & constants.flags.MEDIAN_TIME_PAST)
ts = (lockFlags & constants.flags.MEDIAN_TIME_PAST) !== 0
? medianTime
: block.ts;
@ -668,7 +668,7 @@ Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callbac
if (err)
return callback(err);
if (!self._checkReward(block))
if (block.getClaimed().cmp(block.getReward()) > 0)
return callback(new VerifyError(block, 'invalid', 'bad-cb-amount', 100));
// Check all transactions
@ -757,18 +757,6 @@ Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callbac
});
};
Chain.prototype._checkReward = function _checkReward(block) {
var i, claimed, actual;
claimed = block.txs[0].getOutputValue();
actual = bcoin.block.reward(block.height);
for (i = 1; i < block.txs.length; i++)
actual.iadd(block.txs[i].getFee());
return claimed.cmp(actual) <= 0;
};
Chain.prototype.getHeight = function getHeight(hash) {
if (Buffer.isBuffer(hash))
hash = utils.toHex(hash);

View File

@ -548,9 +548,6 @@ TX.prototype.testInputs = function testInputs(addressMap, index) {
return this.inputs[index].test(addressMap);
for (i = 0; i < this.inputs.length; i++) {
if (index != null && i !== index)
continue;
if (this.inputs[i].test(addressMap))
return true;
}
@ -574,9 +571,6 @@ TX.prototype.testOutputs = function testOutputs(addressMap, index) {
return this.outputs[index].test(addressMap);
for (i = 0; i < this.outputs.length; i++) {
if (index != null && i !== index)
continue;
if (this.outputs[i].test(addressMap))
return true;
}
@ -723,9 +717,9 @@ TX.prototype.getSigops = function getSigops(scriptHash, accurate) {
// CheckTransaction
TX.prototype.isSane = function isSane(ret) {
var uniq = {};
var prevout = {};
var total = new bn(0);
var i, input, output, size;
var i, input, output, size, key;
if (!ret)
ret = {};
@ -774,12 +768,13 @@ TX.prototype.isSane = function isSane(ret) {
for (i = 0; i < this.inputs.length; i++) {
input = this.inputs[i];
if (uniq[input.prevout.hash]) {
key = input.prevout.hash + '/' + input.prevout.index;
if (prevout[key]) {
ret.reason = 'bad-txns-inputs-duplicate';
ret.score = 100;
return false;
}
uniq[input.prevout.hash] = true;
prevout[key] = true;
}
if (this.isCoinbase()) {
@ -792,7 +787,8 @@ TX.prototype.isSane = function isSane(ret) {
} else {
for (i = 0; i < this.inputs.length; i++) {
input = this.inputs[i];
if (input.prevout.hash === constants.nullHash) {
if (input.prevout.hash === constants.nullHash
&& input.prevout.index === 0xffffffff) {
ret.reason = 'bad-txns-prevout-null';
ret.score = 10;
return false;