from __future__ import division
import hashlib
+import random
+import warnings
+import p2pool
from p2pool.util import math, pack
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):
- def __init__(self, inner):
+ def __init__(self, inner, checksum_func=lambda data: hashlib.sha256(hashlib.sha256(data).digest()).digest()[:4]):
self.inner = inner
+ self.checksum_func = checksum_func
def read(self, file):
obj, file = self.inner.read(file)
data = self.inner.pack(obj)
- checksum, file = pack.read(file, 4)
- if checksum != hashlib.sha256(hashlib.sha256(data).digest()).digest()[:4]:
+ calculated_checksum = self.checksum_func(data)
+ checksum, file = pack.read(file, len(calculated_checksum))
+ if checksum != calculated_checksum:
raise ValueError('invalid checksum')
return obj, file
def write(self, file, item):
data = self.inner.pack(item)
- return (file, data), hashlib.sha256(hashlib.sha256(data).digest()).digest()[:4]
+ return (file, data), self.checksum_func(data)
class FloatingInteger(object):
__slots__ = ['bits', '_target']
('lock_time', pack.IntType(32)),
])
-merkle_branch_type = pack.ListType(pack.IntType(256))
+merkle_link_type = pack.ComposedType([
+ ('branch', pack.ListType(pack.IntType(256))),
+ ('index', pack.IntType(32)),
+])
merkle_tx_type = pack.ComposedType([
('tx', tx_type),
('block_hash', pack.IntType(256)),
- ('merkle_branch', merkle_branch_type),
- ('index', pack.IntType(32)),
+ ('merkle_link', merkle_link_type),
])
block_header_type = pack.ComposedType([
('txs', pack.ListType(tx_type)),
])
+# merged mining
+
aux_pow_type = pack.ComposedType([
('merkle_tx', merkle_tx_type),
- ('merkle_branch', merkle_branch_type),
- ('index', pack.IntType(32)),
+ ('merkle_link', merkle_link_type),
('parent_block_header', block_header_type),
])
+aux_pow_coinbase_type = pack.ComposedType([
+ ('merkle_root', pack.IntType(256, 'big')),
+ ('size', pack.IntType(32)),
+ ('nonce', pack.IntType(32)),
+])
+
+def make_auxpow_tree(chain_ids):
+ for size in (2**i for i in xrange(31)):
+ if size < len(chain_ids):
+ continue
+ res = {}
+ for chain_id in chain_ids:
+ pos = (1103515245 * chain_id + 1103515245 * 12345 + 12345) % size
+ if pos in res:
+ break
+ res[pos] = chain_id
+ else:
+ return res, size
+ raise AssertionError()
+
+# merkle trees
merkle_record_type = pack.ComposedType([
('left', pack.IntType(256)),
return 0
hash_list = list(hashes)
while len(hash_list) > 1:
- hash_list = [hash256(merkle_record_type.pack(dict(left=left, right=left if right is None else right)))
- for left, right in zip(hash_list[::2], hash_list[1::2] + [None])]
+ hash_list = [hash256(merkle_record_type.pack(dict(left=left, right=right)))
+ for left, right in zip(hash_list[::2], hash_list[1::2] + [hash_list[::2][-1]])]
return hash_list[0]
-def calculate_merkle_branch(hashes, index):
+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)],
)
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]
- assert check_merkle_branch(hashes[index], index, res) == hash_list[0][0]
+ if p2pool.DEBUG:
+ 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 res
+ return dict(branch=res, index=index)
-def check_merkle_branch(tip_hash, index, merkle_branch):
+def check_merkle_link(tip_hash, link):
+ if link['index'] >= 2**len(link['branch']):
+ raise ValueError('index too large')
return reduce(lambda c, (i, h): hash256(merkle_record_type.pack(
- dict(left=h, right=c) if 2**i & index else
+ dict(left=h, right=c) if (link['index'] >> i) & 1 else
dict(left=c, right=h)
- )), enumerate(merkle_branch), tip_hash)
+ )), enumerate(link['branch']), tip_hash)
+
+# 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)
-# 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'])
+def difficulty_to_target(difficulty):
+ assert difficulty >= 0
+ return min(int((0xffff0000 * 2**(256-64) + 1)/difficulty - 1 + 0.5), 2**256-1)
# human addresses
# transactions
def pubkey_to_script2(pubkey):
- return ('\x41' + pubkey) + '\xac'
+ assert len(pubkey) <= 75
+ return (chr(len(pubkey)) + pubkey) + '\xac'
def pubkey_hash_to_script2(pubkey_hash):
return '\x76\xa9' + ('\x14' + pack.IntType(160).pack(pubkey_hash)) + '\x88\xac'