aboutsummaryrefslogtreecommitdiff
path: root/src/mint/taler-mint-dbinit.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mint/taler-mint-dbinit.c')
-rw-r--r--src/mint/taler-mint-dbinit.c285
1 files changed, 285 insertions, 0 deletions
diff --git a/src/mint/taler-mint-dbinit.c b/src/mint/taler-mint-dbinit.c
new file mode 100644
index 000000000..d877f62c6
--- /dev/null
+++ b/src/mint/taler-mint-dbinit.c
@@ -0,0 +1,285 @@
1/*
2 This file is part of TALER
3 (C) 2014 Christian Grothoff (and other contributing authors)
4
5 TALER is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
8
9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along with
14 TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
15*/
16
17/**
18 * @file taler-mint-dbinit.c
19 * @brief Create tables for the mint database.
20 * @author Florian Dold
21 */
22
23#include "platform.h"
24#include <gnunet/gnunet_util_lib.h>
25#include <libpq-fe.h>
26#include "mint.h"
27
28
29#define break_db_err(result) do { \
30 GNUNET_break(0); \
31 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Database failure: %s\n", PQresultErrorMessage (result)); \
32 PQclear (result); \
33 } while (0)
34
35
36static char *mint_base_dir;
37static struct GNUNET_CONFIGURATION_Handle *cfg;
38static PGconn *db_conn;
39static char *TALER_MINT_db_connection_cfg_str;
40
41
42int
43TALER_MINT_init_withdraw_tables (PGconn *conn)
44{
45 PGresult *result;
46 result = PQexec (conn,
47 "CREATE TABLE IF NOT EXISTS reserves"
48 "("
49 " reserve_pub BYTEA PRIMARY KEY"
50 ",balance_value INT4 NOT NULL"
51 ",balance_fraction INT4 NOT NULL"
52 ",balance_currency VARCHAR(4) NOT NULL"
53 ",status_sig BYTEA"
54 ",status_sign_pub BYTEA"
55 ",expiration_date INT8 NOT NULL"
56 ")");
57 if (PGRES_COMMAND_OK != PQresultStatus(result))
58 {
59 break_db_err (result);
60 return GNUNET_SYSERR;
61 }
62 PQclear (result);
63
64 result = PQexec (conn,
65 "CREATE TABLE IF NOT EXISTS collectable_blindcoins"
66 "("
67 "blind_ev BYTEA PRIMARY KEY"
68 ",blind_ev_sig BYTEA NOT NULL"
69 ",denom_pub BYTEA NOT NULL"
70 ",reserve_sig BYTEA NOT NULL"
71 ",reserve_pub BYTEA NOT NULL REFERENCES reserves (reserve_pub)"
72 ")");
73 if (PGRES_COMMAND_OK != PQresultStatus(result))
74 {
75 break_db_err (result);
76 return GNUNET_SYSERR;
77 }
78 PQclear (result);
79
80 result = PQexec (conn,
81 "CREATE TABLE IF NOT EXISTS known_coins "
82 "("
83 " coin_pub BYTEA NOT NULL PRIMARY KEY"
84 ",denom_pub BYTEA NOT NULL"
85 ",denom_sig BYTEA NOT NULL"
86 ",expended_value INT4 NOT NULL"
87 ",expended_fraction INT4 NOT NULL"
88 ",expended_currency VARCHAR(4) NOT NULL"
89 ",refresh_session_pub BYTEA"
90 ")");
91 if (PGRES_COMMAND_OK != PQresultStatus(result))
92 {
93 break_db_err (result);
94 return GNUNET_SYSERR;
95 }
96 PQclear (result);
97
98 result = PQexec (conn,
99 "CREATE TABLE IF NOT EXISTS refresh_sessions "
100 "("
101 " session_pub BYTEA PRIMARY KEY CHECK (length(session_pub) = 32)"
102 ",session_melt_sig BYTEA"
103 ",session_commit_sig BYTEA"
104 ",noreveal_index INT2 NOT NULL"
105 // non-zero if all reveals were ok
106 // and the new coin signatures are ready
107 ",reveal_ok BOOLEAN NOT NULL DEFAULT false"
108 ") ");
109 if (PGRES_COMMAND_OK != PQresultStatus(result))
110 {
111 break_db_err (result);
112 return GNUNET_SYSERR;
113 }
114 PQclear (result);
115
116 result = PQexec (conn,
117 "CREATE TABLE IF NOT EXISTS refresh_order "
118 "( "
119 " session_pub BYTEA NOT NULL REFERENCES refresh_sessions (session_pub)"
120 ",newcoin_index INT2 NOT NULL "
121 ",denom_pub BYTEA NOT NULL "
122 ",PRIMARY KEY (session_pub, newcoin_index)"
123 ") ");
124
125 if (PGRES_COMMAND_OK != PQresultStatus(result))
126 {
127 break_db_err (result);
128 return GNUNET_SYSERR;
129 }
130 PQclear (result);
131
132
133 result = PQexec (conn,
134 "CREATE TABLE IF NOT EXISTS refresh_commit_link"
135 "("
136 " session_pub BYTEA NOT NULL REFERENCES refresh_sessions (session_pub)"
137 ",transfer_pub BYTEA NOT NULL"
138 ",link_secret_enc BYTEA NOT NULL"
139 // index of the old coin in the customer's request
140 ",oldcoin_index INT2 NOT NULL"
141 // index for cut and choose,
142 // ranges from 0 to kappa-1
143 ",cnc_index INT2 NOT NULL"
144 ")");
145
146 if (PGRES_COMMAND_OK != PQresultStatus(result))
147 {
148 break_db_err (result);
149 return GNUNET_SYSERR;
150 }
151 PQclear (result);
152
153 result = PQexec (conn,
154 "CREATE TABLE IF NOT EXISTS refresh_commit_coin"
155 "("
156 " session_pub BYTEA NOT NULL REFERENCES refresh_sessions (session_pub) "
157 ",link_vector_enc BYTEA NOT NULL"
158 // index of the new coin in the customer's request
159 ",newcoin_index INT2 NOT NULL"
160 // index for cut and choose,
161 ",cnc_index INT2 NOT NULL"
162 ",coin_ev BYTEA NOT NULL"
163 ")");
164
165 if (PGRES_COMMAND_OK != PQresultStatus(result))
166 {
167 break_db_err (result);
168 return GNUNET_SYSERR;
169 }
170 PQclear (result);
171
172 result = PQexec (conn,
173 "CREATE TABLE IF NOT EXISTS refresh_melt"
174 "("
175 " session_pub BYTEA NOT NULL REFERENCES refresh_sessions (session_pub) "
176 ",coin_pub BYTEA NOT NULL REFERENCES known_coins (coin_pub) "
177 ",denom_pub BYTEA NOT NULL "
178 ",oldcoin_index INT2 NOT NULL"
179 ")");
180
181 if (PGRES_COMMAND_OK != PQresultStatus(result))
182 {
183 break_db_err (result);
184 return GNUNET_SYSERR;
185 }
186 PQclear (result);
187
188 result = PQexec (conn,
189 "CREATE TABLE IF NOT EXISTS refresh_collectable"
190 "("
191 " session_pub BYTEA NOT NULL REFERENCES refresh_sessions (session_pub) "
192 ",ev_sig BYTEA NOT NULL"
193 ",newcoin_index INT2 NOT NULL"
194 ")");
195
196 if (PGRES_COMMAND_OK != PQresultStatus(result))
197 {
198 break_db_err (result);
199 return GNUNET_SYSERR;
200 }
201 PQclear (result);
202
203 result = PQexec (conn,
204 "CREATE TABLE IF NOT EXISTS deposits "
205 "( "
206 " coin_pub BYTEA NOT NULL PRIMARY KEY CHECK (length(coin_pub)=32)"
207 ",denom_pub BYTEA NOT NULL CHECK (length(denom_pub)=32)"
208 ",transaction_id INT8 NOT NULL"
209 ",amount_currency VARCHAR(4) NOT NULL"
210 ",amount_value INT4 NOT NULL"
211 ",amount_fraction INT4 NOT NULL"
212 ",merchant_pub BYTEA NOT NULL"
213 ",h_contract BYTEA NOT NULL CHECK (length(h_contract)=64)"
214 ",h_wire BYTEA NOT NULL CHECK (length(h_wire)=64)"
215 ",coin_sig BYTEA NOT NULL CHECK (length(coin_sig)=64)"
216 ",wire TEXT NOT NULL"
217 ")");
218
219 if (PGRES_COMMAND_OK != PQresultStatus(result))
220 {
221 break_db_err (result);
222 return GNUNET_SYSERR;
223 }
224 PQclear (result);
225
226 return GNUNET_OK;
227}
228
229
230/**
231 * The main function of the serve tool
232 *
233 * @param argc number of arguments from the command line
234 * @param argv command line arguments
235 * @return 0 ok, 1 on error
236 */
237int
238main (int argc, char *const *argv)
239{
240 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
241 GNUNET_GETOPT_OPTION_HELP ("gnunet-mint-keyup OPTIONS"),
242 {'d', "mint-dir", "DIR",
243 "mint directory", 1,
244 &GNUNET_GETOPT_set_filename, &mint_base_dir},
245 GNUNET_GETOPT_OPTION_END
246 };
247
248 if (GNUNET_GETOPT_run ("taler-mint-serve", options, argc, argv) < 0)
249 return 1;
250
251 GNUNET_assert (GNUNET_OK == GNUNET_log_setup ("taler-mint-dbinit", "INFO", NULL));
252
253 if (NULL == mint_base_dir)
254 {
255 fprintf (stderr, "Mint base directory not given.\n");
256 return 1;
257 }
258
259 cfg = TALER_MINT_config_load (mint_base_dir);
260 if (NULL == cfg)
261 {
262 fprintf (stderr, "Can't load mint configuration.\n");
263 return 1;
264 }
265 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "mint", "db", &TALER_MINT_db_connection_cfg_str))
266 {
267 fprintf (stderr, "Configuration 'mint.db' not found.\n");
268 return 42;
269 }
270 db_conn = PQconnectdb (TALER_MINT_db_connection_cfg_str);
271 if (CONNECTION_OK != PQstatus (db_conn))
272 {
273 fprintf (stderr, "Database connection failed: %s\n", PQerrorMessage (db_conn));
274 return 1;
275 }
276
277 if (GNUNET_OK != TALER_MINT_init_withdraw_tables (db_conn))
278 {
279 fprintf (stderr, "Failed to initialize database.\n");
280 return 1;
281 }
282
283 return 0;
284}
285