indentation and imports cleaned up
[p2pool.git] / p2pool / bitcoin / data.py
index ca4fb73..dc8b8d6 100644 (file)
@@ -1,11 +1,10 @@
 from __future__ import division
 
 import hashlib
-import itertools
 import struct
 
 from . import base58, skiplists
-from p2pool.util import bases, math, skiplist
+from p2pool.util import bases, math, variable
 import p2pool
 
 class EarlyEnd(Exception):
@@ -445,7 +444,7 @@ def script2_to_human(script2, net):
     try:
         pubkey = script2[1:-1]
         script2_test = pubkey_to_script2(pubkey)
-    except: # XXX
+    except:
         pass
     else:
         if script2_test == script2:
@@ -454,7 +453,7 @@ def script2_to_human(script2, net):
     try:
         pubkey_hash = ShortHashType().unpack(script2[3:-2])
         script2_test2 = pubkey_hash_to_script2(pubkey_hash)
-    except: # XXX
+    except:
         pass
     else:
         if script2_test2 == script2:
@@ -481,11 +480,14 @@ class Tracker(object):
         '''
         
         self.get_nth_parent_hash = skiplists.DistanceSkipList(self)
+        
+        self.added = variable.Event()
+        self.removed = variable.Event()
     
     def add(self, share):
         assert not isinstance(share, (int, long, type(None)))
         if share.hash in self.shares:
-            return # XXX raise exception?
+            raise ValueError('share already present')
         
         '''
         parent_id = self.ids.get(share.previous_hash, None)
@@ -523,6 +525,8 @@ class Tracker(object):
         
         for head in heads:
             self.heads[head] = tail
+        
+        self.added.happened(share)
     
     def test(self):
         t = Tracker()
@@ -568,6 +572,7 @@ class Tracker(object):
         else:
             raise NotImplementedError()
         
+        to_remove = set()
         for share_hash2 in self.heights:
             height_to, other_share_hash, work_inc = self.heights[share_hash2]
             if other_share_hash != share.previous_hash:
@@ -579,7 +584,9 @@ class Tracker(object):
                 work_inc -= target_to_average_attempts(share.target)
                 self.heights[share_hash2] = height_to, other_share_hash, work_inc
             else:
-                del self.heights[share_hash2]
+                to_remove.add(share_hash2)
+        for share_hash2 in to_remove:
+            del self.heights[share_hash2]
         if share.hash in self.heights:
             del self.heights[share.hash]
         
@@ -625,6 +632,7 @@ class Tracker(object):
             self.reverse_shares.pop(share.previous_hash)
         
         #assert self.test() is None
+        self.removed.happened(share)
     
     def get_height(self, share_hash):
         height, work, last = self.get_height_work_and_last(share_hash)
@@ -634,6 +642,10 @@ class Tracker(object):
         height, work, last = self.get_height_work_and_last(share_hash)
         return work
     
+    def get_last(self, share_hash):
+        height, work, last = self.get_height_work_and_last(share_hash)
+        return last
+    
     def get_height_and_last(self, share_hash):
         height, work, last = self.get_height_work_and_last(share_hash)
         return height, last