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
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,
)))
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:
+++ /dev/null
-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
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))
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]
+++ /dev/null
-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]