improve coinbase height check.

This commit is contained in:
Christopher Jeffrey 2015-12-22 20:06:57 -08:00
parent 618177b6a6
commit 3d417f48df
2 changed files with 35 additions and 12 deletions

View File

@ -14,7 +14,7 @@ var constants = bcoin.protocol.constants;
function Block(data, subtype) { function Block(data, subtype) {
var self = this; var self = this;
var tx; var tx, height;
if (!(this instanceof Block)) if (!(this instanceof Block))
return new Block(data, subtype); return new Block(data, subtype);
@ -61,8 +61,11 @@ function Block(data, subtype) {
if (this.version >= 2) { if (this.version >= 2) {
tx = this.txs[0]; tx = this.txs[0];
if (tx && tx.inputs[0] && +tx.inputs[0].out.hash === 0) if (tx && tx.inputs[0] && +tx.inputs[0].out.hash === 0) {
this._height = bcoin.script.coinbase(tx.inputs[0].script); var height = bcoin.script.coinbaseHeight(tx.inputs[0].script);
if (height > 0)
this._height = height;
}
} }
this.invalid = !this._checkBlock(); this.invalid = !this._checkBlock();

View File

@ -1138,20 +1138,40 @@ script.isScripthashInput = function isScripthashInput(s, redeem) {
return keys; return keys;
}; };
script.coinbase = function coinbase(s) { script.coinbaseHeight = function coinbaseHeight(s) {
var base = s._raw || script.encode(s); var raw = s._raw || script.encode(s);
var len = Math.min(base[0], base.length); var height;
var height = 0;
var i, b;
for (i = 0; i < len; i++) { if (!Array.isArray(s[0]))
b = base[i + 1] & 0xff; return -1;
height |= b << (8 * i);
} if (raw[0] < 0x03 || s[0].length < 3)
return -1;
height = new bn(s[0].reverse()).toNumber();
// Last v1 block
if (height < 227835)
return -1;
return height; return height;
}; };
script.coinbase = function coinbase(s) {
var raw = s._raw || script.encode(s);
var data = script.coinbaseHeight(s);
if (height > 0)
raw = raw.slice(raw[0] + 1);
return {
height: height,
script: s,
data: raw,
text: utils.array2utf8(raw)
};
};
// https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki // https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki
/** /**
* A canonical signature exists of: <30> <total len> <02> <len R> <R> <02> <len S> <S> <hashtype> * A canonical signature exists of: <30> <total len> <02> <len R> <R> <02> <len S> <S> <hashtype>