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_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_duplicate_transaction( blockchain_three_blocks): transaction = Transaction(Wallet(), 'recipient', 1).to_json() blockchain_three_blocks.add_block([transaction, transaction]) with pytest.raises(Exception, match='is not unique'): 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_replace_chain_not_longest(blockchain_three_blocks): blockchain = Blockchain() with pytest.raises( Exception, match='Cannot replace. The incoming chain must be longer.'): blockchain_three_blocks.replace_chain(blockchain.chain)
def test_replace_chain_bad_chain(blockchain_three_blocks): blockchain = Blockchain() blockchain_three_blocks.chain[1].hash = 'evil-hash' #TODO: fix mismatching match parameter in the test. with pytest.raises( Exception, match='Cannot replace. The incoming chain must be longer'): blockchain_three_blocks.replace_chain(blockchain_three_blocks.chain)
def test_clear_blockchain_transactions(): transaction_pool = TransactionPool() transaction1 = Transaction(Wallet(), 'recipient', 1) transaction2 = Transaction(Wallet(), 'recipient', 2) transaction_pool.set_transaction(transaction1) transaction_pool.set_transaction(transaction2) blockchain = Blockchain() blockchain.add_block([transaction1.to_json(), transaction2.to_json()]) assert transaction1.id in transaction_pool.transaction_map assert transaction2.id in transaction_pool.transaction_map transaction_pool.clear_blockchain_transactions(blockchain) assert transaction1.id not in transaction_pool.transaction_map assert transaction2.id not 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 = 77 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
import time from core.blockchain.blockchain import Blockchain from core.config import SECONDS blockchain = Blockchain() times = [] for i in range(1000): start_time = time.time_ns() blockchain.add_block(i) end_time = time.time_ns() time_to_time = (end_time - start_time) / SECONDS times.append(time_to_time) average_time = sum(times) / len(times) print(f'New Block Difficulty: {blockchain.chain[-1].difficulty}') print(f'Time to mine new block: {time_to_time}s') print(f'Average time to add blocks: {average_time}s\n')
def test_blockchain_instance(): blockchain = Blockchain() assert blockchain.chain[0].hash == GENESIS_DATA['hash']
def test_add_block(): blockchain = Blockchain() data = 'test-data' blockchain.add_block(data) assert blockchain.chain[-1].data == 'test-data'
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='The 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
import os import random import requests from flask import Flask, jsonify, request from flask_cors import CORS from core.blockchain.blockchain import Blockchain from core.pubsub import PubSub from core.wallet.wallet import Wallet from core.wallet.transaction import Transaction from core.wallet.transaction_pool import TransactionPool app = Flask(__name__) CORS(app, resources={r'/*': {'origins': 'http://*****:*****@app.route('/') def default(): return 'Welcome to the blockchain application.' @app.route('/blockchain') def route_blockchain(): return jsonify(blockchain.to_json()) @app.route('/blockchain/range')
def test_valid_transaction_chain(blockchain_three_blocks): Blockchain.is_valid_transaction_chain(blockchain_three_blocks.chain)