PPCoin: Release alert and checkpoint master public keys
[novacoin.git] / bitcointools / blkindex.py
1 #
2 # Code for parsing the blkindex.dat file
3 #
4
5 from bsddb.db import *
6 import logging
7 from operator import itemgetter
8 import sys
9 import time
10
11 from BCDataStream import *
12 from base58 import public_key_to_bc_address
13 from util import short_hex
14 from deserialize import *
15
16 def dump_blkindex_summary(db_env):
17   db = DB(db_env)
18   try:
19     r = db.open("blkindex.dat", "main", DB_BTREE, DB_THREAD|DB_RDONLY)
20   except DBError:
21     r = True
22
23   if r is not None:
24     logging.error("Couldn't open blkindex.dat/main.  Try quitting any running Bitcoin apps.")
25     sys.exit(1)
26
27   kds = BCDataStream()
28   vds = BCDataStream()
29
30   n_tx = 0
31   n_blockindex = 0
32
33   print("blkindex file summary:")
34   for (key, value) in db.items():
35     kds.clear(); kds.write(key)
36     vds.clear(); vds.write(value)
37
38     type = kds.read_string()
39
40     if type == "tx":
41       n_tx += 1
42     elif type == "blockindex":
43       n_blockindex += 1
44     elif type == "version":
45       version = vds.read_int32()
46       print(" Version: %d"%(version,))
47     elif type == "hashBestChain":
48       hash = vds.read_bytes(32)
49       print(" HashBestChain: %s"%(hash.encode('hex_codec'),))
50     else:
51       logging.warn("blkindex: unknown type '%s'"%(type,))
52       continue
53
54   print(" %d transactions, %d blocks."%(n_tx, n_blockindex))
55   db.close()