added some checks to bitcoin target util functions
authorForrest Voight <forrest@forre.st>
Mon, 24 Jun 2013 02:06:40 +0000 (22:06 -0400)
committerForrest Voight <forrest@forre.st>
Wed, 26 Jun 2013 18:38:45 +0000 (14:38 -0400)
p2pool/bitcoin/data.py
p2pool/data.py

index de6d405..8e4fa52 100644 (file)
@@ -2,6 +2,7 @@ from __future__ import division
 
 import hashlib
 import random
+import warnings
 
 import p2pool
 from p2pool.util import math, pack
@@ -214,13 +215,18 @@ def check_merkle_link(tip_hash, link):
 # targets
 
 def target_to_average_attempts(target):
+    assert 0 <= target and isinstance(target, (int, long)), target
+    if target >= 2**256: warnings.warn('target >= 2**256!')
     return 2**256//(target + 1)
 
 def target_to_difficulty(target):
+    assert 0 <= target and isinstance(target, (int, long)), target
+    if target >= 2**256: warnings.warn('target >= 2**256!')
     return (0xffff0000 * 2**(256-64) + 1)/(target + 1)
 
 def difficulty_to_target(difficulty):
-    return (0xffff0000 * 2**(256-64) + 1)/difficulty - 1
+    assert difficulty >= 0
+    return min(int((0xffff0000 * 2**(256-64) + 1)/difficulty - 1 + 0.5), 2**256-1)
 
 # human addresses
 
index 1793043..8881623 100644 (file)
@@ -514,7 +514,7 @@ class OkayTracker(forest.Tracker):
                 best = best_share.previous_hash
             
             timestamp_cutoff = min(int(time.time()), best_share.timestamp) - 3600
-            target_cutoff = 2**256//(self.net.SHARE_PERIOD*best_tail_score[1] + 1) * 2 if best_tail_score[1] is not None else 2**256-1
+            target_cutoff = int(2**256//(self.net.SHARE_PERIOD*best_tail_score[1] + 1) * 2 + .5) if best_tail_score[1] is not None else 2**256-1
         else:
             timestamp_cutoff = int(time.time()) - 24*60*60
             target_cutoff = 2**256-1