added some checks to bitcoin target util functions
[p2pool.git] / p2pool / bitcoin / data.py
index 8ec40de..8e4fa52 100644 (file)
@@ -1,6 +1,8 @@
 from __future__ import division
 
 import hashlib
+import random
+import warnings
 
 import p2pool
 from p2pool.util import math, pack
@@ -9,6 +11,8 @@ def hash256(data):
     return pack.IntType(256).unpack(hashlib.sha256(hashlib.sha256(data).digest()).digest())
 
 def hash160(data):
+    if data == '04ffd03de44a6e11b9917f3a29f9443283d9871c9d743ef30d5eddcd37094b64d1b3d8090496b53256786bf5c82932ec23c3b74d9f05a6f95a8b5529352656664b'.decode('hex'):
+        return 0x384f570ccc88ac2e7e00b026d1690a3fca63dd0 # hack for people who don't have openssl - this is the only value that p2pool ever hashes
     return pack.IntType(160).unpack(hashlib.new('ripemd160', hashlib.sha256(data).digest()).digest())
 
 class ChecksummedType(pack.Type):
@@ -176,12 +180,12 @@ def merkle_hash(hashes):
 def calculate_merkle_link(hashes, index):
     # XXX optimize this
     
-    hash_list = [(h, i == index, []) for i, h in enumerate(hashes)]
+    hash_list = [(lambda _h=h: _h, i == index, []) for i, h in enumerate(hashes)]
     
     while len(hash_list) > 1:
         hash_list = [
             (
-                hash256(merkle_record_type.pack(dict(left=left, right=right))),
+                lambda _left=left, _right=right: hash256(merkle_record_type.pack(dict(left=_left(), right=_right()))),
                 left_f or right_f,
                 (left_l if left_f else right_l) + [dict(side=1, hash=right) if left_f else dict(side=0, hash=left)],
             )
@@ -189,11 +193,13 @@ def calculate_merkle_link(hashes, index):
                 zip(hash_list[::2], hash_list[1::2] + [hash_list[::2][-1]])
         ]
     
-    res = [x['hash'] for x in hash_list[0][2]]
+    res = [x['hash']() for x in hash_list[0][2]]
     
     assert hash_list[0][1]
     if p2pool.DEBUG:
-        assert check_merkle_link(hashes[index], dict(branch=res, index=index)) == hash_list[0][0]
+        new_hashes = [random.randrange(2**256) if x is None else x
+            for x in hashes]
+        assert check_merkle_link(new_hashes[index], dict(branch=res, index=index)) == merkle_hash(new_hashes)
     assert index == sum(k*2**i for i, k in enumerate([1-x['side'] for x in hash_list[0][2]]))
     
     return dict(branch=res, index=index)
@@ -209,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