def test_is_valid_transaction_chain_bad_history_balance( blockchain_three_blocks): wallet = Wallet() bad_transaction = Transaction(wallet, "recipient", 1) bad_transaction.output[wallet.address] = 9000 bad_transaction.input["amount"] = 9001 bad_transaction.input["signature"] = wallet.sign(bad_transaction.output) blockchain_three_blocks.add_block([bad_transaction.to_json()]) with pytest.raises(Exception, match="has an invalid input amount"): Blockchain.is_valid_transaction_chain(blockchain_three_blocks.chain)
def test_is_valid_transaction_chain_bad_history_balance( blockchain_three_blocks): wallet = Wallet() bad_transaction = Transaction(wallet, 'deadly_recipient', 1) bad_transaction.output[wallet.address] = 9999 bad_transaction.input['amount'] = 10000 bad_transaction.input['signature'] = wallet.sign(bad_transaction.output) blockchain_three_blocks.add_block([bad_transaction.to_json()]) with pytest.raises(Exception, match='has invalid input amount'): Blockchain.is_valid_transaction_chain(blockchain_three_blocks.chain)
def test_is_valid_transaction_chain_bad_historic_balance(blockchain_three_blocks): wallet = Wallet() bad_transaction = Transaction(wallet, 'recipient', 1) # don't understand the point of this line of code. the point of the test is to ensure bad data isn't entered into the input['amount'] field which would result in the sender wallet's balance getting inflated. altering the output[wallet.address] field only serves to change the signature result, as far as i can tell. the test even passes when commenting out this line of code. bad_transaction.output[wallet.address] = 9000 bad_transaction.input['amount'] = 9001 bad_transaction.input['signature'] = wallet.sign(bad_transaction.output) blockchain_three_blocks.add_block([bad_transaction.to_json()]) with pytest.raises(Exception, match='has invalid input amount'): Blockchain.is_valid_transaction_chain(blockchain_three_blocks.chain)
def test_calculate_balance(): blockchain = Blockchain() wallet = Wallet() assert Wallet.calculate_balance(blockchain, wallet.address) == wallet.balance transaction = Transaction(wallet, 'recipient', 30) blockchain.add_block([transaction.to_json()]) assert Wallet.calculate_balance(blockchain, wallet.address) == wallet.balance - 30 received_amount_1 = 25 recieved_transaction_1 = Transaction(Wallet(), wallet.address, received_amount_1) received_amount_2 = 25 recieved_transaction_2 = Transaction(Wallet(), wallet.address, received_amount_2) blockchain.add_block( [recieved_transaction_1.to_json(), recieved_transaction_2.to_json()]) assert Wallet.calculate_balance( blockchain, wallet.address ) == wallet.balance - 30 + received_amount_1 + received_amount_2
def route_wallet_transact(): transaction_data = request.get_json() transaction = transaction_pool.existing_transaction(wallet.address) if transaction: transaction.update(wallet, transaction_data['recipient'], transaction_data['amount']) else: transaction = Transaction(wallet, transaction_data['recipient'], transaction_data['amount']) pubsub.broadcast_transaction(transaction) return jsonify(transaction.to_json())
def test_is_transaction_chain_valid_balance_tillnow(blockchain_ten_blocks): wallet = Wallet() corrupt_transaction = Transaction(wallet, "abcd", 10) corrupt_transaction.output[wallet.address] = 9000 corrupt_transaction.input['amount'] = 9010 corrupt_transaction.input['signature'] = wallet.sign( corrupt_transaction.output) blockchain_ten_blocks.add_block([corrupt_transaction.to_json()]) with pytest.raises(Exception): BlockChain.is_transaction_chain_valid(blockchain_ten_blocks.chain)
def test_is_valid_transaction_chain_bad_historic_balance(): blockchain_three_blocks = Blockchain() for i in range(3): blockchain_three_blocks.add_block([Transaction(Wallet(), 'recipient', i).to_json()]) wallet = Wallet() bad_transaction = Transaction(wallet, 'recipient', 5) bad_transaction.output[wallet.address] = 10000 bad_transaction.input['amount'] = 10005 bad_transaction.input['signature'] = wallet.sign(bad_transaction.output) blockchain_three_blocks.add_block([bad_transaction.to_json()]) with pytest.raises(Exception, match="has an invalid input amount"): Blockchain.is_valid_transaction_chain(blockchain_three_blocks.chain)
def test_invalid_transaction_chain_bad_historic_balance(blockchain_init): wallet = Wallet() bad_transaction = Transaction(wallet, "recipient", 1) bad_transaction.output[wallet.address] = 9999 bad_transaction.input["amount"] = 10000 bad_transaction.input["signature"] = wallet.sign(bad_transaction.output) blockchain_init.add_block([bad_transaction.to_json()]) with pytest.raises(Exception, match="invalid input amount."): Blockchain.is_valid_transaction_chain(blockchain_init.chain)
def test_calculate_balance(): ''' Test that a wallet can calculate its balance correctly. ''' blockchain = Blockchain() wallet = Wallet() # Assert that the wallet's balance on a new blockchain is equal to the # global starting balance. assert (Wallet.calculate_balance(blockchain, wallet.address) == STARTING_BALANCE) # Create a transaction of amount 50 and verify that it is subtracted from # the wallet's balance after adding the new transaction to the blockchain. amount = 50 trans = Transaction(wallet, 'recipient', amount) blockchain.add_block([trans.to_json()]) EXPECTED_BALANCE = STARTING_BALANCE - amount # Assert that the new calculated balance is equal to the expected balance. assert (Wallet.calculate_balance(blockchain, wallet.address) == EXPECTED_BALANCE) # Create first transaction to be recieved by the wallet after the calculation. recieved_amount1 = 25 recieved_trans1 = Transaction(Wallet(), wallet.address, recieved_amount1) # Create second transaction to be recieved by the wallet after the calculation. recieved_amount2 = 43 recieved_trans2 = Transaction(Wallet(), wallet.address, recieved_amount2) blockchain.add_block( [recieved_trans1.to_json(), recieved_trans2.to_json()]) EXPECTED_BALANCE = (STARTING_BALANCE - amount + recieved_amount1 + recieved_amount2) # Assert that the new calculated balance is equal to the expected balance. assert (Wallet.calculate_balance(blockchain, wallet.address) == EXPECTED_BALANCE)
def test_calculate_balance(): blockchain = Blockchain() wallet = Wallet() assert Wallet.calculate_balance(blockchain, wallet.address) == STARTING_BALANCE amount = 50 transaction = Transaction(wallet, 'recipient', amount) blockchain.add_block([transaction.to_json()]) assert Wallet.calculate_balance(blockchain, wallet.address) == \ STARTING_BALANCE - amount recieved_amount_1 = 25 recieved_transaction_1 = Transaction( Wallet(), wallet.address, recieved_amount_1 ) recieved_amount_2 = 43 recieved_transaction_2 = Transaction( Wallet(), wallet.address, recieved_amount_2 ) blockchain.add_block( [ recieved_transaction_1.to_json(), recieved_transaction_2.to_json() ] ) assert Wallet.calculate_balance(blockchain, wallet.address) == \ STARTING_BALANCE - amount + recieved_amount_1 + recieved_amount_2
def walletTransact(request): if assets.address != request.user.username: assets.address = request.user.username if request.method == 'POST': transaction_data = json.loads(request.body.decode('utf-8')) print(transaction_data) transaction = transaction_pool.existing_transaction(wallet.address) if transaction: transaction.update(wallet, transaction_data['recipient'], transaction_data['amount']) else: transaction = Transaction(wallet, transaction_data['recipient'], transaction_data['amount']) pubsub.broadcast_transaction(transaction) return JsonResponse(transaction.to_json(), safe=False) # return JsonResponse({'ali1':'aa'},safe=False) if request.method == 'GET': return JsonResponse({'ali': 'aa'}, safe=False)
def route_wallet_transact(): print('start.... TRANSACTION:::::') # {'recipient' : 'foo', 'amount' : 15} transaction_data = request.get_json() transaction = transaction_pool.existing_transaction(wallet.address) if transaction: transaction.update( wallet, transaction_data['recipient'], transaction_data['amount'] ) else: transaction = Transaction( wallet, transaction_data['recipient'], transaction_data['amount'] ) pubsub.broadcast_transaction(transaction) return jsonify(transaction.to_json())
def test_valid_transaction_chain_bad_transaction(populated_blockchain): bad_transaction = Transaction(Wallet(), 'recipient', 1) bad_transaction.input['signature'] = Wallet().sign(bad_transaction.output) populated_blockchain.add_block([bad_transaction.to_json()]) with pytest.raises(Exception): Blockchain.is_valid_transaction_chain(populated_blockchain.chain)
def test_is_valid_transaction_chain_bad_transaction(blockchain_three_blocks): bad_transaction = Transaction(Wallet(), "recipient", 1) bad_transaction.input["signature"] = Wallet().sign(bad_transaction.output) blockchain_three_blocks.add_block([bad_transaction.to_json()]) with pytest.raises(Exception): Blockchain.is_valid_transaction_chain(blockchain_three_blocks.chain)
def test_is_valid_transaction_chain_bad_transaction(blockchain_3b): bad_trans = Transaction(Wallet(), 'recipient', 1) bad_trans.input['signature'] = Wallet().sign(bad_trans.output) blockchain_3b.add_block([bad_trans.to_json()]) with pytest.raises(Exception): Blockchain.is_valid_trans_chain(blockchain_3b.chain)