exchange

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

iterate_denomination_info.c (5942B)


      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/iterate_denomination_info.c
     18  * @brief Implementation of the iterate_denomination_info function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/taler_error_codes.h"
     22 #include "taler/taler_pq_lib.h"
     23 #include "exchange-database/iterate_denomination_info.h"
     24 #include "helper.h"
     25 
     26 
     27 /**
     28  * Closure for #domination_cb_helper()
     29  */
     30 struct DenomIteratorContext
     31 {
     32   /**
     33    * Function to call with the results.
     34    */
     35   TALER_EXCHANGEDB_DenominationCallback cb;
     36 
     37   /**
     38    * Closure to pass to @e cb
     39    */
     40   void *cb_cls;
     41 
     42   /**
     43    * Plugin context.
     44    */
     45   struct TALER_EXCHANGEDB_PostgresContext *pg;
     46 };
     47 
     48 
     49 /**
     50  * Helper function for #TALER_EXCHANGEDB_iterate_denomination_info().
     51  * Calls the callback with each denomination key.
     52  *
     53  * @param cls a `struct DenomIteratorContext`
     54  * @param result db results
     55  * @param num_results number of results in @a result
     56  */
     57 static void
     58 domination_cb_helper (void *cls,
     59                       PGresult *result,
     60                       unsigned int num_results)
     61 {
     62   struct DenomIteratorContext *dic = cls;
     63   struct TALER_EXCHANGEDB_PostgresContext *pg = dic->pg;
     64 
     65   for (unsigned int i = 0; i<num_results; i++)
     66   {
     67     struct TALER_EXCHANGEDB_DenominationKeyInformation issue;
     68     struct TALER_DenominationPublicKey denom_pub;
     69     struct TALER_DenominationHashP denom_hash;
     70     uint64_t denom_serial;
     71     struct GNUNET_PQ_ResultSpec rs[] = {
     72       GNUNET_PQ_result_spec_uint64 ("denominations_serial",
     73                                     &denom_serial),
     74       GNUNET_PQ_result_spec_auto_from_type ("master_sig",
     75                                             &issue.signature),
     76       GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash",
     77                                             &denom_hash),
     78       GNUNET_PQ_result_spec_timestamp ("valid_from",
     79                                        &issue.start),
     80       GNUNET_PQ_result_spec_timestamp ("expire_withdraw",
     81                                        &issue.expire_withdraw),
     82       GNUNET_PQ_result_spec_timestamp ("expire_deposit",
     83                                        &issue.expire_deposit),
     84       GNUNET_PQ_result_spec_timestamp ("expire_legal",
     85                                        &issue.expire_legal),
     86       TALER_PQ_RESULT_SPEC_AMOUNT ("coin",
     87                                    &issue.value),
     88       TALER_PQ_RESULT_SPEC_AMOUNT ("fee_withdraw",
     89                                    &issue.fees.withdraw),
     90       TALER_PQ_RESULT_SPEC_AMOUNT ("fee_deposit",
     91                                    &issue.fees.deposit),
     92       TALER_PQ_RESULT_SPEC_AMOUNT ("fee_refresh",
     93                                    &issue.fees.refresh),
     94       TALER_PQ_RESULT_SPEC_AMOUNT ("fee_refund",
     95                                    &issue.fees.refund),
     96       TALER_PQ_result_spec_denom_pub ("denom_pub",
     97                                       &denom_pub),
     98       GNUNET_PQ_result_spec_uint32 ("age_mask",
     99                                     &issue.age_mask.bits),
    100       GNUNET_PQ_result_spec_end
    101     };
    102 
    103     if (GNUNET_OK !=
    104         GNUNET_PQ_extract_result (result,
    105                                   rs,
    106                                   i))
    107     {
    108       GNUNET_break (0);
    109       return;
    110     }
    111 
    112     /* Unfortunately we have to carry the age mask in both, the
    113      * TALER_DenominationPublicKey and
    114      * TALER_EXCHANGEDB_DenominationKeyInformation at different times.
    115      * Here we use _both_ so let's make sure the values are the same. */
    116     denom_pub.age_mask = issue.age_mask;
    117     TALER_denom_pub_hash (&denom_pub,
    118                           &issue.denom_hash);
    119     if (0 !=
    120         GNUNET_memcmp (&issue.denom_hash,
    121                        &denom_hash))
    122     {
    123       GNUNET_break (0);
    124     }
    125     else
    126     {
    127       dic->cb (dic->cb_cls,
    128                denom_serial,
    129                &denom_pub,
    130                &issue);
    131     }
    132     TALER_denom_pub_free (&denom_pub);
    133   }
    134 }
    135 
    136 
    137 enum GNUNET_DB_QueryStatus
    138 TALER_EXCHANGEDB_iterate_denomination_info (struct
    139                                             TALER_EXCHANGEDB_PostgresContext *pg
    140                                             ,
    141                                             TALER_EXCHANGEDB_DenominationCallback
    142                                             cb,
    143                                             void *cb_cls)
    144 {
    145   struct GNUNET_PQ_QueryParam params[] = {
    146     GNUNET_PQ_query_param_end
    147   };
    148   struct DenomIteratorContext dic = {
    149     .cb = cb,
    150     .cb_cls = cb_cls,
    151     .pg = pg
    152   };
    153 
    154   PREPARE (pg,
    155            "iterate_denomination_info",
    156            "SELECT"
    157            " denominations_serial"
    158            ",master_sig"
    159            ",denom_pub_hash"
    160            ",valid_from"
    161            ",expire_withdraw"
    162            ",expire_deposit"
    163            ",expire_legal"
    164            ",coin" /* value of this denom */
    165            ",fee_withdraw"
    166            ",fee_deposit"
    167            ",fee_refresh"
    168            ",fee_refund"
    169            ",denom_pub"
    170            ",age_mask"
    171            " FROM denominations;");
    172   return GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    173                                                "iterate_denomination_info",
    174                                                params,
    175                                                &domination_cb_helper,
    176                                                &dic);
    177 }