def mine_block(self): # mines a new block by adding all open transactions to the blockchain if self.hosting_node == None: return False last_block = self.__chain[-1] hashed_block = hashBlock(last_block) proof = self.proof_of_work() # issues a reward to the miner # reward_transaction = { # "sender": "MINING", # "recipient": owner, # "amount": MINING_REWARD # } reward_transaction = Transaction("", "MINING", self.hosting_node, MINING_REWARD) copied_transactions = self.__open_transactions[:] copied_transactions.append(reward_transaction) block = Block(index=len(self.__chain), previous_hash=hashed_block, transactions=copied_transactions, proof=proof) for tx in block.transactions: if not Wallet.verify_signature(tx): return False self.__chain.append(block) self.__open_transactions = [] self.save_data() return True
def add_value(self, signature, sender, recipient, amount=1.0): """ appends a new value to the open transaction list. Arguments: :sender: The sender of the transaction. :recipient: Receiver of the transaction. :amount: The amount of the transaction. """ # transaction = { # "sender": sender, # "recipient": recipient, # "amount": amount # } if self.hosting_node == None: return False transaction = Transaction(signature, sender, recipient, amount) if not Wallet.verify_signature(transaction): return False if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) self.save_data() return True print("insufficient funds") return False
def get_proof(): global miner serialized_tx = request.form['transaction'] tx = Transaction.deserialize(serialized_tx) proof = miner.get_transaction_proof(tx) response = Response(response=proof, status=200) return response
def receive_transaction(): global miner serialized_tx = request.form['block'] tx = Transaction.deserialize(serialized_tx) try: miner.add_transaction(tx) print("Added transaction\n") return Response(status=200) except ValueError: return Response(status=500)
def load_data(self): try: with open("blockchain.txt", mode="r") as data: starting_data = data.readlines() # file_content = pickle.loads(data.read()) # blockchain = file_content["bc"] # open_transactions = file_content["ot"] blockchain_data = json.loads(starting_data[0][:-1]) updated_blockchain = [] for block in blockchain_data: converted_tx = [ Transaction(transaction["signature"], transaction["sender"], transaction["recipient"], transaction["amount"]) for transaction in block["transactions"] ] updated_block = Block(index=len(updated_blockchain), previous_hash=block["previous_hash"], transactions=converted_tx, proof=block["proof"], timestamp=block["timestamp"]) updated_blockchain.append(updated_block) self.chain = updated_blockchain open_transactions_data = json.loads(starting_data[1]) self.__open_transactions = [ Transaction(transaction["signature"], transaction["sender"], transaction["recipient"], transaction["amount"]) for transaction in open_transactions_data ] except (IOError, IndexError): print("file load error")
def receive_proof(): global client serialized_tx = request.form['transaction'] tx = Transaction.deserialize(serialized_tx) serialized_proof = request.form['proof'] # ([proof_idx, proof], root) proof, root = client.deserialize_proof(serialized_proof) if(client.validate_transaction(tx, proof, root)): print("Proof valid") client.balance += int(tx.amount) print(f"Received {tx.amount}") return Response(status=200) else: print("Proof BAAAAAAAAAAAAAAD") return Response(status=406)
def initialize_transaction(sender, receiver, amount, fee, message): new_transaction = Transaction(sender, receiver, amount, fee, message) return new_transaction