def validate(self): # Validates a block's PoW data = self._prepare_data(self._block.nonce) hash_hex = utils.sum256(data) hash_int = int(hash_hex, 16) return True if hash_int < self._target else False
def hashTX(self): # return a hash of the transactions in the block tx_hashs = [] for tx in self.data: tx_hashs.append(pickle.dumps(tx.ID)) m_tree = MerkleTree(tx_hashs) return utils.sum256(m_tree.root_hash)
def mine(self): nonce = 0 #print("Mining the block containing \'%s\'" % (self._block.data)) while True: data = self._init_data(nonce) hash_str = sum256(data) #sys.stdout.write("%s \r" % (hash_str)) hash_int = int(hash_str, 16) #sys.stdout.write("%d \r" %(hash_int)) if hash_int < self._target: break else: nonce += 1 print("Mined!\nhash: %s" % hash_str) return nonce, hash_str
def run(self): # Performs a proof-of-work nonce = 0 print("Mining the block containing: {0}".format(self._block.data)) while nonce < self.max_nonce: data = self._prepare_data(nonce) hash_hex = utils.sum256(data) sys.stdout.write("%s \r" % (hash_hex)) hash_int = int(hash_hex, 16) if hash_int < self._target: break else: nonce += 1 return nonce, hash_hex
def validate(self): data = self._init_data(self._block.nonce) hash_int = int(sum256(data), 16) return True if hash_int < self._target else False
def hash_transactions(self): tx_hashes = [] for tx in self._tx_list: tx_hashes.append(tx.ID) return sum256(encode(''.join(tx_hashes)))
def set_id(self): # sets ID of a transaction self._id = utils.sum256(pickle.dumps(self)) return self