-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-5.py
317 lines (261 loc) · 9.49 KB
/
2-5.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
292
293
294
295
296
297
298
299
300
301
302
303
# Stream Socket -> Server vs Client
import hashlib
import pickle
import socket
import sys
import threading
import time
import rsa
class Transaction:
def __init__(self, sender, receiver, amounts, fee, message):
self.sender = sender
self.receiver = receiver
self.amounts = amounts
self.fee = fee
self.message = message
class Block:
def __init__(self, previous_hash, difficulty, miner, miner_rewards):
self.previous_hash = previous_hash
self.hash = ''
self.difficulty = difficulty
self.nonce = 0
self.timestamp = int(time.time())
self.transactions = []
self.miner = miner
self.miner_rewards = miner_rewards
class BlockChain:
def __init__(self):
self.adjust_difficulty_blocks = 10
self.difficulty = 1
self.block_time = 30
self.miner_rewards = 10
self.block_limitation = 32
self.chain = []
self.pending_transactions = []
## ------------------------ 2-5 starts from ------------------------>>>>>>
# For P2P connection
self.socket_host = "127.0.0.1" #https://localhost ?
self.socket_port = int(sys.argv[1])
self.start_socket_erver()
## <<<<<<------------------------ 2-5 ends ------------------------
#------------ create Hash ------------
def transaction_to_string(self, transaction):
transaction_dict = {
'sender': str(transaction.sender),
'receiver': str(transaction.receiver),
'amounts': transaction.amounts,
'fee': transaction.fee,
'message': transaction.message
}
return str(transaction_dict)
def get_transactions_string(self, block):
transaction_str = ''
for transaction in block.transactions:
transaction_str += self.transaction_to_string(transaction)
return transaction_str
def get_hash(self, block, nonce):
s = hashlib.sha1()
s.update(
(
block.previous_hash
+ str(block.timestamp)
+ self.get_transactions_string(block)
+ str(nonce)
).encode("utf-8")
)
h = s.hexdigest()
return h
#------------ create genesis block ------------
def create_genesis_block(self):
print("Create genesis block...")
new_block = Block('Hello world!', self.difficulty, 'ramonliao', self.miner_rewards)
new_block.hash = self.get_hash(new_block, 0)
self.chain.append(new_block)
#------------ Add transactions to a new block ------------
def add_transaction_to_block(self, block):
# Get the transaction with highest fee by block_limitation
self.pending_transactions.sort(key=lambda x: x.fee, reverse=True)
if len(self.pending_transactions) > self.block_limitation:
transaction_accepted = self.pending_transactions[:self.block_limitation]
self.pending_transactions = self.pending_transactions[self.block_limitation:]
else:
transaction_accepted = self.pending_transactions
self.pending_transactions = []
block.transactions = transaction_accepted
#------------ Mine a new block ------------
def mine_block(self, miner):
start = time.process_time()
last_block = self.chain[-1]
new_block = Block(last_block.hash, self.difficulty, miner, self.miner_rewards)
self.add_transaction_to_block(new_block)
new_block.previous_hash = last_block.hash
new_block.difficulty = self.difficulty
new_block.hash = self.get_hash(new_block, new_block.nonce)
while new_block.hash[0: self.difficulty] != '0' * self.difficulty:
new_block.nonce += 1
new_block.hash = self.get_hash(new_block, new_block.nonce)
time_consumed = round(time.process_time() - start, 5)
print(f"Previous hash: {new_block.previous_hash}")
print(f"Hash found: {new_block.hash} @ difficulty {self.difficulty}, time cost: {time_consumed}s, nonce: {new_block.nonce}")
self.chain.append(new_block)
#------------ Adjust difficulty ------------
def adjust_difficulty(self):
if len(self.chain) % self.adjust_difficulty_blocks != 1:
return self.difficulty
elif len(self.chain) <= self.adjust_difficulty_blocks:
return self.difficulty
else:
start = self.chain[-1 * self.adjust_difficulty_blocks - 1].timestam
finish = self.chain[-1].timestamp
average_time_consumed = round((finish - start) / (self.adjust_difficulty_blocks), 2)
if average_time_consumed > self.block_time:
print(f"Average block time: {average_time_consumed}s. Lower the difficulty")
self.difficulty -= 1
else:
print(f"Average block time: {average_time_consumed}s. High up the difficulty")
self.difficulty += 1
#------------ Calculate account balance ------------
def get_balance(self, account):
balance = 0
for block in self.chain:
# Check miner reward
miner = False
if block.miner == account:
miner = True
balance += block.miner_rewards
for transaction in block.transactions:
if miner:
balance += transaction.fee
if transaction.sender == account:
balance -= transaction.amounts
balance -= transaction.fee
elif transaction.receiver == account:
balance += transaction.account
return balance
#------------ Confirm hash correctness ------------
def verify_blockchain(self):
previous_hash = ''
for idx, block in enumerate(self.chain):
print(f"Block {idx} Hash:", block.hash)
print(f"Block {idx} Hash:", self.get_hash(block, block.nonce), "(check)")
if self.get_hash(block, block.nonce) != block.hash:
print("Error: Hash not matched!")
return False
elif previous_hash != block.previous_hash and idx:
print("Error: Hash not matched to previous_hash")
return False
previous_hash = block.hash
print("Hash correct!")
return True
#------------ Generate Address ------------
def generate_address(self):
public, private = rsa.newkeys(512)
public_key = public.save_pkcs1()
private_key = private.save_pkcs1()
# print(public_key)
return self.get_address_from_public(public_key), private_key
#------------ Get Address from Public key ------------
def get_address_from_public(self, public):
address = str(public).replace('\\n','')
address = address.replace("b'-----BEGIN RSA PUBLIC KEY-----", '')
address = address.replace("-----END RSA PUBLIC KEY-----'", '')
address = address.replace(' ', '')
print('Address: ', address, "\n")
return address
#------------ 1. Initialize a transaction (Client side) ------------
def initialize_transaction(self, sender, receiver, amount, fee, message):
if self.get_balance(sender) < amount + fee:
print("Balance not enough!")
return False
new_transaction = Transaction(sender, receiver, amount, fee, message)
return new_transaction
#------------ 2. Sign the transaction (Client side) ------------
def sign_transaction(self, transaction, private_key):
private_key_pkcs = rsa.PrivateKey.load_pkcs1(private_key)
transaction_str = self.transaction_to_string(transaction)
signature = rsa.sign(transaction_str.encode('utf-8'), private_key_pkcs, 'SHA-1')
return signature
#------------ 3. Add transaction ------------
def add_transaction(self, transaction, signature):
public_key = '-----BEGIN RSA PUBLIC KEY-----\n'
public_key += transaction.sender
public_key += '\n-----END RSA PUBLIC KEY-----\n'
public_key_pkcs = rsa.PublicKey.load_pkcs1(public_key.encode('utf-8'))
transaction_str = self.transaction_to_string(transaction)
if transaction.fee + transaction.amounts > self.get_balance(transaction.sender):
print("Balance not enough!")
return False
try:
# Verify Sender
rsa.verify(transaction_str.encode('utf-8'), signature, public_key_pkcs)
print("Authorized successfully!")
self.pending_transactions.append(transaction)
return True
except Exception:
print("RSA Verified wrong!")
## ------------------------ 2-5 starts from ------------------------>>>>>>
#------------ Start a blockchain ------------
def start(self):
address, private = self.generate_address()
print(f"Miner address: {address}")
print(f"Miner private: {private}")
self.create_genesis_block()
while(True):
self.mine_block(address)
self.adjust_difficulty
#------------ Start a Socket Server ------------
def start_socket_erver(self):
t = threading.Thread(target=self.wait_for_socket_connection)
t.start()
def wait_for_socket_connection(self):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((self.socket_host, self.socket_port))
s.listen()
while True:
coon, address = s.accept()
client_handler = threading.Thread(
target = self.receive_socket_message,
args=(conn, address)
)
client_handler.start()
def receive_socket_message(self, connection, address):
with connection:
print(f'Connected by: {address}')
while True:
message = connection.recv(1024)
print(f"[*] Received: {message}")
try:
parsed_message = pickle.loads(message)
except Exception:
print(f"{message} cannot by parsed")
if message:
if parsed_message["request"] == "get_balance":
print("Start to get the balance for client...")
address = parsed_message["address"]
balance = self.get_balance(address)
response = {
"address": address,
"balance": balance
}
elif parsed_message["request"] == "transaction":
print("Start to transaction for client...")
new_transaction = parsed_message["data"]
result, result_message = self.add_transaction(
new_transaction,
parsed_message["signature"]
)
response = {
"result": result,
"result_message": result_message
}
else:
response = {
"message": "Unkown command."
}
resonse_bytes = str(response).encode('utf8')
connection.sendall(resonse_bytes)
## <<<<<<------------------------ 2-5 ends ------------------------
#------------ Test 3------------
if __name__ == '__main__':
block = BlockChain()
block.start()