Skip to content

Commit d6acbc3

Browse files
committed
refactor sqlthread and add create test cases
1 parent 3b5c239 commit d6acbc3

28 files changed

+913
-548
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def run(self):
5151
if __name__ == "__main__":
5252
here = os.path.abspath(os.path.dirname(__file__))
5353
with open(os.path.join(here, 'README.md')) as f:
54-
README = f.read()
54+
README. = f.read()
5555

5656
with open(os.path.join(here, 'requirements.txt'), 'r') as f:
5757
requirements = list(f.readlines())

src/class_sqlThread.py

Lines changed: 341 additions & 519 deletions
Large diffs are not rendered by default.

src/sql/config_setting_ver_2.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE pubkeys ADD usedpersonally text DEFAULT 'no';

src/sql/config_setting_ver_3.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ALTER TABLE inbox ADD encodingtype int DEFAULT '2';
2+
3+
ALTER TABLE inbox ADD read bool DEFAULT '1';
4+
5+
ALTER TABLE sent ADD encodingtype int DEFAULT '2';

src/sql/init_version_10.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- --
2+
-- -- Update the address colunm to unique in addressbook table
3+
-- --
4+
5+
ALTER TABLE addressbook RENAME TO old_addressbook;
6+
7+
CREATE TABLE `addressbook` (
8+
`label` text ,
9+
`address` text ,
10+
UNIQUE(address) ON CONFLICT IGNORE
11+
) ;
12+
13+
INSERT INTO addressbook SELECT label, address FROM old_addressbook;
14+
15+
DROP TABLE old_addressbook;

src/sql/init_version_2.sql

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--
2+
-- Let's get rid of the first20bytesofencryptedmessage field in the inventory table.
3+
--
4+
5+
CREATE TEMP TABLE `inventory_backup` (
6+
`hash` blob ,
7+
`objecttype` text ,
8+
`streamnumber` int ,
9+
`payload` blob ,
10+
`receivedtime` int ,
11+
UNIQUE(hash) ON CONFLICT REPLACE
12+
) ;
13+
14+
INSERT INTO `inventory_backup` SELECT hash, objecttype, streamnumber, payload, receivedtime FROM inventory;
15+
16+
DROP TABLE inventory;
17+
18+
CREATE TABLE `inventory` (
19+
`hash` blob ,
20+
`objecttype` text ,
21+
`streamnumber` int ,
22+
`payload` blob ,
23+
`receivedtime` int ,
24+
UNIQUE(hash) ON CONFLICT REPLACE
25+
) ;
26+
27+
INSERT INTO inventory SELECT hash, objecttype, streamnumber, payload, receivedtime FROM inventory_backup;
28+
29+
DROP TABLE inventory_backup;

src/sql/init_version_3.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--
2+
-- Add a new column to the inventory table to store tags.
3+
--
4+
5+
ALTER TABLE inventory ADD tag blob DEFAULT '';

src/sql/init_version_4.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--
2+
-- Add a new column to the pubkeys table to store the address version.
3+
-- We're going to trash all of our pubkeys and let them be redownloaded.
4+
--
5+
6+
DROP TABLE pubkeys;
7+
8+
CREATE TABLE `pubkeys` (
9+
`hash` blob ,
10+
`addressversion` int ,
11+
`transmitdata` blob ,
12+
`time` int ,
13+
`usedpersonally` text ,
14+
UNIQUE(hash, addressversion) ON CONFLICT REPLACE
15+
) ;
16+
17+
DELETE FROM inventory WHERE objecttype = 'pubkey';

src/sql/init_version_5.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--
2+
-- Add a new table: objectprocessorqueue with which to hold objects
3+
-- that have yet to be processed if the user shuts down Bitmessage.
4+
--
5+
6+
DROP TABLE knownnodes;
7+
8+
CREATE TABLE `objectprocessorqueue` (
9+
`objecttype` text,
10+
`data` blob,
11+
UNIQUE(objecttype, data) ON CONFLICT REPLACE
12+
) ;

src/sql/init_version_6.sql

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--
2+
-- changes related to protocol v3
3+
-- In table inventory and objectprocessorqueue, objecttype is now
4+
-- an integer (it was a human-friendly string previously)
5+
--
6+
7+
DROP TABLE inventory;
8+
9+
CREATE TABLE `inventory` (
10+
`hash` blob,
11+
`objecttype` int,
12+
`streamnumber` int,
13+
`payload` blob,
14+
`expirestime` integer,
15+
`tag` blob,
16+
UNIQUE(hash) ON CONFLICT REPLACE
17+
) ;
18+
19+
DROP TABLE objectprocessorqueue;
20+
21+
CREATE TABLE `objectprocessorqueue` (
22+
`objecttype` int,
23+
`data` blob,
24+
UNIQUE(objecttype, data) ON CONFLICT REPLACE
25+
) ;

