exchange

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

iterate_denominations_without_sigs.c (5490B)


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