pg_select_refreshes_above_serial_id.c (5612B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022, 2025 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_select_refreshes_above_serial_id.c 18 * @brief Implementation of the select_refreshes_above_serial_id 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_select_refreshes_above_serial_id.h" 26 #include "pg_helper.h" 27 28 29 /** 30 * Closure for #refreshs_serial_helper_cb(). 31 */ 32 struct RefreshsSerialContext 33 { 34 35 /** 36 * Callback to call. 37 */ 38 TALER_EXCHANGEDB_RefreshesCallback cb; 39 40 /** 41 * Closure for @e cb. 42 */ 43 void *cb_cls; 44 45 /** 46 * Plugin context. 47 */ 48 struct PostgresClosure *pg; 49 50 /** 51 * Status code, set to #GNUNET_SYSERR on hard errors. 52 */ 53 enum GNUNET_GenericReturnValue status; 54 }; 55 56 57 /** 58 * Helper function to be called with the results of a SELECT statement 59 * that has returned @a num_results results. 60 * 61 * @param cls closure of type `struct RefreshsSerialContext` 62 * @param result the postgres result 63 * @param num_results the number of results in @a result 64 */ 65 static void 66 refreshs_serial_helper_cb (void *cls, 67 PGresult *result, 68 unsigned int num_results) 69 { 70 struct RefreshsSerialContext *rsc = cls; 71 struct PostgresClosure *pg = rsc->pg; 72 73 for (unsigned int i = 0; i<num_results; i++) 74 { 75 struct TALER_DenominationPublicKey old_denom_pub; 76 struct TALER_CoinSpendPublicKeyP coin_pub; 77 struct TALER_CoinSpendSignatureP coin_sig; 78 struct TALER_AgeCommitmentHashP h_age_commitment; 79 bool ac_isnull; 80 struct TALER_Amount amount_with_fee; 81 uint64_t rowid; 82 struct TALER_RefreshCommitmentP rc; 83 size_t num_nds; 84 uint64_t *nds; 85 struct GNUNET_PQ_ResultSpec rs[] = { 86 TALER_PQ_result_spec_denom_pub ("old_denom_pub", 87 &old_denom_pub), 88 GNUNET_PQ_result_spec_auto_from_type ("old_coin_pub", 89 &coin_pub), 90 GNUNET_PQ_result_spec_allow_null ( 91 GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash", 92 &h_age_commitment), 93 &ac_isnull), 94 GNUNET_PQ_result_spec_auto_from_type ("old_coin_sig", 95 &coin_sig), 96 TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee", 97 &amount_with_fee), 98 GNUNET_PQ_result_spec_uint64 ("refresh_id", 99 &rowid), 100 GNUNET_PQ_result_spec_auto_from_type ("rc", 101 &rc), 102 GNUNET_PQ_result_spec_array_uint64 (pg->conn, 103 "new_denominations_serials", 104 &num_nds, 105 &nds), 106 GNUNET_PQ_result_spec_end 107 }; 108 enum GNUNET_GenericReturnValue ret; 109 110 if (GNUNET_OK != 111 GNUNET_PQ_extract_result (result, 112 rs, 113 i)) 114 { 115 GNUNET_break (0); 116 rsc->status = GNUNET_SYSERR; 117 return; 118 } 119 120 ret = rsc->cb (rsc->cb_cls, 121 rowid, 122 &old_denom_pub, 123 &coin_pub, 124 &coin_sig, 125 ac_isnull ? NULL : &h_age_commitment, 126 &amount_with_fee, 127 num_nds, 128 nds, 129 &rc); 130 GNUNET_PQ_cleanup_result (rs); 131 if (GNUNET_OK != ret) 132 break; 133 } 134 } 135 136 137 enum GNUNET_DB_QueryStatus 138 TEH_PG_select_refreshes_above_serial_id ( 139 void *cls, 140 uint64_t serial_id, 141 TALER_EXCHANGEDB_RefreshesCallback cb, 142 void *cb_cls) 143 { 144 struct PostgresClosure *pg = cls; 145 struct GNUNET_PQ_QueryParam params[] = { 146 GNUNET_PQ_query_param_uint64 (&serial_id), 147 GNUNET_PQ_query_param_end 148 }; 149 struct RefreshsSerialContext rsc = { 150 .cb = cb, 151 .cb_cls = cb_cls, 152 .pg = pg, 153 .status = GNUNET_OK 154 }; 155 enum GNUNET_DB_QueryStatus qs; 156 157 PREPARE (pg, 158 "select_refreshes_above_serial_id", 159 "SELECT" 160 " denom.denom_pub AS old_denom_pub" 161 ",r.old_coin_pub" 162 ",kc.age_commitment_hash" 163 ",r.old_coin_sig" 164 ",r.amount_with_fee" 165 ",r.refresh_id" 166 ",r.rc" 167 ",r.denom_serials AS new_denominations_serials" 168 " FROM refresh r" 169 " JOIN known_coins kc" 170 " ON (r.old_coin_pub = kc.coin_pub)" 171 " JOIN denominations denom" 172 " ON (kc.denominations_serial = denom.denominations_serial)" 173 " WHERE refresh_id>=$1" 174 " ORDER BY refresh_id ASC;"); 175 qs = GNUNET_PQ_eval_prepared_multi_select ( 176 pg->conn, 177 "select_refreshes_above_serial_id", 178 params, 179 &refreshs_serial_helper_cb, 180 &rsc); 181 if (GNUNET_OK != rsc.status) 182 return GNUNET_DB_STATUS_HARD_ERROR; 183 return qs; 184 }