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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import datetime
|
| 4 |
+
import hashlib
|
| 5 |
+
import json
|
| 6 |
+
import os
|
| 7 |
+
import requests
|
| 8 |
+
from mychain import Blockchain
|
| 9 |
+
from mychain import MyChainSend
|
| 10 |
+
from mychain import MyChainRec
|
| 11 |
+
from mychain import MyChainTrans
|
| 12 |
+
import bc_utils
|
| 13 |
+
|
| 14 |
+
from huggingface_hub import (create_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi)
|
| 15 |
+
|
| 16 |
+
main_chain='https://huggingface.co/datasets/Omnibus/blockchain-sim-2/raw/main/chains/'
|
| 17 |
+
main_balance='https://huggingface.co/datasets/Omnibus/blockchain-sim-2/raw/main/balance/'
|
| 18 |
+
main_trans='https://huggingface.co/datasets/Omnibus/blockchain-sim-2/raw/main/transact/'
|
| 19 |
+
|
| 20 |
+
#main_nodes='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/node_file1.json'
|
| 21 |
+
#main_pending='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/pending1.json'
|
| 22 |
+
token_self = os.environ['HF_TOKEN']
|
| 23 |
+
pa=os.environ['PASS']
|
| 24 |
+
|
| 25 |
+
trans_name = 'trans1.json'
|
| 26 |
+
#repo_d='Omnibus/static-bin'
|
| 27 |
+
chain_d='chain1.json'
|
| 28 |
+
#node_file='node_file.json'
|
| 29 |
+
#space='blockchain-simulator-dev1'
|
| 30 |
+
api = HfApi(token=token_self)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
repo = main_balance.split('datasets/',1)[1].split('/raw',1)[0].split('/',1)[0]
|
| 35 |
+
name = main_balance.split('datasets/',1)[1].split('/raw',1)[0].split('/',1)[1]
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
############################## BLOCKCHAIN END ###############################
|
| 41 |
+
|
| 42 |
+
def checkp(inp):
|
| 43 |
+
if inp == pa:
|
| 44 |
+
return gr.update(visible=False), gr.update(visible=True)
|
| 45 |
+
elif inp != pa:
|
| 46 |
+
return gr.update(visible=True), gr.update(visible=False)
|
| 47 |
+
|
| 48 |
+
############################## MYCHAIN START ###############################
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def get_my_chain_send(sender_name=None):
|
| 53 |
+
global mychain_send
|
| 54 |
+
global mychain_trans
|
| 55 |
+
mes_error = ""
|
| 56 |
+
try:
|
| 57 |
+
r = requests.get(f'{main_balance}{sender_name}.json')
|
| 58 |
+
|
| 59 |
+
#print (f'r={r.text}')
|
| 60 |
+
mychain_send = MyChainSend(chain_load=main_balance,load=r.text)
|
| 61 |
+
|
| 62 |
+
response = {'chain': mychain_send.chain,
|
| 63 |
+
'length': len(mychain_send.chain)}
|
| 64 |
+
#print (f'response={response}')
|
| 65 |
+
try:
|
| 66 |
+
d = requests.get(f'{main_trans}{trans_name}')
|
| 67 |
+
mychain_trans = MyChainTrans(chain_load=main_trans,load=d.text)
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print (f'error1:: {e}')
|
| 70 |
+
try:
|
| 71 |
+
mychain_trans = MyChainTrans(chain_load=main_trans,create=trans_name)
|
| 72 |
+
print ("this worked")
|
| 73 |
+
except Exception as ee:
|
| 74 |
+
print (f'error2:: {ee}')
|
| 75 |
+
pass
|
| 76 |
+
try:
|
| 77 |
+
response_trans = {'chain': mychain_trans.chain,
|
| 78 |
+
'length': len(mychain_trans.chain)}
|
| 79 |
+
print(f'response_trans:: {response_trans}')
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print (e)
|
| 82 |
+
pass
|
| 83 |
+
|
| 84 |
+
message = f"Blockchain loaded from: {main_balance}{sender_name}.json"
|
| 85 |
+
return response,message
|
| 86 |
+
except Exception:
|
| 87 |
+
message = f"Error loading from: {sender_name}"
|
| 88 |
+
print (message)
|
| 89 |
+
return ["Error Loading Chain"],message
|
| 90 |
+
|
| 91 |
+
def get_my_chain_rec(recipient_name=None):
|
| 92 |
+
global mychain_rec
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
r = requests.get(f'{main_balance}{recipient_name}.json')
|
| 96 |
+
mychain_rec = MyChainRec(chain_load=main_balance,load=r.text)
|
| 97 |
+
response = {'chain': mychain_rec.chain,
|
| 98 |
+
'length': len(mychain_rec.chain)}
|
| 99 |
+
message = f"Blockchain loaded from: {main_balance}{recipient_name}.json"
|
| 100 |
+
return response,message
|
| 101 |
+
except Exception:
|
| 102 |
+
try:
|
| 103 |
+
mychain_rec = MyChainRec(chain_load=main_balance,create=recipient_name)
|
| 104 |
+
response = {'chain': mychain_rec.chain,
|
| 105 |
+
'length': len(mychain_rec.chain)}
|
| 106 |
+
|
| 107 |
+
message = f"Blockchain loaded from: {main_balance}{recipient_name}.json"
|
| 108 |
+
return response,message
|
| 109 |
+
except Exception:
|
| 110 |
+
message = f"Error loading from: {recipient_name}"
|
| 111 |
+
return ["Error Loading Chain"],message
|
| 112 |
+
|
| 113 |
+
def display_chain_send():
|
| 114 |
+
response = {'chain': mychain_send.chain,
|
| 115 |
+
'length': len(mychain_send.chain)}
|
| 116 |
+
return response
|
| 117 |
+
|
| 118 |
+
def display_chain_rec():
|
| 119 |
+
response = {'chain': mychain_rec.chain,
|
| 120 |
+
'length': len(mychain_rec.chain)}
|
| 121 |
+
return response
|
| 122 |
+
|
| 123 |
+
def valid_send():
|
| 124 |
+
valid,ind,mes = mychain_send.chain_valid(mychain_send.chain)
|
| 125 |
+
if valid:
|
| 126 |
+
response = 'The Blockchain is valid.'
|
| 127 |
+
z=True
|
| 128 |
+
else:
|
| 129 |
+
response = f'Sender Blockchain is not valid. {mes} at Index {ind}'
|
| 130 |
+
z=False
|
| 131 |
+
return response,z,mes,ind
|
| 132 |
+
|
| 133 |
+
def valid_rec():
|
| 134 |
+
valid,ind,mes = mychain_rec.chain_valid(mychain_rec.chain)
|
| 135 |
+
if valid:
|
| 136 |
+
response = 'The Blockchain is valid.'
|
| 137 |
+
z=True
|
| 138 |
+
else:
|
| 139 |
+
response = f'Blockchain is not valid. {mes} at Index {ind}'
|
| 140 |
+
z=False
|
| 141 |
+
return response,z, mes, ind
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def mychain_mine_block_send(balance, chain_r=None,chain_n=None):
|
| 146 |
+
previous_block = mychain_send.print_previous_block()
|
| 147 |
+
previous_proof = previous_block['proof']
|
| 148 |
+
proof = mychain_send.proof_of_work(previous_proof)
|
| 149 |
+
previous_hash = mychain_send.hash(previous_block)
|
| 150 |
+
block = mychain_send.create_block(balance, proof, previous_hash,len(bc_utils.blockchain.chain)+1,chain_r,chain_n)
|
| 151 |
+
|
| 152 |
+
response = {'message': 'A block is MINED',
|
| 153 |
+
'index': block['index'],
|
| 154 |
+
'timestamp': block['timestamp'],
|
| 155 |
+
'balance': block['balance'],
|
| 156 |
+
'proof': block['proof'],
|
| 157 |
+
'previous_hash': block['previous_hash']
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
message = "A block is MINED"
|
| 161 |
+
show_chain = display_chain_send()
|
| 162 |
+
|
| 163 |
+
if len(mychain_send.chain) > 1000:
|
| 164 |
+
mychain_send.reset()
|
| 165 |
+
response = None
|
| 166 |
+
show_chain=display_chain_send()
|
| 167 |
+
message = "New Chain Created at Max 20 Blocks"
|
| 168 |
+
|
| 169 |
+
return response, show_chain,message
|
| 170 |
+
|
| 171 |
+
def mychain_mine_block_trans(balance, chain_r=None,chain_n=None):
|
| 172 |
+
previous_block = mychain_trans.print_previous_block()
|
| 173 |
+
previous_proof = previous_block['proof']
|
| 174 |
+
proof = mychain_trans.proof_of_work(previous_proof)
|
| 175 |
+
previous_hash = mychain_trans.hash(previous_block)
|
| 176 |
+
block = mychain_trans.create_block(balance, proof, previous_hash,len(bc_utils.blockchain.chain)+1,chain_r,chain_n)
|
| 177 |
+
|
| 178 |
+
response = {'message': 'A block is MINED',
|
| 179 |
+
'index': block['index'],
|
| 180 |
+
'timestamp': block['timestamp'],
|
| 181 |
+
'balance': block['balance'],
|
| 182 |
+
'proof': block['proof'],
|
| 183 |
+
'previous_hash': block['previous_hash']
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
message = "A block is MINED"
|
| 187 |
+
show_chain = display_chain_send()
|
| 188 |
+
|
| 189 |
+
if len(mychain_trans.chain) > 1000:
|
| 190 |
+
mychain_trans.reset()
|
| 191 |
+
response = None
|
| 192 |
+
show_chain=display_chain_send()
|
| 193 |
+
message = "New Chain Created at Max 20 Blocks"
|
| 194 |
+
|
| 195 |
+
return response, show_chain,message
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
def mychain_mine_block_rec(balance, chain_r=None,chain_n=None):
|
| 200 |
+
previous_block = mychain_rec.print_previous_block()
|
| 201 |
+
previous_proof = previous_block['proof']
|
| 202 |
+
proof = mychain_rec.proof_of_work(previous_proof)
|
| 203 |
+
previous_hash = mychain_rec.hash(previous_block)
|
| 204 |
+
block = mychain_rec.create_block(balance, proof, previous_hash,len(bc_utils.blockchain.chain)+1,chain_r,chain_n)
|
| 205 |
+
|
| 206 |
+
response = {'message': 'A block is MINED',
|
| 207 |
+
'index': block['index'],
|
| 208 |
+
'timestamp': block['timestamp'],
|
| 209 |
+
'balance': block['balance'],
|
| 210 |
+
'proof': block['proof'],
|
| 211 |
+
'previous_hash': block['previous_hash']
|
| 212 |
+
}
|
| 213 |
+
message = "A block is MINED"
|
| 214 |
+
show_chain = display_chain_rec()
|
| 215 |
+
if len(mychain_rec.chain) > 1000:
|
| 216 |
+
mychain_rec.reset()
|
| 217 |
+
response = None
|
| 218 |
+
show_chain=display_chain_rec()
|
| 219 |
+
message = "New Chain Created at Max 20 Blocks"
|
| 220 |
+
return response, show_chain, message
|
| 221 |
+
|
| 222 |
+
def merge_trans():
|
| 223 |
+
trans_bx = []
|
| 224 |
+
previous_post_send = mychain_send.print_previous_block()
|
| 225 |
+
previous_post_rec = mychain_rec.print_previous_block()
|
| 226 |
+
|
| 227 |
+
send_block = {
|
| 228 |
+
'role': 'Sender',
|
| 229 |
+
'name': previous_post_send['transactions'][0]['sender'],
|
| 230 |
+
'index': previous_post_send['index'],
|
| 231 |
+
'timestamp': previous_post_send['timestamp'],
|
| 232 |
+
'recipient': previous_post_send['transactions'][0]['recipient'],
|
| 233 |
+
'amount': previous_post_send['transactions'][0]['amount'],
|
| 234 |
+
'balance': previous_post_send['balance'],
|
| 235 |
+
'proof': previous_post_send['proof'],
|
| 236 |
+
'previous_hash': previous_post_send['previous_hash'],
|
| 237 |
+
'block': len(bc_utils.blockchain.chain)+1}
|
| 238 |
+
rec_block = {
|
| 239 |
+
'role': 'Recipient',
|
| 240 |
+
'name': previous_post_rec['transactions'][0]['recipient'],
|
| 241 |
+
'index': previous_post_rec['index'],
|
| 242 |
+
'timestamp': previous_post_rec['timestamp'],
|
| 243 |
+
'sender': previous_post_rec['transactions'][0]['sender'],
|
| 244 |
+
'amount': previous_post_rec['transactions'][0]['amount'],
|
| 245 |
+
'balance': previous_post_rec['balance'],
|
| 246 |
+
'proof': previous_post_rec['proof'],
|
| 247 |
+
'previous_hash': previous_post_rec['previous_hash'],
|
| 248 |
+
'block': len(bc_utils.blockchain.chain)+1}
|
| 249 |
+
|
| 250 |
+
trans_bx.append(send_block)
|
| 251 |
+
trans_bx.append(rec_block)
|
| 252 |
+
return trans_bx
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
def issue_tokens(send,rec,amount):
|
| 258 |
+
response_send={}
|
| 259 |
+
response_rec={}
|
| 260 |
+
show_chain_send={}
|
| 261 |
+
show_chain_rec={}
|
| 262 |
+
data_send=None
|
| 263 |
+
data_rec=None
|
| 264 |
+
rec_send=None
|
| 265 |
+
rec_drop=None
|
| 266 |
+
message_send=None
|
| 267 |
+
message_rec=None
|
| 268 |
+
trans_data=None
|
| 269 |
+
trans_mes=None
|
| 270 |
+
balance_send = 0
|
| 271 |
+
balance_rec = 0
|
| 272 |
+
mes = "blank message"
|
| 273 |
+
try:
|
| 274 |
+
b_mes,z=bc_utils.valid()
|
| 275 |
+
if z == True:
|
| 276 |
+
try:
|
| 277 |
+
response,message = get_my_chain_send(send)
|
| 278 |
+
#print (f'response:{response}::message{message}')
|
| 279 |
+
#r = requests.get(f'{main_balance}{send}.json')
|
| 280 |
+
lod=response
|
| 281 |
+
#lod = json.loads(response)
|
| 282 |
+
#print (lod)
|
| 283 |
+
p=True
|
| 284 |
+
except Exception as e:
|
| 285 |
+
lod=[]
|
| 286 |
+
p=False
|
| 287 |
+
mes = f"Sender has no wallet {e}"
|
| 288 |
+
pass
|
| 289 |
+
mes1,val,mes2,ind = valid_send()
|
| 290 |
+
print (f'val:: {val}')
|
| 291 |
+
if val == False:
|
| 292 |
+
p=False
|
| 293 |
+
mes = f"Sender: {send} Blockchain is not valid. {mes2} at Index {ind}"
|
| 294 |
+
#mes = "Blockchain is not valid."
|
| 295 |
+
|
| 296 |
+
#if val == True:
|
| 297 |
+
if p==True:
|
| 298 |
+
try:
|
| 299 |
+
balance = lod["chain"][-1]["balance"]
|
| 300 |
+
except Exception:
|
| 301 |
+
balance = 0
|
| 302 |
+
p=False
|
| 303 |
+
#print (balance)
|
| 304 |
+
balance_send =int(balance)-int(amount)
|
| 305 |
+
if balance_send >=0:
|
| 306 |
+
p=True
|
| 307 |
+
response,message = get_my_chain_rec(rec)
|
| 308 |
+
lod_rec = response
|
| 309 |
+
mes1,val,mes2,ind = valid_rec()
|
| 310 |
+
print (f'val:: {val}')
|
| 311 |
+
if val == False:
|
| 312 |
+
p=False
|
| 313 |
+
mes = f"Recipient: {rec} Blockchain is not valid. {mes2} at Index {ind}"
|
| 314 |
+
#mes = "Blockchain is not valid."
|
| 315 |
+
#print (lod_rec)
|
| 316 |
+
if val == True:
|
| 317 |
+
try:
|
| 318 |
+
balance = lod_rec["chain"][-1]["balance"]
|
| 319 |
+
#print (balance)
|
| 320 |
+
except Exception:
|
| 321 |
+
p=False
|
| 322 |
+
balance = 0
|
| 323 |
+
balance_rec =int(balance)+int(amount)
|
| 324 |
+
|
| 325 |
+
if balance_send < 0:
|
| 326 |
+
mes ="Not enough tokens"
|
| 327 |
+
p = False
|
| 328 |
+
|
| 329 |
+
print(mes)
|
| 330 |
+
|
| 331 |
+
if z==False:
|
| 332 |
+
mes = b_mes
|
| 333 |
+
p=False
|
| 334 |
+
except Exception as e:
|
| 335 |
+
mes = f"Blockchain not loaded? {e}"
|
| 336 |
+
p=False
|
| 337 |
+
print (mes)
|
| 338 |
+
|
| 339 |
+
if p==False:
|
| 340 |
+
return (mes, p,None,None,None,None,None,None,None,None,None,None,None)
|
| 341 |
+
|
| 342 |
+
#return (mes, p,{},{},{},{},{},{},None,None,None,None,{})
|
| 343 |
+
#json, json, json, json, dataframe, dataframe, textbox, textbox, dropdown, dropdown, dataframe
|
| 344 |
+
if p==True:
|
| 345 |
+
|
| 346 |
+
|
| 347 |
+
mychain_send.new_transaction(f"{send}",f"{rec}",f"{amount}",f"{balance_send}")
|
| 348 |
+
mychain_trans.new_transaction(f"{send}",f"{rec}",f"{amount}",f"{balance_send}")
|
| 349 |
+
|
| 350 |
+
message_send = "Transaction Added to Pool"
|
| 351 |
+
data_send = pd.DataFrame(mychain_send.pending_transactions)
|
| 352 |
+
mychain_rec.new_transaction(f"{send}",f"{rec}",f"{amount}",f"{balance_rec}")
|
| 353 |
+
message_rec = "Transaction Added to Pool"
|
| 354 |
+
data_rec = pd.DataFrame(mychain_rec.pending_transactions)
|
| 355 |
+
response_send, show_chain_send, message_send = mychain_mine_block_send(balance_send, chain_r=None,chain_n=send)
|
| 356 |
+
|
| 357 |
+
mychain_mine_block_trans(balance_send, chain_r=None,chain_n=send)
|
| 358 |
+
|
| 359 |
+
response_rec, show_chain_rec, message_rec = mychain_mine_block_rec(balance_rec, chain_r=None,chain_n=rec)
|
| 360 |
+
mes = (f'Send: {message_send} :: Recieve: {message_rec}')
|
| 361 |
+
_,rec_send=update_send_list()
|
| 362 |
+
_,rec_drop=update_rec_list()
|
| 363 |
+
trans_bx = merge_trans()
|
| 364 |
+
trans_data, mes = bc_utils.bc_transactions(trans_bx)
|
| 365 |
+
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)
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
|
| 369 |
+
|
| 370 |
+
############################## MYCHAIN END ###############################
|
| 371 |
+
|
| 372 |
+
def res_source():
|
| 373 |
+
block = {'index': 1,
|
| 374 |
+
'timestamp': str(datetime.datetime.now()),
|
| 375 |
+
'transactions': [],
|
| 376 |
+
'balance': 10000000000000000000,
|
| 377 |
+
'proof': 1,
|
| 378 |
+
'previous_hash': 0}
|
| 379 |
+
|
| 380 |
+
pending_transactions_x = []
|
| 381 |
+
pending_transactions_x.append(block)
|
| 382 |
+
json_object = json.dumps(pending_transactions_x, indent=4)
|
| 383 |
+
with open("tmp_send.json", "w") as outfile:
|
| 384 |
+
outfile.write(json_object)
|
| 385 |
+
try:
|
| 386 |
+
api.upload_file(
|
| 387 |
+
path_or_fileobj="tmp_send.json",
|
| 388 |
+
path_in_repo="balance/__Source__.json",
|
| 389 |
+
repo_id=main_balance.split('datasets/',1)[1].split('/raw',1)[0],
|
| 390 |
+
token=token_self,
|
| 391 |
+
repo_type="dataset",
|
| 392 |
+
)
|
| 393 |
+
os.remove("tmp_send.json")
|
| 394 |
+
return "__Source__ Reset"
|
| 395 |
+
except Exception as e:
|
| 396 |
+
return e
|
| 397 |
+
|
| 398 |
+
|
| 399 |
+
def update_send_list():
|
| 400 |
+
f_ist = (api.list_repo_files(repo_id=f'{repo}/{name}', repo_type="dataset"))
|
| 401 |
+
send_list =[]
|
| 402 |
+
for i,ea in enumerate(f_ist):
|
| 403 |
+
if "balance/" in ea:
|
| 404 |
+
try:
|
| 405 |
+
send_list.append(ea.split("/",1)[1].split(".",1)[0])
|
| 406 |
+
except Exception:
|
| 407 |
+
pass
|
| 408 |
+
return send_list, gr.Dropdown.update(label="Sender", choices=[f for f in send_list])
|
| 409 |
+
send_list,send_drop = update_send_list()
|
| 410 |
+
|
| 411 |
+
def update_rec_list():
|
| 412 |
+
f_ist = (api.list_repo_files(repo_id=f'{repo}/{name}', repo_type="dataset"))
|
| 413 |
+
rec_list =[]
|
| 414 |
+
for i,ea in enumerate(f_ist):
|
| 415 |
+
if "balance/" in ea:
|
| 416 |
+
try:
|
| 417 |
+
if not "__Source__" in ea:
|
| 418 |
+
rec_list.append(ea.split("/",1)[1].split(".",1)[0])
|
| 419 |
+
except Exception:
|
| 420 |
+
pass
|
| 421 |
+
return rec_list, gr.Dropdown.update(label="Recipient", choices=[f for f in rec_list])
|
| 422 |
+
rec_list, rec_drop = update_rec_list()
|
| 423 |
+
|
| 424 |
+
with gr.Blocks() as bc:
|
| 425 |
+
with gr.Row(visible=True) as invalid:
|
| 426 |
+
pass_box = gr.Textbox()
|
| 427 |
+
pass_btn = gr.Button()
|
| 428 |
+
|
| 429 |
+
with gr.Box(visible=False) as valida:
|
| 430 |
+
gr.Markdown("""<h1><center>Blockchain Simulator<br><h3>(Transactions have no value)<br><h4>Chain will reset at 20 blocks""")
|
| 431 |
+
#blockchain = gr.State()
|
| 432 |
+
with gr.Row():
|
| 433 |
+
with gr.Tab("OG"):
|
| 434 |
+
with gr.Row():
|
| 435 |
+
with gr.Column():
|
| 436 |
+
with gr.Accordion(label="Load",open=False):
|
| 437 |
+
with gr.Row():
|
| 438 |
+
chain_repo=gr.Textbox(label="repo/name")
|
| 439 |
+
chain_n=gr.Textbox(label="Chain file")
|
| 440 |
+
with gr.Row():
|
| 441 |
+
in_chain_btn=gr.Button("Load Chain")
|
| 442 |
+
create_bc = gr.Button("Create New Blockchain")
|
| 443 |
+
#send=gr.Textbox(label="Sender")
|
| 444 |
+
send = gr.Dropdown(label="Sender", choices=[f for f in send_list], value = "Bank")
|
| 445 |
+
rec=gr.Dropdown(label="Recipient", choices=[f for f in rec_list], allow_custom_value=True)
|
| 446 |
+
am=gr.Textbox(label="Amount")
|
| 447 |
+
send_trans=gr.Button("Post Transaction")
|
| 448 |
+
mine_b = gr.Button("Mine Block")
|
| 449 |
+
check = gr.Button("Check Chain")
|
| 450 |
+
check_trans = gr.Button("Check Transactions")
|
| 451 |
+
check_all = gr.Button("Check All")
|
| 452 |
+
with gr.Column():
|
| 453 |
+
block_text = gr.Textbox()
|
| 454 |
+
trans_data = gr.Dataframe()
|
| 455 |
+
json_out = gr.JSON()
|
| 456 |
+
chain_json = gr.JSON()
|
| 457 |
+
with gr.Accordion("Nodes", open=False):
|
| 458 |
+
with gr.Row():
|
| 459 |
+
this_space=gr.Textbox(label="This Repo/Space")
|
| 460 |
+
with gr.Row():
|
| 461 |
+
node_repo=gr.Textbox(label="Node Repo")
|
| 462 |
+
node_space=gr.Textbox(label="Node Space")
|
| 463 |
+
node_file=gr.Textbox(label="Node File")
|
| 464 |
+
node_add=gr.Button("Add Node")
|
| 465 |
+
with gr.Accordion("Tokens", open=False):
|
| 466 |
+
with gr.Row():
|
| 467 |
+
with gr.Column():
|
| 468 |
+
issue_btn=gr.Button()
|
| 469 |
+
with gr.Column():
|
| 470 |
+
with gr.Row():
|
| 471 |
+
reset_blockchain=gr.Button("Reset Main Blockchain")
|
| 472 |
+
reset_source=gr.Button("Reset __Source__")
|
| 473 |
+
with gr.Row():
|
| 474 |
+
reset_sender=gr.Button("Reset Sender")
|
| 475 |
+
reset_recieve=gr.Button("Reset Recipient")
|
| 476 |
+
out_box_bool=gr.Textbox()
|
| 477 |
+
with gr.Row():
|
| 478 |
+
with gr.Column():
|
| 479 |
+
block_text_send = gr.Textbox()
|
| 480 |
+
trans_data_send = gr.Dataframe()
|
| 481 |
+
json_out_send = gr.JSON()
|
| 482 |
+
chain_json_send = gr.JSON()
|
| 483 |
+
with gr.Column():
|
| 484 |
+
block_text_rec = gr.Textbox()
|
| 485 |
+
trans_data_rec = gr.Dataframe()
|
| 486 |
+
json_out_rec = gr.JSON()
|
| 487 |
+
chain_json_rec = gr.JSON()
|
| 488 |
+
with gr.Tab("BC"):
|
| 489 |
+
with gr.Row():
|
| 490 |
+
with gr.Tab("Gen Wal"):
|
| 491 |
+
gen_wal_btn=gr.Button()
|
| 492 |
+
seed = gr.Textbox(label='Seed Phrase')
|
| 493 |
+
img1=gr.Pil(label='Private Key')
|
| 494 |
+
out1 = gr.Textbox(label='Private Key',max_lines=4)
|
| 495 |
+
img2=gr.Pil(label='Public Key')
|
| 496 |
+
out2 = gr.Textbox(label='Public Key',max_lines=4)
|
| 497 |
+
img3=gr.Pil(label='Address')
|
| 498 |
+
out3 = gr.Textbox(label='Address')
|
| 499 |
+
with gr.Tab("Encrypt"):
|
| 500 |
+
rsa_to_enc = gr.Textbox(label="txt to encrypt")
|
| 501 |
+
pub_key_in = gr.Image(label="Public Key", type="filepath")
|
| 502 |
+
rsa_enc_btn = gr.Button("RSA Encrypt")
|
| 503 |
+
rsa_enc_mes = gr.Textbox(label="encoded", max_lines=4)
|
| 504 |
+
qr_enc_mes = gr.Image(type="filepath")
|
| 505 |
+
with gr.Tab("Decrypt"):
|
| 506 |
+
mes_in = gr.Image(label="Message", type="filepath")
|
| 507 |
+
priv_key_in = gr.Image(label="Private Key", type="filepath")
|
| 508 |
+
rsa_dec_btn = gr.Button("RSA Decrypt")
|
| 509 |
+
rsa_dec_mes = gr.Textbox(label="decoded")
|
| 510 |
+
|
| 511 |
+
|
| 512 |
+
|
| 513 |
+
def res_bc():
|
| 514 |
+
bc_utils.blockchain.reset(create="chain1.json")
|
| 515 |
+
return ("Main Blockchain Reset")
|
| 516 |
+
def res_send(send):
|
| 517 |
+
mychain_send = MyChainSend(chain_load=main_balance,create=send)
|
| 518 |
+
return ("Sender Blockchain Reset")
|
| 519 |
+
|
| 520 |
+
def deep_trans():
|
| 521 |
+
chain1=mychain_send.chain
|
| 522 |
+
chain2=bc_utils.blockchain.chain
|
| 523 |
+
valid,ind,mes = mychain_send.deep_valid_send(chain1,chain2)
|
| 524 |
+
|
| 525 |
+
if valid:
|
| 526 |
+
response = 'The Blockchain is valid.'
|
| 527 |
+
z=True
|
| 528 |
+
else:
|
| 529 |
+
response = f'Blockchain is not valid. {mes} at Index {ind}'
|
| 530 |
+
z=False
|
| 531 |
+
return response,z
|
| 532 |
+
|
| 533 |
+
def mine_block(chain_r=None,chain_n=None):
|
| 534 |
+
try:
|
| 535 |
+
a,b,c,d = bc_utils.mine_block(chain_r=None,chain_n=None)
|
| 536 |
+
mychain_trans.reset(create=trans_name)
|
| 537 |
+
return a,b,c,d
|
| 538 |
+
except Exception as e:
|
| 539 |
+
print (e)
|
| 540 |
+
return e,None,None,e
|
| 541 |
+
check_trans.click(deep_trans,None,[block_text,out_box_bool])
|
| 542 |
+
|
| 543 |
+
reset_sender.click(res_send,send,block_text)
|
| 544 |
+
reset_blockchain.click(res_bc,None,block_text)
|
| 545 |
+
reset_source.click(res_source,None,block_text)
|
| 546 |
+
|
| 547 |
+
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])
|
| 548 |
+
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])
|
| 549 |
+
#issue_btn.click(issue_tokens,[send,rec,am],[block_text,out_box_bool,json_out_send])
|
| 550 |
+
|
| 551 |
+
node_add.click(bc_utils.add_node,[this_space,node_repo,node_space,node_file],block_text)
|
| 552 |
+
pass_btn.click(checkp,pass_box,[invalid,valida])
|
| 553 |
+
in_chain_btn.click(bc_utils.get_chain,[chain_repo,chain_n],[chain_json,block_text])
|
| 554 |
+
create_bc.click(bc_utils.create_chain,[chain_n],[block_text,json_out,chain_json])
|
| 555 |
+
check.click(bc_utils.valid,None,[block_text,out_box_bool])
|
| 556 |
+
#check_all.click(sort_valid,None,block_text)
|
| 557 |
+
|
| 558 |
+
#send_trans.click(bc_transactions,[send,rec,am],[trans_data,block_text,send,rec,am])
|
| 559 |
+
mine_b.click(mine_block,[chain_repo,chain_n],[json_out,chain_json,trans_data,block_text])
|
| 560 |
+
|
| 561 |
+
|
| 562 |
+
bc.launch(enable_queue=False)
|