Skip to content

Commit 7cf9c08

Browse files
committed
add genesis block for mainnet/testnet and some parsing tests to make sure it looks good
1 parent 1512df3 commit 7cf9c08

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

cryptos/block.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
from .sha256 import sha256
1313

14+
# -----------------------------------------------------------------------------
15+
# Block headers, 80 bytes
16+
GENESIS_BLOCK = {
17+
'main': bytes.fromhex('0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c'),
18+
'test': bytes.fromhex('0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff001d1aa4ae18'),
19+
}
1420
# -----------------------------------------------------------------------------
1521
# helper functions
1622

tests/test_block.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from io import BytesIO
66

77
from cryptos.block import Block, calculate_new_bits, bits_to_target, target_to_bits
8+
from cryptos.block import GENESIS_BLOCK
89

910
def test_block():
1011

@@ -47,3 +48,25 @@ def test_calculate_bits():
4748
target = bits_to_target(bits)
4849
bits2 = target_to_bits(target)
4950
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

Comments
 (0)