|
5 | 5 | from io import BytesIO |
6 | 6 |
|
7 | 7 | from cryptos.block import Block, calculate_new_bits, bits_to_target, target_to_bits |
| 8 | +from cryptos.block import GENESIS_BLOCK |
8 | 9 |
|
9 | 10 | def test_block(): |
10 | 11 |
|
@@ -47,3 +48,25 @@ def test_calculate_bits(): |
47 | 48 | target = bits_to_target(bits) |
48 | 49 | bits2 = target_to_bits(target) |
49 | 50 | assert bits == bits2 |
| 51 | + |
| 52 | +def test_genesis_block(): |
| 53 | + """ |
| 54 | + Validate the Bitcoin mainnet genesis block header |
| 55 | + Reference: https://en.bitcoin.it/wiki/Genesis_block |
| 56 | + Original code: https://sourceforge.net/p/bitcoin/code/133/tree/trunk/main.cpp#l1613 |
| 57 | + """ |
| 58 | + block_bytes = GENESIS_BLOCK['main'] |
| 59 | + assert len(block_bytes) == 80 |
| 60 | + block = Block.decode(BytesIO(block_bytes)) |
| 61 | + |
| 62 | + assert block.version == 1 |
| 63 | + assert block.prev_block == b'\x00'*32 # ;) |
| 64 | + assert block.merkle_root.hex() == '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b' |
| 65 | + assert block.timestamp == 1231006505 |
| 66 | + assert block.bits[::-1].hex() == '1d00ffff' |
| 67 | + assert int.from_bytes(block.nonce, 'little') == 2083236893 |
| 68 | + |
| 69 | + # validate proof of work. by two extra zeros, too! |
| 70 | + assert block.id() == '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f' |
| 71 | + assert format(block.target(), '064x') == '00000000ffff0000000000000000000000000000000000000000000000000000' |
| 72 | + assert block.validate() |
0 commit comments