def resolve(self): winner_chain = self.get_chain() replace = False for node in self.__peer_nodes: url = 'http://{}/chain'.format(node) try: response = requests.get(url) node_chain = response.json() node_chain = [ Block(block['index'], block['previous_hash'], [ Transaction(tx['sender'], tx['recipient'], tx['signature'], tx['amount']) for tx in block['transactions'] ], block['proof'], block['timestamp']) for block in node_chain ] node_chain_length = len(node_chain) local_chain_length = len(winner_chain) if (node_chain_length > local_chain_length and Verification.verify_chain_integrity(node_chain)): winner_chain = node_chain replace = True except requests.exceptions.ConnectionError: continue self.resolve_conflicts = False self.__chain = winner_chain if replace: self.__open_transactions = [] self.save_data() return replace
def listen_for_input(self): while True: choice = self.get_menu_input() if choice == 1: tx_recipient, tx_amount = self.get_transaction_input() signature = self.wallet.sign_transactions( self.wallet.public_key, tx_recipient, tx_amount) if self.blockchain.add_transaction( recipient=tx_recipient, sender=self.wallet.public_key, signature=signature, amount=tx_amount): print('Added Transaction') else: print('Transaction Failed!') print(self.blockchain.get_open_transactions()) elif choice == 2: if not self.blockchain.mine_block(): print('Mining Failed...Got wallet ?') elif choice == 3: self.display_blockchain() elif choice == 4: if Verification.verify_transactions( self.blockchain.get_open_transactions(), self.blockchain.get_balance): print('All transactions are valid!') else: print('Invalid Tx present!') elif choice == 5: self.wallet.create_keys() self.blockchain = Blockchain(self.wallet.public_key) elif choice == 6: self.wallet.load_keys() self.blockchain = Blockchain(self.wallet.public_key) elif choice == 7: self.wallet.save_keys() elif choice == 8: break else: print('Invalid Input!') if not Verification.verify_chain_integrity( self.blockchain.get_chain()): print('Block chain has been compromised .... x x x x ') break print( f'Balance of {self.wallet.public_key} = {self.blockchain.get_balance():.2f}' ) print('Done :) ') return 0