exchange

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

iterate_misattribution_in_inconsistencies.c (5476B)


      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_pq_lib.h"
     19 #include "pg_helper.h"
     20 
     21 #include "auditor-database/iterate_misattribution_in_inconsistencies.h"
     22 
     23 
     24 struct MisattributionInInconsistencyContext
     25 {
     26 
     27   /**
     28    * Function to call for each bad sig loss.
     29    */
     30   TALER_AUDITORDB_MisattributionInInconsistencyCallback cb;
     31 
     32   /**
     33    * Closure for @e cb
     34    */
     35   void *cb_cls;
     36 
     37   /**
     38    * Plugin context.
     39    */
     40   struct TALER_AUDITORDB_PostgresContext *pg;
     41 
     42   /**
     43    * Query status to return.
     44    */
     45   enum GNUNET_DB_QueryStatus qs;
     46 };
     47 
     48 
     49 /**
     50  * Helper function for #TALER_AUDITORDB_iterate_misattribution_in_inconsistencies().
     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 MisattributionInInconsistencyContext *`
     55  * @param result the postgres result
     56  * @param num_results the number of results in @a result
     57  */
     58 static void
     59 misattribution_in_inconsistency_cb (void *cls,
     60                                     PGresult *result,
     61                                     unsigned int num_results)
     62 {
     63   struct MisattributionInInconsistencyContext *dcc = cls;
     64   struct TALER_AUDITORDB_PostgresContext *pg = dcc->pg;
     65 
     66   for (unsigned int i = 0; i < num_results; i++)
     67   {
     68     struct TALER_AUDITORDB_MisattributionInInconsistency dc;
     69     struct GNUNET_PQ_ResultSpec rs[] = {
     70       GNUNET_PQ_result_spec_uint64 ("row_id",
     71                                     &dc.row_id),
     72       TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
     73                                    &dc.amount),
     74       GNUNET_PQ_result_spec_uint64 ("bank_row",
     75                                     &dc.bank_row),
     76       GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
     77                                             &dc.reserve_pub),
     78       GNUNET_PQ_result_spec_bool ("suppressed",
     79                                   &dc.suppressed),
     80       GNUNET_PQ_result_spec_end
     81     };
     82     enum GNUNET_GenericReturnValue rval;
     83 
     84     if (GNUNET_OK !=
     85         GNUNET_PQ_extract_result (result,
     86                                   rs,
     87                                   i))
     88     {
     89       GNUNET_break (0);
     90       dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
     91       return;
     92     }
     93     dcc->qs = i + 1;
     94     rval = dcc->cb (dcc->cb_cls,
     95                     &dc);
     96     GNUNET_PQ_cleanup_result (rs);
     97     if (GNUNET_OK != rval)
     98       break;
     99   }
    100 }
    101 
    102 
    103 enum GNUNET_DB_QueryStatus
    104 TALER_AUDITORDB_iterate_misattribution_in_inconsistencies (struct
    105                                                            TALER_AUDITORDB_PostgresContext
    106                                                            *pg,
    107                                                            int64_t limit,
    108                                                            uint64_t offset,
    109                                                            bool
    110                                                            return_suppressed,
    111                                                            TALER_AUDITORDB_MisattributionInInconsistencyCallback
    112                                                            cb,
    113                                                            void *cb_cls)
    114 {
    115   uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit);
    116   struct GNUNET_PQ_QueryParam params[] = {
    117     GNUNET_PQ_query_param_uint64 (&offset),
    118     GNUNET_PQ_query_param_bool (return_suppressed),
    119     GNUNET_PQ_query_param_uint64 (&plimit),
    120     GNUNET_PQ_query_param_end
    121   };
    122   struct MisattributionInInconsistencyContext dcc = {
    123     .cb = cb,
    124     .cb_cls = cb_cls,
    125     .pg = pg
    126   };
    127   enum GNUNET_DB_QueryStatus qs;
    128 
    129   PREPARE (pg,
    130            "iterate_misattribution_in_inconsistencies_inconsistency_get_desc",
    131            "SELECT"
    132            " row_id"
    133            ",amount"
    134            ",bank_row"
    135            ",reserve_pub"
    136            ",suppressed"
    137            " FROM auditor_misattribution_in_inconsistency"
    138            " WHERE (row_id < $1)"
    139            " AND ($2 OR NOT suppressed)"
    140            " ORDER BY row_id DESC"
    141            " LIMIT $3"
    142            );
    143   PREPARE (pg,
    144            "iterate_misattribution_in_inconsistencies_inconsistency_get_asc",
    145            "SELECT"
    146            " row_id"
    147            ",amount"
    148            ",bank_row"
    149            ",reserve_pub"
    150            ",suppressed"
    151            " FROM auditor_misattribution_in_inconsistency"
    152            " WHERE (row_id > $1)"
    153            " AND ($2 OR NOT suppressed)"
    154            " ORDER BY row_id ASC"
    155            " LIMIT $3"
    156            );
    157   qs = GNUNET_PQ_eval_prepared_multi_select (
    158     pg->conn,
    159     (limit > 0)
    160     ? "iterate_misattribution_in_inconsistencies_inconsistency_get_asc"
    161     : "iterate_misattribution_in_inconsistencies_inconsistency_get_desc",
    162     params,
    163     &misattribution_in_inconsistency_cb,
    164     &dcc);
    165   if (qs > 0)
    166     return dcc.qs;
    167   GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
    168   return qs;
    169 }