moved read_stale_frac to Share classes
authorForrest Voight <forrest.voight@gmail.com>
Thu, 1 Dec 2011 01:45:10 +0000 (20:45 -0500)
committerForrest Voight <forrest.voight@gmail.com>
Thu, 1 Dec 2011 01:45:10 +0000 (20:45 -0500)
p2pool/data.py
p2pool/main.py

index f5135cb..9d5882c 100644 (file)
@@ -6,6 +6,7 @@ import itertools
 import random
 import time
 import os
+import struct
 
 from twisted.python import log
 
@@ -139,7 +140,7 @@ class Share(object):
     def from_share1b(cls, share1b, net):
         return cls(net, **share1b)
     
-    __slots__ = 'header previous_block share_info merkle_branch other_txs timestamp share_data new_script subsidy previous_hash previous_share_hash target nonce pow_hash header_hash hash time_seen peer'.split(' ')
+    __slots__ = 'header previous_block share_info merkle_branch other_txs timestamp share_data new_script subsidy previous_hash previous_share_hash target nonce pow_hash header_hash hash time_seen peer stale_frac'.split(' ')
     
     def __init__(self, net, header, share_info, merkle_branch=None, other_txs=None):
         if merkle_branch is None and other_txs is None:
@@ -193,6 +194,12 @@ class Share(object):
         if script.get_sigop_count(self.new_script) > 1:
             raise ValueError('too many sigops!')
         
+        self.stale_frac = None
+        if len(self.nonce) >= 4:
+            a, b = struct.unpack("<HH", self.nonce[-4:])
+            if a != 0 or a == b:
+                self.stale_frac = a/65535
+        
         # XXX eww
         self.time_seen = time.time()
         self.peer = None
@@ -244,7 +251,7 @@ class Share(object):
         return '<Share %s>' % (' '.join('%s=%r' % (k, getattr(self, k)) for k in self.__slots__),)
 
 class NewShare(Share):
-    __slots__ = 'header previous_block share_info merkle_branch other_txs timestamp share_data new_script subsidy previous_hash previous_share_hash target nonce pow_hash header_hash hash time_seen peer donation'.split(' ')
+    __slots__ = 'header previous_block share_info merkle_branch other_txs timestamp share_data new_script subsidy previous_hash previous_share_hash target nonce pow_hash header_hash hash time_seen peer donation stale_frac'.split(' ')
     
     @classmethod
     def from_share(cls, share, net):
@@ -321,6 +328,8 @@ class NewShare(Share):
         if script.get_sigop_count(self.new_script) > 1:
             raise ValueError('too many sigops!')
         
+        self.stale_frac = self.share_data['stale_frac']/254 if self.share_data['stale_frac'] != 255 else None
+        
         # XXX eww
         self.time_seen = time.time()
         self.peer = None
index af285de..4030d1c 100644 (file)
@@ -554,7 +554,7 @@ def main(args):
             if current_work.value['best_share_hash'] is not None:
                 height, last = tracker.get_height_and_last(current_work.value['best_share_hash'])
                 att_s = p2pool.get_pool_attempts_per_second(tracker, current_work.value['best_share_hash'], args.net, min(height - 1, 720))
-                fracs = [read_stale_frac(share) for share in itertools.islice(tracker.get_chain_known(current_work.value['best_share_hash']), 120) if read_stale_frac(share) is not None]
+                fracs = [share.stale_frac for share in itertools.islice(tracker.get_chain_known(current_work.value['best_share_hash']), 120) if share.stale_frac is not None]
                 return json.dumps(int(att_s / (1. - (math.median(fracs) if fracs else 0))))
             return json.dumps(None)
         
@@ -630,16 +630,6 @@ def main(args):
             task.LoopingCall(signal.alarm, 30).start(1)
         
         
-        def read_stale_frac(share):
-            if isinstance(share, p2pool.NewShare):
-                return share.share_data['stale_frac']/254 if share.share_data['stale_frac'] != 255 else None
-            if len(share.nonce) < 4:
-                return None
-            a, b = struct.unpack("<HH", share.nonce[-4:])
-            if a == 0 or a != b:
-                return None
-            return a/65535
-
         pool_str = None;
         while True:
             yield deferral.sleep(3)
@@ -653,7 +643,7 @@ def main(args):
                         weights, total_weight, donation_weight = tracker.get_cumulative_weights(current_work.value['best_share_hash'], min(height, 720), 65535*2**256)
                         shares, stale_doa_shares, stale_not_doa_shares = get_share_counts(True)
                         stale_shares = stale_doa_shares + stale_not_doa_shares
-                        fracs = [read_stale_frac(share) for share in itertools.islice(tracker.get_chain_known(current_work.value['best_share_hash']), 120) if read_stale_frac(share) is not None]
+                        fracs = [share.stale_frac for share in itertools.islice(tracker.get_chain_known(current_work.value['best_share_hash']), 120) if share.stale_frac is not None]
                         str = 'Pool: %sH/s in %i shares (%i/%i verified) Recent: %.02f%% >%sH/s Shares: %i (%i orphan, %i dead) Peers: %i' % (
                             math.format(int(att_s / (1. - (math.median(fracs) if fracs else 0)))),
                             height,