Ejemplo n.º 1
0
    def resolve(self):
        winner_chain = self.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 = [JBlock(block["index"], block["previous_hash"],
                                     [Transaction(transaction["sender"], transaction["recipient"], transaction["signature"], transaction["amount"]) for transaction in block["transactions"]],
                                     block["proof"], block["timestamp"]) for block in node_chain]

                node_chain_lenght = len(node_chain)
                local_chain_lenght = len(self.chain)
                if node_chain_lenght > local_chain_lenght and Verification.validate_j_chain(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 = []
        FileHandler.save_j_chain(self)
        return replace
Ejemplo n.º 2
0
    def listen_for_input(self):

        start = True

        quit_j_chain = self.load_or_create_wallet()

        while not quit_j_chain:
            if start:
                self.print_possibilities()
                start = False

            decision = self.get_user_decision()
            if decision == "Q":  # quit
                quit_j_chain = True
                self.print_j_chain_elements()
            elif decision == "T":  # transaction
                if self.wallet.public_key is None:
                    print(
                        "You got no wallet, please load (L) or create (W) one."
                    )
                else:
                    recipient, amount = self.get_transaction_input()
                    signature = self.wallet.sign_transaction(
                        self.wallet.public_key, recipient, amount)
                    self.j_chain.add_transaction(self.wallet.public_key,
                                                 recipient, signature, amount)
            elif decision == "M":  # mine block
                if self.wallet.public_key is None:
                    print(
                        "You got no wallet, please load (L) or create (W) one."
                    )
                else:
                    self.j_chain.mine_block()
            elif decision == "P":  # Participants
                print([
                    participant + ": " +
                    str(self.j_chain.get_balance(participant))
                    for participant in self.participants
                ])
            else:
                print("This action is not available!")

            if not Verification.validate_j_chain(self.j_chain.chain):
                break