iterate_denomination_info.c (6158B)
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 /** 138 * Fetch information about all known denomination keys. 139 * 140 * @param pg the database context 141 * @param cb function to call on each denomination key 142 * @param cb_cls closure for @a cb 143 * @return transaction status code 144 */ 145 enum GNUNET_DB_QueryStatus 146 TALER_EXCHANGEDB_iterate_denomination_info (struct 147 TALER_EXCHANGEDB_PostgresContext *pg 148 , 149 TALER_EXCHANGEDB_DenominationCallback 150 cb, 151 void *cb_cls) 152 { 153 struct GNUNET_PQ_QueryParam params[] = { 154 GNUNET_PQ_query_param_end 155 }; 156 struct DenomIteratorContext dic = { 157 .cb = cb, 158 .cb_cls = cb_cls, 159 .pg = pg 160 }; 161 162 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 return GNUNET_PQ_eval_prepared_multi_select (pg->conn, 181 "denomination_iterate", 182 params, 183 &domination_cb_helper, 184 &dic); 185 }