Omnibus commited on
Commit
145bb0c
·
1 Parent(s): cd41808

Create chain.py

Browse files
Files changed (1) hide show
  1. chain.py +371 -0
chain.py ADDED
@@ -0,0 +1,371 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 huggingface_hub import (create_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi)
9
+
10
+ main_chain='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/'
11
+ main_nodes='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/node_file1.json'
12
+ main_pending='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/pending1.json'
13
+ token_self = os.environ['HF_TOKEN']
14
+ pa=os.environ['PASS']
15
+ repo_d='Omnibus/static-bin'
16
+ chain_d='chain1.json'
17
+ node_file='node_file.json'
18
+ space='blockchain-simulator-dev1'
19
+ api = HfApi(token=token_self)
20
+
21
+ class Blockchain:
22
+
23
+ def __init__(self,load=None,create=None):
24
+
25
+ self.pending_transactions = []
26
+ if load == None or load=="":
27
+ self.chain = []
28
+ self.create_block(proof=1, previous_hash='0',chain_n=create)
29
+ elif load != None and load !="":
30
+ #r = requests.get(load)
31
+ lod = json.loads(load)
32
+ self.chain = lod
33
+ def reset(self,create=None):
34
+ self.chain = []
35
+ self.pending_transactions = []
36
+ self.create_block(proof=1, previous_hash='0',chain_n=create)
37
+ def create_block(self, proof, previous_hash,chain_r=None,chain_n=None):
38
+ if chain_r=="" or chain_r==None:
39
+ chain_r=f"{main_chain.split('datasets/',1)[1].split('/raw',1)[0]}"
40
+ if chain_n=="" or chain_n==None:
41
+ chain_n=chain_d
42
+ block = {'index': len(self.chain) + 1,
43
+ 'timestamp': str(datetime.datetime.now()),
44
+ 'transactions': self.pending_transactions,
45
+ 'proof': proof,
46
+ 'previous_hash': previous_hash}
47
+ if self.block_valid(block) == True:
48
+
49
+ self.pending_transactions = []
50
+ self.chain.append(block)
51
+ json_object = json.dumps(self.chain, indent=4)
52
+ with open("tmp.json", "w") as outfile:
53
+ outfile.write(json_object)
54
+ try:
55
+ api.upload_file(
56
+ path_or_fileobj="tmp.json",
57
+ path_in_repo=chain_n,
58
+ repo_id=chain_r,
59
+ token=token_self,
60
+ repo_type="dataset",
61
+ )
62
+ os.remove("tmp.json")
63
+
64
+ except Exception as e:
65
+ print(e)
66
+ pass
67
+ return block
68
+ else:
69
+ block = {"Not Valid"}
70
+ print("not Valid")
71
+ return block
72
+ def print_previous_block(self):
73
+ return self.chain[-1]
74
+ def new_transaction(self, sender, recipient, amount):
75
+ transaction = {
76
+ 'sender': sender,
77
+ 'recipient': recipient,
78
+ 'amount': amount
79
+ }
80
+ self.pending_transactions.append(transaction)
81
+ def proof_of_work(self, previous_proof):
82
+ new_proof = 1
83
+ check_proof = False
84
+ while check_proof is False:
85
+ hash_operation = hashlib.sha256(
86
+ str(new_proof**2 - previous_proof**2).encode()).hexdigest()
87
+ if hash_operation[:5] == '00000':
88
+ check_proof = True
89
+ else:
90
+ new_proof += 1
91
+ return new_proof
92
+
93
+ def hash(self, block):
94
+ encoded_block = json.dumps(block, sort_keys=True).encode()
95
+ return hashlib.sha256(encoded_block).hexdigest()
96
+ def block_valid(self, block):
97
+ print (block)
98
+ #prev_block=len(self.chain)
99
+ if len(self.chain) > 0:
100
+ prev_block = len(self.chain)-1
101
+ previous_block = self.chain[prev_block]
102
+ print (previous_block)
103
+ out=True
104
+ ind=None
105
+ mes=None
106
+ if block['previous_hash'] != self.hash(previous_block):
107
+ out=False
108
+ #ind=block_index
109
+ mes='Hash'
110
+
111
+ previous_proof = previous_block['proof']
112
+ proof = block['proof']
113
+ hash_operation = hashlib.sha256(
114
+ str(proof**2 - previous_proof**2).encode()).hexdigest()
115
+
116
+ if hash_operation[:5] != '00000':
117
+ out=False
118
+ #ind=block_index+1
119
+ mes='Proof'
120
+ previous_block = block
121
+ else:
122
+ out = True
123
+ return out
124
+
125
+ def chain_valid(self, chain):
126
+ previous_block = chain[0]
127
+ block_index = 1
128
+ out=True
129
+ ind=None
130
+ mes=None
131
+ while block_index < len(chain):
132
+ block = chain[block_index]
133
+ if block['previous_hash'] != self.hash(previous_block):
134
+ out=False
135
+ ind=block_index
136
+ mes='Hash'
137
+ break
138
+
139
+ previous_proof = previous_block['proof']
140
+ proof = block['proof']
141
+ hash_operation = hashlib.sha256(
142
+ str(proof**2 - previous_proof**2).encode()).hexdigest()
143
+
144
+ if hash_operation[:5] != '00000':
145
+ out=False
146
+ ind=block_index+1
147
+ mes='Proof'
148
+ break
149
+ previous_block = block
150
+ block_index += 1
151
+
152
+ return out, ind, mes
153
+
154
+ def bc_transactions(sender,recipient,amount):
155
+ blockchain.new_transaction(f"{sender}",f"{recipient}",f"{amount}")
156
+ message = "Transaction Added to Pool"
157
+ return pd.DataFrame(blockchain.pending_transactions),message,None,None,None
158
+
159
+ def create_chain(create=None):
160
+ global blockchain
161
+ blockchain = Blockchain(create=create)
162
+ #blockchain.reset(create=create)
163
+ return "New Chain Created",None,display_chain()
164
+
165
+ def display_chain():
166
+ response = {'chain': blockchain.chain,
167
+ 'length': len(blockchain.chain)}
168
+ return response
169
+
170
+ def mine_block(chain_r=None,chain_n=None):
171
+ previous_block = blockchain.print_previous_block()
172
+ previous_proof = previous_block['proof']
173
+ proof = blockchain.proof_of_work(previous_proof)
174
+ previous_hash = blockchain.hash(previous_block)
175
+ block = blockchain.create_block(proof, previous_hash,chain_r,chain_n)
176
+
177
+ response = {'message': 'A block is MINED',
178
+ 'index': block['index'],
179
+ 'timestamp': block['timestamp'],
180
+ 'proof': block['proof'],
181
+ 'previous_hash': block['previous_hash']}
182
+ message = "A block is MINED"
183
+ show_chain = display_chain()
184
+ if len(blockchain.chain) > 20:
185
+ blockchain.reset()
186
+ response = None
187
+ show_chain=display_chain()
188
+ message = "New Chain Created at Max 20 Blocks"
189
+ return response, show_chain,pd.DataFrame(blockchain.pending_transactions), message
190
+
191
+ def sort_valid():
192
+ #nodes=main_nodes.replace("'","")
193
+ f = requests.get(f'{main_nodes}')
194
+ t = open('tmp.json','w')
195
+ t.write(f.text)
196
+ t.close()
197
+ f = open('tmp.json','r')
198
+ f = f.read()
199
+ j = json.loads(f)
200
+ #j=f.json()
201
+ print (len(j))
202
+ cnt=0
203
+ valid_chains=[]
204
+ not_valid=[]
205
+ response=''
206
+ while cnt < len(j):
207
+ try:
208
+ r = requests.get(f'{j[cnt]["url"]}')
209
+ g = open('tmp1.json','w')
210
+ g.write(r.text)
211
+ g.close()
212
+ gg = open('tmp1.json','r')
213
+ ggg=gg.read()
214
+ print (ggg)
215
+ leng=len(json.loads(ggg))
216
+ print(f'Blockchain {cnt}: Length {leng} ')
217
+ valid,ind,mes = blockchain.chain_valid(json.loads(ggg))
218
+ except Exception:
219
+ valid=False
220
+ leng="No Data"
221
+ if valid:
222
+ valid_chains.append({"Chain": cnt, "Length": leng})
223
+ response = f'{response} Valid: {cnt}'
224
+ else:
225
+ not_valid.append({"Chain": cnt, "Length": leng})
226
+ response = f'{response} Invalid:{cnt}'
227
+
228
+ per = ((len(valid_chains)+1)/(len(j)+1))*100
229
+ cnt+=1
230
+ response=f'{int(per)}%-{response}'
231
+ print (f'Valid: {valid_chains}')
232
+ print (f'Not Valid: {not_valid}')
233
+ #p = json.loads(str(valid_chains))
234
+ p = valid_chains
235
+ cnt2=0
236
+ bot=0
237
+ out=[]
238
+ while cnt2 < len(p):
239
+ if p[cnt2]['Length'] > bot:
240
+ bot = p[cnt2]['Length']
241
+ out = [cnt2,bot]
242
+ else:
243
+ pass
244
+ cnt2+=1
245
+ print (out)
246
+ return response
247
+
248
+ def valid():
249
+ valid,ind,mes = blockchain.chain_valid(blockchain.chain)
250
+ if valid:
251
+ response = 'The Blockchain is valid.'
252
+ else:
253
+ response = f'Blockchain is not valid. {mes} at Index {ind}'
254
+ return response
255
+
256
+ def get_chain(repo_name=None,chain_name=None,token=None):
257
+ if repo_name == "":
258
+ repo_name = repo_d
259
+ if chain_name=="":
260
+ chain_name = chain_d
261
+ #src = f"https://huggingface.co/spaces/{repo_name}/raw/main/{chain_name}"
262
+
263
+ #ff = open('chain1.json','r')
264
+ #src = ff.read()
265
+ #src = json.loads(ff)
266
+
267
+
268
+ try:
269
+ r = requests.get(f'{main_chain}{chain_name}')
270
+ #create_chain(load=r.text)
271
+ global blockchain
272
+ blockchain = Blockchain(load=r.text)
273
+ #return "New Chain Created",display_chain()
274
+
275
+
276
+
277
+ #create_chain(src)
278
+ response = {'chain': blockchain.chain,
279
+ 'length': len(blockchain.chain)}
280
+ message = f"Blockchain loaded from: {main_chain}{chain_name}"
281
+ return response,message
282
+ except:
283
+ message = f"Error loading from: {src}"
284
+ return ["Error Loading Chain"],message
285
+ def checkp(inp):
286
+ if inp == pa:
287
+ return gr.update(visible=False), gr.update(visible=True)
288
+ elif inp != pa:
289
+ return gr.update(visible=True), gr.update(visible=False)
290
+ def add_node(this_space,repo,space,chain_file):
291
+ #print(f"{api.whoami(['name'])}")
292
+ #repo = f'omnibus/{space}'
293
+ is_valid='True'
294
+ r = requests.get(f'{main_nodes}')
295
+ try:
296
+ lod = json.loads(r.text)
297
+ except:
298
+ lod=[]
299
+ pass
300
+ block = {'index': len(lod) + 1,
301
+ 'timestamp': str(datetime.datetime.now()),
302
+ 'url': f'https://huggingface.co/datasets/{repo}/{space}/raw/main/{chain_file}',
303
+ 'valid': f'{is_valid}'}
304
+ lod.append(block)
305
+
306
+ json_object = json.dumps(lod, indent=4)
307
+ with open("tmp1.json", "w") as outfile:
308
+ outfile.write(json_object)
309
+ try:
310
+ api.upload_file(
311
+ path_or_fileobj="tmp1.json",
312
+ path_in_repo=main_nodes.split('main/',1)[1],
313
+ repo_id=main_nodes.split('datasets/',1)[1].split('/raw',1)[0],
314
+ token=token_self,
315
+ repo_type="dataset",
316
+ )
317
+ os.remove("tmp1.json")
318
+
319
+ except Exception as e:
320
+ pass
321
+
322
+ with gr.Blocks() as bc:
323
+ with gr.Row(visible=True) as invalid:
324
+ pass_box = gr.Textbox()
325
+ pass_btn = gr.Button()
326
+
327
+ with gr.Box(visible=False) as valida:
328
+ gr.Markdown("""<h1><center>Blockchain Simulator<br><h3>(Transactions have no value)<br><h4>Chain will reset at 20 blocks""")
329
+ blockchain = gr.State()
330
+ with gr.Row():
331
+ with gr.Column():
332
+ with gr.Accordion(label="Load",open=False):
333
+ with gr.Row():
334
+ chain_repo=gr.Textbox(label="repo/name")
335
+ chain_n=gr.Textbox(label="Chain file")
336
+ with gr.Row():
337
+ in_chain_btn=gr.Button("Load Chain")
338
+ create_bc = gr.Button("Create New Blockchain")
339
+ send=gr.Textbox(label="Sender")
340
+ rec=gr.Textbox(label="Recipient")
341
+ am=gr.Textbox(label="Amount")
342
+ send_trans=gr.Button("Post Transaction")
343
+ mine_b = gr.Button("Mine Block")
344
+ check = gr.Button("Check Chain")
345
+ check_all = gr.Button("Check All")
346
+ with gr.Column():
347
+ block_text = gr.Textbox()
348
+ trans_data = gr.Dataframe()
349
+ json_out = gr.JSON()
350
+ chain_json = gr.JSON()
351
+ with gr.Accordion("Nodes", open=False):
352
+ with gr.Row():
353
+ this_space=gr.Textbox(label="This Repo/Space")
354
+ with gr.Row():
355
+ node_repo=gr.Textbox(label="Node Repo")
356
+ node_space=gr.Textbox(label="Node Space")
357
+ node_file=gr.Textbox(label="Node File")
358
+ node_add=gr.Button("Add Node")
359
+
360
+ node_add.click(add_node,[this_space,node_repo,node_space,node_file],block_text)
361
+ pass_btn.click(checkp,pass_box,[invalid,valida])
362
+ in_chain_btn.click(get_chain,[chain_repo,chain_n],[chain_json,block_text])
363
+ create_bc.click(create_chain,[chain_n],[block_text,json_out,chain_json])
364
+ check.click(valid,None,block_text)
365
+ check_all.click(sort_valid,None,block_text)
366
+
367
+ send_trans.click(bc_transactions,[send,rec,am],[trans_data,block_text,send,rec,am])
368
+ mine_b.click(mine_block,[chain_repo,chain_n],[json_out,chain_json,trans_data,block_text])
369
+
370
+
371
+ bc.launch(enable_queue=False)