Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import pandas as pd | |
| import datetime | |
| import hashlib | |
| import json | |
| import os | |
| import requests | |
| from mychain import Blockchain | |
| from mychain import MyChainSend | |
| from mychain import MyChainRec | |
| from mychain import MyChainTrans | |
| import bc_utils | |
| import app_og | |
| from huggingface_hub import (create_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi) | |
| main_chain='https://huggingface.co/datasets/Omnibus/blockchain-sim-2/raw/main/chains/' | |
| main_balance='https://huggingface.co/datasets/Omnibus/blockchain-sim-2/raw/main/balance/' | |
| main_trans='https://huggingface.co/datasets/Omnibus/blockchain-sim-2/raw/main/transact/' | |
| #main_nodes='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/node_file1.json' | |
| #main_pending='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/pending1.json' | |
| token_self = os.environ['HF_TOKEN'] | |
| pa=os.environ['PASS'] | |
| trans_name = 'trans1.json' | |
| #repo_d='Omnibus/static-bin' | |
| chain_d='chain1.json' | |
| #node_file='node_file.json' | |
| #space='blockchain-simulator-dev1' | |
| api = HfApi(token=token_self) | |
| repo = main_balance.split('datasets/',1)[1].split('/raw',1)[0].split('/',1)[0] | |
| name = main_balance.split('datasets/',1)[1].split('/raw',1)[0].split('/',1)[1] | |
| ############################## BLOCKCHAIN END ############################### | |
| def checkp(inp): | |
| if inp == pa: | |
| return gr.update(visible=False), gr.update(visible=True) | |
| elif inp != pa: | |
| return gr.update(visible=True), gr.update(visible=False) | |
| ############################## MYCHAIN START ############################### | |
| def get_my_chain_send(sender_name=None): | |
| global mychain_send | |
| global mychain_trans | |
| mes_error = "" | |
| try: | |
| r = requests.get(f'{main_balance}{sender_name}.json') | |
| #print (f'r={r.text}') | |
| mychain_send = MyChainSend(chain_load=main_balance,load=r.text) | |
| response = {'chain': mychain_send.chain, | |
| 'length': len(mychain_send.chain)} | |
| #print (f'response={response}') | |
| try: | |
| d = requests.get(f'{main_trans}{trans_name}') | |
| mychain_trans = MyChainTrans(chain_load=main_trans,load=d.text) | |
| except Exception as e: | |
| print (f'error1:: {e}') | |
| try: | |
| mychain_trans = MyChainTrans(chain_load=main_trans,create=trans_name) | |
| print ("this worked") | |
| except Exception as ee: | |
| print (f'error2:: {ee}') | |
| pass | |
| try: | |
| response_trans = {'chain': mychain_trans.chain, | |
| 'length': len(mychain_trans.chain)} | |
| print(f'response_trans:: {response_trans}') | |
| except Exception as e: | |
| print (e) | |
| pass | |
| message = f"Blockchain loaded from: {main_balance}{sender_name}.json" | |
| return response,message | |
| except Exception: | |
| message = f"Error loading from: {sender_name}" | |
| print (message) | |
| return ["Error Loading Chain"],message | |
| def get_my_chain_rec(recipient_name=None): | |
| global mychain_rec | |
| try: | |
| r = requests.get(f'{main_balance}{recipient_name}.json') | |
| mychain_rec = MyChainRec(chain_load=main_balance,load=r.text) | |
| response = {'chain': mychain_rec.chain, | |
| 'length': len(mychain_rec.chain)} | |
| message = f"Blockchain loaded from: {main_balance}{recipient_name}.json" | |
| return response,message | |
| except Exception: | |
| try: | |
| mychain_rec = MyChainRec(chain_load=main_balance,create=recipient_name) | |
| response = {'chain': mychain_rec.chain, | |
| 'length': len(mychain_rec.chain)} | |
| message = f"Blockchain loaded from: {main_balance}{recipient_name}.json" | |
| return response,message | |
| except Exception: | |
| message = f"Error loading from: {recipient_name}" | |
| return ["Error Loading Chain"],message | |
| def display_chain_send(): | |
| response = {'chain': mychain_send.chain, | |
| 'length': len(mychain_send.chain)} | |
| return response | |
| def display_chain_rec(): | |
| response = {'chain': mychain_rec.chain, | |
| 'length': len(mychain_rec.chain)} | |
| return response | |
| def valid_send(): | |
| valid,ind,mes = mychain_send.chain_valid(mychain_send.chain) | |
| if valid: | |
| response = 'The Blockchain is valid.' | |
| z=True | |
| else: | |
| response = f'Sender Blockchain is not valid. {mes} at Index {ind}' | |
| z=False | |
| return response,z,mes,ind | |
| def valid_rec(): | |
| valid,ind,mes = mychain_rec.chain_valid(mychain_rec.chain) | |
| if valid: | |
| response = 'The Blockchain is valid.' | |
| z=True | |
| else: | |
| response = f'Blockchain is not valid. {mes} at Index {ind}' | |
| z=False | |
| return response,z, mes, ind | |
| def mychain_mine_block_send(balance, chain_r=None,chain_n=None): | |
| previous_block = mychain_send.print_previous_block() | |
| previous_proof = previous_block['proof'] | |
| proof = mychain_send.proof_of_work(previous_proof) | |
| previous_hash = mychain_send.hash(previous_block) | |
| block = mychain_send.create_block(balance, proof, previous_hash,len(bc_utils.blockchain.chain)+1,chain_r,chain_n) | |
| response = {'message': 'A block is MINED', | |
| 'index': block['index'], | |
| 'timestamp': block['timestamp'], | |
| 'balance': block['balance'], | |
| 'proof': block['proof'], | |
| 'previous_hash': block['previous_hash'] | |
| } | |
| message = "A block is MINED" | |
| show_chain = display_chain_send() | |
| if len(mychain_send.chain) > 1000: | |
| mychain_send.reset() | |
| response = None | |
| show_chain=display_chain_send() | |
| message = "New Chain Created at Max 20 Blocks" | |
| return response, show_chain,message | |
| def mychain_mine_block_trans(balance, chain_r=None,chain_n=None): | |
| previous_block = mychain_trans.print_previous_block() | |
| previous_proof = previous_block['proof'] | |
| proof = mychain_trans.proof_of_work(previous_proof) | |
| previous_hash = mychain_trans.hash(previous_block) | |
| block = mychain_trans.create_block(balance, proof, previous_hash,len(bc_utils.blockchain.chain)+1,chain_r,chain_n) | |
| response = {'message': 'A block is MINED', | |
| 'index': block['index'], | |
| 'timestamp': block['timestamp'], | |
| 'balance': block['balance'], | |
| 'proof': block['proof'], | |
| 'previous_hash': block['previous_hash'] | |
| } | |
| message = "A block is MINED" | |
| show_chain = display_chain_send() | |
| if len(mychain_trans.chain) > 1000: | |
| mychain_trans.reset() | |
| response = None | |
| show_chain=display_chain_send() | |
| message = "New Chain Created at Max 20 Blocks" | |
| return response, show_chain,message | |
| def mychain_mine_block_rec(balance, chain_r=None,chain_n=None): | |
| previous_block = mychain_rec.print_previous_block() | |
| previous_proof = previous_block['proof'] | |
| proof = mychain_rec.proof_of_work(previous_proof) | |
| previous_hash = mychain_rec.hash(previous_block) | |
| block = mychain_rec.create_block(balance, proof, previous_hash,len(bc_utils.blockchain.chain)+1,chain_r,chain_n) | |
| response = {'message': 'A block is MINED', | |
| 'index': block['index'], | |
| 'timestamp': block['timestamp'], | |
| 'balance': block['balance'], | |
| 'proof': block['proof'], | |
| 'previous_hash': block['previous_hash'] | |
| } | |
| message = "A block is MINED" | |
| show_chain = display_chain_rec() | |
| if len(mychain_rec.chain) > 1000: | |
| mychain_rec.reset() | |
| response = None | |
| show_chain=display_chain_rec() | |
| message = "New Chain Created at Max 20 Blocks" | |
| return response, show_chain, message | |
| def merge_trans(): | |
| trans_bx = [] | |
| previous_post_send = mychain_send.print_previous_block() | |
| previous_post_rec = mychain_rec.print_previous_block() | |
| send_block = { | |
| 'role': 'Sender', | |
| 'name': previous_post_send['transactions'][0]['sender'], | |
| 'index': previous_post_send['index'], | |
| 'timestamp': previous_post_send['timestamp'], | |
| 'recipient': previous_post_send['transactions'][0]['recipient'], | |
| 'amount': previous_post_send['transactions'][0]['amount'], | |
| 'balance': previous_post_send['balance'], | |
| 'proof': previous_post_send['proof'], | |
| 'previous_hash': previous_post_send['previous_hash'], | |
| 'block': len(bc_utils.blockchain.chain)+1} | |
| rec_block = { | |
| 'role': 'Recipient', | |
| 'name': previous_post_rec['transactions'][0]['recipient'], | |
| 'index': previous_post_rec['index'], | |
| 'timestamp': previous_post_rec['timestamp'], | |
| 'sender': previous_post_rec['transactions'][0]['sender'], | |
| 'amount': previous_post_rec['transactions'][0]['amount'], | |
| 'balance': previous_post_rec['balance'], | |
| 'proof': previous_post_rec['proof'], | |
| 'previous_hash': previous_post_rec['previous_hash'], | |
| 'block': len(bc_utils.blockchain.chain)+1} | |
| trans_bx.append(send_block) | |
| trans_bx.append(rec_block) | |
| return trans_bx | |
| def issue_tokens(send,rec,amount): | |
| response_send={} | |
| response_rec={} | |
| show_chain_send={} | |
| show_chain_rec={} | |
| data_send=None | |
| data_rec=None | |
| rec_send=None | |
| rec_drop=None | |
| message_send=None | |
| message_rec=None | |
| trans_data=None | |
| trans_mes=None | |
| balance_send = 0 | |
| balance_rec = 0 | |
| mes = "blank message" | |
| try: | |
| b_mes,z=bc_utils.valid() | |
| if z == True: | |
| try: | |
| response,message = get_my_chain_send(send) | |
| #print (f'response:{response}::message{message}') | |
| #r = requests.get(f'{main_balance}{send}.json') | |
| lod=response | |
| #lod = json.loads(response) | |
| #print (lod) | |
| p=True | |
| except Exception as e: | |
| lod=[] | |
| p=False | |
| mes = f"Sender has no wallet {e}" | |
| pass | |
| mes1,val,mes2,ind = valid_send() | |
| print (f'val:: {val}') | |
| if val == False: | |
| p=False | |
| mes = f"Sender: {send} Blockchain is not valid. {mes2} at Index {ind}" | |
| #mes = "Blockchain is not valid." | |
| #if val == True: | |
| if p==True: | |
| try: | |
| balance = lod["chain"][-1]["balance"] | |
| except Exception: | |
| balance = 0 | |
| p=False | |
| #print (balance) | |
| balance_send =int(balance)-int(amount) | |
| if balance_send >=0: | |
| p=True | |
| response,message = get_my_chain_rec(rec) | |
| lod_rec = response | |
| mes1,val,mes2,ind = valid_rec() | |
| print (f'val:: {val}') | |
| if val == False: | |
| p=False | |
| mes = f"Recipient: {rec} Blockchain is not valid. {mes2} at Index {ind}" | |
| #mes = "Blockchain is not valid." | |
| #print (lod_rec) | |
| if val == True: | |
| try: | |
| balance = lod_rec["chain"][-1]["balance"] | |
| #print (balance) | |
| except Exception: | |
| p=False | |
| balance = 0 | |
| balance_rec =int(balance)+int(amount) | |
| if balance_send < 0: | |
| mes ="Not enough tokens" | |
| p = False | |
| print(mes) | |
| if z==False: | |
| mes = b_mes | |
| p=False | |
| except Exception as e: | |
| mes = f"Blockchain not loaded? {e}" | |
| p=False | |
| print (mes) | |
| if p==False: | |
| return (mes, p,None,None,None,None,None,None,None,None,None,None,None) | |
| #return (mes, p,{},{},{},{},{},{},None,None,None,None,{}) | |
| #json, json, json, json, dataframe, dataframe, textbox, textbox, dropdown, dropdown, dataframe | |
| if p==True: | |
| mychain_send.new_transaction(f"{send}",f"{rec}",f"{amount}",f"{balance_send}") | |
| mychain_trans.new_transaction(f"{send}",f"{rec}",f"{amount}",f"{balance_send}") | |
| message_send = "Transaction Added to Pool" | |
| data_send = pd.DataFrame(mychain_send.pending_transactions) | |
| mychain_rec.new_transaction(f"{send}",f"{rec}",f"{amount}",f"{balance_rec}") | |
| message_rec = "Transaction Added to Pool" | |
| data_rec = pd.DataFrame(mychain_rec.pending_transactions) | |
| response_send, show_chain_send, message_send = mychain_mine_block_send(balance_send, chain_r=None,chain_n=send) | |
| mychain_mine_block_trans(balance_send, chain_r=None,chain_n=send) | |
| response_rec, show_chain_rec, message_rec = mychain_mine_block_rec(balance_rec, chain_r=None,chain_n=rec) | |
| mes = (f'Send: {message_send} :: Recieve: {message_rec}') | |
| _,rec_send=update_send_list() | |
| _,rec_drop=update_rec_list() | |
| trans_bx = merge_trans() | |
| trans_data, mes = bc_utils.bc_transactions(trans_bx) | |
| return (mes, p, response_send, response_rec, show_chain_send, show_chain_rec, data_send, data_rec, message_send, message_rec, rec_send, rec_drop,trans_data) | |
| #global send_list | |
| #global rec_list | |
| def create_new_chain(address): | |
| address = str(address.strip("b").strip("'")) | |
| mychain_rec = MyChainRec(chain_load=main_balance,create=address) | |
| response = {'chain': mychain_rec.chain, | |
| 'length': len(mychain_rec.chain)} | |
| message = f"Blockchain loaded from: {main_balance}{address}.json" | |
| send_list,send_drop = update_send_list() | |
| rec_list, rec_drop = update_rec_list() | |
| return response,message,send_drop,rec_drop | |
| ############################## MYCHAIN END ############################### | |
| def res_source(): | |
| block = {'index': 1, | |
| 'timestamp': str(datetime.datetime.now()), | |
| 'transactions': [], | |
| 'balance': 10000000000000000000, | |
| 'proof': 1, | |
| 'previous_hash': 0} | |
| pending_transactions_x = [] | |
| pending_transactions_x.append(block) | |
| json_object = json.dumps(pending_transactions_x, indent=4) | |
| with open("tmp_send.json", "w") as outfile: | |
| outfile.write(json_object) | |
| try: | |
| api.upload_file( | |
| path_or_fileobj="tmp_send.json", | |
| path_in_repo="balance/__Source__.json", | |
| repo_id=main_balance.split('datasets/',1)[1].split('/raw',1)[0], | |
| token=token_self, | |
| repo_type="dataset", | |
| ) | |
| os.remove("tmp_send.json") | |
| return "__Source__ Reset" | |
| except Exception as e: | |
| return e | |
| def update_send_list(): | |
| f_ist = (api.list_repo_files(repo_id=f'{repo}/{name}', repo_type="dataset")) | |
| send_list =[] | |
| for i,ea in enumerate(f_ist): | |
| if "balance/" in ea: | |
| try: | |
| send_list.append(ea.split("/",1)[1].split(".",1)[0]) | |
| except Exception: | |
| pass | |
| return send_list, gr.Dropdown.update(label="Sender", choices=[f for f in send_list]) | |
| send_list,send_drop = update_send_list() | |
| def update_rec_list(): | |
| f_ist = (api.list_repo_files(repo_id=f'{repo}/{name}', repo_type="dataset")) | |
| rec_list =[] | |
| for i,ea in enumerate(f_ist): | |
| if "balance/" in ea: | |
| try: | |
| if not "__Source__" in ea: | |
| rec_list.append(ea.split("/",1)[1].split(".",1)[0]) | |
| except Exception: | |
| pass | |
| return rec_list, gr.Dropdown.update(label="Recipient", choices=[f for f in rec_list]) | |
| rec_list, rec_drop = update_rec_list() | |
| with gr.Blocks() as bc: | |
| with gr.Row(visible=True) as invalid: | |
| pass_box = gr.Textbox() | |
| pass_btn = gr.Button() | |
| with gr.Box(visible=False) as valida: | |
| gr.Markdown("""<h1><center>Blockchain Simulator<br><h3>(Transactions have no value)<br><h4>Chain will reset at 20 blocks""") | |
| #blockchain = gr.State() | |
| with gr.Row(): | |
| with gr.Tab("OG"): | |
| with gr.Row(): | |
| with gr.Column(): | |
| with gr.Accordion(label="Load",open=False): | |
| with gr.Row(): | |
| chain_repo=gr.Textbox(label="repo/name") | |
| chain_n=gr.Textbox(label="Chain file") | |
| with gr.Row(): | |
| in_chain_btn=gr.Button("Load Chain") | |
| create_bc = gr.Button("Create New Blockchain") | |
| #send=gr.Textbox(label="Sender") | |
| send = gr.Dropdown(label="Sender", choices=[f for f in send_list], value = "Bank") | |
| rec=gr.Dropdown(label="Recipient", choices=[f for f in rec_list], allow_custom_value=True) | |
| am=gr.Textbox(label="Amount") | |
| send_trans=gr.Button("Post Transaction") | |
| mine_b = gr.Button("Mine Block") | |
| check = gr.Button("Check Chain") | |
| check_trans = gr.Button("Check Transactions") | |
| check_all = gr.Button("Check All") | |
| with gr.Column(): | |
| block_text = gr.Textbox() | |
| trans_data = gr.Dataframe() | |
| json_out = gr.JSON() | |
| chain_json = gr.JSON() | |
| with gr.Accordion("Nodes", open=False): | |
| with gr.Row(): | |
| this_space=gr.Textbox(label="This Repo/Space") | |
| with gr.Row(): | |
| node_repo=gr.Textbox(label="Node Repo") | |
| node_space=gr.Textbox(label="Node Space") | |
| node_file=gr.Textbox(label="Node File") | |
| node_add=gr.Button("Add Node") | |
| with gr.Accordion("Tokens", open=False): | |
| with gr.Row(): | |
| with gr.Column(): | |
| issue_btn=gr.Button() | |
| with gr.Column(): | |
| with gr.Row(): | |
| reset_blockchain=gr.Button("Reset Main Blockchain") | |
| reset_source=gr.Button("Reset __Source__") | |
| with gr.Row(): | |
| reset_sender=gr.Button("Reset Sender") | |
| reset_recieve=gr.Button("Reset Recipient") | |
| out_box_bool=gr.Textbox() | |
| with gr.Row(): | |
| with gr.Column(): | |
| block_text_send = gr.Textbox() | |
| trans_data_send = gr.Dataframe() | |
| json_out_send = gr.JSON() | |
| chain_json_send = gr.JSON() | |
| with gr.Column(): | |
| block_text_rec = gr.Textbox() | |
| trans_data_rec = gr.Dataframe() | |
| json_out_rec = gr.JSON() | |
| chain_json_rec = gr.JSON() | |
| with gr.Tab("BC"): | |
| with gr.Row(): | |
| with gr.Tab("Gen Wal"): | |
| gen_wal_btn=gr.Button() | |
| seed = gr.Textbox(label='Seed Phrase') | |
| img1=gr.Pil(label='Private Key') | |
| out1 = gr.Textbox(label='Private Key',max_lines=4) | |
| img2=gr.Pil(label='Public Key') | |
| out2 = gr.Textbox(label='Public Key',max_lines=4) | |
| img3=gr.Pil(label='Address') | |
| out3 = gr.Textbox(label='Address') | |
| with gr.Tab("Encrypt"): | |
| rsa_to_enc = gr.Textbox(label="txt to encrypt") | |
| pub_key_in = gr.Image(label="Public Key", type="filepath") | |
| priv_key_in1 = gr.Image(label="Private Key(sig)", type="filepath") | |
| rsa_enc_btn = gr.Button("RSA Encrypt") | |
| rsa_enc_mes = gr.Textbox(label="encoded", max_lines=4) | |
| qr_enc_mes = gr.Image(type="filepath") | |
| with gr.Tab("Decrypt"): | |
| mes_in = gr.Image(label="Message", type="filepath") | |
| priv_key_in = gr.Image(label="Private Key", type="filepath") | |
| rsa_dec_btn = gr.Button("RSA Decrypt") | |
| rsa_dec_mes = gr.Textbox(label="decoded") | |
| def res_bc(): | |
| bc_utils.blockchain.reset(create="chain1.json") | |
| return ("Main Blockchain Reset") | |
| def res_send(send): | |
| mychain_send = MyChainSend(chain_load=main_balance,create=send) | |
| return ("Sender Blockchain Reset") | |
| def deep_trans(): | |
| chain1=mychain_send.chain | |
| chain2=bc_utils.blockchain.chain | |
| valid,ind,mes = mychain_send.deep_valid_send(chain1,chain2) | |
| if valid: | |
| response = 'The Blockchain is valid.' | |
| z=True | |
| else: | |
| response = f'Blockchain is not valid. {mes} at Index {ind}' | |
| z=False | |
| return response,z | |
| def mine_block(chain_r=None,chain_n=None): | |
| try: | |
| a,b,c,d = bc_utils.mine_block(chain_r=None,chain_n=None) | |
| mychain_trans.reset(create=trans_name) | |
| return a,b,c,d | |
| except Exception as e: | |
| print (e) | |
| return e,None,None,e | |
| gen_wal_btn.click(app_og.generate_keys,None,[out2,out1, img3,out3,img1,img2]).then(create_new_chain,out3,[json_out,block_text,send,rec]).then(app_og.test_fn,[img1,img2],[priv_key_in,pub_key_in,priv_key_in1]) | |
| rsa_enc_btn.click(app_og.encrypt_text,[rsa_to_enc,pub_key_in,priv_key_in1,out3],[rsa_enc_mes,qr_enc_mes]).then(app_og.test_fn2,qr_enc_mes,mes_in) | |
| rsa_dec_btn.click(app_og.decrypt_text,[mes_in,priv_key_in],rsa_dec_mes) | |
| check_trans.click(deep_trans,None,[block_text,out_box_bool]) | |
| reset_sender.click(res_send,send,block_text) | |
| reset_blockchain.click(res_bc,None,block_text) | |
| reset_source.click(res_source,None,block_text) | |
| send_trans.click(issue_tokens,[send,rec,am],[block_text,out_box_bool,json_out_send,json_out_rec,chain_json_send,chain_json_rec,trans_data_send,trans_data_rec,block_text_send,block_text_rec,send,rec,trans_data]) | |
| issue_btn.click(issue_tokens,[send,rec,am],[block_text,out_box_bool,json_out_send,json_out_rec,chain_json_send,chain_json_rec,trans_data_send,trans_data_rec,block_text_send,block_text_rec,send,rec,trans_data]) | |
| #issue_btn.click(issue_tokens,[send,rec,am],[block_text,out_box_bool,json_out_send]) | |
| node_add.click(bc_utils.add_node,[this_space,node_repo,node_space,node_file],block_text) | |
| pass_btn.click(checkp,pass_box,[invalid,valida]) | |
| in_chain_btn.click(bc_utils.get_chain,[chain_repo,chain_n],[chain_json,block_text]) | |
| create_bc.click(bc_utils.create_chain,[chain_n],[block_text,json_out,chain_json]) | |
| check.click(bc_utils.valid,None,[block_text,out_box_bool]) | |
| #check_all.click(sort_valid,None,block_text) | |
| #send_trans.click(bc_transactions,[send,rec,am],[trans_data,block_text,send,rec,am]) | |
| mine_b.click(mine_block,[chain_repo,chain_n],[json_out,chain_json,trans_data,block_text]) | |
| bc.launch(enable_queue=False) | |