jimmysong / programmingbitcoin

Repository for the book
Other
1.75k stars 656 forks source link

Invalid ScriptSig found on mainnet #275

Closed 1ma closed 8 months ago

1ma commented 1 year ago

While working on my test suite I noticed that the coinbase transaction of the block used as an example in Chapter 9 (height 482737) has actually an invalid ScriptSig that causes the script parser to raise an exception at the end.

fucky

The problem is in the last section: 0c06726d61fd000000000000. 0c should have been 0b as there are only 11 bytes left of script at that point instead of 12. You can see this as a glitch in the mempool interface, as it prints the opcode OP_PUSHBYTES_12 but not its associated data.

Since that block was not rejected by the network my conclusion is that the script parser from Bitcoin Core has (or had) some bug, or more likely it is deliberately more flexible than the one shown in the book. I'd like to understand how the real Script parser treats cases like this.

1ma commented 8 months ago

To answer my own question the ScriptSig is invalid but it belongs to a coinbase transaction, and coinbase transaction ScriptSigs can be arbitrary and are not enforced.

If we examine this transaction with hexdump it's easy to see that this scriptSig is actually the ASCII text that is shown in mempool's interface, so trying to decode it as if it was an actual Bitcoin script doesn't make sense.

$ curl -s https://mempool.space/api/tx/4af77e5afbfd7c8b5a8160a37f6a70bd18eac1250c736ddd2d285bf22226b754/hex | xxd -r -p | hexdump -C

00000000  01 00 00 00 00 01 01 00  00 00 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020  00 00 00 00 00 00 00 ff  ff ff ff 4e 03 b2 5d 07  |...........N..].|
00000030  04 1f 77 a7 59 2f 42 54  43 2e 43 4f 4d 2f 4e 59  |..w.Y/BTC.COM/NY|
00000040  41 2f fa be 6d 6d 1d e4  89 07 7b aa e2 0a 96 0c  |A/..mm....{.....|
00000050  db 62 31 ad b3 28 11 02  36 6f bb 54 6e ea 5c 16  |.b1..(..6o.Tn.\.|
00000060  aa 40 b2 a2 39 55 01 00  00 00 00 00 00 00 0c 06  |.@..9U..........|
00000070  72 6d 61 fd 00 00 00 00  00 00 ff ff ff ff 02 50  |rma............P|
00000080  2e c0 4b 00 00 00 00 17  a9 14 8e a9 31 a5 c9 af  |..K.........1...|
00000090  68 eb 19 33 e2 45 7e 82  e1 62 ac bf 9e 71 87 00  |h..3.E~..b...q..|
000000a0  00 00 00 00 00 00 00 26  6a 24 aa 21 a9 ed 29 19  |.......&j$.!..).|
000000b0  f5 1b 3f 63 56 ac b4 b4  1a 75 61 c1 15 08 cc 56  |..?cV....ua....V|
000000c0  39 89 4a 23 72 9f 3e 17  61 24 21 f2 70 91 01 20  |9.J#r.>.a$!.p.. |
000000d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000000f0  00 00 00 00                                       |....|
000000f4