/
Node.py
329 lines (275 loc) · 11.4 KB
/
Node.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
from Genesis import Genesis
from Blockchain import Blockchain
from Miner import Miner
from Block import Block
from utils import *
from socket import *
from threading import RLock
import _thread
SERVER = 'localhost' # colocar o ip do peer central
SERVER_PORT = 12000
BUFFER = 1024
class Node:
def __init__(self, address, own_port):
self.address = address # wallet address
self.own_port = own_port # porta na qual receberá requests
self.peers = None
self.blockchain = Blockchain(Genesis())
self.difficulty = 4
self.current_state = None
self.miner = None
self.transaction_pool = []
self.lock = RLock()
def start_node(self):
initial_socket = socket(AF_INET, SOCK_STREAM)
initial_socket.connect((SERVER, SERVER_PORT))
initial_socket.send(write_message('connect', self.own_port))
initial_socket.shutdown(SHUT_WR)
try:
response = ''
while True:
data = initial_socket.recv(1024).decode('ascii')
if not data:
break
response += data
print(response)
code, data = read_message(response)
if code == 'ok':
self.peers = eval(str(data))
writePeers("cpeers.txt", self.peers)
finally:
initial_socket.close()
for peer in self.peers:
peer = eval(str(peer))
self.request_top_block(peer_ip=peer[0], peer_port=peer[1])
self.listen_requests()
def request_top_block(self, peer_ip, peer_port):
s = socket(AF_INET, SOCK_STREAM)
try:
s.connect((peer_ip, peer_port))
s.send(write_message('get_top', None))
s.shutdown(SHUT_WR)
response = ''
while True:
data = s.recv(BUFFER).decode('ascii')
if not data:
break
response += data
code, data = read_message(response)
if code == 'top_block':
block = rebuild_block(data)
self.receive_block(block, peer_ip, peer_port)
except ConnectionRefusedError:
# self.peers.remove((peer_ip, peer_port))
print(peer_ip, ':', peer_port, ' is unreachable')
def receive_block(self, new_block, sender_ip, sender_port):
last_block = self.blockchain.get_last()
if new_block.index <= last_block.index:
return False # stale block
else:
# verifica o novo bloco e a chain a qual pertence, se diferente da atual
self.validate_chain(new_block, sender_ip, sender_port)
def request_block(self, block_hash, sender_ip, sender_port):
s = socket(AF_INET, SOCK_STREAM)
s.connect((sender_ip, sender_port))
s.send(write_message('get_block', block_hash))
s.shutdown(SHUT_WR)
response = ''
while True:
data = s.recv(BUFFER).decode('ascii')
if not data:
break
response += data
code, data = read_message(response)
if code == 'block':
block = rebuild_block(data)
return block
return None
def listen_requests(self):
s = socket(AF_INET, SOCK_STREAM)
s.bind(('', self.own_port))
s.listen()
while 1:
conn, addr = s.accept()
_thread.start_new_thread(self.route_request, (conn, addr))
def send_transactions(self, receiving_address, ammount):
for peer in self.peers:
peer = eval(str(peer))
_thread.start_new_thread(self.send_transaction, (peer[0], peer[1], receiving_address, ammount))
def send_transaction(self, peer_ip, peer_port, receiving_address, amount):
data = {self.address: -amount, receiving_address: amount}
try:
s = socket(AF_INET, SOCK_STREAM)
s.connect((peer_ip, peer_port))
s.send(write_message('transaction', data))
s.shutdown(SHUT_WR)
response = ''
while True:
data = s.recv(BUFFER).decode('ascii')
if not data:
break
response += data
code, data = read_message(response)
print(peer_ip, ':', peer_port, ' - ', code)
except ConnectionRefusedError:
print(peer_ip, ':', peer_port, ' is unreachable')
def receive_transaction(self, peer_socket, transaction):
if not self.miner:
peer_socket.send(write_message('not_mining', None))
elif len(self.transaction_pool) >= 9:
peer_socket.send(write_message('full_block', None))
elif self.accept_transaction(transaction):
peer_socket.send(write_message('tr_accepted', None))
peer_socket.close()
def route_request(self, conn, addr):
msg = ''
while 1:
data = conn.recv(BUFFER).decode('ascii')
if not data:
break
msg += data
code, data = read_message(msg)
if code == 'get_block':
block_hash = data
data = self.blockchain.get_block(block_hash)
conn.send(write_message('block', data))
elif code == 'get_top':
data = self.blockchain.get_last()
conn.send(write_message('top_block', data))
elif code == 'new_block':
block = rebuild_block(data)
self.receive_block(block, sender_ip=addr[0], sender_port=addr[1])
elif code == 'transaction':
self.receive_transaction(conn, data)
conn.close()
def accept_transaction(self, transaction):
candidate_state = self.get_current_state().copy()
if sum(transaction.values()) != 0:
return False
for address in transaction.keys():
if address in candidate_state.keys(): # o endereço existe
if candidate_state[address] - transaction[address] >= 0: # o endereço possue fundos suficientes
candidate_state[address] += transaction[address]
else:
return False
# se o endereço não existe mas a quantia é positiva
elif transaction[address] > 0:
# cria o endereço
candidate_state[address] = transaction[address]
else: # a quantia é negativa
return False
self.transaction_pool.append(transaction)
self.restart_miner()
return True
def start_miner(self):
prev_hash = hash_string(str(self.blockchain.get_last()))
self.miner = Miner(self, prev_hash, self.blockchain.get_size(), self.transaction_pool)
self.miner.start()
def restart_miner(self):
if self.miner:
self.stop_miner()
self.start_miner()
def stop_miner(self):
self.miner.stop()
def get_current_state(self):
if not self.current_state:
return self.rebuild_current_state()
return self.current_state
# retorna um dict com todos os addresses existentes e seus fundos
def rebuild_current_state(self):
state = {}
for transaction in self.blockchain.get_all_transactions():
for address in transaction.keys():
if address in state.keys():
state[address] += transaction[address]
else:
state[address] = transaction[address]
self.current_state = state
return state
def validate_chain(self, top_block, sender_ip, sender_port):
self.lock.acquire() # locka a blockchain para que ela não seja alterada durante a verificação
try:
different_blocks = [top_block]
top_index = top_block.index
current_block = top_block
# itera para encontrar blocos diferentes dos quais a blockchain atual armazena
while self.blockchain.get_block(current_block.prev_hash) is None:
# pede o bloco diferente
current_block = self.request_block(current_block.prev_hash, sender_ip, sender_port)
top_index = current_block.index
different_blocks.append(current_block)
# itera sobre os blocos diferentes
for block in different_blocks:
if not self.validate_block(block): # se o bloco não é valido
if top_index <= self.blockchain.get_size(): # e o tamanho da chain recebida é menor que a atual
return # não faz nada
else: # a chain nova é maior e correta até certo ponto
break # adiciona
else: # o bloco é válido
top_index = block.index
self.add_new_blocks(different_blocks, top_index) # adiciona os novos blocos até o maior índice válido
finally:
self.lock.release()
def validate_block(self, block):
block_hash = hash_string(str(block))
if not verify_size(block):
return False
if not self.verify_difficulty(block_hash):
return False
if not self.verify_transactions(block):
return False
print(block_hash, " accepted as valid")
return True
def add_new_blocks(self, blocks, top_index):
self.current_state = None # inutiliza a referencia atual do estado
self.transaction_pool = []
self.restart_miner()
for block in blocks:
if block.index <= top_index:
self.blockchain.add_block(block, block.index)
def verify_difficulty(self, block_hash):
first_chars = block_hash[:self.difficulty]
first_chars = first_chars.replace('0', '') # remove os zeros
if first_chars:
return False # first_chars não está vazia
return True
def verify_transactions(self, block):
current_state = self.get_current_state()
candidate_state = current_state.copy()
print(block.transactions)
# valida a primeira transação
if len(block.transactions[0].keys()) > 1 or sum(block.transactions[0].values()) > 10:
return False
# itera sobre as transações restantes
for transaction in block.transactions[1:]:
if sum(transaction.values()) != 0:
return False
for address in transaction.keys():
if address in candidate_state.keys(): # o endereço existe
if candidate_state[address] - transaction[address] >= 0: # o endereço possue fundos suficientes
candidate_state[address] += transaction[address]
else:
return False
# se o endereço não existe mas a quantia é positiva
elif transaction[address] > 0:
# cria o endereço
candidate_state[address] = transaction[address]
else: # a quantia é negativa
return False
return True
def verify_size(block):
return 2 <= len(block.transactions) <= 10
def rebuild_block(data):
block = Block(data['index'], data['prev_hash'])
block.nonce = data['nonce']
block.transactions = data['transactions']
return block
# test
# from Miner import Miner
#
#
# a = Node('123')
# a.miner = Miner(a,'111',1)
# a.miner.start()
# a.miner.accept_transaction({'1': -2, '100': 2})
# a.miner.accept_transaction({'1': -2, '100': 2})