Skip to content

Commit caf7bae

Browse files
cis-muzahidshportix
authored andcommitted
SQL refactoring and testing
1 parent 427965c commit caf7bae

27 files changed

+1000
-616
lines changed

src/bitmessagemain.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
app_dir = pathmagic.setup()
2020

2121
import depends
22+
2223
depends.check_dependencies()
2324

25+
import helper_sql
2426
import getopt
2527
import multiprocessing
2628
# Used to capture a Ctrl-C keypress so that Bitmessage can shutdown gracefully.
@@ -81,6 +83,7 @@ def signal_handler(signum, frame):
8183

8284
class Main(object):
8385
"""Main PyBitmessage class"""
86+
8487
def start(self):
8588
"""Start main application"""
8689
# pylint: disable=too-many-statements,too-many-branches,too-many-locals
@@ -261,8 +264,8 @@ def start(self):
261264
while state.shutdown == 0:
262265
time.sleep(1)
263266
if (
264-
state.testmode
265-
and time.time() - state.last_api_response >= 30
267+
state.testmode
268+
and time.time() - state.last_api_response >= 30
266269
):
267270
self.stop()
268271
elif not state.enableGUI:
@@ -385,7 +388,6 @@ def main():
385388
if __name__ == "__main__":
386389
main()
387390

388-
389391
# So far, the creation of and management of the Bitmessage protocol and this
390392
# client is a one-man operation. Bitcoin tips are quite appreciated.
391393
# 1H5XaDA6fYENLbknwZyjiYXYPQaFjjLX2u

src/class_sqlThread.py

+324-572
Large diffs are not rendered by default.

src/sql/init_version_1.sql

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE inventory ADD first20bytesofencryptedmessage blob DEFAULT '';
2+
3+
UPDATE settings SET value = 2 WHERE key = 'version';

src/sql/init_version_10.sql

