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
|
/*
This file is part of TALER
Copyright (C) 2022 Taler Systems SA
TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
* @file exchangedb/pg_get_link_data.c
* @brief Implementation of the get_link_data function for Postgres
* @author Christian Grothoff
*/
#include "platform.h"
#include "taler_error_codes.h"
#include "taler_dbevents.h"
#include "taler_pq_lib.h"
#include "pg_get_link_data.h"
#include "pg_helper.h"
/**
* Closure for #add_ldl().
*/
struct LinkDataContext
{
/**
* Function to call on each result.
*/
TALER_EXCHANGEDB_LinkCallback ldc;
/**
* Closure for @e ldc.
*/
void *ldc_cls;
/**
* Last transfer public key for which we have information in @e last.
* Only valid if @e last is non-NULL.
*/
struct TALER_TransferPublicKeyP transfer_pub;
/**
* Link data for @e transfer_pub
*/
struct TALER_EXCHANGEDB_LinkList *last;
/**
* Status, set to #GNUNET_SYSERR on errors,
*/
enum GNUNET_GenericReturnValue status;
};
struct Results {
struct TALER_EXCHANGEDB_LinkList *pos;
struct TALER_TransferPublicKeyP transfer_pub;
};
/**
* Free memory of the link data list.
*
* @param cls the @e cls of this struct with the plugin-specific state (unused)
* @param ldl link data list to release
*/
static void
free_link_data_list (void *cls,
struct TALER_EXCHANGEDB_LinkList *ldl)
{
struct TALER_EXCHANGEDB_LinkList *next;
(void) cls;
while (NULL != ldl)
{
next = ldl->next;
TALER_denom_pub_free (&ldl->denom_pub);
TALER_blinded_denom_sig_free (&ldl->ev_sig);
GNUNET_free (ldl);
ldl = next;
}
}
static int
transfer_pub_cmp (const void *a,
const void *b)
{
const struct Results *ra = a;
const struct Results *rb = b;
return GNUNET_memcmp (&ra->transfer_pub,
&rb->transfer_pub);
}
/**
* Function to be called with the results of a SELECT statement
* that has returned @a num_results results.
*
* @param cls closure of type `struct LinkDataContext *`
* @param result the postgres result
* @param num_results the number of results in @a result
*/
static void
add_ldl (void *cls,
PGresult *result,
unsigned int num_results)
{
struct LinkDataContext *ldctx = cls;
struct Results *temp= GNUNET_new_array (num_results,
struct Results);;
unsigned int temp_off = 0;
for (int i = num_results - 1; i >= 0; i--)
{
struct TALER_EXCHANGEDB_LinkList *pos;
struct TALER_TransferPublicKeyP transfer_pub;
pos = GNUNET_new (struct TALER_EXCHANGEDB_LinkList);
{
struct TALER_BlindedPlanchet bp;
struct GNUNET_PQ_ResultSpec rs[] = {
GNUNET_PQ_result_spec_auto_from_type ("transfer_pub",
&transfer_pub),
GNUNET_PQ_result_spec_auto_from_type ("link_sig",
&pos->orig_coin_link_sig),
TALER_PQ_result_spec_blinded_denom_sig ("ev_sig",
&pos->ev_sig),
GNUNET_PQ_result_spec_uint32 ("freshcoin_index",
&pos->coin_refresh_offset),
TALER_PQ_result_spec_exchange_withdraw_values ("ewv",
&pos->alg_values),
TALER_PQ_result_spec_denom_pub ("denom_pub",
&pos->denom_pub),
TALER_PQ_result_spec_blinded_planchet ("coin_ev",
&bp),
GNUNET_PQ_result_spec_end
};
if (GNUNET_OK !=
GNUNET_PQ_extract_result (result,
rs,
i))
{
GNUNET_break (0);
GNUNET_free (pos);
ldctx->status = GNUNET_SYSERR;
return;
}
if (TALER_DENOMINATION_CS == bp.cipher)
{
pos->nonce = bp.details.cs_blinded_planchet.nonce;
pos->have_nonce = true;
}
TALER_blinded_planchet_free (&bp);
}
temp[temp_off].pos = pos;
temp[temp_off].transfer_pub = transfer_pub;
temp_off++;
}
qsort (temp,
temp_off,
sizeof (struct Results),
transfer_pub_cmp);
for (unsigned int i = 0; i < temp_off; i++)
{
struct TALER_EXCHANGEDB_LinkList *pos;
struct Results *r = &temp[i];
pos = GNUNET_new (struct TALER_EXCHANGEDB_LinkList);
pos->orig_coin_link_sig = r->pos->orig_coin_link_sig;
pos->ev_sig = r->pos->ev_sig;
pos->coin_refresh_offset = r->pos->coin_refresh_offset;
pos->alg_values = r->pos->alg_values;
pos->denom_pub = r->pos->denom_pub;
pos->nonce = r->pos->nonce;
pos->have_nonce = r->pos->have_nonce;
pos->next = ldctx->last;
ldctx->last = pos;
}
if (NULL != ldctx->last)
{
ldctx->ldc (ldctx->ldc_cls,
&ldctx->transfer_pub,
ldctx->last);
free_link_data_list (ldctx,
ldctx->last);
}
ldctx->last = NULL;
GNUNET_free(temp);
/*
if ( (NULL != ldctx->last) &&
(0 == GNUNET_memcmp (&transfer_pub,
&ldctx->transfer_pub)) )
{
pos->next = ldctx->last;
}
else
{
if (NULL != ldctx->last)
{
ldctx->ldc (ldctx->ldc_cls,
&ldctx->transfer_pub,
ldctx->last);
free_link_data_list (cls,
ldctx->last);
}
ldctx->transfer_pub = transfer_pub;
}
ldctx->last = pos;
}
GNUNET_free(temp);*/
}
enum GNUNET_DB_QueryStatus
TEH_PG_get_link_data (void *cls,
const struct TALER_CoinSpendPublicKeyP *coin_pub,
TALER_EXCHANGEDB_LinkCallback ldc,
void *ldc_cls)
{
struct PostgresClosure *pg = cls;
struct GNUNET_PQ_QueryParam params[] = {
GNUNET_PQ_query_param_auto_from_type (coin_pub),
GNUNET_PQ_query_param_end
};
enum GNUNET_DB_QueryStatus qs;
struct LinkDataContext ldctx;
static int percent_refund = -2;
const char *query;
if (-2 == percent_refund)
{
const char *mode = getenv ("NEW_LOGIC");
char dummy;
if ( (NULL==mode) ||
(1 != sscanf (mode,
"%d%c",
&percent_refund,
&dummy)) )
{
if (NULL != mode)
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Bad mode `%s' specified\n",
mode);
percent_refund = 0;
}
}
switch (percent_refund)
{
case 0:
query = "get_link";
PREPARE (pg,
query,
"SELECT "
" tp.transfer_pub"
",denoms.denom_pub"
",rrc.ev_sig"
",rrc.ewv"
",rrc.link_sig"
",rrc.freshcoin_index"
",rrc.coin_ev"
" FROM refresh_commitments"
" JOIN refresh_revealed_coins rrc"
" USING (melt_serial_id)"
" JOIN refresh_transfer_keys tp"
" USING (melt_serial_id)"
" JOIN denominations denoms"
" ON (rrc.denominations_serial = denoms.denominations_serial)"
" WHERE old_coin_pub=$1"
" ORDER BY tp.transfer_pub, rrc.freshcoin_index ASC");
break;
case 1:
query = "get_link_v1";
PREPARE (pg,
query,
"WITH rc AS MATERIALIZED ("
"SELECT"
" melt_serial_id"
" FROM refresh_commitments"
" WHERE old_coin_pub=$1"
")"
"SELECT "
" tp.transfer_pub"
",denoms.denom_pub"
",rrc.ev_sig"
",rrc.ewv"
",rrc.link_sig"
",rrc.freshcoin_index"
",rrc.coin_ev "
"FROM "
"refresh_revealed_coins rrc"
" JOIN refresh_transfer_keys tp"
" USING (melt_serial_id)"
" JOIN denominations denoms"
" USING (denominations_serial)"
" WHERE rrc.melt_serial_id = (SELECT melt_serial_id FROM rc)"
" ORDER BY tp.transfer_pub, rrc.freshcoin_index ASC");
break;
case 2:
query = "get_link_v2";
PREPARE (pg,
query,
"SELECT"
" *"
" FROM"
" exchange_do_get_link_data"
" ($1) "
" AS "
" (transfer_pub BYTEA"
" ,denom_pub BYTEA"
" ,ev_sig BYTEA"
" ,ewv BYTEA"
" ,link_sig BYTEA"
" ,freshcoin_index INT4"
" ,coin_ev BYTEA);");
break;
case 3:
query="get_link_v3";
PREPARE (pg,
query,
"SELECT "
" tp.transfer_pub"
",denoms.denom_pub"
",rrc.ev_sig"
",rrc.ewv"
",rrc.link_sig"
",rrc.freshcoin_index"
",rrc.coin_ev"
" FROM refresh_commitments"
" JOIN refresh_revealed_coins rrc"
" USING (melt_serial_id)"
" JOIN refresh_transfer_keys tp"
" USING (melt_serial_id)"
" JOIN denominations denoms"
" ON (rrc.denominations_serial = denoms.denominations_serial)"
" WHERE old_coin_pub=$1");
break;
case 4:
query="get_link_v4";
PREPARE (pg,
query,
"WITH rc AS MATERIALIZED ("
"SELECT"
" melt_serial_id"
" FROM refresh_commitments"
" WHERE old_coin_pub=$1"
")"
"SELECT "
" tp.transfer_pub"
",denoms.denom_pub"
",rrc.ev_sig"
",rrc.ewv"
",rrc.link_sig"
",rrc.freshcoin_index"
",rrc.coin_ev "
"FROM "
"refresh_revealed_coins rrc"
" JOIN refresh_transfer_keys tp"
" USING (melt_serial_id)"
" JOIN denominations denoms"
" USING (denominations_serial)"
" WHERE rrc.melt_serial_id = (SELECT melt_serial_id FROM rc)"
);
break;
default:
GNUNET_break (0);
return GNUNET_DB_STATUS_HARD_ERROR;
}
ldctx.ldc = ldc;
ldctx.ldc_cls = ldc_cls;
ldctx.last = NULL;
ldctx.status = GNUNET_OK;
qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
query,
params,
&add_ldl,
&ldctx);
if (NULL != ldctx.last)
{
if (GNUNET_OK == ldctx.status)
{
/* call callback one more time! */
ldc (ldc_cls,
&ldctx.transfer_pub,
ldctx.last);
}
free_link_data_list (cls,
ldctx.last);
ldctx.last = NULL;
}
if (GNUNET_OK != ldctx.status)
return GNUNET_DB_STATUS_HARD_ERROR;
return qs;
}
|