Spaces:
Runtime error
Runtime error
Create bc_utils.py
Browse files- bc_utils.py +130 -0
bc_utils.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from mychain import Blockchain
|
| 3 |
+
from mychain import MyChainTrans
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
main_chain='https://huggingface.co/datasets/Omnibus/blockchain-sim-2/raw/main/chains/'
|
| 8 |
+
main_balance='https://huggingface.co/datasets/Omnibus/blockchain-sim-2/raw/main/balance/'
|
| 9 |
+
main_trans='https://huggingface.co/datasets/Omnibus/blockchain-sim-2/raw/main/transact/'
|
| 10 |
+
|
| 11 |
+
trans_name = 'trans1.json'
|
| 12 |
+
repo_d='Omnibus/static-bin'
|
| 13 |
+
chain_d='chain1.json'
|
| 14 |
+
|
| 15 |
+
def bc_transactions(block):
|
| 16 |
+
#mes, out = issue_tokens(sender,recipient,amount)
|
| 17 |
+
print (f'block:: {block}')
|
| 18 |
+
try:
|
| 19 |
+
blockchain.new_transaction(block)
|
| 20 |
+
message = "Transaction Added to Pool"
|
| 21 |
+
data = pd.DataFrame(blockchain.trans_data_out)
|
| 22 |
+
except Exception as e:
|
| 23 |
+
message = e
|
| 24 |
+
data = None
|
| 25 |
+
return data,message
|
| 26 |
+
|
| 27 |
+
def create_chain(create=None):
|
| 28 |
+
global blockchain
|
| 29 |
+
blockchain = Blockchain(chain_load=main_chain,create=create)
|
| 30 |
+
#blockchain.reset(create=create)
|
| 31 |
+
return "New Chain Created",None,display_chain()
|
| 32 |
+
|
| 33 |
+
def display_chain():
|
| 34 |
+
response = {'chain': blockchain.chain,
|
| 35 |
+
'length': len(blockchain.chain)}
|
| 36 |
+
return response
|
| 37 |
+
|
| 38 |
+
def mine_block(chain_r=None,chain_n=None):
|
| 39 |
+
if chain_n=="":
|
| 40 |
+
chain_n = chain_d
|
| 41 |
+
previous_block = blockchain.print_previous_block()
|
| 42 |
+
previous_proof = previous_block['proof']
|
| 43 |
+
proof = blockchain.proof_of_work(previous_proof)
|
| 44 |
+
previous_hash = blockchain.hash(previous_block)
|
| 45 |
+
block = blockchain.create_block(proof, previous_hash,chain_r,chain_n)
|
| 46 |
+
|
| 47 |
+
response = {'message': 'A block is MINED',
|
| 48 |
+
'index': block['index'],
|
| 49 |
+
'timestamp': block['timestamp'],
|
| 50 |
+
'proof': block['proof'],
|
| 51 |
+
'previous_hash': block['previous_hash']}
|
| 52 |
+
message = "A block is MINED"
|
| 53 |
+
show_chain = display_chain()
|
| 54 |
+
if len(blockchain.chain) > 1000:
|
| 55 |
+
blockchain.reset()
|
| 56 |
+
response = None
|
| 57 |
+
show_chain=display_chain()
|
| 58 |
+
message = "New Chain Created at Max 20 Blocks"
|
| 59 |
+
#MyChainTrans.reset(create=chain_d)
|
| 60 |
+
return response, show_chain,pd.DataFrame(blockchain.pending_transactions), message
|
| 61 |
+
|
| 62 |
+
def valid():
|
| 63 |
+
valid,ind,mes = blockchain.chain_valid(blockchain.chain)
|
| 64 |
+
if valid:
|
| 65 |
+
response = 'The Blockchain is valid.'
|
| 66 |
+
z=True
|
| 67 |
+
else:
|
| 68 |
+
response = f'Blockchain is not valid. {mes} at Index {ind}'
|
| 69 |
+
z=False
|
| 70 |
+
return response,z
|
| 71 |
+
|
| 72 |
+
def get_chain(repo_name=None,chain_name=None,token=None):
|
| 73 |
+
if repo_name == "":
|
| 74 |
+
repo_name = repo_d
|
| 75 |
+
if chain_name=="":
|
| 76 |
+
chain_name = chain_d
|
| 77 |
+
try:
|
| 78 |
+
r = requests.get(f'{main_chain}{chain_name}')
|
| 79 |
+
#create_chain(load=r.text)
|
| 80 |
+
global blockchain
|
| 81 |
+
blockchain = Blockchain(chain_load=main_chain,load=r.text)
|
| 82 |
+
response = {'chain': blockchain.chain,
|
| 83 |
+
'length': len(blockchain.chain)}
|
| 84 |
+
message = f"Blockchain loaded from: {main_chain}{chain_name}"
|
| 85 |
+
return response,message
|
| 86 |
+
except:
|
| 87 |
+
message = f"Error loading from: {src}"
|
| 88 |
+
return ["Error Loading Chain"],message
|
| 89 |
+
def checkp(inp):
|
| 90 |
+
if inp == pa:
|
| 91 |
+
return gr.update(visible=False), gr.update(visible=True)
|
| 92 |
+
elif inp != pa:
|
| 93 |
+
return gr.update(visible=True), gr.update(visible=False)
|
| 94 |
+
def add_node(this_space,repo,space,chain_file):
|
| 95 |
+
#print(f"{api.whoami(['name'])}")
|
| 96 |
+
#repo = f'omnibus/{space}'
|
| 97 |
+
is_valid='True'
|
| 98 |
+
r = requests.get(f'{main_nodes}')
|
| 99 |
+
try:
|
| 100 |
+
lod = json.loads(r.text)
|
| 101 |
+
except:
|
| 102 |
+
lod=[]
|
| 103 |
+
pass
|
| 104 |
+
block = {'index': len(lod) + 1,
|
| 105 |
+
'timestamp': str(datetime.datetime.now()),
|
| 106 |
+
'url': f'https://huggingface.co/datasets/{repo}/{space}/raw/main/{chain_file}',
|
| 107 |
+
'valid': f'{is_valid}'}
|
| 108 |
+
lod.append(block)
|
| 109 |
+
|
| 110 |
+
json_object = json.dumps(lod, indent=4)
|
| 111 |
+
with open("tmp1.json", "w") as outfile:
|
| 112 |
+
outfile.write(json_object)
|
| 113 |
+
try:
|
| 114 |
+
api.upload_file(
|
| 115 |
+
path_or_fileobj="tmp1.json",
|
| 116 |
+
path_in_repo=main_nodes.split('main/',1)[1],
|
| 117 |
+
repo_id=main_nodes.split('datasets/',1)[1].split('/raw',1)[0],
|
| 118 |
+
token=token_self,
|
| 119 |
+
repo_type="dataset",
|
| 120 |
+
)
|
| 121 |
+
os.remove("tmp1.json")
|
| 122 |
+
|
| 123 |
+
except Exception as e:
|
| 124 |
+
pass
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
#api = HfApi(token=token)
|
| 129 |
+
repo = main_balance.split('datasets/',1)[1].split('/raw',1)[0].split('/',1)[0]
|
| 130 |
+
name = main_balance.split('datasets/',1)[1].split('/raw',1)[0].split('/',1)[1]
|