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-wire-credit.c (67474B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2017-2024 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 
     13   You should have received a copy of the GNU General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file auditor/taler-helper-auditor-wire-credit.c
     18  * @brief audits that wire transfers match those from an exchange database.
     19  * @author Christian Grothoff
     20  *
     21  * This auditor verifies that 'reserves_in' actually matches
     22  * the incoming wire transfers from the bank.
     23  */
     24 #include "platform.h"
     25 #include <gnunet/gnunet_util_lib.h>
     26 #include <gnunet/gnunet_curl_lib.h>
     27 #include "auditordb_lib.h"
     28 #include "exchangedb_lib.h"
     29 #include "taler/taler_json_lib.h"
     30 #include "taler/taler_bank_service.h"
     31 #include "taler/taler_signatures.h"
     32 #include "report-lib.h"
     33 #include "taler/taler_dbevents.h"
     34 #include \
     35   "exchange-database/iterate_reserves_in_above_serial_id_by_account.h"
     36 #include "auditor-database/delete_reserve_in_inconsistency.h"
     37 #include "auditor-database/event_listen.h"
     38 #include "auditor-database/get_auditor_progress.h"
     39 #include "auditor-database/get_balance.h"
     40 #include "auditor-database/insert_auditor_progress.h"
     41 #include "auditor-database/insert_balance.h"
     42 #include "auditor-database/delete_kycauth_in_inconsistency.h"
     43 #include "auditor-database/get_kycauth_in_inconsistency.h"
     44 #include "auditor-database/insert_kycauth_in_inconsistency.h"
     45 #include "auditor-database/insert_misattribution_in_inconsistency.h"
     46 #include "auditor-database/insert_reserve_in_inconsistency.h"
     47 #include "auditor-database/insert_row_inconsistency.h"
     48 #include "auditor-database/insert_row_minor_inconsistencies.h"
     49 #include "auditor-database/preflight.h"
     50 #include "auditor-database/get_reserve_in_inconsistency.h"
     51 #include "auditor-database/start.h"
     52 #include "exchange-database/preflight.h"
     53 #include "exchange-database/rollback.h"
     54 #include \
     55   "exchange-database/iterate_kycauth_in_above_serial_id_by_account.h"
     56 #include \
     57   "exchange-database/iterate_reserves_in_above_serial_id_by_account.h"
     58 #include "exchange-database/start_read_only.h"
     59 
     60 /**
     61  * How much time do we allow the aggregator to lag behind?  If
     62  * wire transfers should have been made more than #GRACE_PERIOD
     63  * before, we issue warnings.
     64  */
     65 #define GRACE_PERIOD GNUNET_TIME_UNIT_HOURS
     66 
     67 /**
     68  * Maximum number of wire transfers we process per
     69  * (database) transaction.
     70  */
     71 #define MAX_PER_TRANSACTION 1024
     72 
     73 /**
     74  * How much do we allow the bank and the exchange to disagree about
     75  * timestamps? Should be sufficiently large to avoid bogus reports from deltas
     76  * created by imperfect clock synchronization and network delay.
     77  */
     78 #define TIME_TOLERANCE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, \
     79                                                       15)
     80 
     81 
     82 /**
     83  * Run in test mode. Exit when idle instead of
     84  * going to sleep and waiting for more work.
     85  */
     86 static int test_mode;
     87 
     88 /**
     89  * Information we keep for each supported account.
     90  */
     91 struct WireAccount
     92 {
     93   /**
     94    * Accounts are kept in a DLL.
     95    */
     96   struct WireAccount *next;
     97 
     98   /**
     99    * Plugins are kept in a DLL.
    100    */
    101   struct WireAccount *prev;
    102 
    103   /**
    104    * Account details.
    105    */
    106   const struct TALER_EXCHANGEDB_AccountInfo *ai;
    107 
    108   /**
    109    * Active wire request for the transaction history.
    110    */
    111   struct TALER_BANK_CreditHistoryHandle *chh;
    112 
    113   /**
    114    * Progress point for this account.
    115    */
    116   uint64_t last_reserve_in_serial_id;
    117 
    118   /**
    119    * Initial progress point for this account.
    120    */
    121   uint64_t start_reserve_in_serial_id;
    122 
    123   /**
    124    * Progress point for this account in the kycauths_in table.
    125    */
    126   uint64_t last_kycauth_in_serial_id;
    127 
    128   /**
    129    * Where we are in the inbound transaction history.
    130    */
    131   uint64_t wire_off_in;
    132 
    133   /**
    134    * Label under which we store our pp's reserve_in_serial_id.
    135    */
    136   char *label_reserve_in_serial_id;
    137 
    138   /**
    139    * Label under which we store our pp's kycauth_in_serial_id.
    140    */
    141   char *label_kycauth_in_serial_id;
    142 
    143   /**
    144    * Label under which we store our wire_off_in.
    145    */
    146   char *label_wire_off_in;
    147 
    148 };
    149 
    150 
    151 /**
    152  * Return value from main().
    153  */
    154 static int global_ret;
    155 
    156 /**
    157  * State of the current database transaction with
    158  * the auditor DB.
    159  */
    160 static enum GNUNET_DB_QueryStatus global_qs;
    161 
    162 /**
    163  * Map with information about incoming wire transfers.
    164  * Maps hashes of the wire offsets to `struct ReserveInInfo`s.
    165  */
    166 static struct GNUNET_CONTAINER_MultiHashMap *in_map;
    167 
    168 /**
    169  * Map with information about incoming KYCAUTH wire transfers.
    170  * Maps hashes of the wire offsets to `struct KycauthInInfo`s.
    171  */
    172 static struct GNUNET_CONTAINER_MultiHashMap *kycauth_map;
    173 
    174 /**
    175  * Head of list of wire accounts we still need to look at.
    176  */
    177 static struct WireAccount *wa_head;
    178 
    179 /**
    180  * Tail of list of wire accounts we still need to look at.
    181  */
    182 static struct WireAccount *wa_tail;
    183 
    184 /**
    185  * Amount that is considered "tiny"
    186  */
    187 static struct TALER_Amount tiny_amount;
    188 
    189 /**
    190  * Total amount that was transferred too much to the exchange.
    191  */
    192 static TALER_ARL_DEF_AB (total_bad_amount_in_plus);
    193 
    194 /**
    195  * Total amount that was transferred too little to the exchange.
    196  */
    197 static TALER_ARL_DEF_AB (total_bad_amount_in_minus);
    198 
    199 /**
    200  * Total amount where the exchange has the wrong sender account
    201  * for incoming funds and may thus wire funds to the wrong
    202  * destination when closing the reserve.
    203  */
    204 static TALER_ARL_DEF_AB (total_misattribution_in);
    205 
    206 /**
    207  * Total amount credited to exchange accounts, as claimed by the exchange.
    208  * Includes #TAC_total_kycauth_revenue, so that this is the credit side of the
    209  * balance the exchange's bank account is expected to have.
    210  */
    211 static TALER_ARL_DEF_AB (total_wire_in);
    212 
    213 /**
    214  * Total amount credited to exchange accounts via KYCAUTH, as reported by
    215  * the bank.
    216  */
    217 static TALER_ARL_DEF_AB (total_kycauth_in);
    218 
    219 /**
    220  * Total amount credited to exchange accounts via KYCAUTH, as booked by the
    221  * exchange in its `kycauths_in` table.  KYCAUTH funds are not credited to
    222  * any reserve and are never paid back, so they are revenue of the exchange
    223  * operator, like wire fees and closing fees, and may be drained together
    224  * with those.  Counted towards #TAC_total_wire_in for that reason.
    225  */
    226 static TALER_ARL_DEF_AB (total_kycauth_revenue);
    227 
    228 /**
    229  * Total wire credit fees charged to the exchange account.
    230  */
    231 static TALER_ARL_DEF_AB (total_wire_credit_fees);
    232 
    233 /**
    234  * Amount of zero in our currency.
    235  */
    236 static struct TALER_Amount zero;
    237 
    238 /**
    239  * Handle to the context for interacting with the bank.
    240  */
    241 static struct GNUNET_CURL_Context *ctx;
    242 
    243 /**
    244  * Scheduler context for running the @e ctx.
    245  */
    246 static struct GNUNET_CURL_RescheduleContext *rc;
    247 
    248 /**
    249  * Should we run checks that only work for exchange-internal audits?
    250  */
    251 static int internal_checks;
    252 
    253 /**
    254  * Should we ignore if the bank does not know our bank
    255  * account?
    256  */
    257 static int ignore_account_404;
    258 
    259 /**
    260  * Database event handler to wake us up again.
    261  */
    262 static struct GNUNET_DB_EventHandler *eh;
    263 
    264 /**
    265  * Set if we were woken up while an audit was still running, so that
    266  * we start another one as soon as the current one has been committed.
    267  */
    268 static bool rerun;
    269 
    270 /**
    271  * The auditors's configuration.
    272  */
    273 static const struct GNUNET_CONFIGURATION_Handle *cfg;
    274 
    275 /* *****************************   Shutdown   **************************** */
    276 
    277 /**
    278  * Entry in map with wire information we expect to obtain from the
    279  * bank later.
    280  */
    281 struct ReserveInInfo
    282 {
    283 
    284   /**
    285    * Hash of expected row offset.
    286    */
    287   struct GNUNET_HashCode row_off_hash;
    288 
    289   /**
    290    * Expected details about the wire transfer.
    291    * The member "account_url" is to be allocated
    292    * at the end of this struct!
    293    */
    294   struct TALER_BANK_CreditDetails credit_details;
    295 
    296   /**
    297    * RowID in reserves_in table.
    298    */
    299   uint64_t rowid;
    300 
    301 };
    302 
    303 
    304 /**
    305  * Entry in #kycauth_map with the KYCAUTH transfer the exchange claims to
    306  * have received, waiting to be matched against the bank's credit history.
    307  */
    308 struct KycauthInInfo
    309 {
    310 
    311   /**
    312    * Hash of expected row offset.
    313    */
    314   struct GNUNET_HashCode row_off_hash;
    315 
    316   /**
    317    * RowID in the kycauths_in table.
    318    */
    319   uint64_t rowid;
    320 
    321   /**
    322    * Public key the transfer associated with the debited account.
    323    */
    324   union TALER_AccountPublicKeyP account_pub;
    325 
    326   /**
    327    * Amount the exchange says it received.
    328    */
    329   struct TALER_Amount credit;
    330 
    331   /**
    332    * When the exchange says it received the funds.
    333    */
    334   struct GNUNET_TIME_Timestamp execution_date;
    335 
    336   /**
    337    * payto://-URL of the debited account, allocated at the end of this
    338    * struct.  NULL if the exchange has no `wire_targets` row for the
    339    * account, which it only creates the first time it sees one.
    340    */
    341   struct TALER_FullPayto sender_account_details;
    342 
    343 };
    344 
    345 
    346 /**
    347  * Free entry in #in_map.
    348  *
    349  * @param cls NULL
    350  * @param key unused key
    351  * @param value the `struct ReserveInInfo` to free
    352  * @return #GNUNET_OK
    353  */
    354 static enum GNUNET_GenericReturnValue
    355 free_rii (void *cls,
    356           const struct GNUNET_HashCode *key,
    357           void *value)
    358 {
    359   struct ReserveInInfo *rii = value;
    360 
    361   (void) cls;
    362   GNUNET_assert (GNUNET_YES ==
    363                  GNUNET_CONTAINER_multihashmap_remove (in_map,
    364                                                        key,
    365                                                        rii));
    366   GNUNET_free (rii);
    367   return GNUNET_OK;
    368 }
    369 
    370 
    371 /**
    372  * Free entry in #kycauth_map.
    373  *
    374  * @param cls NULL
    375  * @param key unused key
    376  * @param value the `struct KycauthInInfo` to free
    377  * @return #GNUNET_OK
    378  */
    379 static enum GNUNET_GenericReturnValue
    380 free_kii (void *cls,
    381           const struct GNUNET_HashCode *key,
    382           void *value)
    383 {
    384   struct KycauthInInfo *kii = value;
    385 
    386   (void) cls;
    387   GNUNET_assert (GNUNET_YES ==
    388                  GNUNET_CONTAINER_multihashmap_remove (kycauth_map,
    389                                                        key,
    390                                                        kii));
    391   GNUNET_free (kii);
    392   return GNUNET_OK;
    393 }
    394 
    395 
    396 /**
    397  * Task run on shutdown.
    398  *
    399  * @param cls NULL
    400  */
    401 static void
    402 do_shutdown (void *cls)
    403 {
    404   struct WireAccount *wa;
    405 
    406   (void) cls;
    407   if (NULL != eh)
    408   {
    409     TALER_AUDITORDB_event_listen_cancel (eh);
    410     eh = NULL;
    411   }
    412   TALER_ARL_done ();
    413   if (NULL != in_map)
    414   {
    415     GNUNET_CONTAINER_multihashmap_iterate (in_map,
    416                                            &free_rii,
    417                                            NULL);
    418     GNUNET_CONTAINER_multihashmap_destroy (in_map);
    419     in_map = NULL;
    420   }
    421   if (NULL != kycauth_map)
    422   {
    423     GNUNET_CONTAINER_multihashmap_iterate (kycauth_map,
    424                                            &free_kii,
    425                                            NULL);
    426     GNUNET_CONTAINER_multihashmap_destroy (kycauth_map);
    427     kycauth_map = NULL;
    428   }
    429   while (NULL != (wa = wa_head))
    430   {
    431     if (NULL != wa->chh)
    432     {
    433       TALER_BANK_credit_history_cancel (wa->chh);
    434       wa->chh = NULL;
    435     }
    436     GNUNET_CONTAINER_DLL_remove (wa_head,
    437                                  wa_tail,
    438                                  wa);
    439     GNUNET_free (wa->label_reserve_in_serial_id);
    440     GNUNET_free (wa->label_kycauth_in_serial_id);
    441     GNUNET_free (wa->label_wire_off_in);
    442     GNUNET_free (wa);
    443   }
    444   if (NULL != ctx)
    445   {
    446     GNUNET_CURL_fini (ctx);
    447     ctx = NULL;
    448   }
    449   if (NULL != rc)
    450   {
    451     GNUNET_CURL_gnunet_rc_destroy (rc);
    452     rc = NULL;
    453   }
    454   TALER_EXCHANGEDB_unload_accounts ();
    455   TALER_ARL_cfg = NULL;
    456 }
    457 
    458 
    459 /**
    460  * Start the database transactions and begin the audit.
    461  *
    462  * @return transaction status code
    463  */
    464 static enum GNUNET_DB_QueryStatus
    465 begin_transaction (void);
    466 
    467 
    468 /**
    469  * Rollback the current transaction, reset our state and try
    470  * again (we had a serialization error).
    471  */
    472 static void
    473 rollback_and_reset (void)
    474 {
    475   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    476               "Serialization issue, trying again\n");
    477   TALER_AUDITORDB_rollback (TALER_ARL_adb);
    478   for (unsigned int max_retries = 3; max_retries>0; max_retries--)
    479   {
    480     enum GNUNET_DB_QueryStatus qs;
    481 
    482     if (NULL != in_map)
    483     {
    484       GNUNET_CONTAINER_multihashmap_iterate (in_map,
    485                                              &free_rii,
    486                                              NULL);
    487       GNUNET_CONTAINER_multihashmap_destroy (in_map);
    488       in_map = NULL;
    489     }
    490     if (NULL != kycauth_map)
    491     {
    492       GNUNET_CONTAINER_multihashmap_iterate (kycauth_map,
    493                                              &free_kii,
    494                                              NULL);
    495       GNUNET_CONTAINER_multihashmap_destroy (kycauth_map);
    496       kycauth_map = NULL;
    497     }
    498     qs = begin_transaction ();
    499     if (GNUNET_DB_STATUS_HARD_ERROR == qs)
    500       break;
    501   }
    502   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    503               "Hard database error, terminating\n");
    504   GNUNET_SCHEDULER_shutdown ();
    505 }
    506 
    507 
    508 /**
    509  * Commit the transaction, checkpointing our progress in the auditor DB.
    510  *
    511  * @param qs transaction status so far
    512  */
    513 static void
    514 commit (enum GNUNET_DB_QueryStatus qs)
    515 {
    516   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    517               "Transaction logic ended with status %d\n",
    518               qs);
    519   TALER_EXCHANGEDB_rollback (TALER_ARL_edb);
    520   if (qs < 0)
    521     goto handle_db_error;
    522   qs = TALER_AUDITORDB_insert_balance (
    523     TALER_ARL_adb,
    524     TALER_ARL_SET_AB (total_wire_in),
    525     TALER_ARL_SET_AB (total_kycauth_in),
    526     TALER_ARL_SET_AB (total_kycauth_revenue),
    527     TALER_ARL_SET_AB (total_wire_credit_fees),
    528     TALER_ARL_SET_AB (total_bad_amount_in_plus),
    529     TALER_ARL_SET_AB (total_bad_amount_in_minus),
    530     TALER_ARL_SET_AB (total_misattribution_in),
    531     NULL);
    532   if (0 > qs)
    533     goto handle_db_error;
    534   for (struct WireAccount *wa = wa_head;
    535        NULL != wa;
    536        wa = wa->next)
    537   {
    538     qs = TALER_AUDITORDB_insert_auditor_progress (
    539       TALER_ARL_adb,
    540       wa->label_reserve_in_serial_id,
    541       wa->last_reserve_in_serial_id,
    542       wa->label_kycauth_in_serial_id,
    543       wa->last_kycauth_in_serial_id,
    544       wa->label_wire_off_in,
    545       wa->wire_off_in,
    546       NULL);
    547     if (0 > qs)
    548       goto handle_db_error;
    549     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    550                 "Transaction ends at %s=%llu for account `%s'\n",
    551                 wa->label_reserve_in_serial_id,
    552                 (unsigned long long) wa->last_reserve_in_serial_id,
    553                 wa->ai->section_name);
    554   }
    555   qs = TALER_AUDITORDB_commit (TALER_ARL_adb);
    556   if (0 > qs)
    557   {
    558     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    559     goto handle_db_error;
    560   }
    561   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    562               "Transaction concluded!\n");
    563   if (1 == test_mode)
    564   {
    565     GNUNET_SCHEDULER_shutdown ();
    566     return;
    567   }
    568   if (rerun)
    569   {
    570     /* we were woken up while this audit was running */
    571     rerun = false;
    572     if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
    573         begin_transaction ())
    574     {
    575       GNUNET_break (0);
    576       global_ret = EXIT_FAILURE;
    577       GNUNET_SCHEDULER_shutdown ();
    578     }
    579   }
    580   return;
    581 handle_db_error:
    582   rollback_and_reset ();
    583 }
    584 
    585 
    586 /**
    587  * Conclude the credit history check by logging entries that
    588  * were not found and freeing resources. Then move on to
    589  * processing debits.
    590  */
    591 static void
    592 conclude_credit_history (void)
    593 {
    594   if (NULL != in_map)
    595   {
    596     GNUNET_assert (0 ==
    597                    GNUNET_CONTAINER_multihashmap_size (in_map));
    598     GNUNET_CONTAINER_multihashmap_destroy (in_map);
    599     in_map = NULL;
    600   }
    601   if (NULL != kycauth_map)
    602   {
    603     GNUNET_assert (0 ==
    604                    GNUNET_CONTAINER_multihashmap_size (kycauth_map));
    605     GNUNET_CONTAINER_multihashmap_destroy (kycauth_map);
    606     kycauth_map = NULL;
    607   }
    608   commit (global_qs);
    609 }
    610 
    611 
    612 /**
    613  * Check if the given wire transfers are equivalent.
    614  *
    615  * @param credit amount that was received
    616  * @param credit2 2nd amount that was received
    617  * @param reserve_pub public key of the reserve (also the WTID)
    618  * @param reserve_pub2 2nd public key of the reserve (also the WTID)
    619  * @param sender_account_details payto://-URL of the sender's bank account
    620  * @param sender_account_details2 2nd payto://-URL of the sender's bank account
    621  * @param execution_date when did we receive the funds
    622  * @param execution_date2 2nd when did we receive the funds
    623  * @return #GNUNET_YES if so,
    624  *         #GNUNET_NO if not
    625  *         #GNUNET_SYSERR on internal error
    626  */
    627 static enum GNUNET_GenericReturnValue
    628 check_equality (const struct TALER_Amount *credit,
    629                 const struct TALER_Amount *credit2,
    630                 const struct TALER_ReservePublicKeyP *reserve_pub,
    631                 const struct TALER_ReservePublicKeyP *reserve_pub2,
    632                 const struct TALER_FullPayto sender_account_details,
    633                 const struct TALER_FullPayto sender_account_details2,
    634                 struct GNUNET_TIME_Timestamp execution_date,
    635                 struct GNUNET_TIME_Timestamp execution_date2)
    636 {
    637   if (0 != TALER_amount_cmp (credit,
    638                              credit2))
    639     return GNUNET_NO;
    640   if (0 != GNUNET_memcmp (reserve_pub,
    641                           reserve_pub2))
    642     return GNUNET_NO;
    643   {
    644     struct TALER_NormalizedPayto np;
    645     struct TALER_NormalizedPayto np2;
    646     bool fail;
    647 
    648     np = TALER_payto_normalize (sender_account_details);
    649     np2 = TALER_payto_normalize (sender_account_details2);
    650     fail = (0 != TALER_normalized_payto_cmp (np,
    651                                              np2));
    652     GNUNET_free (np.normalized_payto);
    653     GNUNET_free (np2.normalized_payto);
    654     if (fail)
    655       return GNUNET_NO;
    656   }
    657   if (GNUNET_TIME_timestamp_cmp (execution_date,
    658                                  !=,
    659                                  execution_date2))
    660     return GNUNET_NO;
    661   return GNUNET_YES;
    662 }
    663 
    664 
    665 /**
    666  * Check if two payto://-URLs designate the same bank account.
    667  *
    668  * @param a first account, may be NULL
    669  * @param b second account, may be NULL
    670  * @return true if they match, or if either side is unknown
    671  */
    672 static bool
    673 same_account (const struct TALER_FullPayto a,
    674               const struct TALER_FullPayto b)
    675 {
    676   struct TALER_NormalizedPayto na;
    677   struct TALER_NormalizedPayto nb;
    678   bool ret;
    679 
    680   /* The exchange only creates the wire_targets row for an account the
    681      first time it sees one, so it legitimately may not know the debited
    682      account of a KYCAUTH transfer.  Do not turn that into a finding. */
    683   if ( (NULL == a.full_payto) ||
    684        (NULL == b.full_payto) )
    685     return true;
    686   na = TALER_payto_normalize (a);
    687   nb = TALER_payto_normalize (b);
    688   ret = (0 == TALER_normalized_payto_cmp (na,
    689                                           nb));
    690   GNUNET_free (na.normalized_payto);
    691   GNUNET_free (nb.normalized_payto);
    692   return ret;
    693 }
    694 
    695 
    696 /**
    697  * Check if the two views of a KYCAUTH wire transfer agree in every
    698  * respect.
    699  *
    700  * @param credit amount of the first view
    701  * @param credit2 amount of the second view
    702  * @param account_pub account key of the first view
    703  * @param account_pub2 account key of the second view
    704  * @param sender_account_details debited account of the first view
    705  * @param sender_account_details2 debited account of the second view
    706  * @param execution_date execution time of the first view
    707  * @param execution_date2 execution time of the second view
    708  * @return true if the two agree
    709  */
    710 static bool
    711 check_kycauth_equality (
    712   const struct TALER_Amount *credit,
    713   const struct TALER_Amount *credit2,
    714   const union TALER_AccountPublicKeyP *account_pub,
    715   const union TALER_AccountPublicKeyP *account_pub2,
    716   const struct TALER_FullPayto sender_account_details,
    717   const struct TALER_FullPayto sender_account_details2,
    718   struct GNUNET_TIME_Timestamp execution_date,
    719   struct GNUNET_TIME_Timestamp execution_date2)
    720 {
    721   return (0 == TALER_amount_cmp (credit,
    722                                  credit2)) &&
    723          (0 == GNUNET_memcmp (account_pub,
    724                               account_pub2)) &&
    725          same_account (sender_account_details,
    726                        sender_account_details2) &&
    727          GNUNET_TIME_timestamp_cmp (execution_date,
    728                                     ==,
    729                                     execution_date2);
    730 }
    731 
    732 
    733 /**
    734  * Function called with details about incoming wire transfers that
    735  * established an account key for KYC authentication, as claimed by the
    736  * exchange DB.
    737  *
    738  * Such a transfer is not credited to a reserve and is never paid back, so
    739  * the money is simply revenue of the exchange operator.  We book it as
    740  * such: it is part of the balance the exchange's bank account is expected
    741  * to have, and the exchange may drain it together with its fee income.
    742  *
    743  * The row is then remembered in #kycauth_map, so that the walk over the
    744  * bank's credit history that follows can confirm the transfer actually
    745  * happened; whatever is left in that map afterwards is money the exchange
    746  * claims to have received and the bank knows nothing about.
    747  *
    748  * @param cls a `struct WireAccount` we are processing
    749  * @param rowid unique serial ID for the entry in our DB
    750  * @param account_pub public key the transfer associated with the account
    751  * @param credit amount that was received
    752  * @param sender_account_details payto://-URL of the sender's bank account,
    753  *        NULL if the exchange has no `wire_targets` row for it
    754  * @param wire_reference unique identifier for the wire transfer
    755  * @param execution_date when did we receive the funds
    756  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop
    757  */
    758 static enum GNUNET_GenericReturnValue
    759 kycauth_in_cb (void *cls,
    760                uint64_t rowid,
    761                const union TALER_AccountPublicKeyP *account_pub,
    762                const struct TALER_Amount *credit,
    763                const struct TALER_FullPayto sender_account_details,
    764                uint64_t wire_reference,
    765                struct GNUNET_TIME_Timestamp execution_date)
    766 {
    767   struct WireAccount *wa = cls;
    768   struct KycauthInInfo *kii;
    769   size_t slen;
    770 
    771   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    772               "Analyzing exchange KYCAUTH IN (%llu) at %s of %s\n",
    773               (unsigned long long) rowid,
    774               GNUNET_TIME_timestamp2s (execution_date),
    775               TALER_amount2s (credit));
    776   /* Advance the cursor up front: every path below has fully accounted for
    777      this row, and reading it a second time would count the revenue twice. */
    778   wa->last_kycauth_in_serial_id = rowid + 1;
    779   TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_kycauth_revenue),
    780                         &TALER_ARL_USE_AB (total_kycauth_revenue),
    781                         credit);
    782   TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_wire_in),
    783                         &TALER_ARL_USE_AB (total_wire_in),
    784                         credit);
    785   {
    786     enum GNUNET_DB_QueryStatus qs;
    787     struct TALER_AUDITORDB_KycauthInInconsistency dc;
    788 
    789     qs = TALER_AUDITORDB_get_kycauth_in_inconsistency (
    790       TALER_ARL_adb,
    791       wire_reference,
    792       &dc);
    793     switch (qs)
    794     {
    795     case GNUNET_DB_STATUS_HARD_ERROR:
    796     case GNUNET_DB_STATUS_SOFT_ERROR:
    797       global_qs = qs;
    798       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    799       return GNUNET_SYSERR;
    800     case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    801       break;
    802     case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    803       {
    804         bool healed = false;
    805 
    806         if (TALER_amount_is_zero (&dc.amount_exchange_expected))
    807         {
    808           /* An earlier run saw this transfer at the bank while the
    809              exchange had not booked it yet.  If the two now agree, that
    810              finding was a transient and we retract it. */
    811           healed = check_kycauth_equality (&dc.amount_wired,
    812                                            credit,
    813                                            &dc.account_pub,
    814                                            account_pub,
    815                                            dc.account,
    816                                            sender_account_details,
    817                                            GNUNET_TIME_absolute_to_timestamp (
    818                                              dc.timestamp),
    819                                            execution_date);
    820         }
    821         GNUNET_free (dc.account.full_payto);
    822         GNUNET_free (dc.diagnostic);
    823         if (healed)
    824         {
    825           qs = TALER_AUDITORDB_delete_kycauth_in_inconsistency (
    826             TALER_ARL_adb,
    827             dc.serial_id);
    828           if (qs < 0)
    829           {
    830             global_qs = qs;
    831             GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    832             return GNUNET_SYSERR;
    833           }
    834           /* We are retracting the finding, so the excess credit we booked
    835              when we made it has to go as well -- otherwise a transfer the
    836              exchange merely booked late inflates this balance forever. */
    837           TALER_ARL_amount_subtract (
    838             &TALER_ARL_USE_AB (total_bad_amount_in_plus),
    839             &TALER_ARL_USE_AB (total_bad_amount_in_plus),
    840             &dc.amount_wired);
    841           /* The bank side of this transfer was accounted for when the
    842              finding was made, so do not expect it again. */
    843           return GNUNET_OK;
    844         }
    845         break;
    846       }
    847     }
    848   }
    849   slen = (NULL == sender_account_details.full_payto)
    850     ? 0
    851     : strlen (sender_account_details.full_payto) + 1;
    852   kii = GNUNET_malloc (sizeof (struct KycauthInInfo) + slen);
    853   kii->rowid = rowid;
    854   kii->account_pub = *account_pub;
    855   kii->credit = *credit;
    856   kii->execution_date = execution_date;
    857   if (0 != slen)
    858   {
    859     kii->sender_account_details.full_payto = (char *) &kii[1];
    860     GNUNET_memcpy (&kii[1],
    861                    sender_account_details.full_payto,
    862                    slen);
    863   }
    864   GNUNET_CRYPTO_hash (&wire_reference,
    865                       sizeof (uint64_t),
    866                       &kii->row_off_hash);
    867   if (GNUNET_OK !=
    868       GNUNET_CONTAINER_multihashmap_put (
    869         kycauth_map,
    870         &kii->row_off_hash,
    871         kii,
    872         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
    873   {
    874     struct TALER_AUDITORDB_RowInconsistency ri = {
    875       .problem_row_id = rowid,
    876       .row_table = (char *) "kycauths_in",
    877       .diagnostic = (char *) "duplicate wire offset"
    878     };
    879     enum GNUNET_DB_QueryStatus qs;
    880 
    881     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    882                 "Duplicate wire offset\n");
    883     qs = TALER_AUDITORDB_insert_row_inconsistency (
    884       TALER_ARL_adb,
    885       &ri);
    886     GNUNET_free (kii);
    887     if (qs < 0)
    888     {
    889       global_qs = qs;
    890       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    891       return GNUNET_SYSERR;
    892     }
    893   }
    894   return GNUNET_OK;
    895 }
    896 
    897 
    898 /**
    899  * Function called with details about incoming wire transfers
    900  * as claimed by the exchange DB.
    901  *
    902  * @param cls a `struct WireAccount` we are processing
    903  * @param rowid unique serial ID for the entry in our DB
    904  * @param reserve_pub public key of the reserve (also the WTID)
    905  * @param credit amount that was received
    906  * @param sender_account_details payto://-URL of the sender's bank account
    907  * @param wire_reference unique identifier for the wire transfer
    908  * @param execution_date when did we receive the funds
    909  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop
    910  */
    911 static enum GNUNET_GenericReturnValue
    912 reserve_in_cb (void *cls,
    913                uint64_t rowid,
    914                const struct TALER_ReservePublicKeyP *reserve_pub,
    915                const struct TALER_Amount *credit,
    916                const struct TALER_FullPayto sender_account_details,
    917                uint64_t wire_reference,
    918                struct GNUNET_TIME_Timestamp execution_date)
    919 {
    920   struct WireAccount *wa = cls;
    921   struct ReserveInInfo *rii;
    922   size_t slen;
    923 
    924   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    925               "Analyzing exchange wire IN (%llu) at %s of %s with reserve_pub %s\n",
    926               (unsigned long long) rowid,
    927               GNUNET_TIME_timestamp2s (execution_date),
    928               TALER_amount2s (credit),
    929               TALER_B2S (reserve_pub));
    930   /* Advance the cursor up front: every path below has fully accounted for
    931      this row, and reading it a second time would count the credit twice. */
    932   wa->last_reserve_in_serial_id = rowid + 1;
    933   TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_wire_in),
    934                         &TALER_ARL_USE_AB (total_wire_in),
    935                         credit);
    936   {
    937     enum GNUNET_DB_QueryStatus qs;
    938     struct TALER_AUDITORDB_ReserveInInconsistency dc;
    939 
    940     qs = TALER_AUDITORDB_get_reserve_in_inconsistency (
    941       TALER_ARL_adb,
    942       wire_reference,
    943       &dc);
    944     switch (qs)
    945     {
    946     case GNUNET_DB_STATUS_HARD_ERROR:
    947     case GNUNET_DB_STATUS_SOFT_ERROR:
    948       global_qs = qs;
    949       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    950       return GNUNET_SYSERR;
    951     case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    952       break;
    953     case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    954       {
    955         enum GNUNET_GenericReturnValue ret = GNUNET_NO;
    956 
    957         if (TALER_amount_is_zero (&dc.amount_exchange_expected))
    958         {
    959           /* database entry indicates unmatched transaction */
    960           ret = check_equality (&dc.amount_wired,
    961                                 credit,
    962                                 &dc.reserve_pub,
    963                                 reserve_pub,
    964                                 dc.account,
    965                                 sender_account_details,
    966                                 GNUNET_TIME_absolute_to_timestamp (dc.timestamp)
    967                                 ,
    968                                 execution_date);
    969         }
    970         GNUNET_free (dc.account.full_payto);
    971         GNUNET_free (dc.diagnostic);
    972         if (GNUNET_SYSERR == ret)
    973           return GNUNET_SYSERR;
    974         if (GNUNET_YES == ret)
    975         {
    976           qs = TALER_AUDITORDB_delete_reserve_in_inconsistency (
    977             TALER_ARL_adb,
    978             dc.serial_id);
    979           if (qs < 0)
    980           {
    981             global_qs = qs;
    982             GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    983             return GNUNET_SYSERR;
    984           }
    985           /* We are retracting the finding, so the excess credit we booked
    986              when we made it has to go as well -- otherwise a transfer the
    987              exchange merely booked late inflates this balance forever. */
    988           TALER_ARL_amount_subtract (
    989             &TALER_ARL_USE_AB (total_bad_amount_in_plus),
    990             &TALER_ARL_USE_AB (total_bad_amount_in_plus),
    991             &dc.amount_wired);
    992           return GNUNET_OK;
    993         }
    994         break;
    995       }
    996     }
    997   }
    998   slen = strlen (sender_account_details.full_payto) + 1;
    999   rii = GNUNET_malloc (sizeof (struct ReserveInInfo) + slen);
   1000   rii->rowid = rowid;
   1001   rii->credit_details.type = TALER_BANK_CT_RESERVE;
   1002   rii->credit_details.amount = *credit;
   1003   rii->credit_details.execution_date = execution_date;
   1004   rii->credit_details.details.reserve.reserve_pub = *reserve_pub;
   1005   rii->credit_details.debit_account_uri.full_payto = (char *) &rii[1];
   1006   GNUNET_memcpy (&rii[1],
   1007                  sender_account_details.full_payto,
   1008                  slen);
   1009   GNUNET_CRYPTO_hash (&wire_reference,
   1010                       sizeof (uint64_t),
   1011                       &rii->row_off_hash);
   1012   if (GNUNET_OK !=
   1013       GNUNET_CONTAINER_multihashmap_put (in_map,
   1014                                          &rii->row_off_hash,
   1015                                          rii,
   1016                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
   1017   {
   1018     struct TALER_AUDITORDB_RowInconsistency ri = {
   1019       .problem_row_id = rowid,
   1020       .row_table = (char *) "reserves_in",
   1021       .diagnostic = (char *) "duplicate wire offset"
   1022     };
   1023     enum GNUNET_DB_QueryStatus qs;
   1024 
   1025     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1026                 "Duplicate wire offset\n");
   1027     qs = TALER_AUDITORDB_insert_row_inconsistency (
   1028       TALER_ARL_adb,
   1029       &ri);
   1030     GNUNET_free (rii);
   1031     if (qs < 0)
   1032     {
   1033       global_qs = qs;
   1034       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1035       return GNUNET_SYSERR;
   1036     }
   1037     return GNUNET_OK;
   1038   }
   1039   return GNUNET_OK;
   1040 }
   1041 
   1042 
   1043 /**
   1044  * Complain that we failed to match an entry from #in_map.
   1045  *
   1046  * @param cls a `struct WireAccount`
   1047  * @param key unused key
   1048  * @param value the `struct ReserveInInfo` to free
   1049  * @return #GNUNET_OK
   1050  */
   1051 static enum GNUNET_GenericReturnValue
   1052 complain_in_not_found (void *cls,
   1053                        const struct GNUNET_HashCode *key,
   1054                        void *value)
   1055 {
   1056   struct WireAccount *wa = cls;
   1057   struct ReserveInInfo *rii = value;
   1058   enum GNUNET_DB_QueryStatus qs;
   1059   struct TALER_AUDITORDB_ReserveInInconsistency riiDb = {
   1060     .bank_row_id = rii->rowid,
   1061     .diagnostic = (char *)
   1062                   "incoming wire transfer claimed by exchange not found",
   1063     .account = wa->ai->payto_uri,
   1064     .amount_exchange_expected = rii->credit_details.amount,
   1065     .amount_wired = zero,
   1066     .reserve_pub = rii->credit_details.details.reserve.reserve_pub,
   1067     .timestamp = rii->credit_details.execution_date.abs_time
   1068   };
   1069 
   1070   (void) key;
   1071   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1072               "Incoming wire transfer #%llu claimed by exchange not found\n",
   1073               (unsigned long long) rii->rowid);
   1074   GNUNET_assert (TALER_BANK_CT_RESERVE ==
   1075                  rii->credit_details.type);
   1076   qs = TALER_AUDITORDB_insert_reserve_in_inconsistency (
   1077     TALER_ARL_adb,
   1078     &riiDb);
   1079   if (qs < 0)
   1080   {
   1081     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1082     global_qs = qs;
   1083     return GNUNET_SYSERR;
   1084   }
   1085   TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_bad_amount_in_minus),
   1086                         &TALER_ARL_USE_AB (total_bad_amount_in_minus),
   1087                         &rii->credit_details.amount);
   1088   return GNUNET_OK;
   1089 }
   1090 
   1091 
   1092 /**
   1093  * Complain that we failed to match an entry from #kycauth_map: the
   1094  * exchange booked a KYCAUTH transfer the bank never made.  Beyond the
   1095  * missing money, this means the exchange let an account authenticate
   1096  * without anyone ever having proven that they control it.
   1097  *
   1098  * @param cls a `struct WireAccount`
   1099  * @param key unused key
   1100  * @param value the `struct KycauthInInfo` we could not match
   1101  * @return #GNUNET_OK
   1102  */
   1103 static enum GNUNET_GenericReturnValue
   1104 complain_kycauth_in_not_found (void *cls,
   1105                                const struct GNUNET_HashCode *key,
   1106                                void *value)
   1107 {
   1108   struct WireAccount *wa = cls;
   1109   struct KycauthInInfo *kii = value;
   1110   enum GNUNET_DB_QueryStatus qs;
   1111   struct TALER_AUDITORDB_KycauthInInconsistency kiiDb = {
   1112     .bank_row_id = kii->rowid,
   1113     .diagnostic = (char *)
   1114                   "KYCAUTH wire transfer claimed by exchange not found",
   1115     .account = (NULL == kii->sender_account_details.full_payto)
   1116                ? wa->ai->payto_uri
   1117                : kii->sender_account_details,
   1118     .amount_exchange_expected = kii->credit,
   1119     .amount_wired = zero,
   1120     .account_pub = kii->account_pub,
   1121     .timestamp = kii->execution_date.abs_time
   1122   };
   1123 
   1124   (void) key;
   1125   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1126               "KYCAUTH wire transfer #%llu claimed by exchange not found\n",
   1127               (unsigned long long) kii->rowid);
   1128   qs = TALER_AUDITORDB_insert_kycauth_in_inconsistency (
   1129     TALER_ARL_adb,
   1130     &kiiDb);
   1131   if (qs < 0)
   1132   {
   1133     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1134     global_qs = qs;
   1135     return GNUNET_SYSERR;
   1136   }
   1137   TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_bad_amount_in_minus),
   1138                         &TALER_ARL_USE_AB (total_bad_amount_in_minus),
   1139                         &kii->credit);
   1140   return GNUNET_OK;
   1141 }
   1142 
   1143 
   1144 /**
   1145  * Start processing the next wire account.
   1146  * Shuts down if we are done.
   1147  *
   1148  * @param cls `struct WireAccount` with a wire account list to process
   1149  */
   1150 static void
   1151 process_credits (void *cls);
   1152 
   1153 
   1154 /**
   1155  * We got all of the incoming transactions for @a wa,
   1156  * finish processing the account.
   1157  *
   1158  * @param[in,out] wa wire account to process
   1159  */
   1160 static void
   1161 conclude_account (struct WireAccount *wa)
   1162 {
   1163   GNUNET_assert (NULL == wa->chh);
   1164   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1165               "Reconciling CREDIT processing of account `%s'\n",
   1166               wa->ai->section_name);
   1167   if (NULL != in_map)
   1168   {
   1169     GNUNET_CONTAINER_multihashmap_iterate (in_map,
   1170                                            &complain_in_not_found,
   1171                                            wa);
   1172     /* clean up before 2nd phase */
   1173     GNUNET_CONTAINER_multihashmap_iterate (in_map,
   1174                                            &free_rii,
   1175                                            NULL);
   1176     if (global_qs < 0)
   1177     {
   1178       commit (global_qs);
   1179       return;
   1180     }
   1181   }
   1182   if (NULL != kycauth_map)
   1183   {
   1184     GNUNET_CONTAINER_multihashmap_iterate (kycauth_map,
   1185                                            &complain_kycauth_in_not_found,
   1186                                            wa);
   1187     /* clean up before 2nd phase */
   1188     GNUNET_CONTAINER_multihashmap_iterate (kycauth_map,
   1189                                            &free_kii,
   1190                                            NULL);
   1191     if (global_qs < 0)
   1192     {
   1193       commit (global_qs);
   1194       return;
   1195     }
   1196   }
   1197   process_credits (wa->next);
   1198 }
   1199 
   1200 
   1201 /**
   1202  * Analyze a KYCAUTH credit the bank reports against what the exchange
   1203  * booked in its `kycauths_in` table (collected in #kycauth_map).
   1204  *
   1205  * @param[in,out] wa account that received the transfer
   1206  * @param credit_details transfer details as reported by the bank
   1207  * @return true on success, false to stop loop at this point
   1208  */
   1209 static bool
   1210 analyze_kycauth_credit (
   1211   struct WireAccount *wa,
   1212   const struct TALER_BANK_CreditDetails *credit_details)
   1213 {
   1214   struct KycauthInInfo *kii;
   1215   struct GNUNET_HashCode key;
   1216 
   1217   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1218               "Analyzing bank KYCAUTH CREDIT #%llu at %s of %s\n",
   1219               (unsigned long long) credit_details->serial_id,
   1220               GNUNET_TIME_timestamp2s (credit_details->execution_date),
   1221               TALER_amount2s (&credit_details->amount));
   1222   TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_kycauth_in),
   1223                         &TALER_ARL_USE_AB (total_kycauth_in),
   1224                         &credit_details->amount);
   1225   /* the bank charges its credit fee on this transfer just like on
   1226      any other, so it must be part of the total either way */
   1227   TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_wire_credit_fees),
   1228                         &TALER_ARL_USE_AB (total_wire_credit_fees),
   1229                         &credit_details->credit_fee);
   1230   GNUNET_CRYPTO_hash (&credit_details->serial_id,
   1231                       sizeof (credit_details->serial_id),
   1232                       &key);
   1233   kii = GNUNET_CONTAINER_multihashmap_get (kycauth_map,
   1234                                            &key);
   1235   if (NULL == kii)
   1236   {
   1237     struct TALER_AUDITORDB_KycauthInInconsistency dc = {
   1238       .bank_row_id = credit_details->serial_id,
   1239       .amount_exchange_expected = zero,
   1240       .amount_wired = credit_details->amount,
   1241       .account_pub = credit_details->details.kycauth.account_pub,
   1242       .timestamp = credit_details->execution_date.abs_time,
   1243       .account = credit_details->debit_account_uri,
   1244       .diagnostic = (char *) "unknown to exchange"
   1245     };
   1246     enum GNUNET_DB_QueryStatus qs;
   1247 
   1248     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1249                 "Failed to find KYCAUTH transfer at `%s' in exchange database.\n",
   1250                 GNUNET_TIME_timestamp2s (credit_details->execution_date));
   1251     qs = TALER_AUDITORDB_insert_kycauth_in_inconsistency (TALER_ARL_adb,
   1252                                                           &dc);
   1253     if (qs <= 0)
   1254     {
   1255       global_qs = qs;
   1256       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1257       return false;
   1258     }
   1259     TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_bad_amount_in_plus),
   1260                           &TALER_ARL_USE_AB (total_bad_amount_in_plus),
   1261                           &credit_details->amount);
   1262     /* We have fully processed this credit: the finding is on file, and
   1263        kycauth_in_cb() retracts it if the exchange books the transfer
   1264        later.  Looking at it again would only duplicate the finding. */
   1265     wa->wire_off_in = credit_details->serial_id;
   1266     return true;
   1267   }
   1268 
   1269   /* Update offset */
   1270   wa->wire_off_in = credit_details->serial_id;
   1271   if (0 != TALER_amount_cmp (&kii->credit,
   1272                              &credit_details->amount))
   1273   {
   1274     struct TALER_AUDITORDB_KycauthInInconsistency dc = {
   1275       .bank_row_id = credit_details->serial_id,
   1276       .amount_exchange_expected = kii->credit,
   1277       .amount_wired = credit_details->amount,
   1278       .account_pub = kii->account_pub,
   1279       .timestamp = kii->execution_date.abs_time,
   1280       .account = wa->ai->payto_uri,
   1281       .diagnostic = (char *) "wire amount does not match"
   1282     };
   1283     enum GNUNET_DB_QueryStatus qs;
   1284 
   1285     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1286                 "KYCAUTH transfer amount differs\n");
   1287     qs = TALER_AUDITORDB_insert_kycauth_in_inconsistency (
   1288       TALER_ARL_adb,
   1289       &dc);
   1290     if (qs <= 0)
   1291     {
   1292       global_qs = qs;
   1293       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1294       return false;
   1295     }
   1296     if (0 < TALER_amount_cmp (&credit_details->amount,
   1297                               &kii->credit))
   1298     {
   1299       struct TALER_Amount delta;
   1300 
   1301       TALER_ARL_amount_subtract (&delta,
   1302                                  &credit_details->amount,
   1303                                  &kii->credit);
   1304       TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_bad_amount_in_plus),
   1305                             &TALER_ARL_USE_AB (total_bad_amount_in_plus),
   1306                             &delta);
   1307     }
   1308     else
   1309     {
   1310       struct TALER_Amount delta;
   1311 
   1312       TALER_ARL_amount_subtract (&delta,
   1313                                  &kii->credit,
   1314                                  &credit_details->amount);
   1315       TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_bad_amount_in_minus),
   1316                             &TALER_ARL_USE_AB (total_bad_amount_in_minus),
   1317                             &delta);
   1318     }
   1319   }
   1320   if (0 != GNUNET_memcmp (&kii->account_pub,
   1321                           &credit_details->details.kycauth.account_pub))
   1322   {
   1323     /* No money is missing, but the exchange associated a different key
   1324        with the account than the one the sender asked for: the wrong party
   1325        can now authenticate as the owner of that bank account. */
   1326     struct TALER_AUDITORDB_KycauthInInconsistency dc = {
   1327       .bank_row_id = credit_details->serial_id,
   1328       .amount_exchange_expected = kii->credit,
   1329       .amount_wired = credit_details->amount,
   1330       .account_pub = kii->account_pub,
   1331       .timestamp = kii->execution_date.abs_time,
   1332       .account = wa->ai->payto_uri,
   1333       .diagnostic = (char *) "account public key does not match"
   1334     };
   1335     enum GNUNET_DB_QueryStatus qs;
   1336 
   1337     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1338                 "KYCAUTH account public key differs\n");
   1339     qs = TALER_AUDITORDB_insert_kycauth_in_inconsistency (
   1340       TALER_ARL_adb,
   1341       &dc);
   1342     if (qs <= 0)
   1343     {
   1344       global_qs = qs;
   1345       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1346       return false;
   1347     }
   1348   }
   1349   if (! same_account (kii->sender_account_details,
   1350                       credit_details->debit_account_uri))
   1351   {
   1352     /* Again no money is missing: the exchange authorised the wrong bank
   1353        account.  There is no reserve to close towards it either, so this
   1354        does not feed into #total_misattribution_in. */
   1355     struct TALER_AUDITORDB_KycauthInInconsistency dc = {
   1356       .bank_row_id = credit_details->serial_id,
   1357       .amount_exchange_expected = kii->credit,
   1358       .amount_wired = credit_details->amount,
   1359       .account_pub = kii->account_pub,
   1360       .timestamp = kii->execution_date.abs_time,
   1361       .account = credit_details->debit_account_uri,
   1362       .diagnostic = (char *) "debited account does not match"
   1363     };
   1364     enum GNUNET_DB_QueryStatus qs;
   1365 
   1366     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1367                 "Origin bank account of KYCAUTH transfer differs\n");
   1368     qs = TALER_AUDITORDB_insert_kycauth_in_inconsistency (
   1369       TALER_ARL_adb,
   1370       &dc);
   1371     if (qs <= 0)
   1372     {
   1373       global_qs = qs;
   1374       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1375       return false;
   1376     }
   1377   }
   1378   if (GNUNET_TIME_timestamp_cmp (credit_details->execution_date,
   1379                                  !=,
   1380                                  kii->execution_date))
   1381   {
   1382     struct TALER_AUDITORDB_RowMinorInconsistencies rmi = {
   1383       .problem_row = kii->rowid,
   1384       .diagnostic = (char *) "execution date mismatch",
   1385       .row_table = (char *) "kycauths_in"
   1386     };
   1387     enum GNUNET_DB_QueryStatus qs;
   1388 
   1389     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1390                 "Execution date of KYCAUTH transfer differs\n");
   1391     qs = TALER_AUDITORDB_insert_row_minor_inconsistencies (
   1392       TALER_ARL_adb,
   1393       &rmi);
   1394     if (qs < 0)
   1395     {
   1396       global_qs = qs;
   1397       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1398       return false;
   1399     }
   1400   }
   1401   GNUNET_assert (GNUNET_OK ==
   1402                  free_kii (NULL,
   1403                            &key,
   1404                            kii));
   1405   return true;
   1406 }
   1407 
   1408 
   1409 /**
   1410  * Analyze credit transaction @a details into @a wa.
   1411  *
   1412  * @param[in,out] wa account that received the transfer
   1413  * @param credit_details transfer details
   1414  * @return true on success, false to stop loop at this point
   1415  */
   1416 static bool
   1417 analyze_credit (
   1418   struct WireAccount *wa,
   1419   const struct TALER_BANK_CreditDetails *credit_details)
   1420 {
   1421   struct ReserveInInfo *rii;
   1422   struct GNUNET_HashCode key;
   1423 
   1424   switch (credit_details->type)
   1425   {
   1426   case TALER_BANK_CT_RESERVE:
   1427     break;
   1428   case TALER_BANK_CT_KYCAUTH:
   1429     return analyze_kycauth_credit (wa,
   1430                                    credit_details);
   1431   case TALER_BANK_CT_WAD:
   1432     GNUNET_break (0); /* FIXME: Wad not yet supported */
   1433     return false;
   1434   }
   1435   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1436               "Analyzing bank CREDIT #%llu at %s of %s with Reserve-pub %s\n",
   1437               (unsigned long long) credit_details->serial_id,
   1438               GNUNET_TIME_timestamp2s (credit_details->execution_date),
   1439               TALER_amount2s (&credit_details->amount),
   1440               TALER_B2S (&credit_details->details.reserve.reserve_pub));
   1441   TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_wire_credit_fees),
   1442                         &TALER_ARL_USE_AB (total_wire_credit_fees),
   1443                         &credit_details->credit_fee);
   1444   GNUNET_CRYPTO_hash (&credit_details->serial_id,
   1445                       sizeof (credit_details->serial_id),
   1446                       &key);
   1447   rii = GNUNET_CONTAINER_multihashmap_get (in_map,
   1448                                            &key);
   1449   if (NULL == rii)
   1450   {
   1451     struct TALER_AUDITORDB_ReserveInInconsistency dc = {
   1452       .bank_row_id = credit_details->serial_id,
   1453       .amount_exchange_expected = zero,
   1454       .amount_wired = credit_details->amount,
   1455       .reserve_pub = credit_details->details.reserve.reserve_pub,
   1456       .timestamp = credit_details->execution_date.abs_time,
   1457       .account = credit_details->debit_account_uri,
   1458       .diagnostic = (char *) "unknown to exchange"
   1459     };
   1460     enum GNUNET_DB_QueryStatus qs;
   1461 
   1462     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1463                 "Failed to find wire transfer at `%s' in exchange database.\n",
   1464                 GNUNET_TIME_timestamp2s (credit_details->execution_date));
   1465     qs = TALER_AUDITORDB_insert_reserve_in_inconsistency (TALER_ARL_adb,
   1466                                                           &dc);
   1467     if (qs <= 0)
   1468     {
   1469       global_qs = qs;
   1470       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1471       return false;
   1472     }
   1473     TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_bad_amount_in_plus),
   1474                           &TALER_ARL_USE_AB (total_bad_amount_in_plus),
   1475                           &credit_details->amount);
   1476     /* We have fully processed this credit: the finding is on file, and
   1477        reserve_in_cb() retracts it if the exchange books the transfer
   1478        later.  Looking at it again would only duplicate the finding. */
   1479     wa->wire_off_in = credit_details->serial_id;
   1480     return true;
   1481   }
   1482 
   1483   /* Update offset */
   1484   wa->wire_off_in = credit_details->serial_id;
   1485   /* compare records with expected data */
   1486   if (0 != GNUNET_memcmp (&credit_details->details.reserve.reserve_pub,
   1487                           &rii->credit_details.details.reserve.reserve_pub))
   1488   {
   1489     struct TALER_AUDITORDB_ReserveInInconsistency riiDb = {
   1490       .bank_row_id = credit_details->serial_id,
   1491       .amount_exchange_expected = rii->credit_details.amount,
   1492       .amount_wired = zero,
   1493       .reserve_pub = rii->credit_details.details.reserve.reserve_pub,
   1494       .timestamp = rii->credit_details.execution_date.abs_time,
   1495       .account = wa->ai->payto_uri,
   1496       .diagnostic = (char *) "wire subject does not match"
   1497     };
   1498     enum GNUNET_DB_QueryStatus qs;
   1499 
   1500     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1501                 "Reserve public key differs\n");
   1502     qs = TALER_AUDITORDB_insert_reserve_in_inconsistency (
   1503       TALER_ARL_adb,
   1504       &riiDb);
   1505     if (qs <= 0)
   1506     {
   1507       global_qs = qs;
   1508       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1509       return false;
   1510     }
   1511     TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_bad_amount_in_minus),
   1512                           &TALER_ARL_USE_AB (total_bad_amount_in_minus),
   1513                           &rii->credit_details.amount);
   1514     TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_bad_amount_in_plus),
   1515                           &TALER_ARL_USE_AB (total_bad_amount_in_plus),
   1516                           &credit_details->amount);
   1517     GNUNET_assert (GNUNET_OK ==
   1518                    free_rii (NULL,
   1519                              &key,
   1520                              rii));
   1521     return true;
   1522   }
   1523   if (0 != TALER_amount_cmp (&rii->credit_details.amount,
   1524                              &credit_details->amount))
   1525   {
   1526     struct TALER_AUDITORDB_ReserveInInconsistency riiDb = {
   1527       .diagnostic = (char *) "wire amount does not match",
   1528       .account = wa->ai->payto_uri,
   1529       .bank_row_id = credit_details->serial_id,
   1530       .amount_exchange_expected = rii->credit_details.amount,
   1531       .amount_wired = credit_details->amount,
   1532       .reserve_pub = rii->credit_details.details.reserve.reserve_pub,
   1533       .timestamp = rii->credit_details.execution_date.abs_time
   1534     };
   1535     enum GNUNET_DB_QueryStatus qs;
   1536 
   1537     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1538                 "Wire transfer amount differs\n");
   1539     qs = TALER_AUDITORDB_insert_reserve_in_inconsistency (
   1540       TALER_ARL_adb,
   1541       &riiDb);
   1542     GNUNET_assert (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != qs);
   1543     if (qs < 0)
   1544     {
   1545       global_qs = qs;
   1546       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1547       return false;
   1548     }
   1549     if (0 < TALER_amount_cmp (&credit_details->amount,
   1550                               &rii->credit_details.amount))
   1551     {
   1552       /* details->amount > rii->details.amount: wire transfer was larger than it should have been */
   1553       struct TALER_Amount delta;
   1554 
   1555       TALER_ARL_amount_subtract (&delta,
   1556                                  &credit_details->amount,
   1557                                  &rii->credit_details.amount);
   1558       TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_bad_amount_in_plus),
   1559                             &TALER_ARL_USE_AB (total_bad_amount_in_plus),
   1560                             &delta);
   1561     }
   1562     else
   1563     {
   1564       /* rii->details.amount < details->amount: wire transfer was smaller than it should have been */
   1565       struct TALER_Amount delta;
   1566 
   1567       TALER_ARL_amount_subtract (&delta,
   1568                                  &rii->credit_details.amount,
   1569                                  &credit_details->amount);
   1570       TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_bad_amount_in_minus),
   1571                             &TALER_ARL_USE_AB (total_bad_amount_in_minus),
   1572                             &delta);
   1573     }
   1574   }
   1575 
   1576   {
   1577     struct TALER_NormalizedPayto np;
   1578     struct TALER_NormalizedPayto np2;
   1579 
   1580     np = TALER_payto_normalize (credit_details->debit_account_uri);
   1581     np2 = TALER_payto_normalize (rii->credit_details.debit_account_uri);
   1582     if (0 != TALER_normalized_payto_cmp (np,
   1583                                          np2))
   1584     {
   1585       struct TALER_AUDITORDB_MisattributionInInconsistency mii = {
   1586         .reserve_pub = rii->credit_details.details.reserve.reserve_pub,
   1587         .amount = rii->credit_details.amount,
   1588         .bank_row = credit_details->serial_id
   1589       };
   1590       enum GNUNET_DB_QueryStatus qs;
   1591 
   1592       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1593                   "Origin bank account differs\n");
   1594       qs = TALER_AUDITORDB_insert_misattribution_in_inconsistency (
   1595         TALER_ARL_adb,
   1596         &mii);
   1597       GNUNET_assert (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != qs);
   1598       if (qs < 0)
   1599       {
   1600         global_qs = qs;
   1601         GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1602         GNUNET_free (np.normalized_payto);
   1603         GNUNET_free (np2.normalized_payto);
   1604         return false;
   1605       }
   1606       TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_misattribution_in),
   1607                             &TALER_ARL_USE_AB (total_misattribution_in),
   1608                             &rii->credit_details.amount);
   1609     }
   1610     GNUNET_free (np.normalized_payto);
   1611     GNUNET_free (np2.normalized_payto);
   1612   }
   1613   if (GNUNET_TIME_timestamp_cmp (credit_details->execution_date,
   1614                                  !=,
   1615                                  rii->credit_details.execution_date))
   1616   {
   1617     struct TALER_AUDITORDB_RowMinorInconsistencies rmi = {
   1618       .problem_row = rii->rowid,
   1619       .diagnostic = (char *) "execution date mismatch",
   1620       .row_table = (char *) "reserves_in"
   1621     };
   1622     enum GNUNET_DB_QueryStatus qs;
   1623 
   1624     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1625                 "Execution date differs\n");
   1626     qs = TALER_AUDITORDB_insert_row_minor_inconsistencies (
   1627       TALER_ARL_adb,
   1628       &rmi);
   1629 
   1630     if (qs < 0)
   1631     {
   1632       global_qs = qs;
   1633       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1634       return false;
   1635     }
   1636   }
   1637   GNUNET_assert (GNUNET_OK ==
   1638                  free_rii (NULL,
   1639                            &key,
   1640                            rii));
   1641   return true;
   1642 }
   1643 
   1644 
   1645 /**
   1646  * This function is called for all transactions that
   1647  * are credited to the exchange's account (incoming
   1648  * transactions).
   1649  *
   1650  * @param cls `struct WireAccount` we are processing
   1651  * @param chr HTTP response returned by the bank
   1652  */
   1653 static void
   1654 history_credit_cb (void *cls,
   1655                    const struct TALER_BANK_CreditHistoryResponse *chr)
   1656 {
   1657   struct WireAccount *wa = cls;
   1658 
   1659   wa->chh = NULL;
   1660   switch (chr->http_status)
   1661   {
   1662   case MHD_HTTP_OK:
   1663     for (unsigned int i = 0; i < chr->details.ok.details_length; i++)
   1664     {
   1665       const struct TALER_BANK_CreditDetails *cd
   1666         = &chr->details.ok.details[i];
   1667 
   1668       if (! analyze_credit (wa,
   1669                             cd))
   1670       {
   1671         switch (global_qs)
   1672         {
   1673         case GNUNET_DB_STATUS_SOFT_ERROR:
   1674           rollback_and_reset ();
   1675           return;
   1676         case GNUNET_DB_STATUS_HARD_ERROR:
   1677           GNUNET_SCHEDULER_shutdown ();
   1678           return;
   1679         case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
   1680           /* perfectly fine */
   1681           break;
   1682         case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
   1683           /* perfectly fine */
   1684           break;
   1685         }
   1686         break;
   1687       }
   1688     }
   1689     conclude_account (wa);
   1690     return;
   1691   case MHD_HTTP_NO_CONTENT:
   1692     conclude_account (wa);
   1693     return;
   1694   case MHD_HTTP_NOT_FOUND:
   1695     if (ignore_account_404)
   1696     {
   1697       conclude_account (wa);
   1698       return;
   1699     }
   1700     break;
   1701   default:
   1702     break;
   1703   }
   1704   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   1705               "Error fetching credit history of account %s: %u (%s)\n",
   1706               wa->ai->section_name,
   1707               chr->http_status,
   1708               TALER_ErrorCode_get_hint (chr->ec));
   1709   commit (GNUNET_DB_STATUS_HARD_ERROR);
   1710   global_ret = EXIT_FAILURE;
   1711   GNUNET_SCHEDULER_shutdown ();
   1712 }
   1713 
   1714 
   1715 /* ***************************** Setup logic ************************ */
   1716 
   1717 
   1718 /**
   1719  * Start processing the next wire account.
   1720  * Shuts down if we are done.
   1721  *
   1722  * @param cls `struct WireAccount` with a wire account list to process
   1723  */
   1724 static void
   1725 process_credits (void *cls)
   1726 {
   1727   struct WireAccount *wa = cls;
   1728   enum GNUNET_DB_QueryStatus qs;
   1729 
   1730   /* skip accounts where CREDIT is not enabled */
   1731   while ( (NULL != wa) &&
   1732           (GNUNET_NO == wa->ai->credit_enabled) )
   1733     wa = wa->next;
   1734   if (NULL == wa)
   1735   {
   1736     /* done with all accounts, conclude check */
   1737     conclude_credit_history ();
   1738     return;
   1739   }
   1740   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1741               "Analyzing exchange's wire IN table for account `%s'\n",
   1742               wa->ai->section_name);
   1743   qs = TALER_EXCHANGEDB_iterate_reserves_in_above_serial_id_by_account (
   1744     TALER_ARL_edb,
   1745     wa->ai->section_name,
   1746     wa->last_reserve_in_serial_id,
   1747     &reserve_in_cb,
   1748     wa);
   1749   if (0 > qs)
   1750   {
   1751     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1752     global_ret = EXIT_FAILURE;
   1753     GNUNET_SCHEDULER_shutdown ();
   1754     return;
   1755   }
   1756 
   1757   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1758               "Analyzing exchange's KYCAUTH IN table for account `%s'\n",
   1759               wa->ai->section_name);
   1760   qs = TALER_EXCHANGEDB_iterate_kycauth_in_above_serial_id_by_account (
   1761     TALER_ARL_edb,
   1762     wa->ai->section_name,
   1763     wa->last_kycauth_in_serial_id,
   1764     &kycauth_in_cb,
   1765     wa);
   1766   if (0 > qs)
   1767   {
   1768     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1769     global_ret = EXIT_FAILURE;
   1770     GNUNET_SCHEDULER_shutdown ();
   1771     return;
   1772   }
   1773 
   1774   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1775               "Starting bank CREDIT history of account `%s'\n",
   1776               wa->ai->section_name);
   1777   GNUNET_assert (NULL == wa->chh);
   1778   wa->chh = TALER_BANK_credit_history (ctx,
   1779                                        wa->ai->auth,
   1780                                        wa->wire_off_in,
   1781                                        MAX_PER_TRANSACTION,
   1782                                        GNUNET_TIME_UNIT_ZERO,
   1783                                        &history_credit_cb,
   1784                                        wa);
   1785   if (NULL == wa->chh)
   1786   {
   1787     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   1788                 "Failed to obtain bank transaction history\n");
   1789     commit (GNUNET_DB_STATUS_HARD_ERROR);
   1790     global_ret = EXIT_FAILURE;
   1791     GNUNET_SCHEDULER_shutdown ();
   1792     return;
   1793   }
   1794 }
   1795 
   1796 
   1797 /**
   1798  * Begin audit of CREDITs to the exchange.
   1799  */
   1800 static void
   1801 begin_credit_audit (void)
   1802 {
   1803   GNUNET_assert (NULL == in_map);
   1804   in_map = GNUNET_CONTAINER_multihashmap_create (1024,
   1805                                                  GNUNET_YES);
   1806   GNUNET_assert (NULL == kycauth_map);
   1807   kycauth_map = GNUNET_CONTAINER_multihashmap_create (1024,
   1808                                                       GNUNET_YES);
   1809   /* now go over all bank accounts and check delta with in_map */
   1810   process_credits (wa_head);
   1811 }
   1812 
   1813 
   1814 static enum GNUNET_DB_QueryStatus
   1815 begin_transaction (void)
   1816 {
   1817   enum GNUNET_DB_QueryStatus qs;
   1818 
   1819   if (GNUNET_SYSERR ==
   1820       TALER_EXCHANGEDB_preflight (TALER_ARL_edb))
   1821   {
   1822     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   1823                 "Failed to initialize exchange database connection.\n");
   1824     return GNUNET_DB_STATUS_HARD_ERROR;
   1825   }
   1826   if (GNUNET_SYSERR ==
   1827       TALER_AUDITORDB_preflight (TALER_ARL_adb))
   1828   {
   1829     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   1830                 "Failed to initialize auditor database session.\n");
   1831     return GNUNET_DB_STATUS_HARD_ERROR;
   1832   }
   1833   global_qs = GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
   1834   if (GNUNET_OK !=
   1835       TALER_AUDITORDB_start (TALER_ARL_adb,
   1836                              "auditor-wire-credit"))
   1837   {
   1838     GNUNET_break (0);
   1839     return GNUNET_DB_STATUS_HARD_ERROR;
   1840   }
   1841   if (GNUNET_OK !=
   1842       TALER_EXCHANGEDB_start_read_only (TALER_ARL_edb,
   1843                                         "wire credit auditor"))
   1844   {
   1845     GNUNET_break (0);
   1846     return GNUNET_DB_STATUS_HARD_ERROR;
   1847   }
   1848   qs = TALER_AUDITORDB_get_balance (
   1849     TALER_ARL_adb,
   1850     TALER_ARL_GET_AB (total_wire_in),
   1851     TALER_ARL_GET_AB (total_kycauth_in),
   1852     TALER_ARL_GET_AB (total_kycauth_revenue),
   1853     TALER_ARL_GET_AB (total_wire_credit_fees),
   1854     TALER_ARL_GET_AB (total_bad_amount_in_plus),
   1855     TALER_ARL_GET_AB (total_bad_amount_in_minus),
   1856     TALER_ARL_GET_AB (total_misattribution_in),
   1857     NULL);
   1858   switch (qs)
   1859   {
   1860   case GNUNET_DB_STATUS_HARD_ERROR:
   1861     GNUNET_break (0);
   1862     return qs;
   1863   case GNUNET_DB_STATUS_SOFT_ERROR:
   1864     GNUNET_break (0);
   1865     return qs;
   1866   case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
   1867   case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
   1868     break;
   1869   }
   1870   for (struct WireAccount *wa = wa_head;
   1871        NULL != wa;
   1872        wa = wa->next)
   1873   {
   1874     /* we are called again by rollback_and_reset() after a serialization
   1875        failure, so the labels may already have been computed */
   1876     GNUNET_free (wa->label_reserve_in_serial_id);
   1877     GNUNET_free (wa->label_kycauth_in_serial_id);
   1878     GNUNET_free (wa->label_wire_off_in);
   1879     GNUNET_asprintf (&wa->label_reserve_in_serial_id,
   1880                      "wire-%s-%s",
   1881                      wa->ai->section_name,
   1882                      "reserve_in_serial_id");
   1883     GNUNET_asprintf (&wa->label_kycauth_in_serial_id,
   1884                      "wire-%s-%s",
   1885                      wa->ai->section_name,
   1886                      "kycauth_in_serial_id");
   1887     GNUNET_asprintf (&wa->label_wire_off_in,
   1888                      "wire-%s-%s",
   1889                      wa->ai->section_name,
   1890                      "wire_off_in");
   1891     qs = TALER_AUDITORDB_get_auditor_progress (
   1892       TALER_ARL_adb,
   1893       wa->label_reserve_in_serial_id,
   1894       &wa->last_reserve_in_serial_id,
   1895       wa->label_kycauth_in_serial_id,
   1896       &wa->last_kycauth_in_serial_id,
   1897       wa->label_wire_off_in,
   1898       &wa->wire_off_in,
   1899       NULL);
   1900     if (0 > qs)
   1901     {
   1902       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
   1903       return qs;
   1904     }
   1905     wa->start_reserve_in_serial_id = wa->last_reserve_in_serial_id;
   1906     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1907                 "Starting from reserve_in at %s=%llu for account `%s'\n",
   1908                 wa->label_reserve_in_serial_id,
   1909                 (unsigned long long) wa->start_reserve_in_serial_id,
   1910                 wa->ai->section_name);
   1911   }
   1912 
   1913   begin_credit_audit ();
   1914   return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
   1915 }
   1916 
   1917 
   1918 /**
   1919  * Function called with information about a wire account.  Adds the
   1920  * account to our list for processing (if it is enabled and we can
   1921  * load the plugin).
   1922  *
   1923  * @param cls closure, NULL
   1924  * @param ai account information
   1925  */
   1926 static void
   1927 process_account_cb (void *cls,
   1928                     const struct TALER_EXCHANGEDB_AccountInfo *ai)
   1929 {
   1930   struct WireAccount *wa;
   1931 
   1932   (void) cls;
   1933   if ((! ai->debit_enabled) &&
   1934       (! ai->credit_enabled))
   1935     return; /* not an active exchange account */
   1936   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1937               "Found exchange account `%s'\n",
   1938               ai->section_name);
   1939   wa = GNUNET_new (struct WireAccount);
   1940   wa->ai = ai;
   1941   GNUNET_CONTAINER_DLL_insert (wa_head,
   1942                                wa_tail,
   1943                                wa);
   1944 }
   1945 
   1946 
   1947 /**
   1948  * Function called on events received from Postgres.
   1949  *
   1950  * @param cls closure, NULL
   1951  * @param extra additional event data provided
   1952  * @param extra_size number of bytes in @a extra
   1953  */
   1954 static void
   1955 db_notify (void *cls,
   1956            const void *extra,
   1957            size_t extra_size)
   1958 {
   1959   (void) cls;
   1960   (void) extra;
   1961   (void) extra_size;
   1962 
   1963   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1964               "Received notification to wake wire helper\n");
   1965   if (NULL != in_map)
   1966   {
   1967     /* An audit is still running.  Concluding its accounts now would
   1968        report every transfer whose bank record we have not yet fetched
   1969        as missing, so let it finish and start another one afterwards. */
   1970     rerun = true;
   1971     return;
   1972   }
   1973   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
   1974       begin_transaction ())
   1975   {
   1976     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   1977                 "Audit failed\n");
   1978     GNUNET_break (0);
   1979     global_ret = EXIT_FAILURE;
   1980     GNUNET_SCHEDULER_shutdown ();
   1981   }
   1982 }
   1983 
   1984 
   1985 /**
   1986  * Main function that will be run.
   1987  *
   1988  * @param cls closure
   1989  * @param args remaining command-line arguments
   1990  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
   1991  * @param c configuration
   1992  */
   1993 static void
   1994 run (void *cls,
   1995      char *const *args,
   1996      const char *cfgfile,
   1997      const struct GNUNET_CONFIGURATION_Handle *c)
   1998 {
   1999   (void) cls;
   2000   (void) args;
   2001   (void) cfgfile;
   2002   cfg = c;
   2003   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
   2004               "Launching wire-credit auditor\n");
   2005   if (EXIT_SUCCESS !=
   2006       (global_ret = TALER_ARL_init (c)))
   2007   {
   2008     return;
   2009   }
   2010   if (GNUNET_OK !=
   2011       TALER_config_get_amount (TALER_ARL_cfg,
   2012                                "auditor",
   2013                                "TINY_AMOUNT",
   2014                                &tiny_amount))
   2015   {
   2016     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
   2017                                "auditor",
   2018                                "TINY_AMOUNT");
   2019     global_ret = EXIT_NOTCONFIGURED;
   2020     return;
   2021   }
   2022   GNUNET_assert (GNUNET_OK ==
   2023                  TALER_amount_set_zero (TALER_ARL_currency,
   2024                                         &zero));
   2025   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
   2026                                  NULL);
   2027   ctx = GNUNET_CURL_init (&GNUNET_CURL_gnunet_scheduler_reschedule,
   2028                           &rc);
   2029   rc = GNUNET_CURL_gnunet_rc_create (ctx);
   2030   if (NULL == ctx)
   2031   {
   2032     GNUNET_break (0);
   2033     global_ret = EXIT_FAILURE;
   2034     return;
   2035   }
   2036   if (GNUNET_OK !=
   2037       TALER_EXCHANGEDB_load_accounts (TALER_ARL_cfg,
   2038                                       TALER_EXCHANGEDB_ALO_CREDIT
   2039                                       | TALER_EXCHANGEDB_ALO_AUTHDATA))
   2040   {
   2041     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   2042                 "No bank accounts configured\n");
   2043     global_ret = EXIT_NOTCONFIGURED;
   2044     GNUNET_SCHEDULER_shutdown ();
   2045     return;
   2046   }
   2047   TALER_EXCHANGEDB_find_accounts (&process_account_cb,
   2048                                   NULL);
   2049 
   2050   if (0 == test_mode)
   2051   {
   2052     struct GNUNET_DB_EventHeaderP es = {
   2053       .size = htons (sizeof (es)),
   2054       .type = htons (TALER_DBEVENT_EXCHANGE_AUDITOR_WAKE_HELPER_WIRE)
   2055     };
   2056 
   2057     eh = TALER_AUDITORDB_event_listen (TALER_ARL_adb,
   2058                                        &es,
   2059                                        GNUNET_TIME_UNIT_FOREVER_REL,
   2060                                        &db_notify,
   2061                                        NULL);
   2062     GNUNET_assert (NULL != eh);
   2063   }
   2064   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
   2065       begin_transaction ())
   2066   {
   2067     GNUNET_break (0);
   2068     global_ret = EXIT_FAILURE;
   2069     GNUNET_SCHEDULER_shutdown ();
   2070     return;
   2071   }
   2072 }
   2073 
   2074 
   2075 /**
   2076  * The main function of the wire auditing tool. Checks that
   2077  * the exchange's records of wire transfers match that of
   2078  * the wire gateway.
   2079  *
   2080  * @param argc number of arguments from the command line
   2081  * @param argv command line arguments
   2082  * @return 0 ok, 1 on error
   2083  */
   2084 int
   2085 main (int argc,
   2086       char *const *argv)
   2087 {
   2088   const struct GNUNET_GETOPT_CommandLineOption options[] = {
   2089     GNUNET_GETOPT_option_flag ('i',
   2090                                "internal",
   2091                                "perform checks only applicable for exchange-internal audits",
   2092                                &internal_checks),
   2093     GNUNET_GETOPT_option_flag ('I',
   2094                                "ignore-not-found",
   2095                                "continue, even if the bank account of the exchange was not found",
   2096                                &ignore_account_404),
   2097     GNUNET_GETOPT_option_flag ('t',
   2098                                "test",
   2099                                "run in test mode and exit when idle",
   2100                                &test_mode),
   2101     GNUNET_GETOPT_option_timetravel ('T',
   2102                                      "timetravel"),
   2103     GNUNET_GETOPT_OPTION_END
   2104   };
   2105   enum GNUNET_GenericReturnValue ret;
   2106 
   2107   ret = GNUNET_PROGRAM_run (
   2108     TALER_AUDITOR_project_data (),
   2109     argc,
   2110     argv,
   2111     "taler-helper-auditor-wire-credit",
   2112     gettext_noop (
   2113       "Audit exchange database for consistency with the bank's wire transfers"),
   2114     options,
   2115     &run,
   2116     NULL);
   2117   if (GNUNET_SYSERR == ret)
   2118     return EXIT_NOTCONFIGURED;
   2119   if (GNUNET_NO == ret)
   2120     return EXIT_SUCCESS;
   2121   return global_ret;
   2122 }
   2123 
   2124 
   2125 /* end of taler-helper-auditor-wire-credit.c */