diff --git a/blockchain/input.txt b/blockchain/input.txt index 4efbe386abffd854811b30f5f76367dd5d66a174..e41c12f7b5e42ce45f13b9a7c6ba86886094b4a7 100644 Binary files a/blockchain/input.txt and b/blockchain/input.txt differ diff --git a/blockchain/test.txt b/blockchain/test.txt index 0bdc04c78f645138deb73a7c35eb02bdf3f047a9..182ec82a42e63f42e02837105959426f11d1fb7e 100644 Binary files a/blockchain/test.txt and b/blockchain/test.txt differ diff --git a/blockchain_gremaud_tefridj.py b/blockchain_gremaud_tefridj.py index 5645925d13f1d77e6104c221df2627f60683a988..8593b8204e03c0013fd3a404ac37dac207d2de51 100644 --- a/blockchain_gremaud_tefridj.py +++ b/blockchain_gremaud_tefridj.py @@ -2,9 +2,10 @@ Usage: ====== - python blockchain_gremaud_tefridj.py file_name + python blockchain_gremaud_tefridj.py file_name nonce file_name: le nom du fichier pour commencer la blockchain + nonce: la taille du nonce voulu pour le minage """ __authors__ = ("Denis Gremaud", "Yanis Tefridj") @@ -17,11 +18,10 @@ __version__= "1.0.1" import sys import hashlib import sys -import math import os import time -NONCE_SIZE = 13 # number of numbre that should be 0 in nonce +NONCE_SIZE = 13 """permet de lire un fichier @@ -58,7 +58,6 @@ def write_file_blockchain(input_file_path, data): file.write(data) file.close() - """permet d'obtenir le hash d'un fichier en sha256 Args: @@ -75,19 +74,16 @@ def hash_data(data, print_hash=False): print('hash : {}'.format(hash_file)) return hash_file -"""permet de lire un fichier et ensuite de le hash en sha256 - - Args: - file_name: le path ou nom du fichier a lire et a hash - print_hash: variable initialiser a false mais si true permet d'afficher le hash - - Returns: - le hash du contenu du fichier +def print_last_block(input_file_path, block_current, time): + print("-"*90) + print("Insertion de {}".format(input_file_path)) + print("Temps de minage : {} secondes\n".format(time)) + print("Numéro du block : {}".format(block_current.block_number[0])) + print("Hash du block précedent : 0x{}".format(block_current.block_prev_hash.hex())) + print("Nonce : {}".format(int.from_bytes(block_current.nonce, byteorder ='big'))) + print("Hash du block courant : 0x{}".format(block_current.block_hash)) + print("-"*90) -""" -def hash_file(input_file_path, print_hash=False): - return hash_data(read_file_blockchain(input_file_path), print_hash) - class blockchain: def __init__(self, input_file_path="", strength=13): @@ -118,12 +114,6 @@ class blockchain: self.first_block = block self.total_block += 1 - def read(self, input_file_path): - read_b = block(input_file_path) - read_b.block_number, read_b.block_prev_hash = read_file_blockchain(input_file_path, mode=2) - read_b.block_hash = hash_file(input_file_path) - return read_b - class block: def __init__(self, file_path, block_number=0, block_prev_hash=0x00, block_obj=None, nonce=0): @@ -134,41 +124,36 @@ class block: self.block_obj = block_obj self.block_hash = "" -def print_block(input_file_path, block_current): - print("Insertion de [", input_file_path, "]\n") - print("Numéro du block : ", block_current.block_number[0]) - print("Hash du block pointé : 0x", block_current.block_prev_hash.hex()) - print("Nonce : ", int.from_bytes(block_current.nonce, byteorder ='big')) - print("Hash du block inséré : 0x", block_current.block_hash) - print("-"*90) - if __name__ == '__main__': if len(sys.argv) > 1: # on test si on a spécifié un nom de fichier à trier input_file_path = sys.argv[1] + + if len(sys.argv) == 2: + nonce_size = NONCE_SIZE + else: + nonce_size = int(sys.argv[2]) + if os.path.exists(input_file_path) == True: if os.path.isdir('blockchain') is not True: os.mkdir('blockchain') - t = time.time() - blockchain = blockchain(input_file_path, NONCE_SIZE) - t = time.time() - t - print("\nTemps d'insertion : ", t) - - input_file_path = os.path.join("blockchain", input_file_path) - first_block_read = blockchain.read(input_file_path) - print_block(input_file_path, blockchain.first_block) + time_exec = time.time() + blockchain = blockchain(input_file_path, nonce_size) + time_exec = time.time() - time_exec + + print_last_block(input_file_path, blockchain.first_block, time_exec) input_file_path = input("\nEntrer un nouveau chemin de fichier à insérer : ") while input_file_path != "exit": if os.path.exists(input_file_path) == True: + block_input = block(input_file_path, blockchain.total_block, int(blockchain.first_block.block_hash, 16), blockchain.first_block) - t = time.time() + + time_exec = time.time() blockchain.insert(block_input) - t = time.time() - t - print("\nTemps d'insertion : ", t) - input_file_path = os.path.join("blockchain", input_file_path) - block_read = blockchain.read(input_file_path) - print_block(input_file_path, blockchain.first_block) + time_exec = time.time() - time_exec + + print_last_block(input_file_path, blockchain.first_block, time_exec) input_file_path = input("\nEntrer un nouveau chemin de fichier à insérer : ") else: print("Le fichier spécifié n'existe pas !") # message affiché si le fichier n'existe pas