bitcoin/bitcoin — pull request #1526 by gavinandresen (2012-06-27 23:47:34+00:00)
Title: Transition to requiring block height in block coinbases

This builds on #1525 (the framework for smoothly rolling out new chain/transaction rules), defining "nVersion=2" blocks that include the block height as the first bytes of their coinbase.

Putting the height in the coinbase is desired for at least two reasons:
1. It guarantees that every subsequent block and transaction hash is unique.
2. It can be used to better reason about plausible difficulty for not-yet-connected blocks.

The format of the height is "serialized CScript" -- first byte is number of bytes in the number (will be 0x03 on main net for the next 300 or so years), following bytes are little-endian representation of the number.

Only blocks with nVersion=2 are expected to have the height as the first bytes, and the "must have the height in the coinbase" rule is only enforced if nVersion=2 blocks are a super-majority of the block's immediate ancestors.

I tested this with a testnet-in-a-box setup, creating a 100-block-long nVersion=2 chain and then making sure that:
1. nVersion=1 blocks were accepted
2. nVersion=2 blocks that included the wrong block height were rejected (I hacked a bitcoind to test that).