exchange

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

pg_lookup_wire_fee_by_time.c (4557B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022 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_wire_fee_by_time.c
     18  * @brief Implementation of the lookup_wire_fee_by_time function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/taler_pq_lib.h"
     22 #include "taler/exchange-database/lookup_wire_fee_by_time.h"
     23 #include "helper.h"
     24 
     25 
     26 /**
     27  * Closure for #wire_fee_by_time_helper()
     28  */
     29 struct WireFeeLookupContext
     30 {
     31 
     32   /**
     33    * Set to the wire fees. Set to invalid if fees conflict over
     34    * the given time period.
     35    */
     36   struct TALER_WireFeeSet *fees;
     37 
     38   /**
     39    * Plugin context.
     40    */
     41   struct TALER_EXCHANGEDB_PostgresContext *pg;
     42 };
     43 
     44 
     45 /**
     46  * Helper function for #TALER_EXCHANGEDB_lookup_wire_fee_by_time().
     47  * Calls the callback with the wire fee structure.
     48  *
     49  * @param cls a `struct WireFeeLookupContext`
     50  * @param result db results
     51  * @param num_results number of results in @a result
     52  */
     53 static void
     54 wire_fee_by_time_helper (void *cls,
     55                          PGresult *result,
     56                          unsigned int num_results)
     57 {
     58   struct WireFeeLookupContext *wlc = cls;
     59   struct TALER_EXCHANGEDB_PostgresContext *pg = wlc->pg;
     60 
     61   for (unsigned int i = 0; i<num_results; i++)
     62   {
     63     struct TALER_WireFeeSet fs;
     64     struct GNUNET_PQ_ResultSpec rs[] = {
     65       TALER_PQ_RESULT_SPEC_AMOUNT ("wire_fee",
     66                                    &fs.wire),
     67       TALER_PQ_RESULT_SPEC_AMOUNT ("closing_fee",
     68                                    &fs.closing),
     69       GNUNET_PQ_result_spec_end
     70     };
     71 
     72     if (GNUNET_OK !=
     73         GNUNET_PQ_extract_result (result,
     74                                   rs,
     75                                   i))
     76     {
     77       GNUNET_break (0);
     78       /* invalidate */
     79       memset (wlc->fees,
     80               0,
     81               sizeof (struct TALER_WireFeeSet));
     82       return;
     83     }
     84     if (0 == i)
     85     {
     86       *wlc->fees = fs;
     87       continue;
     88     }
     89     if (0 !=
     90         TALER_wire_fee_set_cmp (&fs,
     91                                 wlc->fees))
     92     {
     93       /* invalidate */
     94       memset (wlc->fees,
     95               0,
     96               sizeof (struct TALER_WireFeeSet));
     97       return;
     98     }
     99   }
    100 }
    101 
    102 
    103 /**
    104  * Lookup information about known wire fees.  Finds all applicable
    105  * fees in the given range. If they are identical, returns the
    106  * respective @a fees. If any of the fees
    107  * differ between @a start_time and @a end_time, the transaction
    108  * succeeds BUT returns an invalid amount for both fees.
    109  *
    110  * @param pg the database context
    111  * @param wire_method the wire method to lookup fees for
    112  * @param start_time starting time of fee
    113  * @param end_time end time of fee
    114  * @param[out] fees wire fees for that time period; if
    115  *             different fees exists within this time
    116  *             period, an 'invalid' amount is returned.
    117  * @return transaction status code
    118  */
    119 enum GNUNET_DB_QueryStatus
    120 TALER_EXCHANGEDB_lookup_wire_fee_by_time (
    121   struct TALER_EXCHANGEDB_PostgresContext *pg,
    122   const char *wire_method,
    123   struct GNUNET_TIME_Timestamp start_time,
    124   struct GNUNET_TIME_Timestamp end_time,
    125   struct TALER_WireFeeSet *fees)
    126 {
    127   struct GNUNET_PQ_QueryParam params[] = {
    128     GNUNET_PQ_query_param_string (wire_method),
    129     GNUNET_PQ_query_param_timestamp (&start_time),
    130     GNUNET_PQ_query_param_timestamp (&end_time),
    131     GNUNET_PQ_query_param_end
    132   };
    133   struct WireFeeLookupContext wlc = {
    134     .fees = fees,
    135     .pg = pg
    136   };
    137 
    138   PREPARE (pg,
    139            "lookup_wire_fee_by_time",
    140            "SELECT"
    141            " wire_fee"
    142            ",closing_fee"
    143            " FROM wire_fee"
    144            " WHERE wire_method=$1"
    145            " AND end_date > $2"
    146            " AND start_date < $3;");
    147   return GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    148                                                "lookup_wire_fee_by_time",
    149                                                params,
    150                                                &wire_fee_by_time_helper,
    151                                                &wlc);
    152 }