forked from tuxsoul/bitcoin-tools
/
statistics.py
executable file
·67 lines (57 loc) · 1.77 KB
/
statistics.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
#
# Read the block database, generate monthly statistics and dump out
# a CSV file.
#
from bsddb.db import *
from datetime import date
import logging
import os
import sys
from BCDataStream import *
from block import scan_blocks
from deserialize import parse_Block
from util import determine_db_dir, create_env
def main():
import optparse
parser = optparse.OptionParser(usage="%prog [options]")
parser.add_option("--datadir", dest="datadir", default=None,
help="Look for files here (defaults to bitcoin default)")
(options, args) = parser.parse_args()
if options.datadir is None:
db_dir = determine_db_dir()
else:
db_dir = options.datadir
try:
db_env = create_env(db_dir)
except DBNoSuchFileError:
logging.error("Couldn't open " + db_dir)
sys.exit(1)
blockfile = open(os.path.join(db_dir, "blk%04d.dat"%(1,)), "rb")
block_datastream = BCDataStream()
block_datastream.map_file(blockfile, 0)
n_transactions = { }
v_transactions = { }
def gather_stats(block_data):
block_datastream.seek_file(block_data['nBlockPos'])
data = parse_Block(block_datastream)
block_date = date.fromtimestamp(data['nTime'])
key = "%d-%02d"%(block_date.year, block_date.month)
for txn in data['transactions'][1:]:
for txout in txn['txOut']:
if key in n_transactions:
n_transactions[key] += 1
v_transactions[key] += txout['value']
else:
n_transactions[key] = 1
v_transactions[key] = txout['value']
return True
scan_blocks(db_dir, db_env, gather_stats)
db_env.close()
keys = n_transactions.keys()
keys.sort()
for k in keys:
v = v_transactions[k]/1.0e8
print "%s,%d,%.2f"%(k, n_transactions[k], v)
if __name__ == '__main__':
main()