modulate share difficulty to prevent any node from producing more than 5% of shares
[p2pool.git] / p2pool / bitcoin / data.py
index 8e4fa52..88e1730 100644 (file)
@@ -219,6 +219,10 @@ def target_to_average_attempts(target):
     if target >= 2**256: warnings.warn('target >= 2**256!')
     return 2**256//(target + 1)
 
+def average_attempts_to_target(average_attempts):
+    assert average_attempts > 0
+    return min(int(2**256/average_attempts - 1 + 0.5), 2**256-1)
+
 def target_to_difficulty(target):
     assert 0 <= target and isinstance(target, (int, long)), target
     if target >= 2**256: warnings.warn('target >= 2**256!')
@@ -226,6 +230,7 @@ def target_to_difficulty(target):
 
 def difficulty_to_target(difficulty):
     assert difficulty >= 0
+    if difficulty == 0: return 2**256-1
     return min(int((0xffff0000 * 2**(256-64) + 1)/difficulty - 1 + 0.5), 2**256-1)
 
 # human addresses