30
|
1
|
|
2 # Table of Contents
|
|
3
|
|
4 1. [Installation](#org79ef3e8)
|
|
5 2. [Usage](#org47a2d85)
|
|
6 1. [Example 1 -> Deserialize genesis block](#orgb8ccd7c)
|
|
7 2. [Example 2 -> Deserialize entire blockchain](#org6c651d9)
|
|
8
|
|
9 
|
|
10
|
|
11
|
|
12 <a id="org79ef3e8"></a>
|
|
13
|
|
14 # Installation
|
|
15
|
|
16 Recommended:
|
|
17
|
|
18 pip install bitcaviar-plus
|
|
19
|
|
20 Manual:
|
|
21
|
|
22 python setup.py install
|
|
23
|
|
24
|
|
25 <a id="org47a2d85"></a>
|
|
26
|
|
27 # Usage
|
|
28
|
|
29
|
|
30 <a id="orgb8ccd7c"></a>
|
|
31
|
|
32 ## Example 1 -> Deserialize genesis block
|
|
33
|
|
34 from bitcaviar_plus.block import deserialize_block
|
|
35
|
|
36
|
|
37 def parse_genesis_block():
|
|
38 with open('path/to/file/blk00000.dat', 'rb') as f:
|
|
39 block = deserialize_block(f)
|
|
40 print(block)
|
|
41
|
|
42
|
|
43 <a id="org6c651d9"></a>
|
|
44
|
|
45 ## Example 2 -> Deserialize entire blockchain
|
|
46
|
|
47 import os
|
|
48 from bitcaviar_plus.block import deserialize_block
|
|
49 from bitcaviar_plus.errors import InvalidMagicBytes
|
|
50
|
|
51
|
|
52 def parse_entire_blockchain():
|
|
53 file_counter = -1
|
|
54 while True:
|
|
55 file_counter += 1
|
|
56 file_name = 'path/to/file/blk{}.dat'.format(str(file_counter).zfill(5))
|
|
57 with open(file_name, 'rb') as f:
|
|
58 file_size = os.path.getsize(file_name)
|
|
59 while f.tell() < file_size:
|
|
60 try:
|
|
61 block = deserialize_block(f)
|
|
62 except InvalidMagicBytes as e:
|
|
63 print(e)
|
|
64
|
|
65 Example output:
|
|
66
|
|
67 {
|
|
68 "magic_number":"f9beb4d9",
|
|
69 "size":"0000011d",
|
|
70 "id":"000000000019d6...",
|
|
71 "transaction_count":"01",
|
|
72 "header":{
|
|
73 "version":"00000001",
|
|
74 "previous_block_id":"00000000000000...",
|
|
75 "merkle_root":"4a5e1e4baab89f3a32...",
|
|
76 "time":"495fab29",
|
|
77 "bits":"1d00ffff",
|
|
78 "nonce":"7c2bac1d"
|
|
79 },
|
|
80 "transactions":[
|
|
81 {
|
|
82 "version":"00000001",
|
|
83 "input_count":"01",
|
|
84 "output_count":"01",
|
|
85 "lock_time":"00000000",
|
|
86 "id":"4a5e1e4baab89f3a32518a8...",
|
|
87 "inputs":[
|
|
88 {
|
|
89 "id":"0000000000000000000000...",
|
|
90 "vout":"ffffffff",
|
|
91 "script_sig_size":"4d",
|
|
92 "script_sig":"04ffff001d01044554686520546...",
|
|
93 "sequence":"ffffffff"
|
|
94 }
|
|
95 ],
|
|
96 "outputs":[
|
|
97 {
|
|
98 "value":"000000012a05f200",
|
|
99 "script_pub_key_size":"43",
|
|
100 "script_pub_key":"4104678afdb0fe55482719..."
|
|
101 }
|
|
102 ]
|
|
103 }
|
|
104 ]
|
|
105 }
|
|
106
|