exchange

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

pg_get_reserve_in_inconsistency.c (5496B)


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