added some checks to bitcoin target util functions
[p2pool.git] / p2pool / bitcoin / data.py
index 75dce37..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,18 +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
-
-# tx
-
-def tx_get_sigop_count(tx):
-    return sum(script.get_sigop_count(txin['script']) for txin in tx['tx_ins']) + sum(script.get_sigop_count(txout['script']) for txout in tx['tx_outs'])
+    assert difficulty >= 0
+    return min(int((0xffff0000 * 2**(256-64) + 1)/difficulty - 1 + 0.5), 2**256-1)
 
 # human addresses