def blockchain_three_blocks(): blockchain = Blockchain() for i in range(3): blockchain.add_block(i) return blockchain
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_blockchain_instance(): blockchain = Blockchain() assert blockchain.chain[0].hash == GENESIS_DATA['hash']
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='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 test_add_block(): blockchain = Blockchain() data = 'test-data' blockchain.add_block(data) assert blockchain.chain[-1].data == data
from backend.blockchain.Blockchain import Blockchain from backend.config import SECONDS import time blockchain = Blockchain() times = [] for i in range(1000): start_time = time.time_ns() blockchain.add_block(i) end_time = time.time_ns() time_to_mine = (end_time - start_time) / SECONDS times.append(time_to_mine) average_time = sum(times) / len(times) print(f'New block difficulty: {blockchain.chain[-1].difficulty}') print(f'Time to mine new block: {time_to_mine}s') print(f'Average time to add blocks: {average_time}s\n')
import os import random import requests from flask import Flask, jsonify, request 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__) blockchain = Blockchain() wallet = Wallet() transaction_pool = TransactionPool() pubsub = PubSub(blockchain, transaction_pool) # for i in range(3): # blockchain.add_block(i) @app.route('/') def route_default(): return 'Welcome to the blockchain' @app.route('/blockchain') def route_blockchain(): return jsonify(blockchain.to_json()) @app.route('/blockchain/mine') def route_blockchain_mine(): blockchain.add_block(transaction_pool.transaction_data()) block = blockchain.chain[-1]