get_refresh.c (8776B)
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/get_refresh.c 18 * @brief Implementation of the get_refresh function for Postgres 19 * @author get_refresh 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "exchange-database/get_refresh.h" 23 #include "helper.h" 24 25 26 enum GNUNET_DB_QueryStatus 27 TALER_EXCHANGEDB_get_refresh ( 28 struct TALER_EXCHANGEDB_PostgresContext *pg, 29 const struct TALER_RefreshCommitmentP *rc, 30 struct TALER_EXCHANGEDB_Refresh_vDOLDPLUS *refresh) 31 { 32 struct GNUNET_PQ_QueryParam params[] = { 33 GNUNET_PQ_query_param_auto_from_type (rc), 34 GNUNET_PQ_query_param_end 35 }; 36 bool no_cs_r_values; 37 bool no_cs_r_choices; 38 bool no_transfer_pubs; 39 size_t num_denom_sigs; 40 size_t num_transfer_pubs; 41 struct TALER_BlindedDenominationSignature *denom_sigs = NULL; 42 struct GNUNET_CRYPTO_CSPublicRPairP *cs_r_values = NULL; 43 struct TALER_TransferPublicKeyP *transfer_pubs = NULL; 44 uint64_t *denom_serials = NULL; 45 uint32_t rc_version; 46 struct GNUNET_PQ_ResultSpec rs[] = { 47 GNUNET_PQ_result_spec_uint64 ("refresh_id", 48 &refresh->refresh_id), 49 TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee", 50 &refresh->amount_with_fee), 51 GNUNET_PQ_result_spec_auto_from_type ("old_coin_pub", 52 &refresh->coin.coin_pub), 53 GNUNET_PQ_result_spec_allow_null ( 54 GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash", 55 &refresh->coin.h_age_commitment), 56 &refresh->coin.no_age_commitment), 57 GNUNET_PQ_result_spec_auto_from_type ("old_coin_sig", 58 &refresh->coin_sig), 59 GNUNET_PQ_result_spec_auto_from_type ("refresh_seed", 60 &refresh->refresh_seed), 61 GNUNET_PQ_result_spec_uint32 ("noreveal_index", 62 &refresh->noreveal_index), 63 GNUNET_PQ_result_spec_allow_null ( 64 GNUNET_PQ_result_spec_auto_from_type ("blinding_seed", 65 &refresh->blinding_seed), 66 &refresh->no_blinding_seed), 67 GNUNET_PQ_result_spec_allow_null ( 68 TALER_PQ_result_spec_array_cs_r_pub (pg->conn, 69 "cs_r_values", 70 &refresh->num_cs_r_values, 71 &cs_r_values), 72 &no_cs_r_values), 73 GNUNET_PQ_result_spec_allow_null ( 74 GNUNET_PQ_result_spec_uint64 ("cs_r_choices", 75 &refresh->cs_r_choices), 76 &no_cs_r_choices), 77 GNUNET_PQ_result_spec_auto_from_type ("planchets_h", 78 &refresh->planchets_h), 79 GNUNET_PQ_result_spec_auto_from_type ("selected_h", 80 &refresh->selected_h), 81 GNUNET_PQ_result_spec_allow_null ( 82 GNUNET_PQ_result_spec_array_fixed_size (pg->conn, 83 "transfer_pubs", 84 sizeof(*transfer_pubs), 85 &num_transfer_pubs, 86 (void **) &transfer_pubs), 87 &no_transfer_pubs), 88 GNUNET_PQ_result_spec_array_uint64 (pg->conn, 89 "denom_serials", 90 &refresh->num_coins, 91 &denom_serials), 92 TALER_PQ_result_spec_array_blinded_denom_sig (pg->conn, 93 "denom_sigs", 94 &num_denom_sigs, 95 &denom_sigs), 96 GNUNET_PQ_result_spec_bool ("revealed", 97 &refresh->revealed), 98 GNUNET_PQ_result_spec_uint32 ("rc_version", 99 &rc_version), 100 GNUNET_PQ_result_spec_end 101 }; 102 enum GNUNET_DB_QueryStatus qs; 103 104 memset (&refresh->coin.denom_sig, 105 0, 106 sizeof (refresh->coin.denom_sig)); 107 PREPARE (pg, 108 "get_refresh", 109 "SELECT" 110 " refresh_id" 111 ",amount_with_fee" 112 ",old_coin_pub" 113 ",kc.age_commitment_hash AS age_commitment_hash" 114 ",old_coin_sig" 115 ",refresh_seed" 116 ",noreveal_index" 117 ",blinding_seed" 118 ",cs_r_values" 119 ",cs_r_choices" 120 ",planchets_h" 121 ",transfer_pubs" 122 ",selected_h" 123 ",denom_serials" 124 ",denom_sigs" 125 ",revealed" 126 ",rc_version" 127 " FROM refresh" 128 " JOIN known_coins kc" 129 " ON (old_coin_pub = kc.coin_pub)" 130 " WHERE rc = $1;" 131 ); 132 qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 133 "get_refresh", 134 params, 135 rs); 136 GNUNET_PQ_cleanup_query_params_closures (params); 137 if (0 > qs) 138 { 139 GNUNET_break (0); 140 GNUNET_PQ_cleanup_result (rs); 141 return qs; 142 } 143 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) 144 { 145 GNUNET_PQ_cleanup_result (rs); 146 return qs; 147 } 148 if (refresh->num_coins != num_denom_sigs) 149 { 150 GNUNET_break (0); 151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 152 "got inconsistent number of entries in refresh from DB: " 153 "num_coins=%ld, num_denom_sigs=%ld\n", 154 refresh->num_coins, 155 num_denom_sigs); 156 GNUNET_PQ_cleanup_result (rs); 157 return GNUNET_DB_STATUS_HARD_ERROR; 158 } 159 switch (rc_version) 160 { 161 case TALER_REFRESH_COMMITMENT_VERSION_LEGACY_V27: 162 case TALER_REFRESH_COMMITMENT_VERSION_LEGACY_VDOLDPLUS_BROKEN: 163 case TALER_REFRESH_COMMITMENT_VERSION_CURRENT: 164 break; 165 default: 166 GNUNET_break (0); 167 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 168 "got unknown rc_version %u in refresh from DB\n", 169 (unsigned int) rc_version); 170 GNUNET_PQ_cleanup_result (rs); 171 return GNUNET_DB_STATUS_HARD_ERROR; 172 } 173 refresh->rc_version = (enum TALER_RefreshCommitmentVersion) rc_version; 174 if (no_transfer_pubs != 175 (TALER_REFRESH_COMMITMENT_VERSION_LEGACY_V27 == refresh->rc_version)) 176 { 177 GNUNET_break (0); 178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 179 "rc_version %u inconsistent with transfer_pubs in refresh from DB\n", 180 (unsigned int) rc_version); 181 GNUNET_PQ_cleanup_result (rs); 182 return GNUNET_DB_STATUS_HARD_ERROR; 183 } 184 if (no_transfer_pubs) 185 { 186 refresh->transfer_pubs = NULL; 187 } 188 else 189 { 190 if (num_transfer_pubs != refresh->num_coins) 191 { 192 GNUNET_break (0); 193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 194 "got inconsistent number of transfer_pubs in refresh from DB: " 195 "num_coins=%ld, num_transfer_pubs=%ld\n", 196 refresh->num_coins, 197 num_transfer_pubs); 198 GNUNET_PQ_cleanup_result (rs); 199 return GNUNET_DB_STATUS_HARD_ERROR; 200 } 201 refresh->transfer_pubs = transfer_pubs; 202 } 203 /* A blinding seed may be present without any CS denomination 204 (the client is free to send one), but CS values require it. */ 205 if (refresh->no_blinding_seed && ! no_cs_r_values) 206 { 207 GNUNET_break (0); 208 GNUNET_PQ_cleanup_result (rs); 209 return GNUNET_DB_STATUS_HARD_ERROR; 210 } 211 if (no_cs_r_choices != no_cs_r_values) 212 { 213 GNUNET_break (0); 214 GNUNET_PQ_cleanup_result (rs); 215 return GNUNET_DB_STATUS_HARD_ERROR; 216 } 217 if (no_cs_r_values) 218 { 219 refresh->cs_r_values = NULL; 220 refresh->num_cs_r_values = 0; 221 } 222 if (refresh->coin.no_age_commitment) 223 memset (&refresh->coin.h_age_commitment, 224 0, 225 sizeof(refresh->coin.h_age_commitment)); 226 refresh->rc = *rc; 227 /* move the result arrays */ 228 refresh->denom_sigs = denom_sigs; 229 refresh->denom_serials = denom_serials; 230 refresh->cs_r_values = cs_r_values; 231 transfer_pubs = NULL; 232 denom_sigs = NULL; 233 denom_serials = NULL; 234 cs_r_values = NULL; 235 GNUNET_PQ_cleanup_result (rs); 236 return qs; 237 }