blockchain: Support small coinbase block size

This modifies the ExtractCoinbaseHeight function to recognize small
canonically serialized block heights in coinbase scripts of blocks
higher than version 2.

This allows regression test chains in which blocks encode the serialized
height in the coinbase starting from block 1.
This commit is contained in:
Dave Collins 2016-10-19 22:48:10 -05:00
parent 59a3fc2f66
commit 49cbaf23dd
No known key found for this signature in database
GPG key ID: B8904D9D9C93D1F2

View file

@ -576,6 +576,18 @@ func ExtractCoinbaseHeight(coinbaseTx *btcutil.Tx) (int32, error) {
return 0, ruleError(ErrMissingCoinbaseHeight, str) return 0, ruleError(ErrMissingCoinbaseHeight, str)
} }
// Detect the case when the block height is a small integer encoded with
// as single byte.
opcode := int(sigScript[0])
if opcode == txscript.OP_0 {
return 0, nil
}
if opcode >= txscript.OP_1 && opcode <= txscript.OP_16 {
return int32(opcode - (txscript.OP_1 - 1)), nil
}
// Otherwise, the opcode is the length of the following bytes which
// encode in the block height.
serializedLen := int(sigScript[0]) serializedLen := int(sigScript[0])
if len(sigScript[1:]) < serializedLen { if len(sigScript[1:]) < serializedLen {
str := "the coinbase signature script for blocks of " + str := "the coinbase signature script for blocks of " +