Spaces:
Runtime error
Runtime error
| import os | |
| import json | |
| import hashlib | |
| import datetime | |
| from huggingface_hub import (upload_file,HfApi) | |
| token_self = os.environ['HF_TOKEN'] | |
| api = HfApi(token=token_self) | |
| class Blockchain: | |
| def __init__(self,chain_load,load=None,create=None): | |
| global main_chain | |
| main_chain=chain_load | |
| self.pending_transactions = [] | |
| self.trans_data_out = [] | |
| if load == None or load=="": | |
| self.chain = [] | |
| self.create_block(proof=1, previous_hash='0',chain_n=create) | |
| elif load != None and load !="": | |
| #r = requests.get(load) | |
| lod = json.loads(load) | |
| self.chain = lod | |
| def reset(self,create=None): | |
| self.chain = [] | |
| self.pending_transactions = [] | |
| self.create_block(proof=1, previous_hash='0',chain_n=create) | |
| def create_block(self, proof, previous_hash,chain_r=None,chain_n=None): | |
| if chain_r=="" or chain_r==None: | |
| chain_r=f"{main_chain.split('datasets/',1)[1].split('/raw',1)[0]}" | |
| if chain_n !="" and chain_n !=None: | |
| chain_n = f"{main_chain.split('main/',1)[1]}{chain_n}" | |
| if chain_n=="" or chain_n==None: | |
| chain_n=chain_d | |
| block = {'index': len(self.chain) + 1, | |
| 'timestamp': str(datetime.datetime.now()), | |
| 'transactions': self.pending_transactions, | |
| 'proof': proof, | |
| 'previous_hash': previous_hash} | |
| if self.block_valid(block) == True: | |
| self.pending_transactions = [] | |
| self.chain.append(block) | |
| json_object = json.dumps(self.chain, indent=4) | |
| with open("tmp.json", "w") as outfile: | |
| outfile.write(json_object) | |
| try: | |
| api.upload_file( | |
| path_or_fileobj="tmp.json", | |
| path_in_repo=chain_n, | |
| repo_id=chain_r, | |
| token=token_self, | |
| repo_type="dataset", | |
| ) | |
| os.remove("tmp.json") | |
| except Exception as e: | |
| print(e) | |
| pass | |
| return block | |
| else: | |
| block = {"Not Valid"} | |
| print("not Valid") | |
| return block | |
| def print_previous_block(self): | |
| return self.chain[-1] | |
| def new_transaction(self, block): | |
| print (f'block::{block}') | |
| trans_data = { | |
| 'name': block[0]['name'], | |
| 'recipient': block[0]['recipient'], | |
| 'amount': block[0]['amount'], | |
| 'balance': block[0]['balance'], | |
| 'index': block[0]['index'], | |
| 'timestamp': block[0]['timestamp'], | |
| 'proof': block[0]['proof'], | |
| 'previous_hash': block[0]['previous_hash'] | |
| } | |
| self.trans_data_out.append(trans_data) | |
| self.pending_transactions.append(block) | |
| def proof_of_work(self, previous_proof): | |
| new_proof = 1 | |
| check_proof = False | |
| while check_proof is False: | |
| hash_operation = hashlib.sha256( | |
| str(new_proof**2 - previous_proof**2).encode()).hexdigest() | |
| if hash_operation[:5] == '00000': | |
| check_proof = True | |
| else: | |
| new_proof += 1 | |
| return new_proof | |
| def hash(self, block): | |
| encoded_block = json.dumps(block, sort_keys=True).encode() | |
| return hashlib.sha256(encoded_block).hexdigest() | |
| def block_valid(self, block): | |
| print (block) | |
| #prev_block=len(self.chain) | |
| if len(self.chain) > 0: | |
| prev_block = len(self.chain)-1 | |
| previous_block = self.chain[prev_block] | |
| print (previous_block) | |
| out=True | |
| ind=None | |
| mes=None | |
| if block['previous_hash'] != self.hash(previous_block): | |
| out=False | |
| #ind=block_index | |
| mes='Hash' | |
| previous_proof = previous_block['proof'] | |
| proof = block['proof'] | |
| hash_operation = hashlib.sha256( | |
| str(proof**2 - previous_proof**2).encode()).hexdigest() | |
| if hash_operation[:5] != '00000': | |
| out=False | |
| #ind=block_index+1 | |
| mes='Proof' | |
| previous_block = block | |
| else: | |
| out = True | |
| return out | |
| def chain_valid(self, chain): | |
| previous_block = chain[0] | |
| block_index = 1 | |
| out=True | |
| ind=None | |
| mes=None | |
| while block_index < len(chain): | |
| block = chain[block_index] | |
| if block['previous_hash'] != self.hash(previous_block): | |
| out=False | |
| ind=block_index | |
| mes='Hash' | |
| break | |
| previous_proof = previous_block['proof'] | |
| proof = block['proof'] | |
| hash_operation = hashlib.sha256( | |
| str(proof**2 - previous_proof**2).encode()).hexdigest() | |
| if hash_operation[:5] != '00000': | |
| out=False | |
| ind=block_index+1 | |
| mes='Proof' | |
| break | |
| previous_block = block | |
| block_index += 1 | |
| return out, ind, mes |