exchange

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

iterate_denomination_key_validity_withdraw_inconsistencies.c (5600B)


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