src/sql/init_version_7.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--
2+
-- The format of data stored in the pubkeys table has changed. Let's
3+
-- clear it, and the pubkeys from inventory, so that they'll
4+
-- be re-downloaded.
5+
--
6+
7+
DELETE FROM inventory WHERE objecttype = 1;
8+
9+
DELETE FROM pubkeys;
10+
11+
UPDATE sent SET status='msgqueued' WHERE status='doingmsgpow' or status='badkey';

src/sql/init_version_8.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--
2+
-- Add a new column to the inbox table to store the hash of
3+
-- the message signature. We'll use this as temporary message UUID
4+
-- in order to detect duplicates.
5+
--
6+
7+
ALTER TABLE inbox ADD sighash blob DEFAULT '';

src/sql/init_version_9.sql

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
CREATE TEMPORARY TABLE `sent_backup` (
2+
`msgid` blob,
3+
`toaddress` text,
4+
`toripe` blob,
5+
`fromaddress` text,
6+
`subject` text,
7+
`message` text,
8+
`ackdata` blob,
9+
`lastactiontime` integer,
10+
`status` text,
11+
`retrynumber` integer,
12+
`folder` text,
13+
`encodingtype` int
14+
) ;
15+
16+
INSERT INTO sent_backup SELECT msgid, toaddress, toripe, fromaddress, subject, message, ackdata, lastactiontime, status, 0, folder, encodingtype FROM sent;
17+
18+
DROP TABLE sent;
19+
20+
CREATE TABLE `sent` (
21+
`msgid` blob,
22+
`toaddress` text,
23+
`toripe` blob,
24+
`fromaddress` text,
25+
`subject` text,
26+
`message` text,
27+
`ackdata` blob,
28+
`senttime` integer,
29+
`lastactiontime` integer,
30+
`sleeptill` int,
31+
`status` text,
32+
`retrynumber` integer,
33+
`folder` text,
34+
`encodingtype` int,
35+
`ttl` int
36+
) ;
37+
38+
INSERT INTO sent SELECT msgid, toaddress, toripe, fromaddress, subject, message, ackdata, lastactiontime, lastactiontime, 0, status, 0, folder, encodingtype, 216000 FROM sent_backup;
39+
40+
DROP TABLE sent_backup;
41+
42+
ALTER TABLE pubkeys ADD address text DEFAULT '' ;
43+
44+
--
45+
-- replica for loop to update hashed address
46+
--
47+
48+
UPDATE pubkeys SET address=(enaddr(pubkeys.addressversion, 1, hash));
49+
50+
CREATE TEMPORARY TABLE `pubkeys_backup` (
51+
`address` text,
52+
`addressversion` int,
53+
`transmitdata` blob,
54+
`time` int,
55+
`usedpersonally` text,
56+
UNIQUE(address) ON CONFLICT REPLACE
57+
) ;
58+
59+
INSERT INTO pubkeys_backup SELECT address, addressversion, transmitdata, `time`, usedpersonally FROM pubkeys;
60+
61+
DROP TABLE pubkeys;
62+
63+
CREATE TABLE `pubkeys` (
64+
`address` text,
65+
`addressversion` int,
66+
`transmitdata` blob,
67+
`time` int,
68+
`usedpersonally` text,
69+
UNIQUE(address) ON CONFLICT REPLACE
70+
) ;
71+
72+
INSERT INTO pubkeys SELECT address, addressversion, transmitdata, `time`, usedpersonally FROM pubkeys_backup;
73+
74+
DROP TABLE pubkeys_backup;

