Example #1
0
def hash_blockheader(blockheader):
    if blockheader.hash:
        return blockheader.hash
    if not blockheader.rawdata:
        blockheader.rawdata = BLOCK_SERIALIZE.serialize(blockheader)
    blockheader.hash = Uint256.from_bytestr(doublesha256(blockheader.rawdata))
    return blockheader.hash
Example #2
0
def hash_tx(tx):
    if tx.hash:
        return tx.hash
    if not tx.rawdata:
        tx.rawdata = TX_SERIALIZE.serialize(tx)
    tx.hash = Uint256.from_bytestr(doublesha256(tx.rawdata))
    return tx.hash
Example #3
0
 def mine_block(hash_prev,
                block_height,
                time_source,
                difficulty_bits,
                transactions,
                coinbase_txout_list,
                coinbase_flags=["/P2SH/"],
                nonce_changer=default_nonce_changer):
     template = BlockheaderTemplate(hash_prev,
                                    block_height,
                                    coinbase_txout_list,
                                    transactions,
                                    time_source.get_time(),
                                    difficulty_bits,
                                    coinbase_flags=coinbase_flags)
     difficulty_target = uint256_difficulty(difficulty_bits)
     hash_found = False
     while not hash_found:
         hash = Uint256.from_bytestr(doublesha256(
             template.get_serialized()))
         if (hash <= difficulty_target):
             hash_found = True
         else:
             nonce_changer(template)
     return (template.get_block(), template)
Example #4
0
def check_merkle_branch(txhash, merkle_branch, index_tx):
    """Return True if the merkle branch is valid for txhash and index_tx.
       
       Attributes:
           txhash(Uint256): Hash of a Transaction
           merkle_branch(list of Uint256): Merkle Branch
           index_tx(int): Index of the transaction in the block.
        
    """
    hash = txhash.get_bytestr()
    index = index_tx
    otherside_branch, merkle_root = merkle_branch[:-1], merkle_branch[-1]
    for otherside in otherside_branch:
        if index & 1:
            hash = double_sha256_2_input(otherside.get_bytestr(), hash)
        else:
            hash = double_sha256_2_input(hash, otherside.get_bytestr())
        index = index >> 1
        print "u", Uint256.from_bytestr(hash)           
    return (Uint256.from_bytestr(hash) == merkle_root)
Example #5
0
def check_merkle_branch(txhash, merkle_branch, index_tx):
    """Return True if the merkle branch is valid for txhash and index_tx.
       
       Attributes:
           txhash(Uint256): Hash of a Transaction
           merkle_branch(list of Uint256): Merkle Branch
           index_tx(int): Index of the transaction in the block.
        
    """
    hash = txhash.get_bytestr()
    index = index_tx
    otherside_branch, merkle_root = merkle_branch[:-1], merkle_branch[-1]
    for otherside in otherside_branch:
        if index & 1:
            hash = double_sha256_2_input(otherside.get_bytestr(), hash)
        else:
            hash = double_sha256_2_input(hash, otherside.get_bytestr())
        index = index >> 1
        print "u", Uint256.from_bytestr(hash)
    return (Uint256.from_bytestr(hash) == merkle_root)
Example #6
0
def get_merkle_branch(block, index_tx):
    """Get the merkle branch of a transaction.
    
    block:    block that contains the transaction
    index_tx: index of the transaction in the block
    
    Return value: [list of Uint256] 
    The first element is a hash of a transaction at the bottom of the merkle tree.  
    The last element is the merkle root.
    
    The algorithm uses XOR 1, to select the opposite element at each level.
    """
    merkle_branch = []
    merkle_tree = get_merkle_tree(block)
    for level in merkle_tree:
        merkle_branch.append(Uint256.from_bytestr(level[min(index_tx^1, len(level)-1)]))
        index_tx = index_tx >> 1
    return (merkle_branch)
Example #7
0
def get_merkle_branch(block, index_tx):
    """Get the merkle branch of a transaction.
    
    block:    block that contains the transaction
    index_tx: index of the transaction in the block
    
    Return value: [list of Uint256] 
    The first element is a hash of a transaction at the bottom of the merkle tree.  
    The last element is the merkle root.
    
    The algorithm uses XOR 1, to select the opposite element at each level.
    """
    merkle_branch = []
    merkle_tree = get_merkle_tree(block)
    for level in merkle_tree:
        merkle_branch.append(
            Uint256.from_bytestr(level[min(index_tx ^ 1,
                                           len(level) - 1)]))
        index_tx = index_tx >> 1
    return (merkle_branch)
Example #8
0
 def mine_block(hash_prev,
                block_height,
                time_source,
                difficulty_bits,
                transactions, 
                coinbase_txout_list,
                coinbase_flags=["/P2SH/"],
                nonce_changer=default_nonce_changer):
     template = BlockheaderTemplate(hash_prev, 
                                    block_height,
                                    coinbase_txout_list, 
                                    transactions, 
                                    time_source.get_time(), 
                                    difficulty_bits,
                                    coinbase_flags=coinbase_flags)
     difficulty_target = uint256_difficulty(difficulty_bits)
     hash_found = False
     while not hash_found:
         hash = Uint256.from_bytestr(doublesha256(template.get_serialized()))
         if (hash <= difficulty_target):
             hash_found = True
         else:
             nonce_changer(template)
     return (template.get_block(), template)
Example #9
0
 def get_hashbestchain(self):
     return Uint256.from_bytestr(self.db.get("\x0dhashBestChain", txn=self.dbtxn))
Example #10
0
 def deserialize(self, data, cursor=0):
     if (len(data) - cursor) < 32:
         raise MissingDataException("%s: Not enought data for uint256" %
                                    (self.desc))
     bytestr, = struct.unpack_from("32s", data, cursor)
     return (Uint256.from_bytestr(bytestr), cursor + 32)
Example #11
0
 def get_hashbestchain(self):
     return Uint256.from_bytestr(
         self.db.get("\x0dhashBestChain", txn=self.dbtxn))
Example #12
0
 def deserialize(self, data, cursor=0):
     if (len(data) - cursor) < 32:
         raise MissingDataException("%s: Not enought data for uint256" % (self.desc))
     bytestr, = struct.unpack_from("32s", data, cursor)
     return (Uint256.from_bytestr(bytestr), cursor + 32)
Example #13
0
def compute_merkle_root(transactions):
    hashes = [hash_tx(tx).get_bytestr() for tx in transactions]
    while (len(hashes) != 1):
        hashes = next_merkle_level(hashes)
    return (Uint256.from_bytestr(hashes[0]))
Example #14
0
def compute_merkle_root(transactions):
    hashes = [hash_tx(tx).get_bytestr() for tx in transactions]
    while (len(hashes) != 1):
        hashes = next_merkle_level(hashes)
    return (Uint256.from_bytestr(hashes[0]))