Omnibus commited on
Commit
1848074
·
1 Parent(s): 733bdca

Create mychain.py

Browse files
Files changed (1) hide show
  1. mychain.py +145 -0
mychain.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ mport 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
+
10
+ class MyChain:
11
+
12
+ def __init__(self,chain_load,load=None,create=None):
13
+ global main_chain
14
+ main_chain=chain_load
15
+
16
+ self.pending_transactions = []
17
+ if load == None or load=="":
18
+ self.chain = []
19
+ self.create_block(proof=1, previous_hash='0',chain_n=create)
20
+ elif load != None and load !="":
21
+ #r = requests.get(load)
22
+ lod = json.loads(load)
23
+ self.chain = lod
24
+ def reset(self,create=None):
25
+ self.chain = []
26
+ self.pending_transactions = []
27
+ self.create_block(proof=1, previous_hash='0',chain_n=create)
28
+ def create_block(self, proof, previous_hash,chain_r=None,chain_n=None):
29
+ if chain_r=="" or chain_r==None:
30
+ chain_r=f"{main_chain.split('datasets/',1)[1].split('/raw',1)[0]}"
31
+ if chain_n !="" and chain_n !=None:
32
+ chain_n = f"{main_chain.split('main/',1)[1]}{chain_n}"
33
+ if chain_n=="" or chain_n==None:
34
+ chain_n=chain_d
35
+ block = {'index': len(self.chain) + 1,
36
+ 'timestamp': str(datetime.datetime.now()),
37
+ 'transactions': self.pending_transactions,
38
+ 'proof': proof,
39
+ 'previous_hash': previous_hash}
40
+ if self.block_valid(block) == True:
41
+
42
+ self.pending_transactions = []
43
+ self.chain.append(block)
44
+ json_object = json.dumps(self.chain, indent=4)
45
+ with open("tmp.json", "w") as outfile:
46
+ outfile.write(json_object)
47
+ try:
48
+ api.upload_file(
49
+ path_or_fileobj="tmp.json",
50
+ path_in_repo=chain_n,
51
+ repo_id=chain_r,
52
+ token=token_self,
53
+ repo_type="dataset",
54
+ )
55
+ os.remove("tmp.json")
56
+
57
+ except Exception as e:
58
+ print(e)
59
+ pass
60
+ return block
61
+ else:
62
+ block = {"Not Valid"}
63
+ print("not Valid")
64
+ return block
65
+ def print_previous_block(self):
66
+ return self.chain[-1]
67
+ def new_transaction(self, sender, recipient, amount):
68
+ transaction = {
69
+ 'sender': sender,
70
+ 'recipient': recipient,
71
+ 'amount': amount
72
+ }
73
+ self.pending_transactions.append(transaction)
74
+ def proof_of_work(self, previous_proof):
75
+ new_proof = 1
76
+ check_proof = False
77
+ while check_proof is False:
78
+ hash_operation = hashlib.sha256(
79
+ str(new_proof**2 - previous_proof**2).encode()).hexdigest()
80
+ if hash_operation[:5] == '00000':
81
+ check_proof = True
82
+ else:
83
+ new_proof += 1
84
+ return new_proof
85
+
86
+ def hash(self, block):
87
+ encoded_block = json.dumps(block, sort_keys=True).encode()
88
+ return hashlib.sha256(encoded_block).hexdigest()
89
+ def block_valid(self, block):
90
+ print (block)
91
+ #prev_block=len(self.chain)
92
+ if len(self.chain) > 0:
93
+ prev_block = len(self.chain)-1
94
+ previous_block = self.chain[prev_block]
95
+ print (previous_block)
96
+ out=True
97
+ ind=None
98
+ mes=None
99
+ if block['previous_hash'] != self.hash(previous_block):
100
+ out=False
101
+ #ind=block_index
102
+ mes='Hash'
103
+
104
+ previous_proof = previous_block['proof']
105
+ proof = block['proof']
106
+ hash_operation = hashlib.sha256(
107
+ str(proof**2 - previous_proof**2).encode()).hexdigest()
108
+
109
+ if hash_operation[:5] != '00000':
110
+ out=False
111
+ #ind=block_index+1
112
+ mes='Proof'
113
+ previous_block = block
114
+ else:
115
+ out = True
116
+ return out
117
+
118
+ def chain_valid(self, chain):
119
+ previous_block = chain[0]
120
+ block_index = 1
121
+ out=True
122
+ ind=None
123
+ mes=None
124
+ while block_index < len(chain):
125
+ block = chain[block_index]
126
+ if block['previous_hash'] != self.hash(previous_block):
127
+ out=False
128
+ ind=block_index
129
+ mes='Hash'
130
+ break
131
+
132
+ previous_proof = previous_block['proof']
133
+ proof = block['proof']
134
+ hash_operation = hashlib.sha256(
135
+ str(proof**2 - previous_proof**2).encode()).hexdigest()
136
+
137
+ if hash_operation[:5] != '00000':
138
+ out=False
139
+ ind=block_index+1
140
+ mes='Proof'
141
+ break
142
+ previous_block = block
143
+ block_index += 1
144
+
145
+ return out, ind, mes