indentation and imports cleaned up
[p2pool.git] / p2pool / data.py
index 1740c04..a463665 100644 (file)
@@ -4,13 +4,12 @@ import itertools
 import random
 import time
 
-from twisted.internet import defer
 from twisted.python import log
 
 import p2pool
 from p2pool import skiplists
 from p2pool.bitcoin import data as bitcoin_data, script
-from p2pool.util import memoize, expiring_dict, math, deferral
+from p2pool.util import memoize, expiring_dict, math
 
 
 merkle_branch_type = bitcoin_data.ListType(bitcoin_data.ComposedType([
@@ -209,10 +208,10 @@ class Share(object):
 def get_pool_attempts_per_second(tracker, previous_share_hash, net, dist=None):
     if dist is None:
         dist = net.TARGET_LOOKBEHIND
-    # XXX could be optimized to use nth_parent and cumulative_weights
-    chain = list(itertools.islice(tracker.get_chain_to_root(previous_share_hash), dist))
-    attempts = sum(bitcoin_data.target_to_average_attempts(share.target) for share in chain[:-1])
-    time = chain[0].timestamp - chain[-1].timestamp
+    near = tracker.shares[previous_share_hash]
+    far = tracker.shares[tracker.get_nth_parent_hash(previous_share_hash, dist - 1)]
+    attempts = tracker.get_work(near.hash) - tracker.get_work(far.hash)
+    time = near.timestamp - far.timestamp
     if time == 0:
         time = 1
     return attempts//time
@@ -232,30 +231,10 @@ def generate_transaction(tracker, previous_share_hash, new_script, subsidy, nonc
     attempts_to_block = bitcoin_data.target_to_average_attempts(block_target)
     max_weight = net.SPREAD * attempts_to_block
     
-    '''
-    class fake_share(object):
-        pass
-    fake_share.new_script = new_script
-    fake_share.target = target
-    
-    dest_weights = {}
-    total_weight = 0
-    for share in itertools.chain([fake_share], itertools.islice(tracker.get_chain_to_root(previous_share_hash), net.CHAIN_LENGTH)):
-        weight = bitcoin_data.target_to_average_attempts(share.target)
-        if weight > max_weight - total_weight:
-            weight = max_weight - total_weight
-        
-        dest_weights[share.new_script] = dest_weights.get(share.new_script, 0) + weight
-        total_weight += weight
-        
-        if total_weight == max_weight:
-            break
-    '''
-    
     this_weight = min(bitcoin_data.target_to_average_attempts(target), max_weight)
     other_weights, other_weights_total = tracker.get_cumulative_weights(previous_share_hash, min(height, net.CHAIN_LENGTH), max(0, max_weight - this_weight))
     dest_weights, total_weight = math.add_dicts([{new_script: this_weight}, other_weights]), this_weight + other_weights_total
-    total_weight = sum(dest_weights.itervalues())
+    assert total_weight == sum(dest_weights.itervalues())
     
     amounts = dict((script, subsidy*(396*weight)//(400*total_weight)) for (script, weight) in dest_weights.iteritems())
     amounts[new_script] = amounts.get(new_script, 0) + subsidy*2//400
@@ -300,6 +279,11 @@ class OkayTracker(bitcoin_data.Tracker):
         
         self.get_cumulative_weights = skiplists.WeightsSkipList(self)
     
+    def add(self, share, known_verified=False):
+        bitcoin_data.Tracker.add(self, share)
+        if known_verified:
+            self.verified.add(share)
+    
     def attempt_verify(self, share, now):
         if share.hash in self.verified.shares:
             return True
@@ -376,16 +360,42 @@ class OkayTracker(bitcoin_data.Tracker):
                     -self.verified.shares[h].time_seen
                 )
         
-        for share_hash in scores[:-5]:
-            if self.shares[share_hash].time_seen > time.time() - 30:
-                continue
-            self.remove(share_hash)
-            self.verified.remove(share_hash)
+        # eat away at heads
+        if scores:
+            while True:
+                to_remove = set()
+                for share_hash, tail in self.heads.iteritems():
+                    if share_hash in scores[-5:]:
+                        continue
+                    if self.shares[share_hash].time_seen > time.time() - 30:
+                        continue
+                    if max(self.shares[before_tail_hash].time_seen for before_tail_hash in self.reverse_shares.get(tail)) > time.time() - 120:
+                        continue
+                    to_remove.add(share_hash)
+                for share_hash in to_remove:
+                    self.remove(share_hash)
+                    if share_hash in self.verified.shares:
+                        self.verified.remove(share_hash)
+                if not to_remove:
+                    break
         
-        #for tail, heads in list(self.tails.iteritems()):
-        #    if min(self.get_height(head) for head in heads) > 2*net.CHAIN_LENGTH + 10:
-        #        self.remove(tail)
-        #        self.verified.remove(tail)
+        # drop tails
+        while True:
+            removed = False
+            # if removed from this, it must be removed from verified
+            for tail, heads in list(self.tails.iteritems()):
+                if min(self.get_height(head) for head in heads) < 2*self.net.CHAIN_LENGTH + 10:
+                    continue
+                start = time.time()
+                for aftertail in list(self.reverse_shares.get(tail, set())):
+                    self.remove(aftertail)
+                    if aftertail in self.verified.shares:
+                        self.verified.remove(aftertail)
+                    removed = True
+                end = time.time()
+                print "removed! %x %f" % (tail, end - start)
+            if not removed:
+                break
         
         best = scores[-1] if scores else None
         
@@ -404,9 +414,7 @@ class OkayTracker(bitcoin_data.Tracker):
         score2 = 0
         attempts = 0
         max_height = 0
-        # XXX should only look past a certain share, not at recent ones
         share2_hash = self.verified.get_nth_parent_hash(share_hash, min(self.net.CHAIN_LENGTH//2, head_height//2)) if last is not None else share_hash
-        # XXX this must go in the opposite direction for max_height to make sense
         for share in reversed(list(itertools.islice(self.verified.get_chain_known(share2_hash), self.net.CHAIN_LENGTH))):
             max_height = max(max_height, ht.get_min_height(share.header['previous_block']))
             attempts += bitcoin_data.target_to_average_attempts(share.target)
@@ -432,6 +440,7 @@ class Mainnet(bitcoin_data.Mainnet):
     P2P_PORT = 9333
     MAX_TARGET = 2**256//2**32 - 1
     PERSIST = True
+    HEADERSTORE_FILENAME = 'headers.dat'
 
 class Testnet(bitcoin_data.Testnet):
     SHARE_PERIOD = 1 # seconds
@@ -445,3 +454,4 @@ class Testnet(bitcoin_data.Testnet):
     P2P_PORT = 19333
     MAX_TARGET = 2**256//2**20 - 1
     PERSIST = False
+    HEADERSTORE_FILENAME = 'testnet_headers.dat'