Example #1
0
def test_transaction_update():
    sender_wallet = Wallet()
    first_recipient = 'first_recipient'
    first_amount = 50

    transaction = Transaction(sender_wallet, first_recipient, first_amount)

    next_recipient = 'next_recipient'
    next_amount = 75
    transaction.update(sender_wallet, next_recipient, next_amount)

    assert transaction.output[next_recipient] == next_amount
    assert transaction.output[sender_wallet.address] ==\
        sender_wallet.balance - first_amount - next_amount
    assert Wallet.verify(transaction.input['public_key'], transaction.output,
                         transaction.input['signature'])

    to_first_again_amount = 25
    transaction.update(sender_wallet, first_recipient, to_first_again_amount)

    assert transaction.output[first_recipient] == \
        first_amount + to_first_again_amount
    assert transaction.output[sender_wallet.address] ==\
        sender_wallet.balance - first_amount - next_amount - to_first_again_amount
    assert Wallet.verify(transaction.input['public_key'], transaction.output,
                         transaction.input['signature'])
Example #2
0
 def is_valid_transaction(transaction):
     if transaction.input == MINING_REWARD_INPUT:
         if list(transaction.output.values()) != [MINING_REWARD]:
             raise Exception('Invalid mining reward')
         return
     output_total = sum(transaction.output.values())
     if transaction.input['amount'] != output_total:
         raise Exception('Invalid transaction output values')
     if not Wallet.verify(transaction.input['public_key'],
                          transaction.output,
                          transaction.input['signature']):
         raise Exception('Invalid signature')
Example #3
0
    def is_valid_transaction(transaction):
        """
        Validate a transaction. Raises and exception for invalid transactions.
        """

        if transaction.input == MINING_REWARD_INPUT:
            if list(transaction.output.values()) != [MINING_REWARD]:
                raise Exception("Invalid mining reward.")
            return

        output_sum = sum(transaction.output.values())
        if transaction.input["amount"] != output_sum:
            raise Exception(
                "The transaction is invalid because of the output values.")

        if not Wallet.verify(transaction.input["public_key"],
                             transaction.output,
                             transaction.input["signature"]):
            raise Exception("Invalid Signature.")
Example #4
0
    def is_valid_transaction(transaction):
        """
        Validate a transaction. Use to validate mining reward as well.
        Raise an exception for invalid transactions. 
        """
        if transaction.input == MINING_REWARD_INPUT:
            if list(transaction.output.values()) != [MINING_REWARD]:
                raise Exception("Invalid mining reward")
            return

        output_total = sum(transaction.output.values())
        if transaction.input["amount"] != output_total:
            raise Exception("Invalid transaction output values")
            
        if not Wallet.verify(
            transaction.input["public_key"],
            transaction.output, 
            transaction.input["signature"]
        ):
            raise Exception("Invalid signature")
Example #5
0
 def is_valid_transaction(transaction):
     """
     Validate a transaction
     :param transaction:
     :return:
     """
     if transaction.input == MINING_REWARD_INPUT:
         if list(transaction.output.values()) != [MINING_REWARD]:
             raise Exception('Invalid mining reward')
         # Mining reward is not signed to we need to return right away
         return
     output_total = sum(transaction.output.values())
     if transaction.input.get('amount') != output_total:
         raise Exception('Invalid Transaction output values')
     if not Wallet.verify(
             transaction.input.get('public_key'),
             transaction.output,
             transaction.input.get('signature')
     ):
         raise Exception('Invalid Signature')