Пример #1
0
 def resolve(self):
     for node in self.__peer_nodes:
         winner_chain = self.chain
         replace = False
         url = f"http://{node}/chain"
         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_blockchain(
                     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
Пример #2
0
 def resolve(self):
     winner_chain = self.chain
     replace = False
     for node in self.__peer_nodes:
         url = f'http://{node}/chain'
         try:
             response = requests.get(url)
             node_chain = response.json()
             node_chain = [
                 Block(block['previous_hash'], block['index'], [
                     Transaction(tx['sender'], tx['recipient'],
                                 tx['signature'], tx['amount'])
                     for tx in block['transactions']
                 ], block['proof']) for block in node_chain
             ]
             node_chain_length = len(node_chain)
             winner_chain_length = len(winner_chain)
             if (node_chain_length > winner_chain_length
                     and Verification.verify_blockchain(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
Пример #3
0
    def listen_to_user_input(self):
        waiting_for_user_input = True
        while waiting_for_user_input:
            print("Make a choice")
            print("1: Add new transaction value")
            print("2: Mine a new block")
            print("3: Outputting the blockchain blocks")
            print("4: Check transactions validity")
            print("5: Create wallet")
            print("6: Load wallet")
            print("7: Save keys")
            print("Q ----> Quit")

            user_choice = self.get_user_choice()

            if user_choice == "1":
                recipient, amount = self.get_transaction_value()
                signature = self.wallet.sign_transaction(self.wallet.public_key, recipient, amount)
                if self.blockchain.add_transaction(recipient, self.wallet.public_key, signature, amount=amount):
                    print("Transaction added successfuly!")
                else:
                    print("Transaction failed!")
                print(self.blockchain.get_open_transactions())
            elif user_choice == "2":
                if not self.blockchain.mine_block():
                    print("Mining failed. Got no wallet?")

            elif user_choice == "3":
                self.print_blockchain_elements()
            elif user_choice == "4":
                if Verification.verify_transactions(self.blockchain.get_open_transactions(), self.blockchain.get_balance):
                    print("All transactions are valid")
                else:
                    print("There are invalid transanction")
            elif user_choice == "5":
                  self.wallet.create_keys() 
                  self.blockchain = Blockchain(self.wallet.public_key, "") 
            elif user_choice == "6":
                self.wallet.load_keys()
                self.blockchain = Blockchain(self.wallet.public_key, "")
            elif user_choice == "7":
                self.wallet.save_keys()
            elif user_choice.lower() == 'q':
                break
            else:
                print("Invalid input, Please enter an option from those displayed on your screen!")
            if not Verification.verify_blockchain(self.blockchain.chain):
                self.print_blockchain_elements()
                print("Invalid blockchain!")
                break
            print("Balance of {}: {:6.2f}".format(self.wallet.public_key, self.blockchain.get_balance()))
        print("Done!")
Пример #4
0
 def listen_for_user_input(self):
     waiting_for_input = True
     while waiting_for_input:
         print("Please choose")
         print("1) Add a new value to blockchain.")
         print("2) Mine open transactions.")
         print("3) Print out blockchain.")
         print("4) Check open transactions for validity.")
         print("5) Create wallet.")
         print("6) Load wallet.")
         print("7) Save wallet.")
         print("q) Quit program.")
         choice = self.get_user_choice()
         if choice == "1":
             recipient, amount = self.get_transaction_value()
             signature = self.wallet.sign_transaction(recipient, amount)
             if self.blockchain.add_transaction(self.wallet.public_key,
                                                recipient, signature,
                                                amount):
                 print("Added Transaction.")
             else:
                 print("Transaction failed.")
         elif choice == "2":
             if not self.blockchain.mine_block():
                 print("Mining failed. Got a wallet?")
         elif choice == "3":
             self.print_blockchain_elements()
         elif choice == "4":
             print(
                 Verification.check_transactions_validity(
                     self.blockchain.get_open_transactions,
                     self.blockchain.get_balance))
         elif choice == "5":
             self.wallet.create_keys()
             self.blockchain = Blockchain(self.wallet.public_key)
         elif choice == "6":
             self.wallet.load_wallet()
             self.blockchain = Blockchain(self.wallet.public_key)
         elif choice == "7":
             self.wallet.save_keys()
         elif choice == "q":
             waiting_for_input = False
         else:
             print("Input is invalid.")
         if not Verification.verify_blockchain(self.blockchain.chain):
             print("Blockchain is not valid!")
             waiting_for_input = False
         self.print_balance()
     else:
         print("User left.")
Пример #5
0
    def listening_for_input(self):
        waiting_for_input = True
        while waiting_for_input:
            choice = self.get_user_choice()
            if choice == '1':
                if self.wallet.public_key==None:
                    print('Transaction failed!')
                    continue
                new_recipient, new_amount = self.get_transaction_value()
                signature=self.wallet.sign_transaction(self.wallet.public_key,new_recipient,new_amount)
                if self.blockchain.add_transaction(new_recipient, self.wallet.public_key, signature, new_amount)!=None:
                    print('Added transaction!')
                else:
                    print('Transaction failed!')
            elif choice == '2':
                if not self.blockchain.mine_block():
                    print('Mining failed. Got no wallet?')
            elif choice == '3':
                self.print_blockchain_elements()
            elif choice == '4':
                if Verification.verify_transactions(self.blockchain.get_balance,self.blockchain.get_open_transactions()):
                    print('All transactions are valid')
                else:
                    print('There are invalid transactions')
                print(self.blockchain.get_open_transactions())
            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 == 'q':
                waiting_for_input = False
            else:
                print('Input was invalid, please pick a value from the list!')
            if not Verification.verify_blockchain(self.blockchain.chain):
                print('Invalid blockchain!')
                break
            print(f"Balance of {self.wallet.public_key}: {self.blockchain.get_balance():6.2f}")
        else:
            print('User left!')

        print("Done!")
Пример #6
0
      def resolve( self ):

            winner_chain = self.__chain
            replaced = False

            for node in self.__peer_nodes:
                  
                  try:
                        url = 'http://{}/chain'.format( node )

                        response = requests.get( url )

                        node_chain = response.json()

                        node_chain = [ Block( block['index'], block['previous_hash'], 
                                          [ Transaction( tx['sender'], tx['recipient'], tx['amount'], tx['signature']) for 
                                                      tx in block['transactions'] ],
                                          block[ 'proof' ],
                                          block['timestamp']
                                          ) for block in node_chain ]
                        
                        if len( node_chain ) > len( winner_chain ) and Verification.verify_blockchain( node_chain ):
                              winner_chain = node_chain
                              replaced = True
                  except ConnectionError:
                        print('Connection error occured while connection chain REST api from resolve()...')

            self.__chain = winner_chain
            self.resolve_conflicts = False

            if replaced:
                  self.__open_transactions = []
                  print( 'Chain has been replaced !') 

            self.save_data()
            return replaced