LCOV - code coverage report
Current view: top level - exchangedb - iterate_exchange_credit_transfers.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) 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_exchange_credit_transfers.c
      18              :  * @brief Implementation of the iterate_exchange_credit_transfers function for Postgres
      19              :  * @author Christian Grothoff
      20              :  */
      21              : #include "taler/taler_pq_lib.h"
      22              : #include "exchange-database/iterate_exchange_credit_transfers.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 SelectTransferContext
      37              : {
      38              :   /**
      39              :    * Function to call on each result.
      40              :    */
      41              :   TALER_EXCHANGEDB_AmlTransferCallback 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_exchange_debit_transfers().
      64              :  *
      65              :  * @param cls closure of type `struct SelectTransferContext *`
      66              :  * @param result the postgres result
      67              :  * @param num_results the number of results in @a result
      68              :  */
      69              : static void
      70            7 : handle_transfer_result (void *cls,
      71              :                         PGresult *result,
      72              :                         unsigned int num_results)
      73              : {
      74            7 :   struct SelectTransferContext *stc = cls;
      75            7 :   struct TALER_EXCHANGEDB_PostgresContext *pg = stc->pg;
      76              : 
      77           18 :   for (unsigned int i = 0; i<num_results; i++)
      78              :   {
      79              :     char *payto_uri;
      80              :     uint64_t rowid;
      81              :     struct GNUNET_TIME_Absolute execution_time;
      82              :     struct TALER_Amount amount;
      83           11 :     struct GNUNET_PQ_ResultSpec rs[] = {
      84           11 :       GNUNET_PQ_result_spec_uint64 ("serial_id",
      85              :                                     &rowid),
      86           11 :       GNUNET_PQ_result_spec_string ("payto_uri",
      87              :                                     &payto_uri),
      88           11 :       GNUNET_PQ_result_spec_absolute_time ("execution_time",
      89              :                                            &execution_time),
      90           11 :       TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
      91              :                                    &amount),
      92              :       GNUNET_PQ_result_spec_end
      93              :     };
      94              : 
      95           11 :     if (GNUNET_OK !=
      96           11 :         GNUNET_PQ_extract_result (result,
      97              :                                   rs,
      98              :                                   i))
      99              :     {
     100            0 :       GNUNET_break (0);
     101            0 :       stc->status = GNUNET_SYSERR;
     102            0 :       return;
     103              :     }
     104           11 :     stc->cb (stc->cb_cls,
     105              :              rowid,
     106              :              payto_uri,
     107              :              execution_time,
     108              :              &amount);
     109           11 :     GNUNET_PQ_cleanup_result (rs);
     110              :   }
     111              : }
     112              : 
     113              : 
     114              : enum GNUNET_DB_QueryStatus
     115            7 : TALER_EXCHANGEDB_iterate_exchange_credit_transfers (
     116              :   struct TALER_EXCHANGEDB_PostgresContext *pg,
     117              :   const struct TALER_Amount *threshold,
     118              :   uint64_t offset,
     119              :   int64_t limit,
     120              :   const struct TALER_NormalizedPaytoHashP *h_payto,
     121              :   TALER_EXCHANGEDB_AmlTransferCallback cb,
     122              :   void *cb_cls)
     123              : {
     124            7 :   struct SelectTransferContext stc = {
     125              :     .pg = pg,
     126              :     .cb = cb,
     127              :     .cb_cls = cb_cls,
     128              :     .status = GNUNET_OK
     129              :   };
     130            7 :   uint64_t ulimit = GNUNET_MIN ((uint64_t) MAX_RECORDS,
     131              :                                 TALER_EXCHANGEDB_abs_limit (limit));
     132            7 :   struct GNUNET_PQ_QueryParam params[] = {
     133            7 :     GNUNET_PQ_query_param_uint64 (&offset),
     134            7 :     GNUNET_PQ_query_param_uint64 (&ulimit),
     135            7 :     TALER_PQ_query_param_amount (pg->conn,
     136              :                                  threshold),
     137              :     NULL != h_payto
     138            1 :     ? GNUNET_PQ_query_param_auto_from_type (h_payto)
     139            7 :     : GNUNET_PQ_query_param_null (),
     140              :     GNUNET_PQ_query_param_end
     141              :   };
     142              :   enum GNUNET_DB_QueryStatus qs;
     143              : 
     144            7 :   PREPARE (pg,
     145              :            "iterate_exchange_credit_transfers_inc",
     146              :            "SELECT"
     147              :            " ri.reserve_in_serial_id AS serial_id"
     148              :            ",wt.payto_uri"
     149              :            ",ri.execution_date AS execution_time"
     150              :            ",ri.credit AS amount"
     151              :            " FROM reserves_in ri"
     152              :            " LEFT JOIN wire_targets wt"
     153              :            "   ON (ri.wire_source_h_payto = wt.wire_target_h_payto)"
     154              :            " WHERE (ri.reserve_in_serial_id > $1)"
     155              :            "   AND ( ($4::BYTEA IS NULL) OR (wt.h_normalized_payto=$4) )"
     156              :            "   AND ( ( (ri.credit).val > ($3::taler_amount).val)"
     157              :            "      OR ( ( (ri.credit).val >= ($3::taler_amount).val)"
     158              :            "       AND ( (ri.credit).frac >= ($3::taler_amount).frac) ) )"
     159              :            " ORDER BY ri.reserve_in_serial_id ASC"
     160              :            " LIMIT $2");
     161            7 :   PREPARE (pg,
     162              :            "iterate_exchange_credit_transfers_dec",
     163              :            "SELECT"
     164              :            " ri.reserve_in_serial_id AS serial_id"
     165              :            ",wt.payto_uri"
     166              :            ",ri.execution_date AS execution_time"
     167              :            ",ri.credit AS amount"
     168              :            " FROM reserves_in ri"
     169              :            " LEFT JOIN wire_targets wt"
     170              :            "   ON (ri.wire_source_h_payto = wt.wire_target_h_payto)"
     171              :            " WHERE (ri.reserve_in_serial_id < $1)"
     172              :            "   AND ( ($4::BYTEA IS NULL) OR (wt.h_normalized_payto=$4) )"
     173              :            "   AND ( ( (ri.credit).val > ($3::taler_amount).val)"
     174              :            "      OR ( ( (ri.credit).val >= ($3::taler_amount).val)"
     175              :            "       AND ( (ri.credit).frac >= ($3::taler_amount).frac) ) )"
     176              :            " ORDER BY ri.reserve_in_serial_id DESC"
     177              :            " LIMIT $2");
     178            7 :   qs = GNUNET_PQ_eval_prepared_multi_select (
     179              :     pg->conn,
     180              :     (limit > 0)
     181              :     ? "iterate_exchange_credit_transfers_inc"
     182              :     : "iterate_exchange_credit_transfers_dec",
     183              :     params,
     184              :     &handle_transfer_result,
     185              :     &stc);
     186            7 :   if (GNUNET_OK != stc.status)
     187            0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     188            7 :   return qs;
     189              : }
        

Generated by: LCOV version 2.0-1