Omnibus commited on
Commit
06fd40f
·
1 Parent(s): 7014d23

Create mychain.py

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