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