3 # Read the block database, generate monthly statistics and dump out
7 from datetime import date
12 from BCDataStream import *
13 from block import scan_blocks
14 from collections import defaultdict
15 from deserialize import parse_Block
16 from util import determine_db_dir, create_env
20 parser = optparse.OptionParser(usage="%prog [options]")
21 parser.add_option("--datadir", dest="datadir", default=None,
22 help="Look for files here (defaults to bitcoin default)")
23 (options, args) = parser.parse_args()
25 if options.datadir is None:
26 db_dir = determine_db_dir()
28 db_dir = options.datadir
31 db_env = create_env(db_dir)
32 except DBNoSuchFileError:
33 logging.error("Couldn't open " + db_dir)
36 blockfile = open(os.path.join(db_dir, "blk%04d.dat"%(1,)), "rb")
37 block_datastream = BCDataStream()
38 block_datastream.map_file(blockfile, 0)
40 n_transactions = defaultdict(int)
41 v_transactions = defaultdict(float)
42 v_transactions_min = defaultdict(float)
43 v_transactions_max = defaultdict(float)
44 def gather_stats(block_data):
45 block_datastream.seek_file(block_data['nBlockPos'])
46 data = parse_Block(block_datastream)
47 block_date = date.fromtimestamp(data['nTime'])
48 key = "%d-%02d"%(block_date.year, block_date.month)
49 for txn in data['transactions'][1:]:
51 for txout in txn['txOut']:
52 n_transactions[key] += 1
53 v_transactions[key] += txout['value']
54 values.append(txout['value'])
55 v_transactions_min[key] += min(values)
56 v_transactions_max[key] += max(values)
59 scan_blocks(db_dir, db_env, gather_stats)
63 keys = n_transactions.keys()
66 v = v_transactions[k]/1.0e8
67 v_min = v_transactions_min[k]/1.0e8
68 v_max = v_transactions_max[k]/1.0e8
70 # month n_transactions min max total
71 # ... where min and max add up just the smallest or largest
72 # output in each transaction; the true value of bitcoins
73 # transferred will be somewhere between min and max.
74 # We don't know how many are transfers-to-self, though, and
75 # this will undercount multi-txout-transactions (which is good
76 # right now, because they're mostly used for mining pool
77 # payouts that arguably shouldn't count).
78 print "%s,%d,%.2f,%.2f,%.2f"%(k, n_transactions[k], v_min, v_max, v)
80 if __name__ == '__main__':