refactor.
This commit is contained in:
parent
1dc15922f6
commit
82ee87c0f5
@ -254,6 +254,24 @@ Block.prototype.getCoinbaseHeight = function getCoinbaseHeight() {
|
|||||||
return height;
|
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) {
|
Block.reward = function reward(height) {
|
||||||
var halvings = height / network.halvingInterval | 0;
|
var halvings = height / network.halvingInterval | 0;
|
||||||
var reward;
|
var reward;
|
||||||
|
|||||||
@ -521,7 +521,7 @@ Chain.prototype._verify = function _verify(block, prev, callback) {
|
|||||||
flags |= constants.flags.VERIFY_CHECKLOCKTIMEVERIFY;
|
flags |= constants.flags.VERIFY_CHECKLOCKTIMEVERIFY;
|
||||||
|
|
||||||
// Segregrated witness is now usable
|
// 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)) {
|
if (block.version >= 5 && prev.isUpgraded(5)) {
|
||||||
flags |= constants.flags.VERIFY_WITNESS;
|
flags |= constants.flags.VERIFY_WITNESS;
|
||||||
segwit = true;
|
segwit = true;
|
||||||
@ -565,7 +565,7 @@ Chain.prototype._verify = function _verify(block, prev, callback) {
|
|||||||
return done(new VerifyError(block, 'invalid', 'bad-cb-height', 100));
|
return done(new VerifyError(block, 'invalid', 'bad-cb-height', 100));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (block.version >= 5 && segwit) {
|
if (segwit) {
|
||||||
if (block.commitmentHash !== block.getCommitmentHash('hex')) {
|
if (block.commitmentHash !== block.getCommitmentHash('hex')) {
|
||||||
return done(new VerifyError(block,
|
return done(new VerifyError(block,
|
||||||
'invalid',
|
'invalid',
|
||||||
@ -582,7 +582,7 @@ Chain.prototype._verify = function _verify(block, prev, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get timestamp for tx.isFinal().
|
// Get timestamp for tx.isFinal().
|
||||||
ts = (lockFlags & constants.flags.MEDIAN_TIME_PAST)
|
ts = (lockFlags & constants.flags.MEDIAN_TIME_PAST) !== 0
|
||||||
? medianTime
|
? medianTime
|
||||||
: block.ts;
|
: block.ts;
|
||||||
|
|
||||||
@ -668,7 +668,7 @@ Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callbac
|
|||||||
if (err)
|
if (err)
|
||||||
return callback(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));
|
return callback(new VerifyError(block, 'invalid', 'bad-cb-amount', 100));
|
||||||
|
|
||||||
// Check all transactions
|
// 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) {
|
Chain.prototype.getHeight = function getHeight(hash) {
|
||||||
if (Buffer.isBuffer(hash))
|
if (Buffer.isBuffer(hash))
|
||||||
hash = utils.toHex(hash);
|
hash = utils.toHex(hash);
|
||||||
|
|||||||
@ -548,9 +548,6 @@ TX.prototype.testInputs = function testInputs(addressMap, index) {
|
|||||||
return this.inputs[index].test(addressMap);
|
return this.inputs[index].test(addressMap);
|
||||||
|
|
||||||
for (i = 0; i < this.inputs.length; i++) {
|
for (i = 0; i < this.inputs.length; i++) {
|
||||||
if (index != null && i !== index)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (this.inputs[i].test(addressMap))
|
if (this.inputs[i].test(addressMap))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -574,9 +571,6 @@ TX.prototype.testOutputs = function testOutputs(addressMap, index) {
|
|||||||
return this.outputs[index].test(addressMap);
|
return this.outputs[index].test(addressMap);
|
||||||
|
|
||||||
for (i = 0; i < this.outputs.length; i++) {
|
for (i = 0; i < this.outputs.length; i++) {
|
||||||
if (index != null && i !== index)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (this.outputs[i].test(addressMap))
|
if (this.outputs[i].test(addressMap))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -723,9 +717,9 @@ TX.prototype.getSigops = function getSigops(scriptHash, accurate) {
|
|||||||
|
|
||||||
// CheckTransaction
|
// CheckTransaction
|
||||||
TX.prototype.isSane = function isSane(ret) {
|
TX.prototype.isSane = function isSane(ret) {
|
||||||
var uniq = {};
|
var prevout = {};
|
||||||
var total = new bn(0);
|
var total = new bn(0);
|
||||||
var i, input, output, size;
|
var i, input, output, size, key;
|
||||||
|
|
||||||
if (!ret)
|
if (!ret)
|
||||||
ret = {};
|
ret = {};
|
||||||
@ -774,12 +768,13 @@ TX.prototype.isSane = function isSane(ret) {
|
|||||||
|
|
||||||
for (i = 0; i < this.inputs.length; i++) {
|
for (i = 0; i < this.inputs.length; i++) {
|
||||||
input = this.inputs[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.reason = 'bad-txns-inputs-duplicate';
|
||||||
ret.score = 100;
|
ret.score = 100;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
uniq[input.prevout.hash] = true;
|
prevout[key] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isCoinbase()) {
|
if (this.isCoinbase()) {
|
||||||
@ -792,7 +787,8 @@ TX.prototype.isSane = function isSane(ret) {
|
|||||||
} else {
|
} else {
|
||||||
for (i = 0; i < this.inputs.length; i++) {
|
for (i = 0; i < this.inputs.length; i++) {
|
||||||
input = this.inputs[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.reason = 'bad-txns-prevout-null';
|
||||||
ret.score = 10;
|
ret.score = 10;
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user