From 49cbaf23dd94d2eb6a44eac3cc24e98f9a582aa5 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 19 Oct 2016 22:48:10 -0500 Subject: [PATCH] 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. --- blockchain/validate.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/blockchain/validate.go b/blockchain/validate.go index 6a477a25..bc318d26 100644 --- a/blockchain/validate.go +++ b/blockchain/validate.go @@ -576,6 +576,18 @@ func ExtractCoinbaseHeight(coinbaseTx *btcutil.Tx) (int32, error) { 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]) if len(sigScript[1:]) < serializedLen { str := "the coinbase signature script for blocks of " +