defer adding of weight dicts until solution is finalized
authorForrest Voight <forrest@forre.st>
Sat, 24 Dec 2011 07:16:34 +0000 (02:16 -0500)
committerForrest Voight <forrest@forre.st>
Sat, 24 Dec 2011 08:23:43 +0000 (03:23 -0500)
p2pool/skiplists.py
p2pool/util/math.py

index 28520e3..9baf4f6 100644 (file)
@@ -18,18 +18,17 @@ class WeightsSkipList(forest.TrackerSkipList):
     
     def initial_solution(self, start, (max_shares, desired_weight)):
         assert desired_weight % 65535 == 0, divmod(desired_weight, 65535)
-        return 0, {}, 0, 0
+        return 0, None, 0, 0
     
-    def apply_delta(self, (share_count1, weights1, total_weight1, total_donation_weight1), (share_count2, weights2, total_weight2, total_donation_weight2), (max_shares, desired_weight)):
+    def apply_delta(self, (share_count1, weights_list, total_weight1, total_donation_weight1), (share_count2, weights2, total_weight2, total_donation_weight2), (max_shares, desired_weight)):
         if total_weight1 + total_weight2 > desired_weight and share_count2 == 1:
-            script, = weights2.iterkeys()
-            new_weights = dict(weights1)
             assert (desired_weight - total_weight1) % 65535 == 0
-            new_weights[script] = new_weights.get(script, 0) + (desired_weight - total_weight1)//65535*weights2[script]//(total_weight2//65535)
-            return share_count1 + share_count2, new_weights, desired_weight, total_donation_weight1 + (desired_weight - total_weight1)//65535*total_donation_weight2//(total_weight2//65535)
-        return share_count1 + share_count2, math.add_dicts(weights1, weights2), total_weight1 + total_weight2, total_donation_weight1 + total_donation_weight2
+            script, = weights2.iterkeys()
+            new_weights = dict(script=(desired_weight - total_weight1)//65535*weights2[script]//(total_weight2//65535))
+            return share_count1 + share_count2, (weights_list, new_weights), desired_weight, total_donation_weight1 + (desired_weight - total_weight1)//65535*total_donation_weight2//(total_weight2//65535)
+        return share_count1 + share_count2, (weights_list, weights2), total_weight1 + total_weight2, total_donation_weight1 + total_donation_weight2
     
-    def judge(self, (share_count, weights, total_weight, total_donation_weight), (max_shares, desired_weight)):
+    def judge(self, (share_count, weights_list, total_weight, total_donation_weight), (max_shares, desired_weight)):
         if share_count > max_shares or total_weight > desired_weight:
             return 1
         elif share_count == max_shares or total_weight == desired_weight:
@@ -37,10 +36,10 @@ class WeightsSkipList(forest.TrackerSkipList):
         else:
             return -1
     
-    def finalize(self, (share_count, weights, total_weight, total_donation_weight), (max_shares, desired_weight)):
+    def finalize(self, (share_count, weights_list, total_weight, total_donation_weight), (max_shares, desired_weight)):
         assert share_count <= max_shares and total_weight <= desired_weight
         assert share_count == max_shares or total_weight == desired_weight
-        return weights, total_weight, total_donation_weight
+        return math.add_dicts(*math.flatten_linked_list(weights_list)), total_weight, total_donation_weight
 
 class SumSkipList(forest.TrackerSkipList):
     def __init__(self, tracker, value_func, identity_value=0, add_func=operator.add):
index 66b802e..3cea2e4 100644 (file)
@@ -130,6 +130,11 @@ def add_tuples(res, *tuples):
         res = tuple(a + b for a, b in zip(res, t))
     return res
 
+def flatten_linked_list(x):
+    while x is not None:
+        x, cur = x
+        yield cur
+
 if __name__ == '__main__':
     import random
     a = 1