LCOV - code coverage report
Current view: top level - auditor - taler-helper-auditor-deposits.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 73 93 78.5 %
Date: 2021-08-30 06:43:37 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2016-2021 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-deposits.c
      18             :  * @brief audits an exchange database for deposit confirmation consistency
      19             :  * @author Christian Grothoff
      20             :  *
      21             :  * We simply check that all of the deposit confirmations reported to us
      22             :  * by merchants were also reported to us by the exchange.
      23             :  */
      24             : #include "platform.h"
      25             : #include <gnunet/gnunet_util_lib.h>
      26             : #include "taler_auditordb_plugin.h"
      27             : #include "taler_exchangedb_lib.h"
      28             : #include "taler_json_lib.h"
      29             : #include "taler_bank_service.h"
      30             : #include "taler_signatures.h"
      31             : #include "report-lib.h"
      32             : 
      33             : 
      34             : /**
      35             :  * Return value from main().
      36             :  */
      37             : static int global_ret;
      38             : 
      39             : /**
      40             :  * Array of reports about missing deposit confirmations.
      41             :  */
      42             : static json_t *report_deposit_confirmation_inconsistencies;
      43             : 
      44             : /**
      45             :  * Total number of deposit confirmations that we did not get.
      46             :  */
      47             : static json_int_t number_missed_deposit_confirmations;
      48             : 
      49             : /**
      50             :  * Total amount involved in deposit confirmations that we did not get.
      51             :  */
      52             : static struct TALER_Amount total_missed_deposit_confirmations;
      53             : 
      54             : /**
      55             :  * Should we run checks that only work for exchange-internal audits?
      56             :  */
      57             : static int internal_checks;
      58             : 
      59             : /**
      60             :  * Closure for #test_dc.
      61             :  */
      62             : struct DepositConfirmationContext
      63             : {
      64             : 
      65             :   /**
      66             :    * How many deposit confirmations did we NOT find in the #TALER_ARL_edb?
      67             :    */
      68             :   unsigned long long missed_count;
      69             : 
      70             :   /**
      71             :    * What is the total amount missing?
      72             :    */
      73             :   struct TALER_Amount missed_amount;
      74             : 
      75             :   /**
      76             :    * Lowest SerialID of the first coin we missed? (This is where we
      77             :    * should resume next time).
      78             :    */
      79             :   uint64_t first_missed_coin_serial;
      80             : 
      81             :   /**
      82             :    * Lowest SerialID of the first coin we missed? (This is where we
      83             :    * should resume next time).
      84             :    */
      85             :   uint64_t last_seen_coin_serial;
      86             : 
      87             :   /**
      88             :    * Success or failure of (exchange) database operations within
      89             :    * #test_dc.
      90             :    */
      91             :   enum GNUNET_DB_QueryStatus qs;
      92             : 
      93             : };
      94             : 
      95             : 
      96             : /**
      97             :  * Given a deposit confirmation from #TALER_ARL_adb, check that it is also
      98             :  * in #TALER_ARL_edb.  Update the deposit confirmation context accordingly.
      99             :  *
     100             :  * @param cls our `struct DepositConfirmationContext`
     101             :  * @param serial_id row of the @a dc in the database
     102             :  * @param dc the deposit confirmation we know
     103             :  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop iterating
     104             :  */
     105             : static int
     106         118 : test_dc (void *cls,
     107             :          uint64_t serial_id,
     108             :          const struct TALER_AUDITORDB_DepositConfirmation *dc)
     109             : {
     110         118 :   struct DepositConfirmationContext *dcc = cls;
     111             : 
     112         118 :   dcc->last_seen_coin_serial = serial_id;
     113             :   {
     114             :     enum GNUNET_DB_QueryStatus qs;
     115         118 :     struct TALER_EXCHANGEDB_Deposit dep = {
     116             :       .coin.coin_pub = dc->coin_pub,
     117             :       .h_contract_terms = dc->h_contract_terms,
     118             :       .merchant_pub = dc->merchant,
     119             :       .h_wire = dc->h_wire,
     120             :       .refund_deadline = dc->refund_deadline
     121             :     };
     122             :     struct GNUNET_TIME_Absolute exchange_timestamp;
     123             :     struct TALER_Amount deposit_fee;
     124             : 
     125         118 :     qs = TALER_ARL_edb->have_deposit (TALER_ARL_edb->cls,
     126             :                                       &dep,
     127             :                                       GNUNET_NO /* do not check refund deadline */,
     128             :                                       &deposit_fee,
     129             :                                       &exchange_timestamp);
     130         118 :     if (qs > 0)
     131             :     {
     132         108 :       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     133             :                   "Found deposit %s in exchange database\n",
     134             :                   GNUNET_h2s (&dc->h_contract_terms));
     135         108 :       if (TALER_ARL_do_abort ())
     136         108 :         return GNUNET_SYSERR;
     137         108 :       return GNUNET_OK; /* found, all good */
     138             :     }
     139          10 :     if (qs < 0)
     140             :     {
     141           0 :       GNUNET_break (0); /* DB error, complain */
     142           0 :       dcc->qs = qs;
     143           0 :       return GNUNET_SYSERR;
     144             :     }
     145             :   }
     146             :   /* deposit confirmation missing! report! */
     147          10 :   TALER_ARL_report (report_deposit_confirmation_inconsistencies,
     148          10 :                     GNUNET_JSON_PACK (
     149             :                       TALER_JSON_pack_time_abs_human ("timestamp",
     150             :                                                       dc->exchange_timestamp),
     151             :                       TALER_JSON_pack_amount ("amount",
     152             :                                               &dc->amount_without_fee),
     153             :                       GNUNET_JSON_pack_uint64 ("rowid",
     154             :                                                serial_id),
     155             :                       GNUNET_JSON_pack_data_auto ("account",
     156             :                                                   &dc->h_wire)));
     157          10 :   dcc->first_missed_coin_serial = GNUNET_MIN (dcc->first_missed_coin_serial,
     158             :                                               serial_id);
     159          10 :   dcc->missed_count++;
     160          10 :   TALER_ARL_amount_add (&dcc->missed_amount,
     161             :                         &dcc->missed_amount,
     162             :                         &dc->amount_without_fee);
     163          10 :   if (TALER_ARL_do_abort ())
     164           0 :     return GNUNET_SYSERR;
     165          10 :   return GNUNET_OK;
     166             : }
     167             : 
     168             : 
     169             : /**
     170             :  * Check that the deposit-confirmations that were reported to
     171             :  * us by merchants are also in the exchange's database.
     172             :  *
     173             :  * @param cls closure
     174             :  * @return transaction status code
     175             :  */
     176             : static enum GNUNET_DB_QueryStatus
     177          83 : analyze_deposit_confirmations (void *cls)
     178             : {
     179             :   struct TALER_AUDITORDB_ProgressPointDepositConfirmation ppdc;
     180             :   struct DepositConfirmationContext dcc;
     181             :   enum GNUNET_DB_QueryStatus qs;
     182             :   enum GNUNET_DB_QueryStatus qsx;
     183             :   enum GNUNET_DB_QueryStatus qsp;
     184             : 
     185             :   (void) cls;
     186          83 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     187             :               "Analyzing deposit confirmations\n");
     188          83 :   ppdc.last_deposit_confirmation_serial_id = 0;
     189          83 :   qsp = TALER_ARL_adb->get_auditor_progress_deposit_confirmation (
     190          83 :     TALER_ARL_adb->cls,
     191             :     &TALER_ARL_master_pub,
     192             :     &ppdc);
     193          83 :   if (0 > qsp)
     194             :   {
     195           0 :     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qsp);
     196           0 :     return qsp;
     197             :   }
     198          83 :   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qsp)
     199             :   {
     200          41 :     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
     201             :                 "First analysis using deposit auditor, starting audit from scratch\n");
     202             :   }
     203             :   else
     204             :   {
     205          42 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     206             :                 "Resuming deposit confirmation audit at %llu\n",
     207             :                 (unsigned long long) ppdc.last_deposit_confirmation_serial_id);
     208             :   }
     209             : 
     210             :   /* setup 'cc' */
     211          83 :   GNUNET_assert (GNUNET_OK ==
     212             :                  TALER_amount_set_zero (TALER_ARL_currency,
     213             :                                         &dcc.missed_amount));
     214          83 :   dcc.qs = GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
     215          83 :   dcc.missed_count = 0LLU;
     216          83 :   dcc.first_missed_coin_serial = UINT64_MAX;
     217          83 :   qsx = TALER_ARL_adb->get_deposit_confirmations (
     218          83 :     TALER_ARL_adb->cls,
     219             :     &TALER_ARL_master_pub,
     220             :     ppdc.last_deposit_confirmation_serial_id,
     221             :     &test_dc,
     222             :     &dcc);
     223          83 :   if (0 > qsx)
     224             :   {
     225           0 :     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qsx);
     226           0 :     return qsx;
     227             :   }
     228          83 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     229             :               "Analyzed %d deposit confirmations (above serial ID %llu)\n",
     230             :               (int) qsx,
     231             :               (unsigned long long) ppdc.last_deposit_confirmation_serial_id);
     232          83 :   if (0 > dcc.qs)
     233             :   {
     234           0 :     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == dcc.qs);
     235           0 :     return dcc.qs;
     236             :   }
     237          83 :   if (UINT64_MAX == dcc.first_missed_coin_serial)
     238          77 :     ppdc.last_deposit_confirmation_serial_id = dcc.last_seen_coin_serial;
     239             :   else
     240           6 :     ppdc.last_deposit_confirmation_serial_id = dcc.first_missed_coin_serial - 1;
     241             : 
     242             :   /* sync 'cc' back to disk */
     243          83 :   if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qsp)
     244          42 :     qs = TALER_ARL_adb->update_auditor_progress_deposit_confirmation (
     245          42 :       TALER_ARL_adb->cls,
     246             :       &TALER_ARL_master_pub,
     247             :       &ppdc);
     248             :   else
     249          41 :     qs = TALER_ARL_adb->insert_auditor_progress_deposit_confirmation (
     250          41 :       TALER_ARL_adb->cls,
     251             :       &TALER_ARL_master_pub,
     252             :       &ppdc);
     253          83 :   if (0 >= qs)
     254             :   {
     255           0 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     256             :                 "Failed to update auditor DB, not recording progress\n");
     257           0 :     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
     258           0 :     return qs;
     259             :   }
     260          83 :   number_missed_deposit_confirmations = (json_int_t) dcc.missed_count;
     261          83 :   total_missed_deposit_confirmations = dcc.missed_amount;
     262             : 
     263          83 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     264             :               "Concluded deposit confirmation audit step at %llu\n",
     265             :               (unsigned long long) ppdc.last_deposit_confirmation_serial_id);
     266          83 :   return qs;
     267             : }
     268             : 
     269             : 
     270             : /**
     271             :  * Main function that will be run.
     272             :  *
     273             :  * @param cls closure
     274             :  * @param args remaining command-line arguments
     275             :  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
     276             :  * @param c configuration
     277             :  */
     278             : static void
     279          83 : run (void *cls,
     280             :      char *const *args,
     281             :      const char *cfgfile,
     282             :      const struct GNUNET_CONFIGURATION_Handle *c)
     283             : {
     284             :   (void) cls;
     285             :   (void) args;
     286             :   (void) cfgfile;
     287          83 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     288             :               "Launching deposit auditor\n");
     289          83 :   if (GNUNET_OK !=
     290          83 :       TALER_ARL_init (c))
     291             :   {
     292           0 :     global_ret = EXIT_FAILURE;
     293           0 :     return;
     294             :   }
     295          83 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     296             :               "Starting deposit audit\n");
     297          83 :   GNUNET_assert (NULL !=
     298             :                  (report_deposit_confirmation_inconsistencies = json_array ()));
     299          83 :   if (GNUNET_OK !=
     300          83 :       TALER_ARL_setup_sessions_and_run (&analyze_deposit_confirmations,
     301             :                                         NULL))
     302             :   {
     303           0 :     global_ret = EXIT_FAILURE;
     304           0 :     return;
     305             :   }
     306          83 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     307             :               "Deposit audit complete\n");
     308          83 :   TALER_ARL_done (
     309          83 :     GNUNET_JSON_PACK (
     310             :       GNUNET_JSON_pack_array_steal ("deposit_confirmation_inconsistencies",
     311             :                                     report_deposit_confirmation_inconsistencies),
     312             :       GNUNET_JSON_pack_uint64 ("missing_deposit_confirmation_count",
     313             :                                number_missed_deposit_confirmations),
     314             :       TALER_JSON_pack_amount ("missing_deposit_confirmation_total",
     315             :                               &total_missed_deposit_confirmations),
     316             :       TALER_JSON_pack_time_abs_human ("auditor_start_time",
     317             :                                       start_time),
     318             :       TALER_JSON_pack_time_abs_human ("auditor_end_time",
     319             :                                       GNUNET_TIME_absolute_get ())));
     320             : }
     321             : 
     322             : 
     323             : /**
     324             :  * The main function of the deposit auditing helper tool.
     325             :  *
     326             :  * @param argc number of arguments from the command line
     327             :  * @param argv command line arguments
     328             :  * @return 0 ok, 1 on error
     329             :  */
     330             : int
     331          83 : main (int argc,
     332             :       char *const *argv)
     333             : {
     334          83 :   const struct GNUNET_GETOPT_CommandLineOption options[] = {
     335          83 :     GNUNET_GETOPT_option_flag ('i',
     336             :                                "internal",
     337             :                                "perform checks only applicable for exchange-internal audits",
     338             :                                &internal_checks),
     339          83 :     GNUNET_GETOPT_option_base32_auto ('m',
     340             :                                       "exchange-key",
     341             :                                       "KEY",
     342             :                                       "public key of the exchange (Crockford base32 encoded)",
     343             :                                       &TALER_ARL_master_pub),
     344          83 :     GNUNET_GETOPT_option_timetravel ('T',
     345             :                                      "timetravel"),
     346             :     GNUNET_GETOPT_OPTION_END
     347             :   };
     348             :   enum GNUNET_GenericReturnValue ret;
     349             : 
     350             :   /* force linker to link against libtalerutil; if we do
     351             :      not do this, the linker may "optimize" libtalerutil
     352             :      away and skip #TALER_OS_init(), which we do need */
     353          83 :   (void) TALER_project_data_default ();
     354          83 :   if (GNUNET_OK !=
     355          83 :       GNUNET_STRINGS_get_utf8_args (argc, argv,
     356             :                                     &argc, &argv))
     357           0 :     return EXIT_INVALIDARGUMENT;
     358          83 :   ret = GNUNET_PROGRAM_run (
     359             :     argc,
     360             :     argv,
     361             :     "taler-helper-auditor-deposits",
     362             :     gettext_noop (
     363             :       "Audit Taler exchange database for deposit confirmation consistency"),
     364             :     options,
     365             :     &run,
     366             :     NULL);
     367          83 :   GNUNET_free_nz ((void *) argv);
     368          83 :   if (GNUNET_SYSERR == ret)
     369           0 :     return EXIT_INVALIDARGUMENT;
     370          83 :   if (GNUNET_NO == ret)
     371           0 :     return EXIT_SUCCESS;
     372          83 :   return global_ret;
     373             : }
     374             : 
     375             : 
     376             : /* end of taler-helper-auditor-deposits.c */

Generated by: LCOV version 1.14