exchange

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

pg_lookup_aml_history.c (5780B)


      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/pg_lookup_aml_history.c
     18  * @brief Implementation of the lookup_aml_history function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/taler_pq_lib.h"
     22 #include "taler/exchange-database/lookup_aml_history.h"
     23 #include "helper.h"
     24 
     25 
     26 /**
     27  * Closure for callbacks called from #TALER_EXCHANGEDB_lookup_aml_history()
     28  */
     29 struct AmlHistoryContext
     30 {
     31 
     32   /**
     33    * Function to call on each result.
     34    */
     35   TALER_EXCHANGEDB_AmlHistoryCallback cb;
     36 
     37   /**
     38    * Closure for @e cb.
     39    */
     40   void *cb_cls;
     41 
     42   /**
     43    * Plugin context.
     44    */
     45   struct TALER_EXCHANGEDB_PostgresContext *pg;
     46 
     47   /**
     48    * Set to 'true' if the transaction failed.
     49    */
     50   bool failed;
     51 
     52 };
     53 
     54 
     55 /**
     56  * Function 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 AmlHistoryContext`
     60  * @param result the postgres result
     61  * @param num_results the number of results in @a result
     62  */
     63 static void
     64 handle_aml_entry (void *cls,
     65                   PGresult *result,
     66                   unsigned int num_results)
     67 {
     68   struct AmlHistoryContext *ahc = cls;
     69 
     70   for (unsigned int i = 0; i < num_results; i++)
     71   {
     72     uint64_t outcome_serial_id;
     73     struct GNUNET_TIME_Timestamp decision_time;
     74     char *justification;
     75     struct TALER_AmlOfficerPublicKeyP decider_pub;
     76     json_t *jproperties;
     77     json_t *jnew_rules = NULL;
     78     bool to_investigate;
     79     bool is_active;
     80     struct GNUNET_PQ_ResultSpec rs[] = {
     81       GNUNET_PQ_result_spec_uint64 ("outcome_serial_id",
     82                                     &outcome_serial_id),
     83       GNUNET_PQ_result_spec_timestamp ("decision_time",
     84                                        &decision_time),
     85       GNUNET_PQ_result_spec_string ("justification",
     86                                     &justification),
     87       GNUNET_PQ_result_spec_auto_from_type ("decider_pub",
     88                                             &decider_pub),
     89       GNUNET_PQ_result_spec_allow_null (
     90         TALER_PQ_result_spec_json ("jproperties",
     91                                    &jproperties),
     92         NULL),
     93       TALER_PQ_result_spec_json ("jnew_rules",
     94                                  &jnew_rules),
     95       GNUNET_PQ_result_spec_bool ("to_investigate",
     96                                   &to_investigate),
     97       GNUNET_PQ_result_spec_bool ("is_active",
     98                                   &is_active),
     99       GNUNET_PQ_result_spec_end
    100     };
    101 
    102     if (GNUNET_OK !=
    103         GNUNET_PQ_extract_result (result,
    104                                   rs,
    105                                   i))
    106     {
    107       GNUNET_break (0);
    108       ahc->failed = true;
    109       return;
    110     }
    111     ahc->cb (ahc->cb_cls,
    112              outcome_serial_id,
    113              decision_time,
    114              justification,
    115              &decider_pub,
    116              jproperties,
    117              jnew_rules,
    118              to_investigate,
    119              is_active);
    120     GNUNET_PQ_cleanup_result (rs);
    121   }
    122 }
    123 
    124 
    125 enum GNUNET_DB_QueryStatus
    126 TALER_EXCHANGEDB_lookup_aml_history (
    127   struct TALER_EXCHANGEDB_PostgresContext *pg,
    128   const struct TALER_NormalizedPaytoHashP *h_payto,
    129   uint64_t offset,
    130   int64_t limit,
    131   TALER_EXCHANGEDB_AmlHistoryCallback cb,
    132   void *cb_cls)
    133 {
    134   struct AmlHistoryContext ahc = {
    135     .pg = pg,
    136     .cb = cb,
    137     .cb_cls = cb_cls
    138   };
    139   uint64_t ulimit = (limit < 0) ? (-limit) : limit;
    140   struct GNUNET_PQ_QueryParam params[] = {
    141     GNUNET_PQ_query_param_auto_from_type (h_payto),
    142     GNUNET_PQ_query_param_uint64 (&offset),
    143     GNUNET_PQ_query_param_uint64 (&ulimit),
    144     GNUNET_PQ_query_param_end
    145   };
    146   enum GNUNET_DB_QueryStatus qs;
    147 
    148   PREPARE (pg,
    149            "lookup_aml_history_desc",
    150            "SELECT"
    151            " lo.decision_time"
    152            ",lo.outcome_serial_id"
    153            ",ah.justification"
    154            ",ah.decider_pub"
    155            ",lo.jproperties::TEXT"
    156            ",lo.jnew_rules::TEXT"
    157            ",lo.to_investigate"
    158            ",lo.is_active"
    159            " FROM aml_history ah"
    160            " JOIN legitimization_outcomes lo"
    161            "   USING (outcome_serial_id)"
    162            " WHERE ah.h_payto=$1"
    163            "   AND lo.outcome_serial_id < $2"
    164            " ORDER BY outcome_serial_id DESC"
    165            " LIMIT $3;");
    166   PREPARE (pg,
    167            "lookup_aml_history_asc",
    168            "SELECT"
    169            " lo.decision_time"
    170            ",lo.outcome_serial_id"
    171            ",ah.justification"
    172            ",ah.decider_pub"
    173            ",lo.jproperties::TEXT"
    174            ",lo.jnew_rules::TEXT"
    175            ",lo.to_investigate"
    176            ",lo.is_active"
    177            " FROM aml_history ah"
    178            " JOIN legitimization_outcomes lo"
    179            "   USING (outcome_serial_id)"
    180            " WHERE ah.h_payto=$1"
    181            "   AND lo.outcome_serial_id > $2"
    182            " ORDER BY outcome_serial_id ASC"
    183            " LIMIT $3;");
    184   qs = GNUNET_PQ_eval_prepared_multi_select (
    185     pg->conn,
    186     (limit < 0)
    187     ? "lookup_aml_history_desc"
    188     : "lookup_aml_history_asc",
    189     params,
    190     &handle_aml_entry,
    191     &ahc);
    192   if (qs <= 0)
    193     return qs;
    194   if (ahc.failed)
    195   {
    196     GNUNET_break (0);
    197     return GNUNET_DB_STATUS_HARD_ERROR;
    198   }
    199   return qs;
    200 }