Author

Topic: BIP: 34 and the ERROR: AcceptBlock() : block height mismatch in coinbase (Read 1088 times)

hero member
Activity: 780
Merit: 501
I understand the design behind BIP: 34.  However I don't understand it's implementation in main.cpp.  

Can someone explain (in a somewhat 'dumbed down' fashion) what is happening here: https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp#L2673.  Specifically this check:

Code:
        CScript expect = CScript() << nHeight;
        if (block.vtx[0].vin[0].scriptSig.size() < expect.size() ||
            !std::equal(expect.begin(), expect.end(), block.vtx[0].vin[0].scriptSig.begin())) {
            return state.DoS(100, error("%s: block height mismatch in coinbase", __func__), REJECT_INVALID, "bad-cb-height");
        }

Why is this coinbase being rejected by the above code?
This specific example is version 2 (PoS) and has TX messaging

Code:
02000000 
c60c4b55
01
0000000000000000000000000000000000000000000000000000000000000000
ffffffff
1b
0101 <= serialized block height
04430d4b5508f8000001000000000a2f54696465506f6f6c2f
00000000
01
809fd50000000000
23
21022bec99ba133dc4bb055e1eb954370a4ca73f173348a6409c4ae8f92ef7ffd0f7ac
00000000
105365637572655061796d656e742e4343

Here is the code that generates the serialized height in Python

Code:
def encode_coinbase_nheight(n, min_size = 1):
# For encoding nHeight into coinbase
s = bytearray(b'\1')

while n > 127:
s[0] += 1
s.append(n % 256)
n //= 256

# Add to byte array
s.append(n)

# This can never be less that 1 bytes long
if min_size < 1:
min_size = 1

# Satisfy Minimum Length
while len(s) < min_size + 1:
s.append(0)
s[0] += 1

return bytes(s)

Jump to: