Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022-2023 Taler Systems SA
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, see <http://www.gnu.org/licenses/>
15 : */
16 : /**
17 : * @file get_coin_transactions.c
18 : * @brief Low-level (statement-level) Postgres database access for the exchange
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_error_codes.h"
22 : #include "exchangedb_lib.h"
23 : #include "taler/taler_pq_lib.h"
24 : #include "exchange-database/get_coin_transactions.h"
25 : #include "helper.h"
26 : #include "exchange-database/start_read_committed.h"
27 : #include "exchange-database/commit.h"
28 : #include "exchange-database/rollback.h"
29 :
30 :
31 : /**
32 : * How often do we re-try when encountering DB serialization issues?
33 : * (We are read-only, so can only happen due to concurrent insert,
34 : * which should be very rare.)
35 : */
36 : #define RETRIES 3
37 :
38 : /**
39 : * Closure for callbacks called from #TALER_EXCHANGEDB_get_coin_transactions()
40 : */
41 : struct CoinHistoryContext
42 : {
43 : /**
44 : * Head of the coin's history list.
45 : */
46 : struct TALER_EXCHANGEDB_TransactionList *head;
47 :
48 : /**
49 : * Public key of the coin we are building the history for.
50 : */
51 : const struct TALER_CoinSpendPublicKeyP *coin_pub;
52 :
53 : /**
54 : * Plugin context.
55 : */
56 : struct TALER_EXCHANGEDB_PostgresContext *pg;
57 :
58 : /**
59 : * Our current offset in the coin history.
60 : */
61 : uint64_t chid;
62 :
63 : /**
64 : * Set to 'true' if the transaction failed.
65 : */
66 : bool failed;
67 :
68 : };
69 :
70 :
71 : /**
72 : * Function to be called with the results of a SELECT statement
73 : * that has returned @a num_results results.
74 : *
75 : * @param cls closure of type `struct CoinHistoryContext`
76 : * @param result the postgres result
77 : * @param num_results the number of results in @a result
78 : */
79 : static void
80 77 : add_coin_deposit (void *cls,
81 : PGresult *result,
82 : unsigned int num_results)
83 : {
84 77 : struct CoinHistoryContext *chc = cls;
85 77 : struct TALER_EXCHANGEDB_PostgresContext *pg = chc->pg;
86 :
87 154 : for (unsigned int i = 0; i < num_results; i++)
88 : {
89 : struct TALER_EXCHANGEDB_DepositListEntry *deposit;
90 : struct TALER_EXCHANGEDB_TransactionList *tl;
91 : uint64_t serial_id;
92 :
93 77 : deposit = GNUNET_new (struct TALER_EXCHANGEDB_DepositListEntry);
94 : {
95 77 : struct GNUNET_PQ_ResultSpec rs[] = {
96 77 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
97 : &deposit->amount_with_fee),
98 77 : TALER_PQ_RESULT_SPEC_AMOUNT ("fee_deposit",
99 : &deposit->deposit_fee),
100 77 : GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash",
101 : &deposit->h_denom_pub),
102 77 : GNUNET_PQ_result_spec_allow_null (
103 77 : GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash",
104 : &deposit->h_age_commitment),
105 : &deposit->no_age_commitment),
106 77 : GNUNET_PQ_result_spec_allow_null (
107 77 : GNUNET_PQ_result_spec_auto_from_type ("wallet_data_hash",
108 : &deposit->wallet_data_hash),
109 : &deposit->no_wallet_data_hash),
110 77 : GNUNET_PQ_result_spec_timestamp ("wallet_timestamp",
111 : &deposit->timestamp),
112 77 : GNUNET_PQ_result_spec_timestamp ("refund_deadline",
113 : &deposit->refund_deadline),
114 77 : GNUNET_PQ_result_spec_timestamp ("wire_deadline",
115 : &deposit->wire_deadline),
116 77 : GNUNET_PQ_result_spec_auto_from_type ("merchant_pub",
117 : &deposit->merchant_pub),
118 77 : GNUNET_PQ_result_spec_auto_from_type ("h_contract_terms",
119 : &deposit->h_contract_terms),
120 77 : GNUNET_PQ_result_spec_auto_from_type ("wire_salt",
121 : &deposit->wire_salt),
122 77 : GNUNET_PQ_result_spec_string ("payto_uri",
123 : &deposit->receiver_wire_account.full_payto
124 : ),
125 77 : GNUNET_PQ_result_spec_auto_from_type ("coin_sig",
126 : &deposit->csig),
127 77 : GNUNET_PQ_result_spec_uint64 ("coin_deposit_serial_id",
128 : &serial_id),
129 77 : GNUNET_PQ_result_spec_auto_from_type ("done",
130 : &deposit->done),
131 : GNUNET_PQ_result_spec_end
132 : };
133 :
134 77 : if (GNUNET_OK !=
135 77 : GNUNET_PQ_extract_result (result,
136 : rs,
137 : i))
138 : {
139 0 : GNUNET_break (0);
140 0 : GNUNET_free (deposit);
141 0 : chc->failed = true;
142 0 : return;
143 : }
144 : }
145 77 : tl = GNUNET_new (struct TALER_EXCHANGEDB_TransactionList);
146 77 : tl->next = chc->head;
147 77 : tl->type = TALER_EXCHANGEDB_TT_DEPOSIT;
148 77 : tl->details.deposit = deposit;
149 77 : tl->serial_id = serial_id;
150 77 : tl->coin_history_id = chc->chid;
151 77 : chc->head = tl;
152 : }
153 : }
154 :
155 :
156 : /**
157 : * Function to be called with the results of a SELECT statement
158 : * that has returned @a num_results results.
159 : *
160 : * @param cls closure of type `struct CoinHistoryContext`
161 : * @param result the postgres result
162 : * @param num_results the number of results in @a result
163 : */
164 : static void
165 9 : add_coin_purse_deposit (void *cls,
166 : PGresult *result,
167 : unsigned int num_results)
168 : {
169 9 : struct CoinHistoryContext *chc = cls;
170 9 : struct TALER_EXCHANGEDB_PostgresContext *pg = chc->pg;
171 :
172 18 : for (unsigned int i = 0; i < num_results; i++)
173 : {
174 : struct TALER_EXCHANGEDB_PurseDepositListEntry *deposit;
175 : struct TALER_EXCHANGEDB_TransactionList *tl;
176 : uint64_t serial_id;
177 :
178 9 : deposit = GNUNET_new (struct TALER_EXCHANGEDB_PurseDepositListEntry);
179 : {
180 : bool not_finished;
181 9 : struct GNUNET_PQ_ResultSpec rs[] = {
182 9 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
183 : &deposit->amount),
184 9 : TALER_PQ_RESULT_SPEC_AMOUNT ("fee_deposit",
185 : &deposit->deposit_fee),
186 9 : GNUNET_PQ_result_spec_auto_from_type ("purse_pub",
187 : &deposit->purse_pub),
188 9 : GNUNET_PQ_result_spec_uint64 ("purse_deposit_serial_id",
189 : &serial_id),
190 9 : GNUNET_PQ_result_spec_allow_null (
191 : GNUNET_PQ_result_spec_string ("partner_base_url",
192 : &deposit->exchange_base_url),
193 : NULL),
194 9 : GNUNET_PQ_result_spec_auto_from_type ("coin_sig",
195 : &deposit->coin_sig),
196 9 : GNUNET_PQ_result_spec_allow_null (
197 9 : GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash",
198 : &deposit->h_age_commitment),
199 : &deposit->no_age_commitment),
200 9 : GNUNET_PQ_result_spec_allow_null (
201 : GNUNET_PQ_result_spec_bool ("refunded",
202 : &deposit->refunded),
203 : ¬_finished),
204 9 : GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash",
205 : &deposit->h_denom_pub),
206 : GNUNET_PQ_result_spec_end
207 : };
208 :
209 9 : if (GNUNET_OK !=
210 9 : GNUNET_PQ_extract_result (result,
211 : rs,
212 : i))
213 : {
214 0 : GNUNET_break (0);
215 0 : GNUNET_free (deposit);
216 0 : chc->failed = true;
217 0 : return;
218 : }
219 9 : if (not_finished)
220 7 : deposit->refunded = false;
221 : /* double-check for all-zeros age commitment */
222 9 : if (! deposit->no_age_commitment)
223 : deposit->no_age_commitment
224 0 : = GNUNET_is_zero (&deposit->h_age_commitment);
225 : }
226 9 : tl = GNUNET_new (struct TALER_EXCHANGEDB_TransactionList);
227 9 : tl->next = chc->head;
228 9 : tl->type = TALER_EXCHANGEDB_TT_PURSE_DEPOSIT;
229 9 : tl->details.purse_deposit = deposit;
230 9 : tl->serial_id = serial_id;
231 9 : tl->coin_history_id = chc->chid;
232 9 : chc->head = tl;
233 : }
234 : }
235 :
236 :
237 : /**
238 : * Function to be called with the results of a SELECT statement
239 : * that has returned @a num_results results.
240 : *
241 : * @param cls closure of type `struct CoinHistoryContext`
242 : * @param result the postgres result
243 : * @param num_results the number of results in @a result
244 : */
245 : static void
246 14 : add_coin_melt (void *cls,
247 : PGresult *result,
248 : unsigned int num_results)
249 : {
250 14 : struct CoinHistoryContext *chc = cls;
251 14 : struct TALER_EXCHANGEDB_PostgresContext *pg = chc->pg;
252 :
253 28 : for (unsigned int i = 0; i<num_results; i++)
254 : {
255 : struct TALER_EXCHANGEDB_MeltListEntry *melt;
256 : struct TALER_EXCHANGEDB_TransactionList *tl;
257 : uint64_t serial_id;
258 :
259 14 : melt = GNUNET_new (struct TALER_EXCHANGEDB_MeltListEntry);
260 : {
261 14 : struct TALER_DenominationHashP *denom_pub_hashes = NULL;
262 14 : struct TALER_TransferPublicKeyP *transfer_pubs = NULL;
263 14 : size_t num_transfer_pubs = 0;
264 : bool no_transfer_pubs;
265 14 : struct GNUNET_PQ_ResultSpec rs[] = {
266 14 : GNUNET_PQ_result_spec_auto_from_type ("rc",
267 : &melt->rc),
268 : /* oldcoin_index not needed */
269 14 : GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash",
270 : &melt->h_denom_pub),
271 14 : GNUNET_PQ_result_spec_auto_from_type ("old_coin_sig",
272 : &melt->coin_sig),
273 14 : GNUNET_PQ_result_spec_auto_from_type ("refresh_seed",
274 : &melt->refresh_seed),
275 14 : TALER_PQ_result_spec_array_denom_hash (pg->conn,
276 : "denom_pub_hashes",
277 : &melt->num_coins,
278 : &denom_pub_hashes),
279 14 : GNUNET_PQ_result_spec_allow_null (
280 14 : GNUNET_PQ_result_spec_auto_from_type ("blinding_seed",
281 : &melt->blinding_seed),
282 : &melt->no_blinding_seed),
283 14 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
284 : &melt->amount_with_fee),
285 14 : TALER_PQ_RESULT_SPEC_AMOUNT ("fee_refresh",
286 : &melt->melt_fee),
287 14 : GNUNET_PQ_result_spec_allow_null (
288 14 : GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash",
289 : &melt->h_age_commitment),
290 : &melt->no_age_commitment),
291 14 : GNUNET_PQ_result_spec_uint64 ("refresh_id",
292 : &serial_id),
293 14 : GNUNET_PQ_result_spec_uint32 ("noreveal_index",
294 : &melt->noreveal_index),
295 14 : GNUNET_PQ_result_spec_allow_null (
296 : GNUNET_PQ_result_spec_array_fixed_size (pg->conn,
297 : "transfer_pubs",
298 : sizeof (*transfer_pubs),
299 : &num_transfer_pubs,
300 : (void **) &transfer_pubs),
301 : &no_transfer_pubs),
302 : GNUNET_PQ_result_spec_end
303 : };
304 :
305 14 : if (GNUNET_OK !=
306 14 : GNUNET_PQ_extract_result (result,
307 : rs,
308 : i))
309 : {
310 0 : GNUNET_break (0);
311 0 : GNUNET_PQ_cleanup_result (rs);
312 0 : GNUNET_free (melt);
313 0 : chc->failed = true;
314 0 : return;
315 : }
316 14 : if ( (! no_transfer_pubs) &&
317 14 : (num_transfer_pubs != melt->num_coins) )
318 : {
319 0 : GNUNET_break (0);
320 0 : GNUNET_PQ_cleanup_result (rs);
321 0 : GNUNET_free (melt);
322 0 : chc->failed = true;
323 0 : return;
324 : }
325 14 : melt->denom_pub_hashes = denom_pub_hashes;
326 14 : denom_pub_hashes = NULL;
327 14 : if (! no_transfer_pubs)
328 : {
329 14 : melt->transfer_pubs = transfer_pubs;
330 14 : transfer_pubs = NULL;
331 : }
332 14 : GNUNET_PQ_cleanup_result (rs);
333 : }
334 14 : tl = GNUNET_new (struct TALER_EXCHANGEDB_TransactionList);
335 14 : tl->next = chc->head;
336 14 : tl->type = TALER_EXCHANGEDB_TT_MELT;
337 14 : tl->details.melt = melt;
338 14 : tl->serial_id = serial_id;
339 14 : tl->coin_history_id = chc->chid;
340 14 : chc->head = tl;
341 : }
342 : }
343 :
344 :
345 : /**
346 : * Function to be called with the results of a SELECT statement
347 : * that has returned @a num_results results.
348 : *
349 : * @param cls closure of type `struct CoinHistoryContext`
350 : * @param result the postgres result
351 : * @param num_results the number of results in @a result
352 : */
353 : static void
354 13 : add_coin_refund (void *cls,
355 : PGresult *result,
356 : unsigned int num_results)
357 : {
358 13 : struct CoinHistoryContext *chc = cls;
359 13 : struct TALER_EXCHANGEDB_PostgresContext *pg = chc->pg;
360 :
361 26 : for (unsigned int i = 0; i<num_results; i++)
362 : {
363 : struct TALER_EXCHANGEDB_RefundListEntry *refund;
364 : struct TALER_EXCHANGEDB_TransactionList *tl;
365 : uint64_t serial_id;
366 :
367 13 : refund = GNUNET_new (struct TALER_EXCHANGEDB_RefundListEntry);
368 : {
369 13 : struct GNUNET_PQ_ResultSpec rs[] = {
370 13 : GNUNET_PQ_result_spec_auto_from_type ("merchant_pub",
371 : &refund->merchant_pub),
372 13 : GNUNET_PQ_result_spec_auto_from_type ("merchant_sig",
373 : &refund->merchant_sig),
374 13 : GNUNET_PQ_result_spec_auto_from_type ("h_contract_terms",
375 : &refund->h_contract_terms),
376 13 : GNUNET_PQ_result_spec_uint64 ("rtransaction_id",
377 : &refund->rtransaction_id),
378 13 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
379 : &refund->refund_amount),
380 13 : TALER_PQ_RESULT_SPEC_AMOUNT ("fee_refund",
381 : &refund->refund_fee),
382 13 : GNUNET_PQ_result_spec_uint64 ("refund_serial_id",
383 : &serial_id),
384 : GNUNET_PQ_result_spec_end
385 : };
386 :
387 13 : if (GNUNET_OK !=
388 13 : GNUNET_PQ_extract_result (result,
389 : rs,
390 : i))
391 : {
392 0 : GNUNET_break (0);
393 0 : GNUNET_free (refund);
394 0 : chc->failed = true;
395 0 : return;
396 : }
397 : }
398 13 : tl = GNUNET_new (struct TALER_EXCHANGEDB_TransactionList);
399 13 : tl->next = chc->head;
400 13 : tl->type = TALER_EXCHANGEDB_TT_REFUND;
401 13 : tl->details.refund = refund;
402 13 : tl->serial_id = serial_id;
403 13 : tl->coin_history_id = chc->chid;
404 13 : chc->head = tl;
405 : }
406 : }
407 :
408 :
409 : /**
410 : * Function to be called with the results of a SELECT statement
411 : * that has returned @a num_results results.
412 : *
413 : * @param cls closure of type `struct CoinHistoryContext`
414 : * @param result the postgres result
415 : * @param num_results the number of results in @a result
416 : */
417 : static void
418 0 : add_coin_purse_decision (void *cls,
419 : PGresult *result,
420 : unsigned int num_results)
421 : {
422 0 : struct CoinHistoryContext *chc = cls;
423 0 : struct TALER_EXCHANGEDB_PostgresContext *pg = chc->pg;
424 :
425 0 : for (unsigned int i = 0; i<num_results; i++)
426 : {
427 : struct TALER_EXCHANGEDB_PurseRefundListEntry *prefund;
428 : struct TALER_EXCHANGEDB_TransactionList *tl;
429 : uint64_t serial_id;
430 :
431 0 : prefund = GNUNET_new (struct TALER_EXCHANGEDB_PurseRefundListEntry);
432 : {
433 0 : struct GNUNET_PQ_ResultSpec rs[] = {
434 0 : GNUNET_PQ_result_spec_auto_from_type ("purse_pub",
435 : &prefund->purse_pub),
436 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
437 : &prefund->refund_amount),
438 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("fee_refund",
439 : &prefund->refund_fee),
440 0 : GNUNET_PQ_result_spec_uint64 ("purse_decision_serial_id",
441 : &serial_id),
442 : GNUNET_PQ_result_spec_end
443 : };
444 :
445 0 : if (GNUNET_OK !=
446 0 : GNUNET_PQ_extract_result (result,
447 : rs,
448 : i))
449 : {
450 0 : GNUNET_break (0);
451 0 : GNUNET_free (prefund);
452 0 : chc->failed = true;
453 0 : return;
454 : }
455 : }
456 0 : tl = GNUNET_new (struct TALER_EXCHANGEDB_TransactionList);
457 0 : tl->next = chc->head;
458 0 : tl->type = TALER_EXCHANGEDB_TT_PURSE_REFUND;
459 0 : tl->details.purse_refund = prefund;
460 0 : tl->serial_id = serial_id;
461 0 : tl->coin_history_id = chc->chid;
462 0 : chc->head = tl;
463 : }
464 : }
465 :
466 :
467 : /**
468 : * Function to be called with the results of a SELECT statement
469 : * that has returned @a num_results results.
470 : *
471 : * @param cls closure of type `struct CoinHistoryContext`
472 : * @param result the postgres result
473 : * @param num_results the number of results in @a result
474 : */
475 : static void
476 1 : add_old_coin_recoup (void *cls,
477 : PGresult *result,
478 : unsigned int num_results)
479 : {
480 1 : struct CoinHistoryContext *chc = cls;
481 1 : struct TALER_EXCHANGEDB_PostgresContext *pg = chc->pg;
482 :
483 2 : for (unsigned int i = 0; i<num_results; i++)
484 : {
485 : struct TALER_EXCHANGEDB_RecoupRefreshListEntry *recoup;
486 : struct TALER_EXCHANGEDB_TransactionList *tl;
487 : uint64_t serial_id;
488 :
489 1 : recoup = GNUNET_new (struct TALER_EXCHANGEDB_RecoupRefreshListEntry);
490 : {
491 1 : struct GNUNET_PQ_ResultSpec rs[] = {
492 1 : GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
493 : &recoup->coin.coin_pub),
494 1 : GNUNET_PQ_result_spec_auto_from_type ("coin_sig",
495 : &recoup->coin_sig),
496 1 : GNUNET_PQ_result_spec_auto_from_type ("coin_blind",
497 : &recoup->coin_blind),
498 1 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
499 : &recoup->value),
500 1 : GNUNET_PQ_result_spec_timestamp ("recoup_timestamp",
501 : &recoup->timestamp),
502 1 : GNUNET_PQ_result_spec_uint32 ("coin_index",
503 : &recoup->coin_index),
504 1 : GNUNET_PQ_result_spec_auto_from_type ("rc",
505 : &recoup->rc),
506 1 : GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash",
507 : &recoup->coin.denom_pub_hash),
508 1 : TALER_PQ_result_spec_denom_sig ("denom_sig",
509 : &recoup->coin.denom_sig),
510 1 : GNUNET_PQ_result_spec_uint64 ("recoup_refresh_uuid",
511 : &serial_id),
512 : GNUNET_PQ_result_spec_end
513 : };
514 :
515 1 : if (GNUNET_OK !=
516 1 : GNUNET_PQ_extract_result (result,
517 : rs,
518 : i))
519 : {
520 0 : GNUNET_break (0);
521 0 : GNUNET_free (recoup);
522 0 : chc->failed = true;
523 0 : return;
524 : }
525 1 : recoup->old_coin_pub = *chc->coin_pub;
526 : }
527 1 : tl = GNUNET_new (struct TALER_EXCHANGEDB_TransactionList);
528 1 : tl->next = chc->head;
529 1 : tl->type = TALER_EXCHANGEDB_TT_RECOUP_REFRESH_RECEIVER;
530 1 : tl->details.old_coin_recoup = recoup;
531 1 : tl->serial_id = serial_id;
532 1 : tl->coin_history_id = chc->chid;
533 1 : chc->head = tl;
534 : }
535 : }
536 :
537 :
538 : /**
539 : * Function to be called with the results of a SELECT statement
540 : * that has returned @a num_results results.
541 : *
542 : * @param cls closure of type `struct CoinHistoryContext`
543 : * @param result the postgres result
544 : * @param num_results the number of results in @a result
545 : */
546 : static void
547 11 : add_coin_recoup (void *cls,
548 : PGresult *result,
549 : unsigned int num_results)
550 : {
551 11 : struct CoinHistoryContext *chc = cls;
552 11 : struct TALER_EXCHANGEDB_PostgresContext *pg = chc->pg;
553 :
554 22 : for (unsigned int i = 0; i<num_results; i++)
555 : {
556 : struct TALER_EXCHANGEDB_RecoupListEntry *recoup;
557 : struct TALER_EXCHANGEDB_TransactionList *tl;
558 : uint64_t serial_id;
559 :
560 11 : recoup = GNUNET_new (struct TALER_EXCHANGEDB_RecoupListEntry);
561 : {
562 11 : struct GNUNET_PQ_ResultSpec rs[] = {
563 11 : GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
564 : &recoup->reserve_pub),
565 11 : GNUNET_PQ_result_spec_auto_from_type ("coin_sig",
566 : &recoup->coin_sig),
567 11 : GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash",
568 : &recoup->h_denom_pub),
569 11 : GNUNET_PQ_result_spec_auto_from_type ("coin_blind",
570 : &recoup->coin_blind),
571 11 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
572 : &recoup->value),
573 11 : GNUNET_PQ_result_spec_timestamp ("recoup_timestamp",
574 : &recoup->timestamp),
575 11 : GNUNET_PQ_result_spec_uint32 ("coin_index",
576 : &recoup->coin_index),
577 11 : GNUNET_PQ_result_spec_auto_from_type ("planchets_h",
578 : &recoup->planchets_h),
579 11 : GNUNET_PQ_result_spec_uint64 ("recoup_uuid",
580 : &serial_id),
581 : GNUNET_PQ_result_spec_end
582 : };
583 :
584 11 : if (GNUNET_OK !=
585 11 : GNUNET_PQ_extract_result (result,
586 : rs,
587 : i))
588 : {
589 0 : GNUNET_break (0);
590 0 : GNUNET_free (recoup);
591 0 : chc->failed = true;
592 0 : return;
593 : }
594 : }
595 11 : tl = GNUNET_new (struct TALER_EXCHANGEDB_TransactionList);
596 11 : tl->next = chc->head;
597 11 : tl->type = TALER_EXCHANGEDB_TT_RECOUP_WITHDRAW;
598 11 : tl->details.recoup = recoup;
599 11 : tl->serial_id = serial_id;
600 11 : tl->coin_history_id = chc->chid;
601 11 : chc->head = tl;
602 : }
603 : }
604 :
605 :
606 : /**
607 : * Function to be called with the results of a SELECT statement
608 : * that has returned @a num_results results.
609 : *
610 : * @param cls closure of type `struct CoinHistoryContext`
611 : * @param result the postgres result
612 : * @param num_results the number of results in @a result
613 : */
614 : static void
615 1 : add_coin_recoup_refresh (void *cls,
616 : PGresult *result,
617 : unsigned int num_results)
618 : {
619 1 : struct CoinHistoryContext *chc = cls;
620 1 : struct TALER_EXCHANGEDB_PostgresContext *pg = chc->pg;
621 :
622 2 : for (unsigned int i = 0; i<num_results; i++)
623 : {
624 : struct TALER_EXCHANGEDB_RecoupRefreshListEntry *recoup;
625 : struct TALER_EXCHANGEDB_TransactionList *tl;
626 : uint64_t serial_id;
627 :
628 1 : recoup = GNUNET_new (struct TALER_EXCHANGEDB_RecoupRefreshListEntry);
629 : {
630 1 : struct GNUNET_PQ_ResultSpec rs[] = {
631 1 : GNUNET_PQ_result_spec_auto_from_type ("old_coin_pub",
632 : &recoup->old_coin_pub),
633 1 : GNUNET_PQ_result_spec_auto_from_type ("coin_sig",
634 : &recoup->coin_sig),
635 1 : GNUNET_PQ_result_spec_auto_from_type ("coin_blind",
636 : &recoup->coin_blind),
637 1 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
638 : &recoup->value),
639 1 : GNUNET_PQ_result_spec_timestamp ("recoup_timestamp",
640 : &recoup->timestamp),
641 1 : GNUNET_PQ_result_spec_uint32 ("coin_index",
642 : &recoup->coin_index),
643 1 : GNUNET_PQ_result_spec_auto_from_type ("rc",
644 : &recoup->rc),
645 1 : GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash",
646 : &recoup->coin.denom_pub_hash),
647 1 : TALER_PQ_result_spec_denom_sig ("denom_sig",
648 : &recoup->coin.denom_sig),
649 1 : GNUNET_PQ_result_spec_uint64 ("recoup_refresh_uuid",
650 : &serial_id),
651 : GNUNET_PQ_result_spec_end
652 : };
653 :
654 1 : if (GNUNET_OK !=
655 1 : GNUNET_PQ_extract_result (result,
656 : rs,
657 : i))
658 : {
659 0 : GNUNET_break (0);
660 0 : GNUNET_free (recoup);
661 0 : chc->failed = true;
662 0 : return;
663 : }
664 1 : recoup->coin.coin_pub = *chc->coin_pub;
665 : }
666 1 : tl = GNUNET_new (struct TALER_EXCHANGEDB_TransactionList);
667 1 : tl->next = chc->head;
668 1 : tl->type = TALER_EXCHANGEDB_TT_RECOUP_REFRESH;
669 1 : tl->details.recoup_refresh = recoup;
670 1 : tl->serial_id = serial_id;
671 1 : tl->coin_history_id = chc->chid;
672 1 : chc->head = tl;
673 : }
674 : }
675 :
676 :
677 : /**
678 : * Function to be called with the results of a SELECT statement
679 : * that has returned @a num_results results.
680 : *
681 : * @param cls closure of type `struct CoinHistoryContext`
682 : * @param result the postgres result
683 : * @param num_results the number of results in @a result
684 : */
685 : static void
686 6 : add_coin_reserve_open (void *cls,
687 : PGresult *result,
688 : unsigned int num_results)
689 : {
690 6 : struct CoinHistoryContext *chc = cls;
691 6 : struct TALER_EXCHANGEDB_PostgresContext *pg = chc->pg;
692 :
693 12 : for (unsigned int i = 0; i<num_results; i++)
694 : {
695 : struct TALER_EXCHANGEDB_ReserveOpenListEntry *role;
696 : struct TALER_EXCHANGEDB_TransactionList *tl;
697 : uint64_t serial_id;
698 :
699 6 : role = GNUNET_new (struct TALER_EXCHANGEDB_ReserveOpenListEntry);
700 : {
701 6 : struct GNUNET_PQ_ResultSpec rs[] = {
702 6 : GNUNET_PQ_result_spec_auto_from_type ("reserve_sig",
703 : &role->reserve_sig),
704 6 : GNUNET_PQ_result_spec_auto_from_type ("coin_sig",
705 : &role->coin_sig),
706 6 : TALER_PQ_RESULT_SPEC_AMOUNT ("contribution",
707 : &role->coin_contribution),
708 6 : GNUNET_PQ_result_spec_allow_null (
709 6 : GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash",
710 : &role->h_age_commitment),
711 : &role->no_age_commitment),
712 6 : GNUNET_PQ_result_spec_uint64 ("reserve_open_deposit_uuid",
713 : &serial_id),
714 : GNUNET_PQ_result_spec_end
715 : };
716 :
717 6 : if (GNUNET_OK !=
718 6 : GNUNET_PQ_extract_result (result,
719 : rs,
720 : i))
721 : {
722 0 : GNUNET_break (0);
723 0 : GNUNET_free (role);
724 0 : chc->failed = true;
725 0 : return;
726 : }
727 : }
728 6 : tl = GNUNET_new (struct TALER_EXCHANGEDB_TransactionList);
729 6 : tl->next = chc->head;
730 6 : tl->type = TALER_EXCHANGEDB_TT_RESERVE_OPEN;
731 6 : tl->details.reserve_open = role;
732 6 : tl->serial_id = serial_id;
733 6 : tl->coin_history_id = chc->chid;
734 6 : chc->head = tl;
735 : }
736 : }
737 :
738 :
739 : /**
740 : * Work we need to do.
741 : */
742 : struct Work
743 : {
744 : /**
745 : * Name of the table.
746 : */
747 : const char *table;
748 :
749 : /**
750 : * SQL prepared statement name.
751 : */
752 : const char *statement;
753 :
754 : /**
755 : * Function to call to handle the result(s).
756 : */
757 : GNUNET_PQ_PostgresResultHandler cb;
758 : };
759 :
760 :
761 : /**
762 : * We found a coin history entry. Lookup details
763 : * from the respective table and store in @a cls.
764 : *
765 : * @param[in,out] cls a `struct CoinHistoryContext`
766 : * @param result a coin history entry result set
767 : * @param num_results total number of results in @a results
768 : */
769 : static void
770 86 : handle_history_entry (void *cls,
771 : PGresult *result,
772 : unsigned int num_results)
773 : {
774 86 : struct CoinHistoryContext *chc = cls;
775 86 : struct TALER_EXCHANGEDB_PostgresContext *pg = chc->pg;
776 : static const struct Work work[] = {
777 : [TALER_EXCHANGEDB_TT_DEPOSIT] =
778 : { "coin_deposits",
779 : "get_coin_transactions_deposit_with_coin_pub",
780 : &add_coin_deposit },
781 : [TALER_EXCHANGEDB_TT_MELT] =
782 : { "refresh",
783 : "get_coin_transactions_refresh_by_coin",
784 : &add_coin_melt },
785 : [TALER_EXCHANGEDB_TT_PURSE_DEPOSIT] =
786 : { "purse_deposits",
787 : "get_coin_transactions_purse_deposit_by_coin_pub",
788 : &add_coin_purse_deposit },
789 : [TALER_EXCHANGEDB_TT_PURSE_REFUND] =
790 : { "purse_decision",
791 : "get_coin_transactions_purse_decision_by_coin_pub",
792 : &add_coin_purse_decision },
793 : [TALER_EXCHANGEDB_TT_REFUND] =
794 : { "refunds",
795 : "get_coin_transactions_refunds_by_coin",
796 : &add_coin_refund },
797 : [TALER_EXCHANGEDB_TT_RECOUP_WITHDRAW] =
798 : { "recoup",
799 : "get_coin_transactions_recoup_by_coin",
800 : &add_coin_recoup },
801 : [TALER_EXCHANGEDB_TT_RECOUP_REFRESH] =
802 : { "recoup_refresh::NEW",
803 : "get_coin_transactions_recoup_by_refreshed_coin",
804 : &add_coin_recoup_refresh },
805 : [TALER_EXCHANGEDB_TT_RECOUP_REFRESH_RECEIVER] =
806 : { "recoup_refresh::OLD",
807 : "get_coin_transactions_recoup_by_old_coin",
808 : &add_old_coin_recoup },
809 : [TALER_EXCHANGEDB_TT_RESERVE_OPEN] =
810 : { "reserves_open_deposits",
811 : "get_coin_transactions_reserve_open_by_coin",
812 : &add_coin_reserve_open },
813 : { NULL, NULL, NULL }
814 : };
815 : char *table_name;
816 : uint64_t serial_id;
817 86 : struct GNUNET_PQ_ResultSpec rs[] = {
818 86 : GNUNET_PQ_result_spec_string ("table_name",
819 : &table_name),
820 86 : GNUNET_PQ_result_spec_uint64 ("serial_id",
821 : &serial_id),
822 86 : GNUNET_PQ_result_spec_uint64 ("coin_history_serial_id",
823 : &chc->chid),
824 : GNUNET_PQ_result_spec_end
825 : };
826 86 : struct GNUNET_PQ_QueryParam params[] = {
827 86 : GNUNET_PQ_query_param_auto_from_type (chc->coin_pub),
828 86 : GNUNET_PQ_query_param_uint64 (&serial_id),
829 : GNUNET_PQ_query_param_end
830 : };
831 :
832 218 : for (unsigned int i = 0; i<num_results; i++)
833 : {
834 : enum GNUNET_DB_QueryStatus qs;
835 132 : bool found = false;
836 :
837 132 : if (GNUNET_OK !=
838 132 : GNUNET_PQ_extract_result (result,
839 : rs,
840 : i))
841 : {
842 0 : GNUNET_break (0);
843 0 : chc->failed = true;
844 0 : return;
845 : }
846 :
847 132 : for (unsigned int s = 0;
848 326 : NULL != work[s].statement;
849 194 : s++)
850 : {
851 520 : if (0 != strcmp (table_name,
852 326 : work[s].table))
853 194 : continue;
854 132 : found = true;
855 132 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
856 132 : work[s].statement,
857 : params,
858 132 : work[s].cb,
859 : chc);
860 132 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
861 : "Coin %s had %d transactions at %llu in table %s\n",
862 : TALER_B2S (chc->coin_pub),
863 : (int) qs,
864 : (unsigned long long) serial_id,
865 : table_name);
866 132 : if (0 > qs)
867 0 : chc->failed = true;
868 132 : break;
869 : }
870 132 : if (! found)
871 : {
872 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
873 : "Coin history includes unsupported table `%s`\n",
874 : table_name);
875 0 : chc->failed = true;
876 : }
877 132 : GNUNET_PQ_cleanup_result (rs);
878 132 : if (chc->failed)
879 0 : break;
880 : }
881 : }
882 :
883 :
884 : enum GNUNET_DB_QueryStatus
885 91 : TALER_EXCHANGEDB_get_coin_transactions (
886 : struct TALER_EXCHANGEDB_PostgresContext *pg,
887 : bool begin_transaction,
888 : const struct TALER_CoinSpendPublicKeyP *coin_pub,
889 : uint64_t start_off,
890 : uint64_t etag_in,
891 : uint64_t *etag_out,
892 : struct TALER_Amount *balance,
893 : struct TALER_DenominationHashP *h_denom_pub,
894 : struct TALER_EXCHANGEDB_TransactionList **tlp)
895 : {
896 91 : struct GNUNET_PQ_QueryParam params[] = {
897 91 : GNUNET_PQ_query_param_auto_from_type (coin_pub),
898 : GNUNET_PQ_query_param_end
899 : };
900 91 : struct GNUNET_PQ_QueryParam lparams[] = {
901 91 : GNUNET_PQ_query_param_auto_from_type (coin_pub),
902 91 : GNUNET_PQ_query_param_uint64 (&start_off),
903 : GNUNET_PQ_query_param_end
904 : };
905 91 : struct CoinHistoryContext chc = {
906 : .head = NULL,
907 : .coin_pub = coin_pub,
908 : .pg = pg
909 : };
910 :
911 91 : *tlp = NULL;
912 91 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
913 : "Getting transactions for coin %s\n",
914 : TALER_B2S (coin_pub));
915 91 : PREPARE (pg,
916 : "get_coin_transactions_history_etag_balance",
917 : "SELECT"
918 : " ch.coin_history_serial_id"
919 : ",kc.remaining"
920 : ",denom.denom_pub_hash"
921 : " FROM coin_history ch"
922 : " JOIN known_coins kc"
923 : " USING (coin_pub)"
924 : " JOIN denominations denom"
925 : " USING (denominations_serial)"
926 : " WHERE coin_pub=$1"
927 : " ORDER BY coin_history_serial_id DESC"
928 : " LIMIT 1;");
929 91 : PREPARE (pg,
930 : "get_coin_transactions_history",
931 : "SELECT"
932 : " table_name"
933 : ",serial_id"
934 : ",coin_history_serial_id"
935 : " FROM coin_history"
936 : " WHERE coin_pub=$1"
937 : " AND coin_history_serial_id > $2"
938 : " ORDER BY coin_history_serial_id DESC;");
939 91 : PREPARE (pg,
940 : "get_coin_transactions_deposit_with_coin_pub",
941 : "SELECT"
942 : " cdep.amount_with_fee"
943 : ",denoms.fee_deposit"
944 : ",denoms.denom_pub_hash"
945 : ",kc.age_commitment_hash"
946 : ",bdep.wallet_timestamp"
947 : ",bdep.refund_deadline"
948 : ",bdep.wire_deadline"
949 : ",bdep.merchant_pub"
950 : ",bdep.h_contract_terms"
951 : ",bdep.wallet_data_hash"
952 : ",bdep.wire_salt"
953 : ",wt.payto_uri"
954 : ",cdep.coin_sig"
955 : ",cdep.coin_deposit_serial_id"
956 : ",bdep.done"
957 : " FROM coin_deposits cdep"
958 : " JOIN batch_deposits bdep"
959 : " USING (batch_deposit_serial_id)"
960 : " JOIN wire_targets wt"
961 : " USING (wire_target_h_payto)"
962 : " JOIN known_coins kc"
963 : " ON (kc.coin_pub = cdep.coin_pub)"
964 : " JOIN denominations denoms"
965 : " USING (denominations_serial)"
966 : " WHERE cdep.coin_pub=$1"
967 : " AND cdep.coin_deposit_serial_id=$2;");
968 91 : PREPARE (pg,
969 : "get_coin_transactions_refresh_by_coin",
970 : "SELECT"
971 : " rc"
972 : ",refresh_seed"
973 : ",blinding_seed"
974 : ",old_coin_sig"
975 : ",amount_with_fee"
976 : ",denoms.denom_pub_hash"
977 : ",denoms.fee_refresh"
978 : ",kc.age_commitment_hash"
979 : ",refresh_id"
980 : ",noreveal_index"
981 : ",transfer_pubs"
982 : ",ARRAY("
983 : " SELECT fresh_denoms.denom_pub_hash"
984 : " FROM unnest(refresh.denom_serials) WITH ORDINALITY"
985 : " AS fresh(denom_serial, coin_index)"
986 : " LEFT JOIN denominations fresh_denoms"
987 : " ON fresh_denoms.denominations_serial=fresh.denom_serial"
988 : " ORDER BY fresh.coin_index"
989 : ") AS denom_pub_hashes"
990 : " FROM refresh"
991 : " JOIN known_coins kc"
992 : " ON (refresh.old_coin_pub = kc.coin_pub)"
993 : " JOIN denominations denoms"
994 : " USING (denominations_serial)"
995 : " WHERE old_coin_pub=$1"
996 : " AND refresh_id=$2;");
997 91 : PREPARE (pg,
998 : "get_coin_transactions_purse_deposit_by_coin_pub",
999 : "SELECT"
1000 : " partner_base_url"
1001 : ",pd.amount_with_fee"
1002 : ",denoms.fee_deposit"
1003 : ",denoms.denom_pub_hash"
1004 : ",pd.purse_pub"
1005 : ",kc.age_commitment_hash"
1006 : ",pd.coin_sig"
1007 : ",pd.purse_deposit_serial_id"
1008 : ",pdes.refunded"
1009 : " FROM purse_deposits pd"
1010 : " LEFT JOIN partners"
1011 : " USING (partner_serial_id)"
1012 : " JOIN purse_requests pr"
1013 : " USING (purse_pub)"
1014 : " LEFT JOIN purse_decision pdes"
1015 : " USING (purse_pub)"
1016 : " JOIN known_coins kc"
1017 : " ON (pd.coin_pub = kc.coin_pub)"
1018 : " JOIN denominations denoms"
1019 : " USING (denominations_serial)"
1020 : " WHERE pd.purse_deposit_serial_id=$2"
1021 : " AND pd.coin_pub=$1;");
1022 91 : PREPARE (pg,
1023 : "get_coin_transactions_purse_decision_by_coin_pub",
1024 : "SELECT"
1025 : " pdes.purse_pub"
1026 : ",pd.amount_with_fee"
1027 : ",denom.fee_refund"
1028 : ",pdes.purse_decision_serial_id"
1029 : " FROM purse_decision pdes"
1030 : " JOIN purse_deposits pd"
1031 : " USING (purse_pub)"
1032 : " JOIN known_coins kc"
1033 : " ON (pd.coin_pub = kc.coin_pub)"
1034 : " JOIN denominations denom"
1035 : " USING (denominations_serial)"
1036 : " WHERE pd.coin_pub=$1"
1037 : " AND pdes.purse_decision_serial_id=$2"
1038 : " AND pdes.refunded;");
1039 91 : PREPARE (pg,
1040 : "get_coin_transactions_refunds_by_coin",
1041 : "SELECT"
1042 : " bdep.merchant_pub"
1043 : ",ref.merchant_sig"
1044 : ",bdep.h_contract_terms"
1045 : ",ref.rtransaction_id"
1046 : ",ref.amount_with_fee"
1047 : ",denom.fee_refund"
1048 : ",ref.refund_serial_id"
1049 : " FROM refunds ref"
1050 : " JOIN coin_deposits cdep"
1051 : " ON (ref.coin_pub = cdep.coin_pub AND ref.batch_deposit_serial_id = cdep.batch_deposit_serial_id)"
1052 : " JOIN batch_deposits bdep"
1053 : " ON (ref.batch_deposit_serial_id = bdep.batch_deposit_serial_id)"
1054 : " JOIN known_coins kc"
1055 : " ON (ref.coin_pub = kc.coin_pub)"
1056 : " JOIN denominations denom"
1057 : " USING (denominations_serial)"
1058 : " WHERE ref.coin_pub=$1"
1059 : " AND ref.refund_serial_id=$2;");
1060 91 : PREPARE (pg,
1061 : "get_coin_transactions_recoup_by_old_coin",
1062 : "SELECT"
1063 : " coins.coin_pub"
1064 : ",rr.coin_sig"
1065 : ",rr.coin_blind"
1066 : ",rr.amount"
1067 : ",rr.recoup_timestamp"
1068 : ",rr.coin_index"
1069 : ",rfc.rc"
1070 : ",denoms.denom_pub_hash"
1071 : ",coins.denom_sig"
1072 : ",rr.recoup_refresh_uuid"
1073 : " FROM recoup_refresh rr"
1074 : " JOIN refresh rfc"
1075 : " ON (rfc.refresh_id = rr.refresh_id)"
1076 : " JOIN known_coins coins"
1077 : " ON (coins.coin_pub = rr.coin_pub)"
1078 : " JOIN denominations denoms"
1079 : " ON (denoms.denominations_serial = coins.denominations_serial)"
1080 : " WHERE rr.recoup_refresh_uuid=$2"
1081 : " AND rfc.old_coin_pub=$1;");
1082 91 : PREPARE (pg,
1083 : "get_coin_transactions_recoup_by_coin",
1084 : "SELECT"
1085 : " res.reserve_pub"
1086 : ",denoms.denom_pub_hash"
1087 : ",rcp.coin_sig"
1088 : ",rcp.coin_blind"
1089 : ",rcp.amount"
1090 : ",rcp.recoup_timestamp"
1091 : ",rcp.coin_index"
1092 : ",ro.planchets_h"
1093 : ",rcp.recoup_uuid"
1094 : " FROM recoup rcp"
1095 : " JOIN withdraw ro"
1096 : " USING (withdraw_id)"
1097 : " JOIN reserves res"
1098 : " USING (reserve_pub)"
1099 : " JOIN known_coins coins"
1100 : " USING (coin_pub)"
1101 : " JOIN denominations denoms"
1102 : " ON (denoms.denominations_serial = coins.denominations_serial)"
1103 : " WHERE rcp.recoup_uuid=$2"
1104 : " AND coins.coin_pub=$1;");
1105 : /* Used to obtain recoup transactions
1106 : for a refreshed coin */
1107 91 : PREPARE (pg,
1108 : "get_coin_transactions_recoup_by_refreshed_coin",
1109 : "SELECT"
1110 : " old_coins.coin_pub AS old_coin_pub"
1111 : ",rr.coin_sig"
1112 : ",rr.coin_blind"
1113 : ",rr.amount"
1114 : ",rr.recoup_timestamp"
1115 : ",rr.coin_index"
1116 : ",rfc.rc"
1117 : ",denoms.denom_pub_hash"
1118 : ",coins.denom_sig"
1119 : ",recoup_refresh_uuid"
1120 : " FROM recoup_refresh rr"
1121 : " JOIN refresh rfc"
1122 : " ON (rr.refresh_id = rfc.refresh_id)"
1123 : " JOIN known_coins old_coins"
1124 : " ON (rfc.old_coin_pub = old_coins.coin_pub)"
1125 : " JOIN known_coins coins"
1126 : " ON (rr.coin_pub = coins.coin_pub)"
1127 : " JOIN denominations denoms"
1128 : " ON (denoms.denominations_serial = coins.denominations_serial)"
1129 : " WHERE rr.recoup_refresh_uuid=$2"
1130 : " AND coins.coin_pub=$1;");
1131 91 : PREPARE (pg,
1132 : "get_coin_transactions_reserve_open_by_coin",
1133 : "SELECT"
1134 : " rod.reserve_open_deposit_uuid"
1135 : ",rod.coin_sig"
1136 : ",rod.reserve_sig"
1137 : ",rod.contribution"
1138 : ",kc.age_commitment_hash"
1139 : " FROM reserves_open_deposits rod"
1140 : " JOIN known_coins kc"
1141 : " ON (rod.coin_pub = kc.coin_pub)"
1142 : " WHERE rod.coin_pub=$1"
1143 : " AND rod.reserve_open_deposit_uuid=$2;");
1144 91 : for (unsigned int i = 0; i<RETRIES; i++)
1145 : {
1146 : enum GNUNET_DB_QueryStatus qs;
1147 : uint64_t end;
1148 91 : struct GNUNET_PQ_ResultSpec rs[] = {
1149 91 : GNUNET_PQ_result_spec_uint64 ("coin_history_serial_id",
1150 : &end),
1151 91 : GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash",
1152 : h_denom_pub),
1153 91 : TALER_PQ_RESULT_SPEC_AMOUNT ("remaining",
1154 : balance),
1155 : GNUNET_PQ_result_spec_end
1156 : };
1157 :
1158 91 : if (begin_transaction)
1159 : {
1160 28 : if (GNUNET_OK !=
1161 28 : TALER_EXCHANGEDB_start_read_committed (pg,
1162 : "get-coin-transactions"))
1163 : {
1164 0 : GNUNET_break (0);
1165 91 : return GNUNET_DB_STATUS_HARD_ERROR;
1166 : }
1167 : }
1168 : /* First only check the last item, to see if
1169 : we even need to iterate */
1170 91 : qs = GNUNET_PQ_eval_prepared_singleton_select (
1171 : pg->conn,
1172 : "get_coin_transactions_history_etag_balance",
1173 : params,
1174 : rs);
1175 91 : switch (qs)
1176 : {
1177 0 : case GNUNET_DB_STATUS_HARD_ERROR:
1178 0 : if (begin_transaction)
1179 0 : TALER_EXCHANGEDB_rollback (pg);
1180 0 : return qs;
1181 0 : case GNUNET_DB_STATUS_SOFT_ERROR:
1182 0 : if (begin_transaction)
1183 0 : TALER_EXCHANGEDB_rollback (pg);
1184 0 : continue;
1185 4 : case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
1186 4 : if (begin_transaction)
1187 4 : TALER_EXCHANGEDB_rollback (pg);
1188 4 : return qs;
1189 87 : case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
1190 87 : *etag_out = end;
1191 87 : if (end == etag_in)
1192 : {
1193 : /* client is up-to-date, nothing more to do */
1194 1 : if (begin_transaction)
1195 1 : TALER_EXCHANGEDB_rollback (pg);
1196 1 : return qs;
1197 : }
1198 : }
1199 : /* We indeed need to iterate over the history */
1200 86 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1201 : "Current ETag for coin %s is %llu\n",
1202 : TALER_B2S (coin_pub),
1203 : (unsigned long long) end);
1204 :
1205 86 : qs = GNUNET_PQ_eval_prepared_multi_select (
1206 : pg->conn,
1207 : "get_coin_transactions_history",
1208 : lparams,
1209 : &handle_history_entry,
1210 : &chc);
1211 86 : switch (qs)
1212 : {
1213 0 : case GNUNET_DB_STATUS_HARD_ERROR:
1214 0 : if (begin_transaction)
1215 0 : TALER_EXCHANGEDB_rollback (pg);
1216 0 : return qs;
1217 0 : case GNUNET_DB_STATUS_SOFT_ERROR:
1218 0 : if (begin_transaction)
1219 0 : TALER_EXCHANGEDB_rollback (pg);
1220 0 : TALER_EXCHANGEDB_free_coin_transaction_list (chc.head);
1221 0 : chc.head = NULL;
1222 0 : continue;
1223 86 : default:
1224 86 : break;
1225 : }
1226 86 : if (chc.failed)
1227 : {
1228 0 : if (begin_transaction)
1229 0 : TALER_EXCHANGEDB_rollback (pg);
1230 0 : TALER_EXCHANGEDB_free_coin_transaction_list (chc.head);
1231 0 : return GNUNET_DB_STATUS_SOFT_ERROR;
1232 : }
1233 86 : if (! begin_transaction)
1234 : {
1235 63 : *tlp = chc.head;
1236 63 : return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
1237 : }
1238 23 : qs = TALER_EXCHANGEDB_commit (pg);
1239 23 : switch (qs)
1240 : {
1241 0 : case GNUNET_DB_STATUS_HARD_ERROR:
1242 0 : TALER_EXCHANGEDB_free_coin_transaction_list (chc.head);
1243 0 : chc.head = NULL;
1244 0 : return qs;
1245 0 : case GNUNET_DB_STATUS_SOFT_ERROR:
1246 0 : TALER_EXCHANGEDB_free_coin_transaction_list (chc.head);
1247 0 : chc.head = NULL;
1248 0 : continue;
1249 23 : case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
1250 : case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
1251 23 : *tlp = chc.head;
1252 23 : return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
1253 : }
1254 : }
1255 0 : return GNUNET_DB_STATUS_SOFT_ERROR;
1256 : }
|