X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=p2pool%2Fbitcoin%2Fdata.py;h=74cb94bb7bc51986c4a9dd6ddda9fb0b862e1aa6;hb=f732111a6e08d7d0649c330d1c703535a8ea80b5;hp=de6d4057f4d29ae0b7f364faaaa17fee3666d9ef;hpb=3364c14b442a384fc9cda77863071e7421cfc61d;p=p2pool.git diff --git a/p2pool/bitcoin/data.py b/p2pool/bitcoin/data.py index de6d405..74cb94b 100644 --- a/p2pool/bitcoin/data.py +++ b/p2pool/bitcoin/data.py @@ -2,6 +2,7 @@ from __future__ import division import hashlib import random +import warnings import p2pool from p2pool.util import math, pack @@ -10,10 +11,11 @@ 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()) +def scrypt(data): + return pack.IntType(256).unpack(__import__('ltc_scrypt').getPoWHash(data)) + class ChecksummedType(pack.Type): def __init__(self, inner, checksum_func=lambda data: hashlib.sha256(hashlib.sha256(data).digest()).digest()[:4]): self.inner = inner @@ -92,6 +94,7 @@ address_type = pack.ComposedType([ tx_type = pack.ComposedType([ ('version', pack.IntType(32)), + ('timestamp', pack.IntType(32)), # txn timestamp ('tx_ins', pack.ListType(pack.ComposedType([ ('previous_output', pack.PossiblyNoneType(dict(hash=0, index=2**32 - 1), pack.ComposedType([ ('hash', pack.IntType(256)), @@ -130,6 +133,7 @@ block_header_type = pack.ComposedType([ block_type = pack.ComposedType([ ('header', block_header_type), ('txs', pack.ListType(tx_type)), + ('signature', pack.VarStrType()), # header signature field ]) # merged mining @@ -214,13 +218,23 @@ 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 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!') return (0xffff0000 * 2**(256-64) + 1)/(target + 1) def difficulty_to_target(difficulty): - return (0xffff0000 * 2**(256-64) + 1)/difficulty - 1 + 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 @@ -251,6 +265,16 @@ def address_to_pubkey_hash(address, net): raise ValueError('address not for this net!') return x['pubkey_hash'] +def address_to_script(address, net): + x = human_address_type.unpack(base58_decode(address)) + + print x['pubkey_hash'] + + if x['version'] != net.ADDRESS_VERSION: + raise ValueError('address not for this net!') + return '\x76\xa9' + ('\x14' + pack.IntType(160).pack(x['pubkey_hash'])) + '\x88\xac' + + # transactions def pubkey_to_script2(pubkey):