import os
import sys
import time
+import traceback
from twisted.internet import defer, task
from twisted.python import log
import p2pool
from bitcoin import data as bitcoin_data
from . import data as p2pool_data
-from util import deferred_resource, graph, math, pack
+from util import deferred_resource, graph, math, memory, pack, variable
def _atomic_read(filename):
try:
os.remove(filename)
os.rename(filename + '.new', filename)
-def get_web_root(wb, datadir_path, bitcoind_warning_var):
+def get_web_root(wb, datadir_path, bitcoind_warning_var, stop_event=variable.Event()):
node = wb.node
start_time = time.time()
res = yield self.func(*self.args)
defer.returnValue(json.dumps(res) if self.mime_type == 'application/json' else res)
- web_root.putChild('rate', WebInterface(lambda: p2pool_data.get_pool_attempts_per_second(node.tracker, node.best_share_var.value, 720)/(1-p2pool_data.get_average_stale_prop(node.tracker, node.best_share_var.value, 720))))
+ def decent_height():
+ return min(node.tracker.get_height(node.best_share_var.value), 720)
+ web_root.putChild('rate', WebInterface(lambda: p2pool_data.get_pool_attempts_per_second(node.tracker, node.best_share_var.value, decent_height())/(1-p2pool_data.get_average_stale_prop(node.tracker, node.best_share_var.value, decent_height()))))
web_root.putChild('difficulty', WebInterface(lambda: bitcoin_data.target_to_difficulty(node.tracker.items[node.best_share_var.value].max_target)))
web_root.putChild('users', WebInterface(get_users))
web_root.putChild('user_stales', WebInterface(lambda: dict((bitcoin_data.pubkey_hash_to_address(ph, node.net.PARENT), prop) for ph, prop in
web_root.putChild('recent_blocks', WebInterface(lambda: [dict(
ts=s.timestamp,
hash='%064x' % s.header_hash,
- number=pack.IntType(24).unpack(s.share_data['coinbase'][1:4]),
+ number=pack.IntType(24).unpack(s.share_data['coinbase'][1:4]) if len(s.share_data['coinbase']) >= 4 else None,
share='%064x' % s.hash,
) for s in node.tracker.get_chain(node.best_share_var.value, min(node.tracker.get_height(node.best_share_var.value), 24*60*60//node.net.SHARE_PERIOD)) if s.pow_hash <= s.header['bits'].target]))
web_root.putChild('uptime', WebInterface(lambda: time.time() - start_time))
- web_root.putChild('stale_rates', WebInterface(lambda: p2pool_data.get_stale_counts(node.tracker, node.best_share_var.value, 720, rates=True)))
+ web_root.putChild('stale_rates', WebInterface(lambda: p2pool_data.get_stale_counts(node.tracker, node.best_share_var.value, decent_height(), rates=True)))
new_root = resource.Resource()
web_root.putChild('web', new_root)
with open(os.path.join(datadir_path, 'stats'), 'wb') as f:
f.write(json.dumps(stat_log))
- task.LoopingCall(update_stat_log).start(5*60)
+ x = task.LoopingCall(update_stat_log)
+ x.start(5*60)
+ stop_event.watch(x.stop)
new_root.putChild('log', WebInterface(lambda: stat_log))
def get_share(share_hash_str):
return dict(
parent='%064x' % share.previous_hash,
children=['%064x' % x for x in sorted(node.tracker.reverse.get(share.hash, set()), key=lambda sh: -len(node.tracker.reverse.get(sh, set())))], # sorted from most children to least children
+ type_name=type(share).__name__,
local=dict(
verified=share.hash in node.tracker.verified.items,
time_first_seen=start_time if share.time_seen == 0 else share.time_seen,
- peer_first_received_from=share.peer.addr if share.peer is not None else None,
+ peer_first_received_from=share.peer_addr,
),
share_data=dict(
timestamp=share.timestamp,
'last_month': graph.DataViewDescription(300, 60*60*24*30),
'last_year': graph.DataViewDescription(300, 60*60*24*365.25),
}
- def build_pool_rates(ds_name, ds_desc, dv_name, dv_desc, obj):
+ def build_desired_rates(ds_name, ds_desc, dv_name, dv_desc, obj):
if not obj:
last_bin_end = 0
bins = dv_desc.bin_count*[{}]
else:
- pool_rate = obj['pool_rate'][dv_name]
- pool_stale_rate = obj['pool_stale_rate'][dv_name]
- last_bin_end = max(pool_rate['last_bin_end'], pool_stale_rate['last_bin_end'])
- bins = dv_desc.bin_count*[{}]
- def get_value(obj, t):
- n = int((obj['last_bin_end'] - t)/dv_desc.bin_width)
+ pool_rates = obj['pool_rates'][dv_name]
+ desired_versions = obj['desired_versions'][dv_name]
+ def get_total_pool_rate(t):
+ n = int((pool_rates['last_bin_end'] - t)/dv_desc.bin_width)
if n < 0 or n >= dv_desc.bin_count:
- return None, 0
- total, count = obj['bins'][n].get('null', [0, 0])
+ return None
+ total = sum(x[0] for x in pool_rates['bins'][n].values())
+ count = math.mean(x[1] for x in pool_rates['bins'][n].values())
if count == 0:
- return None, 0
- return total/count, count
- def get_bin(t):
- total, total_count = get_value(pool_rate, t)
- bad, bad_count = get_value(pool_stale_rate, t)
- if total is None or bad is None:
- return {}
- count = int((total_count+bad_count)/2+1/2)
- return dict(good=[(total-bad)*count, count], bad=[bad*count, count], null=[0, count])
- bins = [get_bin(last_bin_end - (i+1/2)*dv_desc.bin_width) for i in xrange(dv_desc.bin_count)]
+ return None
+ return total/count
+ last_bin_end = desired_versions['last_bin_end']
+ bins = [dict((name, (total*get_total_pool_rate(last_bin_end - (i+1/2)*dv_desc.bin_width), count)) for name, (total, count) in desired_versions['bins'][i].iteritems()) for i in xrange(dv_desc.bin_count)]
return graph.DataView(dv_desc, ds_desc, last_bin_end, bins)
hd = graph.HistoryDatabase.from_obj({
'local_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_rates': graph.DataStreamDescription(dataview_descriptions, multivalues=True,
- multivalue_undefined_means_0=True, default_func=build_pool_rates),
+ multivalue_undefined_means_0=True),
'current_payout': graph.DataStreamDescription(dataview_descriptions),
'current_payouts': graph.DataStreamDescription(dataview_descriptions, multivalues=True),
'incoming_peers': graph.DataStreamDescription(dataview_descriptions),
'miner_dead_hash_rates': graph.DataStreamDescription(dataview_descriptions, is_gauge=False, multivalues=True),
'desired_versions': graph.DataStreamDescription(dataview_descriptions, multivalues=True,
multivalue_undefined_means_0=True),
+ 'desired_version_rates': graph.DataStreamDescription(dataview_descriptions, multivalues=True,
+ multivalue_undefined_means_0=True, default_func=build_desired_rates),
'traffic_rate': graph.DataStreamDescription(dataview_descriptions, is_gauge=False, multivalues=True),
+ 'getwork_latency': graph.DataStreamDescription(dataview_descriptions),
+ 'memory_usage': graph.DataStreamDescription(dataview_descriptions),
}, hd_obj)
- task.LoopingCall(lambda: _atomic_write(hd_path, json.dumps(hd.to_obj()))).start(100)
+ x = task.LoopingCall(lambda: _atomic_write(hd_path, json.dumps(hd.to_obj())))
+ x.start(100)
+ stop_event.watch(x.stop)
@wb.pseudoshare_received.watch
def _(work, dead, user):
t = time.time()
lookbehind = min(node.net.CHAIN_LENGTH, 60*60//node.net.SHARE_PERIOD, node.tracker.get_height(node.best_share_var.value))
t = time.time()
- hd.datastreams['pool_rates'].add_datum(t, p2pool_data.get_stale_counts(node.tracker, node.best_share_var.value, lookbehind, rates=True))
+ pool_rates = p2pool_data.get_stale_counts(node.tracker, node.best_share_var.value, lookbehind, rates=True)
+ pool_total = sum(pool_rates.itervalues())
+ hd.datastreams['pool_rates'].add_datum(t, pool_rates)
current_txouts = node.get_current_txouts()
hd.datastreams['current_payout'].add_datum(t, current_txouts.get(bitcoin_data.pubkey_hash_to_script2(wb.my_pubkey_hash), 0)*1e-8)
vs = p2pool_data.get_desired_version_counts(node.tracker, node.best_share_var.value, lookbehind)
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)
+ hd.datastreams['desired_version_rates'].add_datum(t, dict((str(k), v/vs_total*pool_total) for k, v in vs.iteritems()))
+ try:
+ hd.datastreams['memory_usage'].add_datum(t, memory.resident())
+ except:
+ if p2pool.DEBUG:
+ traceback.print_exc()
+ x = task.LoopingCall(add_point)
+ x.start(5)
+ stop_event.watch(x.stop)
+ @node.bitcoind_work.changed.watch
+ def _(new_work):
+ hd.datastreams['getwork_latency'].add_datum(time.time(), new_work['latency'])
new_root.putChild('graph_data', WebInterface(lambda source, view: hd.datastreams[source].dataviews[view].get_data(time.time())))
web_root.putChild('static', static.File(os.path.join(os.path.dirname(sys.argv[0]), 'web-static')))