-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathminingrate.py
More file actions
executable file
·191 lines (164 loc) · 7.55 KB
/
miningrate.py
File metadata and controls
executable file
·191 lines (164 loc) · 7.55 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python
###########################################################################
# author: JanKalin
#
# Calculates an estimate of mining rate on this computer
###########################################################################
import argparse
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
from prettytable import PrettyTable
from scipy import stats
from zcutils import *
###########################################################################
# Printout functions
###########################################################################
def time(timestamp):
return datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
def dtime(delta):
return str(datetime.timedelta(seconds=int(np.round(delta))))
###########################################################################
parser = argparse.ArgumentParser(description="Estimate mining rate by reading transactions",
fromfile_prefix_chars='@',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--zcash_cli", help="Path to zcash-cli binary.", default="~/zcash/src/zcash-cli")
parser.add_argument("--transactions", help="Process this many transactions", default=10000)
groupdata = parser.add_mutually_exclusive_group()
groupdata.add_argument("--savedata", help="Saves data to 'miningrate.npy' after loading it from blockchain", action='store_true')
groupdata.add_argument("--loaddata", help="Loads data from 'miningrate.npy' instead of loading it from blockchain", action='store_true')
parser.add_argument("--notable", help="Do not print out table", action='store_true')
parser.add_argument("--nostats", help="Do not print out stats", action='store_true')
parser.add_argument("--noprogress", help="Do not display progress", action='store_true')
parser.add_argument("--graph", help="Display graph", action='store_true')
parser.add_argument("--maxdtime", help="Maximum dtime in minutes. Used to skip periods of no activity", type=int, default=0)
args = parser.parse_args()
# dtype for data
dtype=[('height', int), ('time', int), ('dheight', int), ('dtime', int), ('blocktime', float), ('immature', int), ('difficulty', float)]
# Perhaps load data
if args.loaddata:
data = np.load('miningrate.npy')
else:
# Create the object, get transactions and find the interesting ones
zcli = zcli.zcli(args.zcash_cli)
if not args.noprogress:
print "Getting transactions ...",
transactions = zcli.listtransactions(args.transactions)
if not args.noprogress:
print "100%"
generated = [x for x in transactions if 'generated' in x]
if not len(generated):
print "Sorry, no coins mined on this computer"
exit(0)
# Create a numpy array and fill it
data = np.zeros((len(generated),), dtype=dtype)
if len(generated) > 1 and not args.noprogress:
progress = utils.Progress("Reading {} blocks ... {{}}% ".format(len(generated)), len(generated))
for idx, transaction in enumerate(generated):
if len(generated) > 1 and not args.noprogress:
progress.step()
block = zcli.getblock(transaction['blockhash'])
for field in ['height', 'time', 'difficulty']:
data[field][idx] = block[field]
data['immature'][idx] = transaction['category'] == 'immature'
data['dheight'][1:] = np.diff(data['height'])
data['dtime'][1:] = np.diff(data['time'])
data['blocktime'][1:] = data['dtime'][1:]/data['dheight'][1:]
if args.maxdtime:
where = np.where(data['dtime'] > 60.*args.maxdtime)
data = np.delete(data, where)
# Perhaps save data
if args.savedata:
np.save('miningrate.npy', data)
# Table
if not args.notable:
table = PrettyTable(['height', 'time', 'difficulty', 'immature', 'dheight', 'dtime'])
table.align['dheight'] = 'r'
table.float_format = "9.2"
for idx in range(len(data)):
row = [data['height'][idx], time(data['time'][idx]), data['difficulty'][idx], "*" if data['immature'][idx] else ""]
if idx:
row += [data['dheight'][idx], dtime(data['dtime'][idx])]
else:
row += ["", ""]
table.add_row(row)
print table
# Stats
if not args.nostats:
table = PrettyTable(['qty', 'mean', 'std', 'median'])
table.float_format = "7.2"
table.align['qty'] = 'r'
table.add_row(['difficulty',
np.mean(data['difficulty'][1:]),
np.std(data['difficulty'][1:]),
np.median(data['difficulty'][1:])])
table.add_row(['dheight',
np.mean(data['dheight'][1:]),
np.std(data['dheight'][1:]),
np.median(data['dheight'][1:])])
table.add_row(['blockttime',
dtime(np.mean(data['blocktime'][1:])),
dtime(np.std(data['blocktime'][1:])),
dtime(np.median(data['blocktime'][1:]))])
table.add_row(['dtime',
dtime(np.mean(data['dtime'][1:])),
dtime(np.std(data['dtime'][1:])),
dtime(np.median(data['dtime'][1:]))])
try:
delta = 1.*(len(data)-1)/(data['time'][-1] - data['time'][1])*86400
except:
delta = 0
immature = len(np.where(data['immature'])[0]);
table.add_row(['blocks', "{} + {}".format(len(data)-immature, immature), "", ""])
table.add_row(['blocks/day', delta, "", ""])
print table
# Get per-day stats
days = np.unique([datetime.date.fromtimestamp(x) for x in data['time']])
daydata = np.zeros((len(days),), dtype=[('time', object), ('dtime', object), ('count', int)])
for idx, day in enumerate(days):
where = [jdx for (jdx, val) in enumerate(data['time']) if datetime.date.fromtimestamp(val) == day]
daydata['time'][idx] = day.strftime("%Y-%m-%d")
daydata['dtime'][idx] = dtime(np.mean(data['dtime'][where]))
daydata['count'][idx] = len(where)
table = PrettyTable(['day', 'dtime', 'count'])
table.align['count'] = 'r'
for idx in range(len(daydata)):
table.add_row([daydata['time'][idx], daydata['dtime'][idx], daydata['count'][idx]])
print table
# Histograms
if args.graph:
xfmt = mdates.DateFormatter("%d.%m. %H:%M")
yfmt = mdates.DateFormatter("%H:%M:%S")
fig = plt.figure()
fig.autofmt_xdate()
times = [datetime.datetime.fromtimestamp(x) for x in data['time'][1:]]
deltas = [datetime.datetime.fromtimestamp(x-3600) for x in data['dtime'][1:]]
ax = fig.add_subplot(311)
ax.plot(times, deltas)
ax.plot(times, [datetime.datetime.fromtimestamp(np.mean(data['dtime'][1:])-3600)]*len(times))
ax.xaxis.set_major_formatter(xfmt)
ax.yaxis.set_major_formatter(yfmt)
plt.xticks(rotation='vertical')
times = np.array([datetime.datetime.fromtimestamp(x) for x in daydata['time']])
deltas = np.array([datetime.datetime.fromtimestamp(x-3600) for x in daydata['dtime']])
try:
res = stats.mstats.theilslopes(daydata['dtime'], daydata['time'], 0.90)
except:
res = None
ax = fig.add_subplot(312)
ax.plot(times, deltas)
if res:
try:
ax.plot(times, [datetime.datetime.fromtimestamp(x-3600) for x in res[1] + res[0] * daydata['time']], 'r-')
except:
pass
ax.xaxis.set_major_formatter(xfmt)
ax.yaxis.set_major_formatter(yfmt)
ax.set_ylim(int(ax.get_ylim()[0]), (1.0/24)*(int(ax.get_ylim()[1]*24)+1))
plt.xticks(rotation='vertical')
ax = fig.add_subplot(313)
ax.plot(times, daydata['count'])
ax.xaxis.set_major_formatter(xfmt)
plt.xticks(rotation='vertical')
plt.show()