def test_is_valid_transaction_chain_multiple_rewards(blockchain_three_blocks): reward_1 = Transaction.reward_transaction(Wallet()).to_json() reward_2 = Transaction.reward_transaction(Wallet()).to_json() blockchain_three_blocks.add_block([reward_1, reward_2]) with pytest.raises(Exception, match='one mining reward per block'): Blockchain.is_valid_transaction_chain(blockchain_three_blocks.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_historic_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_clear_blockchain_transactions(): transaction_pool = TransactionPool() transaction_1 = Transaction(Wallet(), 'recipient', 1) transaction_2 = Transaction(Wallet(), 'recipient', 2) transaction_pool.set_transaction(transaction_1) transaction_pool.set_transaction(transaction_2) blockchain = Blockchain() blockchain.add_block([transaction_1.to_json(), transaction_2.to_json()]) assert transaction_1.id in transaction_pool.transaction_map assert transaction_2.id in transaction_pool.transaction_map transaction_pool.clear_blockchain_transactions(blockchain) assert not transaction_1.id in transaction_pool.transaction_map assert not transaction_2.id in transaction_pool.transaction_map
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 received_amount_1 = 25 received_transaction_1 = Transaction( Wallet(), wallet.address, received_amount_1 ) received_amount_2 = 43 received_transaction_2 = Transaction( Wallet(), wallet.address, received_amount_2 ) blockchain.add_block( [received_transaction_1.to_json(), received_transaction_2.to_json()] ) assert Wallet.calculate_balance(blockchain, wallet.address) == STARTING_BALANCE - amount + received_amount_1 + received_amount_2
def test_blockchain_instance(): blockchain = Blockchain() assert blockchain.chain[0].hash == GENESIS_DATA['hash']
def test_valid_transaction_chain(blockchain_three_blocks): Blockchain.is_valid_transaction_chain(blockchain_three_blocks.chain)
def test_replace_chain_bad_chain(blockchain_three_blocks): blockchain = Blockchain() blockchain_three_blocks.chain[1].hash = 'evil_hash' with pytest.raises(Exception, match='The incoming chain is invalid'): blockchain.replace_chain(blockchain_three_blocks.chain)
def test_replace_chain_not_longer(blockchain_three_blocks): blockchain = Blockchain() with pytest.raises(Exception, match='The incoming chain must be longer'): blockchain_three_blocks.replace_chain(blockchain.chain)
def test_replace_chain(blockchain_three_blocks): blockchain = Blockchain() blockchain.replace_chain(blockchain_three_blocks.chain) assert blockchain.chain == blockchain_three_blocks.chain
def test_is_valid_chain_bad_genesis(blockchain_three_blocks): blockchain_three_blocks.chain[0].hash = 'evil_hash' with pytest.raises(Exception, match='genesis block must be valid'): Blockchain.is_valid_chain(blockchain_three_blocks.chain)
def test_is_valid_chain(blockchain_three_blocks): Blockchain.is_valid_chain(blockchain_three_blocks.chain)
def blockchain_three_blocks(): blockchain = Blockchain() for i in range(3): blockchain.add_block([Transaction(Wallet(), 'recipient', i).to_json()]) return blockchain
def test_add_block(): blockchain = Blockchain() data = 'test-data' blockchain.add_block(data) assert blockchain.chain[-1].data == data
import os import random import requests from flask import Flask, jsonify, request from flask_cors import CORS from backend.Blockchain.blockchain import Blockchain from backend.wallet.wallet import Wallet from backend.wallet.transaction import Transaction from backend.wallet.transaction_pool import TransactionPool from backend.pubsub import PubSub app = Flask(__name__) CORS(app, resources={ r'/*': {'origins': 'http://*****:*****@app.route('/') def route_default(): return 'Welcome to the Blockchain' @app.route('/blockchain') def route_blockchain(): return jsonify(blockchain.to_json()) @app.route('/blockchain/range') def route_blockchain_range():