def test_hachage(some_blocks): bc = Blockchain() block_json = json.dumps(some_blocks, sort_keys=True).encode() hash_test = hashlib.sha256(block_json).hexdigest() assert len(hash_test) == 64 assert isinstance(hash_test, type(bc.hachage(some_blocks))) assert hash_test == bc.hachage(some_blocks)
def test_block_creation(a_valid_block, proof=123, previous_hash='abc'): bc = Blockchain() block_a_tester = bc.new_block(proof, previous_hash) assert block_a_tester['index'] == a_valid_block['index'] assert isinstance(block_a_tester['timestamp'], type(a_valid_block['timestamp'])) assert block_a_tester['proof'] == a_valid_block['proof'] assert block_a_tester['previous_hash'] == a_valid_block['previous_hash']
def test_create_transaction(): bc = Blockchain() bc.new_transaction('a', 'b', 1) transaction = bc.current_transactions[-1] assert transaction assert transaction['sender'] == 'a' assert transaction['recipient'] == 'b' assert transaction['amount'] == 1
def test_initialization_blockchain(first_block): bc = Blockchain() assert bc.chain[0]['index'] == first_block['index'] assert isinstance(bc.chain[0]['timestamp'], type(first_block['timestamp'])) assert bc.chain[0]['transactions'] == first_block['transactions'] assert bc.chain[0]['proof'] == first_block['proof'] assert bc.chain[0]['previous_hash'] == first_block['previous_hash']
def test_resets_current_transactions_when_mined(): bc = Blockchain() bc.new_transaction('a', 'b', 1) initial_length = len(bc.current_transactions) bc.new_block(123, 'abc') current_length = len(bc.current_transactions) assert initial_length == 1 assert current_length == 0
from argparse import ArgumentParser from uuid import uuid4 from flask import Flask, jsonify, request from app.chaine.blockchain import Blockchain app = Flask(__name__) # Générer une adresse unique pour ce nœud # uuid4 est globalement plus aléatoire/unique que uuid1 node_identifier = str(uuid4()).replace('-', '') # Instantie la Blockchain blockchain = Blockchain() @app.route('/mine', methods=['GET']) def mine(): """ mine permet de miner un nouveau block. Elle est déclenchée par le endpoint /mine (requête GET) - exécute l'algorithme de preuve de travail. - reçoit une récompense (un coin/une pièce) pour avoir trouvé la preuve. - L'expéditeur est "0" pour signifier que c'est le nœud qui a extrait une nouvelle pièce/coin. - Fabrique le nouveau bloc en l'ajoutant à la chaîne :return: réponse en JSON contenant les informations sur le nouveau block et le hash précédent (immuabilité de la blockchain), et un code 200 OK. :rtype: <JSON> """
def test_register_node(some_adresses, striped_adresses, expected): bc = Blockchain() bc.register_node(some_adresses) assert (striped_adresses in bc.nodes) is expected
def test_last_block(): bc = Blockchain() assert bc.last_block == bc.chain[-1]
def one_transaction(): bc = Blockchain() bc.new_transaction('a', 'b', 1)