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) | |
chain_d="chain1.json" | |
chain_t="trans1.json" | |
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=f"{main_chain.split('main/',1)[1]}{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.trans_data_out =[] | |
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) | |
outfile.close() | |
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") | |
#MyChainTrans.reset(self,create=chain_n) | |
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 | |
class MyChainSend: | |
def __init__(self,chain_load,load=None,create=None): | |
global main_chain_send | |
main_chain_send=chain_load | |
self.pending_transactions = [] | |
if load == None or load=="": | |
self.chain = [] | |
self.create_block(balance=0,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, balance, proof, previous_hash,prev_block=None,chain_r=None,chain_n=None): | |
if chain_r=="" or chain_r==None: | |
chain_r=f"{main_chain_send.split('datasets/',1)[1].split('/raw',1)[0]}" | |
if chain_n !="" and chain_n !=None: | |
chain_n = f"{main_chain_send.split('main/',1)[1]}{chain_n}.json" | |
if chain_n=="" or chain_n==None: | |
chain_n="error_send.json" | |
#prev_block = len(blockchain.chain)-1 | |
#previous_block = blockchain.chain[prev_block] | |
block = {'index': len(self.chain) + 1, | |
'timestamp': str(datetime.datetime.now()), | |
'block': prev_block, | |
'transactions': self.pending_transactions, | |
'balance': balance, | |
'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_send.json", "w") as outfile: | |
outfile.write(json_object) | |
outfile.close() | |
try: | |
api.upload_file( | |
path_or_fileobj="tmp_send.json", | |
path_in_repo=chain_n, | |
repo_id=chain_r, | |
token=token_self, | |
repo_type="dataset", | |
) | |
os.remove("tmp_send.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, sender, recipient, amount, balance): | |
transaction = { | |
'sender': sender, | |
'recipient': recipient, | |
'amount': amount, | |
'balance': balance | |
} | |
self.pending_transactions.append(transaction) | |
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 | |
def deep_valid_send(self, chain1,chain2): | |
block_index = 1 | |
out=True | |
ind=None | |
mes=None | |
while block_index < len(chain1): | |
block = chain1[block_index] | |
block_ind = block['block'] | |
block2 = chain2[block_ind] | |
check1 = { | |
'timestamp':block['timestamp'], | |
'recipient':block['transactions']['recipient'], | |
'amount':block['transactions']['amount'], | |
'balance':block['transactions']['balance'], | |
'balance2':block['balance'], | |
'proof':block['proof'], | |
'previous_hash':block['previous_hash'] | |
} | |
zz=1 | |
while zz < len(block2[transactions]): | |
check2 = { | |
'timestamp':block2['timestamp'], | |
'sender':block2['transactions'][zz][0]['name'], | |
'recipient':block2['transactions'][zz][0]['recipient'], | |
'amount':block2['transactions'][zz][0]['amount'], | |
'balance':block2['transactions'][zz][0]['balance'], | |
'balance2':block2['transactions'][zz][0]['balance'], | |
'proof':block2['proof'], | |
'previous_hash':block2['previous_hash'] | |
} | |
zz+=1 | |
if self.hash(check1) != self.hash(check2): | |
out=False | |
ind=block_index | |
mes='Hash' | |
break | |
if out == False: | |
break | |
block_index += 1 | |
return out, ind, mes | |
class MyChainRec: | |
def __init__(self,chain_load, load=None,create=None): | |
global main_chain_rec | |
main_chain_rec=chain_load | |
self.pending_transactions = [] | |
if load == None or load=="": | |
self.chain = [] | |
self.create_block(balance=0, proof=1, previous_hash='0',chain_n=create) | |
if 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, balance, proof, previous_hash, prev_block=None,chain_r=None,chain_n=None): | |
if chain_r=="" or chain_r==None: | |
chain_r=f"{main_chain_rec.split('datasets/',1)[1].split('/raw',1)[0]}" | |
if chain_n !="" and chain_n !=None: | |
chain_n = f"{main_chain_rec.split('main/',1)[1]}{chain_n}.json" | |
if chain_n=="" or chain_n==None: | |
chain_n="error_rec.json" | |
#prev_block = len(blockchain.chain)-1 | |
#previous_block = blockchain.chain[prev_block] | |
block = {'index': len(self.chain) + 1, | |
'timestamp': str(datetime.datetime.now()), | |
'block': prev_block, | |
'transactions': self.pending_transactions, | |
'balance': balance, | |
'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_rec.json", "w") as outfile: | |
outfile.write(json_object) | |
outfile.close() | |
try: | |
api.upload_file( | |
path_or_fileobj="tmp_rec.json", | |
path_in_repo=chain_n, | |
repo_id=chain_r, | |
token=token_self, | |
repo_type="dataset", | |
) | |
os.remove("tmp_rec.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, sender, recipient, amount, balance): | |
transaction = { | |
'sender': sender, | |
'recipient': recipient, | |
'amount': amount, | |
'balance': balance | |
} | |
self.pending_transactions.append(transaction) | |
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 | |
class MyChainTrans: | |
def __init__(self,chain_load,load=None,create=None): | |
global main_chain_trans | |
main_chain_trans=chain_load | |
self.pending_transactions = [] | |
if load == None or load=="": | |
self.chain = [] | |
self.create_block(balance=0,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(balance=0, proof=1, previous_hash='0',chain_n=create) | |
def create_block(self,balance, proof, previous_hash,prev_block=None,chain_r=None,chain_n=None): | |
if chain_r=="" or chain_r==None: | |
chain_r=f"{main_chain_trans.split('datasets/',1)[1].split('/raw',1)[0]}" | |
if chain_n !="" and chain_n !=None: | |
chain_n = f"{main_chain_trans.split('main/',1)[1]}{chain_t}" | |
if chain_n=="" or chain_n==None: | |
chain_n="error_send.json" | |
#prev_block = len(blockchain.chain)-1 | |
#previous_block = blockchain.chain[prev_block] | |
block = {'index': len(self.chain) + 1, | |
'timestamp': str(datetime.datetime.now()), | |
#'block': len(Blockchain.chain)+1, | |
'transactions': self.pending_transactions, | |
'balance': balance, | |
'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_trans.json", "w") as outfile: | |
outfile.write(json_object) | |
outfile.close() | |
try: | |
api.upload_file( | |
path_or_fileobj="tmp_trans.json", | |
path_in_repo=chain_n, | |
repo_id=chain_r, | |
token=token_self, | |
repo_type="dataset", | |
) | |
os.remove("tmp_trans.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, sender, recipient, amount, balance): | |
transaction = { | |
'sender': sender, | |
'recipient': recipient, | |
'amount': amount, | |
'balance': balance | |
} | |
self.pending_transactions.append(transaction) | |
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 |