-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzeitcoinamp.py
2018 lines (1878 loc) · 71.9 KB
/
zeitcoinamp.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#########################################
# Zeitcoin AMP Class
#########################################
import sys, os, time, threading, hashlib, random
from zeitcoindb import hashtable
from zeitcoinutility import utility,encyption
from zeitcointrans import transactions
from twisted.protocols import amp
from twisted.protocols.amp import AMP
from twisted.web import server
from twisted.application import service, internet
from twisted.internet import reactor, defer, endpoints, task, threads
from twisted.internet.defer import inlineCallbacks, Deferred
from twisted.internet.endpoints import TCP4ClientEndpoint, connectProtocol
from twisted.internet.protocol import Factory
from twisted.internet.threads import deferToThread
ALIVE=1
FNAME='zeitcoin'
ADDRESS='127.0.0.1'
PORT=1234
GUID='1'
TXCOINHASH=''
TXADDRESS=''
TXMESSAGE=''
class Getuuid(amp.Command):
# Get a unique id for a peer (guid)
arguments = [('guid', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.String())]
class Copyht(amp.Command):
# copy a peer hast table and give it to a newly joined peer
arguments = [('guid', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.String())]
class Getnumtb(amp.Command):
# Get the number of transaction in the transaction block
arguments = [('guid', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.Integer())]
class Updatetb(amp.Command):
# Tells the server to send the most recent txid
arguments = [('thash', amp.String()),('guid', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.String())]
class Encryptdata(amp.Command):
# Tells the peer to encrypted the data send to it
arguments = [('guid', amp.String()),('address', amp.String()), ('port', amp.Integer()),('data', amp.String())]
response = [('reply', amp.String())]
class Getnewaddress(amp.Command):
# Tells the server to create a new account and send back its address
arguments = [('guid', amp.String()),('address', amp.String()), ('port', amp.Integer()),('account', amp.String())]
response = [('reply', amp.String())]
class Getleader(amp.Command):
# Tells the server to randomly select a leader by generating a guid and find the nearest peer
arguments = [('clientaddress', amp.String()), ('clientport', amp.Integer()), ('guid', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.String())]
class Setleader(amp.Command):
# Tells the server to be the leader
arguments = [('clientaddress', amp.String()), ('clientport', amp.Integer()), ('guid', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.Boolean())]
class Sendtransaction(amp.Command):
# Tells the server to be the leader
arguments = [('guid', amp.String()),('address', amp.String()), ('port', amp.Integer()), ('coinhash', amp.String()),('receiveraddress', amp.String()), ('message', amp.Integer())]
response = [('reply', amp.Boolean())]
class Gettransaction(amp.Command):
# Tells the server to be the leader
arguments = [('guid', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.Boolean())]
class Boardcasttrans(amp.Command):
# Tells the server to be the leader
arguments = [('txid', amp.Integer()), ('thash', amp.String()), ('ts', amp.Float()),('guid', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.String())]
class Generatecoin(amp.Command):
# Tells the server to generate a new coin
arguments = [('guid', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.String())]
class Acceptcoin(amp.Command):
# Tells the server to accept the newly generated coin
arguments = [('txid', amp.Integer()), ('thash', amp.String()), ('ts', amp.Float()),('guid', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.String())]
class Ping(amp.Command):
# pings a given peer hash table to see what nodes are still alive and remove any that isn't from the hash table
# should be ran once an hour or so
#arguments = []
arguments =[('guid', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.String())]
class Donetourguide(amp.Command):
# pings a given peer hash table to see what nodes are still alive and remove any that isn't from the hash table
# should be ran once an hour or so
#arguments = []
arguments =[('guid', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.String())]
class Getpuzzle(amp.Command):
# Tells the server to generate a new puzzle for a guided tour h0,ts,L,arraytg
arguments = [('guid', amp.String()),('h0', amp.String()),('ts', amp.Float()),('L', amp.Integer()),('arraytg', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.String())]
class Verifypuzzle(amp.Command):
# Tells the server to verify a puzzle for a guided tour
arguments = [('guid', amp.String()),('h0', amp.String()),('hl', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.Boolean())]
class Leaderinfo(amp.Command):
# Tells the server to be the leader
arguments = [('address', amp.String()), ('port', amp.Integer()), ('guid', amp.String())]
response = [('reply', amp.Boolean())]
class Initleader(amp.Command):
# Tells the server to return the closest guid in it's hash table
arguments = [('message', amp.String()),('guid', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.String())]
class Getclosest(amp.Command):
# Tells the server to return the closest guid in it's hash table
arguments = [('guid', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.String())]
class Getclosestpeer(amp.Command):
# Tells the server to return the closest guid in it's hash table
arguments = [('guid', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.String())]
class Sendpublickey(amp.Command):
# Tells the server to send its public key
arguments = [('guid', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.String())]
class Join(amp.Command):
# Tells the server that a new peer has join the network
arguments = [('guid', amp.String()), ('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.String())]
class Leave(amp.Command):
# Tells the server that a peer has left the network
arguments = [('guid', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.String())]
class Sendtotg(amp.Command):
# Tells the server that is acting like a tour guide to verify the hash sent to it from the client
arguments = [('hashvalue', amp.String()), ('stopnumber', amp.Integer()), ('length', amp.Integer()), ('guid', amp.String()), ('ts', amp.Float()),('guid', amp.String()),('address', amp.String()), ('port', amp.Integer())]
response = [('reply', amp.String())]
##########################################################################
# move to another file protocol which holds classes Ziet, clientcommands #
##########################################################################
class Zeit(amp.AMP):
def getuuid(self,guid,address,port):
# tested and works
global ADDRESS,PORT,FNAME
d = str(self.address)+":"+str(self.port)
frm=str(self.address)+":"+str(self.port)
print frm+' received a getuuid message ',d
ut=utility(self.filename,self.address,self.port)
guid=ut.generateguid()
ut.updatepeer(guid,address,port)
return {'reply': guid}
Getuuid.responder(getuuid)
def ping(self,guid,address,port):
# tested and works
global ADDRESS,PORT,FNAME
d = str(self.address)+":"+str(self.port)
frm=str(self.address)+":"+str(self.port)
print frm+' received a ping message ',d
str1="hello: address - "+str(address)+" port - "+str(port)
ut=utility(self.filename,self.address,self.port)
ut.updatepeer(guid,address,port)
return {'reply': str1}
Ping.responder(ping)
def donetourguide(self,guid,address,port):
# tested and works
global ADDRESS,PORT,FNAME
d = str(self.address)+":"+str(self.port)
frm=str(self.address)+":"+str(self.port)
print frm+' received a donetourguide message ',d
self.donewithtg(address,port)
str1="ok"
ut=utility(self.filename,self.address,self.port)
ut.updatepeer(guid,address,port)
return {'reply': str1}
Donetourguide.responder(donetourguide)
def getpuzzle(self,guid,h0,ts,L,arraytg,address,port):
global ADDRESS,PORT,FNAME
d = str(self.address)+":"+str(self.port)
frm=str(self.address)+":"+str(self.port)
print frm+' received a getpuzzle message ',d
gt=guidedtour()
gt.getpuzzle(guid,h0,ts,L,arraytg)
ut=utility(self.filename,self.address,self.port)
ut.updatepeer(guid,address,port)
return {'reply': 'ok'}
Getpuzzle.responder(getpuzzle)
def verifypuzzle(self,guid,h0,hl,address,port):
global ADDRESS,PORT,FNAME
d = str(self.address)+":"+str(self.port)
frm=str(self.address)+":"+str(self.port)
print frm+' received a verifypuzzle message ',d
result=True
gt=guidedtour()
#gt.verifyupuzzle(guid,h0,hl)
flag=0
keylist=[]
h0=firsthash
ht=hashtable()
dbpool=ht.tconnectdb(FNAME)
d = defer.Deferred()
d.addCallback(ht.tnumtb)
d.addCallback(ht.tgetkeys)
d.addCallback(self._verifypuzzle,ht,dbpool)
d.addCallback(ht.tdeleteleader,dbpool)
d.addCallback(self.__verifypuzzle,ht,dbpool)
d.callback(dbpool)
#N=ht.numtg()
#keyslist=ht.getkeys()
ut=utility(self.filename,self.address,self.port)
ut.updatepeer(guid,address,port)
return d #{'reply': result}
Verifypuzzle.responder(verifypuzzle)
def _verifypuzzle(self,ht,dbpool):
print "got to _verifypuzzle"
for i in range(0,L):
tourindex=self.gettourindex(h0,N)
key=keylist[tourindex]
h0=self.tourguideverify(h0,i+1,L,clientguid,key,ts)
#ht.deleteleader()
if (h0==lasthash):
result=True
#store the transaction and boardcast it
else:
result=False
print "message to verify h0-"+h0+" hl-"+hl+" from client-"+guid
return result
def __verifypuzzle(self,result,ht,dbpool):
print "got to _verifypuzzle"
ht.tclosedb(dbpool)
return {'reply': result}
def getnewaddress(self,guid,address,port,account):
# Need to fix database (dbpool)
d = str(self.address)+":"+str(self.port)
frm=str(self.address)+":"+str(self.port)
print frm+" received a getnewaddress message ",d
ut=utility(self.filename,self.address,self.port)
ht=hashtable()
en=encyption()
time1=ut.gettimestamp()
address=ut.generateguid()
pubkey,privkey=en.generatekeys()
dbpool=ht.tconnectdb(FNAME)
d = defer.Deferred()
d.addCallback(ht.taddaccount,account,privkey,pubkey,address,time1)
d.addCallback(self._getnewaddress,address,ht,dbpool)
d.callback(dbpool)
return d #{'reply': count1}
Getnewaddress.responder(getnewaddress)
def _getnewaddress(self,dummy,guid,address,ht,dbpool):
print "got to _getnewaddress"
print "newaddress - ",address
ht.tclosedb(dbpool)
ut=utility(self.filename,self.address,self.port)
ut.updatepeer(guid,address,port)
return {'reply': address}
def getnumtb(self,guid,address,port):
# Need to fix database (dbpool)
d = str(self.address)+":"+str(self.port)
frm=str(self.address)+":"+str(self.port)
print frm+" received a getnumtb message ",d
ht=hashtable()
dbpool=ht.tconnectdb(FNAME)
d = defer.Deferred()
d.addCallback(ht.tnumtb)
d.addCallback(self._getnumtb,ht,dbpool)
d.callback(dbpool)
return d #{'reply': count1}
Getnumtb.responder(getnumtb)
def _getnumtb(self,count1,ht,dbpool):
print "got to _getnumtb"
ht.tclosedb(dbpool)
ut=utility(self.filename,self.address,self.port)
ut.updatepeer(guid,address,port)
return {'reply': count1}
def updatetb(self,thash,guid,address,port):
# Need to fix database (dbpool)
frm=str(self.address)+":"+str(self.port)
d = str(self.address)+":"+str(self.port)
print frm+" received a updatetb message ",d
ht=hashtable()
dbpool=ht.tconnectdb(FNAME)
#conn,c=ht.connectdb(FNAME)
d = defer.Deferred()
d.addCallback(ht.tgetalltb)
d.addCallback(self._updatetb,thash,ht,dbpool)
d.callback(dbpool)
#listtb=ht.getalltb(c)
return d #{'reply': str(newtrans[1])}
Updatetb.responder(updatetb)
def _updatetb(self,listtb,thash,ht,dbpool):
print "got to _updatetb"
trans=listtb[0]
thash1=trans[1]
if (thash==thash1):
newtrans=listtb[1]
else:
newtrans=list
ht.tclosedb(dbpool)
ut=utility(self.filename,self.address,self.port)
ut.updatepeer(guid,address,port)
return {'reply': str(newtrans[1])}
def getleader(self,clientaddress,clientport,guid,address,port):
d = str(self.address)+":"+str(self.port)
frm=str(self.address)+":"+str(self.port)
print frm+" received a getleader message ",d
ut=utility(self.filename,self.address,self.port)
result=self.getleader(address,port,guid)
ut=utility(self.filename,self.address,self.port)
ut.updatepeer(guid,address,port)
return {'reply': 'ok'}
Getleader.responder(getleader)
def boardcasttrans(self,txid,thash,ts,guid,address,port):
global ADDRESS,PORT,FNAME
d = str(self.address)+":"+str(self.port)
frm=str(self.address)+":"+str(self.port)
print frm+" received a boardcast messageb ",d
ut=utility(self.filename,self.address,self.port)
result=self.boardcasttrans(txid,thash,ts)
return {'reply': 'ok'}
Boardcasttrans.responder(boardcasttrans)
def generatecoin(self,guid,address,port):
global ADDRESS,PORT,FNAME
d = str(self.address)+":"+str(self.port)
frm=str(self.address)+":"+str(self.port)
print frm+" received a generatecoin message ",d
#tx=transactions()
self.generatenewcoin()
ut=utility(self.filename,self.address,self.port)
ut.updatepeer(guid,address,port)
return {'reply': 'ok'}
Generatecoin.responder(generatecoin)
def encryptdata(self,guid,address,port,data):
global ADDRESS,PORT,FNAME
d = str(self.address)+":"+str(self.port)
frm=str(self.address)+":"+str(self.port)
print frm+" received an encryptdata message ",d
en=encyption()
pubkey=en.getpubkey(FNAME)
message=en.encyptmessage(data,pubkey)
ut=utility(self.filename,self.address,self.port)
ut.updatepeer(guid,address,port)
return {'reply': str(message)}
Encryptdata.responder(encryptdata)
def acceptcoin(self,txid,thash,ts,guid,address,port):
global ADDRESS,PORT,FNAME
d = str(self.address)+":"+str(self.port)
frm=str(self.address)+":"+str(self.port)
print frm+" received an acceptcoin message ",d
tx=transactions()
tx.acceptcoin(txid,thash,ts)
ut=utility(self.filename,self.address,self.port)
ut.updatepeer(guid,address,port)
return {'reply': 'ok'}
Acceptcoin.responder(acceptcoin)
def setleader(self,clientaddress,clientport,guid,address,port):
global Leaderflag,Clientaddress,Clientport,Clientguid,ADDRESS,PORT
frm=str(self.address)+":"+str(self.port)
d = str(self.address)+":"+str(self.port)
print frm+" received a setleader message ",d
if (Leaderflag==1):
result=False
else:
# store client address,port and guid
self.setleader(clientaddress,clientport,guid)
Leaderflag=1
result=True
ut=utility(self.filename,self.address,self.port)
ut.updatepeer(guid,address,port)
return {'reply': result}
Setleader.responder(setleader)
def sendtransaction(self,guid,address,port,coinhash,receiveraddress,message):
global ADDRESS,PORT,FNAME,TXCOINHASH,TXADDRESS,TXMESSAGE
frm=str(self.address)+":"+str(self.port)
d = str(self.address)+":"+str(self.port)
print frm+" received a sendtransaction message ",d
# store this information to be sent after guide tour
TXCOINHASH=coinhash
TXADDRESS=receiveraddress
TXMESSAGE=message
result=True
ut=utility(self.filename,self.address,self.port)
ut.updatepeer(guid,address,port)
return {'reply': result}
Sendtransaction.responder(sendtransaction)
def gettransaction(self,guid,address,port):
global ADDRESS,PORT,FNAME,TXCOINHASH,TXADDRESS,TXMESSAGE
frm=str(self.address)+":"+str(self.port)
d = str(self.address)+":"+str(self.port)
print frm+" received a sendtransaction message ",d
# store this information to be sent after guide tour
result=str(TXCOINHASH)+":"+str(TXADDRESS)+":"+str(TXMESSAGE)
ut=utility(self.filename,self.address,self.port)
ut.updatepeer(guid,address,port)
return {'reply': result}
Gettransaction.responder(gettransaction)
def leaderinfo(self,address,port,guid):
global Leaderaddress, Leaderport, Leaderguid,ADDRESS,PORT
frm=str(self.address)+":"+str(self.port)
d = str(self.address)+":"+str(self.port)
Leaderaddress=self.address
Leaderport=self.port
Leaderguid=guid
print frm+" received a leaderinfo message ",d
ut=utility(self.filename,self.address,self.port)
ut.updatepeer(guid,address,port)
return {'reply': 'ok'}
Leaderinfo.responder(leaderinfo)
def initleader(self,message,guid,address,port):
global self.filename,self.address,self.port
frm=str(self.address)+":"+str(self.port)
d = str(self.address)+":"+str(self.port)
print frm+" received a initleader message ",d
ut=utility(self.filename,self.address,self.port)
ut.updatepeer(guid,address,port)
return {'reply': 'ok'}
Initleader.responder(initleader)
def sendpublickey(self,guid,address,port):
# tested and works
global self.filename,self.address,self.port
frm=str(self.address)+":"+str(self.port)
d = str(self.address)+":"+str(self.port)
print frm+" received a sendpublickey message ",d
en = encyption()
pubkey=en.getpublickey(FNAME)
ut=utility(self.filename,self.address,self.port)
ut.updatepeer(guid,address,port)
return {'reply': str(pubkey)}
Sendpublickey.responder(sendpublickey)
def join(self,guid,addres,port):
# new peer join command
global ADDRESS,PORT,FNAME
d = str(self.address)+":"+str(self.port)
frm=str(self.address)+":"+str(self.port)
ut=utility(self.filename,self.address,self.port)
guid=ut.generateguid()
result=ut.getht()
result+=guid
return {'reply': result}
Join.responder(join)
def leave(self,guid,address,port):
# peer leave command
global ADDRESS,PORT,FNAME
d = str(self.address)+":"+str(self.port)
frm=str(self.address)+":"+str(self.port)
print frm+' received a leave message ',d
self.leave(guid)
return {'reply': 'ok'}
Leave.responder(leave)
def sendtotg(self,hashvalue,stopnumber,length,guid,ts,guid1,address,port):
# h_{l} = hash(h_{l-1}\; ||\; l\; ||\; L\; ||\; A_{x}\; ||\; ts\; ||\; k_{js})
# ks - pulled from public key
# ts and guid will be stored by the tg and verify when sedtotg is called
global ADDRESS,PORT,FNAME
frm=str(self.address)+":"+str(self.port)
d = str(self.address)+":"+str(self.port)
gt=guidedtour()
print frm+" received a sendtotg message ",d
print "tour guided received h0-"+hashvalue+" stopnumber-"+str(stopnumber)+" L-"+str(length)+" guid-"+guid+" ts-"+str(ts)
#result=gt.tourguideverify(hashvalue,stopnumber,length,guid,ts)
result="true"
ut=utility(self.filename,self.address,self.port)
ut.updatepeer(guid,address,port)
return {'reply': result}
Sendtotg.responder(sendtotg)
def copyht(self,guid,address,port):
# tested and works
global ADDRESS,PORT,FNAME,FNAME
d = str(self.address)+":"+str(self.port)
frm=str(self.address)+":"+str(self.port)
print frm+" received a copyht message ",d
#ut=utility(self.filename,self.address,self.port)
#result=ut.getht()
ht=hashtable()
dbpool=ht.tconnectdb(FNAME)
d = defer.Deferred()
d.addCallback(ht.tgetallht)
d.addCallback(self._copyht,ht,dbpool)
d.callback(dbpool)
return d #{'reply': result}
Copyht.responder(copyht)
def _copyht(self,htlist,ht,dbpool):
print "got to _copyht"
result=''
for row in htlist:
result+=str(row[0])+","+str(row[1])+","+str(row[2])+","+str(row[3])+","+str(row[4])+":"
res=result[:-1]
ht.tclosedb(dbpool)
return {'reply': res}
def getclosest(self,guid,address,port):
# Need to fix database (dbpool)
global self.filename,self.address,self.port
d = str(self.address)+":"+str(self.port)
frm=str(self.address)+":"+str(self.port)
print frm+" received a getclosest message ",d
ht=hashtable()
dbpool=ht.tconnectdb(FNAME)
d = defer.Deferred()
d.addCallback(ht.tgetallguidht)
d.addCallback(self._getclosest,guid,ht,dbpool)
d.addCallback(ht.tgetaddress,dbpool)
d.addCallback(self.__getclosest,ht,dbpool)
d.callback(dbpool)
return d #{'reply': result}
Getclosest.responder(getclosest)
def _getclosest(self,peerlist,guid,ht,dbpool):
print "got to _getclosest"
ut=utility(self.filename,self.address,self.port)
guid1,d1=ut.closestpeer(guid, peerlist)
print "dbpool ",dbpool
print "guid1 ",guid1
print "d1 ",d1
#ht.tclosedb(dbpool)
return guid1,d1
def __getclosest(self,guid1,ht,dbpool):
print "got to __getclosest"
ht.tclosedb(dbpool)
result=str(guid1)
return {'reply': result}
def getclosestpeer(self,guid):
global CLOSESTGUID,CLOSESTADDRESS,CLOSESTPORT,CLOSESTDISTANCE
d = defer.Deferred()
d.addCallback(self._getclosestpeer)
d.addCallback(self.__getclosestpeer)
d.callback(guid)
return d
Getclosestpeer.responder(getclosestpeer)
def __getclosestpeer(self,guid):
global CLOSESTGUID,CLOSESTADDRESS,CLOSESTPORT,CLOSESTDISTANCE
result= str(CLOSESTGUID)+":"+str(CLOSESTADDRESS)+":"+str(CLOSESTPORT)+":"+str(CLOSESTDISTANCE)
return {'reply': result}
@defer.deferredGenerator
def _getclosestpeer(self,guid):
global FNAME,CLOSESTGUID,CLOSESTADDRESS,CLOSESTPORT,CLOSESTDISTANCE
flag=0
d1=999999999999999999999999999999999999999999
cc=clientcommands()
ht=hashtable()
ut=utility(self.filename,self.address,self.port)
conn,c=ht.connectdb(FNAME)
peerlist=ht.getallguidht(c)
guid,d2=ut.closestpeer(peerid, peerlist)
address,port=ht.getaddress(c,guid)
ht.closedb(conn)
if (d2==0): #[4^3 *3/(5-8)*(n^2+3)]^2+3*5+7=.19*(5+0.07)*5-2^2+5*2
flag=1
guid1=guid
while (flag==0):
#guid1,address,port,d2=cc.dogetclosest(address,port,guid) # use yield
wfd = defer.waitForDeferred(cc.dogetclosest(address,port,guid))
yield wfd
data = str(wfd.getResult())
datalist=data.split(":")
guid1=datalist[0]
address=datalist[1]
port=datalist[2]
d2=datalist[3]
if (d2==0):
flag=1
else:
if (d2<d1):
d1=d2
address1=address
port1=port
guid2=guid1
else:
address=self.address1
port=self.port1
guid1=guid2
flag=1
CLOSESTaddress=self.address
CLOSESTport=self.port
CLOSESTGUID=guid1
CLOSESTDISTANCE=D1
#return guid1,address,port
@defer.deferredGenerator
def leave(self,guid):
global FNAME
ht=hashtable()
dbpool=ht.tconnectdb(FNAME)
wfd = defer.waitForDeferred(ht.tdeleteht(dbpool,guid))
yield wfd
ht.tclosedb(dbpool)
print "Guid - "+guid+" is leaving the network"
return
def boardcasttrans(self,txid,thash,ts):
global FNAME
cc=clientcommands()
ht=hashtable()
ut=utility(self.filename,self.address,self.port)
conn,c=ht.connectdb(FNAME)
res=ht.checktrans(conn,c,txid)
if (res==False):
guid=ht.getguid(c)
ht.addtb(conn,c,txid,thash,ts)
peerlist=ht.getallguidht(c)
guid1,d1=ut.closestpeer(guid,peerlist)
guid2,d2=ut.farestpeer(guid,peerlist)
address,port=ht.getaddress(c,guid1)
address1,port1=ht.getaddress(c,guid2)
print "closest peer ",guid1,address,port
print "farest peer ",guid2,address1,port1
cc.doboardcasttrans(address,port,txid,thash,ts)
cc.doboardcasttrans(address1,port1,txid,thash,ts)
else:
print "already exist in the transaction block"
return
@defer.deferredGenerator
def getleader(self,address,port,guid):
global FNAME
cc=clientcommands()
ut=utility(self.filename,self.address,self.port)
randomguid=ut.generateguid()
ht=hashtable()
dbpool=ht.tconnectdb(FNAME)
#conn,c=ht.connectdb(FNAME)
wfd = defer.waitForDeferred(ht.tgetallguidht(dbpool))
yield wfd
peerlist = wfd.getResult()
#peerlist=ht.getallguidht(c)
guid1,d2=ut.closestpeer(randomguid, peerlist)
wfd = defer.waitForDeferred(ht.tgetaddress(dbpool,guid1,d2))
yield wfd
data1 = wfd.getResult()
address1=str(data1[0])
port1=int(data1[1])
#address1,port1=ht.getaddress(c,guid1)
ht.tclosedb(dbpool)
# check if the random is already a leader or a tour guide
#res=self.setleader(address1,port)
wfd = defer.waitForDeferred(cc.dosetleader(str(address1),int(port1),str(address),int(port),str(guid1)))
yield wfd
res = wfd.getResult()
print "res=",res
wfd = defer.waitForDeferred(cc.doleaderinfo(str(address),int(port),str(guid1),str(address1),int(port1)))
yield wfd
res = wfd.getResult()
print "result-",res
print "The leader will have guid - "+guid1+" address - "+address1+" port - "+str(port1)
#wfd = defer.waitForDeferred(cc.doinitleader(str(address1),int(port1)))
#yield wfd
#res = wfd.getResult()
#print "result-",res
return
@defer.deferredGenerator
def setleader(self,address,port,guid):
global Leaderflag,Clientaddress,Clientport,Clientguid,FNAME
gt=guidedtour()
Clientaddress=self.address
Clientport=self.port
Clientguid=guid
#gtlist=gt.gettourguides()
gtlist=[]
N=random.randint(gt.MINTG,gt.MAXTG)
ht=hashtable()
dbpool=ht.tconnectdb(FNAME)
wfd = defer.waitForDeferred(ht.tgetallguidht(dbpool))
yield wfd
peerlist = wfd.getResult()
wfd = defer.waitForDeferred(ht.tnumht(dbpool))
yield wfd
count1 = wfd.getResult()
print "peerlist=",peerlist
for i in range(1,N):
gt = random.randint(0,count1-1)
print "gt=",gt
gtlist.append(peerlist[gt])
gt.getsharedsecret(gtlist)
return
@defer.deferredGenerator
def donewithtg(self,guid,address,port):
global Leaderflag,Clientaddress,Clientport,Clientguid,FNAME,TXCOINHASH,TXADDRESS,TXMESSAGE, Leaderaddress,Leaderport
wfd = defer.waitForDeferred(cc.dogettransaction(str(address),int(port)))
yield wfd
res = str(wfd.getResult())
listres=res.split(":")
TXCOINHASH = str(listres[0])
TXADDRESS = str(listres[1])
TXMESSAGE = str(listres[2])
wfd = defer.waitForDeferred(cc.dogettransaction(str(Leaderaddress),int(Leaderport),str(TXCOINHASH), str(TXADDRESS),str(TXMESSAGE)))
yield wfd
res = str(wfd.getResult())
wfd = defer.waitForDeferred(cc.doinitleader(str(Leaderaddress),int(Leaderport)))
yield wfd
res = wfd.getResult()
print "result-",res
return
@defer.deferredGenerator
def generateht(self):
# Once a hash table is copy the new peer will have to use it to generate its own hash table
global FNAME
ht=hashtable()
dbpool=ht.tconnectdb(FNAME)
#conn,c=ht.connectdb(FNAME)
wfd = defer.waitForDeferred(ht.tgetallguidht(dbpool))
yield wfd
guidlist = wfd.getResult()
#guidlist=ht.getallguidht(c)
for guid in guidlist:
#address,port=ht.getaddress(c,guid)
wfd = defer.waitForDeferred(ht.tgetaddress(dbpool,guid1))
yield wfd
address1,port1 = wfd.getResult()
#guid1,d1,address,port=cc.getclosest(address,port,guid)
wfd = defer.waitForDeferred(cc.getclosest(address,port,guid))
yield wfd
data = str(wfd.getResult())
# check to see if the guid exist if so do nothing
#res=ht.checkguid(conn,c,guid1)
wfd = defer.waitForDeferred(ht.tcheckguid(dbpool,guid1))
yield wfd
res = wfd.getResult()
# else delete the guid and add guid1 to the hash table
if (res==False):
wfd = defer.waitForDeferred(ht.tdeleteht(dbpool,guid1))
yield wfd
#ht.deleteht(conn,c,guid1)
time1=time.time()
flag=0
wfd = defer.waitForDeferred(ht.taddht(dbpool,guid1,address,port,flag,time1))
yield wfd
#ht.addht(conn,c,guid1,address,port,flag,time1)
ht.tclosedb(dbpool)
return
@defer.deferredGenerator
def sendcoin(self,guid,address,port,receiveraddress,coinhash,message):
ut=utility(self.filename,self.address,self.port)
time1=ut.gettimestamp()
wfd = defer.waitForDeferred(cc.doencryptdata(address,port,str(coinhash)))
yield wfd
signmessage1 = wfd.getResult()
senderscript = str(coin)+" "+str(signmessage1)+" "
receiverscript = " decode = verify"
transaction=tx.formattransaction(coinhash,senderscript,receiverscript,receiveraddress,message)
txid=hashlib.sha1(transaction).hexdigest()
cc.doacceptcoin(address,port,txid,transaction,receiveraddress,coinhash,time1)
return
@defer.deferredGenerator
def generatenewcoin(self): #doacceptcoin(self,address,port,txid1,thash1,ts1)
# will generate a new coin and give it to a random client
global FNAME
previoushash="00000000000000000000000000000000"
ut=utility(self.filename,self.address,self.port)
en= encyption()
ht=hashtable()
cc=clientcommands()
gt=guidedtour()
tx=transactions()
time1=ut.gettimestamp()
dbpool=ht.tconnectdb(FNAME)
randaddress=ut.generateguid()
coin=ut.generateguid()
privkey=en.getprivatekey(FNAME)
pubkey=en. getpublickey(FNAME)
signmessage=en.signmessage(coin,privkey)
# publickey = receiver public key
wfd = defer.waitForDeferred(ht.tgetallguidht(dbpool))
yield wfd
peerlist = wfd.getResult()
guid,d2=ut.closestpeer(randaddress, peerlist)
wfd = defer.waitForDeferred(ht.tgetaddress(dbpool,guid,d2))
yield wfd
data1 = wfd.getResult()
address=str(data1[0])
port=int(data1[1])
ht.tclosedb(dbpool)
wfd = defer.waitForDeferred(cc.dogetnewaddress(address,port,"newcoin"))
yield wfd
receiveraddress = str(wfd.getResult())
print "receiveraddress=",receiveraddress
wfd = defer.waitForDeferred(cc.doencryptdata(address,port,str(coin)))
yield wfd
signmessage1 = wfd.getResult()
#signmessage1=en.encyptmessage(coin,publickey)
senderscript = str(coin)+" "+str(signmessage1)+" "
receiverscript = " decode = verify"
message="test"
transaction=tx.formattransaction(previoushash,senderscript,receiverscript,receiveraddress,message)
txid=hashlib.sha1(transaction).hexdigest()
print "new coinhash - ",coin
print "transaction - ",transaction
print "txid - ",txid
print "sending it to guid-"+guid+" at address-"+address+" port-"+str(port)
cc.doacceptcoin(address,port,txid,transaction,receiveraddress,coin,time1)
previousthash,lensender,senderscript,receiveraddress,lenreceiver,receiverscript,lenmessage,message = tx.decodetransaction(transaction)
print "decoding transaction......."
print "previoushash = ",previousthash
print "lensender = ",lensender
print "senderrscript = ",senderscript
print "receiveraddress = ",receiveraddress
print "lenreceiver = ", lenreceiver
print "receiverscript = ",receiverscript
print "lenmessage = ",lenmessage
print "message = ",message
# send this to the randaddress
return
@defer.deferredGenerator
def checkblocktrans(txid):
# check the network for updated transactions
global FNAME,highestnumtb,updateaddress,updateport
highestnumtb=0
ht=hashtable()
cc=clientcommands()
#conn,c=ht.connectdb(FNAME)
dbpool=ht.tconnectdb(FNAME)
#peerlist=ht.getallguidht(c)
wfd = defer.waitForDeferred(ht.tgetallguidht(dbpool))
yield wfd
peerlist = wfd.getResult()
for peer in peerlist:
#address,port=ht.getaddress(c,peer)
wfd = defer.waitForDeferred(ht.tgetaddress(dbpool,guid1))
yield wfd
address1,port1 = wfd.getResult()
wfd = defer.waitForDeferred(cc.dogetnumtb(address,port))
yield wfd
data = int(wfd.getResult())
if (data>highestnumtb):
highestnumtb=data
updateaddress=self.address
updateport=self.port
ht.tclosedb(dbpool)
# find the peer with the highest numtb and request update from that peer
self.updateblocktrans(txid,updateaddress,updateport,highestnumtbnum)
return
@defer.deferredGenerator
def updateblocktrans(txid,guid,address,port,num):
# update transaction block with new txid
global FNAME,highestnumtb,updateaddress,updateport
cc=clientcommands()
ht=hashtable()
#conn,c=ht.connectdb(FNAME)
dbpool=ht.tconnectdb(FNAME)
wfd = defer.waitForDeferred(ht.tnumtb(dbpool))
yield wfd
numtb = int(wfd.getResult())
#numtb=ht.numtb(c)
count1=0
while (numtb<highestnumtb and count1<50):
# find a way to loop through this to get all the updates
wfd = defer.waitForDeferred(cc.doupdatetb(address,port))
yield wfd
count+=1
wfd = defer.waitForDeferred(ht.tnumtb(dbpool))
yield wfd
numtb = int(wfd.getResult())
#numtb=ht.numtb(c)
ht.tclosedb(dbpool)
return
class clientcommands:
def __init__(self,filename,address,port):
self.filename = filename
self.address = address
self.port = port
def doping(self,address1,port1,guid):
global GUID
dest=str(address1)+":"+str(port1)
frm=str(self.address)+":"+str(self.port)
destination = TCP4ClientEndpoint(reactor, address1, port1)
pingDeferred = connectProtocol(destination, AMP())
def connected(ampProto):
print frm+" is sending a ping message to ",dest
return ampProto.callRemote(Ping, guid=GUID,address=self.address, port=self.port )
pingDeferred.addCallback(connected)
def handleFailure(f):
#would this be the best way. will it block if it timeout?
print "errback"
print "we got an exception: %s" % (f.getTraceback(),)
f.trap(RuntimeError)
ut=utility(self.filename,self.address,self.port)
message = "no reply from the ping message"
ut.messagefail(message, guid)
ut.updateflag(guid)
return False
pingDeferred.addErrback(handleFailure)
def pinged(result):
print frm+" has received a ping reply from ", dest
print "result = ",result['reply']
return result['reply']
pingDeferred.addCallback(pinged)
return pingDeferred
def dodonetourguide(self,address1,port1):
global GUID
dest=str(address1)+":"+str(port1)
frm=str(self.address)+":"+str(self.port)
destination = TCP4ClientEndpoint(reactor, address1, port1)
donetourguideDeferred = connectProtocol(destination, AMP())
def connected(ampProto):
print frm+" is sending a donetourguide message to ",dest
return ampProto.callRemote(Donetourguide, guid=GUID,address=self.address, port=self.port )
donetourguideDeferred.addCallback(connected)
def handleFailure(f):
print "errback"
print "we got an exception: %s" % (f.getTraceback(),)
f.trap(RuntimeError)
ut=utility(self.filename,self.address,self.port)
message = "no reply from the ping message"
ut.messagefail(message, guid)
ut.updateflag(guid)
return False
donetourguideDeferred.addErrback(handleFailure)
def donetourguideed(result):
print frm+" has received a donetourguide reply from ", dest
print "result = ",result['reply']
return result['reply']
donetourguideDeferred.addCallback(donetourguideed)
return donetourguideDeferred
def dogetguid(self,address,port):
global GUID
dest=str(address)+":"+str(port)
frm=str(self.address)+":"+str(self.port)
destination = TCP4ClientEndpoint(reactor, address, port)
getguidDeferred = connectProtocol(destination, AMP())
def connected(ampProto):
print frm+" is sending a getguid message to ",dest
return ampProto.callRemote(Getuuid,guid=GUID,address=self.address,port=self.port)
getguidDeferred.addCallback(connected)
def handleFailure(f):
f.trap(RuntimeError)
ut=utility(self.filename,self.address,self.port)
message = "no reply from the getguid message"
ut.messagefail(message, guid)
ut.updateflag(guid)
return False
getguidDeferred.addErrback(handleFailure)
def getguided(result):
print frm+" has received a getguid reply from ", dest
print "result = ", result['reply']
return result['reply']
getguidDeferred.addCallback(getguided)