exchange

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

iterate_coin_inconsistencies.c (5658B)


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