exchange

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

taler-helper-auditor-coins.c (121352B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2016-2025 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero 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 Affero Public License for more details.
     12 
     13   You should have received a copy of the GNU Affero Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file auditor/taler-helper-auditor-coins.c
     18  * @brief audits coins in an exchange database.
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include "auditordb_lib.h"
     23 #include "report-lib.h"
     24 #include "taler/taler_dbevents.h"
     25 #include "exchangedb_lib.h"
     26 #include "auditor-database/delete_denomination_balance.h"
     27 #include "auditor-database/event_listen.h"
     28 #include "auditor-database/get_auditor_progress.h"
     29 #include "auditor-database/get_balance.h"
     30 #include "auditor-database/get_denomination_balance.h"
     31 #include "auditor-database/get_exists_historic_denom_revenue.h"
     32 #include "auditor-database/insert_amount_arithmetic_inconsistency.h"
     33 #include "auditor-database/insert_auditor_progress.h"
     34 #include "auditor-database/insert_bad_sig_losses.h"
     35 #include "auditor-database/insert_balance.h"
     36 #include "auditor-database/insert_denomination_balance.h"
     37 #include "auditor-database/insert_denominations_without_sigs.h"
     38 #include "auditor-database/insert_emergency.h"
     39 #include "auditor-database/insert_emergency_by_count.h"
     40 #include "auditor-database/insert_historic_denom_revenue.h"
     41 #include "auditor-database/insert_row_inconsistency.h"
     42 #include "auditor-database/update_denomination_balance.h"
     43 #include "exchange-database/get_count_known_coins.h"
     44 #include "exchange-database/get_coin_transactions.h"
     45 #include "exchange-database/get_denomination_revocation.h"
     46 #include "exchange-database/get_known_coin.h"
     47 struct CoinContext;
     48 struct SweepContext;
     49 #define TALER_AUDITORDB_DENOMINATION_PENDING_RESULT_CLOSURE struct SweepContext
     50 #include "auditor-database/iterate_denomination_pending.h"
     51 #define TALER_EXCHANGEDB_DENOMINATION_RESULT_CLOSURE enum GNUNET_DB_QueryStatus
     52 #define TALER_EXCHANGEDB_DEPOSIT_RESULT_CLOSURE struct CoinContext
     53 #define TALER_EXCHANGEDB_PURSE_DECISION_RESULT_CLOSURE struct CoinContext
     54 #define TALER_EXCHANGEDB_PURSE_DEPOSIT_RESULT_CLOSURE struct CoinContext
     55 #define TALER_EXCHANGEDB_PURSE_REFUND_COIN_RESULT_CLOSURE struct CoinContext
     56 #define TALER_EXCHANGEDB_RECOUP_RESULT_CLOSURE struct CoinContext
     57 #define TALER_EXCHANGEDB_RECOUP_REFRESH_RESULT_CLOSURE struct CoinContext
     58 #define TALER_EXCHANGEDB_REFRESHES_RESULT_CLOSURE struct CoinContext
     59 #define TALER_EXCHANGEDB_REFUND_RESULT_CLOSURE struct CoinContext
     60 #define TALER_EXCHANGEDB_WITHDRAW_RESULT_CLOSURE struct CoinContext
     61 #include "exchange-database/iterate_denomination_info.h"
     62 #include "exchange-database/get_auditor_denom_sig.h"
     63 #include "exchange-database/iterate_coin_deposits_above_serial_id.h"
     64 #include "exchange-database/iterate_purse_decisions_above_serial_id.h"
     65 #include "exchange-database/iterate_purse_deposits_above_serial_id.h"
     66 #include "exchange-database/iterate_purse_deposits_by_purse.h"
     67 #include "exchange-database/iterate_recoups_above_serial_id.h"
     68 #include "exchange-database/iterate_recoup_refreshes_above_serial_id.h"
     69 #include "exchange-database/iterate_refreshes_above_serial_id.h"
     70 #include "exchange-database/iterate_refunds_above_serial_id.h"
     71 #include "exchange-database/iterate_withdrawals_above_serial_id.h"
     72 
     73 
     74 /**
     75  * How many coin histories do we keep in RAM at any given point in time?
     76  * Expect a few kB per coin history to be used.  All entries are dropped
     77  * at the end of each pass, but within a pass an entry is only evicted when
     78  * another coin hashes to the same slot: the number of slots is thus the
     79  * actual bound on the memory consumption of the cache.  Larger values
     80  * reduce database accesses.
     81  */
     82 #define MAX_COIN_HISTORIES (64 * 1024)
     83 
     84 /**
     85  * Use a 1 day grace period to deal with clocks not being perfectly synchronized.
     86  */
     87 #define DEPOSIT_GRACE_PERIOD GNUNET_TIME_UNIT_DAYS
     88 
     89 /**
     90  * How many pending denominations do we fetch per query when looking for
     91  * those that no transaction of this pass touched?
     92  */
     93 #define SWEEP_BATCH_SIZE 1024
     94 
     95 /**
     96  * Return value from main().
     97  */
     98 static int global_ret;
     99 
    100 /**
    101  * Run in test mode. Exit when idle instead of
    102  * going to sleep and waiting for more work.
    103  */
    104 static int test_mode;
    105 
    106 /**
    107  * Checkpointing our progress for coins.
    108  */
    109 static TALER_ARL_DEF_PP (coins_withdraw_serial_id);
    110 static TALER_ARL_DEF_PP (coins_deposit_serial_id);
    111 static TALER_ARL_DEF_PP (coins_melt_serial_id);
    112 static TALER_ARL_DEF_PP (coins_refund_serial_id);
    113 static TALER_ARL_DEF_PP (coins_recoup_serial_id);
    114 static TALER_ARL_DEF_PP (coins_recoup_refresh_serial_id);
    115 static TALER_ARL_DEF_PP (coins_purse_deposits_serial_id);
    116 static TALER_ARL_DEF_PP (coins_purse_refunds_serial_id);
    117 
    118 
    119 /**
    120  * Global coin balance sheet (for coins).
    121  */
    122 static TALER_ARL_DEF_AB (coin_balance_risk);
    123 static TALER_ARL_DEF_AB (total_escrowed);
    124 static TALER_ARL_DEF_AB (coin_irregular_loss);
    125 static TALER_ARL_DEF_AB (coin_melt_fee_revenue);
    126 static TALER_ARL_DEF_AB (coin_deposit_fee_revenue);
    127 static TALER_ARL_DEF_AB (coin_deposit_fee_loss);
    128 static TALER_ARL_DEF_AB (coin_refund_fee_revenue);
    129 static TALER_ARL_DEF_AB (total_recoup_loss);
    130 
    131 /**
    132  * Profits the exchange made by bad amount calculations.
    133  */
    134 static TALER_ARL_DEF_AB (coins_total_arithmetic_delta_plus);
    135 
    136 /**
    137  * Losses the exchange made by bad amount calculations.
    138  */
    139 static TALER_ARL_DEF_AB (coins_total_arithmetic_delta_minus);
    140 
    141 /**
    142  * Total amount reported in all calls to #report_emergency_by_count().
    143  */
    144 static TALER_ARL_DEF_AB (coins_reported_emergency_risk_by_count);
    145 
    146 /**
    147  * Total amount reported in all calls to #report_emergency_by_amount().
    148  */
    149 static TALER_ARL_DEF_AB (coins_reported_emergency_risk_by_amount);
    150 
    151 /**
    152  * Total amount in losses reported in all calls to #report_emergency_by_amount().
    153  */
    154 static TALER_ARL_DEF_AB (coins_emergencies_loss);
    155 
    156 /**
    157  * Total amount in losses reported in all calls to #report_emergency_by_count().
    158  */
    159 static TALER_ARL_DEF_AB (coins_emergencies_loss_by_count);
    160 
    161 
    162 /**
    163  * Coin and associated transaction history.
    164  */
    165 struct CoinHistory
    166 {
    167   /**
    168    * Public key of the coin.
    169    */
    170   struct TALER_CoinSpendPublicKeyP coin_pub;
    171 
    172   /**
    173    * The transaction list for the @a coin_pub.
    174    */
    175   struct TALER_EXCHANGEDB_TransactionList *tl;
    176 };
    177 
    178 /**
    179  * Array of transaction histories for coins.  The index is based on the coin's
    180  * public key.  Entries are replaced whenever we have a collision.
    181  */
    182 static struct CoinHistory coin_histories[MAX_COIN_HISTORIES];
    183 
    184 /**
    185  * Indices of the entries of #coin_histories that are in use.  Used to
    186  * flush the cache at the end of a pass without touching the entire
    187  * (huge) #coin_histories array.  An index is recorded only when its
    188  * entry goes from empty to used, so there are never more than
    189  * #MAX_COIN_HISTORIES of them.  Static rather than grown with
    190  * GNUNET_array_append(): that copies the whole array on every call
    191  * (quadratic in the number of coins of a pass) and aborts once the
    192  * array exceeds GNUNET_MAX_MALLOC_CHECKED (about 10.5M entries).
    193  */
    194 static unsigned int used_histories[MAX_COIN_HISTORIES];
    195 
    196 /**
    197  * Number of entries of #used_histories that are in use.
    198  */
    199 static unsigned int num_used_histories;
    200 
    201 /**
    202  * Should we run checks that only work for exchange-internal audits?
    203  */
    204 static int internal_checks;
    205 
    206 static struct GNUNET_DB_EventHandler *eh;
    207 
    208 /**
    209  * The auditors's configuration.
    210  */
    211 static const struct GNUNET_CONFIGURATION_Handle *cfg;
    212 
    213 
    214 /**
    215  * Return the index we should use for @a coin_pub in #coin_histories.
    216  *
    217  * @param coin_pub a coin's public key
    218  * @return index for caching this coin's history in #coin_histories
    219  */
    220 static unsigned int
    221 coin_history_index (const struct TALER_CoinSpendPublicKeyP *coin_pub)
    222 {
    223   uint32_t i;
    224 
    225   GNUNET_memcpy (&i,
    226                  coin_pub,
    227                  sizeof (i));
    228   return i % MAX_COIN_HISTORIES;
    229 }
    230 
    231 
    232 /**
    233  * Add a coin history to our in-memory cache.
    234  *
    235  * @param coin_pub public key of the coin to cache
    236  * @param tl history to store
    237  */
    238 static void
    239 cache_history (const struct TALER_CoinSpendPublicKeyP *coin_pub,
    240                struct TALER_EXCHANGEDB_TransactionList *tl)
    241 {
    242   unsigned int i = coin_history_index (coin_pub);
    243 
    244   if (NULL == tl)
    245     return; /* nothing to cache; get_cached_history() would miss anyway */
    246   if (NULL != coin_histories[i].tl)
    247   {
    248     TALER_EXCHANGEDB_free_coin_transaction_list (coin_histories[i].tl);
    249   }
    250   else
    251   {
    252     GNUNET_assert (num_used_histories < MAX_COIN_HISTORIES);
    253     used_histories[num_used_histories++] = i;
    254   }
    255   coin_histories[i].coin_pub = *coin_pub;
    256   coin_histories[i].tl = tl;
    257 }
    258 
    259 
    260 /**
    261  * Drop all cached coin histories.
    262  *
    263  * Must be called at the end of each pass: within a pass the history we
    264  * fetched is complete (we read it from a consistent snapshot), but the
    265  * exchange keeps adding transactions to coins we already saw.  Keeping
    266  * the entry across passes would make us skip the over-spending check
    267  * for every coin the process has already looked at.
    268  */
    269 static void
    270 flush_coin_histories (void)
    271 {
    272   for (unsigned int j = 0; j < num_used_histories; j++)
    273   {
    274     struct CoinHistory *ch = &coin_histories[used_histories[j]];
    275 
    276     TALER_EXCHANGEDB_free_coin_transaction_list (ch->tl);
    277     memset (ch,
    278             0,
    279             sizeof (*ch));
    280   }
    281   num_used_histories = 0;
    282 }
    283 
    284 
    285 /**
    286  * Obtain a coin's history from our in-memory cache.
    287  *
    288  * @param coin_pub public key of the coin to cache
    289  * @return NULL if @a coin_pub is not in the cache
    290  */
    291 static struct TALER_EXCHANGEDB_TransactionList *
    292 get_cached_history (const struct TALER_CoinSpendPublicKeyP *coin_pub)
    293 {
    294   unsigned int i = coin_history_index (coin_pub);
    295 
    296   if (0 ==
    297       GNUNET_memcmp (coin_pub,
    298                      &coin_histories[i].coin_pub))
    299   {
    300     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    301                 "Found verification of %s in cache\n",
    302                 TALER_B2S (coin_pub));
    303     return coin_histories[i].tl;
    304   }
    305   return NULL;
    306 }
    307 
    308 
    309 /* ***************************** Report logic **************************** */
    310 
    311 /**
    312  * Called in case we detect an emergency situation where the exchange
    313  * is paying out a larger amount on a denomination than we issued in
    314  * that denomination.  This means that the exchange's private keys
    315  * might have gotten compromised, and that we need to trigger an
    316  * emergency request to all wallets to deposit pending coins for the
    317  * denomination (and as an exchange suffer a huge financial loss).
    318  *
    319  * @param issue denomination key where the loss was detected
    320  * @param risk maximum risk that might have just become real (coins created by this @a issue)
    321  * @param loss actual losses already (actualized before denomination was revoked)
    322  * @return transaction status
    323  */
    324 static enum GNUNET_DB_QueryStatus
    325 report_emergency_by_amount (
    326   const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue,
    327   const struct TALER_Amount *risk,
    328   const struct TALER_Amount *loss)
    329 {
    330   enum GNUNET_DB_QueryStatus qs;
    331   struct TALER_AUDITORDB_Emergency emergency = {
    332     .denom_loss = *loss,
    333     .denompub_h = *&issue->denom_hash,
    334     .denom_risk = *risk,
    335     .deposit_start = *&issue->start.abs_time,
    336     .deposit_end = *&issue->expire_deposit.abs_time,
    337     .value = *&issue->value
    338   };
    339 
    340   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    341               "Reporting emergency on denomination `%s' over loss of %s\n",
    342               GNUNET_h2s (&issue->denom_hash.hash),
    343               TALER_amount2s (loss));
    344 
    345   qs = TALER_AUDITORDB_insert_emergency (
    346     TALER_ARL_adb,
    347     &emergency);
    348   if (qs < 0)
    349   {
    350     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    351     return qs;
    352   }
    353   TALER_ARL_amount_add (&TALER_ARL_USE_AB (
    354                           coins_reported_emergency_risk_by_amount),
    355                         &TALER_ARL_USE_AB (
    356                           coins_reported_emergency_risk_by_amount),
    357                         risk);
    358   TALER_ARL_amount_add (&TALER_ARL_USE_AB (coins_emergencies_loss),
    359                         &TALER_ARL_USE_AB (coins_emergencies_loss),
    360                         loss);
    361   return qs;
    362 }
    363 
    364 
    365 /**
    366  * Called in case we detect an emergency situation where the exchange
    367  * is paying out a larger NUMBER of coins of a denomination than we
    368  * issued in that denomination.  This means that the exchange's
    369  * private keys might have gotten compromised, and that we need to
    370  * trigger an emergency request to all wallets to deposit pending
    371  * coins for the denomination (and as an exchange suffer a huge
    372  * financial loss).
    373  *
    374  * @param issue denomination key where the loss was detected
    375  * @param num_issued number of coins that were issued
    376  * @param num_known number of coins that have been deposited
    377  * @param risk amount that is at risk
    378  * @return transaction status
    379  */
    380 static enum GNUNET_DB_QueryStatus
    381 report_emergency_by_count (
    382   const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue,
    383   uint64_t num_issued,
    384   uint64_t num_known,
    385   const struct TALER_Amount *risk)
    386 {
    387   enum GNUNET_DB_QueryStatus qs;
    388   struct TALER_AUDITORDB_EmergenciesByCount emergenciesByCount = {
    389     .denompub_h = issue->denom_hash,
    390     .num_issued = num_issued,
    391     .num_known = num_known,
    392     .start = issue->start.abs_time,
    393     .deposit_end = issue->expire_deposit.abs_time,
    394     .value = issue->value
    395   };
    396 
    397   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    398               "Reporting emergency on denomination `%s' with issued %lu vs known %lu over risk of %s\n",
    399               GNUNET_h2s (&issue->denom_hash.hash),
    400               num_issued,
    401               num_known,
    402               TALER_amount2s (risk));
    403 
    404   qs = TALER_AUDITORDB_insert_emergency_by_count (
    405     TALER_ARL_adb,
    406     &emergenciesByCount);
    407 
    408   if (qs < 0)
    409   {
    410     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    411     return qs;
    412   }
    413   TALER_ARL_amount_add (&TALER_ARL_USE_AB (
    414                           coins_reported_emergency_risk_by_count),
    415                         &TALER_ARL_USE_AB (
    416                           coins_reported_emergency_risk_by_count),
    417                         risk);
    418   for (uint64_t i = num_issued; i < num_known; i++)
    419     TALER_ARL_amount_add (&TALER_ARL_USE_AB (coins_emergencies_loss_by_count),
    420                           &TALER_ARL_USE_AB (coins_emergencies_loss_by_count),
    421                           &issue->value);
    422   return qs;
    423 }
    424 
    425 
    426 /**
    427  * Report a (serious) inconsistency in the exchange's database with
    428  * respect to calculations involving amounts.
    429  *
    430  * @param operation what operation had the inconsistency
    431  * @param rowid affected row, 0 if row is missing
    432  * @param exchange amount calculated by exchange
    433  * @param auditor amount calculated by auditor
    434  * @param profitable 1 if @a exchange being larger than @a auditor is
    435  *           profitable for the exchange for this operation
    436  *           (and thus @a exchange being smaller than @ auditor
    437  *            representing a loss for the exchange);
    438  *           -1 if @a exchange being smaller than @a auditor is
    439  *           profitable for the exchange; and 0 if it is unclear
    440  * @return transaction status
    441  */
    442 static enum GNUNET_DB_QueryStatus
    443 report_amount_arithmetic_inconsistency (
    444   const char *operation,
    445   uint64_t rowid,
    446   const struct TALER_Amount *exchange,
    447   const struct TALER_Amount *auditor,
    448   int profitable)
    449 {
    450   struct TALER_Amount delta;
    451   struct TALER_Amount *target;
    452 
    453   if (0 < TALER_amount_cmp (exchange,
    454                             auditor))
    455   {
    456     /* exchange > auditor */
    457     TALER_ARL_amount_subtract (&delta,
    458                                exchange,
    459                                auditor);
    460   }
    461   else
    462   {
    463     /* exchange <= auditor */
    464     profitable = -profitable;
    465     TALER_ARL_amount_subtract (&delta,
    466                                auditor,
    467                                exchange);
    468   }
    469 
    470   {
    471     struct TALER_AUDITORDB_AmountArithmeticInconsistency aai = {
    472       .profitable = profitable,
    473       .problem_row_id = rowid,
    474       .operation = (char *) operation,
    475       .exchange_amount = *exchange,
    476       .auditor_amount = *auditor
    477     };
    478     enum GNUNET_DB_QueryStatus qs;
    479 
    480     qs = TALER_AUDITORDB_insert_amount_arithmetic_inconsistency (
    481       TALER_ARL_adb,
    482       &aai);
    483     if (qs < 0)
    484     {
    485       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    486       return qs;
    487     }
    488   }
    489   if (0 != profitable)
    490   {
    491     target = (1 == profitable)
    492       ? &TALER_ARL_USE_AB (coins_total_arithmetic_delta_plus)
    493       : &TALER_ARL_USE_AB (coins_total_arithmetic_delta_minus);
    494     TALER_ARL_amount_add (target,
    495                           target,
    496                           &delta);
    497   }
    498   return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
    499 }
    500 
    501 
    502 /**
    503  * Report a (serious) inconsistency in the exchange's database.
    504  *
    505  * @param table affected table
    506  * @param rowid affected row, 0 if row is missing
    507  * @param diagnostic message explaining the problem
    508  * @return transaction status
    509  */
    510 static enum GNUNET_DB_QueryStatus
    511 report_row_inconsistency (const char *table,
    512                           uint64_t rowid,
    513                           const char *diagnostic)
    514 {
    515 
    516   enum GNUNET_DB_QueryStatus qs;
    517   struct TALER_AUDITORDB_RowInconsistency ri = {
    518     .row_table = (char *) table,
    519     .problem_row_id = rowid,
    520     .diagnostic = (char *) diagnostic
    521   };
    522 
    523   qs = TALER_AUDITORDB_insert_row_inconsistency (
    524     TALER_ARL_adb,
    525     &ri);
    526   if (qs < 0)
    527   {
    528     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    529     return qs;
    530   }
    531   return qs;
    532 }
    533 
    534 
    535 /* ************* Analyze history of a coin ******************** */
    536 
    537 
    538 /**
    539  * Obtain @a coin_pub's history, verify it, report inconsistencies
    540  * and store the result in our cache.
    541  *
    542  * @param coin_pub public key of the coin to check the history of
    543  * @param rowid a row identifying the transaction
    544  * @param operation operation matching @a rowid
    545  * @param value value of the respective coin's denomination
    546  * @return database status code, negative on failures
    547  */
    548 static enum GNUNET_DB_QueryStatus
    549 check_coin_history (const struct TALER_CoinSpendPublicKeyP *coin_pub,
    550                     uint64_t rowid,
    551                     const char *operation,
    552                     const struct TALER_Amount *value)
    553 {
    554   struct TALER_EXCHANGEDB_TransactionList *tl;
    555   enum GNUNET_DB_QueryStatus qs = GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
    556   struct TALER_Amount total;
    557   struct TALER_Amount spent;
    558   struct TALER_Amount refunded;
    559   struct TALER_Amount deposit_fee;
    560   bool have_refund;
    561   uint64_t etag_out;
    562 
    563   /* FIXME-Optimization: could use 'etag' mechanism to only fetch transactions
    564      we did not yet process, instead of going over them
    565      again and again. */
    566   {
    567     struct TALER_Amount balance;
    568     struct TALER_DenominationHashP h_denom_pub;
    569 
    570     qs = TALER_EXCHANGEDB_get_coin_transactions (TALER_ARL_edb,
    571                                                  false,
    572                                                  coin_pub,
    573                                                  0,
    574                                                  0,
    575                                                  &etag_out,
    576                                                  &balance,
    577                                                  &h_denom_pub,
    578                                                  &tl);
    579   }
    580   if (0 > qs)
    581     return qs;
    582   GNUNET_assert (GNUNET_OK ==
    583                  TALER_amount_set_zero (value->currency,
    584                                         &refunded));
    585   GNUNET_assert (GNUNET_OK ==
    586                  TALER_amount_set_zero (value->currency,
    587                                         &spent));
    588   GNUNET_assert (GNUNET_OK ==
    589                  TALER_amount_set_zero (value->currency,
    590                                         &deposit_fee));
    591   have_refund = false;
    592   for (struct TALER_EXCHANGEDB_TransactionList *pos = tl;
    593        NULL != pos;
    594        pos = pos->next)
    595   {
    596     switch (pos->type)
    597     {
    598     case TALER_EXCHANGEDB_TT_DEPOSIT:
    599       /* spent += pos->amount_with_fee */
    600       TALER_ARL_amount_add (&spent,
    601                             &spent,
    602                             &pos->details.deposit->amount_with_fee);
    603       deposit_fee = pos->details.deposit->deposit_fee;
    604       break;
    605     case TALER_EXCHANGEDB_TT_MELT:
    606       /* spent += pos->amount_with_fee */
    607       TALER_ARL_amount_add (&spent,
    608                             &spent,
    609                             &pos->details.melt->amount_with_fee);
    610       break;
    611     case TALER_EXCHANGEDB_TT_REFUND:
    612       /* refunded += pos->refund_amount - pos->refund_fee */
    613       TALER_ARL_amount_add (&refunded,
    614                             &refunded,
    615                             &pos->details.refund->refund_amount);
    616       TALER_ARL_amount_add (&spent,
    617                             &spent,
    618                             &pos->details.refund->refund_fee);
    619       have_refund = true;
    620       break;
    621     case TALER_EXCHANGEDB_TT_RECOUP_REFRESH_RECEIVER:
    622       /* refunded += pos->value */
    623       TALER_ARL_amount_add (&refunded,
    624                             &refunded,
    625                             &pos->details.old_coin_recoup->value);
    626       break;
    627     case TALER_EXCHANGEDB_TT_RECOUP_WITHDRAW:
    628       /* spent += pos->value */
    629       TALER_ARL_amount_add (&spent,
    630                             &spent,
    631                             &pos->details.recoup->value);
    632       break;
    633     case TALER_EXCHANGEDB_TT_RECOUP_REFRESH:
    634       /* spent += pos->value */
    635       TALER_ARL_amount_add (&spent,
    636                             &spent,
    637                             &pos->details.recoup_refresh->value);
    638       break;
    639     case TALER_EXCHANGEDB_TT_PURSE_DEPOSIT:
    640       /* spent += pos->value */
    641       TALER_ARL_amount_add (&spent,
    642                             &spent,
    643                             &pos->details.purse_deposit->amount);
    644       break;
    645     case TALER_EXCHANGEDB_TT_PURSE_REFUND:
    646       TALER_ARL_amount_add (&refunded,
    647                             &refunded,
    648                             &pos->details.purse_refund->refund_amount);
    649       break;
    650     case TALER_EXCHANGEDB_TT_RESERVE_OPEN:
    651       TALER_ARL_amount_add (&spent,
    652                             &spent,
    653                             &pos->details.reserve_open->coin_contribution);
    654       break;
    655     } /* switch (pos->type) */
    656   } /* for (...) */
    657   if (have_refund)
    658   {
    659     /* If we gave any refund, also discount ONE deposit fee */
    660     TALER_ARL_amount_add (&refunded,
    661                           &refunded,
    662                           &deposit_fee);
    663   }
    664   /* total coin value = original value plus refunds */
    665   TALER_ARL_amount_add (&total,
    666                         &refunded,
    667                         value);
    668   if (1 ==
    669       TALER_amount_cmp (&spent,
    670                         &total))
    671   {
    672     /* spent > total: bad */
    673     struct TALER_Amount loss;
    674 
    675     TALER_ARL_amount_subtract (&loss,
    676                                &spent,
    677                                &total);
    678     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    679                 "Loss detected for coin %s - %s\n",
    680                 TALER_B2S (coin_pub),
    681                 TALER_amount2s (&loss));
    682     qs = report_amount_arithmetic_inconsistency (operation,
    683                                                  rowid,
    684                                                  &spent,
    685                                                  &total,
    686                                                  -1);
    687     if (qs < 0)
    688     {
    689       TALER_EXCHANGEDB_free_coin_transaction_list (tl);
    690       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    691       return qs;
    692     }
    693   }
    694   cache_history (coin_pub,
    695                  tl);
    696   return qs;
    697 }
    698 
    699 
    700 /* ************************* Analyze coins ******************** */
    701 /* This logic checks that the exchange did the right thing for each
    702    coin, checking deposits, refunds, refresh* and known_coins
    703    tables */
    704 
    705 
    706 /**
    707  * Summary data we keep per denomination.
    708  */
    709 struct DenominationSummary
    710 {
    711   /**
    712    * Information about the circulation.
    713    */
    714   struct TALER_AUDITORDB_DenominationCirculationData dcd;
    715 
    716   /**
    717    * Denomination key information for this denomination.
    718    */
    719   const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue;
    720 
    721   /**
    722    * True if this record already existed in the DB.
    723    * Used to decide between insert/update in
    724    * #sync_denomination().
    725    */
    726   bool in_db;
    727 
    728   /**
    729    * Should we report an emergency for this denomination, causing it to be
    730    * revoked (because more coins were deposited than issued)?
    731    */
    732   bool report_emergency;
    733 
    734   /**
    735    * True if this denomination was revoked.
    736    */
    737   bool was_revoked;
    738 
    739   /**
    740    * True if the remaining balance of this denomination was already booked
    741    * as revenue in an earlier pass, which left a historic revenue row
    742    * behind.  Then @e dcd only holds what happened to its coins in this
    743    * pass.
    744    */
    745   bool finalized;
    746 };
    747 
    748 
    749 /**
    750  * Closure for callbacks during #analyze_coins().
    751  */
    752 struct CoinContext
    753 {
    754 
    755   /**
    756    * Map for tracking information about denominations.
    757    */
    758   struct GNUNET_CONTAINER_MultiHashMap *denom_summaries;
    759 
    760   /**
    761    * Transaction status code.
    762    */
    763   enum GNUNET_DB_QueryStatus qs;
    764 
    765 };
    766 
    767 
    768 /**
    769  * Initialize information about denomination from the database.
    770  *
    771  * @param denom_hash hash of the public key of the denomination
    772  * @param[out] ds summary to initialize
    773  * @return transaction status code
    774  */
    775 static enum GNUNET_DB_QueryStatus
    776 init_denomination (const struct TALER_DenominationHashP *denom_hash,
    777                    struct DenominationSummary *ds)
    778 {
    779   enum GNUNET_DB_QueryStatus qs;
    780   struct TALER_MasterSignatureP msig;
    781   uint64_t rowid;
    782 
    783   qs = TALER_AUDITORDB_get_denomination_balance (TALER_ARL_adb,
    784                                                  denom_hash,
    785                                                  &ds->dcd);
    786   if (0 > qs)
    787   {
    788     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    789     return qs;
    790   }
    791   if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
    792   {
    793     ds->in_db = true;
    794   }
    795   else
    796   {
    797     GNUNET_assert (GNUNET_OK ==
    798                    TALER_amount_set_zero (TALER_ARL_currency,
    799                                           &ds->dcd.denom_balance));
    800     GNUNET_assert (GNUNET_OK ==
    801                    TALER_amount_set_zero (TALER_ARL_currency,
    802                                           &ds->dcd.denom_loss));
    803     GNUNET_assert (GNUNET_OK ==
    804                    TALER_amount_set_zero (TALER_ARL_currency,
    805                                           &ds->dcd.denom_risk));
    806     GNUNET_assert (GNUNET_OK ==
    807                    TALER_amount_set_zero (TALER_ARL_currency,
    808                                           &ds->dcd.recoup_loss));
    809     /* No pending balance: either we never saw this denomination before, or
    810        #sync_denomination() already booked its remaining balance as
    811        revenue, which left a historic revenue row behind. */
    812     qs = TALER_AUDITORDB_get_exists_historic_denom_revenue (TALER_ARL_adb,
    813                                                             denom_hash);
    814     if (0 > qs)
    815     {
    816       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    817       return qs;
    818     }
    819     ds->finalized = (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs);
    820     if (ds->finalized)
    821       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    822                   "Denomination `%s' was already booked as finished\n",
    823                   GNUNET_h2s (&denom_hash->hash));
    824   }
    825   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    826               "Starting balance for denomination `%s' is %s (%llu)\n",
    827               GNUNET_h2s (&denom_hash->hash),
    828               TALER_amount2s (&ds->dcd.denom_balance),
    829               (unsigned long long) ds->dcd.num_issued);
    830   qs = TALER_EXCHANGEDB_get_denomination_revocation (TALER_ARL_edb,
    831                                                      denom_hash,
    832                                                      &msig,
    833                                                      &rowid);
    834   if (0 > qs)
    835   {
    836     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    837     return qs;
    838   }
    839   if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
    840   {
    841     /* check revocation signature */
    842     if (GNUNET_OK !=
    843         TALER_exchange_offline_denomination_revoke_verify (
    844           denom_hash,
    845           &TALER_ARL_master_pub,
    846           &msig))
    847     {
    848       qs = report_row_inconsistency ("denomination revocations",
    849                                      rowid,
    850                                      "revocation signature invalid");
    851       if (qs < 0)
    852       {
    853         GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    854         return qs;
    855       }
    856     }
    857     else
    858     {
    859       ds->was_revoked = true;
    860     }
    861   }
    862   return ds->in_db
    863     ? GNUNET_DB_STATUS_SUCCESS_ONE_RESULT
    864     : GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
    865 }
    866 
    867 
    868 /**
    869  * Obtain the denomination summary for the given @a dh
    870  *
    871  * @param cc our execution context
    872  * @param issue denomination key information for @a dh
    873  * @return NULL on error
    874  */
    875 static struct DenominationSummary *
    876 get_denomination_summary (
    877   struct CoinContext *cc,
    878   const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue)
    879 {
    880   struct DenominationSummary *ds;
    881   const struct TALER_DenominationHashP *dh = &issue->denom_hash;
    882 
    883   ds = GNUNET_CONTAINER_multihashmap_get (cc->denom_summaries,
    884                                           &dh->hash);
    885   if (NULL != ds)
    886     return ds;
    887   ds = GNUNET_new (struct DenominationSummary);
    888   ds->issue = issue;
    889   if (0 > (cc->qs = init_denomination (dh,
    890                                        ds)))
    891   {
    892     GNUNET_break (0);
    893     GNUNET_free (ds);
    894     return NULL;
    895   }
    896   GNUNET_assert (GNUNET_OK ==
    897                  GNUNET_CONTAINER_multihashmap_put (cc->denom_summaries,
    898                                                     &dh->hash,
    899                                                     ds,
    900                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
    901                  );
    902   return ds;
    903 }
    904 
    905 
    906 /**
    907  * Check whether the deposit period of a denomination is over, so that no
    908  * coin of it can be spent any more and the risk of a recoup on it is gone.
    909  * Allows for a grace period of #DEPOSIT_GRACE_PERIOD, as clocks are not
    910  * perfectly synchronized.
    911  *
    912  * @param issue denomination to check
    913  * @return true if the deposit period of @a issue (plus grace) is over
    914  */
    915 static bool
    916 is_past_deposit_grace (
    917   const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue)
    918 {
    919   return GNUNET_TIME_absolute_cmp (
    920     GNUNET_TIME_absolute_get (),
    921     >,
    922     GNUNET_TIME_absolute_add (issue->expire_deposit.abs_time,
    923                               DEPOSIT_GRACE_PERIOD));
    924 }
    925 
    926 
    927 /**
    928  * Check whether the legal period of a denomination is over.  From then on
    929  * the exchange may forget the denomination, so nothing can move value on
    930  * it any anymore and its remaining balance can be booked as revenue for
    931  * good.
    932  *
    933  * @param issue denomination to check
    934  * @return true if the legal period of @a issue (plus grace) is over
    935  */
    936 static bool
    937 is_past_legal_grace (
    938   const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue)
    939 {
    940   return GNUNET_TIME_absolute_cmp (
    941     GNUNET_TIME_absolute_get (),
    942     >,
    943     GNUNET_TIME_absolute_add (issue->expire_legal.abs_time,
    944                               DEPOSIT_GRACE_PERIOD));
    945 }
    946 
    947 
    948 /**
    949  * Take the risk of a denomination out of ``coin_balance_risk`` and zero it in @a
    950  * ds.  Called once the deposit period of the denomination is over as from
    951  * then on there is nothing left to lose.
    952  *
    953  * @param[in,out] ds summary of the denomination
    954  */
    955 static void
    956 release_denomination_risk (struct DenominationSummary *ds)
    957 {
    958   if (TALER_amount_is_zero (&ds->dcd.denom_risk))
    959     return;
    960   TALER_ARL_amount_subtract (&TALER_ARL_USE_AB (coin_balance_risk),
    961                              &TALER_ARL_USE_AB (coin_balance_risk),
    962                              &ds->dcd.denom_risk);
    963   /* If the above fails, our risk assessment is inconsistent!  This is
    964      really, really bad (auditor-internal invariant would be violated).
    965      Hence we can "safely" assert.  If this assertion fails, well, good
    966      luck: there is a bug in the auditor _or_ the auditor's database is
    967      corrupt. */
    968   GNUNET_assert (GNUNET_OK ==
    969                  TALER_amount_set_zero (TALER_ARL_currency,
    970                                         &ds->dcd.denom_risk));
    971 }
    972 
    973 
    974 /**
    975  * Book the remaining balance of a denomination as revenue: the coins can
    976  * no longer be spent, so the exchange keeps their value and no longer
    977  * holds it in escrow.  Appends a row to the historic revenue table, which
    978  * also marks the denomination as finished for the passes that follow.
    979  *
    980  * @param[in,out] ds summary of the denomination
    981  * @param denom_h hash of the denomination public key
    982  * @return transaction status
    983  */
    984 static enum GNUNET_DB_QueryStatus
    985 book_denomination_revenue (struct DenominationSummary *ds,
    986                            const struct TALER_DenominationHashP *denom_h)
    987 {
    988   enum GNUNET_DB_QueryStatus qs;
    989 
    990   release_denomination_risk (ds);
    991   if (! TALER_amount_is_zero (&ds->dcd.denom_balance))
    992   {
    993     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    994                 "Denomination `%s' is finished, booking %s in expiration profits\n",
    995                 GNUNET_h2s (&denom_h->hash),
    996                 TALER_amount2s (&ds->dcd.denom_balance));
    997     if (-1 == TALER_amount_cmp (&TALER_ARL_USE_AB (total_escrowed),
    998                                 &ds->dcd.denom_balance))
    999     {
   1000       /* We never held that much in escrow?  Can happen if the exchange
   1001          accepted coins it never issued (an emergency). */
   1002       qs = report_amount_arithmetic_inconsistency (
   1003         "subtracting expired coins from escrow balance",
   1004         0,
   1005         &TALER_ARL_USE_AB (total_escrowed),
   1006         &ds->dcd.denom_balance,
   1007         0);
   1008       if (0 > qs)
   1009       {
   1010         GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1011         return qs;
   1012       }
   1013     }
   1014     else
   1015     {
   1016       TALER_ARL_amount_subtract (&TALER_ARL_USE_AB (total_escrowed),
   1017                                  &TALER_ARL_USE_AB (total_escrowed),
   1018                                  &ds->dcd.denom_balance);
   1019     }
   1020   }
   1021   /* Written unconditionally: the row is what tells the next pass that this
   1022      denomination was already finished. */
   1023   return TALER_AUDITORDB_insert_historic_denom_revenue (
   1024     TALER_ARL_adb,
   1025     denom_h,
   1026     ds->issue->expire_deposit,
   1027     &ds->dcd.denom_balance,
   1028     &ds->dcd.recoup_loss);
   1029 }
   1030 
   1031 
   1032 /**
   1033  * Settle the state of a denomination at the end of a pass: release the
   1034  * risk once its deposit period is over, book its remaining balance as
   1035  * revenue once its legal period is over, and otherwise write the pending
   1036  * balance back to the database.
   1037  *
   1038  * @param cc our execution context
   1039  * @param denom_h hash of the denomination public key
   1040  * @param[in,out] ds summary of the denomination
   1041  * @return #GNUNET_OK on success,
   1042  *         #GNUNET_SYSERR on failure (@e qs of @a cc is set)
   1043  */
   1044 static enum GNUNET_GenericReturnValue
   1045 settle_denomination (struct CoinContext *cc,
   1046                      const struct TALER_DenominationHashP *denom_h,
   1047                      struct DenominationSummary *ds)
   1048 {
   1049   const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue = ds->issue;
   1050   enum GNUNET_DB_QueryStatus qs;
   1051 
   1052   if (ds->finalized)
   1053   {
   1054     /* The denomination was booked as finished in an earlier pass, but the
   1055        exchange moved value on it again.  The bound on the purse expiration
   1056        is supposed to prevent that, so report it -- and append the credit
   1057        as another final row instead of rewriting the row that was already
   1058        published. */
   1059     qs = report_row_inconsistency (
   1060       "historic denomination revenue",
   1061       0,
   1062       "denomination was credited after it was booked as finished");
   1063     if (0 > qs)
   1064     {
   1065       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1066       cc->qs = qs;
   1067       return GNUNET_SYSERR;
   1068     }
   1069     qs = book_denomination_revenue (ds,
   1070                                     denom_h);
   1071     if (0 > qs)
   1072     {
   1073       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1074       cc->qs = qs;
   1075       return GNUNET_SYSERR;
   1076     }
   1077     return GNUNET_OK;
   1078   }
   1079   if (is_past_deposit_grace (issue))
   1080   {
   1081     /* The coins of this denomination can no longer be deposited, so the
   1082        risk of a recoup on it is gone (stage one).  What was not spent only
   1083        becomes revenue once the legal period is over as well: until then
   1084        the exchange still has to answer for those coins, and until then it
   1085        can still move value on the denomination, which the pending row has
   1086        to be around to record. */
   1087     release_denomination_risk (ds);
   1088   }
   1089   if (is_past_legal_grace (issue))
   1090   {
   1091     /* Nothing can happen to this denomination any more: book what is left
   1092        as revenue (stage two) and drop the pending row.  The historic row
   1093        is final, and finalization happens exactly once. */
   1094     if (ds->in_db)
   1095     {
   1096       qs = TALER_AUDITORDB_delete_denomination_balance (TALER_ARL_adb,
   1097                                                         denom_h);
   1098       if (0 >= qs)
   1099       {
   1100         GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1101         cc->qs = qs;
   1102         return GNUNET_SYSERR;
   1103       }
   1104     }
   1105     qs = book_denomination_revenue (ds,
   1106                                     denom_h);
   1107     if (0 > qs)
   1108     {
   1109       /* Failed to store profits? Bad database */
   1110       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1111       cc->qs = qs;
   1112       return GNUNET_SYSERR;
   1113     }
   1114     return GNUNET_OK;
   1115   }
   1116   /* Not finished yet, just store the current denomination summary to the
   1117      auditor database for the next iteration */
   1118   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1119               "Final balance for denomination `%s' is %s (%llu)\n",
   1120               GNUNET_h2s (&denom_h->hash),
   1121               TALER_amount2s (&ds->dcd.denom_balance),
   1122               (unsigned long long) ds->dcd.num_issued);
   1123   if (ds->in_db)
   1124     qs = TALER_AUDITORDB_update_denomination_balance (TALER_ARL_adb,
   1125                                                       denom_h,
   1126                                                       &ds->dcd);
   1127   else
   1128     qs = TALER_AUDITORDB_insert_denomination_balance (TALER_ARL_adb,
   1129                                                       denom_h,
   1130                                                       &ds->dcd);
   1131   if (qs < 0)
   1132   {
   1133     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1134     cc->qs = qs;
   1135     return GNUNET_SYSERR;
   1136   }
   1137   return GNUNET_OK;
   1138 }
   1139 
   1140 
   1141 /**
   1142  * Write information about the current knowledge about a denomination key
   1143  * back to the database and update our global reporting data about the
   1144  * denomination.
   1145  *
   1146  * @param cls the `struct CoinContext`
   1147  * @param denom_hash the hash of the denomination key
   1148  * @param value a `struct DenominationSummary`
   1149  * @return #GNUNET_OK (continue to iterate)
   1150  *         #GNUNET_SYSERR (stop to iterate)
   1151  */
   1152 static enum GNUNET_GenericReturnValue
   1153 sync_denomination (void *cls,
   1154                    const struct GNUNET_HashCode *denom_hash,
   1155                    void *value)
   1156 {
   1157   struct CoinContext *cc = cls;
   1158   struct TALER_DenominationHashP denom_h = {
   1159     .hash = *denom_hash
   1160   };
   1161   struct DenominationSummary *ds = value;
   1162   const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue = ds->issue;
   1163   enum GNUNET_DB_QueryStatus qs;
   1164 
   1165   /* Check for emergencies first: this must happen regardless of whether the
   1166      denomination has expired in the meantime, as a denomination that was
   1167      over-issued or over-spent still signals a key compromise (and rows are
   1168      routinely first analyzed after the deposit expiration). */
   1169   {
   1170     long long cnt = 0;
   1171 
   1172     /* For a denomination that was already booked as finished, ds->dcd only
   1173        covers this pass and its num_issued is zero, so comparing it with
   1174        the number of known coins would report a false emergency.  The count
   1175        was checked when the denomination was finished, and no coin can be
   1176        issued in it any more. */
   1177     if (! ds->finalized)
   1178       cnt = TALER_EXCHANGEDB_get_count_known_coins (TALER_ARL_edb,
   1179                                                     &denom_h);
   1180     if (0 > cnt)
   1181     {
   1182       /* Failed to obtain count? Bad database */
   1183       qs = (enum GNUNET_DB_QueryStatus) cnt;
   1184       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1185       cc->qs = qs;
   1186       return GNUNET_SYSERR;
   1187     }
   1188     if (ds->dcd.num_issued < (uint64_t) cnt)
   1189     {
   1190       /* more coins deposited than issued! very bad */
   1191       qs = report_emergency_by_count (issue,
   1192                                       ds->dcd.num_issued,
   1193                                       cnt,
   1194                                       &ds->dcd.denom_risk);
   1195       if (qs < 0)
   1196       {
   1197         GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1198         cc->qs = qs;
   1199         return GNUNET_SYSERR;
   1200       }
   1201     }
   1202     if (ds->report_emergency)
   1203     {
   1204       /* Value of coins deposited exceed value of coins
   1205          issued! Also very bad! */
   1206       qs = report_emergency_by_amount (issue,
   1207                                        &ds->dcd.denom_risk,
   1208                                        &ds->dcd.denom_loss);
   1209       if (qs < 0)
   1210       {
   1211         GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1212         cc->qs = qs;
   1213         return GNUNET_SYSERR;
   1214       }
   1215     }
   1216   }
   1217   return settle_denomination (cc,
   1218                               &denom_h,
   1219                               ds);
   1220 }
   1221 
   1222 
   1223 /**
   1224  * Closure for #sweep_denomination_cb().
   1225  */
   1226 struct SweepContext
   1227 {
   1228   /**
   1229    * Our execution context.
   1230    */
   1231   struct CoinContext *cc;
   1232 
   1233   /**
   1234    * Row ID of the last pending denomination we looked at.
   1235    */
   1236   uint64_t last_row;
   1237 
   1238   /**
   1239    * Number of pending denominations we looked at.
   1240    */
   1241   unsigned int count;
   1242 
   1243   /**
   1244    * Transaction status code.
   1245    */
   1246   enum GNUNET_DB_QueryStatus qs;
   1247 };
   1248 
   1249 
   1250 /**
   1251  * Function called on each denomination that still has a pending balance.
   1252  * Settles those that no transaction of this pass touched; the ones that
   1253  * were touched are settled by #sync_denomination() and are skipped here.
   1254  *
   1255  * @param sc context for the sweep
   1256  * @param serial_id row of the pending balance
   1257  * @param dp the pending balance
   1258  * @return #GNUNET_OK to continue to iterate
   1259  */
   1260 static enum GNUNET_GenericReturnValue
   1261 sweep_denomination_cb (struct SweepContext *sc,
   1262                        uint64_t serial_id,
   1263                        const struct TALER_AUDITORDB_DenominationPending *dp)
   1264 {
   1265   struct CoinContext *cc = sc->cc;
   1266   const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue;
   1267   struct DenominationSummary *ds;
   1268   enum GNUNET_DB_QueryStatus qs;
   1269   enum GNUNET_GenericReturnValue ret;
   1270 
   1271   sc->last_row = serial_id;
   1272   sc->count++;
   1273   if (NULL !=
   1274       GNUNET_CONTAINER_multihashmap_get (cc->denom_summaries,
   1275                                          &dp->denom_pub_hash.hash))
   1276     return GNUNET_OK; /* settled by #sync_denomination() */
   1277   qs = TALER_ARL_get_denomination_info_by_hash (&dp->denom_pub_hash,
   1278                                                 &issue);
   1279   if (0 > qs)
   1280   {
   1281     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1282     sc->qs = qs;
   1283     return GNUNET_SYSERR;
   1284   }
   1285   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
   1286   {
   1287     /* The exchange no longer knows the denomination, so we cannot tell
   1288        when it ends and can never settle this balance.  exchange_do_main_gc
   1289        only drops a denomination once its legal period is over, so this
   1290        means that the exchange forgot it before we were done with it. */
   1291     qs = report_row_inconsistency (
   1292       "denomination pending",
   1293       serial_id,
   1294       "denomination with a pending balance is unknown to the exchange");
   1295     if (0 > qs)
   1296     {
   1297       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1298       sc->qs = qs;
   1299       return GNUNET_SYSERR;
   1300     }
   1301     return GNUNET_OK;
   1302   }
   1303   if (! is_past_deposit_grace (issue))
   1304     return GNUNET_OK; /* nothing to settle yet */
   1305   /* No coin of this denomination was created or spent in this pass, so
   1306      neither the number of known coins nor the balance can have changed
   1307      since the pass that last touched it: the emergency checks of
   1308      #sync_denomination() would only repeat themselves here. */
   1309   ds = GNUNET_new (struct DenominationSummary);
   1310   ds->issue = issue;
   1311   ds->in_db = true;
   1312   ds->dcd.denom_balance = dp->denom_balance;
   1313   ds->dcd.denom_loss = dp->denom_loss;
   1314   ds->dcd.denom_risk = dp->denom_risk;
   1315   ds->dcd.recoup_loss = dp->recoup_loss;
   1316   ds->dcd.num_issued = dp->num_issued;
   1317   ret = settle_denomination (cc,
   1318                              &dp->denom_pub_hash,
   1319                              ds);
   1320   GNUNET_free (ds);
   1321   if (GNUNET_OK != ret)
   1322     sc->qs = cc->qs;
   1323   return ret;
   1324 }
   1325 
   1326 
   1327 /**
   1328  * Settle the denominations that still have a pending balance but that no
   1329  * transaction of this pass touched.  Without this, a denomination whose
   1330  * coins see no activity after its deposit period would keep its risk and
   1331  * its outstanding balance on the auditor's books forever.
   1332  *
   1333  * @param cc our execution context
   1334  * @return transaction status
   1335  */
   1336 static enum GNUNET_DB_QueryStatus
   1337 sweep_denominations (struct CoinContext *cc)
   1338 {
   1339   struct SweepContext sc = {
   1340     .cc = cc
   1341   };
   1342 
   1343   while (true)
   1344   {
   1345     enum GNUNET_DB_QueryStatus qs;
   1346 
   1347     sc.count = 0;
   1348     qs = TALER_AUDITORDB_iterate_denomination_pending (
   1349       TALER_ARL_adb,
   1350       SWEEP_BATCH_SIZE,
   1351       sc.last_row,
   1352       &sweep_denomination_cb,
   1353       &sc);
   1354     if (0 > qs)
   1355     {
   1356       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1357       return qs;
   1358     }
   1359     if (0 > sc.qs)
   1360       return sc.qs;
   1361     if (SWEEP_BATCH_SIZE > sc.count)
   1362       return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
   1363   }
   1364 }
   1365 
   1366 
   1367 /**
   1368  * Remove and free the memory of @a value from the
   1369  * denomination summaries.
   1370  *
   1371  * @param cls the `struct CoinContext`
   1372  * @param denom_hash the hash of the denomination key
   1373  * @param value a `struct DenominationSummary`
   1374  * @return #GNUNET_OK (continue to iterate)
   1375  */
   1376 static enum GNUNET_GenericReturnValue
   1377 cleanup_denomination (void *cls,
   1378                       const struct GNUNET_HashCode *denom_hash,
   1379                       void *value)
   1380 {
   1381   struct CoinContext *cc = cls;
   1382   struct DenominationSummary *ds = value;
   1383 
   1384   GNUNET_assert (GNUNET_YES ==
   1385                  GNUNET_CONTAINER_multihashmap_remove (cc->denom_summaries,
   1386                                                        denom_hash,
   1387                                                        ds));
   1388   GNUNET_free (ds);
   1389   return GNUNET_OK;
   1390 }
   1391 
   1392 
   1393 /**
   1394  * Function called with details about all withdraw operations.
   1395  * Updates the denomination balance and the overall balance as
   1396  * we now have additional coins that have been issued.
   1397  *
   1398  * Note that the signature was already checked in
   1399  * taler-helper-auditor-reserves.c::#handle_withdrawals(), so we do not check
   1400  * it again here.
   1401  *
   1402  * @param cc our `struct CoinContext`
   1403  * @param rowid unique serial ID for the refresh session in our DB
   1404  * @param num_denom_serials number of elements in @e denom_serials array
   1405  * @param denom_serials array with length @e num_denom_serials of serial ID's of denominations in our DB
   1406  * @param selected_h hash over the gamma-selected planchets
   1407  * @param h_planchets running hash over all hashes of blinded planchets in the original withdraw request
   1408  * @param blinding_seed the blinding seed for CS denominations that was provided during withdraw; might be NULL
   1409  * @param age_proof_required true if the withdraw request required an age proof.
   1410  * @param max_age if @e age_proof_required is true, the maximum age that was set on the coins.
   1411  * @param noreveal_index if @e age_proof_required is true, the index that was returned by the exchange for the reveal phase.
   1412  * @param reserve_pub public key of the reserve
   1413  * @param reserve_sig signature over the withdraw operation
   1414  * @param execution_date when did the wallet withdraw the coin
   1415  * @param amount_with_fee amount that was withdrawn
   1416  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop
   1417  */
   1418 static enum GNUNET_GenericReturnValue
   1419 withdraw_cb (
   1420   struct CoinContext *cc,
   1421   uint64_t rowid,
   1422   size_t num_denom_serials,
   1423   const uint64_t *denom_serials,
   1424   const struct TALER_HashBlindedPlanchetsP *selected_h,
   1425   const struct TALER_HashBlindedPlanchetsP *h_planchets,
   1426   const struct TALER_BlindingMasterSeedP *blinding_seed,
   1427   bool age_proof_required,
   1428   uint8_t max_age,
   1429   uint8_t noreveal_index,
   1430   const struct TALER_ReservePublicKeyP *reserve_pub,
   1431   const struct TALER_ReserveSignatureP *reserve_sig,
   1432   struct GNUNET_TIME_Timestamp execution_date,
   1433   const struct TALER_Amount *amount_with_fee)
   1434 {
   1435 
   1436   /* Note: some optimization potential here: lots of fields we
   1437      could avoid fetching from the database with a custom function. */
   1438   (void) h_planchets;
   1439   (void) blinding_seed;
   1440   (void) reserve_pub;
   1441   (void) reserve_sig;
   1442   (void) execution_date;
   1443   (void) amount_with_fee;
   1444 
   1445   GNUNET_assert (rowid >=
   1446                  TALER_ARL_USE_PP (coins_withdraw_serial_id)); /* should be monotonically increasing */
   1447   TALER_ARL_USE_PP (coins_withdraw_serial_id) = rowid + 1;
   1448 
   1449   for (size_t i=0; i < num_denom_serials; i++)
   1450   {
   1451     struct DenominationSummary *ds;
   1452     const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue;
   1453     enum GNUNET_DB_QueryStatus qs;
   1454 
   1455     qs = TALER_ARL_get_denomination_info_by_serial (denom_serials[i],
   1456                                                     &issue);
   1457     if (0 > qs)
   1458     {
   1459       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1460       cc->qs = qs;
   1461       return GNUNET_SYSERR;
   1462     }
   1463     if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
   1464     {
   1465       qs = report_row_inconsistency ("withdraw",
   1466                                      rowid,
   1467                                      "denomination key not found");
   1468       if (0 > qs)
   1469       {
   1470         GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1471         cc->qs = qs;
   1472         return GNUNET_SYSERR;
   1473       }
   1474       /* We cannot account for this planchet, but the other planchets
   1475          of the batch must still be processed. */
   1476       continue;
   1477     }
   1478     ds = get_denomination_summary (cc,
   1479                                    issue);
   1480     if (NULL == ds)
   1481     {
   1482       /* cc->qs is set by #get_denomination_summary() */
   1483       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == cc->qs);
   1484       return GNUNET_SYSERR;
   1485     }
   1486     ds->dcd.num_issued++;
   1487     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1488                 "Issued coin in denomination `%s' of total value %s\n",
   1489                 GNUNET_h2s (&issue->denom_hash.hash),
   1490                 TALER_amount2s (&issue->value));
   1491     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1492                 "New balance of denomination `%s' after withdraw is %s\n",
   1493                 GNUNET_h2s (&issue->denom_hash.hash),
   1494                 TALER_amount2s (&ds->dcd.denom_balance));
   1495     TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_escrowed),
   1496                           &TALER_ARL_USE_AB (total_escrowed),
   1497                           &issue->value);
   1498     TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_balance_risk),
   1499                           &TALER_ARL_USE_AB (coin_balance_risk),
   1500                           &issue->value);
   1501     TALER_ARL_amount_add (&ds->dcd.denom_balance,
   1502                           &ds->dcd.denom_balance,
   1503                           &issue->value);
   1504     TALER_ARL_amount_add (&ds->dcd.denom_risk,
   1505                           &ds->dcd.denom_risk,
   1506                           &issue->value);
   1507   }
   1508   return GNUNET_OK;
   1509 }
   1510 
   1511 
   1512 /**
   1513  * Check that the @a coin_pub is a known coin with a proper
   1514  * signature for denominatinon @a denom_pub. If not, report
   1515  * a loss of @a loss_potential.
   1516  *
   1517  * @param operation which operation is this about
   1518  * @param issue denomination key information about the coin
   1519  * @param rowid which row is this operation in
   1520  * @param coin_pub public key of a coin
   1521  * @param denom_pub expected denomination of the coin
   1522  * @param loss_potential how big could the loss be if the coin is
   1523  *        not properly signed
   1524  * @return database transaction status; on success
   1525  *  #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT, or
   1526  *  #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if the coin is not known to
   1527  *  the exchange at all (which was reported to the auditor database)
   1528  */
   1529 static enum GNUNET_DB_QueryStatus
   1530 check_known_coin (
   1531   const char *operation,
   1532   const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue,
   1533   uint64_t rowid,
   1534   const struct TALER_CoinSpendPublicKeyP *coin_pub,
   1535   const struct TALER_DenominationPublicKey *denom_pub,
   1536   const struct TALER_Amount *loss_potential)
   1537 {
   1538   struct TALER_CoinPublicInfo ci;
   1539   enum GNUNET_DB_QueryStatus qs;
   1540 
   1541   if (NULL == get_cached_history (coin_pub))
   1542   {
   1543     qs = check_coin_history (coin_pub,
   1544                              rowid,
   1545                              operation,
   1546                              &issue->value);
   1547     if (0 > qs)
   1548     {
   1549       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1550       return qs;
   1551     }
   1552     GNUNET_break (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != qs);
   1553   }
   1554 
   1555   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
   1556               "Checking denomination signature on %s\n",
   1557               TALER_B2S (coin_pub));
   1558   qs = TALER_EXCHANGEDB_get_known_coin (TALER_ARL_edb,
   1559                                         coin_pub,
   1560                                         &ci);
   1561   if (0 > qs)
   1562   {
   1563     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1564     return qs;
   1565   }
   1566   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
   1567   {
   1568     /* The exchange has no denomination signature on file for this
   1569        coin at all; that is the strongest evidence of a forged coin
   1570        and must be persisted, not merely logged. */
   1571     struct TALER_AUDITORDB_BadSigLosses bsl = {
   1572       .problem_row_id = rowid,
   1573       .operation = (char *) operation,
   1574       .loss = *loss_potential,
   1575       .operation_specific_pub = coin_pub->eddsa_pub
   1576     };
   1577     enum GNUNET_DB_QueryStatus rqs;
   1578 
   1579     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1580                 "Coin %s used in %s in row %llu is not known to the exchange\n",
   1581                 TALER_B2S (coin_pub),
   1582                 operation,
   1583                 (unsigned long long) rowid);
   1584     rqs = report_row_inconsistency ("known_coins",
   1585                                     rowid,
   1586                                     "coin not known to exchange");
   1587     if (0 > rqs)
   1588     {
   1589       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == rqs);
   1590       return rqs;
   1591     }
   1592     rqs = TALER_AUDITORDB_insert_bad_sig_losses (
   1593       TALER_ARL_adb,
   1594       &bsl);
   1595     if (0 > rqs)
   1596     {
   1597       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == rqs);
   1598       return rqs;
   1599     }
   1600     TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_irregular_loss),
   1601                           &TALER_ARL_USE_AB (coin_irregular_loss),
   1602                           loss_potential);
   1603     return qs;
   1604   }
   1605   if (GNUNET_YES !=
   1606       TALER_test_coin_valid (&ci,
   1607                              denom_pub))
   1608   {
   1609     struct TALER_AUDITORDB_BadSigLosses bsl = {
   1610       .problem_row_id = rowid,
   1611       .operation = (char *) operation,
   1612       .loss = *loss_potential,
   1613       .operation_specific_pub = coin_pub->eddsa_pub
   1614     };
   1615 
   1616     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1617                 "Failed to verify coin denomination signature in row %llu\n",
   1618                 (unsigned long long) rowid);
   1619     qs = TALER_AUDITORDB_insert_bad_sig_losses (
   1620       TALER_ARL_adb,
   1621       &bsl);
   1622     if (qs < 0)
   1623     {
   1624       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1625       return qs;
   1626     }
   1627     TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_irregular_loss),
   1628                           &TALER_ARL_USE_AB (coin_irregular_loss),
   1629                           loss_potential);
   1630   }
   1631   TALER_denom_sig_free (&ci.denom_sig);
   1632   return qs;
   1633 }
   1634 
   1635 
   1636 /**
   1637  * Update the denom balance in @a dso reducing it by
   1638  * @a amount_with_fee. If this is not possible, report
   1639  * an emergency.  Also updates the balance.
   1640  *
   1641  * @param dso denomination summary to update
   1642  * @param rowid responsible row (for logging)
   1643  * @param amount_with_fee amount to subtract
   1644  * @return transaction status
   1645  */
   1646 static enum GNUNET_DB_QueryStatus
   1647 reduce_denom_balance (struct DenominationSummary *dso,
   1648                       uint64_t rowid,
   1649                       const struct TALER_Amount *amount_with_fee)
   1650 {
   1651   struct TALER_Amount tmp;
   1652   enum GNUNET_DB_QueryStatus qs;
   1653 
   1654   if (TALER_ARL_SR_INVALID_NEGATIVE ==
   1655       TALER_ARL_amount_subtract_neg (&tmp,
   1656                                      &dso->dcd.denom_balance,
   1657                                      amount_with_fee))
   1658   {
   1659     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1660                 "Emergency: failed to reduce balance of denomination `%s' by %s\n",
   1661                 GNUNET_h2s (&dso->issue->denom_hash.hash),
   1662                 TALER_amount2s (amount_with_fee));
   1663     TALER_ARL_amount_add (&dso->dcd.denom_loss,
   1664                           &dso->dcd.denom_loss,
   1665                           amount_with_fee);
   1666     dso->report_emergency = true;
   1667   }
   1668   else
   1669   {
   1670     dso->dcd.denom_balance = tmp;
   1671   }
   1672   if (-1 == TALER_amount_cmp (&TALER_ARL_USE_AB (total_escrowed),
   1673                               amount_with_fee))
   1674   {
   1675     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1676                 "Failed to total escrow by %s\n",
   1677                 TALER_amount2s (amount_with_fee));
   1678     /* This can theoretically happen if for example the exchange
   1679        never issued any coins (i.e. escrow balance is zero), but
   1680        accepted a forged coin (i.e. emergency situation after
   1681        private key compromise). In that case, we cannot even
   1682        subtract the profit we make from the fee from the escrow
   1683        balance. Tested as part of test-auditor.sh, case #18 */
   1684     qs = report_amount_arithmetic_inconsistency (
   1685       "subtracting amount from escrow balance",
   1686       rowid,
   1687       &TALER_ARL_USE_AB (total_escrowed),
   1688       amount_with_fee,
   1689       0);
   1690     if (0 > qs)
   1691     {
   1692       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1693       return qs;
   1694     }
   1695   }
   1696   else
   1697   {
   1698     TALER_ARL_amount_subtract (&TALER_ARL_USE_AB (total_escrowed),
   1699                                &TALER_ARL_USE_AB (total_escrowed),
   1700                                amount_with_fee);
   1701   }
   1702   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1703               "New balance of denomination `%s' is %s\n",
   1704               GNUNET_h2s (&dso->issue->denom_hash.hash),
   1705               TALER_amount2s (&dso->dcd.denom_balance));
   1706   return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
   1707 }
   1708 
   1709 
   1710 /**
   1711  * Function called with details about coins that were melted, with the
   1712  * goal of auditing the refresh's execution.  Verifies the signature
   1713  * and updates our information about coins outstanding (the old coin's
   1714  * denomination has less, the fresh coins increased outstanding
   1715  * balances).
   1716  *
   1717  * @param cc closure
   1718  * @param rowid unique serial ID for the refresh session in our DB
   1719  * @param old_denom_pub denomination public key of @a coin_pub
   1720  * @param coin_pub public key of the coin
   1721  * @param coin_sig signature from the coin
   1722  * @param h_age_commitment hash of the age commitment for the coin
   1723  * @param amount_with_fee amount that was deposited including fee
   1724  * @param num_nds length of the @a new_denom_serials array
   1725  * @param new_denom_serials array of denomination serials of fresh coins
   1726  * @param rc what the refresh commitment
   1727  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop
   1728  */
   1729 static enum GNUNET_GenericReturnValue
   1730 refresh_session_cb (struct CoinContext *cc,
   1731                     uint64_t rowid,
   1732                     const struct TALER_DenominationPublicKey *old_denom_pub,
   1733                     const struct TALER_CoinSpendPublicKeyP *coin_pub,
   1734                     const struct TALER_CoinSpendSignatureP *coin_sig,
   1735                     const struct TALER_AgeCommitmentHashP *h_age_commitment,
   1736                     const struct TALER_Amount *amount_with_fee,
   1737                     size_t num_nds,
   1738                     uint64_t new_denom_serials[static num_nds],
   1739                     const struct TALER_RefreshCommitmentP *rc)
   1740 {
   1741   const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue;
   1742   struct DenominationSummary *dso;
   1743   enum GNUNET_DB_QueryStatus qs;
   1744   struct TALER_DenominationHashP h_denom_pub;
   1745 
   1746   GNUNET_assert (rowid >=
   1747                  TALER_ARL_USE_PP (coins_melt_serial_id)); /* should be monotonically increasing */
   1748   TALER_ARL_USE_PP (coins_melt_serial_id) = rowid + 1;
   1749   if ( (0 == num_nds) ||
   1750        (num_nds > TALER_MAX_COINS) )
   1751   {
   1752     /* the number of fresh coins sizes arrays on the stack below */
   1753     qs = report_row_inconsistency ("melt",
   1754                                    rowid,
   1755                                    "invalid number of fresh coins");
   1756     if (0 > qs)
   1757     {
   1758       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1759       cc->qs = qs;
   1760       return GNUNET_SYSERR;
   1761     }
   1762     return GNUNET_OK;
   1763   }
   1764   qs = TALER_ARL_get_denomination_info (old_denom_pub,
   1765                                         &issue,
   1766                                         &h_denom_pub);
   1767   if (0 > qs)
   1768   {
   1769     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1770     cc->qs = qs;
   1771     return GNUNET_SYSERR;
   1772   }
   1773   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
   1774   {
   1775     qs = report_row_inconsistency ("melt",
   1776                                    rowid,
   1777                                    "denomination key not found");
   1778     if (0 > qs)
   1779     {
   1780       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1781       cc->qs = qs;
   1782       return GNUNET_SYSERR;
   1783     }
   1784     return GNUNET_OK;
   1785   }
   1786   qs = check_known_coin ("melt",
   1787                          issue,
   1788                          rowid,
   1789                          coin_pub,
   1790                          old_denom_pub,
   1791                          amount_with_fee);
   1792   if (0 > qs)
   1793   {
   1794     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1795     cc->qs = qs;
   1796     return GNUNET_SYSERR;
   1797   }
   1798 
   1799   /* verify melt signature */
   1800   if (GNUNET_OK !=
   1801       TALER_wallet_melt_verify (amount_with_fee,
   1802                                 &issue->fees.refresh,
   1803                                 rc,
   1804                                 &h_denom_pub,
   1805                                 h_age_commitment,
   1806                                 coin_pub,
   1807                                 coin_sig))
   1808   {
   1809     struct TALER_AUDITORDB_BadSigLosses bsl = {
   1810       .problem_row_id = rowid,
   1811       .operation = (char *) "melt",
   1812       .loss = *amount_with_fee,
   1813       .operation_specific_pub = coin_pub->eddsa_pub
   1814     };
   1815 
   1816     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1817                 "Failed to verify coin melt signature in row %llu\n",
   1818                 (unsigned long long) rowid);
   1819     qs = TALER_AUDITORDB_insert_bad_sig_losses (
   1820       TALER_ARL_adb,
   1821       &bsl);
   1822     if (qs < 0)
   1823     {
   1824       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1825       cc->qs = qs;
   1826       return GNUNET_SYSERR;
   1827     }
   1828     TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_irregular_loss),
   1829                           &TALER_ARL_USE_AB (coin_irregular_loss),
   1830                           amount_with_fee);
   1831   }
   1832   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
   1833               "Melting coin %s in denomination `%s' of value %s\n",
   1834               TALER_B2S (coin_pub),
   1835               GNUNET_h2s (&issue->denom_hash.hash),
   1836               TALER_amount2s (amount_with_fee));
   1837 
   1838   {
   1839     struct TALER_Amount refresh_cost;
   1840     struct TALER_Amount amount_without_fee;
   1841     const struct TALER_EXCHANGEDB_DenominationKeyInformation *nis[num_nds];
   1842 
   1843     /* Check that the resulting amounts are consistent with the value being
   1844      refreshed by calculating the total refresh cost */
   1845     GNUNET_assert (GNUNET_OK ==
   1846                    TALER_amount_set_zero (amount_with_fee->currency,
   1847                                           &refresh_cost));
   1848     for (size_t i = 0; i < num_nds; i++)
   1849     {
   1850       qs = TALER_ARL_get_denomination_info_by_serial (new_denom_serials[i],
   1851                                                       &nis[i]);
   1852       if (0 > qs)
   1853       {
   1854         GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1855         cc->qs = qs;
   1856         return GNUNET_SYSERR;
   1857       }
   1858       if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
   1859       {
   1860         /* nis[i] was not set; we cannot audit this refresh at all */
   1861         qs = report_row_inconsistency ("refresh_reveal",
   1862                                        rowid,
   1863                                        "denomination key for fresh coin not found");
   1864         if (0 > qs)
   1865         {
   1866           GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1867           cc->qs = qs;
   1868           return GNUNET_SYSERR;
   1869         }
   1870         return GNUNET_OK;
   1871       }
   1872       /* update cost of refresh */
   1873       TALER_ARL_amount_add (&refresh_cost,
   1874                             &refresh_cost,
   1875                             &nis[i]->fees.withdraw);
   1876       TALER_ARL_amount_add (&refresh_cost,
   1877                             &refresh_cost,
   1878                             &nis[i]->value);
   1879     }
   1880 
   1881     /* compute contribution of old coin */
   1882     if (TALER_ARL_SR_POSITIVE !=
   1883         TALER_ARL_amount_subtract_neg (&amount_without_fee,
   1884                                        amount_with_fee,
   1885                                        &issue->fees.refresh))
   1886     {
   1887       /* Melt fee higher than contribution of melted coin; this makes
   1888          no sense (exchange should never have accepted the operation) */
   1889       qs = report_amount_arithmetic_inconsistency ("melt contribution vs. fee",
   1890                                                    rowid,
   1891                                                    amount_with_fee,
   1892                                                    &issue->fees.refresh,
   1893                                                    -1);
   1894       if (0 > qs)
   1895       {
   1896         GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1897         cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   1898         return GNUNET_SYSERR;
   1899       }
   1900       /* To continue, best assumption is the melted coin contributed
   1901          nothing (=> all withdrawal amounts will be counted as losses) */
   1902       GNUNET_assert (GNUNET_OK ==
   1903                      TALER_amount_set_zero (TALER_ARL_currency,
   1904                                             &amount_without_fee));
   1905     }
   1906 
   1907     /* check old coin covers complete expenses (of refresh operation) */
   1908     if (1 == TALER_amount_cmp (&refresh_cost,
   1909                                &amount_without_fee))
   1910     {
   1911       /* refresh_cost > amount_without_fee, which is bad (exchange lost) */
   1912       GNUNET_break_op (0);
   1913       qs = report_amount_arithmetic_inconsistency ("melt (cost)",
   1914                                                    rowid,
   1915                                                    &amount_without_fee, /* 'exchange' */
   1916                                                    &refresh_cost, /* 'auditor' */
   1917                                                    1);
   1918       if (0 > qs)
   1919       {
   1920         GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1921         cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   1922         return GNUNET_SYSERR;
   1923       }
   1924     }
   1925 
   1926     /* update outstanding denomination amounts for fresh coins withdrawn */
   1927     for (size_t i = 0; i < num_nds; i++)
   1928     {
   1929       const struct TALER_EXCHANGEDB_DenominationKeyInformation *ni
   1930         = nis[i];
   1931       struct DenominationSummary *dsi;
   1932 
   1933       dsi = get_denomination_summary (cc,
   1934                                       ni);
   1935       if (NULL == dsi)
   1936       {
   1937         qs = report_row_inconsistency ("refresh_reveal",
   1938                                        rowid,
   1939                                        "denomination key for fresh coin unknown to auditor");
   1940         if (0 > qs)
   1941         {
   1942           GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1943           cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   1944           return GNUNET_SYSERR;
   1945         }
   1946       }
   1947       else
   1948       {
   1949         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1950                     "Created fresh coin in denomination `%s' of value %s\n",
   1951                     GNUNET_h2s (&ni->denom_hash.hash),
   1952                     TALER_amount2s (&ni->value));
   1953         dsi->dcd.num_issued++;
   1954         TALER_ARL_amount_add (&dsi->dcd.denom_balance,
   1955                               &dsi->dcd.denom_balance,
   1956                               &ni->value);
   1957         TALER_ARL_amount_add (&dsi->dcd.denom_risk,
   1958                               &dsi->dcd.denom_risk,
   1959                               &ni->value);
   1960         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1961                     "New balance of denomination `%s' after refresh_reveal is %s\n",
   1962                     GNUNET_h2s (&ni->denom_hash.hash),
   1963                     TALER_amount2s (&dsi->dcd.denom_balance));
   1964         TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_escrowed),
   1965                               &TALER_ARL_USE_AB (total_escrowed),
   1966                               &ni->value);
   1967         TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_balance_risk),
   1968                               &TALER_ARL_USE_AB (coin_balance_risk),
   1969                               &ni->value);
   1970       }
   1971     }
   1972   }
   1973 
   1974   /* update old coin's denomination balance */
   1975   dso = get_denomination_summary (cc,
   1976                                   issue);
   1977   if (NULL == dso)
   1978   {
   1979     qs = report_row_inconsistency ("refresh_reveal",
   1980                                    rowid,
   1981                                    "denomination key for dirty coin unknown to auditor");
   1982     if (0 > qs)
   1983     {
   1984       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1985       cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   1986       return GNUNET_SYSERR;
   1987     }
   1988   }
   1989   else
   1990   {
   1991     qs = reduce_denom_balance (dso,
   1992                                rowid,
   1993                                amount_with_fee);
   1994     if (0 > qs)
   1995     {
   1996       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1997       cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   1998       return GNUNET_SYSERR;
   1999     }
   2000   }
   2001 
   2002   /* update global melt fees */
   2003   TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_melt_fee_revenue),
   2004                         &TALER_ARL_USE_AB (coin_melt_fee_revenue),
   2005                         &issue->fees.refresh);
   2006   return GNUNET_OK;
   2007 }
   2008 
   2009 
   2010 /**
   2011  * Function called with details about deposits that have been made,
   2012  * with the goal of auditing the deposit's execution.
   2013  *
   2014  * @param cc closure
   2015  * @param rowid unique serial ID for the deposit in our DB
   2016  * @param exchange_timestamp when did the exchange get the deposit
   2017  * @param deposit deposit details
   2018  * @param denom_pub denomination public key of @a coin_pub
   2019  * @param done flag set if the deposit was already executed (or not)
   2020  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop
   2021  */
   2022 static enum GNUNET_GenericReturnValue
   2023 deposit_cb (struct CoinContext *cc,
   2024             uint64_t rowid,
   2025             struct GNUNET_TIME_Timestamp exchange_timestamp,
   2026             const struct TALER_EXCHANGEDB_Deposit *deposit,
   2027             const struct TALER_DenominationPublicKey *denom_pub,
   2028             bool done)
   2029 {
   2030   const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue;
   2031   struct DenominationSummary *ds;
   2032   enum GNUNET_DB_QueryStatus qs;
   2033 
   2034   (void) done;
   2035   (void) exchange_timestamp;
   2036   GNUNET_assert (rowid >=
   2037                  TALER_ARL_USE_PP (coins_deposit_serial_id)); /* should be monotonically increasing */
   2038   TALER_ARL_USE_PP (coins_deposit_serial_id) = rowid + 1;
   2039 
   2040   qs = TALER_ARL_get_denomination_info (denom_pub,
   2041                                         &issue,
   2042                                         NULL);
   2043   if (0 > qs)
   2044   {
   2045     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2046     cc->qs = qs;
   2047     return GNUNET_SYSERR;
   2048   }
   2049   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
   2050   {
   2051     qs = report_row_inconsistency ("deposits",
   2052                                    rowid,
   2053                                    "denomination key not found");
   2054     if (0 > qs)
   2055     {
   2056       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2057       cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   2058       return GNUNET_SYSERR;
   2059     }
   2060     return GNUNET_OK;
   2061   }
   2062   if (GNUNET_TIME_timestamp_cmp (deposit->refund_deadline,
   2063                                  >,
   2064                                  deposit->wire_deadline))
   2065   {
   2066     qs = report_row_inconsistency ("deposits",
   2067                                    rowid,
   2068                                    "refund deadline past wire deadline");
   2069     if (0 > qs)
   2070     {
   2071       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2072       cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   2073       return GNUNET_SYSERR;
   2074     }
   2075   }
   2076   qs = check_known_coin ("deposit",
   2077                          issue,
   2078                          rowid,
   2079                          &deposit->coin.coin_pub,
   2080                          denom_pub,
   2081                          &deposit->amount_with_fee);
   2082   if (0 > qs)
   2083   {
   2084     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2085     cc->qs = qs;
   2086     return GNUNET_SYSERR;
   2087   }
   2088 
   2089   /* Verify deposit signature */
   2090   {
   2091     struct TALER_MerchantWireHashP h_wire;
   2092     struct TALER_DenominationHashP h_denom_pub;
   2093 
   2094     TALER_denom_pub_hash (denom_pub,
   2095                           &h_denom_pub);
   2096     TALER_merchant_wire_signature_hash (deposit->receiver_wire_account,
   2097                                         &deposit->wire_salt,
   2098                                         &h_wire);
   2099     /* NOTE: This is one of the operations we might eventually
   2100        want to do in parallel in the background to improve
   2101        auditor performance! */
   2102     if (GNUNET_OK !=
   2103         TALER_wallet_deposit_verify (&deposit->amount_with_fee,
   2104                                      &issue->fees.deposit,
   2105                                      &h_wire,
   2106                                      &deposit->h_contract_terms,
   2107                                      deposit->no_wallet_data_hash
   2108                                      ? NULL
   2109                                      : &deposit->wallet_data_hash,
   2110                                      &deposit->coin.h_age_commitment,
   2111                                      &deposit->h_policy,
   2112                                      &h_denom_pub,
   2113                                      deposit->timestamp,
   2114                                      &deposit->merchant_pub,
   2115                                      deposit->refund_deadline,
   2116                                      &deposit->coin.coin_pub,
   2117                                      &deposit->csig))
   2118     {
   2119       struct TALER_AUDITORDB_BadSigLosses bsl = {
   2120         .problem_row_id = rowid,
   2121         .operation = (char *) "deposit",
   2122         .loss = deposit->amount_with_fee,
   2123         .operation_specific_pub = deposit->coin.coin_pub.eddsa_pub
   2124       };
   2125 
   2126       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   2127                   "Failed to verify coin deposit signature in row %llu\n",
   2128                   (unsigned long long) rowid);
   2129       qs = TALER_AUDITORDB_insert_bad_sig_losses (
   2130         TALER_ARL_adb,
   2131         &bsl);
   2132       if (0 > qs)
   2133       {
   2134         GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2135         cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   2136         return GNUNET_SYSERR;
   2137       }
   2138       TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_irregular_loss),
   2139                             &TALER_ARL_USE_AB (coin_irregular_loss),
   2140                             &deposit->amount_with_fee);
   2141       return GNUNET_OK;
   2142     }
   2143   }
   2144   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   2145               "Deposited coin %s in denomination `%s' of value %s\n",
   2146               TALER_B2S (&deposit->coin.coin_pub),
   2147               GNUNET_h2s (&issue->denom_hash.hash),
   2148               TALER_amount2s (&deposit->amount_with_fee));
   2149 
   2150   /* update old coin's denomination balance */
   2151   ds = get_denomination_summary (cc,
   2152                                  issue);
   2153   if (NULL == ds)
   2154   {
   2155     qs = report_row_inconsistency ("deposit",
   2156                                    rowid,
   2157                                    "denomination key for deposited coin unknown to auditor");
   2158     if (0 > qs)
   2159     {
   2160       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2161       cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   2162       return GNUNET_SYSERR;
   2163     }
   2164   }
   2165   else
   2166   {
   2167     qs = reduce_denom_balance (ds,
   2168                                rowid,
   2169                                &deposit->amount_with_fee);
   2170     if (0 > qs)
   2171     {
   2172       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2173       cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   2174       return GNUNET_SYSERR;
   2175     }
   2176   }
   2177 
   2178   /* update global deposit fees */
   2179   TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_deposit_fee_revenue),
   2180                         &TALER_ARL_USE_AB (coin_deposit_fee_revenue),
   2181                         &issue->fees.deposit);
   2182   return GNUNET_OK;
   2183 }
   2184 
   2185 
   2186 /**
   2187  * Function called with details about coins that were refunding,
   2188  * with the goal of auditing the refund's execution.  Adds the
   2189  * refunded amount back to the outstanding balance of the respective
   2190  * denomination.
   2191  *
   2192  * @param cc closure
   2193  * @param rowid unique serial ID for the refund in our DB
   2194  * @param denom_pub denomination public key of @a coin_pub
   2195  * @param coin_pub public key of the coin
   2196  * @param merchant_pub public key of the merchant
   2197  * @param merchant_sig signature of the merchant
   2198  * @param h_contract_terms hash of the proposal data known to merchant and customer
   2199  * @param rtransaction_id refund transaction ID chosen by the merchant
   2200  * @param full_refund true if the refunds total up to the entire deposited value
   2201  * @param amount_with_fee amount that was deposited including fee
   2202  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop
   2203  */
   2204 static enum GNUNET_GenericReturnValue
   2205 refund_cb (struct CoinContext *cc,
   2206            uint64_t rowid,
   2207            const struct TALER_DenominationPublicKey *denom_pub,
   2208            const struct TALER_CoinSpendPublicKeyP *coin_pub,
   2209            const struct TALER_MerchantPublicKeyP *merchant_pub,
   2210            const struct TALER_MerchantSignatureP *merchant_sig,
   2211            const struct TALER_PrivateContractHashP *h_contract_terms,
   2212            uint64_t rtransaction_id,
   2213            bool full_refund,
   2214            const struct TALER_Amount *amount_with_fee)
   2215 {
   2216   const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue;
   2217   struct DenominationSummary *ds;
   2218   struct TALER_Amount amount_without_fee;
   2219   enum GNUNET_DB_QueryStatus qs;
   2220 
   2221   GNUNET_assert (rowid >= TALER_ARL_USE_PP (coins_refund_serial_id)); /* should be monotonically increasing */
   2222   TALER_ARL_USE_PP (coins_refund_serial_id) = rowid + 1;
   2223 
   2224   qs = TALER_ARL_get_denomination_info (denom_pub,
   2225                                         &issue,
   2226                                         NULL);
   2227   if (0 > qs)
   2228   {
   2229     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2230     cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   2231     return GNUNET_SYSERR;
   2232   }
   2233   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
   2234   {
   2235     qs = report_row_inconsistency ("refunds",
   2236                                    rowid,
   2237                                    "denomination key not found");
   2238     if (0 > qs)
   2239     {
   2240       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2241       cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   2242       return GNUNET_SYSERR;
   2243     }
   2244     return GNUNET_OK;
   2245   }
   2246 
   2247   /* verify refund signature */
   2248   if (GNUNET_OK !=
   2249       TALER_merchant_refund_verify (coin_pub,
   2250                                     h_contract_terms,
   2251                                     rtransaction_id,
   2252                                     amount_with_fee,
   2253                                     merchant_pub,
   2254                                     merchant_sig))
   2255   {
   2256     struct TALER_AUDITORDB_BadSigLosses bsl = {
   2257       .problem_row_id = rowid,
   2258       .operation = (char *) "refund",
   2259       .loss = *amount_with_fee,
   2260       .operation_specific_pub = coin_pub->eddsa_pub
   2261     };
   2262 
   2263     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   2264                 "Failed to verify merchant refund signature in row %llu\n",
   2265                 (unsigned long long) rowid);
   2266     qs = TALER_AUDITORDB_insert_bad_sig_losses (
   2267       TALER_ARL_adb,
   2268       &bsl);
   2269     if (0 > qs)
   2270     {
   2271       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2272       cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   2273       return GNUNET_SYSERR;
   2274     }
   2275     TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_irregular_loss),
   2276                           &TALER_ARL_USE_AB (coin_irregular_loss),
   2277                           amount_with_fee);
   2278     return GNUNET_OK;
   2279   }
   2280 
   2281   if (TALER_ARL_SR_INVALID_NEGATIVE ==
   2282       TALER_ARL_amount_subtract_neg (&amount_without_fee,
   2283                                      amount_with_fee,
   2284                                      &issue->fees.refund))
   2285   {
   2286     qs = report_amount_arithmetic_inconsistency ("refund (fee)",
   2287                                                  rowid,
   2288                                                  amount_with_fee,
   2289                                                  &issue->fees.refund,
   2290                                                  -1);
   2291     if (0 > qs)
   2292     {
   2293       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2294       cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   2295       return GNUNET_SYSERR;
   2296     }
   2297     return GNUNET_OK;
   2298   }
   2299 
   2300   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   2301               "Refunding coin %s in denomination `%s' value %s\n",
   2302               TALER_B2S (coin_pub),
   2303               GNUNET_h2s (&issue->denom_hash.hash),
   2304               TALER_amount2s (amount_with_fee));
   2305 
   2306   /* update coin's denomination balance */
   2307   ds = get_denomination_summary (cc,
   2308                                  issue);
   2309   if (NULL == ds)
   2310   {
   2311     qs = report_row_inconsistency ("refund",
   2312                                    rowid,
   2313                                    "denomination key for refunded coin unknown to auditor");
   2314     if (0 > qs)
   2315     {
   2316       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2317       cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   2318       return GNUNET_SYSERR;
   2319     }
   2320   }
   2321   else
   2322   {
   2323     TALER_ARL_amount_add (&ds->dcd.denom_balance,
   2324                           &ds->dcd.denom_balance,
   2325                           &amount_without_fee);
   2326     TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_escrowed),
   2327                           &TALER_ARL_USE_AB (total_escrowed),
   2328                           &amount_without_fee);
   2329     /* A coin of a denomination whose deposit period is over cannot be
   2330        recouped (#TEH_recoup_check_keys() refuses it), so crediting it back
   2331        does not put anything at risk again; see
   2332        #release_denomination_risk(). */
   2333     if (! is_past_deposit_grace (issue))
   2334     {
   2335       TALER_ARL_amount_add (&ds->dcd.denom_risk,
   2336                             &ds->dcd.denom_risk,
   2337                             &amount_without_fee);
   2338       TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_balance_risk),
   2339                             &TALER_ARL_USE_AB (coin_balance_risk),
   2340                             &amount_without_fee);
   2341     }
   2342     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   2343                 "New balance of denomination `%s' after refund is %s\n",
   2344                 GNUNET_h2s (&issue->denom_hash.hash),
   2345                 TALER_amount2s (&ds->dcd.denom_balance));
   2346   }
   2347   /* update total refund fee balance */
   2348   TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_refund_fee_revenue),
   2349                         &TALER_ARL_USE_AB (coin_refund_fee_revenue),
   2350                         &issue->fees.refund);
   2351   if (full_refund)
   2352   {
   2353     TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_deposit_fee_loss),
   2354                           &TALER_ARL_USE_AB (coin_deposit_fee_loss),
   2355                           &issue->fees.deposit);
   2356   }
   2357   return GNUNET_OK;
   2358 }
   2359 
   2360 
   2361 /**
   2362  * Function called with details about purse refunds that have been made, with
   2363  * the goal of auditing the purse refund's execution.
   2364  *
   2365  * @param cc closure
   2366  * @param rowid row of the purse-refund
   2367  * @param amount_with_fee amount of the deposit into the purse
   2368  * @param coin_pub coin that is to be refunded the @a given amount_with_fee
   2369  * @param denom_pub denomination of @a coin_pub
   2370  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop
   2371  */
   2372 static enum GNUNET_GenericReturnValue
   2373 purse_refund_coin_cb (
   2374   struct CoinContext *cc,
   2375   uint64_t rowid,
   2376   const struct TALER_Amount *amount_with_fee,
   2377   const struct TALER_CoinSpendPublicKeyP *coin_pub,
   2378   const struct TALER_DenominationPublicKey *denom_pub)
   2379 {
   2380   const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue;
   2381   struct DenominationSummary *ds;
   2382   enum GNUNET_DB_QueryStatus qs;
   2383 
   2384   qs = TALER_ARL_get_denomination_info (denom_pub,
   2385                                         &issue,
   2386                                         NULL);
   2387   if (0 > qs)
   2388   {
   2389     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2390     cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   2391     return GNUNET_SYSERR;
   2392   }
   2393   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
   2394   {
   2395     qs = report_row_inconsistency ("purse-refunds",
   2396                                    rowid,
   2397                                    "denomination key not found");
   2398     if (0 > qs)
   2399     {
   2400       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2401       cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   2402       return GNUNET_SYSERR;
   2403     }
   2404     return GNUNET_OK;
   2405   }
   2406   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   2407               "Aborted purse-deposit of coin %s in denomination `%s' value %s\n",
   2408               TALER_B2S (coin_pub),
   2409               GNUNET_h2s (&issue->denom_hash.hash),
   2410               TALER_amount2s (amount_with_fee));
   2411 
   2412   /* update coin's denomination balance */
   2413   ds = get_denomination_summary (cc,
   2414                                  issue);
   2415   if (NULL == ds)
   2416   {
   2417     qs = report_row_inconsistency ("purse-refund",
   2418                                    rowid,
   2419                                    "denomination key for purse-refunded coin unknown to auditor");
   2420     if (0 > qs)
   2421     {
   2422       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2423       cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   2424       return GNUNET_SYSERR;
   2425     }
   2426   }
   2427   else
   2428   {
   2429     TALER_ARL_amount_add (&ds->dcd.denom_balance,
   2430                           &ds->dcd.denom_balance,
   2431                           amount_with_fee);
   2432     TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_escrowed),
   2433                           &TALER_ARL_USE_AB (total_escrowed),
   2434                           amount_with_fee);
   2435     /* A coin of a denomination whose deposit period is over cannot be
   2436        recouped (#TEH_recoup_check_keys() refuses it), so crediting it back
   2437        does not put anything at risk again; see
   2438        #release_denomination_risk(). */
   2439     if (! is_past_deposit_grace (issue))
   2440     {
   2441       TALER_ARL_amount_add (&ds->dcd.denom_risk,
   2442                             &ds->dcd.denom_risk,
   2443                             amount_with_fee);
   2444       TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_balance_risk),
   2445                             &TALER_ARL_USE_AB (coin_balance_risk),
   2446                             amount_with_fee);
   2447     }
   2448     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   2449                 "New balance of denomination `%s' after purse-refund is %s\n",
   2450                 GNUNET_h2s (&issue->denom_hash.hash),
   2451                 TALER_amount2s (&ds->dcd.denom_balance));
   2452   }
   2453   /* update total deposit fee balance */
   2454   TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_deposit_fee_loss),
   2455                         &TALER_ARL_USE_AB (coin_deposit_fee_loss),
   2456                         &issue->fees.deposit);
   2457 
   2458   return GNUNET_OK;
   2459 }
   2460 
   2461 
   2462 /**
   2463  * Function called with details about a purse that was refunded.  Adds the
   2464  * refunded amounts back to the outstanding balance of the respective
   2465  * denominations.
   2466  *
   2467  * @param cc closure
   2468  * @param rowid unique serial ID for the refund in our DB
   2469  * @param purse_pub public key of the purse
   2470  * @param reserve_pub public key of the targeted reserve (ignored)
   2471  * @param val targeted amount to be in the reserve (ignored)
   2472  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop
   2473  */
   2474 static enum GNUNET_GenericReturnValue
   2475 purse_refund_cb (struct CoinContext *cc,
   2476                  uint64_t rowid,
   2477                  const struct TALER_PurseContractPublicKeyP *purse_pub,
   2478                  const struct TALER_ReservePublicKeyP *reserve_pub,
   2479                  const struct TALER_Amount *val)
   2480 {
   2481   enum GNUNET_DB_QueryStatus qs;
   2482 
   2483   (void) val; /* irrelevant on refund */
   2484   (void) reserve_pub; /* irrelevant, may even be NULL */
   2485   GNUNET_assert (rowid >=
   2486                  TALER_ARL_USE_PP (coins_purse_refunds_serial_id)); /* should be monotonically increasing */
   2487   qs = TALER_EXCHANGEDB_iterate_purse_deposits_by_purse (
   2488     TALER_ARL_edb,
   2489     purse_pub,
   2490     &purse_refund_coin_cb,
   2491     cc);
   2492   if (qs < 0)
   2493   {
   2494     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2495     cc->qs = qs;
   2496     return GNUNET_SYSERR;
   2497   }
   2498   if (0 > cc->qs)
   2499     return GNUNET_SYSERR; /* #purse_refund_coin_cb() failed */
   2500   TALER_ARL_USE_PP (coins_purse_refunds_serial_id) = rowid + 1;
   2501   return GNUNET_OK;
   2502 }
   2503 
   2504 
   2505 /**
   2506  * Check that the recoup operation was properly initiated by a coin
   2507  * and update the denomination's losses accordingly.
   2508  *
   2509  * The caller must have verified the wallet's signature authorizing the
   2510  * recoup (which differs between recoup and recoup-refresh) and must not
   2511  * call this function if that signature was invalid, as this function
   2512  * books the recoup as legitimate.
   2513  *
   2514  * @param cc the context with details about the coin
   2515  * @param operation name of the operation matching @a rowid
   2516  * @param rowid row identifier used to uniquely identify the recoup operation
   2517  * @param amount how much should be added back to the reserve
   2518  * @param coin public information about the coin
   2519  * @param denom_pub public key of the denomionation of @a coin
   2520  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop
   2521  */
   2522 static enum GNUNET_GenericReturnValue
   2523 check_recoup (struct CoinContext *cc,
   2524               const char *operation,
   2525               uint64_t rowid,
   2526               const struct TALER_Amount *amount,
   2527               const struct TALER_CoinPublicInfo *coin,
   2528               const struct TALER_DenominationPublicKey *denom_pub)
   2529 {
   2530   struct DenominationSummary *ds;
   2531   enum GNUNET_DB_QueryStatus qs;
   2532   const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue;
   2533 
   2534   if (GNUNET_OK !=
   2535       TALER_test_coin_valid (coin,
   2536                              denom_pub))
   2537   {
   2538     struct TALER_AUDITORDB_BadSigLosses bsl = {
   2539       .problem_row_id = rowid,
   2540       .operation = (char *) operation,
   2541       .loss = *amount,
   2542       .operation_specific_pub = coin->coin_pub.eddsa_pub
   2543     };
   2544 
   2545     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   2546                 "Failed to verify coin signature in row %llu\n",
   2547                 (unsigned long long) rowid);
   2548     qs = TALER_AUDITORDB_insert_bad_sig_losses (
   2549       TALER_ARL_adb,
   2550       &bsl);
   2551 
   2552     if (0 > qs)
   2553     {
   2554       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2555       cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   2556       return GNUNET_SYSERR;
   2557     }
   2558     TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_irregular_loss),
   2559                           &TALER_ARL_USE_AB (coin_irregular_loss),
   2560                           amount);
   2561   }
   2562   qs = TALER_ARL_get_denomination_info_by_hash (&coin->denom_pub_hash,
   2563                                                 &issue);
   2564   if (0 > qs)
   2565   {
   2566     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2567     cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   2568     return GNUNET_SYSERR;
   2569   }
   2570   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
   2571   {
   2572     qs = report_row_inconsistency (operation,
   2573                                    rowid,
   2574                                    "denomination key not found");
   2575     if (0 > qs)
   2576     {
   2577       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2578       cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   2579       return GNUNET_SYSERR;
   2580     }
   2581     return GNUNET_OK;
   2582   }
   2583   qs = check_known_coin (operation,
   2584                          issue,
   2585                          rowid,
   2586                          &coin->coin_pub,
   2587                          denom_pub,
   2588                          amount);
   2589   if (0 > qs)
   2590   {
   2591     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2592     cc->qs = qs;
   2593     return GNUNET_SYSERR;
   2594   }
   2595   ds = get_denomination_summary (cc,
   2596                                  issue);
   2597   if (NULL == ds)
   2598   {
   2599     qs = report_row_inconsistency ("recoup",
   2600                                    rowid,
   2601                                    "denomination key for recouped coin unknown to auditor");
   2602     if (0 > qs)
   2603     {
   2604       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2605       cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   2606       return GNUNET_SYSERR;
   2607     }
   2608   }
   2609   else
   2610   {
   2611     if (! ds->was_revoked)
   2612     {
   2613       struct TALER_AUDITORDB_BadSigLosses bsldnr = {
   2614         .problem_row_id = rowid,
   2615         .operation = (char *) operation,
   2616         .loss = *amount,
   2617         .operation_specific_pub = coin->coin_pub.eddsa_pub
   2618       };
   2619 
   2620       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   2621                   "Recoup allowed on non-revoked denomination in row %llu\n",
   2622                   (unsigned long long) rowid);
   2623       qs = TALER_AUDITORDB_insert_bad_sig_losses (
   2624         TALER_ARL_adb,
   2625         &bsldnr);
   2626 
   2627       if (qs < 0)
   2628       {
   2629         GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2630         cc->qs = qs;
   2631         return GNUNET_SYSERR;
   2632       }
   2633       TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_irregular_loss),
   2634                             &TALER_ARL_USE_AB (coin_irregular_loss),
   2635                             amount);
   2636     }
   2637     TALER_ARL_amount_add (&ds->dcd.recoup_loss,
   2638                           &ds->dcd.recoup_loss,
   2639                           amount);
   2640     TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_recoup_loss),
   2641                           &TALER_ARL_USE_AB (total_recoup_loss),
   2642                           amount);
   2643   }
   2644   return GNUNET_OK;
   2645 }
   2646 
   2647 
   2648 /**
   2649  * Function called about recoups the exchange has to perform.
   2650  *
   2651  * @param cc a `struct CoinContext *`
   2652  * @param rowid row identifier used to uniquely identify the recoup operation
   2653  * @param timestamp when did we receive the recoup request
   2654  * @param amount how much should be added back to the reserve
   2655  * @param reserve_pub public key of the reserve
   2656  * @param coin public information about the coin
   2657  * @param denom_pub denomination public key of @a coin
   2658  * @param coin_sig signature with @e coin_pub of type #TALER_SIGNATURE_WALLET_COIN_RECOUP
   2659  * @param coin_blind blinding factor used to blind the coin
   2660  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop
   2661  */
   2662 static enum GNUNET_GenericReturnValue
   2663 recoup_cb (struct CoinContext *cc,
   2664            uint64_t rowid,
   2665            struct GNUNET_TIME_Timestamp timestamp,
   2666            const struct TALER_Amount *amount,
   2667            const struct TALER_ReservePublicKeyP *reserve_pub,
   2668            const struct TALER_CoinPublicInfo *coin,
   2669            const struct TALER_DenominationPublicKey *denom_pub,
   2670            const struct TALER_CoinSpendSignatureP *coin_sig,
   2671            const union GNUNET_CRYPTO_BlindingSecretP *coin_blind)
   2672 {
   2673   enum GNUNET_DB_QueryStatus qs;
   2674 
   2675   GNUNET_assert (rowid >= TALER_ARL_USE_PP (coins_recoup_serial_id)); /* should be monotonically increasing */
   2676   TALER_ARL_USE_PP (coins_recoup_serial_id) = rowid + 1;
   2677   (void) timestamp;
   2678   (void) reserve_pub;
   2679   if (GNUNET_OK !=
   2680       TALER_wallet_recoup_verify (&coin->denom_pub_hash,
   2681                                   coin_blind,
   2682                                   &coin->coin_pub,
   2683                                   coin_sig))
   2684   {
   2685     struct TALER_AUDITORDB_BadSigLosses bsl = {
   2686       .problem_row_id = rowid,
   2687       .operation = (char *) "recoup",
   2688       .loss = *amount,
   2689       .operation_specific_pub = coin->coin_pub.eddsa_pub
   2690     };
   2691 
   2692     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   2693                 "Failed to verify recoup signature in row %llu\n",
   2694                 (unsigned long long) rowid);
   2695     qs = TALER_AUDITORDB_insert_bad_sig_losses (
   2696       TALER_ARL_adb,
   2697       &bsl);
   2698     if (qs < 0)
   2699     {
   2700       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2701       cc->qs = qs;
   2702       return GNUNET_SYSERR;
   2703     }
   2704     TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_irregular_loss),
   2705                           &TALER_ARL_USE_AB (coin_irregular_loss),
   2706                           amount);
   2707     return GNUNET_OK;
   2708   }
   2709   return check_recoup (cc,
   2710                        "recoup",
   2711                        rowid,
   2712                        amount,
   2713                        coin,
   2714                        denom_pub);
   2715 }
   2716 
   2717 
   2718 /**
   2719  * Function called about recoups on refreshed coins the exchange had to
   2720  * perform. Updates the denomination balance(s). Does not change the
   2721  * coin balances, as those are already updated when we check the coin
   2722  * history.
   2723  *
   2724  * @param cc a `struct CoinContext *`
   2725  * @param rowid row identifier used to uniquely identify the recoup operation
   2726  * @param timestamp when did we receive the recoup request
   2727  * @param amount how much should be added back to the old coin
   2728  * @param old_coin_pub original coin that was refreshed to create @a coin
   2729  * @param old_denom_pub_hash hash of the public key of @a old_coin_pub
   2730  * @param coin public information about the fresh coin
   2731  * @param denom_pub denomination public key of @a coin
   2732  * @param coin_sig signature with @e coin_pub of type #TALER_SIGNATURE_WALLET_COIN_RECOUP
   2733  * @param coin_blind blinding factor used to blind the coin
   2734  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop
   2735  */
   2736 static enum GNUNET_GenericReturnValue
   2737 recoup_refresh_cb (struct CoinContext *cc,
   2738                    uint64_t rowid,
   2739                    struct GNUNET_TIME_Timestamp timestamp,
   2740                    const struct TALER_Amount *amount,
   2741                    const struct TALER_CoinSpendPublicKeyP *old_coin_pub,
   2742                    const struct TALER_DenominationHashP *old_denom_pub_hash,
   2743                    const struct TALER_CoinPublicInfo *coin,
   2744                    const struct TALER_DenominationPublicKey *denom_pub,
   2745                    const struct TALER_CoinSpendSignatureP *coin_sig,
   2746                    const union GNUNET_CRYPTO_BlindingSecretP *coin_blind)
   2747 {
   2748   const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue;
   2749   enum GNUNET_DB_QueryStatus qs;
   2750 
   2751   (void) timestamp;
   2752   (void) old_coin_pub;
   2753   GNUNET_assert (rowid >= TALER_ARL_USE_PP (coins_recoup_refresh_serial_id)); /* should be monotonically increasing */
   2754   TALER_ARL_USE_PP (coins_recoup_refresh_serial_id) = rowid + 1;
   2755   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
   2756               "Recoup-refresh amount is %s\n",
   2757               TALER_amount2s (amount));
   2758 
   2759   /* Update old coin's denomination balance summary */
   2760   qs = TALER_ARL_get_denomination_info_by_hash (old_denom_pub_hash,
   2761                                                 &issue);
   2762   if (qs < 0)
   2763   {
   2764     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2765     cc->qs = qs;
   2766     return GNUNET_SYSERR;
   2767   }
   2768   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
   2769   {
   2770     qs = report_row_inconsistency ("refresh-recoup",
   2771                                    rowid,
   2772                                    "denomination key of old coin not found");
   2773     if (qs < 0)
   2774     {
   2775       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2776       cc->qs = qs;
   2777       return GNUNET_SYSERR;
   2778     }
   2779     /* nothing to update without the denomination */
   2780     return GNUNET_OK;
   2781   }
   2782 
   2783   {
   2784     struct DenominationSummary *dso;
   2785 
   2786     dso = get_denomination_summary (cc,
   2787                                     issue);
   2788     if (NULL == dso)
   2789     {
   2790       qs = report_row_inconsistency ("refresh_reveal",
   2791                                      rowid,
   2792                                      "denomination key for old coin unknown to auditor");
   2793       if (qs < 0)
   2794       {
   2795         GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2796         cc->qs = qs;
   2797         return GNUNET_SYSERR;
   2798       }
   2799     }
   2800     else
   2801     {
   2802       TALER_ARL_amount_add (&dso->dcd.denom_balance,
   2803                             &dso->dcd.denom_balance,
   2804                             amount);
   2805       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   2806                   "New balance of denomination `%s' after refresh-recoup is %s\n",
   2807                   GNUNET_h2s (&issue->denom_hash.hash),
   2808                   TALER_amount2s (&dso->dcd.denom_balance));
   2809     }
   2810   }
   2811 
   2812   if (GNUNET_OK !=
   2813       TALER_wallet_recoup_refresh_verify (&coin->denom_pub_hash,
   2814                                           coin_blind,
   2815                                           &coin->coin_pub,
   2816                                           coin_sig))
   2817   {
   2818     struct TALER_AUDITORDB_BadSigLosses bsl = {
   2819       .problem_row_id = rowid,
   2820       .operation = (char *) "recoup-refresh",
   2821       .loss = *amount,
   2822       .operation_specific_pub = coin->coin_pub.eddsa_pub
   2823     };
   2824 
   2825     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   2826                 "Failed to verify recoup-refresh signature in row %llu\n",
   2827                 (unsigned long long) rowid);
   2828     qs = TALER_AUDITORDB_insert_bad_sig_losses (
   2829       TALER_ARL_adb,
   2830       &bsl);
   2831     if (qs < 0)
   2832     {
   2833       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2834       cc->qs = qs;
   2835       return GNUNET_SYSERR;
   2836     }
   2837     TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_irregular_loss),
   2838                           &TALER_ARL_USE_AB (coin_irregular_loss),
   2839                           amount);
   2840     return GNUNET_OK;
   2841   }
   2842   return check_recoup (cc,
   2843                        "recoup-refresh",
   2844                        rowid,
   2845                        amount,
   2846                        coin,
   2847                        denom_pub);
   2848 }
   2849 
   2850 
   2851 /**
   2852  * Function called with the results of iterate_denomination_info(),
   2853  * or directly (!).  Used to check that we correctly signed the
   2854  * denomination and to warn if there are denominations not approved
   2855  * by this auditor.
   2856  *
   2857  * @param[in,out] iqs closure, pointer to `enum GNUNET_DB_QueryStatus`;
   2858  *           a failure is latched here, we never overwrite a failure
   2859  *           of a previous invocation with a success of this one
   2860  * @param denom_serial row ID of the denominations table of the exchange DB
   2861  * @param denom_pub public key, sometimes NULL (!)
   2862  * @param issue issuing information with value, fees and other info about the denomination.
   2863  */
   2864 static void
   2865 check_denomination (
   2866   enum GNUNET_DB_QueryStatus *iqs,
   2867   uint64_t denom_serial,
   2868   const struct TALER_DenominationPublicKey *denom_pub,
   2869   const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue)
   2870 {
   2871   enum GNUNET_DB_QueryStatus qs;
   2872   struct TALER_AuditorSignatureP auditor_sig;
   2873 
   2874   (void) denom_pub;
   2875   if (0 > *iqs)
   2876     return; /* already failed, do not do more work */
   2877   qs = TALER_EXCHANGEDB_get_auditor_denom_sig (TALER_ARL_edb,
   2878                                                &issue->denom_hash,
   2879                                                &TALER_ARL_auditor_pub,
   2880                                                &auditor_sig);
   2881   if (0 > qs)
   2882   {
   2883     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2884     *iqs = qs;
   2885     return;
   2886   }
   2887   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
   2888   {
   2889     struct TALER_AUDITORDB_DenominationsWithoutSigs dws = {
   2890       .denompub_h = issue->denom_hash,
   2891       .start_time = issue->start.abs_time,
   2892       .end_time = issue->expire_legal.abs_time,
   2893       .value = issue->value
   2894     };
   2895 
   2896     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   2897                 "Encountered denomination `%s' (%s) valid from %s (%llu-%llu) that this auditor is not auditing!\n",
   2898                 GNUNET_h2s (&issue->denom_hash.hash),
   2899                 TALER_amount2s (&issue->value),
   2900                 GNUNET_TIME_timestamp2s (issue->start),
   2901                 (unsigned long long) issue->start.abs_time.abs_value_us,
   2902                 (unsigned long long) issue->expire_legal.abs_time.abs_value_us);
   2903     qs = TALER_AUDITORDB_insert_denominations_without_sigs (
   2904       TALER_ARL_adb,
   2905       &dws);
   2906     if (qs < 0)
   2907     {
   2908       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2909       *iqs = qs;
   2910     }
   2911     return; /* skip! */
   2912   }
   2913   if (GNUNET_OK !=
   2914       TALER_auditor_denom_validity_verify (
   2915         TALER_ARL_auditor_url,
   2916         &issue->denom_hash,
   2917         &TALER_ARL_master_pub,
   2918         issue->start,
   2919         issue->expire_withdraw,
   2920         issue->expire_deposit,
   2921         issue->expire_legal,
   2922         &issue->value,
   2923         &issue->fees,
   2924         &TALER_ARL_auditor_pub,
   2925         &auditor_sig))
   2926   {
   2927     struct TALER_AUDITORDB_DenominationsWithoutSigs dws = {
   2928       .denompub_h = issue->denom_hash,
   2929       .start_time = issue->start.abs_time,
   2930       .end_time = issue->expire_legal.abs_time,
   2931       .value = issue->value
   2932     };
   2933 
   2934     qs = TALER_AUDITORDB_insert_denominations_without_sigs (
   2935       TALER_ARL_adb,
   2936       &dws);
   2937 
   2938     if (qs < 0)
   2939     {
   2940       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2941       *iqs = qs;
   2942       return;
   2943     }
   2944   }
   2945 }
   2946 
   2947 
   2948 /**
   2949  * Function called with details about purse deposits that have been made, with
   2950  * the goal of auditing the deposit's execution.
   2951  *
   2952  * @param cc closure
   2953  * @param rowid unique serial ID for the deposit in our DB
   2954  * @param deposit deposit details
   2955  * @param reserve_pub which reserve is the purse merged into, NULL if unknown
   2956  * @param flags purse flags
   2957  * @param auditor_balance purse balance (according to the
   2958  *          auditor during auditing)
   2959  * @param purse_total target amount the purse should reach
   2960  * @param denom_pub denomination public key of @a coin_pub
   2961  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop
   2962  */
   2963 static enum GNUNET_GenericReturnValue
   2964 purse_deposit_cb (
   2965   struct CoinContext *cc,
   2966   uint64_t rowid,
   2967   const struct TALER_EXCHANGEDB_PurseDeposit *deposit,
   2968   const struct TALER_ReservePublicKeyP *reserve_pub,
   2969   enum TALER_WalletAccountMergeFlags flags,
   2970   const struct TALER_Amount *auditor_balance,
   2971   const struct TALER_Amount *purse_total,
   2972   const struct TALER_DenominationPublicKey *denom_pub)
   2973 {
   2974   enum GNUNET_DB_QueryStatus qs;
   2975   struct TALER_DenominationHashP dh;
   2976   const struct TALER_EXCHANGEDB_DenominationKeyInformation *issue;
   2977   struct DenominationSummary *ds;
   2978 
   2979   (void) flags;
   2980   (void) auditor_balance;
   2981   (void) purse_total;
   2982   (void) reserve_pub;
   2983   GNUNET_assert (rowid >=
   2984                  TALER_ARL_USE_PP (coins_purse_deposits_serial_id));
   2985   TALER_ARL_USE_PP (coins_purse_deposits_serial_id) = rowid + 1;
   2986   qs = TALER_ARL_get_denomination_info (denom_pub,
   2987                                         &issue,
   2988                                         &dh);
   2989   if (0 > qs)
   2990   {
   2991     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   2992     cc->qs = qs;
   2993     return GNUNET_SYSERR;
   2994   }
   2995   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
   2996   {
   2997     qs = report_row_inconsistency ("purse-deposits",
   2998                                    rowid,
   2999                                    "denomination key not found");
   3000     if (0 > qs)
   3001     {
   3002       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3003       cc->qs = qs;
   3004       return GNUNET_SYSERR;
   3005     }
   3006     return GNUNET_OK;
   3007   }
   3008   qs = check_known_coin ("purse-deposit",
   3009                          issue,
   3010                          rowid,
   3011                          &deposit->coin_pub,
   3012                          denom_pub,
   3013                          &deposit->amount);
   3014   if (0 > qs)
   3015   {
   3016     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3017     cc->qs = qs;
   3018     return GNUNET_SYSERR;
   3019   }
   3020 
   3021   if (GNUNET_OK !=
   3022       TALER_wallet_purse_deposit_verify (
   3023         NULL != deposit->exchange_base_url
   3024         ? deposit->exchange_base_url
   3025         : TALER_ARL_exchange_url,
   3026         &deposit->purse_pub,
   3027         &deposit->amount,
   3028         &dh,
   3029         &deposit->h_age_commitment,
   3030         &deposit->coin_pub,
   3031         &deposit->coin_sig))
   3032   {
   3033     struct TALER_AUDITORDB_BadSigLosses bsl = {
   3034       .problem_row_id = rowid,
   3035       .operation = (char *) "purse-deposit",
   3036       .loss = deposit->amount,
   3037       .operation_specific_pub = deposit->coin_pub.eddsa_pub
   3038     };
   3039 
   3040     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   3041                 "Failed to verify purse deposit signature in row %llu\n",
   3042                 (unsigned long long) rowid);
   3043     qs = TALER_AUDITORDB_insert_bad_sig_losses (
   3044       TALER_ARL_adb,
   3045       &bsl);
   3046     if (0 > qs)
   3047     {
   3048       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3049       cc->qs = qs;
   3050       return GNUNET_SYSERR;
   3051     }
   3052     TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_irregular_loss),
   3053                           &TALER_ARL_USE_AB (coin_irregular_loss),
   3054                           &deposit->amount);
   3055     return GNUNET_OK;
   3056   }
   3057 
   3058   /* update coin's denomination balance */
   3059   ds = get_denomination_summary (cc,
   3060                                  issue);
   3061   if (NULL == ds)
   3062   {
   3063     qs = report_row_inconsistency ("purse-deposit",
   3064                                    rowid,
   3065                                    "denomination key for purse-deposited coin unknown to auditor");
   3066     if (0 > qs)
   3067     {
   3068       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3069       cc->qs = qs;
   3070       return GNUNET_SYSERR;
   3071     }
   3072   }
   3073   else
   3074   {
   3075     qs = reduce_denom_balance (ds,
   3076                                rowid,
   3077                                &deposit->amount);
   3078     if (0 > qs)
   3079     {
   3080       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3081       cc->qs = GNUNET_DB_STATUS_HARD_ERROR;
   3082       return GNUNET_SYSERR;
   3083     }
   3084   }
   3085 
   3086   /* update global deposit fees */
   3087   TALER_ARL_amount_add (&TALER_ARL_USE_AB (coin_deposit_fee_revenue),
   3088                         &TALER_ARL_USE_AB (coin_deposit_fee_revenue),
   3089                         &issue->fees.deposit);
   3090   return GNUNET_OK;
   3091 }
   3092 
   3093 
   3094 /**
   3095  * Analyze the exchange's processing of coins.
   3096  *
   3097  * @param cls closure
   3098  * @return transaction status code
   3099  */
   3100 static enum GNUNET_DB_QueryStatus
   3101 analyze_coins (void *cls)
   3102 {
   3103   struct CoinContext cc;
   3104   enum GNUNET_DB_QueryStatus qs;
   3105   enum GNUNET_DB_QueryStatus iqs;
   3106 
   3107   (void) cls;
   3108   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
   3109               "Checking denominations...\n");
   3110   iqs = GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
   3111   qs = TALER_EXCHANGEDB_iterate_denomination_info (TALER_ARL_edb,
   3112                                                    &check_denomination,
   3113                                                    &iqs);
   3114   if (0 > qs)
   3115   {
   3116     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3117     return qs;
   3118   }
   3119   if (0 > iqs)
   3120   {
   3121     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == iqs);
   3122     return iqs;
   3123   }
   3124   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
   3125               "Analyzing coins\n");
   3126   qs = TALER_AUDITORDB_get_auditor_progress (
   3127     TALER_ARL_adb,
   3128     TALER_ARL_GET_PP (coins_withdraw_serial_id),
   3129     TALER_ARL_GET_PP (coins_deposit_serial_id),
   3130     TALER_ARL_GET_PP (coins_melt_serial_id),
   3131     TALER_ARL_GET_PP (coins_refund_serial_id),
   3132     TALER_ARL_GET_PP (coins_recoup_serial_id),
   3133     TALER_ARL_GET_PP (coins_recoup_refresh_serial_id),
   3134     TALER_ARL_GET_PP (coins_purse_deposits_serial_id),
   3135     TALER_ARL_GET_PP (coins_purse_refunds_serial_id),
   3136     NULL);
   3137   if (0 > qs)
   3138   {
   3139     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3140     return qs;
   3141   }
   3142   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
   3143   {
   3144     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
   3145                 "First analysis using this auditor, starting from scratch\n");
   3146   }
   3147   else
   3148   {
   3149     GNUNET_log (
   3150       GNUNET_ERROR_TYPE_INFO,
   3151       "Resuming coin audit at %llu/%llu/%llu/%llu/%llu/%llu/%llu/%llu\n",
   3152       (unsigned long long) TALER_ARL_USE_PP (
   3153         coins_withdraw_serial_id),
   3154       (unsigned long long) TALER_ARL_USE_PP (
   3155         coins_deposit_serial_id),
   3156       (unsigned long long) TALER_ARL_USE_PP (
   3157         coins_melt_serial_id),
   3158       (unsigned long long) TALER_ARL_USE_PP (
   3159         coins_refund_serial_id),
   3160       (unsigned long long) TALER_ARL_USE_PP (
   3161         coins_recoup_serial_id),
   3162       (unsigned long long) TALER_ARL_USE_PP (
   3163         coins_recoup_refresh_serial_id),
   3164       (unsigned long long) TALER_ARL_USE_PP (
   3165         coins_purse_deposits_serial_id),
   3166       (unsigned long long) TALER_ARL_USE_PP (
   3167         coins_purse_refunds_serial_id));
   3168   }
   3169 
   3170   /* setup 'cc' */
   3171   cc.qs = GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
   3172   cc.denom_summaries = GNUNET_CONTAINER_multihashmap_create (256,
   3173                                                              GNUNET_NO);
   3174   qs = TALER_AUDITORDB_get_balance (
   3175     TALER_ARL_adb,
   3176     TALER_ARL_GET_AB (coin_balance_risk),
   3177     TALER_ARL_GET_AB (total_escrowed),
   3178     TALER_ARL_GET_AB (coin_irregular_loss),
   3179     TALER_ARL_GET_AB (coin_melt_fee_revenue),
   3180     TALER_ARL_GET_AB (coin_deposit_fee_revenue),
   3181     TALER_ARL_GET_AB (coin_deposit_fee_loss),
   3182     TALER_ARL_GET_AB (coin_refund_fee_revenue),
   3183     TALER_ARL_GET_AB (total_recoup_loss),
   3184     TALER_ARL_GET_AB (coins_total_arithmetic_delta_plus),
   3185     TALER_ARL_GET_AB (coins_total_arithmetic_delta_minus),
   3186     TALER_ARL_GET_AB (coins_reported_emergency_risk_by_count),
   3187     TALER_ARL_GET_AB (coins_reported_emergency_risk_by_amount),
   3188     TALER_ARL_GET_AB (coins_emergencies_loss),
   3189     TALER_ARL_GET_AB (coins_emergencies_loss_by_count),
   3190     NULL);
   3191   if (0 > qs)
   3192   {
   3193     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3194     goto cleanup;
   3195   }
   3196   /* process withdrawals */
   3197   if (0 >
   3198       (qs = TALER_EXCHANGEDB_iterate_withdrawals_above_serial_id (
   3199          TALER_ARL_edb,
   3200          TALER_ARL_USE_PP (coins_withdraw_serial_id),
   3201          &withdraw_cb,
   3202          &cc)))
   3203   {
   3204     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3205     goto cleanup;
   3206   }
   3207   if (0 > cc.qs)
   3208   {
   3209     qs = cc.qs;
   3210     goto cleanup;
   3211   }
   3212   /* Process all operations that credit a denomination balance (refunds,
   3213      recoup-refresh, purse refunds) before those that debit it (melts,
   3214      deposits, purse deposits): within a pass the tables are processed
   3215      one after the other and not in time order, so a coin that spends
   3216      a credit it received in the same pass would otherwise make
   3217      reduce_denom_balance() see a balance that is too low and report a
   3218      false emergency. */
   3219   /* process refunds */
   3220   if (0 >
   3221       (qs = TALER_EXCHANGEDB_iterate_refunds_above_serial_id (
   3222          TALER_ARL_edb,
   3223          TALER_ARL_USE_PP (coins_refund_serial_id),
   3224          &refund_cb,
   3225          &cc)))
   3226   {
   3227     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3228     goto cleanup;
   3229   }
   3230   if (0 > cc.qs)
   3231   {
   3232     qs = cc.qs;
   3233     goto cleanup;
   3234   }
   3235   /* process recoups of refreshed coins (credit the old coin) */
   3236   if (0 >
   3237       (qs = TALER_EXCHANGEDB_iterate_recoup_refreshes_above_serial_id (
   3238          TALER_ARL_edb,
   3239          TALER_ARL_USE_PP (coins_recoup_refresh_serial_id),
   3240          &recoup_refresh_cb,
   3241          &cc)))
   3242   {
   3243     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3244     goto cleanup;
   3245   }
   3246   if (0 > cc.qs)
   3247   {
   3248     qs = cc.qs;
   3249     goto cleanup;
   3250   }
   3251   /* process purse_refunds */
   3252   if (0 >
   3253       (qs = TALER_EXCHANGEDB_iterate_purse_decisions_above_serial_id (
   3254          TALER_ARL_edb,
   3255          TALER_ARL_USE_PP (coins_purse_refunds_serial_id),
   3256          true, /* only go for refunds! */
   3257          &purse_refund_cb,
   3258          &cc)))
   3259   {
   3260     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3261     goto cleanup;
   3262   }
   3263   if (0 > cc.qs)
   3264   {
   3265     qs = cc.qs;
   3266     goto cleanup;
   3267   }
   3268   /* process refreshes */
   3269   if (0 >
   3270       (qs = TALER_EXCHANGEDB_iterate_refreshes_above_serial_id (
   3271          TALER_ARL_edb,
   3272          TALER_ARL_USE_PP (coins_melt_serial_id),
   3273          &refresh_session_cb,
   3274          &cc)))
   3275   {
   3276     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3277     goto cleanup;
   3278   }
   3279   if (0 > cc.qs)
   3280   {
   3281     qs = cc.qs;
   3282     goto cleanup;
   3283   }
   3284   /* process deposits */
   3285   if (0 >
   3286       (qs = TALER_EXCHANGEDB_iterate_coin_deposits_above_serial_id (
   3287          TALER_ARL_edb,
   3288          TALER_ARL_USE_PP (coins_deposit_serial_id),
   3289          &deposit_cb,
   3290          &cc)))
   3291   {
   3292     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3293     goto cleanup;
   3294   }
   3295   if (0 > cc.qs)
   3296   {
   3297     qs = cc.qs;
   3298     goto cleanup;
   3299   }
   3300   /* process purse_deposits */
   3301   if (0 >
   3302       (qs = TALER_EXCHANGEDB_iterate_purse_deposits_above_serial_id (
   3303          TALER_ARL_edb,
   3304          TALER_ARL_USE_PP (coins_purse_deposits_serial_id),
   3305          &purse_deposit_cb,
   3306          &cc)))
   3307   {
   3308     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3309     goto cleanup;
   3310   }
   3311   if (0 > cc.qs)
   3312   {
   3313     qs = cc.qs;
   3314     goto cleanup;
   3315   }
   3316   if (0 >
   3317       (qs = TALER_EXCHANGEDB_iterate_recoups_above_serial_id (
   3318          TALER_ARL_edb,
   3319          TALER_ARL_USE_PP (coins_recoup_serial_id),
   3320          &recoup_cb,
   3321          &cc)))
   3322   {
   3323     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3324     goto cleanup;
   3325   }
   3326   if (0 > cc.qs)
   3327   {
   3328     qs = cc.qs;
   3329     goto cleanup;
   3330   }
   3331   /* sync 'cc' back to disk */
   3332   cc.qs = GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
   3333   GNUNET_CONTAINER_multihashmap_iterate (cc.denom_summaries,
   3334                                          &sync_denomination,
   3335                                          &cc);
   3336 
   3337   if (0 > cc.qs)
   3338   {
   3339     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == cc.qs);
   3340     qs = cc.qs;
   3341     goto cleanup;
   3342   }
   3343 
   3344   /* settle the denominations that nothing touched in this pass */
   3345   qs = sweep_denominations (&cc);
   3346   if (0 > qs)
   3347   {
   3348     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3349     goto cleanup;
   3350   }
   3351 
   3352   qs = TALER_AUDITORDB_insert_balance (
   3353     TALER_ARL_adb,
   3354     TALER_ARL_SET_AB (coin_balance_risk),
   3355     TALER_ARL_SET_AB (total_escrowed),
   3356     TALER_ARL_SET_AB (coin_irregular_loss),
   3357     TALER_ARL_SET_AB (coin_melt_fee_revenue),
   3358     TALER_ARL_SET_AB (coin_deposit_fee_revenue),
   3359     TALER_ARL_SET_AB (coin_deposit_fee_loss),
   3360     TALER_ARL_SET_AB (coin_refund_fee_revenue),
   3361     TALER_ARL_SET_AB (total_recoup_loss),
   3362     TALER_ARL_SET_AB (coins_total_arithmetic_delta_plus),
   3363     TALER_ARL_SET_AB (coins_total_arithmetic_delta_minus),
   3364     TALER_ARL_SET_AB (coins_reported_emergency_risk_by_count),
   3365     TALER_ARL_SET_AB (coins_reported_emergency_risk_by_amount),
   3366     TALER_ARL_SET_AB (coins_emergencies_loss),
   3367     TALER_ARL_SET_AB (coins_emergencies_loss_by_count),
   3368     NULL);
   3369   if (0 > qs)
   3370   {
   3371     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   3372                 "Failed to update auditor DB, not recording progress\n");
   3373     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3374     goto cleanup;
   3375   }
   3376 
   3377   qs = TALER_AUDITORDB_insert_auditor_progress (
   3378     TALER_ARL_adb,
   3379     TALER_ARL_SET_PP (coins_withdraw_serial_id),
   3380     TALER_ARL_SET_PP (coins_deposit_serial_id),
   3381     TALER_ARL_SET_PP (coins_melt_serial_id),
   3382     TALER_ARL_SET_PP (coins_refund_serial_id),
   3383     TALER_ARL_SET_PP (coins_recoup_serial_id),
   3384     TALER_ARL_SET_PP (coins_recoup_refresh_serial_id),
   3385     TALER_ARL_SET_PP (coins_purse_deposits_serial_id),
   3386     TALER_ARL_SET_PP (coins_purse_refunds_serial_id),
   3387     NULL);
   3388   if (0 > qs)
   3389   {
   3390     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   3391                 "Failed to update auditor DB, not recording progress\n");
   3392     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   3393     goto cleanup;
   3394   }
   3395 
   3396   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   3397               "Concluded coin audit step at %llu/%llu/%llu/%llu/%llu/%llu/%llu/%llu\n",
   3398               (unsigned long long) TALER_ARL_USE_PP (coins_withdraw_serial_id),
   3399               (unsigned long long) TALER_ARL_USE_PP (coins_deposit_serial_id),
   3400               (unsigned long long) TALER_ARL_USE_PP (coins_melt_serial_id),
   3401               (unsigned long long) TALER_ARL_USE_PP (coins_refund_serial_id),
   3402               (unsigned long long) TALER_ARL_USE_PP (coins_recoup_serial_id),
   3403               (unsigned long long) TALER_ARL_USE_PP (
   3404                 coins_recoup_refresh_serial_id),
   3405               (unsigned long long) TALER_ARL_USE_PP (
   3406                 coins_purse_deposits_serial_id),
   3407               (unsigned long long) TALER_ARL_USE_PP (
   3408                 coins_purse_refunds_serial_id));
   3409   qs = GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
   3410 cleanup:
   3411   flush_coin_histories ();
   3412   GNUNET_CONTAINER_multihashmap_iterate (cc.denom_summaries,
   3413                                          &cleanup_denomination,
   3414                                          &cc);
   3415   GNUNET_CONTAINER_multihashmap_destroy (cc.denom_summaries);
   3416   return qs;
   3417 }
   3418 
   3419 
   3420 /**
   3421  * Function called on events received from Postgres.
   3422  *
   3423  * @param cls closure, NULL
   3424  * @param extra additional event data provided
   3425  * @param extra_size number of bytes in @a extra
   3426  */
   3427 static void
   3428 db_notify (void *cls,
   3429            const void *extra,
   3430            size_t extra_size)
   3431 {
   3432   (void) cls;
   3433   (void) extra;
   3434   (void) extra_size;
   3435   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   3436               "Received notification to wake coins helper\n");
   3437   if (GNUNET_OK !=
   3438       TALER_ARL_setup_sessions_and_run (&analyze_coins,
   3439                                         NULL))
   3440   {
   3441     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   3442                 "Audit failed\n");
   3443     GNUNET_SCHEDULER_shutdown ();
   3444     global_ret = EXIT_FAILURE;
   3445     return;
   3446   }
   3447 }
   3448 
   3449 
   3450 /**
   3451  * Function called on shutdown.
   3452  */
   3453 static void
   3454 do_shutdown (void *cls)
   3455 {
   3456   (void) cls;
   3457   if (NULL != eh)
   3458   {
   3459     TALER_AUDITORDB_event_listen_cancel (eh);
   3460     eh = NULL;
   3461   }
   3462   TALER_ARL_done ();
   3463 }
   3464 
   3465 
   3466 /**
   3467  * Main function that will be run.
   3468  *
   3469  * @param cls closure
   3470  * @param args remaining command-line arguments
   3471  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
   3472  * @param c configuration
   3473  */
   3474 static void
   3475 run (void *cls,
   3476      char *const *args,
   3477      const char *cfgfile,
   3478      const struct GNUNET_CONFIGURATION_Handle *c)
   3479 {
   3480   (void) cls;
   3481   (void) args;
   3482   (void) cfgfile;
   3483   cfg = c;
   3484   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
   3485                                  NULL);
   3486   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
   3487               "Launching coins auditor\n");
   3488   if (EXIT_SUCCESS !=
   3489       (global_ret = TALER_ARL_init (c)))
   3490   {
   3491     return;
   3492   }
   3493   if (test_mode != 1)
   3494   {
   3495     struct GNUNET_DB_EventHeaderP es = {
   3496       .size = htons (sizeof (es)),
   3497       .type = htons (TALER_DBEVENT_EXCHANGE_AUDITOR_WAKE_HELPER_COINS)
   3498     };
   3499 
   3500     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   3501                 "Running helper indefinitely\n");
   3502     eh = TALER_AUDITORDB_event_listen (TALER_ARL_adb,
   3503                                        &es,
   3504                                        GNUNET_TIME_UNIT_FOREVER_REL,
   3505                                        &db_notify,
   3506                                        NULL);
   3507   }
   3508   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
   3509               "Starting audit\n");
   3510   if (GNUNET_OK !=
   3511       TALER_ARL_setup_sessions_and_run (&analyze_coins,
   3512                                         NULL))
   3513   {
   3514     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   3515                 "Audit failed\n");
   3516     GNUNET_SCHEDULER_shutdown ();
   3517     global_ret = EXIT_FAILURE;
   3518     return;
   3519   }
   3520 }
   3521 
   3522 
   3523 /**
   3524  * The main function to audit operations on coins.
   3525  *
   3526  * @param argc number of arguments from the command line
   3527  * @param argv command line arguments
   3528  * @return 0 ok, 1 on error
   3529  */
   3530 int
   3531 main (int argc,
   3532       char *const *argv)
   3533 {
   3534   const struct GNUNET_GETOPT_CommandLineOption options[] = {
   3535     GNUNET_GETOPT_option_flag ('i',
   3536                                "internal",
   3537                                "perform checks only applicable for exchange-internal audits",
   3538                                &internal_checks),
   3539     GNUNET_GETOPT_option_flag ('t',
   3540                                "test",
   3541                                "run in test mode and exit when idle",
   3542                                &test_mode),
   3543     GNUNET_GETOPT_option_timetravel ('T',
   3544                                      "timetravel"),
   3545     GNUNET_GETOPT_OPTION_END
   3546   };
   3547   enum GNUNET_GenericReturnValue ret;
   3548 
   3549   ret = GNUNET_PROGRAM_run (
   3550     TALER_AUDITOR_project_data (),
   3551     argc,
   3552     argv,
   3553     "taler-helper-auditor-coins",
   3554     gettext_noop ("Audit Taler coin processing"),
   3555     options,
   3556     &run,
   3557     NULL);
   3558   if (GNUNET_SYSERR == ret)
   3559     return EXIT_NOTCONFIGURED;
   3560   if (GNUNET_NO == ret)
   3561     return EXIT_SUCCESS;
   3562   return global_ret;
   3563 }
   3564 
   3565 
   3566 /* end of taler-helper-auditor-coins.c */