-
Notifications
You must be signed in to change notification settings - Fork 3
/
testblock.py
177 lines (154 loc) · 5.93 KB
/
testblock.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
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
#! /usr/bin/env python
import urllib2
import re
debug = False
class bitcoinapi():
def _debug(self, message):
if debug == True:
print message
def _grabapi(self, apipaths):
# This will attempt to grab the information using blockchain first, then blockexplorer.
sources = ['http://blockchain.info', 'http://blockexplorer.com']
urls = [''.join(t) for t in zip(sources, apipaths)]
# print urls
for url in urls:
try:
self._debug ("Getting: " + url)
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = { 'User-Agent' : user_agent }
bitOpen = urllib2.Request(url, "", headers)
data = urllib2.urlopen(bitOpen).read()
#data = urlopen(url, timeout=5).read()
if data == '':
self._debug ("Got a blank response")
continue
self._debug ("Got: " + data)
return data
except Exception, e:
self._debug (e)
continue
return None
def get_interval(self):
# Average time between blocks in seconds
currentint = self._grabapi(['/q/interval']*2)
if currentint is None or currentint == '':
self._debug ("Opps, there was an error, try later")
return
return float(currentint)
def get_currentblock(self):
# Current block height in the longest chain
currentblock = self._grabapi(['/q/getblockcount']*2)
if currentblock is None or currentblock == '':
self._debug ("Opps, there was an error, try later")
return
return float(currentblock)
def get_hashrate(self):
# Estimated network hash rate in gigahash
hashrate = self._grabapi(['/q/hashrate']*2)
if hashrate is None or hashrate == '':
self._debug ("Opps, there was an error, try later")
return
return float(hashrate)
def get_nextdifficulty(self):
# Get the next difficulty (Only available from blockexplorer)
nextdiff = self._grabapi(['/q/estimate']*2)
if nextdiff is None or nextdiff == '':
self._debug ("Opps, there was an error, try later")
return
return float(nextdiff)
def get_difficulty(self):
# Current difficulty target as a decimal number
currentdiff = self._grabapi(['/q/getdifficulty']*2)
if currentdiff is None or currentdiff == '':
self._debug ("Opps, there was an error, try later")
return
return float(currentdiff)
def get_nextretarget(self):
# Block height of the next difficulty retarget
nextretarget = self._grabapi(['/q/nextretarget']*2)
if nextretarget is None or nextretarget == '':
self._debug ("Opps, there was an error, try later")
return
return float(nextretarget)
def stat_hash(self):
# Hash Stats
currentint = self.get_interval()
currentblock = self.get_currentblock()
hashrate = self.get_hashrate()
if currentint is None or currentblock is None or hashrate is None:
return "There was an error, please try again later"
m, s = divmod(currentint, 60)
abouttime = "%02d Minutes %02d Seconds" % (m, s)
data = "Estimated interval between blocks: %s | Current Block: %d | Global Hashrate: %.2f GH/s " % (abouttime, currentblock, hashrate)
return data
def stat_diff(self):
# Difficulty Stats
nextretarget = self.get_nextretarget()
currentblock = self.get_currentblock()
nextdiff = self.get_nextdifficulty()
currentdiff = self.get_difficulty()
currentint = self.get_interval()
if nextretarget is None or currentblock is None or nextdiff is None or currentdiff is None or currentint is None:
return "There was an error, please try again later"
nextin = nextretarget - currentblock
diffchange = (nextdiff - currentdiff) / currentdiff * 100
timetochange = nextin * currentint
m, s = divmod(timetochange, 60)
h, m = divmod(m, 60)
d, h = divmod(h, 24)
abouttime = "%d Days %d Hours %02d Minutes %02d Seconds" % (d, h, m, s)
data = "Cur Dif: %.2f | Est Next: %.2f (%.2f%%) | Next Diff change in: %d Blocks (~%s)" % (currentdiff, nextdiff, diffchange, nextin, abouttime)
return data
def gettxid(self, txid):
#For now we will just use blockchain.info since block explorers is a bit different. Besides, soon I will be downloading the blockchain
tx = urllib2.urlopen('http://blockchain.info/rawtx/'+txid)
tx=tx.read()
print tx
if tx is None or tx == '':
self._debug ("Opps, there was an error, try later")
return
return tx
def stat_estimate(self, hashrate, quote=0):
currentdiff = self.get_difficulty()
if currentdiff is None:
return "There was an error, please try again later"
estimate = 24 / (currentdiff * 2**32 / ( hashrate * 10**6) / 60 / 60) * 25
if quote == 0:
data = "At %d MH/s, you should earn on avg ~%f BTC/day or %f BTC/hr." % (hashrate, estimate, (estimate/24))
else:
data = "At %d MH/s, you should earn on avg ~%f BTC/day ($%f USD) or %f BTC/hr." % (hashrate, estimate, (estimate*quote), (estimate/24))
return data
if __name__ == "__main__":
btcapi = bitcoinapi()
# Unit tests
ch=""
t=0
r=0
import msvcrt as m
import time
import calendar
import datetime
print calendar.isleap(2014)
timex = urllib2.urlopen("http://just-the-time.appspot.com/").read()
matchObj = re.match( r'(.*)-(.*?)-(.*?) (.*?):(.*?):(.*?) UTC', timex, re.M|re.I)
HaloTimeHour = datetime.datetime(int(matchObj.group(1)),int(matchObj.group(2)),int(matchObj.group(3)),int(matchObj.group(4)),int(matchObj.group(5)),int(matchObj.group(6)))
print HaloTimeHour
print timex
print datetime.date.today
print datetime.datetime
print btcapi.gettxid("59e0a90ebe8fa15a9ee828fa67d417d391338b01d87bdfc7d0482b3f74af9932")
m.getch()
print "block:", btcapi.get_currentblock(), " interval:",r
while ch == "":
time.sleep(60)
t+=1
if t==10:
r+=1
t=0
print "block:", btcapi.get_currentblock(), " interval:",r
print btcapi.get_currentblock()
print btcapi.get_nextretarget()
print btcapi.stat_hash()
print btcapi.stat_diff()
print btcapi.stat_estimate (100)
print btcapi.stat_estimate (100, 118.4)