exchange

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

pg_select_exchange_credit_transfers.c (5872B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 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/pg_select_exchange_credit_transfers.c
     18  * @brief Implementation of the select_exchange_credit_transfers function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/platform.h"
     22 #include "taler/taler_error_codes.h"
     23 #include "taler/taler_dbevents.h"
     24 #include "taler/taler_pq_lib.h"
     25 #include "pg_select_exchange_credit_transfers.h"
     26 #include "pg_helper.h"
     27 
     28 /**
     29  * Closure for #handle_aml_result.
     30  */
     31 struct SelectTransferContext
     32 {
     33   /**
     34    * Function to call on each result.
     35    */
     36   TALER_EXCHANGEDB_AmlTransferCallback cb;
     37 
     38   /**
     39    * Closure for @e cb.
     40    */
     41   void *cb_cls;
     42 
     43   /**
     44    * Plugin context.
     45    */
     46   struct PostgresClosure *pg;
     47 
     48   /**
     49    * Set to #GNUNET_SYSERR on serious errors.
     50    */
     51   enum GNUNET_GenericReturnValue status;
     52 };
     53 
     54 
     55 /**
     56  * Function to be called with the results of a SELECT statement
     57  * that has returned @a num_results results.  Helper function
     58  * for #TEH_PG_select_exchange_debit_transfers().
     59  *
     60  * @param cls closure of type `struct SelectTransferContext *`
     61  * @param result the postgres result
     62  * @param num_results the number of results in @a result
     63  */
     64 static void
     65 handle_transfer_result (void *cls,
     66                         PGresult *result,
     67                         unsigned int num_results)
     68 {
     69   struct SelectTransferContext *stc = cls;
     70   struct PostgresClosure *pg = stc->pg;
     71 
     72   for (unsigned int i = 0; i<num_results; i++)
     73   {
     74     char *payto_uri;
     75     uint64_t rowid;
     76     struct GNUNET_TIME_Absolute execution_time;
     77     struct TALER_Amount amount;
     78     struct GNUNET_PQ_ResultSpec rs[] = {
     79       GNUNET_PQ_result_spec_uint64 ("serial_id",
     80                                     &rowid),
     81       GNUNET_PQ_result_spec_string ("payto_uri",
     82                                     &payto_uri),
     83       GNUNET_PQ_result_spec_absolute_time ("execution_time",
     84                                            &execution_time),
     85       TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
     86                                    &amount),
     87       GNUNET_PQ_result_spec_end
     88     };
     89 
     90     if (GNUNET_OK !=
     91         GNUNET_PQ_extract_result (result,
     92                                   rs,
     93                                   i))
     94     {
     95       GNUNET_break (0);
     96       stc->status = GNUNET_SYSERR;
     97       return;
     98     }
     99     stc->cb (stc->cb_cls,
    100              rowid,
    101              payto_uri,
    102              execution_time,
    103              &amount);
    104     GNUNET_PQ_cleanup_result (rs);
    105   }
    106 }
    107 
    108 
    109 enum GNUNET_DB_QueryStatus
    110 TEH_PG_select_exchange_credit_transfers (
    111   void *cls,
    112   const struct TALER_Amount *threshold,
    113   uint64_t offset,
    114   int64_t limit,
    115   const struct TALER_NormalizedPaytoHashP *h_payto,
    116   TALER_EXCHANGEDB_AmlTransferCallback cb,
    117   void *cb_cls)
    118 {
    119   struct PostgresClosure *pg = cls;
    120   struct SelectTransferContext stc = {
    121     .pg = pg,
    122     .cb = cb,
    123     .cb_cls = cb_cls,
    124     .status = GNUNET_OK
    125   };
    126   uint64_t ulimit = (limit > 0) ? limit : -limit;
    127   struct GNUNET_PQ_QueryParam params[] = {
    128     GNUNET_PQ_query_param_uint64 (&offset),
    129     GNUNET_PQ_query_param_uint64 (&ulimit),
    130     TALER_PQ_query_param_amount (pg->conn,
    131                                  threshold),
    132     NULL != h_payto
    133     ? GNUNET_PQ_query_param_auto_from_type (h_payto)
    134     : GNUNET_PQ_query_param_null (),
    135     GNUNET_PQ_query_param_end
    136   };
    137   enum GNUNET_DB_QueryStatus qs;
    138 
    139   PREPARE (pg,
    140            "select_exchange_credit_transfers_inc",
    141            "SELECT"
    142            " ri.reserve_in_serial_id AS serial_id"
    143            ",wt.payto_uri"
    144            ",ri.execution_date AS execution_time"
    145            ",ri.credit AS amount"
    146            " FROM reserves_in ri"
    147            " LEFT JOIN wire_targets wt"
    148            "   ON (ri.wire_source_h_payto = wt.wire_target_h_payto)"
    149            " WHERE (ri.reserve_in_serial_id > $1)"
    150            "   AND ( ($4::BYTEA IS NULL) OR (wt.h_normalized_payto=$4) )"
    151            "   AND ( ( (ri.credit).val > ($3::taler_amount).val)"
    152            "      OR ( ( (ri.credit).val >= ($3::taler_amount).val)"
    153            "       AND ( (ri.credit).frac >= ($3::taler_amount).frac) ) )"
    154            " ORDER BY ri.reserve_in_serial_id ASC"
    155            " LIMIT $2");
    156   PREPARE (pg,
    157            "select_exchange_credit_transfers_dec",
    158            "SELECT"
    159            " ri.reserve_in_serial_id AS serial_id"
    160            ",wt.payto_uri"
    161            ",ri.execution_date AS execution_time"
    162            ",ri.credit AS amount"
    163            " FROM reserves_in ri"
    164            " LEFT JOIN wire_targets wt"
    165            "   ON (ri.wire_source_h_payto = wt.wire_target_h_payto)"
    166            " WHERE (ri.reserve_in_serial_id < $1)"
    167            "   AND ( ($4::BYTEA IS NULL) OR (wt.h_normalized_payto=$4) )"
    168            "   AND ( ( (ri.credit).val > ($3::taler_amount).val)"
    169            "      OR ( ( (ri.credit).val >= ($3::taler_amount).val)"
    170            "       AND ( (ri.credit).frac >= ($3::taler_amount).frac) ) )"
    171            " ORDER BY ri.reserve_in_serial_id DESC"
    172            " LIMIT $2");
    173   qs = GNUNET_PQ_eval_prepared_multi_select (
    174     pg->conn,
    175     (limit > 0)
    176     ? "select_exchange_credit_transfers_inc"
    177     : "select_exchange_credit_transfers_dec",
    178     params,
    179     &handle_transfer_result,
    180     &stc);
    181   if (GNUNET_OK != stc.status)
    182     return GNUNET_DB_STATUS_HARD_ERROR;
    183   return qs;
    184 }