-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.py
More file actions
63 lines (45 loc) · 1.56 KB
/
Copy pathblock.py
File metadata and controls
63 lines (45 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import json
import os
import hashlib
BLOCKCHAIN_DIR = "blockchain/"
def get_hash(previous_block):
with open (BLOCKCHAIN_DIR+ previous_block, 'rb') as f:
content = f.read()
return hashlib.md5(content).hexdigest()
def check_integrity():
##getting hash of each block
#start from second block as first one has no hash
files = sorted(os.listdir(BLOCKCHAIN_DIR), key=lambda x: int(x))
results = []
for file in files[1:3]:
with open(BLOCKCHAIN_DIR + file) as f:
block = json.load(f)
prev_hash = block.get('previous_block').get(hash)
prev_filename = block.get('previous_block').get('filename')
actual_hash = get_hash(prev_filename)
if prev_hash == actual_hash:
res = 'ok'
else:
red = 'not ok'
print(f'Block {prev_filename}: {res}')
results.append({'block': prev_filename, 'result':res})
return results
def write_block(data, timestamp):
blocks_count = len( os.listdir(BLOCKCHAIN_DIR))
previous_block = str(blocks_count)
data_of_block = {
"data":data,
"timestamp":timestamp,
"previous_block":{
"hash":get_hash(previous_block),
"filename":previous_block
}
}
current_block = BLOCKCHAIN_DIR + str(blocks_count) +1
with open(current_block, 'w') as f:
json.dump(data_of_block, f, indent=3, ensure_ascii=False)
f.write('/n')
def main():
write_block(data='h', timestamp='22')
if __name__=='__main__':
main()