Omnibus commited on
Commit
b524fb3
·
1 Parent(s): 769f5da

Create mychain.py

Browse files
Files changed (1) hide show
  1. mychain.py +639 -0
mychain.py ADDED
@@ -0,0 +1,639 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import hashlib
4
+ import datetime
5
+ from huggingface_hub import (upload_file,HfApi)
6
+
7
+ token_self = os.environ['HF_TOKEN']
8
+ api = HfApi(token=token_self)
9
+ chain_d="chain1.json"
10
+ chain_t="trans1.json"
11
+
12
+
13
+ class Blockchain:
14
+
15
+ def __init__(self,chain_load,load=None,create=None):
16
+ global main_chain
17
+ main_chain=chain_load
18
+
19
+ self.pending_transactions = []
20
+ self.trans_data_out = []
21
+ if load == None or load=="":
22
+ self.chain = []
23
+ self.create_block(proof=1, previous_hash='0',chain_n=create)
24
+ elif load != None and load !="":
25
+ #r = requests.get(load)
26
+ lod = json.loads(load)
27
+ self.chain = lod
28
+ def reset(self,create=None):
29
+ self.chain = []
30
+ self.pending_transactions = []
31
+ self.create_block(proof=1, previous_hash='0',chain_n=create)
32
+ def create_block(self, proof, previous_hash,chain_r=None,chain_n=None):
33
+ if chain_r=="" or chain_r==None:
34
+ chain_r=f"{main_chain.split('datasets/',1)[1].split('/raw',1)[0]}"
35
+ if chain_n !="" and chain_n !=None:
36
+ chain_n = f"{main_chain.split('main/',1)[1]}{chain_n}"
37
+ if chain_n=="" or chain_n==None:
38
+ chain_n=f"{main_chain.split('main/',1)[1]}{chain_d}"
39
+ block = {'index': len(self.chain) + 1,
40
+ 'timestamp': str(datetime.datetime.now()),
41
+ 'transactions': self.pending_transactions,
42
+ 'proof': proof,
43
+ 'previous_hash': previous_hash}
44
+ if self.block_valid(block) == True:
45
+ self.trans_data_out =[]
46
+
47
+ self.pending_transactions = []
48
+ self.chain.append(block)
49
+ json_object = json.dumps(self.chain, indent=4)
50
+ with open("tmp.json", "w") as outfile:
51
+ outfile.write(json_object)
52
+ outfile.close()
53
+ try:
54
+ api.upload_file(
55
+ path_or_fileobj="tmp.json",
56
+ path_in_repo=chain_n,
57
+ repo_id=chain_r,
58
+ token=token_self,
59
+ repo_type="dataset",
60
+ )
61
+ os.remove("tmp.json")
62
+ #MyChainTrans.reset(self,create=chain_n)
63
+ except Exception as e:
64
+ print(e)
65
+ pass
66
+ return block
67
+ else:
68
+ block = {"Not Valid"}
69
+ print("not Valid")
70
+ return block
71
+ def print_previous_block(self):
72
+ return self.chain[-1]
73
+ def new_transaction(self, block):
74
+ print (f'block::{block}')
75
+ trans_data = {
76
+ 'name': block[0]['name'],
77
+ 'recipient': block[0]['recipient'],
78
+ 'amount': block[0]['amount'],
79
+ 'balance': block[0]['balance'],
80
+ 'index': block[0]['index'],
81
+ 'timestamp': block[0]['timestamp'],
82
+ 'proof': block[0]['proof'],
83
+ 'previous_hash': block[0]['previous_hash']
84
+ }
85
+ self.trans_data_out.append(trans_data)
86
+ self.pending_transactions.append(block)
87
+ def proof_of_work(self, previous_proof):
88
+ new_proof = 1
89
+ check_proof = False
90
+ while check_proof is False:
91
+ hash_operation = hashlib.sha256(
92
+ str(new_proof**2 - previous_proof**2).encode()).hexdigest()
93
+ if hash_operation[:5] == '00000':
94
+ check_proof = True
95
+ else:
96
+ new_proof += 1
97
+ return new_proof
98
+
99
+ def hash(self, block):
100
+ encoded_block = json.dumps(block, sort_keys=True).encode()
101
+ return hashlib.sha256(encoded_block).hexdigest()
102
+ def block_valid(self, block):
103
+ print (block)
104
+ #prev_block=len(self.chain)
105
+ if len(self.chain) > 0:
106
+ prev_block = len(self.chain)-1
107
+ previous_block = self.chain[prev_block]
108
+ print (previous_block)
109
+ out=True
110
+ ind=None
111
+ mes=None
112
+ if block['previous_hash'] != self.hash(previous_block):
113
+ out=False
114
+ #ind=block_index
115
+ mes='Hash'
116
+
117
+ previous_proof = previous_block['proof']
118
+ proof = block['proof']
119
+ hash_operation = hashlib.sha256(
120
+ str(proof**2 - previous_proof**2).encode()).hexdigest()
121
+
122
+ if hash_operation[:5] != '00000':
123
+ out=False
124
+ #ind=block_index+1
125
+ mes='Proof'
126
+ previous_block = block
127
+ else:
128
+ out = True
129
+ return out
130
+
131
+ def chain_valid(self, chain):
132
+ previous_block = chain[0]
133
+ block_index = 1
134
+ out=True
135
+ ind=None
136
+ mes=None
137
+ while block_index < len(chain):
138
+ block = chain[block_index]
139
+ if block['previous_hash'] != self.hash(previous_block):
140
+ out=False
141
+ ind=block_index
142
+ mes='Hash'
143
+ break
144
+
145
+ previous_proof = previous_block['proof']
146
+ proof = block['proof']
147
+ hash_operation = hashlib.sha256(
148
+ str(proof**2 - previous_proof**2).encode()).hexdigest()
149
+
150
+ if hash_operation[:5] != '00000':
151
+ out=False
152
+ ind=block_index+1
153
+ mes='Proof'
154
+ break
155
+ previous_block = block
156
+ block_index += 1
157
+
158
+ return out, ind, mes
159
+
160
+ class MyChainSend:
161
+
162
+ def __init__(self,chain_load,load=None,create=None):
163
+ global main_chain_send
164
+ main_chain_send=chain_load
165
+
166
+ self.pending_transactions = []
167
+ if load == None or load=="":
168
+ self.chain = []
169
+ self.create_block(balance=0,proof=1, previous_hash='0',chain_n=create)
170
+ elif load != None and load !="":
171
+ #r = requests.get(load)
172
+ lod = json.loads(load)
173
+ self.chain = lod
174
+ def reset(self,create=None):
175
+ self.chain = []
176
+ self.pending_transactions = []
177
+ self.create_block(proof=1, previous_hash='0',chain_n=create)
178
+ def create_block(self, balance, proof, previous_hash,prev_block=None,chain_r=None,chain_n=None):
179
+ if chain_r=="" or chain_r==None:
180
+ chain_r=f"{main_chain_send.split('datasets/',1)[1].split('/raw',1)[0]}"
181
+ if chain_n !="" and chain_n !=None:
182
+ chain_n = f"{main_chain_send.split('main/',1)[1]}{chain_n}.json"
183
+ if chain_n=="" or chain_n==None:
184
+ chain_n="error_send.json"
185
+ #prev_block = len(blockchain.chain)-1
186
+ #previous_block = blockchain.chain[prev_block]
187
+ block = {'index': len(self.chain) + 1,
188
+ 'timestamp': str(datetime.datetime.now()),
189
+ 'block': prev_block,
190
+ 'transactions': self.pending_transactions,
191
+ 'balance': balance,
192
+ 'proof': proof,
193
+ 'previous_hash': previous_hash}
194
+ if self.block_valid(block) == True:
195
+
196
+ self.pending_transactions = []
197
+ self.chain.append(block)
198
+ json_object = json.dumps(self.chain, indent=4)
199
+ with open("tmp_send.json", "w") as outfile:
200
+ outfile.write(json_object)
201
+ outfile.close()
202
+ try:
203
+ api.upload_file(
204
+ path_or_fileobj="tmp_send.json",
205
+ path_in_repo=chain_n,
206
+ repo_id=chain_r,
207
+ token=token_self,
208
+ repo_type="dataset",
209
+ )
210
+ os.remove("tmp_send.json")
211
+
212
+ except Exception as e:
213
+ print(e)
214
+ pass
215
+ return block
216
+ else:
217
+ block = {"Not Valid"}
218
+ print("not Valid")
219
+ return block
220
+ def print_previous_block(self):
221
+ return self.chain[-1]
222
+ def new_transaction(self, sender, recipient, amount, balance):
223
+ transaction = {
224
+ 'sender': sender,
225
+ 'recipient': recipient,
226
+ 'amount': amount,
227
+ 'balance': balance
228
+ }
229
+ self.pending_transactions.append(transaction)
230
+ def proof_of_work(self, previous_proof):
231
+ new_proof = 1
232
+ check_proof = False
233
+ while check_proof is False:
234
+ hash_operation = hashlib.sha256(
235
+ str(new_proof**2 - previous_proof**2).encode()).hexdigest()
236
+ if hash_operation[:5] == '00000':
237
+ check_proof = True
238
+ else:
239
+ new_proof += 1
240
+ return new_proof
241
+
242
+ def hash(self, block):
243
+ encoded_block = json.dumps(block, sort_keys=True).encode()
244
+ return hashlib.sha256(encoded_block).hexdigest()
245
+ def block_valid(self, block):
246
+ #print (block)
247
+ #prev_block=len(self.chain)
248
+ if len(self.chain) > 0:
249
+ prev_block = len(self.chain)-1
250
+ previous_block = self.chain[prev_block]
251
+ #print (previous_block)
252
+ out=True
253
+ ind=None
254
+ mes=None
255
+ if block['previous_hash'] != self.hash(previous_block):
256
+ out=False
257
+ #ind=block_index
258
+ mes='Hash'
259
+
260
+ previous_proof = previous_block['proof']
261
+ proof = block['proof']
262
+ hash_operation = hashlib.sha256(
263
+ str(proof**2 - previous_proof**2).encode()).hexdigest()
264
+
265
+ if hash_operation[:5] != '00000':
266
+ out=False
267
+ #ind=block_index+1
268
+ mes='Proof'
269
+ previous_block = block
270
+ else:
271
+ out = True
272
+ return out
273
+
274
+ def chain_valid(self, chain):
275
+
276
+ previous_block = chain[0]
277
+ block_index = 1
278
+ out=True
279
+ ind=None
280
+ mes=None
281
+ while block_index < len(chain):
282
+ block = chain[block_index]
283
+ if block['previous_hash'] != self.hash(previous_block):
284
+ out=False
285
+ ind=block_index
286
+ mes='Hash'
287
+ break
288
+
289
+ previous_proof = previous_block['proof']
290
+ proof = block['proof']
291
+ hash_operation = hashlib.sha256(
292
+ str(proof**2 - previous_proof**2).encode()).hexdigest()
293
+
294
+ if hash_operation[:5] != '00000':
295
+ out=False
296
+ ind=block_index+1
297
+ mes='Proof'
298
+ break
299
+ previous_block = block
300
+ block_index += 1
301
+
302
+ return out, ind, mes
303
+
304
+ def deep_valid_send(self, chain1,chain2):
305
+
306
+ block_index = 1
307
+ out=True
308
+ ind=None
309
+ mes=None
310
+ while block_index < len(chain1):
311
+ block = chain1[block_index]
312
+ block_ind = block['block']
313
+ block2 = chain2[block_ind]
314
+
315
+ check1 = {
316
+ 'timestamp':block['timestamp'],
317
+ 'recipient':block['transactions']['recipient'],
318
+ 'amount':block['transactions']['amount'],
319
+ 'balance':block['transactions']['balance'],
320
+ 'balance2':block['balance'],
321
+ 'proof':block['proof'],
322
+ 'previous_hash':block['previous_hash']
323
+ }
324
+ zz=1
325
+ while zz < len(block2[transactions]):
326
+
327
+ check2 = {
328
+ 'timestamp':block2['timestamp'],
329
+ 'sender':block2['transactions'][zz][0]['name'],
330
+ 'recipient':block2['transactions'][zz][0]['recipient'],
331
+ 'amount':block2['transactions'][zz][0]['amount'],
332
+ 'balance':block2['transactions'][zz][0]['balance'],
333
+ 'balance2':block2['transactions'][zz][0]['balance'],
334
+ 'proof':block2['proof'],
335
+ 'previous_hash':block2['previous_hash']
336
+ }
337
+ zz+=1
338
+
339
+
340
+ if self.hash(check1) != self.hash(check2):
341
+ out=False
342
+ ind=block_index
343
+ mes='Hash'
344
+ break
345
+
346
+ if out == False:
347
+ break
348
+ block_index += 1
349
+
350
+ return out, ind, mes
351
+
352
+
353
+
354
+ class MyChainRec:
355
+
356
+ def __init__(self,chain_load, load=None,create=None):
357
+ global main_chain_rec
358
+ main_chain_rec=chain_load
359
+
360
+ self.pending_transactions = []
361
+ if load == None or load=="":
362
+ self.chain = []
363
+ self.create_block(balance=0, proof=1, previous_hash='0',chain_n=create)
364
+ if load != None and load !="":
365
+ #r = requests.get(load)
366
+ lod = json.loads(load)
367
+ self.chain = lod
368
+ def reset(self,create=None):
369
+ self.chain = []
370
+ self.pending_transactions = []
371
+ self.create_block(proof=1, previous_hash='0',chain_n=create)
372
+ def create_block(self, balance, proof, previous_hash, prev_block=None,chain_r=None,chain_n=None):
373
+ if chain_r=="" or chain_r==None:
374
+ chain_r=f"{main_chain_rec.split('datasets/',1)[1].split('/raw',1)[0]}"
375
+ if chain_n !="" and chain_n !=None:
376
+ chain_n = f"{main_chain_rec.split('main/',1)[1]}{chain_n}.json"
377
+ if chain_n=="" or chain_n==None:
378
+ chain_n="error_rec.json"
379
+ #prev_block = len(blockchain.chain)-1
380
+ #previous_block = blockchain.chain[prev_block]
381
+ block = {'index': len(self.chain) + 1,
382
+ 'timestamp': str(datetime.datetime.now()),
383
+ 'block': prev_block,
384
+ 'transactions': self.pending_transactions,
385
+ 'balance': balance,
386
+ 'proof': proof,
387
+ 'previous_hash': previous_hash}
388
+ if self.block_valid(block) == True:
389
+ self.pending_transactions = []
390
+ self.chain.append(block)
391
+ json_object = json.dumps(self.chain, indent=4)
392
+ with open("tmp_rec.json", "w") as outfile:
393
+ outfile.write(json_object)
394
+ outfile.close()
395
+ try:
396
+ api.upload_file(
397
+ path_or_fileobj="tmp_rec.json",
398
+ path_in_repo=chain_n,
399
+ repo_id=chain_r,
400
+ token=token_self,
401
+ repo_type="dataset",
402
+ )
403
+ os.remove("tmp_rec.json")
404
+
405
+ except Exception as e:
406
+ print(e)
407
+ pass
408
+ return block
409
+ else:
410
+ block = {"Not Valid"}
411
+ print("not Valid")
412
+ return block
413
+ def print_previous_block(self):
414
+ return self.chain[-1]
415
+ def new_transaction(self, sender, recipient, amount, balance):
416
+ transaction = {
417
+ 'sender': sender,
418
+ 'recipient': recipient,
419
+ 'amount': amount,
420
+ 'balance': balance
421
+ }
422
+ self.pending_transactions.append(transaction)
423
+ def proof_of_work(self, previous_proof):
424
+ new_proof = 1
425
+ check_proof = False
426
+ while check_proof is False:
427
+ hash_operation = hashlib.sha256(
428
+ str(new_proof**2 - previous_proof**2).encode()).hexdigest()
429
+ if hash_operation[:5] == '00000':
430
+ check_proof = True
431
+ else:
432
+ new_proof += 1
433
+ return new_proof
434
+
435
+ def hash(self, block):
436
+ encoded_block = json.dumps(block, sort_keys=True).encode()
437
+ return hashlib.sha256(encoded_block).hexdigest()
438
+ def block_valid(self, block):
439
+ print (block)
440
+ #prev_block=len(self.chain)
441
+ if len(self.chain) > 0:
442
+ prev_block = len(self.chain)-1
443
+ previous_block = self.chain[prev_block]
444
+ print (previous_block)
445
+ out=True
446
+ ind=None
447
+ mes=None
448
+ if block['previous_hash'] != self.hash(previous_block):
449
+ out=False
450
+ #ind=block_index
451
+ mes='Hash'
452
+
453
+ previous_proof = previous_block['proof']
454
+ proof = block['proof']
455
+ hash_operation = hashlib.sha256(
456
+ str(proof**2 - previous_proof**2).encode()).hexdigest()
457
+
458
+ if hash_operation[:5] != '00000':
459
+ out=False
460
+ #ind=block_index+1
461
+ mes='Proof'
462
+ previous_block = block
463
+ else:
464
+ out = True
465
+ return out
466
+
467
+ def chain_valid(self, chain):
468
+ previous_block = chain[0]
469
+ block_index = 1
470
+ out=True
471
+ ind=None
472
+ mes=None
473
+ while block_index < len(chain):
474
+ block = chain[block_index]
475
+ if block['previous_hash'] != self.hash(previous_block):
476
+ out=False
477
+ ind=block_index
478
+ mes='Hash'
479
+ break
480
+
481
+ previous_proof = previous_block['proof']
482
+ proof = block['proof']
483
+ hash_operation = hashlib.sha256(
484
+ str(proof**2 - previous_proof**2).encode()).hexdigest()
485
+
486
+ if hash_operation[:5] != '00000':
487
+ out=False
488
+ ind=block_index+1
489
+ mes='Proof'
490
+ break
491
+ previous_block = block
492
+ block_index += 1
493
+
494
+ return out, ind, mes
495
+
496
+
497
+ class MyChainTrans:
498
+
499
+ def __init__(self,chain_load,load=None,create=None):
500
+ global main_chain_trans
501
+ main_chain_trans=chain_load
502
+
503
+ self.pending_transactions = []
504
+ if load == None or load=="":
505
+ self.chain = []
506
+ self.create_block(balance=0,proof=1, previous_hash='0',chain_n=create)
507
+ elif load != None and load !="":
508
+ #r = requests.get(load)
509
+ lod = json.loads(load)
510
+ self.chain = lod
511
+ def reset(self,create=None):
512
+ self.chain = []
513
+ self.pending_transactions = []
514
+ self.create_block(balance=0, proof=1, previous_hash='0',chain_n=create)
515
+ def create_block(self,balance, proof, previous_hash,prev_block=None,chain_r=None,chain_n=None):
516
+ if chain_r=="" or chain_r==None:
517
+ chain_r=f"{main_chain_trans.split('datasets/',1)[1].split('/raw',1)[0]}"
518
+ if chain_n !="" and chain_n !=None:
519
+ chain_n = f"{main_chain_trans.split('main/',1)[1]}{chain_t}"
520
+ if chain_n=="" or chain_n==None:
521
+ chain_n="error_send.json"
522
+ #prev_block = len(blockchain.chain)-1
523
+ #previous_block = blockchain.chain[prev_block]
524
+ block = {'index': len(self.chain) + 1,
525
+ 'timestamp': str(datetime.datetime.now()),
526
+ #'block': len(Blockchain.chain)+1,
527
+ 'transactions': self.pending_transactions,
528
+ 'balance': balance,
529
+ 'proof': proof,
530
+ 'previous_hash': previous_hash}
531
+ if self.block_valid(block) == True:
532
+
533
+ self.pending_transactions = []
534
+ self.chain.append(block)
535
+ json_object = json.dumps(self.chain, indent=4)
536
+ with open("tmp_trans.json", "w") as outfile:
537
+ outfile.write(json_object)
538
+ outfile.close()
539
+ try:
540
+ api.upload_file(
541
+ path_or_fileobj="tmp_trans.json",
542
+ path_in_repo=chain_n,
543
+ repo_id=chain_r,
544
+ token=token_self,
545
+ repo_type="dataset",
546
+ )
547
+ os.remove("tmp_trans.json")
548
+
549
+ except Exception as e:
550
+ print(e)
551
+ pass
552
+ return block
553
+ else:
554
+ block = {"Not Valid"}
555
+ print("not Valid")
556
+ return block
557
+ def print_previous_block(self):
558
+ return self.chain[-1]
559
+ def new_transaction(self, sender, recipient, amount, balance):
560
+ transaction = {
561
+ 'sender': sender,
562
+ 'recipient': recipient,
563
+ 'amount': amount,
564
+ 'balance': balance
565
+ }
566
+ self.pending_transactions.append(transaction)
567
+ def proof_of_work(self, previous_proof):
568
+ new_proof = 1
569
+ check_proof = False
570
+ while check_proof is False:
571
+ hash_operation = hashlib.sha256(
572
+ str(new_proof**2 - previous_proof**2).encode()).hexdigest()
573
+ if hash_operation[:5] == '00000':
574
+ check_proof = True
575
+ else:
576
+ new_proof += 1
577
+ return new_proof
578
+
579
+ def hash(self, block):
580
+ encoded_block = json.dumps(block, sort_keys=True).encode()
581
+ return hashlib.sha256(encoded_block).hexdigest()
582
+ def block_valid(self, block):
583
+ #print (block)
584
+ #prev_block=len(self.chain)
585
+ if len(self.chain) > 0:
586
+ prev_block = len(self.chain)-1
587
+ previous_block = self.chain[prev_block]
588
+ #print (previous_block)
589
+ out=True
590
+ ind=None
591
+ mes=None
592
+ if block['previous_hash'] != self.hash(previous_block):
593
+ out=False
594
+ #ind=block_index
595
+ mes='Hash'
596
+
597
+ previous_proof = previous_block['proof']
598
+ proof = block['proof']
599
+ hash_operation = hashlib.sha256(
600
+ str(proof**2 - previous_proof**2).encode()).hexdigest()
601
+
602
+ if hash_operation[:5] != '00000':
603
+ out=False
604
+ #ind=block_index+1
605
+ mes='Proof'
606
+ previous_block = block
607
+ else:
608
+ out = True
609
+ return out
610
+
611
+ def chain_valid(self, chain):
612
+
613
+ previous_block = chain[0]
614
+ block_index = 1
615
+ out=True
616
+ ind=None
617
+ mes=None
618
+ while block_index < len(chain):
619
+ block = chain[block_index]
620
+ if block['previous_hash'] != self.hash(previous_block):
621
+ out=False
622
+ ind=block_index
623
+ mes='Hash'
624
+ break
625
+
626
+ previous_proof = previous_block['proof']
627
+ proof = block['proof']
628
+ hash_operation = hashlib.sha256(
629
+ str(proof**2 - previous_proof**2).encode()).hexdigest()
630
+
631
+ if hash_operation[:5] != '00000':
632
+ out=False
633
+ ind=block_index+1
634
+ mes='Proof'
635
+ break
636
+ previous_block = block
637
+ block_index += 1
638
+
639
+ return out, ind, mes