src/sql/initialize_schema.sql

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
`sighash` blob,
12+
UNIQUE(msgid) ON CONFLICT REPLACE
13+
) ;
14+
15+
CREATE TABLE `sent` (
16+
`msgid` blob,
17+
`toaddress` text,
18+
`toripe` blob,
19+
`fromaddress` text,
20+
`subject` text,
21+
`message` text,
22+
`ackdata` blob,
23+
`senttime` integer,
24+
`lastactiontime` integer,
25+
`sleeptill` integer,
26+
`status` text,
27+
`retrynumber` integer,
28+
`folder` text,
29+
`encodingtype` int,
30+
`ttl` int
31+
) ;
32+
33+
34+
CREATE TABLE `subscriptions` (
35+
`label` text,
36+
`address` text,
37+
`enabled` bool
38+
) ;
39+
40+
41+
CREATE TABLE `addressbook` (
42+
`label` text,
43+
`address` text,
44+
UNIQUE(address) ON CONFLICT IGNORE
45+
) ;
46+
47+
48+
CREATE TABLE `blacklist` (
49+
`label` text,
50+
`address` text,
51+
`enabled` bool
52+
) ;
53+
54+
55+
CREATE TABLE `whitelist` (
56+
`label` text,
57+
`address` text,
58+
`enabled` bool
59+
) ;
60+
61+
62+
CREATE TABLE `pubkeys` (
63+
`address` text,
64+
`addressversion` int,
65+
`transmitdata` blob,
66+
`time` int,
67+
`usedpersonally` text,
68+
UNIQUE(address) ON CONFLICT REPLACE
69+
) ;
70+
71+
72+
CREATE TABLE `inventory` (
73+
`hash` blob,
74+
`objecttype` int,
75+
`streamnumber` int,
76+
`payload` blob,
77+
`expirestime` integer,
78+
`tag` blob,
79+
UNIQUE(hash) ON CONFLICT REPLACE
80+
) ;
81+
82+
83+
INSERT INTO subscriptions VALUES ('Bitmessage new releases/announcements', 'BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw', 1);
84+
85+
86+
CREATE TABLE `settings` (
87+
`key` blob,
88+
`value` blob,
89+
UNIQUE(key) ON CONFLICT REPLACE
90+
) ;
91+
92+
INSERT INTO settings VALUES('version','11');
93+
94+
INSERT INTO settings VALUES('lastvacuumtime', CAST(strftime('%s', 'now') AS STR) );
95+
96+
CREATE TABLE `objectprocessorqueue` (
97+
`objecttype` int,
98+
`data` blob,
99+
UNIQUE(objecttype, data) ON CONFLICT REPLACE
100+
) ;
101+
102+
103+

src/sql/upg_sc_if_old_ver_1.sql

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
CREATE TEMPORARY TABLE `pubkeys_backup` (
2+
`hash` blob,
3+
`transmitdata` blob,
4+
`time` int,
5+
`usedpersonally` text,
6+
UNIQUE(hash) ON CONFLICT REPLACE
7+
) ;
8+
9+
INSERT INTO `pubkeys_backup` SELECT hash, transmitdata, `time`, usedpersonally FROM `pubkeys`;
10+
11+
DROP TABLE `pubkeys`
12+
13+
CREATE TABLE `pubkeys` (
14+
`hash` blob,
15+
`transmitdata` blob,
16+
`time` int,
17+
`usedpersonally` text,
18+
UNIQUE(hash) ON CONFLICT REPLACE
19+
) ;
20+
21+
22+
INSERT INTO `pubkeys` SELECT hash, transmitdata, `time`, usedpersonally FROM `pubkeys_backup`;
23+
24+
DROP TABLE `pubkeys_backup`;
25+
26+
DELETE FROM inventory WHERE objecttype = 'pubkey';
27+
28+
DELETE FROM subscriptions WHERE address='BM-BbkPSZbzPwpVcYZpU4yHwf9ZPEapN5Zx'
29+
30+
INSERT INTO subscriptions VALUES('Bitmessage new releases/announcements','BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw',1)

src/sql/upg_sc_if_old_ver_2.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
UPDATE `sent` SET status='doingmsgpow' WHERE status='doingpow';
2+
3+
UPDATE `sent` SET status='msgsent' WHERE status='sentmessage';
4+
5+
UPDATE `sent` SET status='doingpubkeypow' WHERE status='findingpubkey';
6+
7+
UPDATE `sent` SET status='broadcastqueued' WHERE status='broadcastpending';

src/tests/core.py

Lines changed: 17 additions & 1 deletion
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
@@ -409,6 +409,22 @@ def test_adding_two_same_case_sensitive_addresses(self):
409409
self.delete_address_from_addressbook(address1)
410410
self.delete_address_from_addressbook(address2)
411411

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

413429
def run():
414430
"""Starts all tests defined in this module"""

src/tests/sql/__init__.py

Whitespace-only changes.

src/tests/sql/init_version_10.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
INSERT INTO `addressbook` VALUES ('test', "BM-2cWzMnxjJ7yRP3nLEWUV5LisTZyREWSxYz"), ('testone', "BM-2cWzMnxjJ7yRP3nLEWUV5LisTZyREWSxYz");

0 commit comments

Comments
 (0)