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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 MyChainSend:
|
14 |
+
|
15 |
+
def __init__(self,chain_load,load=None,create=None):
|
16 |
+
global main_chain_send
|
17 |
+
main_chain_send=chain_load
|
18 |
+
|
19 |
+
self.pending_transactions = []
|
20 |
+
if load == None or load=="":
|
21 |
+
self.chain = []
|
22 |
+
self.create_block(proof=1, previous_hash='0',chain_n=create)
|
23 |
+
elif load != None and load !="":
|
24 |
+
#r = requests.get(load)
|
25 |
+
lod = json.loads(load)
|
26 |
+
self.chain = lod
|
27 |
+
def reset(self,create=None):
|
28 |
+
self.chain = []
|
29 |
+
self.pending_transactions = []
|
30 |
+
self.create_block(proof=1, previous_hash='0',chain_n=create)
|
31 |
+
def create_block(self, proof, previous_hash,chain_r=None,chain_n=None):
|
32 |
+
if chain_r=="" or chain_r==None:
|
33 |
+
chain_r=f"{main_chain_send.split('datasets/',1)[1].split('/raw',1)[0]}"
|
34 |
+
if chain_n !="" and chain_n !=None:
|
35 |
+
chain_n = f"{main_chain_send.split('main/',1)[1]}{chain_n}.json"
|
36 |
+
if chain_n=="" or chain_n==None:
|
37 |
+
chain_n="error_send.json"
|
38 |
+
#prev_block = len(blockchain.chain)-1
|
39 |
+
#previous_block = blockchain.chain[prev_block]
|
40 |
+
block = {'index': len(self.chain) + 1,
|
41 |
+
'timestamp': str(datetime.datetime.now()),
|
42 |
+
'message': self.pending_transactions,
|
43 |
+
'proof': proof,
|
44 |
+
'previous_hash': previous_hash}
|
45 |
+
if self.block_valid(block) == True:
|
46 |
+
|
47 |
+
self.pending_transactions = []
|
48 |
+
self.chain.append(block)
|
49 |
+
json_object = json.dumps(self.chain, indent=4)
|
50 |
+
with open("tmp_send.json", "w") as outfile:
|
51 |
+
outfile.write(json_object)
|
52 |
+
outfile.close()
|
53 |
+
try:
|
54 |
+
api.upload_file(
|
55 |
+
path_or_fileobj="tmp_send.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_send.json")
|
62 |
+
|
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, sender, recipient, message):
|
74 |
+
transaction = {
|
75 |
+
'sender': sender,
|
76 |
+
'recipient': recipient,
|
77 |
+
'message': message,
|
78 |
+
}
|
79 |
+
self.pending_transactions.append(transaction)
|
80 |
+
def proof_of_work(self, previous_proof):
|
81 |
+
new_proof = 1
|
82 |
+
check_proof = False
|
83 |
+
while check_proof is False:
|
84 |
+
hash_operation = hashlib.sha256(
|
85 |
+
str(new_proof**2 - previous_proof**2).encode()).hexdigest()
|
86 |
+
if hash_operation[:5] == '00000':
|
87 |
+
check_proof = True
|
88 |
+
else:
|
89 |
+
new_proof += 1
|
90 |
+
return new_proof
|
91 |
+
|
92 |
+
def hash(self, block):
|
93 |
+
encoded_block = json.dumps(block, sort_keys=True).encode()
|
94 |
+
return hashlib.sha256(encoded_block).hexdigest()
|
95 |
+
def block_valid(self, block):
|
96 |
+
#print (block)
|
97 |
+
#prev_block=len(self.chain)
|
98 |
+
if len(self.chain) > 0:
|
99 |
+
prev_block = len(self.chain)-1
|
100 |
+
previous_block = self.chain[prev_block]
|
101 |
+
#print (previous_block)
|
102 |
+
out=True
|
103 |
+
ind=None
|
104 |
+
mes=None
|
105 |
+
if block['previous_hash'] != self.hash(previous_block):
|
106 |
+
out=False
|
107 |
+
#ind=block_index
|
108 |
+
mes='Hash'
|
109 |
+
|
110 |
+
previous_proof = previous_block['proof']
|
111 |
+
proof = block['proof']
|
112 |
+
hash_operation = hashlib.sha256(
|
113 |
+
str(proof**2 - previous_proof**2).encode()).hexdigest()
|
114 |
+
|
115 |
+
if hash_operation[:5] != '00000':
|
116 |
+
out=False
|
117 |
+
#ind=block_index+1
|
118 |
+
mes='Proof'
|
119 |
+
previous_block = block
|
120 |
+
else:
|
121 |
+
out = True
|
122 |
+
return out
|
123 |
+
|
124 |
+
def chain_valid(self, chain):
|
125 |
+
|
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 deep_valid_send(self, chain1,chain2):
|
155 |
+
|
156 |
+
block_index = 1
|
157 |
+
out=True
|
158 |
+
ind=None
|
159 |
+
mes=None
|
160 |
+
while block_index < len(chain1):
|
161 |
+
block = chain1[block_index]
|
162 |
+
block_ind = block['block']
|
163 |
+
block2 = chain2[block_ind]
|
164 |
+
|
165 |
+
check1 = {
|
166 |
+
'timestamp':block['timestamp'],
|
167 |
+
'recipient':block['transactions']['recipient'],
|
168 |
+
'amount':block['transactions']['amount'],
|
169 |
+
'balance':block['transactions']['balance'],
|
170 |
+
'balance2':block['balance'],
|
171 |
+
'proof':block['proof'],
|
172 |
+
'previous_hash':block['previous_hash']
|
173 |
+
}
|
174 |
+
zz=1
|
175 |
+
while zz < len(block2[transactions]):
|
176 |
+
|
177 |
+
check2 = {
|
178 |
+
'timestamp':block2['timestamp'],
|
179 |
+
'sender':block2['transactions'][zz][0]['name'],
|
180 |
+
'recipient':block2['transactions'][zz][0]['recipient'],
|
181 |
+
'amount':block2['transactions'][zz][0]['amount'],
|
182 |
+
'balance':block2['transactions'][zz][0]['balance'],
|
183 |
+
'balance2':block2['transactions'][zz][0]['balance'],
|
184 |
+
'proof':block2['proof'],
|
185 |
+
'previous_hash':block2['previous_hash']
|
186 |
+
}
|
187 |
+
zz+=1
|
188 |
+
|
189 |
+
|
190 |
+
if self.hash(check1) != self.hash(check2):
|
191 |
+
out=False
|
192 |
+
ind=block_index
|
193 |
+
mes='Hash'
|
194 |
+
break
|
195 |
+
|
196 |
+
if out == False:
|
197 |
+
break
|
198 |
+
block_index += 1
|
199 |
+
|
200 |
+
return out, ind, mes
|
201 |
+
|
202 |
+
|
203 |
+
|
204 |
+
class MyChainRec:
|
205 |
+
|
206 |
+
def __init__(self,chain_load, load=None,create=None):
|
207 |
+
global main_chain_rec
|
208 |
+
main_chain_rec=chain_load
|
209 |
+
|
210 |
+
self.pending_transactions = []
|
211 |
+
if load == None or load=="":
|
212 |
+
self.chain = []
|
213 |
+
self.create_block(proof=1, previous_hash='0',chain_n=create)
|
214 |
+
if load != None and load !="":
|
215 |
+
#r = requests.get(load)
|
216 |
+
lod = json.loads(load)
|
217 |
+
self.chain = lod
|
218 |
+
def reset(self,create=None):
|
219 |
+
self.chain = []
|
220 |
+
self.pending_transactions = []
|
221 |
+
self.create_block(proof=1, previous_hash='0',chain_n=create)
|
222 |
+
def create_block(self, proof, previous_hash,chain_r=None,chain_n=None):
|
223 |
+
if chain_r=="" or chain_r==None:
|
224 |
+
chain_r=f"{main_chain_rec.split('datasets/',1)[1].split('/raw',1)[0]}"
|
225 |
+
if chain_n !="" and chain_n !=None:
|
226 |
+
chain_n = f"{main_chain_rec.split('main/',1)[1]}{chain_n}.json"
|
227 |
+
if chain_n=="" or chain_n==None:
|
228 |
+
chain_n="error_rec.json"
|
229 |
+
#prev_block = len(blockchain.chain)-1
|
230 |
+
#previous_block = blockchain.chain[prev_block]
|
231 |
+
block = {'index': len(self.chain) + 1,
|
232 |
+
'timestamp': str(datetime.datetime.now()),
|
233 |
+
'message': self.pending_transactions,
|
234 |
+
'proof': proof,
|
235 |
+
'previous_hash': previous_hash}
|
236 |
+
if self.block_valid(block) == True:
|
237 |
+
self.pending_transactions = []
|
238 |
+
self.chain.append(block)
|
239 |
+
json_object = json.dumps(self.chain, indent=4)
|
240 |
+
with open("tmp_rec.json", "w") as outfile:
|
241 |
+
outfile.write(json_object)
|
242 |
+
outfile.close()
|
243 |
+
try:
|
244 |
+
api.upload_file(
|
245 |
+
path_or_fileobj="tmp_rec.json",
|
246 |
+
path_in_repo=chain_n,
|
247 |
+
repo_id=chain_r,
|
248 |
+
token=token_self,
|
249 |
+
repo_type="dataset",
|
250 |
+
)
|
251 |
+
os.remove("tmp_rec.json")
|
252 |
+
|
253 |
+
except Exception as e:
|
254 |
+
print(e)
|
255 |
+
pass
|
256 |
+
return block
|
257 |
+
else:
|
258 |
+
block = {"Not Valid"}
|
259 |
+
print("not Valid")
|
260 |
+
return block
|
261 |
+
def print_previous_block(self):
|
262 |
+
return self.chain[-1]
|
263 |
+
def new_transaction(self, sender, recipient, message):
|
264 |
+
transaction = {
|
265 |
+
'sender': sender,
|
266 |
+
'recipient': recipient,
|
267 |
+
'message': message,
|
268 |
+
}
|
269 |
+
self.pending_transactions.append(transaction)
|
270 |
+
def proof_of_work(self, previous_proof):
|
271 |
+
new_proof = 1
|
272 |
+
check_proof = False
|
273 |
+
while check_proof is False:
|
274 |
+
hash_operation = hashlib.sha256(
|
275 |
+
str(new_proof**2 - previous_proof**2).encode()).hexdigest()
|
276 |
+
if hash_operation[:5] == '00000':
|
277 |
+
check_proof = True
|
278 |
+
else:
|
279 |
+
new_proof += 1
|
280 |
+
return new_proof
|
281 |
+
|
282 |
+
def hash(self, block):
|
283 |
+
encoded_block = json.dumps(block, sort_keys=True).encode()
|
284 |
+
return hashlib.sha256(encoded_block).hexdigest()
|
285 |
+
def block_valid(self, block):
|
286 |
+
print (block)
|
287 |
+
#prev_block=len(self.chain)
|
288 |
+
if len(self.chain) > 0:
|
289 |
+
prev_block = len(self.chain)-1
|
290 |
+
previous_block = self.chain[prev_block]
|
291 |
+
print (previous_block)
|
292 |
+
out=True
|
293 |
+
ind=None
|
294 |
+
mes=None
|
295 |
+
if block['previous_hash'] != self.hash(previous_block):
|
296 |
+
out=False
|
297 |
+
#ind=block_index
|
298 |
+
mes='Hash'
|
299 |
+
|
300 |
+
previous_proof = previous_block['proof']
|
301 |
+
proof = block['proof']
|
302 |
+
hash_operation = hashlib.sha256(
|
303 |
+
str(proof**2 - previous_proof**2).encode()).hexdigest()
|
304 |
+
|
305 |
+
if hash_operation[:5] != '00000':
|
306 |
+
out=False
|
307 |
+
#ind=block_index+1
|
308 |
+
mes='Proof'
|
309 |
+
previous_block = block
|
310 |
+
else:
|
311 |
+
out = True
|
312 |
+
return out
|
313 |
+
|
314 |
+
def chain_valid(self, chain):
|
315 |
+
previous_block = chain[0]
|
316 |
+
block_index = 1
|
317 |
+
out=True
|
318 |
+
ind=None
|
319 |
+
mes=None
|
320 |
+
while block_index < len(chain):
|
321 |
+
block = chain[block_index]
|
322 |
+
if block['previous_hash'] != self.hash(previous_block):
|
323 |
+
out=False
|
324 |
+
ind=block_index
|
325 |
+
mes='Hash'
|
326 |
+
break
|
327 |
+
|
328 |
+
previous_proof = previous_block['proof']
|
329 |
+
proof = block['proof']
|
330 |
+
hash_operation = hashlib.sha256(
|
331 |
+
str(proof**2 - previous_proof**2).encode()).hexdigest()
|
332 |
+
|
333 |
+
if hash_operation[:5] != '00000':
|
334 |
+
out=False
|
335 |
+
ind=block_index+1
|
336 |
+
mes='Proof'
|
337 |
+
break
|
338 |
+
previous_block = block
|
339 |
+
block_index += 1
|
340 |
+
|
341 |
+
return out, ind, mes
|