-
Notifications
You must be signed in to change notification settings - Fork 10
/
hashrate_ttd.py
96 lines (76 loc) · 2.97 KB
/
hashrate_ttd.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
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
# Calculates and visualizes how much hashrate is needed to achieve given TTD during September
# Run with TTD to estimate in argument, like so:
# python3 hashrate_ttd.py --ttd 58750000000000000000000
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as md
from web3 import Web3
import datetime as dt
import argparse
import time
#Connect to web3 provider
#web3 = Web3(Web3.HTTPProvider("http://127.0.0.1:8545"))
T = lambda blockn: web3.eth.getBlock(blockn).timestamp
TTD = lambda blockn: web3.eth.getBlock(blockn).totalDifficulty
latest_block = web3.eth.get_block('latest')['number']
def estimate_hashrate(target):
current_ttd = web3.eth.get_block('latest')['totalDifficulty']
time_now=web3.eth.get_block('latest')['timestamp']
hashrate=((TTD("latest")-TTD(latest_block-6450))/(T("latest")-T(latest_block-6200))/1000000000000) #hashrate in roughly past day
time_targets=[1663149600, 1663236000, 1663322400, 1663848000, 1664539200]
hashrate_projection=int((target-current_ttd)/(hashrate*1000000000000))+time_now
start=max(hashrate_projection-86400, int(time.time())+43200)
end=hashrate_projection+172800
t=[]
t.append(start)
i=0
while t[i] < end:
i+=1
t.append(t[i-1]+43200)
i=0
h=[]
p=[]
for timet in t:
h.append(((target-current_ttd)/(timet-time_now)/1000000000000))
for hash_t in h:
p.append(((hashrate-hash_t)/hashrate)*(-100))
conv=np.vectorize(dt.datetime.fromtimestamp)
dates=conv(t)
ax=plt.gca()
plt.subplots_adjust(bottom=0.2)
plt.xticks( rotation=25 )
xfmt = md.DateFormatter('%Y-%m-%d')
ax.xaxis.set_major_formatter(xfmt)
plt.title("Hashrate change")
ax.set_ylabel('Δ %')
plt.plot(dates, p)
plt.axhline(y = 0, color = 'r', linestyle = 'dashed')
plt.savefig('percent_delta.png')
ax.grid(True)
# plt.show()
plt.clf()
ax.clear()
ax=plt.gca()
plt.subplots_adjust(bottom=0.2)
plt.xticks( rotation=25 )
xfmt = md.DateFormatter('%Y-%m-%d')
ax.xaxis.set_major_formatter(xfmt)
plt.title("Hashrate to achieve TTD")
ax.set_ylabel('TH/s')
plt.plot(dates, h)
plt.axhline(y = hashrate, color = 'r', linestyle = 'dashed')
plt.plot(conv(hashrate_projection), hashrate, 'ro', color='green')
ax.grid(True)
plt.savefig('hashrate_delta.png')
plt.show()
for time_target in time_targets:
hash_target=((target-current_ttd)/(time_target-time_now)/1000000000000)
delta=((hashrate-hash_target)/hashrate)*100
print("To achieve TTD", target, "at", dt.datetime.utcfromtimestamp(time_target).strftime("%a %b %d %H:%M %Y"),"UTC, around %.1f TH/s in the network is needed as of now." % hash_target)
print("That is around", int(delta), "% change from current hashrate")
ap = argparse.ArgumentParser()
ap.add_argument("--ttd", required=True,
help="TTD value to estimate")
args = vars(ap.parse_args())
target = int(args['ttd'])
estimate_hashrate(target)