def __proof_of_work(self, dictionary): """ Increment a property 'nonce' until the resulting hash has a leading amount of zeros equal to Blockchain.__difficulty """ hashed = Block.sha(dictionary) while hashed[:self.__difficulty[0]] != '0' * self.__difficulty[0]: dictionary['nonce'] += 1 hashed = Block.sha(dictionary) return dictionary
def __validate_gen(self, properties, set_hash): if all(x == 0 for x in [ properties['index'], properties['prev_hash'], properties['origin'], properties['destination'] ]): if properties['data'] == 'Genesis': props_c = copy.deepcopy(properties) hashed = Block.sha(props_c) if all(j == hashed for j in [properties['hash'], set_hash[7]]): if hashed[:self. __difficulty[0]] == '0' * self.__difficulty[0]: return True return False
def __hash_check(self, props, iprops, prev_iprops): """ Incorrect Blocks are initialized as empty dicts, check lengths to detect that Check property:'prev_hash' equals property:'hash' of previous Block Verify hash of all properties equals the property:'hash' value Check resulting hash has a leading amount of zeros equal to Blockchain.__difficulty (check if proof of work was used) """ if len(iprops) == len(prev_iprops): if iprops[3] == prev_iprops[7]: props_c = copy.deepcopy(props) hashed = Block.sha(props_c) if all(x == hashed for x in [props['hash'], iprops[7]]): if hashed[:self. __difficulty[0]] == '0' * self.__difficulty[0]: return True return False