1 from __future__ import division
9 from twisted.internet import reactor, task
10 from twisted.python import log
11 from twisted.web import resource
13 from bitcoin import data as bitcoin_data
14 from . import data as p2pool_data, graphs
17 def get_web_root(tracker, current_work, current_work2, get_current_txouts, datadir_path, net, get_stale_counts, my_pubkey_hash, local_rate_monitor, worker_fee, p2p_node, my_share_hashes, recent_blocks, pseudoshare_received):
18 start_time = time.time()
20 web_root = resource.Resource()
23 if tracker.get_height(current_work.value['best_share_hash']) < 720:
24 return json.dumps(None)
25 return json.dumps(p2pool_data.get_pool_attempts_per_second(tracker, current_work.value['best_share_hash'], 720)
26 / (1 - p2pool_data.get_average_stale_prop(tracker, current_work.value['best_share_hash'], 720)))
29 height, last = tracker.get_height_and_last(current_work.value['best_share_hash'])
30 weights, total_weight, donation_weight = tracker.get_cumulative_weights(current_work.value['best_share_hash'], min(height, 720), 65535*2**256)
32 for script in sorted(weights, key=lambda s: weights[s]):
33 res[bitcoin_data.script2_to_human(script, net.PARENT)] = weights[script]/total_weight
34 return json.dumps(res)
36 def get_current_scaled_txouts(scale, trunc=0):
37 txouts = get_current_txouts()
38 total = sum(txouts.itervalues())
39 results = dict((script, value*scale//total) for script, value in txouts.iteritems())
43 for s in sorted(results, key=results.__getitem__):
44 if results[s] >= trunc:
46 total_random += results[s]
49 winner = math.weighted_choice((script, results[script]) for script in random_set)
50 for script in random_set:
52 results[winner] = total_random
53 if sum(results.itervalues()) < int(scale):
54 results[math.weighted_choice(results.iteritems())] += int(scale) - sum(results.itervalues())
57 def get_current_payouts():
58 return json.dumps(dict((bitcoin_data.script2_to_human(script, net.PARENT), value/1e8) for script, value in get_current_txouts().iteritems()))
60 def get_patron_sendmany(this):
63 this, trunc = this.split('/', 1)
66 return json.dumps(dict(
67 (bitcoin_data.script2_to_address(script, net.PARENT), value/1e8)
68 for script, value in get_current_scaled_txouts(scale=int(float(this)*1e8), trunc=int(float(trunc)*1e8)).iteritems()
69 if bitcoin_data.script2_to_address(script, net.PARENT) is not None
73 return json.dumps(None)
75 def get_global_stats():
76 # averaged over last hour
77 lookbehind = 3600//net.SHARE_PERIOD
78 if tracker.get_height(current_work.value['best_share_hash']) < lookbehind:
81 nonstale_hash_rate = p2pool_data.get_pool_attempts_per_second(tracker, current_work.value['best_share_hash'], lookbehind)
82 stale_prop = p2pool_data.get_average_stale_prop(tracker, current_work.value['best_share_hash'], lookbehind)
83 return json.dumps(dict(
84 pool_nonstale_hash_rate=nonstale_hash_rate,
85 pool_hash_rate=nonstale_hash_rate/(1 - stale_prop),
86 pool_stale_prop=stale_prop,
89 def get_local_stats():
90 lookbehind = 3600//net.SHARE_PERIOD
91 if tracker.get_height(current_work.value['best_share_hash']) < lookbehind:
94 global_stale_prop = p2pool_data.get_average_stale_prop(tracker, current_work.value['best_share_hash'], lookbehind)
96 my_unstale_count = sum(1 for share in tracker.get_chain(current_work.value['best_share_hash'], lookbehind) if share.hash in my_share_hashes)
97 my_orphan_count = sum(1 for share in tracker.get_chain(current_work.value['best_share_hash'], lookbehind) if share.hash in my_share_hashes and share.share_data['stale_info'] == 253)
98 my_doa_count = sum(1 for share in tracker.get_chain(current_work.value['best_share_hash'], lookbehind) if share.hash in my_share_hashes and share.share_data['stale_info'] == 254)
99 my_share_count = my_unstale_count + my_orphan_count + my_doa_count
100 my_stale_count = my_orphan_count + my_doa_count
102 my_stale_prop = my_stale_count/my_share_count if my_share_count != 0 else None
104 my_work = sum(bitcoin_data.target_to_average_attempts(share.target)
105 for share in tracker.get_chain(current_work.value['best_share_hash'], lookbehind - 1)
106 if share.hash in my_share_hashes)
107 actual_time = (tracker.shares[current_work.value['best_share_hash']].timestamp -
108 tracker.shares[tracker.get_nth_parent_hash(current_work.value['best_share_hash'], lookbehind - 1)].timestamp)
109 share_att_s = my_work / actual_time
111 miner_hash_rates = {}
112 miner_dead_hash_rates = {}
113 datums, dt = local_rate_monitor.get_datums_in_last()
115 miner_hash_rates[datum['user']] = miner_hash_rates.get(datum['user'], 0) + datum['work']/dt
117 miner_dead_hash_rates[datum['user']] = miner_dead_hash_rates.get(datum['user'], 0) + datum['work']/dt
119 return json.dumps(dict(
120 my_hash_rates_in_last_hour=dict(
122 nonstale=share_att_s,
123 rewarded=share_att_s/(1 - global_stale_prop),
124 actual=share_att_s/(1 - my_stale_prop) if my_stale_prop is not None else 0, # 0 because we don't have any shares anyway
126 my_share_counts_in_last_hour=dict(
127 shares=my_share_count,
128 unstale_shares=my_unstale_count,
129 stale_shares=my_stale_count,
130 orphan_stale_shares=my_orphan_count,
131 doa_stale_shares=my_doa_count,
133 my_stale_proportions_in_last_hour=dict(
135 orphan_stale=my_orphan_count/my_share_count if my_share_count != 0 else None,
136 dead_stale=my_doa_count/my_share_count if my_share_count != 0 else None,
138 miner_hash_rates=miner_hash_rates,
139 miner_dead_hash_rates=miner_dead_hash_rates,
142 def get_peer_addresses():
143 return ' '.join(peer.transport.getPeer().host + (':' + str(peer.transport.getPeer().port) if peer.transport.getPeer().port != net.P2P_PORT else '') for peer in p2p_node.peers.itervalues())
146 return json.dumps(time.time() - start_time)
148 class WebInterface(resource.Resource):
149 def __init__(self, func, mime_type, *fields):
150 self.func, self.mime_type, self.fields = func, mime_type, fields
152 def render_GET(self, request):
153 request.setHeader('Content-Type', self.mime_type)
154 request.setHeader('Access-Control-Allow-Origin', '*')
155 return self.func(*(request.args[field][0] for field in self.fields))
157 web_root.putChild('rate', WebInterface(get_rate, 'application/json'))
158 web_root.putChild('users', WebInterface(get_users, 'application/json'))
159 web_root.putChild('fee', WebInterface(lambda: json.dumps(worker_fee), 'application/json'))
160 web_root.putChild('current_payouts', WebInterface(get_current_payouts, 'application/json'))
161 web_root.putChild('patron_sendmany', WebInterface(get_patron_sendmany, 'text/plain', 'total'))
162 web_root.putChild('global_stats', WebInterface(get_global_stats, 'application/json'))
163 web_root.putChild('local_stats', WebInterface(get_local_stats, 'application/json'))
164 web_root.putChild('peer_addresses', WebInterface(get_peer_addresses, 'text/plain'))
165 web_root.putChild('payout_addr', WebInterface(lambda: json.dumps(bitcoin_data.pubkey_hash_to_address(my_pubkey_hash, net.PARENT)), 'application/json'))
166 web_root.putChild('recent_blocks', WebInterface(lambda: json.dumps(recent_blocks), 'application/json'))
167 web_root.putChild('uptime', WebInterface(get_uptime, 'application/json'))
171 web_root.putChild('chain_img', WebInterface(lambda: draw.get(tracker, current_work.value['best_share_hash']), 'image/png'))
173 print "Install Pygame and PIL to enable visualizations! Visualizations disabled."
175 new_root = resource.Resource()
176 web_root.putChild('web', new_root)
179 if os.path.exists(os.path.join(datadir_path, 'stats')):
181 with open(os.path.join(datadir_path, 'stats'), 'rb') as f:
182 stat_log = json.loads(f.read())
184 log.err(None, 'Error loading stats:')
185 def update_stat_log():
186 while stat_log and stat_log[0]['time'] < time.time() - 24*60*60:
189 lookbehind = 3600//net.SHARE_PERIOD
190 if tracker.get_height(current_work.value['best_share_hash']) < lookbehind:
193 global_stale_prop = p2pool_data.get_average_stale_prop(tracker, current_work.value['best_share_hash'], lookbehind)
194 (stale_orphan_shares, stale_doa_shares), shares, _ = get_stale_counts()
196 miner_hash_rates = {}
197 miner_dead_hash_rates = {}
198 datums, dt = local_rate_monitor.get_datums_in_last()
200 miner_hash_rates[datum['user']] = miner_hash_rates.get(datum['user'], 0) + datum['work']/dt
202 miner_dead_hash_rates[datum['user']] = miner_dead_hash_rates.get(datum['user'], 0) + datum['work']/dt
204 stat_log.append(dict(
206 pool_hash_rate=p2pool_data.get_pool_attempts_per_second(tracker, current_work.value['best_share_hash'], lookbehind)/(1-global_stale_prop),
207 pool_stale_prop=global_stale_prop,
208 local_hash_rates=miner_hash_rates,
209 local_dead_hash_rates=miner_dead_hash_rates,
211 stale_shares=stale_orphan_shares + stale_doa_shares,
212 stale_shares_breakdown=dict(orphan=stale_orphan_shares, doa=stale_doa_shares),
213 current_payout=get_current_txouts().get(bitcoin_data.pubkey_hash_to_script2(my_pubkey_hash), 0)*1e-8,
215 incoming=sum(1 for peer in p2p_node.peers.itervalues() if peer.incoming),
216 outgoing=sum(1 for peer in p2p_node.peers.itervalues() if not peer.incoming),
218 attempts_to_share=bitcoin_data.target_to_average_attempts(tracker.shares[current_work.value['best_share_hash']].max_target),
219 attempts_to_block=bitcoin_data.target_to_average_attempts(current_work.value['bits'].target),
220 block_value=current_work2.value['subsidy']*1e-8,
223 with open(os.path.join(datadir_path, 'stats'), 'wb') as f:
224 f.write(json.dumps(stat_log))
225 task.LoopingCall(update_stat_log).start(5*60)
226 new_root.putChild('log', WebInterface(lambda: json.dumps(stat_log), 'application/json'))
228 class ShareExplorer(resource.Resource):
229 def __init__(self, share_hash):
230 self.share_hash = share_hash
231 def render_GET(self, request):
232 request.setHeader('Content-Type', 'text/html')
233 if self.share_hash not in tracker.shares:
234 return 'share not known'
235 share = tracker.shares[self.share_hash]
237 format_bits = lambda bits: '%f (bits=%#8x) Work required: %sH</p>' % (bitcoin_data.target_to_difficulty(bits.target), bits.bits, math.format(bitcoin_data.target_to_average_attempts(bits.target)))
239 request.write('<h1>Share <a href="%x">%s</a></h1>' % (share.hash, p2pool_data.format_hash(share.hash)))
240 request.write('<p>Previous: <a href="%x">%s</a></p>' % (share.previous_hash, p2pool_data.format_hash(share.previous_hash)))
241 request.write('<p>Next: %s</p>' % (', '.join('<a href="%x">%s</a>' % (next, p2pool_data.format_hash(next)) for next in tracker.reverse_shares.get(share.hash, set())),))
242 request.write('<p>Verified: %s</p>' % (share.hash in tracker.verified.shares,))
243 request.write('<p>Time first seen: %s</p>' % (time.ctime(start_time if share.time_seen == 0 else share.time_seen),))
244 request.write('<p>Peer first received from: %s</p>' % ('%s:%i' % share.peer.addr if share.peer is not None else 'self or cache',))
246 request.write('<h2>Share data</h2>')
247 request.write('<p>Timestamp: %s</p>' % (time.ctime(share.timestamp),))
248 request.write('<p>Difficulty: %s</p>' % (format_bits(share.share_info['bits']),))
249 request.write('<p>Minimum difficulty: %s</p>' % (format_bits(share.share_info.get('max_bits', share.share_info['bits'])),))
250 request.write('<p>Payout script: %s</p>' % (bitcoin_data.script2_to_human(share.new_script, share.net.PARENT),))
251 request.write('<p>Donation: %.2f%%</p>' % (share.share_data['donation']/65535*100,))
252 request.write('<p>Stale info: %s</p>' % ({0: 'none', 253: 'had an orphan', 254: 'had a dead'}.get(share.share_data['stale_info'], 'unknown %i' % (share.share_data['stale_info'],)),))
253 request.write('<p>Nonce: %s</p>' % (cgi.escape(repr(share.share_data['nonce'])),))
255 request.write('<h2>Block header</h2>')
256 request.write('<p>Hash: <a href="%s%064x">%064x</a></p>' % (net.PARENT.BLOCK_EXPLORER_URL_PREFIX, share.header_hash, share.header_hash))
257 request.write('<p>Version: %i</p>' % (share.header['version'],))
258 request.write('<p>Previous block: <a href="%s%064x">%064x</a></p>' % (net.PARENT.BLOCK_EXPLORER_URL_PREFIX, share.header['previous_block'], share.header['previous_block']))
259 request.write('<p>Timestamp: %s (%i)</p>' % (time.ctime(share.header['timestamp']), share.header['timestamp']))
260 request.write('<p>Difficulty: %f (bits=%#8x) Work: %sH</p>' % (bitcoin_data.target_to_difficulty(share.header['bits'].target), share.header['bits'].bits, math.format(bitcoin_data.target_to_average_attempts(share.header['bits'].target))))
261 request.write('<p>Nonce: %i</p>' % (share.header['nonce'],))
262 if share.other_txs is not None:
263 tx_count = len(share.other_txs)
264 elif len(share.merkle_branch) == 0:
267 tx_count = 'between %i and %i' % (2**len(share.merkle_branch)//2+1, 2**len(share.merkle_branch))
268 request.write('<p>Transactions: %s</p>' % (tx_count,))
269 coinbase = share.share_data['coinbase'].ljust(2, '\x00')
270 request.write('<p>Coinbase: %s %s</p>' % (cgi.escape(repr(coinbase)), coinbase.encode('hex')))
271 request.write('<p>Generation value: %.8f %s</p>' % (share.share_data['subsidy']*1e-8, net.PARENT.SYMBOL))
272 #request.write('<p>Generation txn: %32x</p>' % (share.gentx_hash,))
275 class Explorer(resource.Resource):
276 def render_GET(self, request):
277 if not request.path.endswith('/'):
278 request.redirect(request.path + '/')
280 request.setHeader('Content-Type', 'text/html')
281 request.write('<h1>P2Pool share explorer</h1>')
283 request.write('<h2>Verified heads</h2>')
284 request.write('<ul>')
285 for h in tracker.verified.heads:
286 request.write('<li><a href="%x">%s%s</a></li>' % (h, p2pool_data.format_hash(h), ' BEST' if h == current_work.value['best_share_hash'] else ''))
287 request.write('</ul>')
289 request.write('<h2>Verified tails</h2>')
290 request.write('<ul>')
291 for tail in tracker.verified.tails:
292 for h in tracker.reverse_shares.get(tail, set()):
293 request.write('<li><a href="%x">%s%s</a></li>' % (h, p2pool_data.format_hash(h), ' BEST' if h == current_work.value['best_share_hash'] else ''))
294 request.write('</ul>')
296 request.write('<h2>Heads</h2>')
297 request.write('<ul>')
298 for h in tracker.heads:
299 request.write('<li><a href="%x">%s%s</a></li>' % (h, p2pool_data.format_hash(h), ' BEST' if h == current_work.value['best_share_hash'] else ''))
300 request.write('</ul>')
302 request.write('<h2>Tails</h2>')
303 request.write('<ul>')
304 for tail in tracker.tails:
305 for h in tracker.reverse_shares.get(tail, set()):
306 request.write('<li><a href="%x">%s%s</a></li>' % (h, p2pool_data.format_hash(h), ' BEST' if h == current_work.value['best_share_hash'] else ''))
307 request.write('</ul>')
310 def getChild(self, child, request):
313 return ShareExplorer(int(child, 16))
314 new_root.putChild('explorer', Explorer())
316 grapher = graphs.Grapher(os.path.join(datadir_path, 'rrd'))
317 web_root.putChild('graphs', grapher.get_resource())
319 if tracker.get_height(current_work.value['best_share_hash']) < 720:
321 nonstalerate = p2pool_data.get_pool_attempts_per_second(tracker, current_work.value['best_share_hash'], 720)
322 poolrate = nonstalerate / (1 - p2pool_data.get_average_stale_prop(tracker, current_work.value['best_share_hash'], 720))
323 grapher.add_poolrate_point(poolrate, poolrate - nonstalerate)
324 task.LoopingCall(add_point).start(100)
325 @pseudoshare_received.watch
326 def _(work, dead, user):
327 reactor.callLater(1, grapher.add_localrate_point, work, dead)
329 reactor.callLater(1, grapher.add_localminer_point, user, work, dead)