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-deposits.c (16710B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2016-2025 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU Affero Public License for more details.
     12 
     13   You should have received a copy of the GNU Affero Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file auditor/taler-helper-auditor-deposits.c
     18  * @brief audits an exchange database for deposit confirmation consistency
     19  * @author Christian Grothoff
     20  * @author Nic Eigel
     21  *
     22  * We simply check that all of the deposit confirmations reported to us
     23  * by merchants were also reported to us by the exchange.
     24  */
     25 #include "platform.h"
     26 #include <gnunet/gnunet_util_lib.h>
     27 #include "auditordb_lib.h"
     28 #include "exchangedb_lib.h"
     29 #include "taler/taler_bank_service.h"
     30 #include "report-lib.h"
     31 #include "taler/taler_dbevents.h"
     32 #include <jansson.h>
     33 #include <inttypes.h>
     34 #include "auditor-database/delete_generic.h"
     35 #include "auditor-database/event_listen.h"
     36 #include "auditor-database/get_auditor_progress.h"
     37 #include "auditor-database/get_balance.h"
     38 #include "auditor-database/iterate_deposit_confirmations.h"
     39 #include "auditor-database/insert_auditor_progress.h"
     40 #include "auditor-database/insert_balance.h"
     41 #include "exchange-database/get_exists_deposit.h"
     42 
     43 /*
     44 --
     45 -- SELECT serial_id,h_contract_terms,h_wire,merchant_pub ...
     46 --   FROM auditor.auditor_deposit_confirmations
     47 --   WHERE NOT ancient
     48 --    ORDER BY exchange_timestamp ASC;
     49 --  SELECT 1
     50 -      FROM exchange.deposits dep
     51        WHERE ($RESULT.contract_terms = dep.h_contract_terms) AND ($RESULT.h_wire = dep.h_wire) AND ...);
     52 -- IF FOUND
     53 -- DELETE FROM auditor.auditor_deposit_confirmations
     54 --   WHERE serial_id = $RESULT.serial_id;
     55 -- SELECT exchange_timestamp AS latest
     56 --   FROM exchange.deposits ORDER BY exchange_timestamp DESC;
     57 -- latest -= 1 hour; // time is not exactly monotonic...
     58 -- UPDATE auditor.deposit_confirmations
     59 --   SET ancient=TRUE
     60 --  WHERE exchange_timestamp < latest
     61 --    AND NOT ancient;
     62 */
     63 
     64 /**
     65  * Return value from main().
     66  */
     67 static int global_ret;
     68 
     69 /**
     70  * Row ID until which we have added up missing deposit confirmations
     71  * in the total_missed_deposit_confirmations amount. Missing deposit
     72  * confirmations above this value need to be added, and if any appear
     73  * below this value we should subtract them from the reported amount.
     74  */
     75 static TALER_ARL_DEF_PP (deposit_confirmation_serial_id);
     76 
     77 /**
     78  * Run in test mode. Exit when idle instead of
     79  * going to sleep and waiting for more work.
     80  */
     81 static int test_mode;
     82 
     83 /**
     84  * Total amount involved in deposit confirmations that we did not get.
     85  */
     86 static TALER_ARL_DEF_AB (total_missed_deposit_confirmations);
     87 
     88 /**
     89  * Should we run checks that only work for exchange-internal audits?
     90  * Does nothing for this helper (present only for uniformity).
     91  */
     92 static int internal_checks;
     93 
     94 /**
     95  * Handler to wake us up on new deposits in the exchange database.
     96  */
     97 static struct GNUNET_DB_EventHandler *eh;
     98 
     99 /**
    100  * Handler to wake us up on new deposit confirmations reported
    101  * by merchants (NOTIFY'ed by the trigger on
    102  * auditor_deposit_confirmations).
    103  */
    104 static struct GNUNET_DB_EventHandler *eh_dc;
    105 
    106 /**
    107  * The auditors's configuration.
    108  */
    109 static const struct GNUNET_CONFIGURATION_Handle *cfg;
    110 
    111 /**
    112  * Success or failure of (exchange) database operations within
    113  * #test_dc and #recheck_dc.
    114  */
    115 static enum GNUNET_DB_QueryStatus eqs;
    116 
    117 
    118 /**
    119  * Given a deposit confirmation from #TALER_ARL_adb, check that it is also
    120  * in #TALER_ARL_edb.  Update the deposit confirmation context accordingly.
    121  *
    122  * @param cls NULL
    123  * @param dc the deposit confirmation we know
    124  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop iterating
    125  */
    126 static enum GNUNET_GenericReturnValue
    127 test_dc (void *cls,
    128          const struct TALER_AUDITORDB_DepositConfirmation *dc)
    129 {
    130   bool missing = false;
    131   enum GNUNET_DB_QueryStatus qs;
    132 
    133   (void) cls;
    134   TALER_ARL_USE_PP (deposit_confirmation_serial_id) = dc->row_id;
    135   for (unsigned int i = 0; i < dc->num_coins; i++)
    136   {
    137     struct GNUNET_TIME_Timestamp exchange_timestamp;
    138     struct TALER_Amount deposit_fee;
    139 
    140     qs = TALER_EXCHANGEDB_get_exists_deposit (TALER_ARL_edb,
    141                                               &dc->h_contract_terms,
    142                                               &dc->h_wire,
    143                                               &dc->coin_pubs[i],
    144                                               &dc->merchant,
    145                                               dc->refund_deadline,
    146                                               &deposit_fee,
    147                                               &exchange_timestamp);
    148     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    149                 "Status for deposit confirmation %llu-%u is %d\n",
    150                 (unsigned long long) dc->row_id,
    151                 i,
    152                 qs);
    153     missing |= (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs);
    154     if (qs < 0)
    155     {
    156       GNUNET_break (0); /* DB error, complain */
    157       eqs = qs;
    158       return GNUNET_SYSERR;
    159     }
    160   }
    161   if (! missing)
    162   {
    163     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    164                 "Deleting matching deposit confirmation %llu\n",
    165                 (unsigned long long) dc->row_id);
    166     qs = TALER_AUDITORDB_delete_generic (
    167       TALER_ARL_adb,
    168       TALER_AUDITORDB_DEPOSIT_CONFIRMATION,
    169       dc->row_id);
    170     if (qs < 0)
    171     {
    172       GNUNET_break (0); /* DB error, complain */
    173       eqs = qs;
    174       return GNUNET_SYSERR;
    175     }
    176     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    177                 "Found deposit %s in exchange database\n",
    178                 GNUNET_h2s (&dc->h_contract_terms.hash));
    179     return GNUNET_OK; /* all coins found, all good */
    180   }
    181   TALER_ARL_amount_add (&TALER_ARL_USE_AB (total_missed_deposit_confirmations),
    182                         &TALER_ARL_USE_AB (total_missed_deposit_confirmations),
    183                         &dc->total_without_fee);
    184   return GNUNET_OK;
    185 }
    186 
    187 
    188 /**
    189  * Given a previously missing deposit confirmation from #TALER_ARL_adb, check
    190  * *again* whether it is now in #TALER_ARL_edb.  Update the deposit
    191  * confirmation context accordingly.
    192  *
    193  * @param cls NULL
    194  * @param dc the deposit confirmation we know
    195  * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop iterating
    196  */
    197 static enum GNUNET_GenericReturnValue
    198 recheck_dc (void *cls,
    199             const struct TALER_AUDITORDB_DepositConfirmation *dc)
    200 {
    201   bool missing = false;
    202   enum GNUNET_DB_QueryStatus qs;
    203 
    204   (void) cls;
    205   for (unsigned int i = 0; i < dc->num_coins; i++)
    206   {
    207     struct GNUNET_TIME_Timestamp exchange_timestamp;
    208     struct TALER_Amount deposit_fee;
    209 
    210     qs = TALER_EXCHANGEDB_get_exists_deposit (TALER_ARL_edb,
    211                                               &dc->h_contract_terms,
    212                                               &dc->h_wire,
    213                                               &dc->coin_pubs[i],
    214                                               &dc->merchant,
    215                                               dc->refund_deadline,
    216                                               &deposit_fee,
    217                                               &exchange_timestamp);
    218     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    219                 "Status for deposit confirmation %llu-%u is %d on re-check\n",
    220                 (unsigned long long) dc->row_id,
    221                 i,
    222                 qs);
    223     missing |= (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs);
    224     if (qs < 0)
    225     {
    226       GNUNET_break (0); /* DB error, complain */
    227       eqs = qs;
    228       return GNUNET_SYSERR;
    229     }
    230   }
    231   if (! missing)
    232   {
    233     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    234                 "Deleting matching deposit confirmation %llu\n",
    235                 (unsigned long long) dc->row_id);
    236     qs = TALER_AUDITORDB_delete_generic (
    237       TALER_ARL_adb,
    238       TALER_AUDITORDB_DEPOSIT_CONFIRMATION,
    239       dc->row_id);
    240     if (qs < 0)
    241     {
    242       GNUNET_break (0); /* DB error, complain */
    243       eqs = qs;
    244       return GNUNET_SYSERR;
    245     }
    246     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    247                 "Previously missing deposit %s appeared in exchange database\n",
    248                 GNUNET_h2s (&dc->h_contract_terms.hash));
    249     /* It appeared, so *reduce* total missing balance */
    250     TALER_ARL_amount_subtract (&TALER_ARL_USE_AB (
    251                                  total_missed_deposit_confirmations),
    252                                &TALER_ARL_USE_AB (
    253                                  total_missed_deposit_confirmations),
    254                                &dc->total_without_fee);
    255     return GNUNET_OK; /* all coins found, all good */
    256   }
    257   /* still missing, no change to totalmissing balance */
    258   return GNUNET_OK;
    259 }
    260 
    261 
    262 /**
    263  * Check that the deposit-confirmations that were reported to
    264  * us by merchants are also in the exchange's database.
    265  *
    266  * @param cls closure
    267  * @return transaction status code
    268  */
    269 static enum GNUNET_DB_QueryStatus
    270 analyze_deposit_confirmations (void *cls)
    271 {
    272   enum GNUNET_DB_QueryStatus qs;
    273   bool had_pp;
    274   bool had_bal;
    275   bool had_missing;
    276   uint64_t pp;
    277 
    278   (void) cls;
    279   /* Reset the shared exchange-DB status accumulator: it is a file-scope
    280      static written by the test_dc/recheck_dc callbacks; if a previous
    281      (possibly retried) run left it negative, failing to reset it here would
    282      permanently wedge this helper via the `0 > eqs` checks below. */
    283   eqs = GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
    284   qs = TALER_AUDITORDB_get_auditor_progress (
    285     TALER_ARL_adb,
    286     TALER_ARL_GET_PP (deposit_confirmation_serial_id),
    287     NULL);
    288   if (0 > qs)
    289   {
    290     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    291     return qs;
    292   }
    293   had_pp = (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != qs);
    294   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    295               "Resuming deposit confirmation audit at %llu\n",
    296               (unsigned long long) TALER_ARL_USE_PP (
    297                 deposit_confirmation_serial_id));
    298   pp = TALER_ARL_USE_PP (deposit_confirmation_serial_id);
    299   qs = TALER_AUDITORDB_get_balance (
    300     TALER_ARL_adb,
    301     TALER_ARL_GET_AB (total_missed_deposit_confirmations),
    302     NULL);
    303   if (0 > qs)
    304   {
    305     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    306     return qs;
    307   }
    308   had_bal = (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs);
    309   had_missing = ! TALER_amount_is_zero (
    310     &TALER_ARL_USE_AB (total_missed_deposit_confirmations));
    311   qs = TALER_AUDITORDB_iterate_deposit_confirmations (
    312     TALER_ARL_adb,
    313     INT64_MAX,
    314     TALER_ARL_USE_PP (deposit_confirmation_serial_id),
    315     true, /* return suppressed */
    316     &test_dc,
    317     NULL);
    318   if (0 > qs)
    319   {
    320     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    321     return qs;
    322   }
    323   if (0 > eqs)
    324   {
    325     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == eqs);
    326     return eqs;
    327   }
    328   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    329               "Analyzed %d deposit confirmations\n",
    330               (int) qs);
    331   qs = TALER_AUDITORDB_insert_auditor_progress (
    332     TALER_ARL_adb,
    333     TALER_ARL_SET_PP (deposit_confirmation_serial_id),
    334     NULL);
    335   if (0 > qs)
    336   {
    337     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    338                 "Failed to update auditor DB, not recording progress\n");
    339     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    340     return qs;
    341   }
    342   if (had_bal && had_pp && had_missing)
    343   {
    344     qs = TALER_AUDITORDB_iterate_deposit_confirmations (
    345       TALER_ARL_adb,
    346       -INT64_MAX,
    347       pp + 1, /* previous iteration went up to 'pp', try missing again */
    348       true, /* return suppressed */
    349       &recheck_dc,
    350       NULL);
    351     if (0 > qs)
    352     {
    353       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    354       return qs;
    355     }
    356     if (0 > eqs)
    357     {
    358       GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == eqs);
    359       return eqs;
    360     }
    361     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    362                 "Re-analyzed %d deposit confirmations\n",
    363                 (int) qs);
    364   }
    365   qs = TALER_AUDITORDB_insert_balance (
    366     TALER_ARL_adb,
    367     TALER_ARL_SET_AB (total_missed_deposit_confirmations),
    368     NULL);
    369   if (0 > qs)
    370   {
    371     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    372                 "Failed to update auditor DB, not recording progress\n");
    373     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
    374     return qs;
    375   }
    376   return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
    377 }
    378 
    379 
    380 /**
    381  * Function called on events received from Postgres.
    382  *
    383  * @param cls closure, NULL
    384  * @param extra additional event data provided
    385  * @param extra_size number of bytes in @a extra
    386  */
    387 static void
    388 db_notify (void *cls,
    389            const void *extra,
    390            size_t extra_size)
    391 {
    392   (void) cls;
    393   (void) extra;
    394   (void) extra_size;
    395   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    396               "Received notification for new deposit_confirmation\n");
    397   if (GNUNET_OK !=
    398       TALER_ARL_setup_sessions_and_run (&analyze_deposit_confirmations,
    399                                         NULL))
    400   {
    401     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    402                 "Audit failed\n");
    403     GNUNET_SCHEDULER_shutdown ();
    404     global_ret = EXIT_FAILURE;
    405     return;
    406   }
    407 }
    408 
    409 
    410 /**
    411  * Function called on shutdown.
    412  */
    413 static void
    414 do_shutdown (void *cls)
    415 {
    416   (void) cls;
    417   if (NULL != eh)
    418   {
    419     TALER_AUDITORDB_event_listen_cancel (eh);
    420     eh = NULL;
    421   }
    422   if (NULL != eh_dc)
    423   {
    424     TALER_AUDITORDB_event_listen_cancel (eh_dc);
    425     eh_dc = NULL;
    426   }
    427   TALER_ARL_done ();
    428 }
    429 
    430 
    431 /**
    432  * Main function that will be run.
    433  *
    434  * @param cls closure
    435  * @param args remaining command-line arguments
    436  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
    437  * @param c configuration
    438  */
    439 static void
    440 run (void *cls,
    441      char *const *args,
    442      const char *cfgfile,
    443      const struct GNUNET_CONFIGURATION_Handle *c)
    444 {
    445   (void) cls;
    446   (void) args;
    447   (void) cfgfile;
    448   cfg = c;
    449   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
    450                                  NULL);
    451   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    452               "Launching deposit auditor\n");
    453   if (EXIT_SUCCESS !=
    454       (global_ret = TALER_ARL_init (c)))
    455   {
    456     return;
    457   }
    458 
    459   if (test_mode != 1)
    460   {
    461     struct GNUNET_DB_EventHeaderP es = {
    462       .size = htons (sizeof (es)),
    463       .type = htons (TALER_DBEVENT_EXCHANGE_AUDITOR_WAKE_HELPER_DEPOSITS)
    464     };
    465     struct GNUNET_DB_EventHeaderP es_dc = {
    466       .size = htons (sizeof (es_dc)),
    467       .type = htons (TALER_DBEVENT_EXCHANGE_AUDITOR_NEW_DEPOSIT_CONFIRMATION)
    468     };
    469 
    470     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
    471                 "Running helper indefinitely\n");
    472     eh = TALER_AUDITORDB_event_listen (TALER_ARL_adb,
    473                                        &es,
    474                                        GNUNET_TIME_UNIT_FOREVER_REL,
    475                                        &db_notify,
    476                                        NULL);
    477     eh_dc = TALER_AUDITORDB_event_listen (TALER_ARL_adb,
    478                                           &es_dc,
    479                                           GNUNET_TIME_UNIT_FOREVER_REL,
    480                                           &db_notify,
    481                                           NULL);
    482   }
    483   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
    484               "Starting audit\n");
    485   if (GNUNET_OK !=
    486       TALER_ARL_setup_sessions_and_run (&analyze_deposit_confirmations,
    487                                         NULL))
    488   {
    489     GNUNET_SCHEDULER_shutdown ();
    490     global_ret = EXIT_FAILURE;
    491     return;
    492   }
    493 }
    494 
    495 
    496 /**
    497  * The main function of the deposit auditing helper tool.
    498  *
    499  * @param argc number of arguments from the command line
    500  * @param argv command line arguments
    501  * @return 0 ok, 1 on error
    502  */
    503 int
    504 main (int argc,
    505       char *const *argv)
    506 {
    507   const struct GNUNET_GETOPT_CommandLineOption options[] = {
    508     GNUNET_GETOPT_option_flag ('i',
    509                                "internal",
    510                                "perform checks only applicable for exchange-internal audits",
    511                                &internal_checks),
    512     GNUNET_GETOPT_option_flag ('t',
    513                                "test",
    514                                "run in test mode and exit when idle",
    515                                &test_mode),
    516     GNUNET_GETOPT_option_timetravel ('T',
    517                                      "timetravel"),
    518     GNUNET_GETOPT_OPTION_END
    519   };
    520   enum GNUNET_GenericReturnValue ret;
    521 
    522   ret = GNUNET_PROGRAM_run (
    523     TALER_AUDITOR_project_data (),
    524     argc,
    525     argv,
    526     "taler-helper-auditor-deposits",
    527     gettext_noop (
    528       "Audit Taler exchange database for deposit confirmation consistency"),
    529     options,
    530     &run,
    531     NULL);
    532   if (GNUNET_SYSERR == ret)
    533     return EXIT_NOTCONFIGURED;
    534   if (GNUNET_NO == ret)
    535     return EXIT_SUCCESS;
    536   return global_ret;
    537 }
    538 
    539 
    540 /* end of taler-helper-auditor-deposits.c */