exchange

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

select_wallet_merges.c (6456B)


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