-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzeitcoindb.py
923 lines (778 loc) · 27 KB
/
zeitcoindb.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
#!/usr/bin/env python
#########################################
# Zeitcoin Database Class
#########################################
import sqlite3, sys
from twisted.enterprise import adbapi
from twisted.internet import reactor, defer
class hashtable:
def tconnectdb(self,fname):
db=fname+".db"
dbpool = adbapi.ConnectionPool("sqlite3" , db, check_same_thread=False)
return dbpool
def connectdb(self,fname):
db=fname+".db"
conn = sqlite3.connect(db)
c = conn.cursor()
#print "zeitcoin database has been open"
return conn, c
def createwallet(self,conn,c):
# Create table
c.execute('''CREATE TABLE wallet (guid text, balance integer, time real)''')
conn.commit()
print "wallet table has been created"
return
def createht(self,conn,c):
# Create table
c.execute('''CREATE TABLE hashtable (guid text, address text, port integer, flag integer, time real)''')
conn.commit()
print "hash table has been created"
return
def createtb(self,conn,c):
# Create transaction block table that holds: transaction id, transaction hash, timestamp
c.execute('''CREATE TABLE blocktrans (txid text, thash text, time real)''')
conn.commit()
print "transaction table table has been created"
return
def createlt(self,conn,c):
# Create local transaction list that holds: txid, thash, addresswallet,addressother, amount, timestamp
c.execute('''CREATE TABLE localtrans (txid text, thash text, addresswallet text, addressother text, amount integer, type text, time real)''')
conn.commit()
print "local transaction table has been created"
return
def createaccount(self,conn,c):
# Create the account table that holds: address, privkey, pubkey and account name
c.execute('''CREATE TABLE accounts (address text, privkey text, pubkey text, account text, time real)''')
conn.commit()
print "the account table has been created"
return
def createleader(self,conn,c):
# Create the leader table that holds: sharedkeys, address, port, guid, timestamp
c.execute('''CREATE TABLE leader (sharedkeys text, address text, port integer, guid text, time real)''')
conn.commit()
print "leader table has been created"
return
def addaccount(self,conn,c,account,privkey,pubkey,address,time1):
# add a new account
c.execute("INSERT INTO accounts VALUES ('"+str(address)+"','"+str(privkey)+"','"+str(pubkey)+"','"+str(account)+"','"+str(time1)+"')")
conn.commit()
print "data added to the account table"
return
def _taddaccount(self,txn,account,privkey,pubkey,address,time1):
# add a new account
txn.execute("INSERT INTO accounts VALUES ('"+str(address)+"','"+str(privkey)+"','"+str(pubkey)+"','"+str(account)+"','"+str(time1)+"')")
#conn.commit()
print "data added to the account table"
return
def taddaccount(self,dbpool,account,privkey,pubkey,address,time1):
return dbpool.runInteraction(self._taddaccount,account,privkey,pubkey,address,time1)
def addwallet(self,conn,c,guid,balance,time1):
# add a new account
c.execute("INSERT INTO wallet VALUES ('"+str(guid)+"','"+str(balance)+"','"+str(time1)+"')")
conn.commit()
print "data added to the wallet table"
return
def addtb(self,conn,c,txid,thash,time1):
# add a new transaction to the block transaction id, transaction hash, timestamp
c.execute("INSERT INTO blocktrans VALUES ('"+str(txid)+"','"+str(thash)+"','"+str(time1)+"')")
conn.commit()
print "data added to the transaction block table"
return
def _taddtb(self,txn,txid,thash,time1):
# add a new transaction to the block transaction id, transaction hash, timestamp
txn.execute("INSERT INTO blocktrans VALUES ('"+str(txid)+"','"+str(thash)+"','"+str(time1)+"')")
#conn.commit()
print "data added to the transaction block table"
return
def taddtb(self,dbpool,txid,thash,time1):
return dbpool.runInteraction(self._taddtb,txid,thash,time1)
def addlt(self,conn,c,txid,thash,addresswallet,addressother,amount,type1,time1):
# add a new transaction to the local list txid, thash, addresswallet,addressother, amount, timestamp
c.execute("INSERT INTO localtrans VALUES ('"+str(txid)+"','"+str(thash)+"','"+str(addresswallet)+"','"+str(addressother)+"',"+str(amount)+",'"+str(type1)+"','"+str(time1)+"')")
conn.commit()
print "data added to the local transaction table"
return
def _taddlt(self,txn,txid,thash,addresswallet,addressother,amount,type1,time1):
# add a new transaction to the local list txid, thash, addresswallet,addressother, amount, timestamp
txn.execute("INSERT INTO localtrans VALUES ('"+str(txid)+"','"+str(thash)+"','"+str(addresswallet)+"','"+str(addressother)+"',"+str(amount)+",'"+str(type1)+"','"+str(time1)+"')")
#conn.commit()
print "data added to the local transaction table"
return
def taddlt(self,dbpool,txid,thash,addresswallet,addressother,amount,type1,time1):
return dbpool.runInteraction(self._taddlt,txid,thash,addresswallet,addressother,amount,type1,time1)
def addht(self,conn,c,guid,address,port,flag,time1):
# Insert a row of data
c.execute("INSERT INTO hashtable VALUES ('"+guid+"','"+address+"',"+str(port)+","+str(flag)+","+str(time1)+")")
conn.commit()
print "data added to the hashtable"
return
def _taddht(self,txn,guid,address,port,flag,time1):
# Insert a row of data
txn.execute("INSERT INTO hashtable VALUES ('"+guid+"','"+address+"',"+str(port)+","+str(flag)+","+str(time1)+")")
#conn.commit()
print "data added to the hashtable"
return
def taddht(self,dbpool,guid,address,port,flag,time1):
return dbpool.runInteraction(self._taddht,guid,address,port,flag,time1)
def addleader(self,conn,c,sharedkey, address, port, guid, timestamp):
# adds to the leader table sharedkeys, address, port, guid, timestamp
c.execute("INSERT INTO leader VALUES ('"+str(sharedkey)+"','"+str(address)+"',"+str(port)+",'"+str(guid)+"',"+str(timestamp)+")")
conn.commit()
print "data added to the leader table"
return
def _taddleader(self,txn,sharedkey, address, port, guid, timestamp):
# adds to the leader table sharedkeys, address, port, guid, timestamp
txn.execute("INSERT INTO leader VALUES ('"+str(sharedkey)+"','"+str(address)+"',"+str(port)+",'"+str(guid)+"',"+str(timestamp)+")")
#conn.commit()
print "data added to the leader table"
return
def taddleader(self,dbpool,sharedkey, address, port, guid, timestamp):
return dbpool.runInteraction(self._taddleader,sharedkey, address, port, guid, timestamp)
def deleteht(self,conn,c,guid):
# Delete a row of data
c.execute("DELETE from hashtable where guid='"+guid+"'")
conn.commit()
print "data was delete from the hashtable"
return
def _tdeleteht(self,txn,guid):
# Delete a row of data
txn.execute("DELETE from hashtable where guid='"+guid+"'")
#conn.commit()
print "data was delete from the hashtable"
return
def tdeleteht(self,dbpool,guid):
return dbpool.runInteraction(self._tdeleteht,guid)
def deleteleader(self,conn,c):
# Delete all from the leader table
c.execute("DELETE from leader")
conn.commit()
print "all data was delete from the leader table"
return
def tdeleteleader(self,txn):
# Delete all from the leader table
txn.execute("DELETE from leader")
#conn.commit()
print "all data was delete from the leader table"
return
def tdeleteleader(self,dbpool):
return dbpool.runInteraction(self._tdeleteleader)
def deleteallht(self,conn,c):
# Delete all from the leader table
c.execute("DELETE from hashtable")
conn.commit()
print "all data was delete from the hash table"
return
def deleteallwallet(conn,c):
# Delete all from the leader table
c.execute("DELETE from wallet")
conn.commit()
print "all data was delete from the wallet table"
return
def deleteallblocktrans(self,conn,c):
# Delete all from the leader table
c.execute("DELETE from blocktrans")
conn.commit()
print "all data was delete from the blocktrans table"
return
def deletealllocaltrans(self,conn,c):
# Delete all from the leader table
c.execute("DELETE from localtrans")
conn.commit()
print "all data was delete from the localtrans table"
return
def deleteallaccounts(self,conn,c):
# Delete all from the leader table
c.execute("DELETE from accounts")
conn.commit()
print "all data was delete from the accounts table"
return
def deleteleaderrow(self,con,c, guid):
# Delete a row from the leader table
c.execute("DELETE from leader where guid='"+guid+"'")
conn.commit()
print "data was delete from the leader table"
return
def getalltb(self,c):
# Get all transaction from the block
tblist=[]
for row in c.execute('SELECT * FROM blocktrans order by time'):
tblist.append(row)
#print row
return tblist
def _tgetalltb(self,txn):
# Get all transaction from the block
tblist=[]
for row in txn.execute('SELECT * FROM blocktrans order by time'):
tblist.append(row)
#print row
return tblist
def tgetalltb(self,dbpool):
return dbpool.runInteraction(self._tgetalltb)
def getalllt(self,c):
# Get all local transaction
peerlist=[]
for row in c.execute('SELECT * FROM localtrans'):
peerlist.append(row)
#print row
return peerlist
def _tgetalllt(self,txn):
# Get all local transaction
peerlist=[]
for row in txn.execute('SELECT * FROM localtrans'):
peerlist.append(row)
#print row
return peerlist
def tgetalllt(self,dbpool):
return dbpool.runInteraction(self._tgetalllt)
def _tgetallwallet(self,txn):
# Get all local transaction
peerlist=[]
for row in txn.execute('SELECT * FROM wallet'):
peerlist.append(row)
print row
return peerlist
def tgetallwallet(self,dbpool):
return dbpool.runInteraction(self._tgetallwallet)
def getallwallet(self,c):
# Get all local transaction
peerlist=[]
for row in c.execute('SELECT * FROM wallet'):
peerlist.append(row)
#print row
return peerlist
def getallleader(self,c):
# Get all local transaction
peerlist=[]
for row in c.execute('SELECT * FROM leader'):
peerlist.append(row)
#print row
return peerlist
def _tgetallleader(self,txn):
# Get all local transaction
peerlist=[]
for row in txn.execute('SELECT * FROM leader'):
peerlist.append(row)
#print row
return peerlist
def tgetallleader(self,dbpool):
return dbpool.runInteraction(self._tgetallleader)
def getlisttrans(self,c,starttrans,length):
# Get all local transaction
translist=[]
count1=0
for row in c.execute('SELECT * FROM localtrans order by time'):
if (count1>=starttrans and count1<=starttrans+length):
translist.append(row)
print row
count1=count1+1
return translist
def _tgetlisttrans(self,txn,starttrans,length):
# Get all local transaction
translist=[]
count1=0
for row in txn.execute('SELECT * FROM localtrans order by time'):
if (count1>=starttrans and count1<=starttrans+length):
translist.append(row)
print row
count1=count1+1
return translist
def tgetlisttrans(self,starttrans,length,dbpool):
return dbpool.runInteraction(self._tgetlisttrans,starttrans,length)
def getlistaccounttrans(self,c,account):
# Get all local transaction
translist=[]
count1=0
for row in c.execute("SELECT * FROM localtrans where account='"+account+"'order by timestamp"):
translist.append(row)
print row
count1=count1+1
return translist
def _tgetlistaccounttrans(self,txn,account):
# Get all local transaction
translist=[]
count1=0
for row in txn.execute("SELECT * FROM localtrans where account='"+account+"'order by timestamp"):
translist.append(row)
print row
count1=count1+1
return translist
def tgetlistaccounttrans(self,account,dbpool):
return dbpool.runInteraction(self._tgetlistaccounttrans,account)
def getaddresstrans(self,c,address):
# Get all local transaction
translist=[]
count1=0
c.execute("SELECT * FROM localtrans where addresswallet='"+address+"'")
res=c.fetchone()
return res
def _tgetaddresstrans(self,txn,address):
# Get all local transaction
translist=[]
count1=0
txn.execute("SELECT * FROM localtrans where addresswallet='"+address+"'")
res=txn.fetchone()
return res
def tgetaddresstrans(self,address,dbpool):
return dbpool.runInteraction(self._tgetaddresstrans,address)
def getallaccount(self,c):
# Get all account:
accountlist=[]
for row in c.execute('SELECT * FROM accounts'):
accountlist.append(row)
#print row
return accountlist
def _tgetallaccount(self,txn):
# Get all account:
accountlist=[]
for row in txn.execute('SELECT * FROM accounts'):
accountlist.append(row)
#print row
return accountlist
def tgetallaccount(self,dbpool):
return dbpool.runInteraction(self._tgetallaccount)
def getaddresses(self,c,account):
# Get all account:
addresslist=[]
for row in c.execute("SELECT address FROM accounts where account='"+account+"'"):
addresslist.append(row[0])
print row
return addresslist
def _tgetaddresses(self,txn,account):
# Get all account:
addresslist=[]
for row in txn.execute("SELECT address FROM accounts where account='"+account+"'"):
addresslist.append(row[0])
print row
return addresslist
def tgetaddress(self,account,dbpool):
return dbpool.runInteraction(self._tgetaddress,account)
def getallht(self,c):
peerlist=[]
for row in c.execute('SELECT * FROM hashtable'):
peerlist.append(row)
#print row
return peerlist
def _tgetallht(self,txn):
peerlist=[]
for row in txn.execute('SELECT * FROM hashtable'):
peerlist.append(row)
#print row
return peerlist
def tgetallht(self,dbpool):
return dbpool.runInteraction(self._tgetallht)
def getallguidht(self,c):
peerlist=[]
for row in c.execute('SELECT guid FROM hashtable'):
peerlist.append(row[0])
#print row
return peerlist
def _tgetallguidht(self,txn):
peerlist=[]
for row in txn.execute('SELECT guid FROM hashtable'):
peerlist.append(row[0])
#print row
return peerlist
def tgetallguidht(self,dbpool):
return dbpool.runInteraction(self._tgetallguidht)
def getlineht(self,c,guid):
c.execute("SELECT * FROM hashtable WHERE guid='"+guid+"'")
res=c.fetchone()
return res
def getaddress(self,c,guid):
c.execute("SELECT address,port FROM hashtable WHERE guid='"+guid+"'")
res=c.fetchone()
address=res[0]
port=res[1]
return address,port
def _tgetaddress(self,txn,guid,d1):
txn.execute("SELECT address,port FROM hashtable WHERE guid='"+guid+"'")
res=txn.fetchone()
address=res[0]
port=res[1]
return address,port,guid,d1
def tgetaddress(self,dbpool,guid,d1):
print "guid = ",guid
print "dbpool = ",dbpool
return dbpool.runInteraction(self._tgetaddress,guid,d1)
def getbalance(self,c,guid):
c.execute("SELECT balance FROM wallet WHERE guid='"+guid+"'")
res=c.fetchone()
balance=res[0]
return balance
def _tgetbalance(self,txn,guid):
#print "guid-",guid
txn.execute("SELECT balance FROM wallet WHERE guid='"+guid+"'")
res=txn.fetchone()
balance=res[0]
return balance
def tgetbalance(self,guid,dbpool):
#print "guid, = ",guid
return dbpool.runInteraction(self._tgetbalance,guid)
def getnewbalance(self,c,guid):
c.execute("SELECT sum(amount) FROM localtrans WHERE type='received'")
res=c.fetchone()
received=res[0]
c.execute("SELECT sum(amount) FROM localtrans WHERE type='sent'")
res=c.fetchone()
sent=res[0]
return received-sent
def _tgetnewbalance(self,txn):
c.execute("SELECT sum(amount) FROM localtrans WHERE type='received'")
res=c.fetchone()
received=res[0]
c.execute("SELECT sum(amount) FROM localtrans WHERE type='sent'")
res=c.fetchone()
sent=res[0]
return int(received)-int(sent)
def tgetnewbalance(self,dbpool):
#print "guid, = ",guid
return dbpool.runInteraction(self._tgetnewbalance,guid)
def getaccountbalance(self,c,account):
c.execute("SELECT sum(amount) FROM localtrans WHERE type='received' and account='"+account+"'")
res=c.fetchone()
received=res[0]
c.execute("SELECT sum(amount) FROM localtrans WHERE type='sent'and account='"+account+"'")
res=c.fetchone()
sent=res[0]
return received-sent
def _tgetaccountbalance(self,txn,account):
txn.execute("SELECT sum(amount) FROM localtrans WHERE type='received' and account='"+account+"'")
res=txn.fetchone()
received=res[0]
txn.execute("SELECT sum(amount) FROM localtrans WHERE type='sent'and account='"+account+"'")
res=txn.fetchone()
sent=res[0]
return received-sent
def tgetaccountbalance(self,account,dbpool):
return dbpool.runInteraction(self._tgetaccountbalance,account)
def getguid(self,c):
c.execute("SELECT guid FROM wallet")
res=c.fetchone()
guid=res[0]
return guid
def _tgetguid(self,txn):
txn.execute("SELECT guid FROM wallet")
res=txn.fetchone()
guid=res[0]
print "getguid=",guid
return guid
def tgetguid(self,dbpool):
return dbpool.runInteraction(self._tgetguid)
def gettb(self,c,thash):
# Get a transaction from the block
c.execute("SELECT * FROM blocktrans WHERE thash='"+thash+"'")
res=c.fetchone()
txid=res[0]
thash=res[1]
time1=res[2]
return txid,thash,time1
def _tgettb(self,txn,thash):
# Get a transaction from the block
txn.execute("SELECT * FROM blocktrans WHERE thash='"+thash+"'")
res=txn.fetchone()
txid=res[0]
thash=res[1]
time1=res[2]
return txid,thash,time1
def tgettb(self,thash,dbpool):
return dbpool.runInteraction(self._tgettb,thash)
def getlt(self,c,txid):
# Get a txid from the local transaction list
c.execute("SELECT * FROM localtrans WHERE txid='"+txid+"'")
# add a check in case of nothing return
res=c.fetchone()
txid=res[0]
thash=res[1]
addresswallet=res[2]
addressother=res[3]
amount=res[4]
time1=res[5]
return txid,thash,addresswallet,addressother,time1
def _tgetlt(self,txn,txid):
# Get a txid from the local transaction list
txn.execute("SELECT * FROM localtrans WHERE txid='"+txid+"'")
# add a check in case of nothing return
res=txn.fetchone()
txid=res[0]
thash=res[1]
addresswallet=res[2]
addressother=res[3]
amount=res[4]
time1=res[5]
return txid,thash,addresswallet,addressother,time1
def tgetlt(self,thash,dbpool):
return dbpool.runInteraction(self._tgetlt,thash)
def getaccount(self,c,address):
# Get an account address text, privkey text, pubkey text, account text, time real
c.execute("SELECT * FROM accounts WHERE address='"+address+"'")
# add a check in case of nothing return
res=c.fetchone()
address=res[0]
privkey=res[1]
pubkey=res[2]
account=res[3]
time1=res[4]
return address,privkey,pubkey,account,time1
def _tgetaccount(self,txn,address):
# Get an account address text, privkey text, pubkey text, account text, time real
txn.execute("SELECT * FROM accounts WHERE address='"+address+"'")
# add a check in case of nothing return
res=txn.fetchone()
address=res[0]
privkey=res[1]
pubkey=res[2]
account=res[3]
time1=res[4]
return address,privkey,pubkey,account,time1
def tgetaccount(self,address,dbpool):
return dbpool.runInteraction(self._tgetaccount,address)
def getleader(self,guid):
# Get a row from the leader table based on guid
c.execute("SELECT sharedkey, address, port, guid, time FROM leader WHERE guid='"+guid+"'")
# add a check in case of nothing return
res=c.fetchone()
address=res[0]
port=res[1]
return
def _tgetleader(self,txn,guid):
# Get a row from the leader table based on guid
txn.execute("SELECT sharedkey, address, port, guid, time FROM leader WHERE guid='"+guid+"'")
# add a check in case of nothing return
res=txn.fetchone()
address=res[0]
port=res[1]
return
def tgetleader(self,guid,dbpool):
return dbpool.runInteraction(self._tgetleader,guid)
def getkeys(self,guid):
# Get a row from the leader table based on guid
keylist=[]
for row in c.execute('SELECT sharedkey FROM leader'):
keylist.append(row[0])
print row
return keylist
def _tgetkeys(self,guid):
# Get a row from the leader table based on guid
keylist=[]
for row in txn.execute('SELECT sharedkey FROM leader'):
keylist.append(row[0])
print row
return keylist
def tgetkeys(self,guid,dbpool):
return dbpool.runInteraction(self._tgetkeys,guid)
def updatetime(self,conn,c,guid):
time1=time.time() #taddht(self,dbpool,guid,address,port,flag,time1)
c.execute("UPDATE hashtable SET time = "+str(time1)+" where guid='"+guid+"'")
conn.commit()
print "time was updated in the hashtable"
return
def _tupdatetime(self,txn,guid):
time1=time.time()
txn.execute("UPDATE hashtable SET time = "+str(time1)+" where guid='"+guid+"'")
#conn.commit()
print "time was updated in the hashtable"
return
def tupdatetime(self,dbpool,guid):
return dbpool.runInteraction(self._tupdatetime,guid)
def _tupdatepeer(self,txn,guid,address,port):
time1=time.time()
txn.execute("UPDATE hashtable SET address = "+str(address)+", port = "+str(port)+",flag = 0 ,time = "+str(time1)+" where guid='"+guid+"'")
#conn.commit()
print "peer was updated in the hashtable"
return
def tupdatepeer(self,dbpool,guid,address,port):
return dbpool.runInteraction(self._tupdatepeer,guid,address,port)
def updatebalance(self,conn,c,guid):
balance=self.getnewbalance(c,guid)
c.execute("UPDATE hashtable SET balance = "+str(balance)+" where guid='"+guid+"'")
conn.commit()
print "balance was updated in the wallet table"
return
def _tupdatebalance(self,txn,balance,guid):
#balance=self.getnewbalance(c,guid)
txn.execute("UPDATE hashtable SET balance = "+str(balance)+" where guid='"+guid+"'")
conn.commit()
print "balance was updated in the wallet table"
return
def tupdatebalance(self,dbpool,balance,guid):
return dbpool.runInteraction(self._tupdatebalance,guid)
def updateflag(self,conn,c,guid,flag):
c.execute("UPDATE hashtable SET flag = "+str(flag)+" where guid='"+guid+"'")
conn.commit()
print "flag was updated in the hashtable"
return
def _tupdateflag(self,txn,guid,flag):
txn.execute("UPDATE hashtable SET flag = "+str(flag)+" where guid='"+guid+"'")
#conn.commit()
print "flag was updated in the hashtable"
return
def tupdateflag(self,dbpool,guid,flag):
return dbpool.runInteraction(self._tupdateflag,guid,flag)
def numht(self,c):
c.execute('SELECT count(*) FROM hashtable')
res=c.fetchone()
count1=int(res[0])
print "There are "+str(count1)+" rows in the hashtable"
return count1
def _tnumht(self,txn):
txn.execute('SELECT count(*) FROM hashtable')
res=txn.fetchone()
count1=int(res[0])
print "There are "+str(count1)+" rows in the hashtable"
return count1
def tnumht(self,dbpool):
return dbpool.runInteraction(self._tnumht)
def numtb(self,c):
c.execute('SELECT count(*) FROM blocktrans')
res=c.fetchone()
count1=int(res[0])
print "There are "+str(count1)+" rows in the blocktrans table"
return count1
def _tnumtb(self,txn):
txn.execute('SELECT count(*) FROM blocktrans')
res=txn.fetchone()
count1=int(res[0])
print "There are "+str(count1)+" rows in the blocktrans table"
return count1
def tnumtb(self,dbpool):
return dbpool.runInteraction(self._tnumtb)
def numtg(self,c):
c.execute('SELECT count(*) FROM leader')
res=c.fetchone()
count1=int(res[0])
print "There are "+str(count1)+" rows in the leader table"
return count1
def _tnumtg(self,txn):
txn.execute('SELECT count(*) FROM leader')
res=txn.fetchone()
count1=int(res[0])
print "There are "+str(count1)+" rows in the leader table"
return count1
def tnumtg(self,dbpool):
return dbpool.runInteraction(self._tnumtg)
def checkfullht(self,conn,c):
# Check to see if hash table is full (more than 127 rows)
full=False
self.numht(c)
if (count1>127):
full=True
return full
def _tcheckfullht(self,txn):
# Check to see if hash table is full (more than 127 rows)
full=False
#self.numht(c)
txn.execute('SELECT count(*) FROM hashtable')
res=txn.fetchone()
count1=int(res[0])
if (count1>127):
full=True
return full
def tcheckfullht(self,dbpool):
return dbpool.runInteraction(self._tcheckfullht)
def checkguid(self,conn,c,guid):
# Check to see if a guid is in the hash table
guidexist=False
c.execute("SELECT count(*) FROM hashtable where guid='"+guid+"'")
res=c.fetchone()
count1=int(res[0])
print "There are "+str(count1)+" rows in the hashtable with that guid"
if (count1>0):
guidexist=True
return guidexist
def _tcheckguid(self,txn,guid):
# Check to see if a guid is in the hash table
guidexist=False
txn.execute("SELECT count(*) FROM hashtable where guid='"+guid+"'")
res=txn.fetchone()
count1=int(res[0])
print "There are "+str(count1)+" rows in the hashtable with that guid"
if (count1>0):
guidexist=True
return guidexist
def tcheckguid(self,dbpool,guid):
return dbpool.runInteraction(self._tcheckguid,guid)
def _tcheckpeer(self,txn,guid,address,port):
# Check to see if a guid is in the hash table
guidexist=False
txn.execute("SELECT count(*) FROM hashtable where guid='"+str(guid)+"' and address='"+str(address)+"' and port'"+str(port)+"'")
res=txn.fetchone()
count1=int(res[0])
print "There are "+str(count1)+" rows in the hashtable with that guid, address and port"
if (count1>0):
guidexist=True
return guidexist
def tcheckpeer(self,dbpool,guid,address,port):
return dbpool.runInteraction(self._tcheckpeer)
def _tgetflag(self,txn,guid):
# Check to see if a guid is in the hash table
guid=0
txn.execute("SELECT guid FROM hashtable where flag=0 orderby timestamp")
res=txn.fetchone()
guid=int(res[0])
print "There is an inactive flag"
return guid
def tgetflag(self,dbpool):
return dbpool.runInteraction(self._tgetflag)
def checktrans(self,conn,c,txid):
# Check to see if a guid is in the hash table
txidexist=False
c.execute("SELECT count(*) FROM blocktrans where txid='"+str(txid)+"'")
res=c.fetchone()
count1=int(res[0])
print "There are "+str(count1)+" rows in the hashtable with that guid"
if (count1>0):
txidexist=True
return txidexist
def _tchecktrans(self,txn,txid):
# Check to see if a guid is in the hash table
txidexist=False
txn.execute("SELECT count(*) FROM blocktrans where txid='"+str(txid)+"'")
res=txn.fetchone()
count1=int(res[0])
print "There are "+str(count1)+" rows in the hashtable with that guid"
if (count1>0):
txidexist=True
return txidexist
def tchecktrans(self,txid,dbpool):
return dbpool.runInteraction(self._tchecktrans,txid)
def closedb(self,conn):
conn.close()
#print "zeitcoin database has been closed"
return
def tclosedb(self,dbpool):
dbpool.close()
#print "zeitcoin database has been closed"
return
@defer.deferredGenerator
def showhashdb():
print "=============Hash table Twisted=================="
ht=hashtable()
dbpool=ht.tconnectdb('zeitcoin')
wfd = defer.waitForDeferred(ht.tgetallht(dbpool))
yield wfd
list1 = wfd.getResult()
for i in list1:
print i
ht.tclosedb(dbpool)
reactor.stop()
return
def showhashdb1():
print "=================Hash table======================="
ht=hashtable()
conn,c=ht.connectdb('zeitcoin')
list1=ht.getallht(c)
for i in list1:
print i
ht.closedb(conn)
return
def main():
print "Testing the zeitcoin database class...."
print
showhashdb1()
print
showhashdb()
reactor.run()
print
print "Finish testing the zeitcoin database class"
sys.exit(0)
if __name__ == "__main__" : main()