exchange

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

pg_get_wire_out_inconsistency.c (5099B)


      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_wire_out_inconsistency.h"
     22 
     23 
     24 struct WireOutInconsistencyContext
     25 {
     26 
     27   /**
     28    * Function to call for each bad sig loss.
     29    */
     30   TALER_AUDITORDB_WireOutInconsistencyCallback 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_wire_out_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 WireOutInconsistencyContext *`
     55  * @param result the postgres result
     56  * @param num_results the number of results in @a result
     57  */
     58 static void
     59 wire_out_inconsistency_cb (void *cls,
     60                            PGresult *result,
     61                            unsigned int num_results)
     62 {
     63   struct WireOutInconsistencyContext *dcc = cls;
     64   struct PostgresClosure *pg = dcc->pg;
     65 
     66   for (unsigned int i = 0; i < num_results; i++)
     67   {
     68     struct TALER_AUDITORDB_WireOutInconsistency dc;
     69     struct GNUNET_PQ_ResultSpec rs[] = {
     70       GNUNET_PQ_result_spec_uint64 ("row_id",
     71                                     &dc.row_id),
     72       GNUNET_PQ_result_spec_string ("destination_account",
     73                                     &dc.destination_account.full_payto),
     74       GNUNET_PQ_result_spec_string ("diagnostic",
     75                                     &dc.diagnostic),
     76       GNUNET_PQ_result_spec_uint64 ("wire_out_serial_id",
     77                                     &dc.wire_out_row_id),
     78       TALER_PQ_RESULT_SPEC_AMOUNT ("expected",
     79                                    &dc.expected),
     80       TALER_PQ_RESULT_SPEC_AMOUNT ("claimed",
     81                                    &dc.claimed),
     82       GNUNET_PQ_result_spec_bool ("suppressed",
     83                                   &dc.suppressed),
     84       GNUNET_PQ_result_spec_end
     85     };
     86     enum GNUNET_GenericReturnValue rval;
     87 
     88     if (GNUNET_OK !=
     89         GNUNET_PQ_extract_result (result,
     90                                   rs,
     91                                   i))
     92     {
     93       GNUNET_break (0);
     94       dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
     95       return;
     96     }
     97     dcc->qs = i + 1;
     98     rval = dcc->cb (dcc->cb_cls,
     99                     &dc);
    100     GNUNET_PQ_cleanup_result (rs);
    101     if (GNUNET_OK != rval)
    102       break;
    103   }
    104 }
    105 
    106 
    107 enum GNUNET_DB_QueryStatus
    108 TAH_PG_get_wire_out_inconsistency (
    109   void *cls,
    110   int64_t limit,
    111   uint64_t offset,
    112   bool return_suppressed,
    113   TALER_AUDITORDB_WireOutInconsistencyCallback cb,
    114   void *cb_cls)
    115 {
    116   struct PostgresClosure *pg = cls;
    117   uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit);
    118   struct GNUNET_PQ_QueryParam params[] = {
    119     GNUNET_PQ_query_param_uint64 (&offset),
    120     GNUNET_PQ_query_param_bool (return_suppressed),
    121     GNUNET_PQ_query_param_uint64 (&plimit),
    122     GNUNET_PQ_query_param_end
    123   };
    124   struct WireOutInconsistencyContext dcc = {
    125     .cb = cb,
    126     .cb_cls = cb_cls,
    127     .pg = pg
    128   };
    129   enum GNUNET_DB_QueryStatus qs;
    130 
    131   PREPARE (pg,
    132            "auditor_wire_out_inconsistency_get_desc",
    133            "SELECT"
    134            " row_id"
    135            ",destination_account"
    136            ",diagnostic"
    137            ",wire_out_serial_id"
    138            ",expected"
    139            ",claimed"
    140            ",suppressed"
    141            " FROM auditor_wire_out_inconsistency"
    142            " WHERE (row_id < $1)"
    143            "   AND ($2 OR NOT suppressed)"
    144            " ORDER BY row_id DESC"
    145            " LIMIT $3"
    146            );
    147   PREPARE (pg,
    148            "auditor_wire_out_inconsistency_get_asc",
    149            "SELECT"
    150            " row_id"
    151            ",destination_account"
    152            ",diagnostic"
    153            ",wire_out_serial_id"
    154            ",expected"
    155            ",claimed"
    156            ",suppressed"
    157            " FROM auditor_wire_out_inconsistency"
    158            " WHERE (row_id > $1)"
    159            "   AND ($2 OR NOT suppressed)"
    160            " ORDER BY row_id ASC"
    161            " LIMIT $3"
    162            );
    163   qs = GNUNET_PQ_eval_prepared_multi_select (
    164     pg->conn,
    165     (limit > 0)
    166     ? "auditor_wire_out_inconsistency_get_asc"
    167     : "auditor_wire_out_inconsistency_get_desc",
    168     params,
    169     &wire_out_inconsistency_cb,
    170     &dcc);
    171   if (qs > 0)
    172     return dcc.qs;
    173   GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
    174   return qs;
    175 }