def mine_it(current_block, others_found_first_func, log_it=False): """ TODO: make this cypthon to speed it up """ nonce = current_block.nonce target = 2**256 / current_block.difficulty - 1 while not others_found_first_func(current_block.block_num): current_block.set_nonce(nonce) current_block.update_timestamp() current_hash = sha(current_block.block_header) # has nonce inside sha if log_it: print(nonce, current_hash, target) if current_hash < target: # PoW here return current_block nonce += 1 return None # somebody else found the block first
def mine_it_pos(current_block, others_found_first_func, my_holdings, log_it=False): """ Can optimize so that we find as many blocks in the future as possible. Also try to fork blockchain? Nothing at stake problem Add slashing """ target = (2**256 / current_block.difficulty - 1) * my_holdings while not others_found_first_func(current_block.block_num): current_block.update_timestamp() current_hash = sha(current_block.block_data) # no nonce here if current_hash < target: # PoW here return current_block if log_it: print(current_hash) time.sleep(.1) # can only find one block per timestamp return None # somebody else found the block first
def __init__(self, block_num, transactions, nonce, difficulty, merkle_root=None, block_hash=None, prev_hash=None, is_genesis=False, test=False, timestamp=None): self.block_num = block_num self.transactions = transactions self.difficulty = difficulty self.nonce = nonce mt = MT(transactions) self.block_data = {'transactions': transactions} self.block_header = {'block_num':block_num, 'nonce':nonce, 'difficulty':difficulty, 'transaction_merkle_root':mt.root.data if merkle_root is None else merkle_root, 'timestamp':time.time() if timestamp is None else timestamp } if is_genesis: self.block_header['prev_hash'] = None elif prev_hash is not None: self.block_header['prev_hash'] = prev_hash else: if not test: self.block_header['prev_hash'] = get_prev_hash(self.block_num) else: self.block_header['prev_hash'] = None self.block_header['hash'] = sha(self.block_header) if block_hash is None else block_hash
def __hash__(self): h = sha(' '.join(map(str, map(hash, self.outputs)))) return h
def __hash__(self): h = sha(self.address + str(self.amount) + str(self.script)) return h
def __hash__(self): x = list(map(str, map(hash, self.inputs))) h = sha(' '.join(x)) return h
def __hash__(self): x = str(self.input_data) + self.input_tx_hash + self.input_tx_index h = sha(x) return h
def encode(self): return str(hash(self.inputs) + hash(self.outputs) + sha(self.ts)).encode()
def __hash__(self): return sha(self.encode())
def __init__(self, data): self.data = sha(data) self.non_hashed_data = data
def __init__(self, left, right): self.left = left self.right = right self.data = sha( self.left.data + self.right.data )