exchange

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

iterate_denominations.c (5901B)


      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_denominations.c
     18  * @brief Implementation of the iterate_denominations 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_denominations.h"
     24 #include "helper.h"
     25 
     26 
     27 /**
     28  * Closure for #dominations_cb_helper()
     29  */
     30 struct DenomsIteratorContext
     31 {
     32   /**
     33    * Function to call with the results.
     34    */
     35   TALER_EXCHANGEDB_DenominationsCallback 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_denominations().
     51  * Calls the callback with each denomination key.
     52  *
     53  * @param cls a `struct DenomsIteratorContext`
     54  * @param result db results
     55  * @param num_results number of results in @a result
     56  */
     57 static void
     58 dominations_cb_helper (void *cls,
     59                        PGresult *result,
     60                        unsigned int num_results)
     61 {
     62   struct DenomsIteratorContext *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_DenominationKeyMetaData meta = {0};
     68     struct TALER_DenominationPublicKey denom_pub = {0};
     69     struct TALER_MasterSignatureP master_sig = {0};
     70     struct TALER_DenominationHashP h_denom_pub = {0};
     71     bool revoked;
     72     struct GNUNET_PQ_ResultSpec rs[] = {
     73       GNUNET_PQ_result_spec_uint64 ("denominations_serial",
     74                                     &meta.serial),
     75       GNUNET_PQ_result_spec_auto_from_type ("master_sig",
     76                                             &master_sig),
     77       GNUNET_PQ_result_spec_bool ("revoked",
     78                                   &revoked),
     79       GNUNET_PQ_result_spec_timestamp ("valid_from",
     80                                        &meta.start),
     81       GNUNET_PQ_result_spec_timestamp ("expire_withdraw",
     82                                        &meta.expire_withdraw),
     83       GNUNET_PQ_result_spec_timestamp ("expire_deposit",
     84                                        &meta.expire_deposit),
     85       GNUNET_PQ_result_spec_timestamp ("expire_legal",
     86                                        &meta.expire_legal),
     87       TALER_PQ_RESULT_SPEC_AMOUNT ("coin",
     88                                    &meta.value),
     89       TALER_PQ_RESULT_SPEC_AMOUNT ("fee_withdraw",
     90                                    &meta.fees.withdraw),
     91       TALER_PQ_RESULT_SPEC_AMOUNT ("fee_deposit",
     92                                    &meta.fees.deposit),
     93       TALER_PQ_RESULT_SPEC_AMOUNT ("fee_refresh",
     94                                    &meta.fees.refresh),
     95       TALER_PQ_RESULT_SPEC_AMOUNT ("fee_refund",
     96                                    &meta.fees.refund),
     97       TALER_PQ_result_spec_denom_pub ("denom_pub",
     98                                       &denom_pub),
     99       GNUNET_PQ_result_spec_uint32 ("age_mask",
    100                                     &meta.age_mask.bits),
    101       GNUNET_PQ_result_spec_end
    102     };
    103 
    104     if (GNUNET_OK !=
    105         GNUNET_PQ_extract_result (result,
    106                                   rs,
    107                                   i))
    108     {
    109       GNUNET_break (0);
    110       return;
    111     }
    112 
    113     /* make sure the mask information is the same */
    114     denom_pub.age_mask = meta.age_mask;
    115 
    116     TALER_denom_pub_hash (&denom_pub,
    117                           &h_denom_pub);
    118     dic->cb (dic->cb_cls,
    119              &denom_pub,
    120              &h_denom_pub,
    121              &meta,
    122              &master_sig,
    123              revoked);
    124     GNUNET_PQ_cleanup_result (rs);
    125   }
    126 }
    127 
    128 
    129 enum GNUNET_DB_QueryStatus
    130 TALER_EXCHANGEDB_iterate_denominations (struct
    131                                         TALER_EXCHANGEDB_PostgresContext *pg,
    132                                         TALER_EXCHANGEDB_DenominationsCallback
    133                                         cb,
    134                                         void *cb_cls)
    135 {
    136   struct GNUNET_TIME_Absolute now
    137     = GNUNET_TIME_absolute_get ();
    138   struct GNUNET_PQ_QueryParam params[] = {
    139     GNUNET_PQ_query_param_absolute_time (&now),
    140     GNUNET_PQ_query_param_end
    141   };
    142   struct DenomsIteratorContext dic = {
    143     .cb = cb,
    144     .cb_cls = cb_cls,
    145     .pg = pg
    146   };
    147 
    148   PREPARE (pg,
    149            "select_denominations",
    150            "SELECT"
    151            " denominations_serial"
    152            ",denominations.master_sig"
    153            ",denom_revocations_serial_id IS NOT NULL AS revoked"
    154            ",valid_from"
    155            ",expire_withdraw"
    156            ",expire_deposit"
    157            ",expire_legal"
    158            ",coin"              /* value of this denom */
    159            ",fee_withdraw"
    160            ",fee_deposit"
    161            ",fee_refresh"
    162            ",fee_refund"
    163            ",denom_type"
    164            ",age_mask"
    165            ",denom_pub"
    166            " FROM denominations"
    167            " LEFT JOIN "
    168            "   denomination_revocations USING (denominations_serial)"
    169            " WHERE expire_deposit > $1;");
    170   return GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    171                                                "select_denominations",
    172                                                params,
    173                                                &dominations_cb_helper,
    174                                                &dic);
    175 }