+2
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ CREATE TABLE `addressbook` (
1313
INSERT INTO addressbook SELECT label, address FROM old_addressbook;
1414

1515
DROP TABLE old_addressbook;
16+
17+
UPDATE settings SET value = 11 WHERE key = 'version';

src/sql/init_version_2.sql

+2
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ CREATE TABLE `inventory` (
2727
INSERT INTO inventory SELECT hash, objecttype, streamnumber, payload, receivedtime FROM inventory_backup;
2828

2929
DROP TABLE inventory_backup;
30+
31+
UPDATE settings SET value = 3 WHERE key = 'version';

src/sql/init_version_3.sql

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
--
44

55
ALTER TABLE inventory ADD tag blob DEFAULT '';
6+
7+
UPDATE settings SET value = 4 WHERE key = 'version';

src/sql/init_version_4.sql

+2
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ CREATE TABLE `pubkeys` (
1515
) ;
1616

1717
DELETE FROM inventory WHERE objecttype = 'pubkey';
18+
19+
UPDATE settings SET value = 5 WHERE key = 'version';

src/sql/init_version_5.sql

+2
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ CREATE TABLE `objectprocessorqueue` (
1010
`data` blob,
1111
UNIQUE(objecttype, data) ON CONFLICT REPLACE
1212
) ;
13+
14+
UPDATE settings SET value = 6 WHERE key = 'version';

src/sql/init_version_6.sql

+2
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ CREATE TABLE `objectprocessorqueue` (
2323
`data` blob,
2424
UNIQUE(objecttype, data) ON CONFLICT REPLACE
2525
) ;
26+
27+
UPDATE settings SET value = 7 WHERE key = 'version';

src/sql/init_version_7.sql

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ DELETE FROM inventory WHERE objecttype = 1;
99
DELETE FROM pubkeys;
1010

1111
UPDATE sent SET status='msgqueued' WHERE status='doingmsgpow' or status='badkey';
12+
13+
UPDATE settings SET value = 8 WHERE key = 'version';

src/sql/init_version_8.sql

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
--
66

77
ALTER TABLE inbox ADD sighash blob DEFAULT '';
8+
9+
UPDATE settings SET value = 9 WHERE key = 'version';

src/sql/init_version_9.sql

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ CREATE TABLE `sent` (
2727
`ackdata` blob,
2828
`senttime` integer,
2929
`lastactiontime` integer,
30-
`sleeptill` int,
30+
`sleeptill` integer,
3131
`status` text,
3232
`retrynumber` integer,
3333
`folder` text,
@@ -72,3 +72,5 @@ CREATE TABLE `pubkeys` (
7272
INSERT INTO pubkeys SELECT address, addressversion, transmitdata, `time`, usedpersonally FROM pubkeys_backup;
7373

7474
DROP TABLE pubkeys_backup;
75+
76+
UPDATE settings SET value = 10 WHERE key = 'version';

src/sql/initialize_schema_v1.sql

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
CREATE TABLE `inbox` (
2+
`msgid` blob,
3+
`toaddress` text,
4+
`fromaddress` text,
5+
`subject` text,
6+
`received` text,
7+
`message` text,
8+
`folder` text,
9+
`encodingtype` int,
10+
`read` bool,
11+
UNIQUE(msgid) ON CONFLICT REPLACE
12+
);
13+
14+
CREATE TABLE `sent` (
15+
`msgid` blob,
16+
`toaddress` text,
17+
`toripe` blob,
18+
`fromaddress` text,
19+
`subject` text,
20+
`message` text,
21+
`ackdata` blob,
22+
`lastactiontime` integer,
23+
`status` text,
24+
`pubkeyretrynumber` integer,
25+
`msgretrynumber` integer,
26+
`folder` text,
27+
`encodingtype` int
28+
);
29+
30+
CREATE TABLE `subscriptions` (
31+
`label` text,
32+
`address` text,
33+
`enabled` bool
34+
);
35+
36+
CREATE TABLE `addressbook` (
37+
`label` text,
38+
`address` text
39+
);
40+
41+
CREATE TABLE `blacklist` (
42+
`label` text,
43+
`address` text,
44+
`enabled` bool
45+
);
46+
47+
CREATE TABLE `whitelist` (
48+
`label` text,
49+
`address` text,
50+
`enabled` bool
51+
);
52+
53+
CREATE TABLE `pubkeys` (
54+
`hash` blob,
55+
`transmitdata` blob,
56+
`time` int,
57+
`usedpersonally` text,
58+
UNIQUE(hash) ON CONFLICT REPLACE
59+
);
60+
61+
CREATE TABLE `inventory` (
62+
`hash` blob,
63+
`objecttype` text,
64+
`streamnumber` int,
65+
`payload` blob,
66+
`receivedtime` integer,
67+
UNIQUE(hash) ON CONFLICT REPLACE
68+
);
69+
70+
CREATE TABLE `knownnodes` (
71+
`timelastseen` int,
72+
`stream` int,
73+
`services` blob,
74+
`host` blob,
75+
`port` blob,
76+
UNIQUE(host, stream, port) ON CONFLICT REPLACE
77+
);
78+
79+
CREATE TABLE `settings` (
80+
`key` blob,
81+
`value` blob,
82+
UNIQUE(key) ON CONFLICT REPLACE
83+
);
84+
85+
INSERT INTO subscriptions VALUES ('Bitmessage new releases/announcements', 'BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw', 1);
86+
87+
INSERT INTO settings VALUES('version', 1);
88+
89+
INSERT INTO settings VALUES('lastvacuumtime', CAST(strftime('%s', 'now') AS STR) );
90+
91+
INSERT INTO inventory VALUES( '', 'pubkey', 1, '', 1);
92+

src/tests/core.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from bmconfigparser import config
2525
from helper_msgcoding import MsgEncode, MsgDecode
26-
from helper_sql import sqlQuery
26+
from helper_sql import sqlQuery, sqlExecute
2727
from network import asyncore_pollchoose as asyncore, knownnodes
2828
from network.bmproto import BMProto
2929
from network.connectionpool import BMConnectionPool
@@ -412,6 +412,22 @@ def test_adding_two_same_case_sensitive_addresses(self):
412412
self.delete_address_from_addressbook(address1)
413413
self.delete_address_from_addressbook(address2)
414414

415+
def test_sqlscripts(self):
416+
""" Test sql statements"""
417+
418+
sqlExecute('create table if not exists testtbl (id integer)')
419+
tables = list(sqlQuery("select name from sqlite_master where type is 'table'"))
420+
res = [item for item in tables if 'testtbl' in item]
421+
self.assertEqual(res[0][0], 'testtbl')
422+
423+
queryreturn = sqlExecute("INSERT INTO testtbl VALUES(101);")
424+
self.assertEqual(queryreturn, 1)
425+
426+
queryreturn = sqlQuery('''SELECT * FROM testtbl''')
427+
self.assertEqual(queryreturn[0][0], 101)
428+
429+
sqlQuery("DROP TABLE testtbl")
430+
415431

416432
def run():
417433
"""Starts all tests intended for core run"""

src/tests/sql/init_version_10.sql

-1
This file was deleted.

src/tests/sql/init_version_2.sql

-1
This file was deleted.

src/tests/sql/init_version_3.sql

-1
This file was deleted.

src/tests/sql/init_version_4.sql

-1
This file was deleted.

src/tests/sql/init_version_5.sql

-1
This file was deleted.

src/tests/sql/init_version_6.sql

-1
This file was deleted.

src/tests/sql/init_version_7.sql

-3
This file was deleted.

src/tests/sql/init_version_8.sql

-1
This file was deleted.

src/tests/sql/init_version_9.sql

-2
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
INSERT INTO addressbook VALUES ('', '');
2+
3+
INSERT INTO addressbook VALUES ('', '');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
INSERT INTO inventory VALUES( '', 1, 1, '', 1, '');
2+
3+
INSERT INTO pubkeys VALUES( '', 1, '', 1, '');
4+
5+
INSERT INTO sent VALUES( '', '', '', '', '', '', '', 1, 'doingmsgpow', 1, 1, '', 1);
6+
7+
INSERT INTO sent VALUES( '', '', '', '', '', '', '', 1, 'badkey', 1, 1, '', 1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
INSERT INTO pubkeys VALUES( x'0001010101010101010101010101010101010101', 3, '', 1, '');

0 commit comments

Comments
 (0)