1 from p2pool.util import math, skiplist
3 class WeightsSkipList(skiplist.SkipList):
4 # share_count, weights, total_weight
6 def __init__(self, tracker):
7 skiplist.SkipList.__init__(self)
10 def previous(self, element):
11 return self.tracker.shares[element].previous_hash
13 def get_delta(self, element):
14 from p2pool.bitcoin import data as bitcoin_data
16 return (2**256, {}, 0) # XXX
17 share = self.tracker.shares[element]
18 att = bitcoin_data.target_to_average_attempts(share.target)
19 return 1, {share.new_script: att}, att
21 def combine_deltas(self, (share_count1, weights1, total_weight1), (share_count2, weights2, total_weight2)):
22 return share_count1 + share_count2, math.add_dicts([weights1, weights2]), total_weight1 + total_weight2
24 def initial_solution(self, start, (max_shares, desired_weight)):
27 def apply_delta(self, (share_count1, weights1, total_weight1), (share_count2, weights2, total_weight2), (max_shares, desired_weight)):
28 if total_weight1 + total_weight2 > desired_weight and len(weights2) == 1:
29 script, = weights2.iterkeys()
30 new_weights = dict(weights1)
31 new_weights[script] = new_weights.get(script, 0) + desired_weight - total_weight1
32 return share_count1 + share_count2, new_weights, desired_weight
33 return share_count1 + share_count2, math.add_dicts([weights1, weights2]), total_weight1 + total_weight2
35 def judge(self, (share_count, weights, total_weight), (max_shares, desired_weight)):
36 if share_count > max_shares or total_weight > desired_weight:
38 elif share_count == max_shares or total_weight == desired_weight:
43 def finalize(self, (share_count, weights, total_weight)):
44 return weights, total_weight
46 class CountsSkipList(skiplist.SkipList):
47 # share_count, counts, total_count
49 def __init__(self, tracker, run_identifier):
50 skiplist.SkipList.__init__(self)
51 self.tracker = tracker
52 self.run_identifier = run_identifier
54 def previous(self, element):
55 return self.tracker.shares[element].previous_hash
57 def get_delta(self, element):
59 raise AssertionError()
60 share = self.tracker.shares[element]
61 return 1, set([share.hash]) if share.nonce[:8] == self.run_identifier else set()
63 def combine_deltas(self, (share_count1, share_hashes1), (share_count2, share_hashes2)):
64 if share_hashes1 & share_hashes2:
65 raise AssertionError()
66 return share_count1 + share_count2, share_hashes1 | share_hashes2
68 def initial_solution(self, start, (desired_shares,)):
71 def apply_delta(self, (share_count1, share_hashes1), (share_count2, share_hashes2), (desired_shares,)):
72 if share_hashes1 & share_hashes2:
73 raise AssertionError()
74 return share_count1 + share_count2, share_hashes1 | share_hashes2
76 def judge(self, (share_count, share_hashes), (desired_shares,)):
77 if share_count > desired_shares:
79 elif share_count == desired_shares:
84 def finalize(self, (share_count, share_hashes)):
87 if __name__ == '__main__':
89 from p2pool.bitcoin import data
91 d = WeightsSkipList(t)
92 for i in xrange(2000):
93 t.add(data.FakeShare(hash=i, previous_hash=i - 1 if i > 0 else None, new_script=i, target=random.randrange(2**249, 2**250)))
94 for i in xrange(2000):
95 #a = random.randrange(2000)
97 print d(a, a, 1000000)[1]