exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

get_coin_transactions.c (43279B)


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