fixed address link going to block explorer page
[p2pool.git] / p2pool / web.py
index c44a803..78f60d5 100644 (file)
@@ -43,7 +43,7 @@ def _atomic_write(filename, data):
         os.remove(filename)
         os.rename(filename + '.new', filename)
 
-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, share_received):
+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, pseudoshare_received, share_received):
     start_time = time.time()
     
     web_root = resource.Resource()
@@ -53,8 +53,8 @@ def get_web_root(tracker, current_work, current_work2, get_current_txouts, datad
         weights, total_weight, donation_weight = tracker.get_cumulative_weights(current_work.value['best_share_hash'], min(height, 720), 65535*2**256)
         res = {}
         for script in sorted(weights, key=lambda s: weights[s]):
-            res[bitcoin_data.script2_to_human(script, net.PARENT)] = weights[script]/total_weight
-        return json.dumps(res)
+            res[bitcoin_data.script2_to_address(script, net.PARENT)] = weights[script]/total_weight
+        return res
     
     def get_current_scaled_txouts(scale, trunc=0):
         txouts = get_current_txouts()
@@ -88,6 +88,16 @@ def get_web_root(tracker, current_work, current_work2, get_current_txouts, datad
             if bitcoin_data.script2_to_address(script, net.PARENT) is not None
         )
     
+    def get_local_rates():
+        miner_hash_rates = {}
+        miner_dead_hash_rates = {}
+        datums, dt = local_rate_monitor.get_datums_in_last()
+        for datum in datums:
+            miner_hash_rates[datum['user']] = miner_hash_rates.get(datum['user'], 0) + datum['work']/dt
+            if datum['dead']:
+                miner_dead_hash_rates[datum['user']] = miner_dead_hash_rates.get(datum['user'], 0) + datum['work']/dt
+        return miner_hash_rates, miner_dead_hash_rates
+    
     def get_global_stats():
         # averaged over last hour
         lookbehind = 3600//net.SHARE_PERIOD
@@ -100,6 +110,7 @@ def get_web_root(tracker, current_work, current_work2, get_current_txouts, datad
             pool_nonstale_hash_rate=nonstale_hash_rate,
             pool_hash_rate=nonstale_hash_rate/(1 - stale_prop),
             pool_stale_prop=stale_prop,
+            min_difficulty=bitcoin_data.target_to_difficulty(tracker.shares[current_work.value['best_share_hash']].max_target),
         )
     
     def get_local_stats():
@@ -124,14 +135,7 @@ def get_web_root(tracker, current_work, current_work2, get_current_txouts, datad
             tracker.shares[tracker.get_nth_parent_hash(current_work.value['best_share_hash'], lookbehind - 1)].timestamp)
         share_att_s = my_work / actual_time
         
-        miner_hash_rates = {}
-        miner_dead_hash_rates = {}
-        datums, dt = local_rate_monitor.get_datums_in_last()
-        for datum in datums:
-            miner_hash_rates[datum['user']] = miner_hash_rates.get(datum['user'], 0) + datum['work']/dt
-            if datum['dead']:
-                miner_dead_hash_rates[datum['user']] = miner_dead_hash_rates.get(datum['user'], 0) + datum['work']/dt
-        
+        miner_hash_rates, miner_dead_hash_rates = get_local_rates()
         (stale_orphan_shares, stale_doa_shares), shares, _ = get_stale_counts()
         
         return dict(
@@ -156,6 +160,19 @@ def get_web_root(tracker, current_work, current_work2, get_current_txouts, datad
             miner_hash_rates=miner_hash_rates,
             miner_dead_hash_rates=miner_dead_hash_rates,
             efficiency_if_miner_perfect=(1 - stale_orphan_shares/shares)/(1 - global_stale_prop) if shares else None, # ignores dead shares because those are miner's fault and indicated by pseudoshare rejection
+            efficiency=(1 - (stale_orphan_shares+stale_doa_shares)/shares)/(1 - global_stale_prop) if shares else None,
+            peers=dict(
+                incoming=sum(1 for peer in p2p_node.peers.itervalues() if peer.incoming),
+                outgoing=sum(1 for peer in p2p_node.peers.itervalues() if not peer.incoming),
+            ),
+            shares=dict(
+                total=shares,
+                orphan=stale_orphan_shares,
+                dead=stale_doa_shares,
+            ),
+            uptime=time.time() - start_time,
+            block_value=current_work2.value['subsidy']*1e-8,
+            warnings=p2pool_data.get_warnings(tracker, current_work, net),
         )
     
     class WebInterface(resource.Resource):
@@ -180,10 +197,10 @@ def get_web_root(tracker, current_work, current_work2, get_current_txouts, datad
     web_root.putChild('patron_sendmany', WebInterface(get_patron_sendmany, 'text/plain'))
     web_root.putChild('global_stats', WebInterface(get_global_stats))
     web_root.putChild('local_stats', WebInterface(get_local_stats))
-    web_root.putChild('peer_addresses', WebInterface(lambda: ' '.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()), 'text/plain'))
-    web_root.putChild('peer_versions', WebInterface(lambda: ''.join('%s:%i ' % peer.addr + peer.other_sub_version + '\n' for peer in p2p_node.peers.itervalues()), 'text/plain'))
+    web_root.putChild('peer_addresses', WebInterface(lambda: ['%s:%i' % (peer.transport.getPeer().host, peer.transport.getPeer().port) for peer in p2p_node.peers.itervalues()]))
+    web_root.putChild('peer_versions', WebInterface(lambda: dict(('%s:%i' % peer.addr, peer.other_sub_version) for peer in p2p_node.peers.itervalues())))
     web_root.putChild('payout_addr', WebInterface(lambda: bitcoin_data.pubkey_hash_to_address(my_pubkey_hash, net.PARENT)))
-    web_root.putChild('recent_blocks', WebInterface(lambda: recent_blocks))
+    web_root.putChild('recent_blocks', WebInterface(lambda: [dict(ts=s.timestamp, hash='%064x' % s.header_hash) for s in tracker.get_chain(current_work.value['best_share_hash'], 24*60*60//net.SHARE_PERIOD) if s.pow_hash <= s.header['bits'].target]))
     web_root.putChild('uptime', WebInterface(lambda: time.time() - start_time))
     
     new_root = resource.Resource()
@@ -206,14 +223,7 @@ def get_web_root(tracker, current_work, current_work2, get_current_txouts, datad
         
         global_stale_prop = p2pool_data.get_average_stale_prop(tracker, current_work.value['best_share_hash'], lookbehind)
         (stale_orphan_shares, stale_doa_shares), shares, _ = get_stale_counts()
-        
-        miner_hash_rates = {}
-        miner_dead_hash_rates = {}
-        datums, dt = local_rate_monitor.get_datums_in_last()
-        for datum in datums:
-            miner_hash_rates[datum['user']] = miner_hash_rates.get(datum['user'], 0) + datum['work']/dt
-            if datum['dead']:
-                miner_dead_hash_rates[datum['user']] = miner_dead_hash_rates.get(datum['user'], 0) + datum['work']/dt
+        miner_hash_rates, miner_dead_hash_rates = get_local_rates()
         
         stat_log.append(dict(
             time=time.time(),
@@ -285,6 +295,11 @@ def get_web_root(tracker, current_work, current_work2, get_current_txouts, datad
     new_root.putChild('tails', WebInterface(lambda: ['%064x' % x for t in tracker.tails for x in tracker.reverse_shares.get(t, set())]))
     new_root.putChild('verified_tails', WebInterface(lambda: ['%064x' % x for t in tracker.verified.tails for x in tracker.verified.reverse_shares.get(t, set())]))
     new_root.putChild('best_share_hash', WebInterface(lambda: '%064x' % current_work.value['best_share_hash']))
+    new_root.putChild('currency_info', WebInterface(lambda: dict(
+        symbol=net.PARENT.SYMBOL,
+        block_explorer_url_prefix=net.PARENT.BLOCK_EXPLORER_URL_PREFIX,
+        address_explorer_url_prefix=net.PARENT.ADDRESS_EXPLORER_URL_PREFIX,
+    )))
     
     class Explorer(resource.Resource):
         def render_GET(self, request):
@@ -308,28 +323,24 @@ def get_web_root(tracker, current_work, current_work2, get_current_txouts, datad
         'last_month': graph.DataViewDescription(300, 60*60*24*30),
         'last_year': graph.DataViewDescription(300, 60*60*24*365.25),
     }
-    def combine_and_keep_largest(*dicts):
-        res = {}
-        for d in dicts:
-            for k, v in d.iteritems():
-                res[k] = res.get(k, 0) + v
-        return dict((k, v) for k, v in sorted(res.iteritems(), key=lambda (k, v): v)[-30:] if v)
     hd = graph.HistoryDatabase.from_obj({
-        'local_hash_rate': graph.DataStreamDescription(False, dataview_descriptions),
-        'local_dead_hash_rate': graph.DataStreamDescription(False, dataview_descriptions),
-        'local_share_hash_rate': graph.DataStreamDescription(False, dataview_descriptions),
-        'local_dead_share_hash_rate': graph.DataStreamDescription(False, dataview_descriptions),
-        'pool_rate': graph.DataStreamDescription(True, dataview_descriptions),
-        'pool_stale_rate': graph.DataStreamDescription(True, dataview_descriptions),
-        'current_payout': graph.DataStreamDescription(True, dataview_descriptions),
-        'incoming_peers': graph.DataStreamDescription(True, dataview_descriptions),
-        'outgoing_peers': graph.DataStreamDescription(True, dataview_descriptions),
-        'miner_hash_rates': graph.DataStreamDescription(False, dataview_descriptions, {}, combine_and_keep_largest, math.mult_dict),
-        'miner_dead_hash_rates': graph.DataStreamDescription(False, dataview_descriptions, {}, combine_and_keep_largest, math.mult_dict),
+        'local_hash_rate': graph.DataStreamDescription(dataview_descriptions, is_gauge=False),
+        'local_dead_hash_rate': graph.DataStreamDescription(dataview_descriptions, is_gauge=False),
+        'local_share_hash_rate': graph.DataStreamDescription(dataview_descriptions, is_gauge=False),
+        'local_dead_share_hash_rate': graph.DataStreamDescription(dataview_descriptions, is_gauge=False),
+        'pool_rate': graph.DataStreamDescription(dataview_descriptions),
+        'pool_stale_rate': graph.DataStreamDescription(dataview_descriptions),
+        'current_payout': graph.DataStreamDescription(dataview_descriptions),
+        'current_payouts': graph.DataStreamDescription(dataview_descriptions, multivalues=True),
+        'incoming_peers': graph.DataStreamDescription(dataview_descriptions),
+        'outgoing_peers': graph.DataStreamDescription(dataview_descriptions),
+        'miner_hash_rates': graph.DataStreamDescription(dataview_descriptions, is_gauge=False, multivalues=True),
+        'miner_dead_hash_rates': graph.DataStreamDescription(dataview_descriptions, is_gauge=False, multivalues=True),
+        'desired_versions': graph.DataStreamDescription(dataview_descriptions, multivalues=True),
     }, hd_obj)
     task.LoopingCall(lambda: _atomic_write(hd_path, json.dumps(hd.to_obj()))).start(100)
     @pseudoshare_received.watch
-    def _(work, dead, user, had_vip_pass):
+    def _(work, dead, user):
         t = time.time()
         hd.datastreams['local_hash_rate'].add_datum(t, work)
         if dead:
@@ -352,9 +363,17 @@ def get_web_root(tracker, current_work, current_work2, get_current_txouts, datad
         t = time.time()
         hd.datastreams['pool_rate'].add_datum(t, poolrate)
         hd.datastreams['pool_stale_rate'].add_datum(t, poolrate - nonstalerate)
-        hd.datastreams['current_payout'].add_datum(t, get_current_txouts().get(bitcoin_data.pubkey_hash_to_script2(my_pubkey_hash), 0)*1e-8)
+        current_txouts = get_current_txouts()
+        hd.datastreams['current_payout'].add_datum(t, current_txouts.get(bitcoin_data.pubkey_hash_to_script2(my_pubkey_hash), 0)*1e-8)
+        miner_hash_rates, miner_dead_hash_rates = get_local_rates()
+        current_txouts_by_address = dict((bitcoin_data.script2_to_address(script, net.PARENT), amount) for script, amount in current_txouts.iteritems())
+        hd.datastreams['current_payouts'].add_datum(t, dict((user, current_txouts_by_address[user]*1e-8) for user in miner_hash_rates if user in current_txouts_by_address))
         hd.datastreams['incoming_peers'].add_datum(t, sum(1 for peer in p2p_node.peers.itervalues() if peer.incoming))
         hd.datastreams['outgoing_peers'].add_datum(t, sum(1 for peer in p2p_node.peers.itervalues() if not peer.incoming))
+        
+        vs = p2pool_data.get_desired_version_counts(tracker, current_work.value['best_share_hash'], 720)
+        vs_total = sum(vs.itervalues())
+        hd.datastreams['desired_versions'].add_datum(t, dict((str(k), v/vs_total) for k, v in vs.iteritems()))
     task.LoopingCall(add_point).start(5)
     new_root.putChild('graph_data', WebInterface(lambda source, view: hd.datastreams[source].dataviews[view].get_data(time.time())))