LCOV - code coverage report
Current view: top level - exchangedb - pg_iterate_denomination_info.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 33 36 91.7 %
Date: 2025-06-22 12:09:43 Functions: 2 2 100.0 %

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

Generated by: LCOV version 1.16