Esempio n. 1
0
 def mine_block(self):
     if self.public_key is None:
         return None
     hashed_block = hash_block(self.__chain[-1])
     proof = self.proof_of_work()
     reward_transaction = Transaction('MINING', self.public_key, '',
                                      MINING_REWARD)
     copied_transactions = self.get_open_transactions()
     for tx in copied_transactions:
         if not Verification.verify_transaction(tx):
             return None
     copied_transactions.append(reward_transaction)
     block = Block(index=len(self.__chain),
                   previous_hash=hashed_block,
                   transactions=copied_transactions,
                   proof=proof)
     self.__chain.append(block)
     self.__open_transactions = []
     self.save_data()
     for node in self.__peer_nodes:
         converted_block = block.__dict__.copy()
         converted_block['transactions'] = [
             tx.__dict__ for tx in converted_block['transactions']
         ]
         url = 'http://{}/broadcast-block'.format(node)
         try:
             response = requests.post(url, json={'block': converted_block})
             if response.status_code == 500 or response.status_code == 400:
                 print('Block declined, need resolving')
         except requests.ConnectionError:
             continue
     return block
Esempio n. 2
0
    def add_transaction(self,
                        transaction: SignedRawTransaction,
                        is_receiving: bool = False) -> int:
        """
        Creates a new transaction to go into the next mined Block
        :param transaction: <SignedRawTransaction>
            A single Transaction
        :param is_receiving: Optional <bool>
            Use to determine if the transaction was created
            by this node or another on the network
        :return: <int>
            The index of the Block that will hold this transaction
        """

        if Verification.verify_transaction(transaction, self.get_balance,
                                           self.get_last_tx_nonce):
            final_tx = FinalTransaction(
                transaction_hash=Verification.hash_transaction(transaction),
                transaction_id=Verification.hash_transaction(transaction),
                signed_transaction=transaction,
            )

            self.__open_transactions.append(final_tx)
            self.save_data()

            if not is_receiving:
                self.__broadcast_transaction(transaction, "open")
        else:
            raise ValueError(
                "The sender does not have enough coin to make this "
                "transaction. We may want to change this to not raise "
                "an exception later, but for now, we should break.")
        return self.last_block.index + 1
Esempio n. 3
0
 def add_transaction(self, recipient, sender, amount=1.0):
     transaction = Transaction(sender, recipient, amount)
     verifier = Verification()
     if verifier.verify_transaction(transaction, self.get_balance):
         self.open_transactions.append(transaction)
         self.save_data()
         return True
     return False
Esempio n. 4
0
 def add_transaction(self, recipient, sender, amount=1.0):
     """ appends a new transaction to the last blockchain value """
     #transaction = {'sender': sender, 'recipient': recipient, 'amount': amount}
     transaction = Transaction(sender, recipient, amount)
     if Verification.verify_transaction(transaction, self.get_balance):
         self.__open_transactions.append(transaction)
         self.save_data()
         return True
     return False
Esempio n. 5
0
 def add_transaction(self, recipient, sender, amount=1.0):
     #transaction = {'sender': sender, 'recipient': recipient, 'amount': amount}
     transaction = Transaction(sender, recipient, amount)
     if Verification.verify_transaction(transaction, self.get_balance):
         self.__open_transactions.append(transaction)
         # participants.add(sender)
         # participants.add(recipient)
         self.save_data()
         return True
     return False
Esempio n. 6
0
def add_transaction(recipient, sender=owner, amount=1.0):
    transaction = Transaction(sender, recipient, amount)
    verifier = Verification()
    if verifier.verify_transaction(transaction, get_balance):
        open_transactions.append(transaction)
        participants.add(sender)
        participants.add(recipient)
        save_data()
        return True
    return False
Esempio n. 7
0
def add_transaction(recipient, sender=owner, amount=1.0):
    """ Append a new value as well as the last blockchain value """
    # transaction = {
    #     'sender': sender,
    #     'recipient': recipient,
    #     'amount': amount
    # }
    transaction = Transaction(sender, recipient, amount)
    verifier = Verification()
    if verifier.verify_transaction(transaction, get_balance):
        open_transactions.append(transaction)
        save_data()
        return True
    return False
