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
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
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)
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)
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)
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)
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)
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)
def get_hashbestchain(self): return Uint256.from_bytestr(self.db.get("\x0dhashBestChain", txn=self.dbtxn))
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)
def get_hashbestchain(self): return Uint256.from_bytestr( self.db.get("\x0dhashBestChain", txn=self.dbtxn))
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)
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]))
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]))