This repository has been archived by the owner on May 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblock.py
291 lines (267 loc) · 9.97 KB
/
block.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from hashlib import sha256
from json import dumps, loads
from copy import copy
from blspy import PublicKey, Signature, AggregationInfo
from bls import BLS
from transaction import Transaction
"""
block ->
{
index,
harvester,
previous_hash,
timestamp,
txn[],
signature,
signer[]
}
"""
# noinspection PyTypeChecker
class Block:
def __init__(self, _index=None, _harvester=None, _previous_hash=None,
_txn=None, _signature=None, _signers=None, _timestamp=None):
if (_index is None) or (_harvester is None) or (_previous_hash is None) or (_txn is None) or (
_signature is None) or (_signers is None) or (_timestamp is None):
pass
else:
self.index = _index
self.harvester = _harvester
self.previous_hash = _previous_hash
self.txn = _txn
self.signature = _signature
self.signers = _signers
self.timestamp = _timestamp
@staticmethod
def create_block(json_data):
try:
block = Block()
block.index = json_data["index"]
block.harvester = json_data["harvester"]
block.previous_hash = json_data["previous_hash"]
block.txn = json_data["txn"]
block.signature = json_data["signature"]
block.signers = json_data["signers"]
block.timestamp = json_data["timestamp"]
return block
except:
try:
json_data = loads(json_data)
block = Block()
block.index = json_data["index"]
block.harvester = json_data["harvester"]
block.previous_hash = json_data["previous_hash"]
block.txn = json_data["txn"]
block.signature = json_data["signature"]
block.signers = json_data["signers"]
block.timestamp = json_data["timestamp"]
return block
except:
return None
def jsonify_block(self):
block_content = dumps({
"index": self.index,
"harvester": self.harvester,
"previous_hash": self.previous_hash,
"txn": self.txn,
"signature": self.signature,
"signers": self.signers,
"timestamp": self.timestamp,
})
return block_content
def get_hash(self):
return sha256(self.jsonify_block().encode()).hexdigest()
def sign_block(self, private_key):
if type(self.txn) == list:
txn_hash = self.txn
txn_hash.sort()
elif type(self.txn) == dict:
txn_hash = list(self.txn.keys())
txn_hash.sort()
else:
return False
msg = dumps({
"index": self.index,
"harvester": self.harvester,
"previous_hash": self.previous_hash,
"txn": txn_hash,
"signature": "",
"signers": "",
"timestamp": self.timestamp,
})
temp_array = []
for c in msg:
temp_array.append(ord(c))
msg = bytes(temp_array)
sig = private_key.sign(msg)
return str(sig.serialize(), "ISO-8859-1")
def verify_block(self, blockchain):
transactions = self.txn
local_utxo = blockchain.UTXO.copy()
for txn_hash in transactions:
if txn_hash not in blockchain.current_transactions:
txn = blockchain.fetch_txn(txn_hash, loads(blockchain.chain[blockchain.Hash])['harvester'])
txn = Transaction.createTransaction(txn)
if txn:
blockchain.current_transactions[txn_hash] = txn.jsonify_Transaction()
else:
return False
txn = blockchain.current_transactions[txn_hash]
tx = Transaction().createTransaction(txn)
flag = tx.verify_signature()
if not flag:
return False
if tx.sender in local_utxo:
if tx.amount <= local_utxo[tx.sender]:
local_utxo[tx.sender] = local_utxo[tx.sender] - tx.amount
else:
return False
else:
return False
flag = blockchain.Hash == self.previous_hash
if flag:
return flag
else:
blockchain.resolve_conflict(self.harvester)
return flag
def verify_offline_block(self, blockchain):
if self.verify_block_signature(blockchain):
return blockchain.Hash == self.previous_hash
# print(blockchain.Hash, self.previous_hash)
# if flag:
# return flag
# else:
# blockchain.resolve_conflict(self.harvester)
# return flag
# return flag
else:
if self.harvester == "genesis":
return True
print("tempered offline block signature")
return False
def update_block(self, blockchain):
if blockchain.Hash == self.previous_hash:
block_transactions = dict()
for txn_hash in self.txn:
txn = Transaction.createTransaction(blockchain.current_transactions[txn_hash])
if txn.sender in blockchain.UTXO:
if txn.amount <= blockchain.UTXO[txn.sender]:
if txn.to not in blockchain.UTXO:
blockchain.UTXO[txn.to] = 0
blockchain.UTXO[txn.sender] = blockchain.UTXO[txn.sender] - txn.amount
blockchain.UTXO[txn.to] = blockchain.UTXO[txn.to] + txn.amount
block_transactions[txn.get_hash()] = txn.jsonify_Transaction()
blockchain.current_transactions.pop(txn_hash)
self.txn = block_transactions
def verify_block_signature(self, blockchain):
if type(self.txn) == list:
txn_hash = self.txn
txn_hash.sort()
elif type(self.txn) == dict:
txn_hash = list(self.txn.keys())
txn_hash.sort()
else:
return False
msg = dumps({
"index": self.index,
"harvester": self.harvester,
"previous_hash": self.previous_hash,
"txn": txn_hash,
"signature": "",
"signers": "",
"timestamp": self.timestamp,
})
temp_array = []
for c in msg:
temp_array.append(ord(c))
msg = bytes(temp_array)
if type(self.signature) == Signature:
pass
else:
self.signature = BLS.deserialize(self.signature, Signature)
agg_info_list = []
for node in self.signers:
if node in blockchain.public_key_list:
agg_info = AggregationInfo.from_msg(
PublicKey.from_bytes(bytes(blockchain.public_key_list[node], "ISO-8859-1")), msg)
agg_info_list.append(agg_info)
else:
return False
agg_public_key = AggregationInfo.merge_infos(agg_info_list)
self.signature.set_aggregation_info(agg_public_key)
return self.signature.verify()
def verify_offline_block_signature(self, blockchain):
if type(self.txn) == list:
txn_hash = self.txn
txn_hash.sort()
elif type(self.txn) == dict:
txn_hash = list(self.txn.keys())
txn_hash.sort()
else:
return False
block = copy(self)
block.signature = ""
block.signers = ""
block.txn = txn_hash
msg = block.get_hash()
temp_array = []
for c in msg:
temp_array.append(ord(c))
msg = bytes(temp_array)
if type(self.signature) == Signature:
pass
else:
self.signature = BLS.deserialize(self.signature, Signature)
agg_info_list = []
for node in self.signers:
if node in blockchain.public_key_list:
agg_info = AggregationInfo.from_msg(
PublicKey.from_bytes(bytes(blockchain.public_key_list[node], "ISO-8859-1")), msg)
agg_info_list.append(agg_info)
else:
return False
agg_public_key = AggregationInfo.merge_infos(agg_info_list)
self.signature.set_aggregation_info(agg_public_key)
return self.signature.verify()
# def temp(self, public_key_list):
# if type(self.txn) == list:
# txn_hash = self.txn
# txn_hash.sort()
# elif type(self.txn) == dict:
# txn_hash = list(self.txn.keys())
# txn_hash.sort()
# else:
# return False
# block = copy(self)
# block.signature = ""
# block.signers = ""
# block.txn = txn_hash
# msg = block.get_hash()
#
# temp_array = []
# for c in msg:
# temp_array.append(ord(c))
# msg = bytes(temp_array)
# print("ph ", self.previous_hash)
# if type(self.signature) == Signature:
# pass
# else:
# self.signature = BLS.deserialize(self.signature, Signature)
#
# agg_info_list = []
# for node in self.signers:
# if node in public_key_list:
# agg_info = AggregationInfo.from_msg(public_key_list[node], msg)
# agg_info_list.append(agg_info)
# else:
# return False
# agg_public_key = AggregationInfo.merge_infos(agg_info_list)
#
# self.signature.set_aggregation_info(agg_public_key)
# return self.signature.verify()
# _block = Block("1", "22", "hash", ["2"], "sig", ["22"])
# js = _block.jsonify_block()
# print(_block.jsonify_block())
#
# _block1 = Block.create_block({"index": "1", "harvester": "22", "previous_hash": "hash",
# "txn": ["2"], "signature": "sig", "signers": ["22"], "timestamp": 1562159814.1128297})
# print(_block1.verify_block())