LCOV - code coverage report
Current view: top level - exchangedb - iterate_aml_attributes.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 87.1 % 31 27
Test Date: 2026-09-11 18:55:36 Functions: 100.0 % 2 2

            Line data    Source code
       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              :  * @file exchangedb/iterate_aml_attributes.c
      18              :  * @brief Implementation of the iterate_aml_attributes function for Postgres
      19              :  * @author Christian Grothoff
      20              :  */
      21              : #include "taler/taler_pq_lib.h"
      22              : #include "exchange-database/iterate_aml_attributes.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 AmlAttributeResultContext
      37              : {
      38              :   /**
      39              :    * Function to call on each result.
      40              :    */
      41              :   TALER_EXCHANGEDB_AmlAttributeCallback 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_attributes().
      64              :  *
      65              :  * @param cls closure of type `struct AmlAttributeResultContext *`
      66              :  * @param result the postgres result
      67              :  * @param num_results the number of results in @a result
      68              :  */
      69              : static void
      70           16 : handle_aml_attributes (void *cls,
      71              :                        PGresult *result,
      72              :                        unsigned int num_results)
      73              : {
      74           16 :   struct AmlAttributeResultContext *ctx = cls;
      75              : 
      76           31 :   for (unsigned int i = 0; i<num_results; i++)
      77              :   {
      78              :     uint64_t rowid;
      79              :     struct GNUNET_TIME_Timestamp collection_time;
      80           15 :     char *officer_name = NULL;
      81              :     bool by_aml_officer;
      82              :     size_t enc_attributes_size;
      83              :     void *enc_attributes;
      84           15 :     struct GNUNET_PQ_ResultSpec rs[] = {
      85           15 :       GNUNET_PQ_result_spec_uint64 ("kyc_attributes_serial_id",
      86              :                                     &rowid),
      87           15 :       GNUNET_PQ_result_spec_timestamp ("collection_time",
      88              :                                        &collection_time),
      89           15 :       GNUNET_PQ_result_spec_bool ("by_aml_officer",
      90              :                                   &by_aml_officer),
      91           15 :       GNUNET_PQ_result_spec_allow_null (
      92              :         GNUNET_PQ_result_spec_string ("decider_name",
      93              :                                       &officer_name),
      94              :         NULL),
      95           15 :       GNUNET_PQ_result_spec_variable_size ("encrypted_attributes",
      96              :                                            &enc_attributes,
      97              :                                            &enc_attributes_size),
      98              :       GNUNET_PQ_result_spec_end
      99              :     };
     100              : 
     101           15 :     if (GNUNET_OK !=
     102           15 :         GNUNET_PQ_extract_result (result,
     103              :                                   rs,
     104              :                                   i))
     105              :     {
     106            0 :       GNUNET_break (0);
     107            0 :       ctx->status = GNUNET_SYSERR;
     108            0 :       return;
     109              :     }
     110              : 
     111           15 :     ctx->cb (ctx->cb_cls,
     112              :              rowid,
     113              :              collection_time,
     114              :              by_aml_officer,
     115              :              officer_name,
     116              :              enc_attributes_size,
     117              :              enc_attributes);
     118           15 :     GNUNET_PQ_cleanup_result (rs);
     119              :   }
     120              : }
     121              : 
     122              : 
     123              : enum GNUNET_DB_QueryStatus
     124           16 : TALER_EXCHANGEDB_iterate_aml_attributes (
     125              :   struct TALER_EXCHANGEDB_PostgresContext *pg,
     126              :   const struct TALER_NormalizedPaytoHashP *h_payto,
     127              :   uint64_t offset,
     128              :   int64_t limit,
     129              :   TALER_EXCHANGEDB_AmlAttributeCallback cb,
     130              :   void *cb_cls)
     131              : {
     132           16 :   uint64_t ulimit = GNUNET_MIN ((uint64_t) MAX_RECORDS,
     133              :                                 TALER_EXCHANGEDB_abs_limit (limit));
     134           16 :   struct GNUNET_PQ_QueryParam params[] = {
     135           16 :     GNUNET_PQ_query_param_auto_from_type (h_payto),
     136           16 :     GNUNET_PQ_query_param_uint64 (&offset),
     137           16 :     GNUNET_PQ_query_param_uint64 (&ulimit),
     138              :     GNUNET_PQ_query_param_end
     139              :   };
     140           16 :   struct AmlAttributeResultContext ctx = {
     141              :     .cb = cb,
     142              :     .cb_cls = cb_cls,
     143              :     .pg = pg,
     144              :     .status = GNUNET_OK
     145              :   };
     146              :   enum GNUNET_DB_QueryStatus qs;
     147           16 :   const char *stmt = (limit > 0)
     148              :     ? "iterate_aml_attributes_inc"
     149              :     : "iterate_aml_attributes_dec";
     150              : 
     151              :   /* aml_staff is append-only, so it holds one row per status change of an
     152              :      officer.  Joining it plainly would return the attributes once per
     153              :      status the officer ever had; the LATERAL picks the officer's latest
     154              :      name instead. */
     155           16 :   PREPARE (pg,
     156              :            "iterate_aml_attributes_inc",
     157              :            "SELECT"
     158              :            " ka.kyc_attributes_serial_id"
     159              :            ",ka.collection_time"
     160              :            ",ka.by_aml_officer"
     161              :            ",astaff.decider_name"
     162              :            ",ka.encrypted_attributes"
     163              :            " FROM kyc_attributes ka"
     164              :            " LEFT JOIN legitimization_processes lp"
     165              :            "    ON (ka.by_aml_officer AND"
     166              :            "        (ka.legitimization_serial = lp.legitimization_process_serial_id))"
     167              :            " LEFT JOIN LATERAL"
     168              :            "   (SELECT s.decider_name"
     169              :            "      FROM aml_staff s"
     170              :            "     WHERE ka.by_aml_officer"
     171              :            "       AND (DECODE(lp.provider_user_id, 'base64') = s.decider_pub)"
     172              :            "     ORDER BY s.last_change DESC"
     173              :            "     LIMIT 1) astaff ON TRUE"
     174              :            " WHERE ka.h_payto=$1"
     175              :            "   AND ka.kyc_attributes_serial_id > $2"
     176              :            " ORDER BY ka.kyc_attributes_serial_id ASC"
     177              :            " LIMIT $3");
     178           16 :   PREPARE (pg,
     179              :            "iterate_aml_attributes_dec",
     180              :            "SELECT"
     181              :            " ka.kyc_attributes_serial_id"
     182              :            ",ka.collection_time"
     183              :            ",ka.by_aml_officer"
     184              :            ",astaff.decider_name"
     185              :            ",ka.encrypted_attributes"
     186              :            " FROM kyc_attributes ka"
     187              :            " LEFT JOIN legitimization_processes lp"
     188              :            "    ON (ka.by_aml_officer AND"
     189              :            "        (ka.legitimization_serial = lp.legitimization_process_serial_id))"
     190              :            " LEFT JOIN LATERAL"
     191              :            "   (SELECT s.decider_name"
     192              :            "      FROM aml_staff s"
     193              :            "     WHERE ka.by_aml_officer"
     194              :            "       AND (DECODE(lp.provider_user_id, 'base64') = s.decider_pub)"
     195              :            "     ORDER BY s.last_change DESC"
     196              :            "     LIMIT 1) astaff ON TRUE"
     197              :            " WHERE ka.h_payto=$1"
     198              :            "   AND ka.kyc_attributes_serial_id < $2"
     199              :            " ORDER BY ka.kyc_attributes_serial_id DESC"
     200              :            " LIMIT $3");
     201           16 :   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
     202              :                                              stmt,
     203              :                                              params,
     204              :                                              &handle_aml_attributes,
     205              :                                              &ctx);
     206           16 :   if (GNUNET_OK != ctx.status)
     207            0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     208           16 :   return qs;
     209              : }
        

Generated by: LCOV version 2.0-1