-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
365 lines (294 loc) · 9.39 KB
/
server.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import socket
import time
import queue
import threading
import chain
import block
import blockchain
import pickle
from _thread import *
# Constants
HOST = socket.gethostbyname('DESKTOP-QQB5RUH')
NODEHOST = socket.gethostbyname('DESKTOP-QQB5RUH')
# HOST = '127.0.0.1'
# NODEHOST = '127.0.0.1'
PORT = 12000
NODEPORT = 12500
NUMBER_OF_THREADS = 4
# Global Variables
serverSocket = None
nodeServerSocket = None
voteDetails = None
_chain = None
stopMining = False
votesUpdated = False
blocks = []
callCount = 0
clientConnections = []
clientAddresses = []
votersList = []
queue = queue.Queue()
a_lock = allocate_lock()
# Working area
# ------------------------------Thread 1 -----------------------------
# Create a socket
def createSocket():
global serverSocket
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket
def bindSocket():
global serverSocket
# Bind takes a tuple!
serverSocket.bind((HOST, PORT))
# Listen to te socket
serverSocket.listen(5)
# Accept connetion
def acceptConnection():
global serverSocket
global clientConnections
global clientAddresses
global votersList
global stopMining
global a_lock
# Flush all previous connections
for connection in clientConnections:
connection.close()
del clientConnections[:]
del clientAddresses[:]
while True:
try:
clientConnection, clientAddress = serverSocket.accept()
clientConnections.append(clientConnection)
clientAddresses.append(clientAddress)
votersList.append(False)
print(clientAddress[0] + " just connected")
# Tell the client to vote
data = pickle.dumps("vote")
clientConnection.sendall(data)
start_new_thread(listenToTheClients, (clientConnection, clientAddress))
# Set the minig flag
a_lock.acquire(0)
stopMining = False
a_lock.release()
except Exception as e:
print("Error in accepting connections" + str(e))
# Serve the connection
# ------------------------------Thread 2 -----------------------------
def configure():
global serverSocket
global stopMining
while True:
# Get command
# command = input("Key in the candidate details\n")
stopMining = False
for conn in clientConnections:
data = pickle.dumps(command)
conn.sendall(data)
else:
print("Error no such command...")
# ------------------------------Thread 3 -----------------------------
def pingClients():
global clientConnections
global clientAddresses
while True:
try:
# Loop through all the clients and check the active ones
for i, connection in enumerate(clientConnections):
try:
# Ping client
msg = '@'
data = pickle.dumps(msg)
connection.send(data)
except:
# Delete the client from the list
print(str(clientAddresses[i][0]) + " disconnected")
del clientConnections[i]
del clientAddresses[i]
del votersList[i]
continue
except:
break
time.sleep(5)
# ------------------------------Thread 4 -----------------------------
def listenToTheClients(clientConnection, clientAddress):
global _chain
global blocks
global voteDetails
global votersList
while True:
# Listen to the client's request
data = clientConnection.recv(1024)
candidate = data.decode()
print("Client voted for candidated: " + candidate)
# Mark the client as voted
client = lookUpClient(clientConnection)
votersList[client] = True
# Update vote count locally
votes = blockchain.Blockchain.updateDetails(voteDetails, candidate)
# Initialize chain
# _chain = chain.Chain(lastBlock)
# Get the last block
lastBlock = _chain.getBlock()
# Initialize block
newblock = block.Block(candidate)
newblock.updateBlock(lastBlock)
blocks.append(newblock)
print("Sending this to server")
print(newblock)
# Intimate the node server that a block is available for mining
data = pickle.dumps(newblock)
nodeServerSocket.sendall(data)
# # Send vote details to all clients
# data = pickle.dumps(blockchain.Blockchain.getDetails(votes))
# clientConnection.sendall(data)
# ------------------------------Thread 6 -----------------------------
# def listenToNodeServer():
# global stopMining
# global nodeServerSocket
# time.sleep(1)
# while True:
# data = nodeServerSocket.recv(1024)
# block = pickle.loads(data)
# if "MINED" not in block:
# start_new_thread(mineBlock, (block,))
# else:
# stopMining = True
# ------------------------------Thread 5 -----------------------------
'''
'''
def connectToNodeServer():
global nodeServerSocket
global callCount
global stopMining
global voteDetails
global _chain
global a_lock
# Create the socket
nodeServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect via the socket
try:
nodeServerSocket.connect((NODEHOST, NODEPORT))
except:
print("Unable to connect to the blockchain service")
print("Trying again...")
callCount = callCount + 1
if callCount > 2:
return
else:
connectToNodeServer()
# Get the last block
data = nodeServerSocket.recv(1024)
lastBlock = pickle.loads(data)
if _chain == None:
_chain = chain.Chain(lastBlock)
else:
_chain.setBlock(block)
# Get the vote count
data = nodeServerSocket.recv(1024)
voteDetails = pickle.loads(data)
while True:
data = nodeServerSocket.recv(1024)
msg = pickle.loads(data)
if isinstance(msg, block.Block):
if stopMining:
stopMining = False
start_new_thread(mineBlock, (msg,))
# else:
# print("Server sent this: \n")
# print(msg)
# if _chain == None:
# _chain = chain.Chain(msg)
# else:
# _chain.setBlock(msg)
elif isinstance(msg, list):
voteDetails = msg
# Broadcast the vote count to all connected clients
broadCastVoteCount()
# Another server on the blockchain network has mined the block
else:
a_lock.acquire(0)
stopMining = True
a_lock.release()
data = nodeServerSocket.recv(1024)
msg = pickle.loads(data)
if _chain == None:
_chain = chain.Chain(msg)
else:
_chain.setBlock(msg)
# ------------------------------Thread 7 -----------------------------
def mineBlock(block):
global _chain
global stopMining
diff = 20
maxNonce = 2**32
target = 2 ** (256-diff)
while not stopMining:
for n in range(maxNonce):
if stopMining:
print("Minig halted")
return
if int(block.hash(), 16) <= target:
if _chain == None:
_chain = chain.Chain(block)
previousBlock = _chain.getBlock()
block.previous_hash = previousBlock.hash()
block.blockNo = previousBlock.blockNo + 1
_chain.add(block)
print(block)
msg = "MINED"
data = pickle.dumps(msg)
nodeServerSocket.sendall(data)
data = pickle.dumps(_chain)
nodeServerSocket.sendall(data)
return
else:
block.nonce += 1
data = pickle.dumps(_chain)
nodeServerSocket.sendall(data)
def lookUpClient(client):
global clientConnections
for i, conn in enumerate(clientConnections):
if conn == client:
return i
def broadCastVoteCount():
global voteDetails
global clientConnections
global votersList
# Package the data for sending
votes = blockchain.Blockchain.getDetails(voteDetails)
data = pickle.dumps(votes)
for i, conn in enumerate(clientConnections):
# Send the vote count to the clients who have already voted
if votersList[i]:
conn.sendall(data)
# create jobs
def create_jobs():
global queue
for i in range(1, NUMBER_OF_THREADS + 1):
queue.put(i)
queue.join() # Halt the main thread
def create_threads():
for i in range(NUMBER_OF_THREADS):
thread = threading.Thread(target=work)
thread.daemon = True # make it a background process
thread.start()
def work():
global queue
while True:
job = queue.get()
if job == 1:
createSocket()
bindSocket()
acceptConnection()
# elif job == 2:
# configure()
elif job == 3:
pingClients()
elif job == 4:
connectToNodeServer()
# elif job == 5:
# listenToTheClients()
# elif job == 5:
# listenToNodeServer()
create_threads()
create_jobs()