Spaces:
Runtime error
Runtime error
File size: 20,111 Bytes
29346dc c97b875 c32b01b 9d52a5f 29346dc 3e65c38 29346dc 9053d54 29346dc 3f18a78 2a275bb 3f18a78 29346dc 2a275bb 29346dc 0bda456 29346dc ce5a7f4 29346dc f1a6447 29346dc 0bda456 29346dc 9053d54 e02f6b7 9053d54 d81bf07 a35b37d 9053d54 a35b37d 59a2d12 9053d54 a35b37d 9a3e5c4 9d52a5f c3ba4ee 9a3e5c4 a35b37d 9053d54 47fbf25 9053d54 a35b37d 59a2d12 a35b37d 9d52a5f c3ba4ee a35b37d 47fbf25 a35b37d 9d52a5f 50fa8e8 a35b37d 5664d22 a35b37d 3aa143c a35b37d 11d746f a35b37d 079a5c5 55e7ac1 a35b37d d4d1e89 a35b37d d4d1e89 45af585 9053d54 a35b37d 9053d54 5cb3881 a35b37d 11d746f a35b37d 079a5c5 a35b37d 45af585 3aa143c 55e7ac1 4bfbc30 55e7ac1 4bfbc30 55e7ac1 4bfbc30 55e7ac1 4bfbc30 55e7ac1 9053d54 55e7ac1 29346dc 8d86aa6 e9aeeaa a35b37d 29346dc a35b37d 29346dc a35b37d 5664d22 9053d54 c3ba4ee 9a3e5c4 29346dc 5664d22 29346dc 5664d22 29346dc 332a23b a1461fb 332a23b cbf8eb8 332a23b 50fa8e8 a35b37d 50c1d79 5222d9d a35b37d 5222d9d ad9c747 50fa8e8 402a95b 50fa8e8 ad9c747 a35b37d 2047384 29346dc 5222d9d 29346dc e9aeeaa a35b37d bea69aa a35b37d 28aa97f bea69aa a35b37d 28aa97f 45af585 a35b37d ee49ee4 d1cf408 ee49ee4 9053d54 a98591b 1d0bfef a98591b 2cd5c0c a98591b 7ac7894 183b8e0 8e9df5c ee49ee4 94e11a4 1a6c4e8 ee49ee4 cd033af 29346dc a052075 29346dc e02f6b7 29346dc a35b37d a2d0d5a a35b37d 44fbe19 a35b37d a98591b ee49ee4 c5fb96d 29346dc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 |
import gradio as gr
import pandas as pd
import datetime
import hashlib
import json
import os
import requests
from blockchain import Blockchain
from mychain import MyChainSend
from mychain import MyChainRec
from huggingface_hub import (create_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi)
main_chain='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/chains/'
main_balance='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/balance/'
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']
repo_d='Omnibus/static-bin'
chain_d='chain1.json'
node_file='node_file.json'
space='blockchain-simulator-dev1'
api = HfApi(token=token_self)
######################## BLOCKCHAIN START ###############################
def bc_transactions(block):
#mes, out = issue_tokens(sender,recipient,amount)
try:
blockchain.new_transaction(block)
message = "Transaction Added to Pool"
data = pd.DataFrame(blockchain.pending_transactions)
except Exception as e:
message = e
data = None
return data,message,None,None,None
def create_chain(create=None):
global blockchain
blockchain = Blockchain(chain_load=main_chain,create=create)
#blockchain.reset(create=create)
return "New Chain Created",None,display_chain()
def display_chain():
response = {'chain': blockchain.chain,
'length': len(blockchain.chain)}
return response
def mine_block(chain_r=None,chain_n=None):
if chain_n=="":
chain_n = chain_d
previous_block = blockchain.print_previous_block()
previous_proof = previous_block['proof']
proof = blockchain.proof_of_work(previous_proof)
previous_hash = blockchain.hash(previous_block)
block = blockchain.create_block(proof, previous_hash,chain_r,chain_n)
response = {'message': 'A block is MINED',
'index': block['index'],
'timestamp': block['timestamp'],
'proof': block['proof'],
'previous_hash': block['previous_hash']}
message = "A block is MINED"
show_chain = display_chain()
if len(blockchain.chain) > 1000:
blockchain.reset()
response = None
show_chain=display_chain()
message = "New Chain Created at Max 20 Blocks"
return response, show_chain,pd.DataFrame(blockchain.pending_transactions), message
def sort_valid():
#nodes=main_nodes.replace("'","")
f = requests.get(f'{main_nodes}')
t = open('tmp.json','w')
t.write(f.text)
t.close()
f = open('tmp.json','r')
f = f.read()
j = json.loads(f)
#j=f.json()
print (len(j))
cnt=0
valid_chains=[]
not_valid=[]
response=''
while cnt < len(j):
try:
r = requests.get(f'{j[cnt]["url"]}')
g = open('tmp1.json','w')
g.write(r.text)
g.close()
gg = open('tmp1.json','r')
ggg=gg.read()
print (ggg)
leng=len(json.loads(ggg))
print(f'Blockchain {cnt}: Length {leng} ')
valid,ind,mes = blockchain.chain_valid(json.loads(ggg))
except Exception:
valid=False
leng="No Data"
if valid:
valid_chains.append({"Chain": cnt, "Length": leng})
response = f'{response} Valid: {cnt}'
else:
not_valid.append({"Chain": cnt, "Length": leng})
response = f'{response} Invalid:{cnt}'
per = ((len(valid_chains)+1)/(len(j)+1))*100
cnt+=1
response=f'{int(per)}%-{response}'
print (f'Valid: {valid_chains}')
print (f'Not Valid: {not_valid}')
#p = json.loads(str(valid_chains))
p = valid_chains
cnt2=0
bot=0
out=[]
while cnt2 < len(p):
if p[cnt2]['Length'] > bot:
bot = p[cnt2]['Length']
out = [cnt2,bot]
else:
pass
cnt2+=1
print (out)
return response
def valid():
valid,ind,mes = blockchain.chain_valid(blockchain.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
def get_chain(repo_name=None,chain_name=None,token=None):
if repo_name == "":
repo_name = repo_d
if chain_name=="":
chain_name = chain_d
try:
r = requests.get(f'{main_chain}{chain_name}')
#create_chain(load=r.text)
global blockchain
blockchain = Blockchain(chain_load=main_chain,load=r.text)
response = {'chain': blockchain.chain,
'length': len(blockchain.chain)}
message = f"Blockchain loaded from: {main_chain}{chain_name}"
return response,message
except:
message = f"Error loading from: {src}"
return ["Error Loading Chain"],message
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)
def add_node(this_space,repo,space,chain_file):
#print(f"{api.whoami(['name'])}")
#repo = f'omnibus/{space}'
is_valid='True'
r = requests.get(f'{main_nodes}')
try:
lod = json.loads(r.text)
except:
lod=[]
pass
block = {'index': len(lod) + 1,
'timestamp': str(datetime.datetime.now()),
'url': f'https://huggingface.co/datasets/{repo}/{space}/raw/main/{chain_file}',
'valid': f'{is_valid}'}
lod.append(block)
json_object = json.dumps(lod, indent=4)
with open("tmp1.json", "w") as outfile:
outfile.write(json_object)
try:
api.upload_file(
path_or_fileobj="tmp1.json",
path_in_repo=main_nodes.split('main/',1)[1],
repo_id=main_nodes.split('datasets/',1)[1].split('/raw',1)[0],
token=token_self,
repo_type="dataset",
)
os.remove("tmp1.json")
except Exception as e:
pass
#api = HfApi(token=token)
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 ###############################
############################## MYCHAIN START ###############################
def get_my_chain_send(sender_name=None):
global mychain_send
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}')
message = f"Blockchain loaded from: {main_balance}{sender_name}.json"
return response,message
except Exception:
message = f"Error loading from: {sender_name}"
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 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,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_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,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']}
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['previous_hash']}
trans_bx.append(send_block)
trans_bx.append(rec_block)
bc_transactions(trans_bx)
def issue_tokens(send,rec,amount):
response_send={}
response_rec={}
show_chain_send={}
show_chain_rec={}
data_send=None
data_rec=None
message_send=None
message_rec=None
#print(f"CURRENT USER:: {api.whoami(['name'])}")
#repo = f'omnibus/{space}'
#is_valid='True'
balance_send = 0
balance_rec = 0
try:
_,z=valid()
if z == True:
mes = None
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
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
print (lod_rec)
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
if z==False:
mes = "Invalid Blockchain"
p=False
except Exception as e:
mes = f"Blockchain not loaded? {e}"
p=False
if p==True:
mychain_send.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)
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()
merge_trans()
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)
############################## 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], value = "Bank")
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.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_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():
issue_btn=gr.Button()
reset_source=gr.Button("Reset")
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()
reset_source.click(res_source,None,block_text)
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])
#issue_btn.click(issue_tokens,[send,rec,am],[block_text,out_box_bool,json_out_send])
node_add.click(add_node,[this_space,node_repo,node_space,node_file],block_text)
pass_btn.click(checkp,pass_box,[invalid,valida])
in_chain_btn.click(get_chain,[chain_repo,chain_n],[chain_json,block_text])
create_bc.click(create_chain,[chain_n],[block_text,json_out,chain_json])
check.click(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)
|