forked from prakratisingh/MediLink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bchain.py
83 lines (67 loc) · 2.67 KB
/
bchain.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import json
import hashlib
import os
# MedBlockchain class
class MedBlockchain:
# constructor of the class
def __init__(self):
# creating blockchain.json if it do not exists
if not os.path.exists('blockchain.json'):
# opened the file in writing format
with open('blockchain.json', 'w') as file:
# inserting empty json
data = {}
json.dump(data, file)
# function to calculate hash for that particular block
def calculateHash(self,block):
block_string = json.dumps(block, sort_keys=True).encode()
return hashlib.sha256(block_string).hexdigest()
# function to open the json file in reading mode
def loadBlockchain(self):
with open('blockchain.json', 'r') as file:
return json.load(file)
# function to save the new block added against that user
def saveBlockchain(self,blockchain):
with open('blockchain.json', 'w') as file:
json.dump(blockchain, file)
# function to create a block
def createBlock(self,username, medicine, date):
block = {
'medicine': medicine,
'date': date
}
blockchain = self.loadBlockchain()
user_chain = blockchain.get(username, [])
# getting the last block if it exists otherwise None
previous_block = user_chain[-1] if user_chain else None
if previous_block:
block['previous_hash'] = self.calculateHash(previous_block)
else:
block['previous_hash'] = None
# block['hash'] = calculate_hash(block)
user_chain.append(block)
# updating the chain of that user
blockchain[username] = user_chain
self.saveBlockchain(blockchain)
# returning the hash code of the new block
return self.calculateHash(block)
# function to get the chain of that user
def getUserBlocks(self,username):
blockchain = self.loadBlockchain()
return blockchain.get(username, None)
# function to check integrity of the user's chain
def checkIntegrity(self,username,lastHash):
userBlock = self.getUserBlocks(username)
if not userBlock:
return False
j = len(userBlock)
# comparing the hash of the previous block withe current block's previous hash key
while j>0:
if j==len(userBlock):
if lastHash!=self.calculateHash(userBlock[j-1]):
return False
else:
if userBlock[j]['previous_hash'] != self.calculateHash(userBlock[j-1]):
return False
j = j - 1
return True