From: Forrest Voight Date: Fri, 16 Mar 2012 17:59:01 +0000 (-0400) Subject: moved WeightsSkipList to p2pool.data X-Git-Tag: 0.10.0~35 X-Git-Url: https://git.novaco.in/?a=commitdiff_plain;h=4f878ea92e0db9ff1f80f3d72b1816b52d7fc95f;p=p2pool.git moved WeightsSkipList to p2pool.data --- diff --git a/p2pool/data.py b/p2pool/data.py index b081742..875d631 100644 --- a/p2pool/data.py +++ b/p2pool/data.py @@ -8,7 +8,6 @@ import time from twisted.python import log import p2pool -from p2pool import skiplists from p2pool.bitcoin import data as bitcoin_data, script, sha256 from p2pool.util import math, forest, pack @@ -269,6 +268,43 @@ class Share(object): return dict(header=self.header, txs=[gentx] + self.other_txs) +class WeightsSkipList(forest.TrackerSkipList): + # share_count, weights, total_weight + + def get_delta(self, element): + from p2pool.bitcoin import data as bitcoin_data + share = self.tracker.shares[element] + att = bitcoin_data.target_to_average_attempts(share.target) + return 1, {share.new_script: att*(65535-share.share_data['donation'])}, att*65535, att*share.share_data['donation'] + + def combine_deltas(self, (share_count1, weights1, total_weight1, total_donation_weight1), (share_count2, weights2, total_weight2, total_donation_weight2)): + return share_count1 + share_count2, math.add_dicts(weights1, weights2), total_weight1 + total_weight2, total_donation_weight1 + total_donation_weight2 + + def initial_solution(self, start, (max_shares, desired_weight)): + assert desired_weight % 65535 == 0, divmod(desired_weight, 65535) + return 0, None, 0, 0 + + 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: + assert (desired_weight - total_weight1) % 65535 == 0 + 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_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: + return 0 + else: + return -1 + + 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 math.add_dicts(*math.flatten_linked_list(weights_list)), total_weight, total_donation_weight + class OkayTracker(forest.Tracker): def __init__(self, net, my_share_hashes, my_doa_share_hashes): forest.Tracker.__init__(self, delta_type=forest.get_attributedelta_type(dict(forest.AttributeDelta.attrs, @@ -285,7 +321,7 @@ class OkayTracker(forest.Tracker): ))) self.verified.get_nth_parent_hash = self.get_nth_parent_hash # self is a superset of self.verified - self.get_cumulative_weights = skiplists.WeightsSkipList(self) + self.get_cumulative_weights = WeightsSkipList(self) def attempt_verify(self, share): if share.hash in self.verified.shares: diff --git a/p2pool/skiplists.py b/p2pool/skiplists.py deleted file mode 100644 index d14f306..0000000 --- a/p2pool/skiplists.py +++ /dev/null @@ -1,38 +0,0 @@ -from p2pool.util import forest, math - -class WeightsSkipList(forest.TrackerSkipList): - # share_count, weights, total_weight - - def get_delta(self, element): - from p2pool.bitcoin import data as bitcoin_data - share = self.tracker.shares[element] - att = bitcoin_data.target_to_average_attempts(share.target) - return 1, {share.new_script: att*(65535-share.share_data['donation'])}, att*65535, att*share.share_data['donation'] - - def combine_deltas(self, (share_count1, weights1, total_weight1, total_donation_weight1), (share_count2, weights2, total_weight2, total_donation_weight2)): - return share_count1 + share_count2, math.add_dicts(weights1, weights2), total_weight1 + total_weight2, total_donation_weight1 + total_donation_weight2 - - def initial_solution(self, start, (max_shares, desired_weight)): - assert desired_weight % 65535 == 0, divmod(desired_weight, 65535) - return 0, None, 0, 0 - - 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: - assert (desired_weight - total_weight1) % 65535 == 0 - 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_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: - return 0 - else: - return -1 - - 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 math.add_dicts(*math.flatten_linked_list(weights_list)), total_weight, total_donation_weight diff --git a/p2pool/test/test_data.py b/p2pool/test/test_data.py index 784dc13..c9f3527 100644 --- a/p2pool/test/test_data.py +++ b/p2pool/test/test_data.py @@ -3,6 +3,8 @@ import unittest from p2pool import data from p2pool.bitcoin import data as bitcoin_data +from p2pool.test.util import test_forest +from p2pool.util import forest def random_bytes(length): return ''.join(chr(random.randrange(2**8)) for i in xrange(length)) @@ -28,3 +30,12 @@ class Test(unittest.TestCase): d3 = random_bytes(random.randrange(2048)) x = data.prefix_to_hash_link(d + d2, d2) assert data.check_hash_link(x, d3, d2) == bitcoin_data.hash256(d + d2 + d3) + + def test_skiplist(self): + t = forest.Tracker() + d = data.WeightsSkipList(t) + for i in xrange(200): + t.add(test_forest.FakeShare(hash=i, previous_hash=i - 1 if i > 0 else None, new_script=i, share_data=dict(donation=1234), target=2**249)) + for i in xrange(200): + a = random.randrange(200) + d(a, random.randrange(a + 1), 1000000*65535)[1] diff --git a/p2pool/test/test_skiplists.py b/p2pool/test/test_skiplists.py deleted file mode 100644 index 7934d36..0000000 --- a/p2pool/test/test_skiplists.py +++ /dev/null @@ -1,16 +0,0 @@ -import random -import unittest - -from p2pool import skiplists -from p2pool.util import forest -from p2pool.test.util import test_forest - -class Test(unittest.TestCase): - def test_all(self): - t = forest.Tracker() - d = skiplists.WeightsSkipList(t) - for i in xrange(200): - t.add(test_forest.FakeShare(hash=i, previous_hash=i - 1 if i > 0 else None, new_script=i, share_data=dict(donation=1234), target=2**249)) - for i in xrange(200): - a = random.randrange(200) - d(a, random.randrange(a + 1), 1000000*65535)[1]