forked from jimmysong/programmingbitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
51 lines (50 loc) · 1.53 KB
/
examples.py
File metadata and controls
51 lines (50 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'''
# tag::example1[]
>>> from io import BytesIO
>>> from network import SimpleNode, GetHeadersMessage, HeadersMessage
>>> from block import Block, GENESIS_BLOCK, LOWEST_BITS
>>> from helper import calculate_new_bits
>>> previous = Block.parse(BytesIO(GENESIS_BLOCK))
>>> first_epoch_timestamp = previous.timestamp
>>> expected_bits = LOWEST_BITS
>>> count = 1
>>> node = SimpleNode('mainnet.programmingbitcoin.com', testnet=False)
>>> node.handshake()
>>> for _ in range(19):
... getheaders = GetHeadersMessage(start_block=previous.hash())
... node.send(getheaders)
... headers = node.wait_for(HeadersMessage)
... for header in headers.blocks:
... if not header.check_pow(): # <1>
... raise RuntimeError('bad PoW at block {}'.format(count))
... if header.prev_block != previous.hash(): # <2>
... raise RuntimeError('discontinuous block at {}'.format(count))
... if count % 2016 == 0:
... time_diff = previous.timestamp - first_epoch_timestamp
... expected_bits = calculate_new_bits(previous.bits, time_diff)
... print(expected_bits.hex())
... first_epoch_timestamp = header.timestamp
... if header.bits != expected_bits: # <3>
... raise RuntimeError('bad bits at block {}'.format(count))
... previous = header
... count += 1
ffff001d
ffff001d
ffff001d
ffff001d
ffff001d
ffff001d
ffff001d
ffff001d
ffff001d
ffff001d
ffff001d
ffff001d
ffff001d
ffff001d
ffff001d
6ad8001d
28c4001d
71be001d
# end::example1[]
'''