exchange

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

test_refresh.c (30838B)


      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_refresh.c
     18  * @brief tests for the exchangedb functions whose primary table is
     19  *        `refresh`
     20  * @author Christian Grothoff
     21  *
     22  * Covers #TALER_EXCHANGEDB_do_refresh(), #TALER_EXCHANGEDB_get_refresh(),
     23  * #TALER_EXCHANGEDB_update_to_refresh_revealed(),
     24  * #TALER_EXCHANGEDB_iterate_refreshes_above_serial_id().
     25  *
     26  * `refresh` references `known_coins`, which TDB_coin() creates.  The
     27  * do_refresh() answers checked here are: unknown coin, insufficient coin
     28  * balance, the zombie requirement, blinding-seed reuse and an idempotent
     29  * replay.
     30  */
     31 #include "test_common.h"
     32 #include "exchange-database/do_refresh.h"
     33 #include "exchange-database/get_known_coin.h"
     34 #include "exchange-database/get_refresh.h"
     35 #include "exchange-database/iterate_refreshes_above_serial_id.h"
     36 #include "exchange-database/insert_records_by_table.h"
     37 #include "exchange-database/iterate_records_by_table.h"
     38 #include "exchange-database/update_to_refresh_revealed.h"
     39 
     40 
     41 /**
     42  * Denomination the checks melt.
     43  */
     44 static struct TDB_Denom denom;
     45 
     46 
     47 /**
     48  * Fill in a refresh (melt) request for one fresh coin.
     49  *
     50  * @param seed seed for the commitment, signatures and seeds
     51  * @param amount amount to melt, e.g. "1"
     52  * @param coin coin being melted
     53  * @param with_seed true to pass a blinding seed and a CS R value
     54  * @param[out] rf set to the request; release with free_refresh()
     55  */
     56 static void
     57 make_refresh (uint32_t seed,
     58               const char *amount,
     59               const struct TALER_CoinPublicInfo *coin,
     60               bool with_seed,
     61               struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS *rf)
     62 {
     63   memset (rf,
     64           0,
     65           sizeof (*rf));
     66   rf->coin.coin_pub = coin->coin_pub;
     67   rf->coin.denom_pub_hash = coin->denom_pub_hash;
     68   rf->coin.no_age_commitment = coin->no_age_commitment;
     69   TDB_fill (&rf->coin_sig,
     70             sizeof (rf->coin_sig),
     71             seed);
     72   TDB_fill (&rf->rc,
     73             sizeof (rf->rc),
     74             seed);
     75   TDB_fill (&rf->refresh_seed,
     76             sizeof (rf->refresh_seed),
     77             seed);
     78   TDB_fill (&rf->planchets_h,
     79             sizeof (rf->planchets_h),
     80             seed);
     81   TDB_fill (&rf->selected_h,
     82             sizeof (rf->selected_h),
     83             seed + 1);
     84   rf->amount_with_fee = TDB_amount (amount);
     85   rf->num_coins = 1;
     86   rf->denom_serials = GNUNET_new (uint64_t);
     87   rf->denom_serials[0] = denom.serial;
     88   rf->denom_sigs = GNUNET_new (struct TALER_BlindedDenominationSignature);
     89   TDB_blinded_denom_sig (seed,
     90                          &rf->denom_sigs[0]);
     91   rf->noreveal_index = 1;
     92   /* v27 refresh: the client provides no transfer public keys */
     93   rf->rc_version = TALER_REFRESH_COMMITMENT_VERSION_LEGACY_V27;
     94   rf->no_blinding_seed = ! with_seed;
     95   if (with_seed)
     96   {
     97     TDB_fill (&rf->blinding_seed,
     98               sizeof (rf->blinding_seed),
     99               seed);
    100     rf->num_cs_r_values = 1;
    101     rf->cs_r_values = GNUNET_new (struct GNUNET_CRYPTO_CSPublicRPairP);
    102     TDB_fill (rf->cs_r_values,
    103               sizeof (*rf->cs_r_values),
    104               seed);
    105     rf->cs_r_choices = 0;
    106   }
    107 }
    108 
    109 
    110 /**
    111  * Release what make_refresh() allocated.
    112  *
    113  * @param[in,out] rf request to clean up
    114  */
    115 static void
    116 free_refresh (struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS *rf)
    117 {
    118   for (size_t i = 0; i<rf->num_coins; i++)
    119     TALER_blinded_denom_sig_free (&rf->denom_sigs[i]);
    120   GNUNET_free (rf->denom_sigs);
    121   GNUNET_free (rf->denom_serials);
    122   GNUNET_free (rf->cs_r_values);
    123   GNUNET_free (rf->transfer_pubs);
    124   GNUNET_free (rf->denom_pub_hashes);
    125 }
    126 
    127 
    128 /**
    129  * Outcome flags of a refresh request.
    130  */
    131 struct RefreshStatus
    132 {
    133   /**
    134    * Was there already a row for this commitment?
    135    */
    136   bool found;
    137 
    138   /**
    139    * Did the melt have to be of a zombie coin?
    140    */
    141   bool zombie_required;
    142 
    143   /**
    144    * Was the blinding seed used before?
    145    */
    146   bool nonce_reuse;
    147 
    148   /**
    149    * Was the coin balance sufficient?
    150    */
    151   bool balance_ok;
    152 
    153   /**
    154    * Balance the coin had.
    155    */
    156   struct TALER_Amount coin_balance;
    157 
    158   /**
    159    * Index the exchange chose not to reveal.
    160    */
    161   uint32_t noreveal_index;
    162 };
    163 
    164 
    165 /**
    166  * Perform a refresh request.
    167  *
    168  * @param pg the database context
    169  * @param rf the request
    170  * @param zombie_required whether the coin must be a zombie
    171  * @param[out] st set to the outcome
    172  * @return transaction status
    173  */
    174 static enum GNUNET_DB_QueryStatus
    175 run_refresh (struct TALER_EXCHANGEDB_PostgresContext *pg,
    176              struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS *rf,
    177              bool zombie_required,
    178              struct RefreshStatus *st)
    179 {
    180   struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get ();
    181 
    182   memset (st,
    183           0,
    184           sizeof (*st));
    185   st->zombie_required = zombie_required;
    186   return TALER_EXCHANGEDB_do_refresh (pg,
    187                                       rf,
    188                                       &now,
    189                                       &st->found,
    190                                       &st->noreveal_index,
    191                                       &st->zombie_required,
    192                                       &st->nonce_reuse,
    193                                       &st->balance_ok,
    194                                       &st->coin_balance);
    195 }
    196 
    197 
    198 /**
    199  * Melting a coin that is not known does nothing.
    200  *
    201  * @param pg the database context
    202  * @return 0 on success
    203  */
    204 static int
    205 check_unknown_coin (struct TALER_EXCHANGEDB_PostgresContext *pg)
    206 {
    207   struct TALER_CoinPublicInfo coin;
    208   struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS rf;
    209   struct RefreshStatus st;
    210 
    211   TDB_denom (pg,
    212              10,
    213              "5",
    214              "0.1",
    215              &denom);
    216   memset (&coin,
    217           0,
    218           sizeof (coin));
    219   TDB_FILL (coin.coin_pub,
    220             1);
    221   coin.denom_pub_hash = denom.h_denom_pub;
    222   coin.no_age_commitment = true;
    223   make_refresh (1,
    224                 "1",
    225                 &coin,
    226                 false,
    227                 &rf);
    228   FAILIF_C (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
    229             run_refresh (pg,
    230                          &rf,
    231                          false,
    232                          &st),
    233             free_refresh (&rf));
    234   free_refresh (&rf);
    235   FAILIF (0 != TDB_count (pg,
    236                           "FROM refresh"));
    237   return 0;
    238 }
    239 
    240 
    241 /**
    242  * Melting more than the coin is worth does nothing.
    243  *
    244  * @param pg the database context
    245  * @return 0 on success
    246  */
    247 static int
    248 check_insufficient_balance (struct TALER_EXCHANGEDB_PostgresContext *pg)
    249 {
    250   struct TALER_CoinPublicInfo coin;
    251   struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS rf;
    252   struct RefreshStatus st;
    253   struct TALER_Amount expect = TDB_amount ("5");
    254 
    255   TDB_coin (pg,
    256             &denom,
    257             20,
    258             &coin,
    259             NULL);
    260   make_refresh (2,
    261                 "6",
    262                 &coin,
    263                 false,
    264                 &rf);
    265   FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    266             run_refresh (pg,
    267                          &rf,
    268                          false,
    269                          &st),
    270             free_refresh (&rf); TDB_coin_free (&coin));
    271   free_refresh (&rf);
    272   FAILIF_C (st.balance_ok,
    273             TDB_coin_free (&coin));
    274   FAILIF_C (0 != TALER_amount_cmp (&st.coin_balance,
    275                                    &expect),
    276             TDB_coin_free (&coin));
    277   /* the row was written before the balance was checked, so the caller has
    278      to roll back -- here the check simply notes that it is there */
    279   FAILIF_C (1 != TDB_count (pg,
    280                             "FROM refresh"),
    281             TDB_coin_free (&coin));
    282   FAILIF_C (GNUNET_OK !=
    283             TDB_exec (pg,
    284                       "DELETE FROM refresh;"),
    285             TDB_coin_free (&coin));
    286   TDB_coin_free (&coin);
    287   return 0;
    288 }
    289 
    290 
    291 /**
    292  * A melt within the coin's balance is recorded and debits the coin.
    293  *
    294  * @param pg the database context
    295  * @return 0 on success
    296  */
    297 static int
    298 check_refresh (struct TALER_EXCHANGEDB_PostgresContext *pg)
    299 {
    300   struct TALER_CoinPublicInfo coin;
    301   struct TALER_CoinPublicInfo got_coin;
    302   struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS rf;
    303   struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS got;
    304   struct RefreshStatus st;
    305   struct TALER_Amount expect_amount = TDB_amount ("1");
    306 
    307   TDB_coin (pg,
    308             &denom,
    309             20,
    310             &coin,
    311             NULL);
    312   make_refresh (3,
    313                 "1",
    314                 &coin,
    315                 true,
    316                 &rf);
    317   FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    318             run_refresh (pg,
    319                          &rf,
    320                          false,
    321                          &st),
    322             free_refresh (&rf); TDB_coin_free (&coin));
    323   FAILIF_C (! st.balance_ok,
    324             free_refresh (&rf); TDB_coin_free (&coin));
    325   FAILIF_C (st.found,
    326             free_refresh (&rf); TDB_coin_free (&coin));
    327   FAILIF_C (st.nonce_reuse,
    328             free_refresh (&rf); TDB_coin_free (&coin));
    329   FAILIF_C (1 != TDB_count (pg,
    330                             "FROM refresh"),
    331             free_refresh (&rf); TDB_coin_free (&coin));
    332 
    333   /* the melt is on file with everything it was created with */
    334   memset (&got,
    335           0,
    336           sizeof (got));
    337   FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    338             TALER_EXCHANGEDB_get_refresh (pg,
    339                                           &rf.rc,
    340                                           &got),
    341             free_refresh (&rf); TDB_coin_free (&coin));
    342   FAILIF_C (0 != TALER_amount_cmp (&got.amount_with_fee,
    343                                    &expect_amount),
    344             free_refresh (&got); free_refresh (&rf); TDB_coin_free (&coin));
    345   FAILIF_C (0 != GNUNET_memcmp (&got.coin.coin_pub,
    346                                 &coin.coin_pub),
    347             free_refresh (&got); free_refresh (&rf); TDB_coin_free (&coin));
    348   FAILIF_C (0 != GNUNET_memcmp (&got.coin_sig,
    349                                 &rf.coin_sig),
    350             free_refresh (&got); free_refresh (&rf); TDB_coin_free (&coin));
    351   FAILIF_C (0 != GNUNET_memcmp (&got.refresh_seed,
    352                                 &rf.refresh_seed),
    353             free_refresh (&got); free_refresh (&rf); TDB_coin_free (&coin));
    354   FAILIF_C (1 != got.num_coins,
    355             free_refresh (&got); free_refresh (&rf); TDB_coin_free (&coin));
    356   FAILIF_C (denom.serial != got.denom_serials[0],
    357             free_refresh (&got); free_refresh (&rf); TDB_coin_free (&coin));
    358   FAILIF_C (rf.noreveal_index != got.noreveal_index,
    359             free_refresh (&got); free_refresh (&rf); TDB_coin_free (&coin));
    360   FAILIF_C (got.revealed,
    361             free_refresh (&got); free_refresh (&rf); TDB_coin_free (&coin));
    362   FAILIF_C (got.no_blinding_seed,
    363             free_refresh (&got); free_refresh (&rf); TDB_coin_free (&coin));
    364   FAILIF_C (TALER_REFRESH_COMMITMENT_VERSION_LEGACY_V27 != got.rc_version,
    365             free_refresh (&got); free_refresh (&rf); TDB_coin_free (&coin));
    366   free_refresh (&got);
    367 
    368   /* the coin was debited */
    369   FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    370             TALER_EXCHANGEDB_get_known_coin (pg,
    371                                              &coin.coin_pub,
    372                                              &got_coin),
    373             free_refresh (&rf); TDB_coin_free (&coin));
    374   TALER_denom_sig_free (&got_coin.denom_sig);
    375   {
    376     char *hex = TDB_hex (&coin.coin_pub,
    377                          sizeof (coin.coin_pub));
    378 
    379     FAILIF_C (1 != TDB_count (pg,
    380                               "FROM known_coins"
    381                               " WHERE coin_pub=decode('%s','hex')"
    382                               "   AND remaining=ROW(4,0)::taler_amount",
    383                               hex),
    384               GNUNET_free (hex);
    385               free_refresh (&rf); TDB_coin_free (&coin));
    386     GNUNET_free (hex);
    387   }
    388 
    389   /* a replay is idempotent and reports the index we chose */
    390   FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    391             run_refresh (pg,
    392                          &rf,
    393                          false,
    394                          &st),
    395             free_refresh (&rf); TDB_coin_free (&coin));
    396   FAILIF_C (! st.found,
    397             free_refresh (&rf); TDB_coin_free (&coin));
    398   FAILIF_C (rf.noreveal_index != st.noreveal_index,
    399             free_refresh (&rf); TDB_coin_free (&coin));
    400   FAILIF_C (1 != TDB_count (pg,
    401                             "FROM refresh"),
    402             free_refresh (&rf); TDB_coin_free (&coin));
    403   free_refresh (&rf);
    404   TDB_coin_free (&coin);
    405   return 0;
    406 }
    407 
    408 
    409 /**
    410  * Reusing a blinding seed is refused.
    411  *
    412  * @param pg the database context
    413  * @return 0 on success
    414  */
    415 static int
    416 check_nonce_reuse (struct TALER_EXCHANGEDB_PostgresContext *pg)
    417 {
    418   struct TALER_CoinPublicInfo coin;
    419   struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS rf;
    420   struct RefreshStatus st;
    421 
    422   TDB_coin (pg,
    423             &denom,
    424             20,
    425             &coin,
    426             NULL);
    427   /* a fresh commitment, but the blinding seed of check_refresh() */
    428   make_refresh (4,
    429                 "1",
    430                 &coin,
    431                 true,
    432                 &rf);
    433   TDB_fill (&rf.blinding_seed,
    434             sizeof (rf.blinding_seed),
    435             3);
    436   FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    437             run_refresh (pg,
    438                          &rf,
    439                          false,
    440                          &st),
    441             free_refresh (&rf); TDB_coin_free (&coin));
    442   FAILIF_C (! st.nonce_reuse,
    443             free_refresh (&rf); TDB_coin_free (&coin));
    444   FAILIF_C (st.found,
    445             free_refresh (&rf); TDB_coin_free (&coin));
    446   free_refresh (&rf);
    447   TDB_coin_free (&coin);
    448   FAILIF (1 != TDB_count (pg,
    449                           "FROM refresh"));
    450   return 0;
    451 }
    452 
    453 
    454 /**
    455  * A melt that insists on a zombie coin is refused when the coin never was
    456  * one.
    457  *
    458  * @param pg the database context
    459  * @return 0 on success
    460  */
    461 static int
    462 check_zombie_required (struct TALER_EXCHANGEDB_PostgresContext *pg)
    463 {
    464   struct TALER_CoinPublicInfo coin;
    465   struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS rf;
    466   struct RefreshStatus st;
    467 
    468   TDB_coin (pg,
    469             &denom,
    470             21,
    471             &coin,
    472             NULL);
    473   make_refresh (5,
    474                 "1",
    475                 &coin,
    476                 false,
    477                 &rf);
    478   FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    479             run_refresh (pg,
    480                          &rf,
    481                          true,
    482                          &st),
    483             free_refresh (&rf); TDB_coin_free (&coin));
    484   FAILIF_C (! st.zombie_required,
    485             free_refresh (&rf); TDB_coin_free (&coin));
    486   FAILIF_C (st.balance_ok,
    487             free_refresh (&rf); TDB_coin_free (&coin));
    488   free_refresh (&rf);
    489   TDB_coin_free (&coin);
    490   /* the row went in before the zombie check, so the caller must roll back */
    491   FAILIF (2 != TDB_count (pg,
    492                           "FROM refresh"));
    493   FAILIF (GNUNET_OK !=
    494           TDB_exec (pg,
    495                     "DELETE FROM refresh WHERE NOT revealed"
    496                     " AND rc <> (SELECT rc FROM refresh"
    497                     "             ORDER BY refresh_id ASC LIMIT 1);"));
    498   return 0;
    499 }
    500 
    501 
    502 /**
    503  * A melt can be marked as revealed, and marking one that does not exist
    504  * does nothing.
    505  *
    506  * @param pg the database context
    507  * @return 0 on success
    508  */
    509 static int
    510 check_revealed (struct TALER_EXCHANGEDB_PostgresContext *pg)
    511 {
    512   struct TALER_RefreshCommitmentP rc;
    513   struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS got;
    514 
    515   TDB_FILL (rc,
    516             99);
    517   FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
    518           TALER_EXCHANGEDB_update_to_refresh_revealed (pg,
    519                                                        &rc));
    520   FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
    521           TALER_EXCHANGEDB_get_refresh (pg,
    522                                         &rc,
    523                                         &got));
    524 
    525   TDB_FILL (rc,
    526             3);
    527   FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    528           TALER_EXCHANGEDB_update_to_refresh_revealed (pg,
    529                                                        &rc));
    530   memset (&got,
    531           0,
    532           sizeof (got));
    533   FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    534           TALER_EXCHANGEDB_get_refresh (pg,
    535                                         &rc,
    536                                         &got));
    537   FAILIF_C (! got.revealed,
    538             free_refresh (&got));
    539   free_refresh (&got);
    540   /* marking it again is still reported as a row touched */
    541   FAILIF (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    542           TALER_EXCHANGEDB_update_to_refresh_revealed (pg,
    543                                                        &rc));
    544   return 0;
    545 }
    546 
    547 
    548 /**
    549  * Closure for #refreshes_cb().
    550  */
    551 struct RefreshesContext
    552 {
    553   /**
    554    * How many melts did the callback see?
    555    */
    556   unsigned int total;
    557 
    558   /**
    559    * Stop after this many melts; 0 for no limit.
    560    */
    561   unsigned int stop_after;
    562 
    563   /**
    564    * Row of the last melt.
    565    */
    566   uint64_t rowid;
    567 
    568   /**
    569    * Amount of the last melt.
    570    */
    571   struct TALER_Amount amount;
    572 
    573   /**
    574    * Number of new denominations of the last melt.
    575    */
    576   size_t num_nds;
    577 };
    578 
    579 
    580 /**
    581  * Callback for #TALER_EXCHANGEDB_iterate_refreshes_above_serial_id().
    582  *
    583  * @param cls a `struct RefreshesContext *`
    584  * @param rowid row of the melt
    585  * @param old_denom_pub denomination of the melted coin
    586  * @param coin_pub the melted coin
    587  * @param coin_sig signature authorising the melt
    588  * @param h_age_commitment age commitment of the coin, NULL if none
    589  * @param amount_with_fee how much was melted
    590  * @param num_nds length of @a new_denom_serials
    591  * @param new_denom_serials denominations of the fresh coins
    592  * @param rc commitment of the melt
    593  * @return #GNUNET_OK to continue, #GNUNET_SYSERR to stop
    594  */
    595 static enum GNUNET_GenericReturnValue
    596 refreshes_cb (void *cls,
    597               uint64_t rowid,
    598               const struct TALER_DenominationPublicKey *old_denom_pub,
    599               const struct TALER_CoinSpendPublicKeyP *coin_pub,
    600               const struct TALER_CoinSpendSignatureP *coin_sig,
    601               const struct TALER_AgeCommitmentHashP *h_age_commitment,
    602               const struct TALER_Amount *amount_with_fee,
    603               size_t num_nds,
    604               uint64_t new_denom_serials[static num_nds],
    605               const struct TALER_RefreshCommitmentP *rc)
    606 {
    607   struct RefreshesContext *ctx = cls;
    608 
    609   (void) old_denom_pub;
    610   (void) coin_pub;
    611   (void) coin_sig;
    612   (void) h_age_commitment;
    613   (void) new_denom_serials;
    614   (void) rc;
    615   ctx->total++;
    616   ctx->rowid = rowid;
    617   ctx->amount = *amount_with_fee;
    618   ctx->num_nds = num_nds;
    619   if ( (0 != ctx->stop_after) &&
    620        (ctx->total >= ctx->stop_after) )
    621     return GNUNET_SYSERR;
    622   return GNUNET_OK;
    623 }
    624 
    625 
    626 /**
    627  * The auditor's view walks the melts by serial and stops when asked to.
    628  *
    629  * @param pg the database context
    630  * @return 0 on success
    631  */
    632 static int
    633 check_iterate (struct TALER_EXCHANGEDB_PostgresContext *pg)
    634 {
    635   struct TALER_CoinPublicInfo coin;
    636   struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS rf;
    637   struct RefreshStatus st;
    638   struct RefreshesContext ctx;
    639   uint64_t rows;
    640 
    641   /* one more melt, so that "stop after the first" is distinguishable
    642      from "saw everything" */
    643   TDB_coin (pg,
    644             &denom,
    645             42,
    646             &coin,
    647             NULL);
    648   make_refresh (42,
    649                 "1",
    650                 &coin,
    651                 false,
    652                 &rf);
    653   FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    654             run_refresh (pg,
    655                          &rf,
    656                          false,
    657                          &st),
    658             free_refresh (&rf); TDB_coin_free (&coin));
    659   free_refresh (&rf);
    660   TDB_coin_free (&coin);
    661   FAILIF (! st.balance_ok);
    662   rows = TDB_count (pg,
    663                     "FROM refresh");
    664   FAILIF (2 != rows);
    665   memset (&ctx,
    666           0,
    667           sizeof (ctx));
    668   FAILIF (0 >=
    669           TALER_EXCHANGEDB_iterate_refreshes_above_serial_id (
    670             pg,
    671             0,
    672             &refreshes_cb,
    673             &ctx));
    674   FAILIF (rows != ctx.total);
    675   /* every melt here asked for exactly one fresh coin */
    676   FAILIF (1 != ctx.num_nds);
    677 
    678   /* everything above the last row is nothing */
    679   memset (&ctx,
    680           0,
    681           sizeof (ctx));
    682   FAILIF (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
    683           TALER_EXCHANGEDB_iterate_refreshes_above_serial_id (
    684             pg,
    685             UINT32_MAX,
    686             &refreshes_cb,
    687             &ctx));
    688   FAILIF (0 != ctx.total);
    689 
    690   /* a callback that says stop is not called again */
    691   memset (&ctx,
    692           0,
    693           sizeof (ctx));
    694   ctx.stop_after = 1;
    695   FAILIF (0 >=
    696           TALER_EXCHANGEDB_iterate_refreshes_above_serial_id (
    697             pg,
    698             0,
    699             &refreshes_cb,
    700             &ctx));
    701   FAILIF (1 != ctx.total);
    702   return 0;
    703 }
    704 
    705 
    706 /**
    707  * vDOLDPLUS melts keep the version of their refresh commitment, which
    708  * must agree with the presence of the transfer public keys.  New rows
    709  * default to the current version.
    710  *
    711  * @param pg the database context
    712  * @return 0 on success
    713  */
    714 static int
    715 check_rc_version (struct TALER_EXCHANGEDB_PostgresContext *pg)
    716 {
    717   static const enum TALER_RefreshCommitmentVersion versions[] = {
    718     TALER_REFRESH_COMMITMENT_VERSION_LEGACY_VDOLDPLUS_BROKEN,
    719     TALER_REFRESH_COMMITMENT_VERSION_CURRENT
    720   };
    721 
    722   FAILIF (1 != TDB_count (pg,
    723                           "FROM information_schema.columns"
    724                           " WHERE table_name='refresh'"
    725                           "   AND column_name='rc_version'"
    726                           "   AND column_default='2'"));
    727   for (unsigned int i = 0; i < sizeof (versions) / sizeof (versions[0]); i++)
    728   {
    729     struct TALER_CoinPublicInfo coin;
    730     struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS rf;
    731     struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS got;
    732     struct RefreshStatus st;
    733     char *hex;
    734 
    735     TDB_coin (pg,
    736               &denom,
    737               50 + i,
    738               &coin,
    739               NULL);
    740     make_refresh (50 + i,
    741                   "1",
    742                   &coin,
    743                   false,
    744                   &rf);
    745     rf.rc_version = versions[i];
    746     rf.transfer_pubs = GNUNET_new_array (rf.num_coins,
    747                                          struct TALER_TransferPublicKeyP);
    748     TDB_fill (rf.transfer_pubs,
    749               sizeof (*rf.transfer_pubs) * rf.num_coins,
    750               50 + i);
    751     FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    752               run_refresh (pg,
    753                            &rf,
    754                            false,
    755                            &st),
    756               free_refresh (&rf); TDB_coin_free (&coin));
    757     FAILIF_C (! st.balance_ok,
    758               free_refresh (&rf); TDB_coin_free (&coin));
    759     memset (&got,
    760             0,
    761             sizeof (got));
    762     FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    763               TALER_EXCHANGEDB_get_refresh (pg,
    764                                             &rf.rc,
    765                                             &got),
    766               free_refresh (&rf); TDB_coin_free (&coin));
    767     FAILIF_C (versions[i] != got.rc_version,
    768               free_refresh (&got); free_refresh (&rf); TDB_coin_free (&coin));
    769     FAILIF_C ( (NULL == got.transfer_pubs) ||
    770                (0 != GNUNET_memcmp (&got.transfer_pubs[0],
    771                                     &rf.transfer_pubs[0])),
    772                free_refresh (&got); free_refresh (&rf); TDB_coin_free (&coin));
    773     free_refresh (&got);
    774 
    775     /* a row whose version contradicts its transfer public keys is refused */
    776     hex = TDB_hex (&rf.rc,
    777                    sizeof (rf.rc));
    778     FAILIF_C (GNUNET_OK !=
    779               TDB_exec (pg,
    780                         "UPDATE refresh SET rc_version=0"
    781                         " WHERE rc=decode('%s','hex');",
    782                         hex),
    783               GNUNET_free (hex); free_refresh (&rf); TDB_coin_free (&coin));
    784     GNUNET_free (hex);
    785     memset (&got,
    786             0,
    787             sizeof (got));
    788     FAILIF_C (GNUNET_DB_STATUS_HARD_ERROR !=
    789               TALER_EXCHANGEDB_get_refresh (pg,
    790                                             &rf.rc,
    791                                             &got),
    792               free_refresh (&rf); TDB_coin_free (&coin));
    793     free_refresh (&rf);
    794     TDB_coin_free (&coin);
    795   }
    796   return 0;
    797 }
    798 
    799 
    800 /**
    801  * Closure for #replication_cb().
    802  */
    803 struct ReplicationContext
    804 {
    805   /**
    806    * The melt we look for.
    807    */
    808   const struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS *rf;
    809 
    810   /**
    811    * How many records matched @e rf.
    812    */
    813   unsigned int seen;
    814 
    815   /**
    816    * Set if a matching record differed from @e rf.
    817    */
    818   bool mismatch;
    819 
    820   /**
    821    * Serial of the matching record.
    822    */
    823   uint64_t serial;
    824 
    825   /**
    826    * Execution date of the matching record.
    827    */
    828   struct GNUNET_TIME_Timestamp execution_date;
    829 };
    830 
    831 
    832 /**
    833  * Callback for #TALER_EXCHANGEDB_iterate_records_by_table().
    834  *
    835  * @param cls a `struct ReplicationContext *`
    836  * @param td the record
    837  * @return #GNUNET_OK to continue iterating
    838  */
    839 static int
    840 replication_cb (void *cls,
    841                 const struct TALER_EXCHANGEDB_TableData *td)
    842 {
    843   struct ReplicationContext *rc = cls;
    844   const struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS *rf = rc->rf;
    845 
    846   if (TALER_EXCHANGEDB_RT_REFRESH != td->table)
    847   {
    848     rc->mismatch = true;
    849     return GNUNET_OK;
    850   }
    851   if (0 != GNUNET_memcmp (&td->details.refresh.rc,
    852                           &rf->rc))
    853     return GNUNET_OK;
    854   rc->seen++;
    855   rc->serial = td->serial;
    856   rc->execution_date = td->details.refresh.execution_date;
    857   if ( (rf->num_coins != td->details.refresh.num_coins) ||
    858        (NULL == td->details.refresh.transfer_pubs) ||
    859        (0 != GNUNET_memcmp (&td->details.refresh.transfer_pubs[0],
    860                             &rf->transfer_pubs[0])) ||
    861        (! td->details.refresh.revealed) ||
    862        (rf->rc_version != td->details.refresh.rc_version) )
    863     rc->mismatch = true;
    864   return GNUNET_OK;
    865 }
    866 
    867 
    868 /**
    869  * The auditor replication copies a melt with its transfer public keys,
    870  * reveal status and commitment version.
    871  *
    872  * @param pg the database context
    873  * @return 0 on success
    874  */
    875 static int
    876 check_replication (struct TALER_EXCHANGEDB_PostgresContext *pg)
    877 {
    878   struct TALER_CoinPublicInfo coin;
    879   struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS rf;
    880   struct RefreshStatus st;
    881   struct ReplicationContext rc = {
    882     .rf = &rf
    883   };
    884   struct TALER_EXCHANGEDB_TableData td = {
    885     .table = TALER_EXCHANGEDB_RT_REFRESH
    886   };
    887   char *hex;
    888 
    889   TDB_coin (pg,
    890             &denom,
    891             60,
    892             &coin,
    893             NULL);
    894   make_refresh (60,
    895                 "1",
    896                 &coin,
    897                 false,
    898                 &rf);
    899   TDB_coin_free (&coin);
    900   rf.rc_version = TALER_REFRESH_COMMITMENT_VERSION_CURRENT;
    901   rf.transfer_pubs = GNUNET_new_array (rf.num_coins,
    902                                        struct TALER_TransferPublicKeyP);
    903   TDB_fill (rf.transfer_pubs,
    904             sizeof (*rf.transfer_pubs) * rf.num_coins,
    905             60);
    906   FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    907             run_refresh (pg,
    908                          &rf,
    909                          false,
    910                          &st),
    911             free_refresh (&rf));
    912   hex = TDB_hex (&rf.rc,
    913                  sizeof (rf.rc));
    914   FAILIF_C (GNUNET_OK !=
    915             TDB_exec (pg,
    916                       "UPDATE refresh SET revealed=TRUE"
    917                       " WHERE rc=decode('%s','hex');",
    918                       hex),
    919             GNUNET_free (hex); free_refresh (&rf));
    920   FAILIF_C (0 >=
    921             TALER_EXCHANGEDB_iterate_records_by_table (
    922               pg,
    923               TALER_EXCHANGEDB_RT_REFRESH,
    924               0,
    925               &replication_cb,
    926               &rc),
    927             GNUNET_free (hex); free_refresh (&rf));
    928   FAILIF_C ( (1 != rc.seen) || rc.mismatch,
    929              GNUNET_free (hex); free_refresh (&rf));
    930 
    931   /* drop the melt (and the coin history entry its insert trigger made)
    932      and replicate it back in */
    933   FAILIF_C (GNUNET_OK !=
    934             TDB_exec (pg,
    935                       "DELETE FROM refresh"
    936                       " WHERE rc=decode('%s','hex');",
    937                       hex),
    938             GNUNET_free (hex); free_refresh (&rf));
    939   FAILIF_C (GNUNET_OK !=
    940             TDB_exec (pg,
    941                       "DELETE FROM coin_history"
    942                       " WHERE table_name='refresh'"
    943                       "   AND serial_id=%llu;",
    944                       (unsigned long long) rc.serial),
    945             GNUNET_free (hex); free_refresh (&rf));
    946   GNUNET_free (hex);
    947   td.serial = rc.serial;
    948   td.details.refresh.rc = rf.rc;
    949   td.details.refresh.execution_date = rc.execution_date;
    950   td.details.refresh.amount_with_fee = rf.amount_with_fee;
    951   td.details.refresh.old_coin_pub = rf.coin.coin_pub;
    952   td.details.refresh.old_coin_sig = rf.coin_sig;
    953   td.details.refresh.refresh_seed = rf.refresh_seed;
    954   td.details.refresh.noreveal_index = rf.noreveal_index;
    955   td.details.refresh.planchets_h = rf.planchets_h;
    956   td.details.refresh.selected_h = rf.selected_h;
    957   td.details.refresh.no_blinding_seed = true;
    958   td.details.refresh.num_coins = rf.num_coins;
    959   td.details.refresh.denom_serials = rf.denom_serials;
    960   td.details.refresh.denom_sigs = rf.denom_sigs;
    961   td.details.refresh.transfer_pubs = rf.transfer_pubs;
    962   td.details.refresh.revealed = true;
    963   td.details.refresh.rc_version = rf.rc_version;
    964   FAILIF_C (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
    965             TALER_EXCHANGEDB_insert_records_by_table (pg,
    966                                                       &td),
    967             free_refresh (&rf));
    968   rc.seen = 0;
    969   FAILIF_C (0 >=
    970             TALER_EXCHANGEDB_iterate_records_by_table (
    971               pg,
    972               TALER_EXCHANGEDB_RT_REFRESH,
    973               0,
    974               &replication_cb,
    975               &rc),
    976             free_refresh (&rf));
    977   free_refresh (&rf);
    978   FAILIF ( (1 != rc.seen) || rc.mismatch);
    979   FAILIF (td.serial != rc.serial);
    980   return 0;
    981 }
    982 
    983 
    984 /**
    985  * The checks to run, in order.
    986  */
    987 static const struct TDB_Test tests[] = {
    988   { "refresh-unknown-coin",
    989     &check_unknown_coin },
    990   { "refresh-insufficient-balance",
    991     &check_insufficient_balance },
    992   { "refresh-refresh",
    993     &check_refresh },
    994   { "refresh-nonce-reuse",
    995     &check_nonce_reuse },
    996   { "refresh-zombie-required",
    997     &check_zombie_required },
    998   { "refresh-revealed",
    999     &check_revealed },
   1000   { "refresh-iterate",
   1001     &check_iterate },
   1002   { "refresh-rc-version",
   1003     &check_rc_version },
   1004   { "refresh-replication",
   1005     &check_replication },
   1006   { NULL, NULL }
   1007 };
   1008 
   1009 
   1010 int
   1011 main (int argc,
   1012       char *const *argv)
   1013 {
   1014   int ret;
   1015 
   1016   ret = TDB_main (argc,
   1017                   argv,
   1018                   "test-refresh",
   1019                   "Tests for the exchangedb `refresh' table",
   1020                   tests);
   1021   TDB_denom_free (&denom);
   1022   return ret;
   1023 }
   1024 
   1025 
   1026 /* end of test_refresh.c */