Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -399,45 +399,131 @@ for i,ea in enumerate(f_ist):
|
|
399 |
|
400 |
############################## BLOCKCHAIN END ###############################
|
401 |
############################## MYCHAIN START ###############################
|
402 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
403 |
|
404 |
-
def
|
405 |
try:
|
406 |
-
r = requests.get(f'{main_balance}{
|
407 |
-
global
|
408 |
-
|
409 |
-
response = {'chain':
|
410 |
-
'length': len(
|
411 |
-
message = f"Blockchain loaded from: {main_balance}{
|
412 |
return response,message
|
413 |
except:
|
414 |
message = f"Error loading from: {sender_name}"
|
415 |
return ["Error Loading Chain"],message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
416 |
|
417 |
-
def
|
418 |
-
mes, out = issue_tokens(sender,recipient,amount)
|
419 |
if out == True:
|
420 |
-
|
421 |
message = "Transaction Added to Pool"
|
422 |
-
data = pd.DataFrame(
|
423 |
if out == False:
|
424 |
message = mes
|
425 |
data = None
|
426 |
return data,message,None,None,None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
427 |
|
|
|
|
|
|
|
|
|
|
|
|
|
428 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
429 |
|
430 |
|
431 |
def issue_tokens(send,rec,amount):
|
432 |
-
print(f"CURRENT USER:: {api.whoami(['name'])}")
|
433 |
#repo = f'omnibus/{space}'
|
434 |
#is_valid='True'
|
|
|
|
|
|
|
435 |
try:
|
436 |
_,z=valid()
|
437 |
if z == True:
|
438 |
mes = None
|
439 |
try:
|
440 |
-
response,message =
|
441 |
#r = requests.get(f'{main_balance}{send}.json')
|
442 |
lod = json.loads(response)
|
443 |
p=True
|
@@ -449,74 +535,18 @@ def issue_tokens(send,rec,amount):
|
|
449 |
if p==True:
|
450 |
balance = lod["chain"][-1]["balance"]
|
451 |
print (balance)
|
452 |
-
|
453 |
if balance >=0:
|
454 |
p==True
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
|
462 |
-
|
463 |
-
|
464 |
-
lod.append(block)
|
465 |
-
|
466 |
-
json_object = json.dumps(lod, indent=4)
|
467 |
-
with open("tmp1.json", "w") as outfile:
|
468 |
-
outfile.write(json_object)
|
469 |
-
try:
|
470 |
-
api.upload_file(
|
471 |
-
path_or_fileobj="tmp1.json",
|
472 |
-
path_in_repo=f'{main_balance.split("main/",1)[1]}/{send}.json',
|
473 |
-
repo_id=main_balance.split('datasets/',1)[1].split('/raw',1)[0],
|
474 |
-
token=token_self,
|
475 |
-
repo_type="dataset",
|
476 |
-
)
|
477 |
-
os.remove("tmp1.json")
|
478 |
-
except Exception as e:
|
479 |
-
mes=e
|
480 |
-
pass
|
481 |
-
|
482 |
-
try:
|
483 |
-
r = requests.get(f'{main_balance}{rec}.json')
|
484 |
-
lod = json.loads(r.text)
|
485 |
-
balance = lod[-1]["balance"]
|
486 |
-
|
487 |
-
except:
|
488 |
-
lod=[]
|
489 |
-
balance=0
|
490 |
-
pass
|
491 |
-
print (balance)
|
492 |
-
balance =int(balance)+int(amount)
|
493 |
-
block = {'index': len(lod) + 1,
|
494 |
-
'timestamp': str(datetime.datetime.now()),
|
495 |
-
'sender': f'{send}',
|
496 |
-
'recipient':f'{rec}',
|
497 |
-
'amount': f'{amount}',
|
498 |
-
'balance': f'{balance}'}
|
499 |
-
lod.append(block)
|
500 |
-
|
501 |
-
json_object = json.dumps(lod, indent=4)
|
502 |
-
with open("tmp1.json", "w") as outfile:
|
503 |
-
outfile.write(json_object)
|
504 |
-
try:
|
505 |
-
api.upload_file(
|
506 |
-
path_or_fileobj="tmp1.json",
|
507 |
-
path_in_repo=f'{main_balance.split("main/",1)[1]}/{rec}.json',
|
508 |
-
repo_id=main_balance.split('datasets/',1)[1].split('/raw',1)[0],
|
509 |
-
token=token_self,
|
510 |
-
repo_type="dataset",
|
511 |
-
)
|
512 |
-
os.remove("tmp1.json")
|
513 |
-
|
514 |
-
except Exception as e:
|
515 |
-
mes = e
|
516 |
-
pass
|
517 |
-
if mes == None:
|
518 |
-
mes = f'{amount} sent from {send} to {rec}'
|
519 |
-
'''
|
520 |
if balance < 0:
|
521 |
mes ="Not enough tokens"
|
522 |
p = False
|
@@ -526,7 +556,17 @@ def issue_tokens(send,rec,amount):
|
|
526 |
except Exception:
|
527 |
mes = "Blockchain not loaded?"
|
528 |
p=False
|
529 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
530 |
|
531 |
|
532 |
############################## MYCHAIN END ###############################
|
@@ -575,10 +615,21 @@ with gr.Blocks() as bc:
|
|
575 |
node_file=gr.Textbox(label="Node File")
|
576 |
node_add=gr.Button("Add Node")
|
577 |
with gr.Accordion("Tokens", open=False):
|
|
|
578 |
issue_btn=gr.Button()
|
579 |
out_box_bool=gr.Textbox()
|
580 |
-
|
581 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
582 |
|
583 |
node_add.click(add_node,[this_space,node_repo,node_space,node_file],block_text)
|
584 |
pass_btn.click(checkp,pass_box,[invalid,valida])
|
|
|
399 |
|
400 |
############################## BLOCKCHAIN END ###############################
|
401 |
############################## MYCHAIN START ###############################
|
402 |
+
def display_chain_send():
|
403 |
+
response = {'chain': mychain_send.chain,
|
404 |
+
'length': len(mychain_send.chain)}
|
405 |
+
return response
|
406 |
+
|
407 |
+
def display_chain_rec():
|
408 |
+
response = {'chain': mychain_rec.chain,
|
409 |
+
'length': len(mychain_rec.chain)}
|
410 |
+
return response
|
411 |
+
|
412 |
|
413 |
+
def get_my_chain_send(sender_name=None):
|
414 |
try:
|
415 |
+
r = requests.get(f'{main_balance}{sender_name}.json')
|
416 |
+
global mychain_send
|
417 |
+
mychain_send = MyChain(chain_load=main_balance,load=r.text)
|
418 |
+
response = {'chain': mychain_send.chain,
|
419 |
+
'length': len(mychain_send.chain)}
|
420 |
+
message = f"Blockchain loaded from: {main_balance}{sender_name}.json"
|
421 |
return response,message
|
422 |
except:
|
423 |
message = f"Error loading from: {sender_name}"
|
424 |
return ["Error Loading Chain"],message
|
425 |
+
|
426 |
+
def get_my_chain_rec(recipient_name=None):
|
427 |
+
try:
|
428 |
+
r = requests.get(f'{main_balance}{recipient_name}.json')
|
429 |
+
global mychain_rec
|
430 |
+
mychain_rec = MyChain(chain_load=main_balance,load=r.text)
|
431 |
+
response = {'chain': mychain_rec.chain,
|
432 |
+
'length': len(mychain_rec.chain)}
|
433 |
+
message = f"Blockchain loaded from: {main_balance}{recipient_name}.json"
|
434 |
+
return response,message
|
435 |
+
except:
|
436 |
+
try:
|
437 |
+
global mychain_rec
|
438 |
+
mychain_rec = MyChain(chain_load=main_balance,create=recipient_name)
|
439 |
+
response = {'chain': mychain_rec.chain,
|
440 |
+
'length': len(mychain_rec.chain)}
|
441 |
+
message = f"Blockchain loaded from: {main_balance}{recipient_name}.json"
|
442 |
+
return response,message
|
443 |
+
except:
|
444 |
+
message = f"Error loading from: {recipient_name}"
|
445 |
+
return ["Error Loading Chain"],message
|
446 |
+
|
447 |
+
def mychain_transaction_send(sender,recipient,amount):
|
448 |
+
mes, out, balance_send, balance_rec = issue_tokens(sender,recipient,amount)
|
449 |
+
if out == True:
|
450 |
+
mychain_send.new_transaction(f"{sender}",f"{recipient}",f"{amount}",f"{balance_send}")
|
451 |
+
message = "Transaction Added to Pool"
|
452 |
+
data = pd.DataFrame(mychain_send.pending_transactions)
|
453 |
+
if out == False:
|
454 |
+
message = mes
|
455 |
+
data = None
|
456 |
+
return data,message,None,None,None
|
457 |
|
458 |
+
def mychain_transaction_rec(sender,recipient,amount):
|
459 |
+
mes, out, balance_send, balance_rec = issue_tokens(sender,recipient,amount)
|
460 |
if out == True:
|
461 |
+
mychain_rec.new_transaction(f"{sender}",f"{recipient}",f"{amount}",f"{balance_rec}")
|
462 |
message = "Transaction Added to Pool"
|
463 |
+
data = pd.DataFrame(mychain_rec.pending_transactions)
|
464 |
if out == False:
|
465 |
message = mes
|
466 |
data = None
|
467 |
return data,message,None,None,None
|
468 |
+
|
469 |
+
def mychain_mine_block_send(balance, chain_r=None,chain_n=None):
|
470 |
+
previous_block = mychain_send.print_previous_block()
|
471 |
+
previous_proof = previous_block['proof']
|
472 |
+
proof = mychain_send.proof_of_work(previous_proof)
|
473 |
+
previous_hash = mychain_send.hash(previous_block)
|
474 |
+
block = mychain_send.create_block(balance, proof, previous_hash,chain_r,chain_n)
|
475 |
+
|
476 |
+
response = {'message': 'A block is MINED',
|
477 |
+
'index': block['index'],
|
478 |
+
'timestamp': block['timestamp'],
|
479 |
+
'balance': block['balance'],
|
480 |
+
'proof': block['proof'],
|
481 |
+
'previous_hash': block['previous_hash']}
|
482 |
+
message = "A block is MINED"
|
483 |
+
show_chain = display_chain_send()
|
484 |
+
if len(mychain_send.chain) > 1000:
|
485 |
+
mychain_send.reset()
|
486 |
+
response = None
|
487 |
+
show_chain=display_chain_send()
|
488 |
+
message = "New Chain Created at Max 20 Blocks"
|
489 |
+
return response, show_chain,pd.DataFrame(mychain_send.pending_transactions), message
|
490 |
|
491 |
+
def mychain_mine_block_rec(balance, chain_r=None,chain_n=None):
|
492 |
+
previous_block = mychain_rec.print_previous_block()
|
493 |
+
previous_proof = previous_block['proof']
|
494 |
+
proof = mychain_rec.proof_of_work(previous_proof)
|
495 |
+
previous_hash = mychain_rec.hash(previous_block)
|
496 |
+
block = mychain_rec.create_block(balance, proof, previous_hash,chain_r,chain_n)
|
497 |
|
498 |
+
response = {'message': 'A block is MINED',
|
499 |
+
'index': block['index'],
|
500 |
+
'timestamp': block['timestamp'],
|
501 |
+
'balance': block['balance'],
|
502 |
+
'proof': block['proof'],
|
503 |
+
'previous_hash': block['previous_hash']}
|
504 |
+
message = "A block is MINED"
|
505 |
+
show_chain = display_chain_rec()
|
506 |
+
if len(mychain_rec.chain) > 1000:
|
507 |
+
mychain_rec.reset()
|
508 |
+
response = None
|
509 |
+
show_chain=display_chain_rec()
|
510 |
+
message = "New Chain Created at Max 20 Blocks"
|
511 |
+
return response, show_chain,pd.DataFrame(mychain_rec.pending_transactions), message
|
512 |
|
513 |
|
514 |
def issue_tokens(send,rec,amount):
|
515 |
+
#print(f"CURRENT USER:: {api.whoami(['name'])}")
|
516 |
#repo = f'omnibus/{space}'
|
517 |
#is_valid='True'
|
518 |
+
balance_send = 0
|
519 |
+
balance_rec = 0
|
520 |
+
|
521 |
try:
|
522 |
_,z=valid()
|
523 |
if z == True:
|
524 |
mes = None
|
525 |
try:
|
526 |
+
response,message = get_my_chain_send(send)
|
527 |
#r = requests.get(f'{main_balance}{send}.json')
|
528 |
lod = json.loads(response)
|
529 |
p=True
|
|
|
535 |
if p==True:
|
536 |
balance = lod["chain"][-1]["balance"]
|
537 |
print (balance)
|
538 |
+
balance_send =int(balance)-int(amount)
|
539 |
if balance >=0:
|
540 |
p==True
|
541 |
+
response,message = get_my_chain_rec(rec)
|
542 |
+
lod_rec = json.loads(response)
|
543 |
+
#try:
|
544 |
+
if lod_rec["chain"][-1]["index"] > 1:
|
545 |
+
balance = lod_rec["chain"][-1]["balance"]
|
546 |
+
if lod_rec["chain"][-1]["index"] == 1:
|
547 |
+
balance = 0
|
548 |
+
balance_rec =int(balance)+int(amount)
|
549 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
550 |
if balance < 0:
|
551 |
mes ="Not enough tokens"
|
552 |
p = False
|
|
|
556 |
except Exception:
|
557 |
mes = "Blockchain not loaded?"
|
558 |
p=False
|
559 |
+
if p==True:
|
560 |
+
mychain_send.new_transaction(f"{sender}",f"{recipient}",f"{amount}",f"{balance_send}")
|
561 |
+
message_send = "Transaction Added to Pool"
|
562 |
+
data_send = pd.DataFrame(mychain_send.pending_transactions)
|
563 |
+
mychain_rec.new_transaction(f"{sender}",f"{recipient}",f"{amount}",f"{balance_rec}")
|
564 |
+
message_rec = "Transaction Added to Pool"
|
565 |
+
data_rec = pd.DataFrame(mychain_rec.pending_transactions)
|
566 |
+
response_send, show_chain_send,data_send, message_send = mychain_mine_block_send(balance_send, chain_r=None,chain_n=send):
|
567 |
+
response_rec, show_chain_rec,data_rec, message_rec = mychain_mine_block_send(balance_rec, chain_r=None,chain_n=rec):
|
568 |
+
mes = (f'Send: {message_send} :: Recieve: {message_rec}')
|
569 |
+
return (response_send, response_rec, show_chain_send, show_chain_rec, data_send, data_rec, message_send, message_rec)
|
570 |
|
571 |
|
572 |
############################## MYCHAIN END ###############################
|
|
|
615 |
node_file=gr.Textbox(label="Node File")
|
616 |
node_add=gr.Button("Add Node")
|
617 |
with gr.Accordion("Tokens", open=False):
|
618 |
+
|
619 |
issue_btn=gr.Button()
|
620 |
out_box_bool=gr.Textbox()
|
621 |
+
with gr.Row():
|
622 |
+
with gr.Column():
|
623 |
+
block_text_send = gr.Textbox()
|
624 |
+
trans_data_send = gr.Dataframe()
|
625 |
+
json_out_send = gr.JSON()
|
626 |
+
chain_json_send = gr.JSON()
|
627 |
+
with gr.Column():
|
628 |
+
block_text_rec = gr.Textbox()
|
629 |
+
trans_data_rec = gr.Dataframe()
|
630 |
+
json_out_rec = gr.JSON()
|
631 |
+
chain_json_rec = gr.JSON()
|
632 |
+
issue_btn.click(issue_tokens,[send,rec,am],[json_out_send,json_out_rec,chain_json_send,chain_json_rec,trans_data_send,trans_data_rec,block_text_send,block_text_rec])
|
633 |
|
634 |
node_add.click(add_node,[this_space,node_repo,node_space,node_file],block_text)
|
635 |
pass_btn.click(checkp,pass_box,[invalid,valida])
|