forked from Prod23/SupplyChainManagement_Blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockchain.py
More file actions
207 lines (185 loc) · 7.82 KB
/
Copy pathBlockchain.py
File metadata and controls
207 lines (185 loc) · 7.82 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#Importing libraries.
import hashlib
from datetime import datetime
import json
import random
import qrcode
from merkleTree import MerkleTree
class SupplyChainBlockchain:
def __init__(self):
self.nonce = random.randint(100,999)
self.chain = []
self.unverified_transactions = []
self.current_transactions = []
self.save_current_transactions = []
self.transaction_history = dict()
self.chain = []
self.create_genesis_block(genesis_block=True)
self.participants = dict() # Define participants attribute here
self.deliveries_in_progress = {}
self.disputes = []
self.votes = []
self.digitalSignature = False # Flag to indicate digital signature status
self.delegates = dict()
self.stakers = dict()
self.witnesses = dict()
# Initialize participants
def participant(self, participants):
self.participants.update(participants)
# Add stakers
def add_staker(self, stakers):
self.stakers.update(stakers)
# Create a genesis block for the blockchain
def create_genesis_block(self, genesis_block=True):
if genesis_block:
p_hash = hashlib.sha256(datetime.now().strftime('%Y-%m-%d %H:%M:%S').encode()).hexdigest()
m_root = hashlib.sha256("genesis".encode()).hexdigest()
block = {
'index': len(self.chain),
'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'transactions': None,
'previous_hash': p_hash,
'merkle_root': m_root,
'nonce':self.nonce
}
self.chain.append(block)
return block
# Create a new block in the blockchain
def create_block(self, previous_hash=None, genesis_block=False):
if not genesis_block:
validator = self.dpos_consensus()
else:
validator = None
block = {
'index': len(self.chain) ,
'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'transactions': self.current_transactions,
'validator': validator,
'previous_hash': self.prev_hash(self.chain[-1]),
'merkle_root': self.generate_merkle_root(self.current_transactions),
'nonce':self.nonce
}
self.save_current_transactions.append(self.current_transactions)
self.current_transactions = []
self.deliveries_in_progress = {}
self.chain.append(block)
self.unverified_transactions = []
return block
# Confirm transactions in the blockchain
def confirm_transactions(self):
last_block = self.last_block
transactions = last_block['transactions']
for tx in transactions:
d = tx['distributor']
c = tx['client']
if d in self.deliveries_in_progress and self.deliveries_in_progress[d] == c:
self.confirm_delivery(d, c)
# Confirm delivery of a product
def confirm_delivery(self, distributor, client):
self.deliveries_in_progress.pop(distributor, None)
# Generate the Merkle root of transactions
def generate_merkle_root(self, transactions):
mt = MerkleTree()
for tx in transactions:
tx_string = json.dumps(tx, sort_keys=True)
tx_hex = tx_string.encode('utf-8').hex()
mt.add_leaf(tx_hex)
mt.make_tree()
return mt.generate_merkle_root()
# Implement DPoS consensus mechanism
def dpos_consensus(self):
validators = list(self.witnesses.keys())
selected_validator = random.choice(validators)
return selected_validator
# Add a new transaction to the blockchain
def add_transaction(self, manufacturer, distributor, client, product, amount):
self.digitalSignature = False # Reset digital signature flag
manufacturer_to_distributor = {
'manufacturer': manufacturer,
'distributor': distributor,
'product': product,
'amount': amount,
'timestamps': {
"sent_by_manufacturer": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
"received_by_distributor": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
}
}
distributor_to_client = {
'distributor': distributor,
'client': client,
'product': product,
'amount': amount,
'timestamps': {
"received_by_distributor": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
"dispatched": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
"received_by_client": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
}
}
if client=="":
self.current_transactions.append(manufacturer_to_distributor)
else :
self.current_transactions.append(distributor_to_client)
self.nonce = random.randint(100,999)
self.unverified_transactions.append(self.current_transactions)
self.save_current_transactions.append(self.current_transactions)
return self.last_block['index'] + 1
# Generate a QR code for a transaction
def generate_qr_code(self, transaction, file_path='qr_code.png'):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(str(transaction))
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save(file_path)
# Resolve a dispute and identify the liar
def resolve_dispute(self, dispute):
liar = None
client_id = dispute['client_id']
distributor_id = dispute['distributor_id']
if dispute['client_claim'] != dispute['distributor_claim']:
if dispute['client_claim'] is False:
liar = client_id
else:
liar = distributor_id
self.entities[liar]['amount'] -= dispute['penalty']
return self.digitalSignature # Return the digital signature flag
# Implement DPoS voting
def dpos_vote(self):
self.stakers = self.participants
eligible_to_delegate = False
for participant, _ in self.stakers.items():
self.delegates[participant] = 0
for _, value in self.stakers.items():
candidate, _ = random.choice(list(self.stakers.items()))
stake_vote = value['amount'] if 'amount' in value else 0
x = random.randint(0, stake_vote)
value['amount'] = value['amount']-x
if(x > 400):
eligible_to_delegate = True
self.delegates[candidate] += x
# Get the result of DPoS consensus
def dpos_result(self):
print(self.delegates)
self.delegates = dict(sorted(self.delegates.items(), key=lambda kv: (kv[1], kv[0]), reverse=True))
self.witnesses = dict(list(self.delegates.items())[0:3])
# Calculate the Merkle root hash of a block
@staticmethod
def hash(block):
block_string = json.dumps({
'timestamp': block['timestamp'],
'previous_hash': block['previous_hash'],
'merkle_root': block['merkle_root'],
'nonce': block['nonce'] # Include nonce in the block string for hashing
}, sort_keys=True).encode()
return hashlib.sha256(block_string).hexdigest()
# Get the previous block's Merkle root hash
def prev_hash(self, block):
return self.hash(block)
# Get the last block in the blockchain
@property
def last_block(self):
return self.chain[-1]