Skip to content

Commit

Permalink
added broken_mode option to get_cumulative_weights
Browse files Browse the repository at this point in the history
  • Loading branch information
forrestv committed Mar 17, 2012
1 parent 725af5d commit 576c4bd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
16 changes: 10 additions & 6 deletions p2pool/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def generate_transaction(tracker, share_data, block_target, desired_timestamp, d
weights, total_weight, donation_weight = tracker.get_cumulative_weights(share_data['previous_share_hash'],
min(height, net.REAL_CHAIN_LENGTH),
65535*net.SPREAD*bitcoin_data.target_to_average_attempts(block_target),
True,
)
assert total_weight == sum(weights.itervalues()) + donation_weight, (total_weight, sum(weights.itervalues()) + donation_weight)

Expand Down Expand Up @@ -167,7 +168,7 @@ def generate_transaction(tracker, share_data, block_target, desired_timestamp, d
)

def get_expected_payouts(tracker, best_share_hash, block_target, subsidy, net):
weights, total_weight, donation_weight = tracker.get_cumulative_weights(best_share_hash, min(tracker.get_height(best_share_hash), net.REAL_CHAIN_LENGTH), 65535*net.SPREAD*bitcoin_data.target_to_average_attempts(block_target))
weights, total_weight, donation_weight = tracker.get_cumulative_weights(best_share_hash, min(tracker.get_height(best_share_hash), net.REAL_CHAIN_LENGTH), 65535*net.SPREAD*bitcoin_data.target_to_average_attempts(block_target), False)
res = dict((script, subsidy*weight//total_weight) for script, weight in weights.iteritems())
res[DONATION_SCRIPT] = res.get(DONATION_SCRIPT, 0) + subsidy - sum(res.itervalues())
return res
Expand Down Expand Up @@ -278,27 +279,30 @@ def get_delta(self, element):
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)):
def initial_solution(self, start, (max_shares, desired_weight, broken_mode)):
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)):
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, broken_mode)):
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))
if broken_mode:
new_weights = dict(script=(desired_weight - total_weight1)//65535*weights2[script]//(total_weight2//65535))
else:
new_weights = {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)):
def judge(self, (share_count, weights_list, total_weight, total_donation_weight), (max_shares, desired_weight, broken_mode)):
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)):
def finalize(self, (share_count, weights_list, total_weight, total_donation_weight), (max_shares, desired_weight, broken_mode)):
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
Expand Down
2 changes: 1 addition & 1 deletion p2pool/test/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ def test_skiplist(self):
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]
d(a, random.randrange(a + 1), 1000000*65535, False)[1]
2 changes: 1 addition & 1 deletion p2pool/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_rate():

def get_users():
height, last = tracker.get_height_and_last(current_work.value['best_share_hash'])
weights, total_weight, donation_weight = tracker.get_cumulative_weights(current_work.value['best_share_hash'], min(height, 720), 65535*2**256)
weights, total_weight, donation_weight = tracker.get_cumulative_weights(current_work.value['best_share_hash'], min(height, 720), 65535*2**256, False)
res = {}
for script in sorted(weights, key=lambda s: weights[s]):
res[bitcoin_data.script2_to_human(script, net.PARENT)] = weights[script]/total_weight
Expand Down

0 comments on commit 576c4bd

Please sign in to comment.