exchange

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

iterate_reserve_not_closed_inconsistencies.c (6258B)


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