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 (4574B)


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