separated and cleaned up new block handing and bitcoind polling
authorForrest Voight <forrest@forre.st>
Sat, 24 Dec 2011 07:35:59 +0000 (02:35 -0500)
committerForrest Voight <forrest@forre.st>
Sat, 24 Dec 2011 08:23:43 +0000 (03:23 -0500)
p2pool/data.py
p2pool/main.py

index 570c290..da48acd 100644 (file)
@@ -259,7 +259,7 @@ class OkayTracker(forest.Tracker):
         
         self.get_cumulative_weights = skiplists.WeightsSkipList(self)
     
-    def attempt_verify(self, share, now):
+    def attempt_verify(self, share):
         if share.hash in self.verified.shares:
             return True
         height, last = self.get_height_and_last(share.hash)
@@ -274,7 +274,7 @@ class OkayTracker(forest.Tracker):
             self.verified.add(share)
             return True
     
-    def think(self, ht, previous_block, now):
+    def think(self, ht, previous_block):
         desired = set()
         
         # O(len(self.heads))
@@ -287,7 +287,7 @@ class OkayTracker(forest.Tracker):
             head_height, last = self.get_height_and_last(head)
             
             for share in self.get_chain(head, head_height if last is None else min(5, max(0, head_height - self.net.CHAIN_LENGTH))):
-                if self.attempt_verify(share, now):
+                if self.attempt_verify(share):
                     break
                 if share.hash in self.heads:
                     bads.add(share.hash)
@@ -311,7 +311,7 @@ class OkayTracker(forest.Tracker):
             get = min(want, can)
             #print 'Z', head_height, last_hash is None, last_height, last_last_hash is None, want, can, get
             for share in self.get_chain(last_hash, get):
-                if not self.attempt_verify(share, now):
+                if not self.attempt_verify(share):
                     break
             if head_height < self.net.CHAIN_LENGTH and last_last_hash is not None:
                 desired.add((self.verified.shares[random.choice(list(self.verified.reverse_shares[last_hash]))].peer, last_last_hash))
index 74a7c96..299c6a5 100644 (file)
@@ -154,9 +154,10 @@ def main(args, net, datadir_path):
                 previous_block=work['previous_block_hash'],
                 bits=work['bits'],
             ))
+        factory.new_block.watch(lambda block_hash: set_real_work1())
         
         def set_real_work2():
-            best, desired = tracker.think(ht, pre_current_work.value['previous_block'], time.time() - pre_current_work2.value['clock_offset'])
+            best, desired = tracker.think(ht, pre_current_work.value['previous_block'])
             
             current_work2.set(pre_current_work2.value)
             t = dict(pre_current_work.value)
@@ -582,35 +583,15 @@ def main(args, net, datadir_path):
         print '    ...success!'
         print
         
-        # done!
         
-        # do new getwork when a block is heard on the p2p interface
+        # call getmemorypool every 15 seconds to check that bitcoind is alive
+        task.LoopingCall(set_real_work1).start(15)
+        
         
+        # done!
         print 'Started successfully!'
         print
         
-        @defer.inlineCallbacks
-        def work1_thread():
-            while True:
-                flag = factory.new_block.get_deferred()
-                try:
-                    yield set_real_work1()
-                except:
-                    log.err()
-                yield defer.DeferredList([flag, deferral.sleep(random.uniform(1, 10))], fireOnOneCallback=True)
-        
-        @defer.inlineCallbacks
-        def work2_thread():
-            while True:
-                try:
-                    set_real_work2()
-                except:
-                    log.err()
-                yield deferral.sleep(random.expovariate(1/20))
-        
-        work1_thread()
-        work2_thread()
-        
         
         if hasattr(signal, 'SIGALRM'):
             def watchdog_handler(signum, frame):