-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
170 lines (141 loc) · 6.66 KB
/
db.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
"""PaKeT database interface."""
import logging
import os
import util.db
LOGGER = logging.getLogger('pkt.db')
DB_HOST = os.environ.get('PAKET_DB_HOST', '127.0.0.1')
DB_PORT = int(os.environ.get('PAKET_DB_PORT', 3306))
DB_USER = os.environ.get('PAKET_DB_USER', 'root')
DB_PASSWORD = os.environ.get('PAKET_DB_PASSWORD')
DB_NAME = os.environ.get('PAKET_DB_NAME', 'paket')
SQL_CONNECTION = util.db.custom_sql_connection(DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME)
class UnknownUser(Exception):
"""Unknown user ID."""
class DuplicateUser(Exception):
"""Duplicate user."""
class UnknownPaket(Exception):
"""Unknown paket ID."""
def jsonable(list_of_dicts):
"""Fix for mysql-connector bug which makes sql.fetchall() return some keys as (unjsonable) bytes."""
return [{
key.decode('utf8') if isinstance(key, bytes) else key: val for key, val in dict_.items()}
for dict_ in list_of_dicts]
def init_db():
"""Initialize the database."""
with SQL_CONNECTION() as sql:
sql.execute('''
CREATE TABLE packages(
escrow_pubkey VARCHAR(56) UNIQUE,
launcher_pubkey VARCHAR(56),
recipient_pubkey VARCHAR(56),
deadline INTEGER,
payment INTEGER,
collateral INTEGER,
set_options_transaction VARCHAR(1024),
refund_transaction VARCHAR(1024),
merge_transaction VARCHAR(1024),
payment_transaction VARCHAR(1024))''')
LOGGER.debug('packages table created')
sql.execute('''
CREATE TABLE events(
timestamp TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
escrow_pubkey VARCHAR(56) NULL,
user_pubkey VARCHAR(56),
event_type VARCHAR(20),
location VARCHAR(24),
FOREIGN KEY(escrow_pubkey) REFERENCES packages(escrow_pubkey))''')
LOGGER.debug('events table created')
def add_event(escrow_pubkey, user_pubkey, event_type, location):
"""Add a package event."""
with SQL_CONNECTION() as sql:
sql.execute("""
INSERT INTO events (escrow_pubkey, user_pubkey, event_type, location)
VALUES (%s, %s, %s, %s)
""", (escrow_pubkey, user_pubkey, event_type, location))
def get_events(max_events_num):
"""Get all user and package events up to a limit."""
with SQL_CONNECTION() as sql:
sql.execute("SELECT * FROM events LIMIT %s", (int(max_events_num),))
events = jsonable(sql.fetchall())
return {
'packages_events': [event for event in events if event['escrow_pubkey'] is not None],
'user_events': [event for event in events if event['escrow_pubkey'] is None]}
def get_package_events(escrow_pubkey):
"""Get a list of events relating to a package."""
with SQL_CONNECTION() as sql:
sql.execute("""
SELECT timestamp, user_pubkey, event_type, location FROM events
WHERE escrow_pubkey = %s
ORDER BY timestamp ASC""", (escrow_pubkey,))
return jsonable(sql.fetchall())
def enrich_package(package, user_role=None, user_pubkey=None):
"""Add some periferal data to the package object."""
package['blockchain_url'] = "https://testnet.stellarchain.io/address/{}".format(package['escrow_pubkey'])
package['paket_url'] = "https://paket.global/paket/{}".format(package['escrow_pubkey'])
package['events'] = get_package_events(package['escrow_pubkey'])
if package['events']:
package['launch_date'] = package['events'][0]['timestamp']
event_types = set([event['event_type'] for event in package['events']])
if 'received' in event_types:
package['status'] = 'delivered'
elif 'couriered' in event_types:
package['status'] = 'in transit'
elif 'launched' in event_types:
package['status'] = 'waiting pickup'
else:
package['status'] = 'unknown'
if user_role:
package['user_role'] = user_role
elif user_pubkey:
if user_pubkey == package['launcher_pubkey']:
package['user_role'] = 'launcher'
elif user_pubkey == package['recipient_pubkey']:
package['user_role'] = 'recipient'
else:
package['user_role'] = 'unknown'
return package
def create_package(
escrow_pubkey, launcher_pubkey, recipient_pubkey, payment, collateral, deadline,
set_options_transaction, refund_transaction, merge_transaction, payment_transaction, location=None):
"""Create a new package row."""
with SQL_CONNECTION() as sql:
sql.execute("""
INSERT INTO packages (
escrow_pubkey, launcher_pubkey, recipient_pubkey, deadline, payment, collateral,
set_options_transaction, refund_transaction, merge_transaction, payment_transaction
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""", (
escrow_pubkey, launcher_pubkey, recipient_pubkey, deadline, payment, collateral,
set_options_transaction, refund_transaction, merge_transaction, payment_transaction))
add_event(escrow_pubkey, launcher_pubkey, 'launched', location)
return enrich_package(get_package(escrow_pubkey))
def get_package(escrow_pubkey):
"""Get package details."""
with SQL_CONNECTION() as sql:
sql.execute("SELECT * FROM packages WHERE escrow_pubkey = %s", (escrow_pubkey,))
try:
return enrich_package(sql.fetchone())
except TypeError:
raise UnknownPaket("paket {} is not valid".format(escrow_pubkey))
def get_packages(user_pubkey=None):
"""Get a list of packages."""
with SQL_CONNECTION() as sql:
if user_pubkey:
sql.execute("""
SELECT * FROM packages
WHERE launcher_pubkey = %s""", (user_pubkey,))
launched = [enrich_package(row, user_role='launcher') for row in sql.fetchall()]
sql.execute("""
SELECT * FROM packages
WHERE recipient_pubkey = %s""", (user_pubkey,))
received = [enrich_package(row, user_role='recipient') for row in sql.fetchall()]
sql.execute("""
SELECT * FROM packages
WHERE escrow_pubkey IN (
SELECT escrow_pubkey FROM events
WHERE event_type = 'couriered' AND user_pubkey = %s)""", (user_pubkey,))
couriered = [enrich_package(row, user_role='courier') for row in sql.fetchall()]
return [
dict(package, custodian_pubkey=package['events'][-1]['user_pubkey'])
for package in launched + received + couriered]
sql.execute('SELECT * FROM packages')
return [enrich_package(row) for row in sql.fetchall()]