exchange

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

iterate_reserve_in_inconsistencies.c (6288B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 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 #include "taler/taler_error_codes.h"
     17 #include "taler/taler_dbevents.h"
     18 #include "taler/taler_pq_lib.h"
     19 #include "pg_helper.h"
     20 #include "auditor-database/iterate_reserve_in_inconsistencies.h"
     21 
     22 
     23 /**
     24  * Hard upper bound on the number of records returned by a single
     25  * call, regardless of the limit requested by the client.
     26  */
     27 #define MAX_RECORDS 50000
     28 
     29 
     30 struct ReserveInInconsistencyContext
     31 {
     32 
     33   /**
     34    * Function to call for each bad sig loss.
     35    */
     36   TALER_AUDITORDB_ReserveInInconsistencyCallback cb;
     37 
     38   /**
     39    * Closure for @e cb
     40    */
     41   void *cb_cls;
     42 
     43   /**
     44    * Plugin context.
     45    */
     46   struct TALER_AUDITORDB_PostgresContext *pg;
     47 
     48   /**
     49    * Query status to return.
     50    */
     51   enum GNUNET_DB_QueryStatus qs;
     52 };
     53 
     54 
     55 /**
     56  * Helper function for #TALER_AUDITORDB_iterate_reserve_in_inconsistencies().
     57  * To be called with the results of a SELECT statement
     58  * that has returned @a num_results results.
     59  *
     60  * @param cls closure of type `struct ReserveInInconsistencyContext *`
     61  * @param result the postgres result
     62  * @param num_results the number of results in @a result
     63  */
     64 static void
     65 reserve_in_inconsistency_cb (void *cls,
     66                              PGresult *result,
     67                              unsigned int num_results)
     68 {
     69   struct ReserveInInconsistencyContext *dcc = cls;
     70   struct TALER_AUDITORDB_PostgresContext *pg = dcc->pg;
     71 
     72   for (unsigned int i = 0; i < num_results; i++)
     73   {
     74     struct TALER_AUDITORDB_ReserveInInconsistency dc;
     75     struct GNUNET_PQ_ResultSpec rs[] = {
     76       GNUNET_PQ_result_spec_uint64 ("row_id",
     77                                     &dc.serial_id),
     78       GNUNET_PQ_result_spec_uint64 ("bank_row_id",
     79                                     &dc.bank_row_id),
     80       TALER_PQ_RESULT_SPEC_AMOUNT ("amount_exchange_expected",
     81                                    &dc.amount_exchange_expected),
     82       TALER_PQ_RESULT_SPEC_AMOUNT ("amount_wired",
     83                                    &dc.amount_wired),
     84       GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
     85                                             &dc.reserve_pub),
     86       GNUNET_PQ_result_spec_absolute_time ("timestamp",
     87                                            &dc.timestamp),
     88       GNUNET_PQ_result_spec_string ("account",
     89                                     &dc.account.full_payto),
     90       GNUNET_PQ_result_spec_string ("diagnostic",
     91                                     &dc.diagnostic),
     92       GNUNET_PQ_result_spec_bool ("suppressed",
     93                                   &dc.suppressed),
     94       GNUNET_PQ_result_spec_end
     95     };
     96     enum GNUNET_GenericReturnValue rval;
     97 
     98     if (GNUNET_OK !=
     99         GNUNET_PQ_extract_result (result,
    100                                   rs,
    101                                   i))
    102     {
    103       GNUNET_break (0);
    104       dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
    105       return;
    106     }
    107 
    108     dcc->qs = i + 1;
    109     rval = dcc->cb (dcc->cb_cls,
    110                     &dc);
    111     GNUNET_PQ_cleanup_result (rs);
    112     if (GNUNET_OK != rval)
    113       break;
    114   }
    115 }
    116 
    117 
    118 enum GNUNET_DB_QueryStatus
    119 TALER_AUDITORDB_iterate_reserve_in_inconsistencies (struct
    120                                                     TALER_AUDITORDB_PostgresContext
    121                                                     *
    122                                                     pg,
    123                                                     int64_t limit,
    124                                                     uint64_t offset,
    125                                                     bool return_suppressed,
    126                                                     TALER_AUDITORDB_ReserveInInconsistencyCallback
    127                                                     cb,
    128                                                     void *cb_cls)
    129 {
    130   uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS,
    131                                 TALER_AUDITORDB_abs_limit (limit));
    132   struct GNUNET_PQ_QueryParam params[] = {
    133     GNUNET_PQ_query_param_uint64 (&offset),
    134     GNUNET_PQ_query_param_bool (return_suppressed),
    135     GNUNET_PQ_query_param_uint64 (&plimit),
    136     GNUNET_PQ_query_param_end
    137   };
    138   struct ReserveInInconsistencyContext dcc = {
    139     .cb = cb,
    140     .cb_cls = cb_cls,
    141     .pg = pg
    142   };
    143   enum GNUNET_DB_QueryStatus qs;
    144 
    145   PREPARE (pg,
    146            "iterate_reserve_in_inconsistencies_inconsistency_get_desc",
    147            "SELECT"
    148            " row_id"
    149            ",bank_row_id"
    150            ",amount_exchange_expected"
    151            ",amount_wired"
    152            ",reserve_pub"
    153            ",timestamp"
    154            ",account"
    155            ",diagnostic"
    156            ",suppressed"
    157            " FROM auditor_reserve_in_inconsistency"
    158            " WHERE (row_id < $1)"
    159            " AND ($2 OR suppressed is false)"
    160            " ORDER BY row_id DESC"
    161            " LIMIT $3"
    162            );
    163   PREPARE (pg,
    164            "iterate_reserve_in_inconsistencies_inconsistency_get_asc",
    165            "SELECT"
    166            " row_id"
    167            ",bank_row_id"
    168            ",amount_exchange_expected"
    169            ",amount_wired"
    170            ",reserve_pub"
    171            ",timestamp"
    172            ",account"
    173            ",diagnostic"
    174            ",suppressed"
    175            " FROM auditor_reserve_in_inconsistency"
    176            " WHERE (row_id > $1)"
    177            " AND ($2 OR suppressed is false)"
    178            " ORDER BY row_id ASC"
    179            " LIMIT $3"
    180            );
    181   qs = GNUNET_PQ_eval_prepared_multi_select (
    182     pg->conn,
    183     (limit > 0)
    184     ? "iterate_reserve_in_inconsistencies_inconsistency_get_asc"
    185     : "iterate_reserve_in_inconsistencies_inconsistency_get_desc",
    186     params,
    187     &reserve_in_inconsistency_cb,
    188     &dcc);
    189   if (qs > 0)
    190     return dcc.qs;
    191   GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
    192   return qs;
    193 }