Esempio n. 8
0
    def add_transaction(self, recipient, sender, amount=1.0):
        """ Append a new value as well as the last blockchain value to the blockchain

        Arguments:
            :sender: The sender of the coins.
            :recipient: The recipient of the coins.
            :amount: The amount of coins sent with the transaction (default=1.0)
        """
        # we are using OrderedDict to get an ordered dictionary so that the hash doesn't change due to the order changing
        transaction = Transaction(sender, recipient, amount)

        if Verification.verify_transaction(transaction, self.get_balance):
            self.__open_transactions.append(transaction)
            self.save_data()
            return True
        return False
Esempio n. 9
0
    def add_transaction(self, recipient, sender, amount=1.0):
        """ append a new value and last BC value to BC

        Arguments:
            :sender: the sender of the coins.
            :recipient: the recipient of the coins.
            :amount: the amount of coins sent with the transaction (default=1.0)
        """
        transaction = Transaction(sender, recipient, amount)
        verifier = Verification()
        if verifier.verify_transaction(transaction, self.get_balance):
            self.open_transactions.append(transaction)
            #Benefit of sets is here that if we add another Sophia which is already in the set it will ignore that
            #Hence sets make sure that we have only unique values in the set 
            self.save_data()
            return True
        return False
Esempio n. 10
0
 def add_transaction(self, recipient, sender, amount=1.0):
     """ Append a new value as well as the last blockchain value to the blockchain
     Arguments:
         :sender: The sender of the coins
         :recipient:
         :amount:
     """
     # transaction = {'sender': sender,
     #                'recipient': recipient,
     #                'amount': amount,
     #                }
     transaction = Transaction(sender, recipient, amount)
     if Verification.verify_transaction(transaction, self.get_balance):
         self.__open_transactions.append(transaction)
         self.save_data()
         return True
     return False
Esempio n. 11
0
 def add_transaction(self,recipient, sender, signature, amount = 1.0, is_receiving = False):
     transaction = Transaction(sender, recipient, signature, amount)
     if Verification.verify_transaction(transaction, self.get_balance):
         self.__open_transactions.append(transaction)
         self.save_data()
         if not is_receiving:
             for node in self.__peer_nodes:
                 url = 'http://{}/broadcast-transaction'.format(node)
                 try:
                     response = requests.post(url, json = {'sender': sender, 'recipient': recipient, 'amount': amount, 'signature': signature})
                     if response.status_code == 400 or response.status_code == 500:
                         print('Transaction declined, needs resolving')
                         return False
                 except requests.exceptions.ConnectionError:
                     continue
         return True
     return False
Esempio n. 12
0
    def add_transaction(self, recipient, sender, amount):
        # transaction = {
        #     'sender': sender,
        #     'recipient': recipient,
        #     'amount': amount
        # }

        # ensure node id was set
        if self.hosting_node == None:
            return False
        transaction = Transaction(sender, recipient, amount)

        if Verification.verify_transaction(transaction, self.get_balance):
            self.open_transactions.append(transaction)
            self.save_data()

            return True

        return False
Esempio n. 13
0
    def add_transaction(self, recipient, sender, amount=1.0):
        """ Append a new value as well as the last blockchain value to the blockchain.

        Arguments:
            :sender: The sender of the coins.
            :recipient: The recipient of the coins.
            :amount: The amount of coins sent with the transaction (default = 1.0)
        """
        # transaction = {
        #     'sender': sender,
        #     'recipient': recipient,
        #     'amount': amount
        # }
        transaction = Transaction(sender, recipient, amount)
        verifier = Verification()
        if verifier.verify_transaction(transaction, self.get_balance):
            self.open_transactions.append(transaction)
            self.save_data()
            return True
        return False
Esempio n. 14
0
    def add_transaction(self, recipient, sender, signature, amount=1.0):
        """ Append a new value as well as the last blockchain value to the blockchain 

        Arguments:
            :sender: The sender of the coins.
            :recipient: The recipient of the coins.
            :amount: The amount of coins sent with the transaction(default = 1.0)
        """
        # transaction = {
        #               'sender': sender,
        #               'recipient': recipient,
        #               'amount': amount
        #               }
        if self.hosting_node == None:
            return False
        transaction = Transaction(sender, recipient, signature, amount)
        if not Wallet.verify_transaction(transaction):
            return False
        if Verification.verify_transaction(transaction, self.get_balance):
            self.__open_transactions.append(transaction)
            self.save_data()
            return True
        return False