-
Notifications
You must be signed in to change notification settings - Fork 2
/
r2ac_bkp.py
584 lines (492 loc) · 21.8 KB
/
r2ac_bkp.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import pickle
import Pyro4
import socket
import logging.config
import logging as logger
import os
import sys
import time
import threading
import merkle
import asyncio
import thread
from os import listdir
from os.path import isfile, join
from flask import Flask, request
from Crypto.PublicKey import RSA
import Transaction
import DeviceInfo
import PeerInfo
import DeviceKeyMapping
import chainFunctions
import criptoFunctions
def getMyIP():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
myIP = s.getsockname()[0]
s.close()
return myIP
# logging.config.fileConfig('logging.conf')
# logger = logging.getLogger(__name__)
logger.basicConfig(filename=getMyIP(),level=logging.DEBUG)
# Enable/Disable the transaction validation when peer receives a transaction
validatorClient = True
app = Flask(__name__)
peers = []
genKeysPars = []
myURI = ""
gwPvt = ""
gwPub = ""
# generate the RSA key pair for the gateway
def bootstrapChain2():
global gwPub
global gwPvt
chainFunctions.startBlockChain()
gwPub, gwPvt = criptoFunctions.generateRSAKeyPair()
#############################################################################
#############################################################################
######################### PEER MANAGEMENT ###############################
#############################################################################
#############################################################################
def findPeer(peerURI):
global peers
for p in peers:
if p.peerURI == peerURI:
return True
return False
def getPeer(peerURI):
global peers
for p in peers:
if p.peerURI == peerURI:
return p
return False
def addBack(peer, isFirst):
global myURI
if(isFirst):
obj = peer.object
obj.addPeer(myURI, isFirst)
#else:
# print ("done adding....")
def sendTransactionToPeers(devPublicKey, blockLedger):
global peers
for peer in peers:
obj = peer.object
#logger.debug("sending to: " + peer.peerURI)
dat = pickle.dumps(blockLedger)
obj.updateBlockLedger(devPublicKey, dat)
# class sendBlks(threading.Thread):
# def __init__(self, threadID, iotBlock):
# threading.Thread.__init__(self)
# self.threadID = threadID
# self.iotBlock = iotBlock
#
# def run(self):
# print "Starting "
# # Get lock to synchronize threads
# global peers
# for peer in peers:
# print ("runnin in a thread: ")
# obj = peer.object
# #logger.debug("sending IoT Block to: " + peer.peerURI)
# dat = pickle.dumps(self.iotBlock)
# obj.updateIOTBlockLedger(dat)
def sendBlockToPeers(IoTBlock):
global peers
for peer in peers:
obj = peer.object
#logger.debug("sending IoT Block to: " + peer.peerURI)
dat = pickle.dumps(IoTBlock)
obj.updateIOTBlockLedger(dat)
def syncChain(newPeer):
#write the code to identify only a change in the iot block and insert.
return True
#this method recieves a nameServer parameter, list all remote objects connected to it, and add these remote objetcts as peers to the current node
def connectToPeers(nameServer):
#print ("found # results:"+str(len(nameServer.list())))
for peerURI in nameServer.list():
if(peerURI.startswith("PYRO:") and peerURI != myURI):
#print ("adding new peer:"+peerURI)
addPeer2(peerURI)
#else:
#print ("nothing to do")
#print (peerURI )
print ("finished connecting to all peers")
def addPeer2(peerURI):
global peers
if not (findPeer(peerURI)):
#print ("peer not found. Create new node and add to list")
#print ("[addPeer2]adding new peer:" + peerURI)
newPeer = PeerInfo.PeerInfo(peerURI, Pyro4.Proxy(peerURI))
peers.append(newPeer)
#print("Runnin addback...")
addBack(newPeer, True)
#syncChain(newPeer)
#print ("finished addback...")
return True
return False
#############################################################################
#############################################################################
######################### CRIPTOGRAPHY ################################
#############################################################################
#############################################################################
def generateAESKey(devPubKey):
global genKeysPars
randomAESKey = os.urandom(32) # AES key: 256 bits
obj = DeviceKeyMapping.DeviceKeyMapping(devPubKey, randomAESKey)
genKeysPars.append(obj)
return randomAESKey
def findAESKey(devPubKey):
global genKeysPars
for b in genKeysPars:
if (b.publicKey == devPubKey):
return b.AESKey
return False
#############################################################################
#############################################################################
################# Consensus Algorithm Methods #########################
#############################################################################
#############################################################################
answers = {}
trustedPeers = []
def addTrustedPeers():
global peers
for p in peers:
trustedPeers.append(p.peerURI)
#####NEW CONSENSUS @Roben
###########
###Consensu @Roben
###########
newBlockCandidate = [] ## the idea newBlockCandidate[newBlockHash][gwPubKey] = signature, if the gateway put its signature, it is voting for YES
newTransactionCandidate = [] #same as block, for transaction
def PBFTConsensus(newBlock, generatorGwPub,generatorDevicePub):
threads = []
connectedPeers = preparePBFTConsensus() #verify who will participate in consensus
commitBlockPBFT(newBlock, generatorGwPub,generatorDevicePub,connectedPeers) #send to all peers and for it self the result of validation
if calcBlockPBFT(newBlock,connectedPeers): # calculate, and if it is good, insert new block and call other peers to do the same
for p in connectedPeers:
t = threading.Thread(target=p.object.calcBlockPBFT, args=(newBlock, connectedPeers))
threads.append(t)
for t in threads:
t.join()
def PBFTConsensus(block, newTransaction, generatorGwPub,generatorDevicePub):
connectedPeers = preparePBFTConsensus()
commitTransactionPBFT(block, newTransaction, generatorGwPub, generatorDevicePub,connectedPeers)
#TODO same as block, but verifications for transaction
return True
def preparePBFTConsensus(): #verify all alive peers that will particpate in consensus
alivePeers = []
global peers
for p in peers:
if p.peerURI._pyroBind(): #verify if peer is alive
alivePeers.append(p.peerURI)
return alivePeers
def commitBlockPBFT(newBlock,generatorGwPub,generatorDevicePub,alivePeers):
threads = []
if newBlockCandidate[criptoFunctions.calculateHashForBlock(newBlock)][gwPub] == criptoFunctions.signInfo(gwPvt, newBlock):#if it was already inserted a validation for the candidade block, abort
print 'block already in consensus'
return
if verifyBlockCandidate():#verify if the block is valid
for p in alivePeers:
t = threading.Thread(target=p.object.verifyBlockCandidate, args=(newBlock,generatorGwPub,generatorDevicePub,alivePeers))
threads.append(t) #call all peers to verify if blocks are valid
# join threads
for t in threads:
t.join()
def verifyBlockCandidate(newBlock,generatorGwPub,generatorDevicePub,alivePeers):
blockValidation = True
lastBlk = chainFunctions.getLatestBlock()
# print("Index:"+str(lastBlk.index)+" prevHash:"+str(lastBlk.previousHash)+ " time:"+str(lastBlk.timestamp)+ " pubKey:")
lastBlkHash = criptoFunctions.calculateHash(lastBlk.index, lastBlk.previousHash, lastBlk.timestamp,
lastBlk.publicKey)
# print ("This Hash:"+str(lastBlkHash))
# print ("Last Hash:"+str(block.previousHash))
if (lastBlkHash != newBlock.previousHash):
blockValidation = False
return blockValidation
if (lastBlk.index != (newBlock.index+1)):
blockValidation = False
return blockValidation
if (lastBlk.timestamp >= newBlock.timestamp):
blockValidation = False
return blockValidation
#TODO -> verifySIGNATURE!!!!!
if blockValidation:
voterSign=criptoFunctions.signInfo(gwPvt, newBlock)
addVoteBlockPBFT(newBlock, gwPub, voterSign) #adiciona o seu p
for p in alivePeers:
p.object.addVoteBlockPBFT(newBlock, gwPub, voterSign) #altera a lista de confirmacao de todos os peers
return True
else:
return False
#add the signature of a peer into the newBlockCandidate, using a list to all gw for a single hash, if the block is valid put the signature
def addVoteBlockPBFT(newBlock,voterPub,voterSign):
global newBlockCandidate
newBlockCandidate[criptoFunctions.calculateHashForBlock(newBlock)][voterPub] = voterSign
return True
def calcBlockPBFT(newBlock,alivePeers):
if len(newBlockCandidate[criptoFunctions.calculateHashForBlock(newBlock)]) > ((2/3)*len(alivePeers)):
chainFunctions.addBlockHeader(newBlock)
return True
def commitTransactionPBFT(block, newTransaction, alivePeers):
result = isTransactionValid()#tem que ver o que colocar aqui e verificar se o metodo isValidBlock serve
for p in alivePeers:
alivePeers.sendPBFTAnswer(result) #envia resposta positiva ou negativa de um bloco, precisa enviar junto id do bloco!!!
###########################END NEW CONSENSUS @Roben
##########################
def consensus(newBlock, gatewayPublicKey, devicePublicKey):
addTrustedPeers() # just for testing, delete after
global peers, answers
threads = []
answers[newBlock] = []
# run through peers
numberOfActivePeers = 0
for p in peers:
# if trusted and active create new thread and sendBlockToConsensus
if peerIsTrusted(p.peerURI) and peerIsActive(p.object):
numberOfActivePeers = numberOfActivePeers + 1
t = threading.Thread(target=sendBlockToConsensus, args=(newBlock, gatewayPublicKey, devicePublicKey))
threads.append(t)
# join threads
for t in threads:
t.join()
numberOfTrueResponses = 0
for a in answers[newBlock]:
if a: numberOfTrueResponses = numberOfTrueResponses + 1
# if more then 2/3 -> true, else -> false
del answers[newBlock]
return numberOfTrueResponses >= int((2*numberOfActivePeers)/3)
def peerIsTrusted(i):
global trustedPeers
for p in trustedPeers:
if p == i: return True
return False
def peerIsActive(i):
return True # TO DO
def sendBlockToConsensus(newBlock, gatewayPublicKey, devicePublicKey):
obj = peer.object
data = pickle.dumps(newBlock)
obj.isValidBlock(data, gatewayPublicKey, devicePublicKey)
def receiveBlockConsensus(self, data, gatewayPublicKey, devicePublicKey, consensus):
newBlock = pickle.loads(data)
answer[newBlock].append(consensus)
def isValidBlock(self, data, gatewayPublicKey, devicePublicKey, peer):
newBlock = pickle.loads(data)
blockIoT = chainFunctions.findBlock(devicePublicKey)
consensus = True
if blockIoT == False:
print("Block not found in IoT ledger")
consensus = False
lastBlock = blockIoT.blockLedger[len(blockIoT.blockLedger) - 1]
if newBlock.index != lastBlock.index + 1:
print("New blovk Index not valid")
consensus = False
if lastBlock.calculateHashForBlockLedger(lastBlock) != newBlock.previousHash:
print("New block previous hash not valid")
consensus = False
now = "{:.0f}".format(((time.time() * 1000) * 1000))
# check time
if not (newBlock.timestamp > newBlock.signature.timestamp and newBlock.timestamp < now):
print("New block time not valid")
consensus = False
# check device time
if not (newBlock.signature.timestamp > lastBlock.signature.timestamp and newBlock.signature.timestamp < now):
print("New block device time not valid")
consensus = False
# check device signature with device public key
if not (criptoFunctions.signVerify(newBlock.signature.data, newBlock.signature.deviceSignature, gatewayPublicKey)):
print("New block device signature not valid")
consensus = False
peer = getPeer(peer)
obj = peer.object
obj.receiveBlockConsensus(data, gatewayPublicKey, devicePublicKey, consensus)
def isTransactionValid(transaction,pubKey):
data = str(transaction.data)[-22:-2]
signature = str(transaction.data)[:-22]
res = criptoFunctions.signVerify(data, signature, pubKey)
return res
def isBlockValid(block):
#Todo Fix the comparison between the hashes... for now is just a mater to simulate the time spend calculating the hashes...
#global BlockHeaderChain
#print(str(len(BlockHeaderChain)))
lastBlk = chainFunctions.getLatestBlock()
#print("Index:"+str(lastBlk.index)+" prevHash:"+str(lastBlk.previousHash)+ " time:"+str(lastBlk.timestamp)+ " pubKey:")
lastBlkHash = criptoFunctions.calculateHash(lastBlk.index, lastBlk.previousHash, lastBlk.timestamp, lastBlk.publicKey)
#print ("This Hash:"+str(lastBlkHash))
#print ("Last Hash:"+str(block.previousHash))
if(lastBlkHash == block.previousHash):
return True
else:
return True
#############################################################################
#############################################################################
###################### R2AC Class ###################################
#############################################################################
#############################################################################
@Pyro4.expose
@Pyro4.behavior(instance_mode="single")
class R2ac(object):
def __init__(self):
print("R2AC initialized")
def addTransaction(self, devPublicKey, encryptedObj):
global gwPvt
global gwPub
t1 = time.time()
blk = chainFunctions.findBlock(devPublicKey)
if (blk != False and blk.index > 0):
devAESKey = findAESKey(devPublicKey)
if (devAESKey != False):
# plainObject contains [Signature + Time + Data]
plainObject = criptoFunctions.decryptAES(encryptedObj, devAESKey)
signature = plainObject[:-20] # remove the last 20 chars
devTime = plainObject[-20:-4] # remove the 16 char of timestamp
deviceData = plainObject[-4:] # retrieve the las 4 chars which are the data
d = devTime+deviceData
isSigned = criptoFunctions.signVerify(d, signature, devPublicKey)
if isSigned:
deviceInfo = DeviceInfo.DeviceInfo(signature, devTime, deviceData)
nextInt = blk.transactions[len(blk.transactions) - 1].index + 1
signData = criptoFunctions.signInfo(gwPvt, str(deviceInfo))
gwTime = "{:.0f}".format(((time.time() * 1000) * 1000))
# code responsible to create the hash between Info nodes.
prevInfoHash = criptoFunctions.calculateTransactionHash(chainFunctions.getLatestBlockTransaction(blk))
transaction = Transaction.Transaction(nextInt, prevInfoHash, gwTime, deviceInfo, signData)
# send to consensus
#if not consensus(newBlockLedger, gwPub, devPublicKey):
# return "Not Approved"
chainFunctions.addBlockTransaction(blk, transaction)
logger.debug("block added locally... now sending to peers..")
t2 = time.time()
logger.debug("=====2=====>time to add transaction in a block: " + '{0:.12f}'.format((t2 - t1) * 1000))
sendTransactionToPeers(devPublicKey, transaction) # --->> this function should be run in a different thread.
#print("all done")
return "ok!"
else:
return "Invalid Signature"
return "key not found"
#update local bockchain adding a new transaction
def updateBlockLedger(self, pubKey, block):
b = pickle.loads(block)
t1 = time.time()
logger.debug("Received Transaction #:" + (str(b.index)))
blk = chainFunctions.findBlock(pubKey)
if blk != False:
if not (chainFunctions.blockContainsTransaction(blk, b)):
if validatorClient:
isTransactionValid(b, pubKey)
chainFunctions.addBlockTransaction(blk, b)
t2 = time.time()
logger.debug("=====3=====>time to update transaction received: " + '{0:.12f}'.format((t2 - t1) * 1000))
return "done"
# update local bockchain adding a new block
def updateIOTBlockLedger(self, iotBlock):
b = pickle.loads(iotBlock)
t1 = time.time()
#logger.debug("Received Block #:" + (str(b.index)))
if isBlockValid(b):
chainFunctions.addBlockHeader(b)
t2 = time.time()
logger.debug("=====4=====>time to add new block in peers: " + '{0:.12f}'.format((t2 - t1) * 1000))
def addBlock(self, devPubKey):
aesKey = ''
t1 = time.time()
blk = chainFunctions.findBlock(devPubKey)
if (blk != False and blk.index > 0):
aesKey = findAESKey(devPubKey)
if aesKey == False:
logger.debug("Using existent block data")
aesKey = generateAESKey(blk.publicKey)
else:
#logger.debug("Create New Block Header")
logger.debug("***** New Block: Chain size:" + str(chainFunctions.getBlockchainSize()))
bl = chainFunctions.createNewBlock(devPubKey, gwPvt)
sendBlockToPeers(bl) # --->> this function should be run in a different thread.
# try:
# #thread.start_new_thread(sendBlockToPeers,(bl))
# t1 = sendBlks(1, bl)
# t1.start()
# except:
# print "thread not working..."
aesKey = generateAESKey(devPubKey)
encKey = criptoFunctions.encryptRSA2(devPubKey, aesKey)
t2 = time.time()
logger.debug("=====1=====>time to generate key: " + '{0:.12f}'.format((t2 - t1) * 1000))
return encKey
def addPeer(self, peerURI, isFirst):
global peers
if not (findPeer(peerURI)):
newPeer = PeerInfo.PeerInfo(peerURI, Pyro4.Proxy(peerURI))
peers.append(newPeer)
if isFirst:
#after adding the original peer, send false to avoid loop
addBack(newPeer, False)
syncChain(newPeer)
return True
else:
print("peer is already on the list")
return False
def showIoTLedger(self):
logger.debug("Showing Block Header data for peer: " + myURI)
size = chainFunctions.getBlockchainSize()
logger.debug("IoT Ledger size: " + str(size))
logger.debug("|-----------------------------------------|")
theChain = chainFunctions.getFullChain()
for b in theChain:
logger.debug(b.strBlock())
logger.debug("|-----------------------------------------|")
return "ok"
def showBlockLedger(self, index):
logger.debug("Showing Trasactions data for peer: " + myURI)
blk = chainFunctions.getBlockByIndex(index)
size = len(blk.transactions)
logger.debug("Block Ledger size: " + str(size))
logger.debug("-------")
for b in blk.transactions:
logger.debug(b.strBlock())
logger.debug("-------")
return "ok"
def listPeer(self):
global peers
logger.debug("|--------------------------------------|")
for p in peers:
logger.debug("PEER URI: "+p.peerURI)
logger.debug("|--------------------------------------|")
return "ok"
def calcMerkleTree(self, blockToCalculate):
print ("received: "+str(blockToCalculate))
t1 = time.time()
blk = chainFunctions.getBlockByIndex(blockToCalculate)
trans = blk.transactions
size = len(blk.transactions)
mt = merkle.MerkleTools()
mt.add_leaf(trans, True)
mt.make_tree()
t2 = time.time()
logger.debug("=====5=====>time to generate Merkle Tree size (" + str(size) + ") : " + '{0:.12f}'.format((t2 - t1) * 1000))
print("=====5=====>time to generate Merkle Tree size (" + str(size) + ") : " + '{0:.12f}'.format((t2 - t1) * 1000))
return "ok"
#############################################################################
#############################################################################
###################### Main ################################
#############################################################################
#############################################################################
def main():
global myURI
bootstrapChain2()
print ("Please copy the server address: PYRO:chain.server...... as shown and use it in deviceSimulator.py")
names = sys.argv[1]
ns = Pyro4.locateNS(names)
daemon = Pyro4.Daemon(getMyIP())
uri = daemon.register(R2ac)
myURI = str(uri)
ns.register(myURI, uri, True)
print("uri=" + myURI)
connectToPeers(ns)
daemon.requestLoop()
if __name__ == '__main__':
if len(sys.argv[1:]) < 1:
print ("Command Line usage:")
print (" python r2ac.py <Pyro4 Namer Server IP>")
print (" *** remember launch in a new terminal or machine the name server: pyro4-ns -n <machine IP> ***")
quit()
os.system("clear")
main()