pg_select_recoup_refresh_above_serial_id.c (7345B)
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_select_recoup_refresh_above_serial_id.c 18 * @brief Implementation of the select_recoup_refresh_above_serial_id function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "taler/exchange-database/select_recoup_refresh_above_serial_id.h" 23 #include "helper.h" 24 25 26 /** 27 * Closure for #recoup_refresh_serial_helper_cb(). 28 */ 29 struct RecoupRefreshSerialContext 30 { 31 32 /** 33 * Callback to call. 34 */ 35 TALER_EXCHANGEDB_RecoupRefreshCallback cb; 36 37 /** 38 * Closure for @e cb. 39 */ 40 void *cb_cls; 41 42 /** 43 * Plugin context. 44 */ 45 struct TALER_EXCHANGEDB_PostgresContext *pg; 46 47 /** 48 * Status code, set to #GNUNET_SYSERR on hard errors. 49 */ 50 enum GNUNET_GenericReturnValue status; 51 }; 52 53 54 /** 55 * Helper function to be called with the results of a SELECT statement 56 * that has returned @a num_results results. 57 * 58 * @param cls closure of type `struct RecoupRefreshSerialContext` 59 * @param result the postgres result 60 * @param num_results the number of results in @a result 61 */ 62 static void 63 recoup_refresh_serial_helper_cb (void *cls, 64 PGresult *result, 65 unsigned int num_results) 66 { 67 struct RecoupRefreshSerialContext *psc = cls; 68 struct TALER_EXCHANGEDB_PostgresContext *pg = psc->pg; 69 70 for (unsigned int i = 0; i<num_results; i++) 71 { 72 uint64_t rowid; 73 struct TALER_CoinSpendPublicKeyP old_coin_pub; 74 struct TALER_CoinPublicInfo coin; 75 struct TALER_CoinSpendSignatureP coin_sig; 76 union GNUNET_CRYPTO_BlindingSecretP coin_blind; 77 struct TALER_DenominationPublicKey denom_pub; 78 struct TALER_DenominationHashP old_denom_pub_hash; 79 struct TALER_Amount amount; 80 struct TALER_BlindedCoinHashP h_blind_ev; 81 struct GNUNET_TIME_Timestamp timestamp; 82 struct GNUNET_PQ_ResultSpec rs[] = { 83 GNUNET_PQ_result_spec_uint64 ("recoup_refresh_uuid", 84 &rowid), 85 GNUNET_PQ_result_spec_timestamp ("recoup_timestamp", 86 ×tamp), 87 GNUNET_PQ_result_spec_auto_from_type ("old_coin_pub", 88 &old_coin_pub), 89 GNUNET_PQ_result_spec_auto_from_type ("old_denom_pub_hash", 90 &old_denom_pub_hash), 91 GNUNET_PQ_result_spec_auto_from_type ("coin_pub", 92 &coin.coin_pub), 93 GNUNET_PQ_result_spec_auto_from_type ("coin_sig", 94 &coin_sig), 95 GNUNET_PQ_result_spec_auto_from_type ("coin_blind", 96 &coin_blind), 97 TALER_PQ_result_spec_denom_pub ("denom_pub", 98 &denom_pub), 99 GNUNET_PQ_result_spec_auto_from_type ("h_blind_ev", 100 &h_blind_ev), 101 GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash", 102 &coin.denom_pub_hash), 103 GNUNET_PQ_result_spec_allow_null ( 104 GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash", 105 &coin.h_age_commitment), 106 &coin.no_age_commitment), 107 TALER_PQ_result_spec_denom_sig ("denom_sig", 108 &coin.denom_sig), 109 TALER_PQ_RESULT_SPEC_AMOUNT ("amount", 110 &amount), 111 GNUNET_PQ_result_spec_end 112 }; 113 enum GNUNET_GenericReturnValue ret; 114 115 if (GNUNET_OK != 116 GNUNET_PQ_extract_result (result, 117 rs, 118 i)) 119 { 120 GNUNET_break (0); 121 psc->status = GNUNET_SYSERR; 122 return; 123 } 124 ret = psc->cb (psc->cb_cls, 125 rowid, 126 timestamp, 127 &amount, 128 &old_coin_pub, 129 &old_denom_pub_hash, 130 &coin, 131 &denom_pub, 132 &coin_sig, 133 &coin_blind); 134 GNUNET_PQ_cleanup_result (rs); 135 if (GNUNET_OK != ret) 136 break; 137 } 138 } 139 140 141 enum GNUNET_DB_QueryStatus 142 TALER_EXCHANGEDB_select_recoup_refresh_above_serial_id ( 143 struct TALER_EXCHANGEDB_PostgresContext *pg, 144 uint64_t serial_id, 145 TALER_EXCHANGEDB_RecoupRefreshCallback cb, 146 void *cb_cls) 147 { 148 struct GNUNET_PQ_QueryParam params[] = { 149 GNUNET_PQ_query_param_uint64 (&serial_id), 150 GNUNET_PQ_query_param_end 151 }; 152 struct RecoupRefreshSerialContext psc = { 153 .cb = cb, 154 .cb_cls = cb_cls, 155 .pg = pg, 156 .status = GNUNET_OK 157 }; 158 enum GNUNET_DB_QueryStatus qs; 159 160 PREPARE (pg, 161 "select_recoup_refresh_above_serial_id", 162 "SELECT" 163 " rr.recoup_refresh_uuid" 164 ",rr.recoup_timestamp" 165 ",rr.coin_sig" 166 ",rr.coin_blind" 167 ",rr.amount" 168 ",rrc.h_coin_ev AS h_blind_ev" // FIXME:-#9828 r.rc? r.selected_h? Old logic wanted a TALER_BlindedCoinHash, which we now need to derive (from rr.coin_blind) 169 ",new_coins.age_commitment_hash" 170 ",new_coins.coin_pub AS coin_pub" 171 ",new_denoms.denom_pub AS denom_pub" 172 ",new_denoms.denom_pub_hash" 173 ",new_coins.denom_sig AS denom_sig" 174 ",old_coins.coin_pub AS old_coin_pub" 175 ",old_denoms.denom_pub_hash AS old_denom_pub_hash" 176 " FROM recoup_refresh rr" 177 " INNER JOIN refresh_revealed_coins rrc" // FIXME-#9828: no such table anymore! 178 // but we have 'refresh_id" which is an FK into 'refresh'! 179 " USING (rrc_serial)" 180 " INNER JOIN refresh r" 181 // but we have 'refresh_id" which is an FK into 'refresh'! 182 " USING (refresh_id)" 183 " INNER JOIN known_coins old_coins" 184 " ON (r.old_coin_pub = old_coins.coin_pub)" 185 " INNER JOIN known_coins new_coins" 186 " ON (rr.coin_pub == new_coins.coin_pub)" 187 " INNER JOIN refresh_commitments rfc" 188 " ON (rrc.melt_serial_id = rfc.melt_serial_id)" 189 " INNER JOIN denominations new_denoms" 190 " ON (new_coins.denominations_serial = new_denoms.denominations_serial)" 191 " INNER JOIN denominations old_denoms" 192 " ON (old_coins.denominations_serial = old_denoms.denominations_serial)" 193 " WHERE recoup_refresh_uuid>=$1" 194 " ORDER BY recoup_refresh_uuid ASC;"); 195 qs = GNUNET_PQ_eval_prepared_multi_select ( 196 pg->conn, 197 "select_recoup_refresh_above_serial_id", 198 params, 199 &recoup_refresh_serial_helper_cb, 200 &psc); 201 if (GNUNET_OK != psc.status) 202 return GNUNET_DB_STATUS_HARD_ERROR; 203 return qs; 204 }