exchange

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

test_coin_history.c (35615B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2026 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 exchangedb/test_coin_history.c
     18  * @brief tests for the exchangedb functions whose primary table is
     19  *        `coin_history`
     20  * @author Christian Grothoff
     21  *
     22  * Covers #TALER_EXCHANGEDB_get_coin_transactions().
     23  *
     24  * Nothing writes to `coin_history` directly: every table that can spend or
     25  * credit a coin has an INSERT trigger that appends a row naming itself and
     26  * its own serial.  get_coin_transactions() walks those rows and looks each
     27  * one up in the table it names.  So the interesting part of this test is
     28  * spending one coin in as many different ways as possible and then checking
     29  * that every one of them comes back with the right type and amount.
     30  */
     31 #include "test_common.h"
     32 #include "exchange-database/do_purse_deposit.h"
     33 #include "exchange-database/rollback.h"
     34 #include "exchange-database/start.h"
     35 #include "exchange-database/do_recoup.h"
     36 #include "exchange-database/do_recoup_refresh.h"
     37 #include "exchange-database/do_refresh.h"
     38 #include "exchange-database/do_refund.h"
     39 #include "exchange-database/get_coin_transactions.h"
     40 #include "exchange-database/insert_reserve_open_deposit.h"
     41 #include "exchange-database/update_to_refresh_revealed.h"
     42 
     43 /**
     44  * Number of fresh coins of the melt in check_other_spends().
     45  */
     46 #define NUM_FRESH_COINS 3
     47 
     48 
     49 /**
     50  * Account the checks fund their reserves from.
     51  */
     52 static struct TDB_Account account;
     53 
     54 
     55 /**
     56  * Denomination the checks use.
     57  */
     58 static struct TDB_Denom denom;
     59 
     60 
     61 /**
     62  * A different denomination used among the refresh outputs.
     63  */
     64 static struct TDB_Denom fresh_denom;
     65 
     66 
     67 /**
     68  * The coin whose history we build up.
     69  */
     70 static struct TALER_CoinPublicInfo coin;
     71 
     72 
     73 /**
     74  * Row of @e coin in `known_coins`.
     75  */
     76 static uint64_t known_coin_id;
     77 
     78 
     79 /**
     80  * Reserve @e coin was withdrawn from.
     81  */
     82 static struct TALER_ReservePublicKeyP reserve_pub;
     83 
     84 
     85 /**
     86  * Row of the withdraw that created @e coin.
     87  */
     88 static uint64_t withdraw_id;
     89 
     90 
     91 /**
     92  * The deposit we later refund.
     93  */
     94 static struct TDB_Deposit deposit;
     95 
     96 
     97 /**
     98  * ETag of the history right after the deposit was refunded.
     99  */
    100 static uint64_t etag_after_refund;
    101 
    102 
    103 /**
    104  * Count the entries of @a tl and remember which types occurred.
    105  *
    106  * @param tl transaction list to walk
    107  * @param[out] types set to the bitmask of the types seen
    108  * @return number of entries in @a tl
    109  */
    110 static unsigned int
    111 summarize (const struct TALER_EXCHANGEDB_TransactionList *tl,
    112            unsigned int *types)
    113 {
    114   unsigned int cnt = 0;
    115 
    116   *types = 0;
    117   for (const struct TALER_EXCHANGEDB_TransactionList *pos = tl;
    118        NULL != pos;
    119        pos = pos->next)
    120   {
    121     cnt++;
    122     *types |= 1U << pos->type;
    123   }
    124   return cnt;
    125 }
    126 
    127 
    128 /**
    129  * Ask for the full history of @e coin.
    130  *
    131  * @param pg the database context
    132  * @param start_off offset to start from
    133  * @param etag_in ETag the caller already has
    134  * @param[out] etag_out set to the current ETag
    135  * @param[out] tlp set to the history
    136  * @return transaction status
    137  */
    138 static enum GNUNET_DB_QueryStatus
    139 history (struct TALER_EXCHANGEDB_PostgresContext *pg,
    140          uint64_t start_off,
    141          uint64_t etag_in,
    142          uint64_t *etag_out,
    143          struct TALER_EXCHANGEDB_TransactionList **tlp)
    144 {
    145   struct TALER_Amount balance;
    146   struct TALER_DenominationHashP h_denom_pub;
    147 
    148   return TALER_EXCHANGEDB_get_coin_transactions (pg,
    149                                                  true,
    150                                                  &coin.coin_pub,
    151                                                  start_off,
    152                                                  etag_in,
    153                                                  etag_out,
    154                                                  &balance,
    155                                                  &h_denom_pub,
    156                                                  tlp);
    157 }
    158 
    159 
    160 /**
    161  * A coin the exchange never saw has no history.
    162  *
    163  * @param pg the database context
    164  * @return 0 on success
    165  */
    166 static int
    167 check_unknown_coin (struct TALER_EXCHANGEDB_PostgresContext *pg)
    168 {
    169   struct TALER_EXCHANGEDB_TransactionList *tl = (void *) 1;
    170   struct TALER_CoinSpendPublicKeyP unknown;
    171   struct TALER_Amount balance;
    172   struct TALER_DenominationHashP h_denom_pub;
    173   uint64_t etag = 42;
    174 
    175   TDB_account (pg,
    176                10,
    177                &account);
    178   TDB_denom (pg,
    179              10,
    180              "5",
    181              "0.1",
    182              &denom);
    183   TDB_FILL (unknown,
    184             99);
    185   FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
    186           TALER_EXCHANGEDB_get_coin_transactions (pg,
    187                                                   true,
    188                                                   &unknown,
    189                                                   0,
    190                                                   0,
    191                                                   &etag,
    192                                                   &balance,
    193                                                   &h_denom_pub,
    194                                                   &tl));
    195   FAILIF (NULL != tl);
    196   return 0;
    197 }
    198 
    199 
    200 /**
    201  * A coin that was withdrawn but never spent has no history either: the
    202  * `withdraw` table has no coin history trigger.
    203  *
    204  * @param pg the database context
    205  * @return 0 on success
    206  */
    207 static int
    208 check_fresh_coin (struct TALER_EXCHANGEDB_PostgresContext *pg)
    209 {
    210   struct TALER_EXCHANGEDB_TransactionList *tl = (void *) 1;
    211   uint64_t etag = 42;
    212 
    213   TDB_reserve_in (pg,
    214                   &account,
    215                   11,
    216                   "10",
    217                   &reserve_pub);
    218   withdraw_id = TDB_withdraw (pg,
    219                               &denom,
    220                               &reserve_pub,
    221                               11,
    222                               "5");
    223   TDB_coin (pg,
    224             &denom,
    225             20,
    226             &coin,
    227             &known_coin_id);
    228   FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
    229           history (pg,
    230                    0,
    231                    0,
    232                    &etag,
    233                    &tl));
    234   FAILIF (NULL != tl);
    235   FAILIF (0 != TDB_count (pg,
    236                           "FROM coin_history"));
    237   return 0;
    238 }
    239 
    240 
    241 /**
    242  * A deposit and its refund show up as two entries.
    243  *
    244  * @param pg the database context
    245  * @return 0 on success
    246  */
    247 static int
    248 check_deposit_and_refund (struct TALER_EXCHANGEDB_PostgresContext *pg)
    249 {
    250   struct TALER_EXCHANGEDB_TransactionList *tl = NULL;
    251   struct TALER_EXCHANGEDB_Refund refund;
    252   struct TALER_Amount deposit_fee = TDB_amount ("0.1");
    253   struct TALER_Amount balance;
    254   struct TALER_Amount expect = TDB_amount ("4.5");
    255   struct TALER_DenominationHashP h_denom_pub;
    256   unsigned int types;
    257   uint64_t etag = 0;
    258   bool not_found;
    259   bool refund_ok;
    260   bool gone;
    261   bool conflict;
    262 
    263   TDB_deposit (pg,
    264                &account,
    265                &coin,
    266                30,
    267                "1",
    268                "0.1",
    269                GNUNET_TIME_relative_to_timestamp (GNUNET_TIME_UNIT_HOURS),
    270                GNUNET_TIME_relative_to_timestamp (GNUNET_TIME_UNIT_HOURS),
    271                &deposit);
    272   FAILIF (1 != TDB_count (pg,
    273                           "FROM coin_history"));
    274 
    275   memset (&refund,
    276           0,
    277           sizeof (refund));
    278   refund.coin = coin;
    279   refund.details.merchant_pub = deposit.merchant_pub;
    280   TDB_FILL (refund.details.merchant_sig,
    281             31);
    282   refund.details.h_contract_terms = deposit.h_contract_terms;
    283   refund.details.rtransaction_id = 1;
    284   refund.details.refund_amount = TDB_amount ("0.5");
    285   refund.details.refund_fee = TDB_amount ("0");
    286   FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    287           TALER_EXCHANGEDB_do_refund (pg,
    288                                       &refund,
    289                                       &deposit_fee,
    290                                       0,
    291                                       &not_found,
    292                                       &refund_ok,
    293                                       &gone,
    294                                       &conflict));
    295   FAILIF (not_found);
    296   FAILIF (! refund_ok);
    297   FAILIF (2 != TDB_count (pg,
    298                           "FROM coin_history"));
    299 
    300   FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    301           TALER_EXCHANGEDB_get_coin_transactions (pg,
    302                                                   true,
    303                                                   &coin.coin_pub,
    304                                                   0,
    305                                                   0,
    306                                                   &etag,
    307                                                   &balance,
    308                                                   &h_denom_pub,
    309                                                   &tl));
    310   FAILIF_C (2 != summarize (tl,
    311                             &types),
    312             TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    313   FAILIF_C (0 == (types & (1U << TALER_EXCHANGEDB_TT_DEPOSIT)),
    314             TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    315   FAILIF_C (0 == (types & (1U << TALER_EXCHANGEDB_TT_REFUND)),
    316             TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    317   /* 5 - 1 + 0.5 */
    318   FAILIF_C (0 != TALER_amount_cmp (&balance,
    319                                    &expect),
    320             TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    321   FAILIF_C (0 != GNUNET_memcmp (&h_denom_pub,
    322                                 &denom.h_denom_pub),
    323             TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    324   TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    325   FAILIF (0 == etag);
    326   etag_after_refund = etag;
    327   return 0;
    328 }
    329 
    330 
    331 /**
    332  * A caller that is already up to date gets no list back, and the
    333  * transaction the lookup opened is not left dangling.
    334  *
    335  * @param pg the database context
    336  * @return 0 on success
    337  */
    338 static int
    339 check_etag (struct TALER_EXCHANGEDB_PostgresContext *pg)
    340 {
    341   struct TALER_EXCHANGEDB_TransactionList *tl = (void *) 1;
    342   uint64_t etag = 0;
    343 
    344   FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    345           history (pg,
    346                    0,
    347                    etag_after_refund,
    348                    &etag,
    349                    &tl));
    350   FAILIF (NULL != tl);
    351   FAILIF (etag != etag_after_refund);
    352   /* the lookup must not leave a transaction open (it starts one of its
    353      own when begin_transaction is true) */
    354   FAILIF (NULL != pg->transaction_name);
    355 
    356   /* an ETag that is not the current one still returns the history */
    357   FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    358           history (pg,
    359                    0,
    360                    etag_after_refund - 1,
    361                    &etag,
    362                    &tl));
    363   FAILIF (NULL == tl);
    364   TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    365   FAILIF (NULL != pg->transaction_name);
    366   return 0;
    367 }
    368 
    369 
    370 /**
    371  * The offset skips everything up to and including it.
    372  *
    373  * @param pg the database context
    374  * @return 0 on success
    375  */
    376 static int
    377 check_offset (struct TALER_EXCHANGEDB_PostgresContext *pg)
    378 {
    379   struct TALER_EXCHANGEDB_TransactionList *tl = NULL;
    380   unsigned int types;
    381   uint64_t etag = 0;
    382 
    383   /* everything after the deposit: only the refund is left */
    384   FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    385           history (pg,
    386                    etag_after_refund - 1,
    387                    0,
    388                    &etag,
    389                    &tl));
    390   FAILIF_C (1 != summarize (tl,
    391                             &types),
    392             TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    393   FAILIF_C (0 == (types & (1U << TALER_EXCHANGEDB_TT_REFUND)),
    394             TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    395   TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    396 
    397   /* everything after the last entry: nothing, but the ETag is still
    398      reported */
    399   tl = (void *) 1;
    400   etag = 0;
    401   FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    402           history (pg,
    403                    etag_after_refund,
    404                    0,
    405                    &etag,
    406                    &tl));
    407   FAILIF (NULL != tl);
    408   FAILIF (etag != etag_after_refund);
    409   return 0;
    410 }
    411 
    412 
    413 /**
    414  * Spending the coin into a purse, on a reserve and in a melt adds one
    415  * entry each.
    416  *
    417  * @param pg the database context
    418  * @return 0 on success
    419  */
    420 static int
    421 check_other_spends (struct TALER_EXCHANGEDB_PostgresContext *pg)
    422 {
    423   struct TALER_EXCHANGEDB_TransactionList *tl = NULL;
    424   struct TDB_Purse purse;
    425   struct TALER_CoinSpendSignatureP coin_sig;
    426   struct TALER_ReserveSignatureP reserve_sig;
    427   struct TALER_Amount one = TDB_amount ("1");
    428   struct TALER_Amount zero = TDB_amount ("0");
    429   unsigned int types;
    430   uint64_t etag = 0;
    431   bool balance_ok;
    432   bool too_late;
    433   bool conflict;
    434   bool insufficient_funds;
    435 
    436   /* into a purse */
    437   TDB_purse (pg,
    438              40,
    439              "1",
    440              GNUNET_TIME_relative_to_timestamp (GNUNET_TIME_UNIT_HOURS),
    441              &purse);
    442   TDB_FILL (coin_sig,
    443             41);
    444   FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    445           TALER_EXCHANGEDB_do_purse_deposit (pg,
    446                                              &purse.purse_pub,
    447                                              &coin.coin_pub,
    448                                              &one,
    449                                              &coin_sig,
    450                                              &one,
    451                                              &balance_ok,
    452                                              &too_late,
    453                                              &conflict));
    454   FAILIF (! balance_ok);
    455   FAILIF (conflict);
    456 
    457   /* to keep a reserve open */
    458   TDB_FILL (coin_sig,
    459             42);
    460   TDB_FILL (reserve_sig,
    461             42);
    462   FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    463           TALER_EXCHANGEDB_insert_reserve_open_deposit (pg,
    464                                                         &coin,
    465                                                         &coin_sig,
    466                                                         known_coin_id,
    467                                                         &one,
    468                                                         &reserve_sig,
    469                                                         &reserve_pub,
    470                                                         &insufficient_funds));
    471   FAILIF (insufficient_funds);
    472 
    473   /* and into a melt */
    474   {
    475     struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS rf;
    476     struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get ();
    477     struct TALER_Amount coin_balance;
    478     bool found;
    479     bool zombie_required = false;
    480     bool nonce_reuse;
    481     bool melt_balance_ok;
    482     uint32_t noreveal_index;
    483     struct TALER_TransferPublicKeyP transfer_pubs[NUM_FRESH_COINS];
    484     enum GNUNET_DB_QueryStatus qs;
    485 
    486     memset (&rf,
    487             0,
    488             sizeof (rf));
    489     rf.coin.coin_pub = coin.coin_pub;
    490     rf.coin.denom_pub_hash = coin.denom_pub_hash;
    491     rf.coin.no_age_commitment = coin.no_age_commitment;
    492     TDB_FILL (rf.coin_sig,
    493               43);
    494     TDB_FILL (rf.rc,
    495               43);
    496     TDB_FILL (rf.refresh_seed,
    497               43);
    498     TDB_FILL (rf.planchets_h,
    499               43);
    500     TDB_FILL (rf.selected_h,
    501               44);
    502     rf.amount_with_fee = one;
    503     TDB_denom (pg,
    504                11,
    505                "0.1",
    506                "0.01",
    507                &fresh_denom);
    508     rf.num_coins = NUM_FRESH_COINS;
    509     rf.denom_serials = GNUNET_new_array (rf.num_coins,
    510                                          uint64_t);
    511     /* Deliberately neither sorted nor distinct. */
    512     rf.denom_serials[0] = fresh_denom.serial;
    513     rf.denom_serials[1] = denom.serial;
    514     rf.denom_serials[2] = fresh_denom.serial;
    515     rf.denom_sigs = GNUNET_new_array (
    516       rf.num_coins,
    517       struct TALER_BlindedDenominationSignature);
    518     for (unsigned int i = 0; i < rf.num_coins; i++)
    519       TDB_blinded_denom_sig (43 + i,
    520                              &rf.denom_sigs[i]);
    521     for (unsigned int i = 0; i < rf.num_coins; i++)
    522       TDB_FILL (transfer_pubs[i],
    523                 50 + i);
    524     rf.transfer_pubs = transfer_pubs;
    525     rf.noreveal_index = 1;
    526     rf.rc_version = TALER_REFRESH_COMMITMENT_VERSION_CURRENT;
    527     rf.no_blinding_seed = true;
    528     qs = TALER_EXCHANGEDB_do_refresh (pg,
    529                                       &rf,
    530                                       &now,
    531                                       &found,
    532                                       &noreveal_index,
    533                                       &zombie_required,
    534                                       &nonce_reuse,
    535                                       &melt_balance_ok,
    536                                       &coin_balance);
    537     for (unsigned int i = 0; i < rf.num_coins; i++)
    538       TALER_blinded_denom_sig_free (&rf.denom_sigs[i]);
    539     GNUNET_free (rf.denom_sigs);
    540     GNUNET_free (rf.denom_serials);
    541     FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs);
    542     FAILIF (! melt_balance_ok);
    543 
    544     /* The melt entry is the same before and after the reveal. */
    545     for (unsigned int revealed = 0; revealed < 2; revealed++)
    546     {
    547       bool found_melt = false;
    548 
    549       if (revealed)
    550         FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    551                 TALER_EXCHANGEDB_update_to_refresh_revealed (pg,
    552                                                              &rf.rc));
    553       FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    554               history (pg,
    555                        0,
    556                        0,
    557                        &etag,
    558                        &tl));
    559       for (const struct TALER_EXCHANGEDB_TransactionList *pos = tl;
    560            NULL != pos;
    561            pos = pos->next)
    562       {
    563         const struct TALER_EXCHANGEDB_MeltListEntry *melt;
    564 
    565         if (TALER_EXCHANGEDB_TT_MELT != pos->type)
    566           continue;
    567         found_melt = true;
    568         melt = pos->details.melt;
    569         FAILIF_C (NUM_FRESH_COINS != melt->num_coins,
    570                   TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    571         FAILIF_C (0 != GNUNET_memcmp (&melt->denom_pub_hashes[0],
    572                                       &fresh_denom.h_denom_pub),
    573                   TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    574         FAILIF_C (0 != GNUNET_memcmp (&melt->denom_pub_hashes[1],
    575                                       &denom.h_denom_pub),
    576                   TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    577         FAILIF_C (0 != GNUNET_memcmp (&melt->denom_pub_hashes[2],
    578                                       &fresh_denom.h_denom_pub),
    579                   TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    580         FAILIF_C (noreveal_index != melt->noreveal_index,
    581                   TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    582         FAILIF_C ( (NULL == melt->transfer_pubs) ||
    583                    (0 != memcmp (melt->transfer_pubs,
    584                                  transfer_pubs,
    585                                  sizeof (transfer_pubs))),
    586                    TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    587       }
    588       TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    589       tl = NULL;
    590       FAILIF (! found_melt);
    591     }
    592   }
    593 
    594   FAILIF (5 != TDB_count (pg,
    595                           "FROM coin_history"));
    596   FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    597           history (pg,
    598                    0,
    599                    0,
    600                    &etag,
    601                    &tl));
    602   FAILIF_C (5 != summarize (tl,
    603                             &types),
    604             TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    605   FAILIF_C (0 == (types & (1U << TALER_EXCHANGEDB_TT_PURSE_DEPOSIT)),
    606             TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    607   FAILIF_C (0 == (types & (1U << TALER_EXCHANGEDB_TT_RESERVE_OPEN)),
    608             TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    609   FAILIF_C (0 == (types & (1U << TALER_EXCHANGEDB_TT_MELT)),
    610             TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    611 
    612   /* the ETag is the highest coin history row of the list */
    613   {
    614     uint64_t max = 0;
    615 
    616     for (const struct TALER_EXCHANGEDB_TransactionList *pos = tl;
    617          NULL != pos;
    618          pos = pos->next)
    619       max = GNUNET_MAX (max,
    620                         pos->coin_history_id);
    621     FAILIF_C (etag != max,
    622               TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    623   }
    624 
    625   /* the spends add up to what the coin no longer has */
    626   {
    627     struct TALER_Amount total;
    628     struct TALER_Amount expect = TDB_amount ("3.5");
    629 
    630     FAILIF_C (GNUNET_OK !=
    631               TALER_EXCHANGEDB_calculate_transaction_list_totals (tl,
    632                                                                   &zero,
    633                                                                   &total),
    634               TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    635     FAILIF_C (0 != TALER_amount_cmp (&total,
    636                                      &expect),
    637               TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    638   }
    639   TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    640   return 0;
    641 }
    642 
    643 
    644 /**
    645  * Recouping the coin adds the last entry and empties it.
    646  *
    647  * @param pg the database context
    648  * @return 0 on success
    649  */
    650 static int
    651 check_recoup (struct TALER_EXCHANGEDB_PostgresContext *pg)
    652 {
    653   struct TALER_EXCHANGEDB_TransactionList *tl = NULL;
    654   struct TALER_CoinSpendSignatureP coin_sig;
    655   union GNUNET_CRYPTO_BlindingSecretP coin_bks;
    656   struct GNUNET_TIME_Timestamp recoup_timestamp;
    657   struct TALER_Amount recoup_amount;
    658   struct TALER_Amount balance;
    659   struct TALER_Amount zero = TDB_amount ("0");
    660   struct TALER_DenominationHashP h_denom_pub;
    661   unsigned int types;
    662   uint64_t etag = 0;
    663   bool recoup_ok;
    664   bool internal_failure;
    665 
    666   TDB_FILL (coin_sig,
    667             50);
    668   TDB_FILL (coin_bks,
    669             50);
    670   /* in/out: the caller picks the time, the callee only overwrites it if
    671      the coin was recouped before */
    672   recoup_timestamp = GNUNET_TIME_timestamp_get ();
    673   FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    674           TALER_EXCHANGEDB_do_recoup (pg,
    675                                       &reserve_pub,
    676                                       withdraw_id,
    677                                       3,
    678                                       &coin_bks,
    679                                       &coin.coin_pub,
    680                                       known_coin_id,
    681                                       &coin_sig,
    682                                       &recoup_timestamp,
    683                                       &recoup_amount,
    684                                       &recoup_ok,
    685                                       &internal_failure));
    686   FAILIF (internal_failure);
    687   FAILIF (! recoup_ok);
    688   FAILIF (6 != TDB_count (pg,
    689                           "FROM coin_history"));
    690 
    691   FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    692           TALER_EXCHANGEDB_get_coin_transactions (pg,
    693                                                   true,
    694                                                   &coin.coin_pub,
    695                                                   0,
    696                                                   0,
    697                                                   &etag,
    698                                                   &balance,
    699                                                   &h_denom_pub,
    700                                                   &tl));
    701   FAILIF_C (6 != summarize (tl,
    702                             &types),
    703             TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    704   FAILIF_C (0 == (types & (1U << TALER_EXCHANGEDB_TT_RECOUP_WITHDRAW)),
    705             TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    706   /* the entry names the withdraw operation (by its commitment, seeded
    707      like the withdraw in check_fresh_coin()) and the coin's position */
    708   {
    709     const struct TALER_EXCHANGEDB_RecoupListEntry *recoup = NULL;
    710     struct TALER_HashBlindedPlanchetsP planchets_h;
    711 
    712     TDB_FILL (planchets_h,
    713               11);
    714     for (const struct TALER_EXCHANGEDB_TransactionList *pos = tl;
    715          NULL != pos;
    716          pos = pos->next)
    717       if (TALER_EXCHANGEDB_TT_RECOUP_WITHDRAW == pos->type)
    718         recoup = pos->details.recoup;
    719     FAILIF_C (NULL == recoup,
    720               TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    721     FAILIF_C (3 != recoup->coin_index,
    722               TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    723     FAILIF_C (0 != GNUNET_memcmp (&recoup->planchets_h,
    724                                   &planchets_h),
    725               TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    726     FAILIF_C (0 != GNUNET_memcmp (&recoup->reserve_pub,
    727                                   &reserve_pub),
    728               TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    729     FAILIF_C (0 != GNUNET_memcmp (&recoup->coin_sig,
    730                                   &coin_sig),
    731               TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    732     FAILIF_C (0 != GNUNET_memcmp (&recoup->coin_blind,
    733                                   &coin_bks),
    734               TALER_EXCHANGEDB_free_coin_transaction_list (tl));
    735   }
    736   TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    737   FAILIF (0 != TALER_amount_cmp (&balance,
    738                                  &zero));
    739   return 0;
    740 }
    741 
    742 
    743 /**
    744  * With begin_transaction false the caller's transaction is used and left
    745  * open.
    746  *
    747  * @param pg the database context
    748  * @return 0 on success
    749  */
    750 static int
    751 check_in_transaction (struct TALER_EXCHANGEDB_PostgresContext *pg)
    752 {
    753   struct TALER_EXCHANGEDB_TransactionList *tl = NULL;
    754   struct TALER_Amount balance;
    755   struct TALER_DenominationHashP h_denom_pub;
    756   unsigned int types;
    757   uint64_t etag = 0;
    758 
    759   FAILIF (GNUNET_OK !=
    760           TALER_EXCHANGEDB_start (pg,
    761                                   "coin-history"));
    762   FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    763             TALER_EXCHANGEDB_get_coin_transactions (pg,
    764                                                     false,
    765                                                     &coin.coin_pub,
    766                                                     0,
    767                                                     0,
    768                                                     &etag,
    769                                                     &balance,
    770                                                     &h_denom_pub,
    771                                                     &tl),
    772             TALER_EXCHANGEDB_rollback (pg));
    773   FAILIF_C (6 != summarize (tl,
    774                             &types),
    775             TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    776             TALER_EXCHANGEDB_rollback (pg));
    777   TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    778   FAILIF_C (NULL == pg->transaction_name,
    779             TALER_EXCHANGEDB_rollback (pg));
    780   TALER_EXCHANGEDB_rollback (pg);
    781   return 0;
    782 }
    783 
    784 
    785 /**
    786  * Recouping a coin that was refreshed from @e coin credits @e coin and adds
    787  * a RECOUP-REFRESH-RECEIVER entry to its history that names the refresh
    788  * operation (by its commitment, seeded like the melt in
    789  * check_other_spends()), the recouped coin and its position in the batch.
    790  * The recouped coin's own history gets the matching RECOUP-REFRESH entry.
    791  *
    792  * @param pg the database context
    793  * @return 0 on success
    794  */
    795 static int
    796 check_recoup_refresh (struct TALER_EXCHANGEDB_PostgresContext *pg)
    797 {
    798   struct TALER_EXCHANGEDB_TransactionList *tl = NULL;
    799   const struct TALER_EXCHANGEDB_RecoupRefreshListEntry *rr = NULL;
    800   struct TALER_CoinPublicInfo fresh_coin;
    801   struct TALER_RefreshCommitmentP rc;
    802   struct TALER_CoinSpendSignatureP coin_sig;
    803   union GNUNET_CRYPTO_BlindingSecretP coin_bks;
    804   struct GNUNET_TIME_Timestamp recoup_timestamp;
    805   struct TALER_Amount recoup_amount;
    806   struct TALER_Amount balance;
    807   struct TALER_Amount zero = TDB_amount ("0");
    808   struct TALER_Amount credited = TDB_amount ("0.1");
    809   struct TALER_DenominationHashP h_denom_pub;
    810   uint64_t fresh_known_coin_id;
    811   uint64_t refresh_id;
    812   uint64_t etag = 0;
    813   unsigned int types;
    814   bool recoup_ok;
    815   bool internal_failure;
    816 
    817   TDB_FILL (rc,
    818             43);
    819   /* row of the melt made in check_other_spends() */
    820   {
    821     struct GNUNET_PQ_QueryParam params[] = {
    822       GNUNET_PQ_query_param_auto_from_type (&rc),
    823       GNUNET_PQ_query_param_end
    824     };
    825     struct GNUNET_PQ_ResultSpec rs[] = {
    826       GNUNET_PQ_result_spec_uint64 ("refresh_id",
    827                                     &refresh_id),
    828       GNUNET_PQ_result_spec_end
    829     };
    830 
    831     FAILIF (GNUNET_OK !=
    832             GNUNET_PQ_prepare_anon (pg->conn,
    833                                     "SELECT refresh_id"
    834                                     " FROM refresh"
    835                                     " WHERE rc=$1;"));
    836     FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    837             GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
    838                                                       "",
    839                                                       params,
    840                                                       rs));
    841   }
    842   TDB_coin (pg,
    843             &fresh_denom,
    844             60,
    845             &fresh_coin,
    846             &fresh_known_coin_id);
    847   TDB_FILL (coin_sig,
    848             60);
    849   TDB_FILL (coin_bks,
    850             60);
    851   recoup_timestamp = GNUNET_TIME_timestamp_get ();
    852   FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    853             TALER_EXCHANGEDB_do_recoup_refresh (pg,
    854                                                 &coin.coin_pub,
    855                                                 refresh_id,
    856                                                 2,
    857                                                 &coin_bks,
    858                                                 &fresh_coin.coin_pub,
    859                                                 fresh_known_coin_id,
    860                                                 &coin_sig,
    861                                                 &recoup_timestamp,
    862                                                 &recoup_amount,
    863                                                 &recoup_ok,
    864                                                 &internal_failure),
    865             TDB_coin_free (&fresh_coin));
    866   FAILIF_C (internal_failure || ! recoup_ok,
    867             TDB_coin_free (&fresh_coin));
    868   FAILIF_C (0 != TALER_amount_cmp (&recoup_amount,
    869                                    &credited),
    870             TDB_coin_free (&fresh_coin));
    871 
    872   /* the old coin is credited and sees the receiver entry */
    873   FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    874             TALER_EXCHANGEDB_get_coin_transactions (pg,
    875                                                     true,
    876                                                     &coin.coin_pub,
    877                                                     0,
    878                                                     0,
    879                                                     &etag,
    880                                                     &balance,
    881                                                     &h_denom_pub,
    882                                                     &tl),
    883             TDB_coin_free (&fresh_coin));
    884   FAILIF_C (7 != summarize (tl,
    885                             &types),
    886             TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    887             TDB_coin_free (&fresh_coin));
    888   for (const struct TALER_EXCHANGEDB_TransactionList *pos = tl;
    889        NULL != pos;
    890        pos = pos->next)
    891     if (TALER_EXCHANGEDB_TT_RECOUP_REFRESH_RECEIVER == pos->type)
    892       rr = pos->details.old_coin_recoup;
    893   FAILIF_C (NULL == rr,
    894             TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    895             TDB_coin_free (&fresh_coin));
    896   FAILIF_C (2 != rr->coin_index,
    897             TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    898             TDB_coin_free (&fresh_coin));
    899   FAILIF_C (0 != GNUNET_memcmp (&rr->rc,
    900                                 &rc),
    901             TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    902             TDB_coin_free (&fresh_coin));
    903   FAILIF_C (0 != GNUNET_memcmp (&rr->coin.coin_pub,
    904                                 &fresh_coin.coin_pub),
    905             TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    906             TDB_coin_free (&fresh_coin));
    907   FAILIF_C (0 != GNUNET_memcmp (&rr->old_coin_pub,
    908                                 &coin.coin_pub),
    909             TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    910             TDB_coin_free (&fresh_coin));
    911   FAILIF_C (0 != TALER_amount_cmp (&rr->value,
    912                                    &credited),
    913             TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    914             TDB_coin_free (&fresh_coin));
    915   FAILIF_C (0 != TALER_amount_cmp (&balance,
    916                                    &credited),
    917             TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    918             TDB_coin_free (&fresh_coin));
    919   TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    920   tl = NULL;
    921 
    922   /* the recouped coin is emptied and sees the debit entry */
    923   etag = 0;
    924   FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    925             TALER_EXCHANGEDB_get_coin_transactions (pg,
    926                                                     true,
    927                                                     &fresh_coin.coin_pub,
    928                                                     0,
    929                                                     0,
    930                                                     &etag,
    931                                                     &balance,
    932                                                     &h_denom_pub,
    933                                                     &tl),
    934             TDB_coin_free (&fresh_coin));
    935   FAILIF_C (1 != summarize (tl,
    936                             &types),
    937             TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    938             TDB_coin_free (&fresh_coin));
    939   FAILIF_C (TALER_EXCHANGEDB_TT_RECOUP_REFRESH != tl->type,
    940             TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    941             TDB_coin_free (&fresh_coin));
    942   rr = tl->details.recoup_refresh;
    943   FAILIF_C (2 != rr->coin_index,
    944             TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    945             TDB_coin_free (&fresh_coin));
    946   FAILIF_C (0 != GNUNET_memcmp (&rr->rc,
    947                                 &rc),
    948             TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    949             TDB_coin_free (&fresh_coin));
    950   FAILIF_C (0 != GNUNET_memcmp (&rr->old_coin_pub,
    951                                 &coin.coin_pub),
    952             TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    953             TDB_coin_free (&fresh_coin));
    954   FAILIF_C (0 != GNUNET_memcmp (&rr->coin_sig,
    955                                 &coin_sig),
    956             TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    957             TDB_coin_free (&fresh_coin));
    958   FAILIF_C (0 != TALER_amount_cmp (&balance,
    959                                    &zero),
    960             TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    961             TDB_coin_free (&fresh_coin));
    962   TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    963   TDB_coin_free (&fresh_coin);
    964   return 0;
    965 }
    966 
    967 
    968 /**
    969  * The checks to run, in order.
    970  */
    971 static const struct TDB_Test tests[] = {
    972   { "coin-history-unknown-coin",
    973     &check_unknown_coin },
    974   { "coin-history-fresh-coin",
    975     &check_fresh_coin },
    976   { "coin-history-deposit-and-refund",
    977     &check_deposit_and_refund },
    978   { "coin-history-etag",
    979     &check_etag },
    980   { "coin-history-offset",
    981     &check_offset },
    982   { "coin-history-other-spends",
    983     &check_other_spends },
    984   { "coin-history-recoup",
    985     &check_recoup },
    986   { "coin-history-in-transaction",
    987     &check_in_transaction },
    988   { "coin-history-recoup-refresh",
    989     &check_recoup_refresh },
    990   { NULL, NULL }
    991 };
    992 
    993 
    994 int
    995 main (int argc,
    996       char *const *argv)
    997 {
    998   int ret;
    999 
   1000   ret = TDB_main (argc,
   1001                   argv,
   1002                   "test-coin-history",
   1003                   "Tests for the exchangedb `coin_history' table",
   1004                   tests);
   1005   TDB_coin_free (&coin);
   1006   TDB_denom_free (&denom);
   1007   TDB_denom_free (&fresh_denom);
   1008   TDB_account_free (&account);
   1009   return ret;
   1010 }
   1011 
   1012 
   1013 /* end of test_coin_history.c */