exchange

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

iterate_aml_measures.c (6153B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2024, 2025 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  * @file exchangedb/iterate_aml_measures.c
     18  * @brief Implementation of the iterate_aml_measures function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/taler_pq_lib.h"
     22 #include "exchange-database/iterate_aml_measures.h"
     23 #include "helper.h"
     24 
     25 
     26 /**
     27  * Hard upper bound on the number of records returned by a single
     28  * call, regardless of the limit requested by the client.
     29  */
     30 #define MAX_RECORDS 50000
     31 
     32 
     33 /**
     34  * Closure for #handle_aml_result.
     35  */
     36 struct LegiMeasureResultContext
     37 {
     38   /**
     39    * Function to call on each result.
     40    */
     41   TALER_EXCHANGEDB_LegitimizationMeasureCallback cb;
     42 
     43   /**
     44    * Closure for @e cb.
     45    */
     46   void *cb_cls;
     47 
     48   /**
     49    * Plugin context.
     50    */
     51   struct TALER_EXCHANGEDB_PostgresContext *pg;
     52 
     53   /**
     54    * Set to #GNUNET_SYSERR on serious errors.
     55    */
     56   enum GNUNET_GenericReturnValue status;
     57 };
     58 
     59 
     60 /**
     61  * Function to be called with the results of a SELECT statement
     62  * that has returned @a num_results results.  Helper function
     63  * for #TALER_EXCHANGEDB_iterate_aml_measures().
     64  *
     65  * @param cls closure of type `struct LegiMeasureResultContext *`
     66  * @param result the postgres result
     67  * @param num_results the number of results in @a result
     68  */
     69 static void
     70 handle_aml_result (void *cls,
     71                    PGresult *result,
     72                    unsigned int num_results)
     73 {
     74   struct LegiMeasureResultContext *ctx = cls;
     75 
     76   for (unsigned int i = 0; i<num_results; i++)
     77   {
     78     struct TALER_NormalizedPaytoHashP h_payto;
     79     uint64_t rowid;
     80     struct GNUNET_TIME_Absolute start_time;
     81     json_t *jmeasures;
     82     bool is_finished;
     83     struct GNUNET_PQ_ResultSpec rs[] = {
     84       GNUNET_PQ_result_spec_uint64 ("legitimization_measure_serial_id",
     85                                     &rowid),
     86       GNUNET_PQ_result_spec_auto_from_type ("h_normalized_payto",
     87                                             &h_payto),
     88       GNUNET_PQ_result_spec_absolute_time ("start_time",
     89                                            &start_time),
     90       TALER_PQ_result_spec_json ("jmeasures",
     91                                  &jmeasures),
     92       GNUNET_PQ_result_spec_bool ("is_finished",
     93                                   &is_finished),
     94       GNUNET_PQ_result_spec_end
     95     };
     96 
     97     if (GNUNET_OK !=
     98         GNUNET_PQ_extract_result (result,
     99                                   rs,
    100                                   i))
    101     {
    102       GNUNET_break (0);
    103       ctx->status = GNUNET_SYSERR;
    104       return;
    105     }
    106     ctx->cb (ctx->cb_cls,
    107              &h_payto,
    108              start_time,
    109              jmeasures,
    110              is_finished,
    111              rowid);
    112     GNUNET_PQ_cleanup_result (rs);
    113   }
    114 }
    115 
    116 
    117 enum GNUNET_DB_QueryStatus
    118 TALER_EXCHANGEDB_iterate_aml_measures (
    119   struct TALER_EXCHANGEDB_PostgresContext *pg,
    120   const struct TALER_NormalizedPaytoHashP *h_payto,
    121   enum TALER_EXCHANGE_YesNoAll active_only,
    122   uint64_t offset,
    123   int64_t limit,
    124   TALER_EXCHANGEDB_LegitimizationMeasureCallback cb,
    125   void *cb_cls)
    126 {
    127   uint64_t ulimit = GNUNET_MIN ((uint64_t) MAX_RECORDS,
    128                                 TALER_EXCHANGEDB_abs_limit (limit));
    129   struct GNUNET_PQ_QueryParam params[] = {
    130     GNUNET_PQ_query_param_bool (NULL == h_payto),
    131     NULL == h_payto
    132       ? GNUNET_PQ_query_param_null ()
    133       : GNUNET_PQ_query_param_auto_from_type (h_payto),
    134     GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_ALL ==
    135                                  active_only)),
    136     GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_NO ==
    137                                  active_only)),
    138     GNUNET_PQ_query_param_uint64 (&offset),
    139     GNUNET_PQ_query_param_uint64 (&ulimit),
    140     GNUNET_PQ_query_param_end
    141   };
    142   struct LegiMeasureResultContext ctx = {
    143     .cb = cb,
    144     .cb_cls = cb_cls,
    145     .pg = pg,
    146     .status = GNUNET_OK
    147   };
    148   enum GNUNET_DB_QueryStatus qs;
    149   const char *stmt = (limit > 0)
    150     ? "iterate_aml_measures_inc"
    151     : "iterate_aml_measures_dec";
    152 
    153   PREPARE (pg,
    154            "iterate_aml_measures_inc",
    155            "SELECT"
    156            " lm.legitimization_measure_serial_id"
    157            ",kt.h_normalized_payto"
    158            ",lm.jmeasures::TEXT"
    159            ",lm.start_time"
    160            ",lm.is_finished"
    161            " FROM kyc_targets kt"
    162            " JOIN legitimization_measures lm"
    163            "   USING (access_token)"
    164            " WHERE (legitimization_measure_serial_id > $5)"
    165            "   AND ($1 OR (kt.h_normalized_payto = $2))"
    166            "   AND ($3 OR (lm.is_finished = $4))"
    167            " ORDER BY lm.legitimization_measure_serial_id ASC"
    168            " LIMIT $6");
    169   PREPARE (pg,
    170            "iterate_aml_measures_dec",
    171            "SELECT"
    172            " lm.legitimization_measure_serial_id"
    173            ",kt.h_normalized_payto"
    174            ",lm.jmeasures::TEXT"
    175            ",lm.start_time"
    176            ",lm.is_finished"
    177            " FROM kyc_targets kt"
    178            " JOIN legitimization_measures lm"
    179            "   USING (access_token)"
    180            " WHERE (legitimization_measure_serial_id < $5)"
    181            "   AND ($1 OR (kt.h_normalized_payto = $2))"
    182            "   AND ($3 OR (lm.is_finished = $4))"
    183            " ORDER BY lm.legitimization_measure_serial_id DESC"
    184            " LIMIT $6");
    185   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    186                                              stmt,
    187                                              params,
    188                                              &handle_aml_result,
    189                                              &ctx);
    190   if (GNUNET_OK != ctx.status)
    191     return GNUNET_DB_STATUS_HARD_ERROR;
    192   return qs;
    193 }