Omnibus commited on
Commit
5977300
·
1 Parent(s): 18dcb82

Update mychain.py

Browse files
Files changed (1) hide show
  1. mychain.py +143 -0
mychain.py CHANGED
@@ -3,6 +3,7 @@ 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)
@@ -34,6 +35,7 @@ class MyChainSend:
34
  chain_n=chain_d
35
  block = {'index': len(self.chain) + 1,
36
  'timestamp': str(datetime.datetime.now()),
 
37
  'transactions': self.pending_transactions,
38
  'balance': balance,
39
  'proof': proof,
@@ -173,6 +175,7 @@ class MyChainRec:
173
  chain_n=chain_d
174
  block = {'index': len(self.chain) + 1,
175
  'timestamp': str(datetime.datetime.now()),
 
176
  'transactions': self.pending_transactions,
177
  'balance': balance,
178
  'proof': proof,
@@ -255,6 +258,146 @@ class MyChainRec:
255
  out = True
256
  return out
257
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258
  def chain_valid(self, chain):
259
  previous_block = chain[0]
260
  block_index = 1
 
3
  import hashlib
4
  import datetime
5
  from huggingface_hub import (upload_file,HfApi)
6
+ from blockchain import Blockchain as blockchain
7
 
8
  token_self = os.environ['HF_TOKEN']
9
  api = HfApi(token=token_self)
 
35
  chain_n=chain_d
36
  block = {'index': len(self.chain) + 1,
37
  'timestamp': str(datetime.datetime.now()),
38
+ 'block': blockchain.print_previous_block,
39
  'transactions': self.pending_transactions,
40
  'balance': balance,
41
  'proof': proof,
 
175
  chain_n=chain_d
176
  block = {'index': len(self.chain) + 1,
177
  'timestamp': str(datetime.datetime.now()),
178
+ 'block': blockchain.print_previous_block,
179
  'transactions': self.pending_transactions,
180
  'balance': balance,
181
  'proof': proof,
 
258
  out = True
259
  return out
260
 
261
+ def chain_valid(self, chain):
262
+ previous_block = chain[0]
263
+ block_index = 1
264
+ out=True
265
+ ind=None
266
+ mes=None
267
+ while block_index < len(chain):
268
+ block = chain[block_index]
269
+ if block['previous_hash'] != self.hash(previous_block):
270
+ out=False
271
+ ind=block_index
272
+ mes='Hash'
273
+ break
274
+
275
+ previous_proof = previous_block['proof']
276
+ proof = block['proof']
277
+ hash_operation = hashlib.sha256(
278
+ str(proof**2 - previous_proof**2).encode()).hexdigest()
279
+
280
+ if hash_operation[:5] != '00000':
281
+ out=False
282
+ ind=block_index+1
283
+ mes='Proof'
284
+ break
285
+ previous_block = block
286
+ block_index += 1
287
+
288
+ return out, ind, mes
289
+
290
+
291
+ class MyChainTrans:
292
+
293
+ def __init__(self,chain_load,block_trans,load=None,create=None):
294
+ global main_chain
295
+ main_chain=chain_load
296
+
297
+ self.pending_transactions = []
298
+ if load == None or load=="":
299
+ self.chain = []
300
+ self.create_block(balance=0,proof=1, previous_hash='0',chain_n=create)
301
+ elif load != None and load !="":
302
+ #r = requests.get(load)
303
+ lod = json.loads(load)
304
+ self.chain = lod
305
+ def reset(self,create=None):
306
+ self.chain = []
307
+ self.pending_transactions = []
308
+ self.create_block(proof=1, previous_hash='0',chain_n=create)
309
+ def create_block(self, balance, proof, previous_hash,chain_r=None,chain_n=None):
310
+ if chain_r=="" or chain_r==None:
311
+ chain_r=f"{main_chain.split('datasets/',1)[1].split('/raw',1)[0]}"
312
+ if chain_n !="" and chain_n !=None:
313
+ chain_n = f"{main_chain.split('main/',1)[1]}{chain_n}.json"
314
+ if chain_n=="" or chain_n==None:
315
+ chain_n=chain_d
316
+ block = {'block':blockchain.print_previous_block,
317
+ 'index': len(self.chain) + 1,
318
+ 'timestamp': str(datetime.datetime.now()),
319
+ 'transactions': self.pending_transactions,
320
+ 'proof': proof,
321
+ 'previous_hash': previous_hash}
322
+ if self.block_valid(block) == True:
323
+
324
+ self.pending_transactions = []
325
+ self.chain.append(block)
326
+ json_object = json.dumps(self.chain, indent=4)
327
+ with open("tmp.json", "w") as outfile:
328
+ outfile.write(json_object)
329
+ try:
330
+ api.upload_file(
331
+ path_or_fileobj="tmp.json",
332
+ path_in_repo=chain_n,
333
+ repo_id=chain_r,
334
+ token=token_self,
335
+ repo_type="dataset",
336
+ )
337
+ os.remove("tmp.json")
338
+
339
+ except Exception as e:
340
+ print(e)
341
+ pass
342
+ return block
343
+ else:
344
+ block = {"Not Valid"}
345
+ print("not Valid")
346
+ return block
347
+ def print_previous_block(self):
348
+ return self.chain[-1]
349
+ def new_transaction(self, sender, recipient, amount, balance):
350
+ transaction = {
351
+ 'sender': sender,
352
+ 'recipient': recipient,
353
+ 'amount': amount,
354
+ 'balance': balance
355
+ }
356
+ self.pending_transactions.append(transaction)
357
+ def proof_of_work(self, previous_proof):
358
+ new_proof = 1
359
+ check_proof = False
360
+ while check_proof is False:
361
+ hash_operation = hashlib.sha256(
362
+ str(new_proof**2 - previous_proof**2).encode()).hexdigest()
363
+ if hash_operation[:5] == '00000':
364
+ check_proof = True
365
+ else:
366
+ new_proof += 1
367
+ return new_proof
368
+
369
+ def hash(self, block):
370
+ encoded_block = json.dumps(block, sort_keys=True).encode()
371
+ return hashlib.sha256(encoded_block).hexdigest()
372
+ def block_valid(self, block):
373
+ #print (block)
374
+ #prev_block=len(self.chain)
375
+ if len(self.chain) > 0:
376
+ prev_block = len(self.chain)-1
377
+ previous_block = self.chain[prev_block]
378
+ #print (previous_block)
379
+ out=True
380
+ ind=None
381
+ mes=None
382
+ if block['previous_hash'] != self.hash(previous_block):
383
+ out=False
384
+ #ind=block_index
385
+ mes='Hash'
386
+
387
+ previous_proof = previous_block['proof']
388
+ proof = block['proof']
389
+ hash_operation = hashlib.sha256(
390
+ str(proof**2 - previous_proof**2).encode()).hexdigest()
391
+
392
+ if hash_operation[:5] != '00000':
393
+ out=False
394
+ #ind=block_index+1
395
+ mes='Proof'
396
+ previous_block = block
397
+ else:
398
+ out = True
399
+ return out
400
+
401
  def chain_valid(self, chain):
402
  previous_block = chain[0]
403
  block_index = 1