do_import_credits.c (13287B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022--2026 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/do_import_credits.c 18 * @brief Implementation of the do_import_credits function for Postgres 19 * @author Christian Grothoff 20 * @author Joseph Xu 21 */ 22 #include "taler/taler_pq_lib.h" 23 #include "exchange-database/do_import_credits.h" 24 #include "helper.h" 25 26 27 /** 28 * Compute the notification channel for a reserve that was credited. 29 * 30 * @param reserve_pub reserve to notify on 31 * @return string to pass to postgres for the notification 32 */ 33 static char * 34 compute_notify_on_reserve (const struct TALER_ReservePublicKeyP *reserve_pub) 35 { 36 struct TALER_EXCHANGEDB_ReserveEventP rep = { 37 .header.size = htons (sizeof (rep)), 38 .header.type = htons (TALER_DBEVENT_EXCHANGE_RESERVE_INCOMING), 39 .reserve_pub = *reserve_pub 40 }; 41 42 return GNUNET_PQ_get_event_notify_channel (&rep.header); 43 } 44 45 46 /** 47 * Compute the notification channel for an account that authenticated itself. 48 * 49 * @param h_payto normalized hash of the account that was authenticated 50 * @return string to pass to postgres for the notification 51 */ 52 static char * 53 compute_notify_on_kycauth (const struct TALER_NormalizedPaytoHashP *h_payto) 54 { 55 struct TALER_EXCHANGEDB_KycCompletedEventP rep = { 56 .header.size = htons (sizeof (rep)), 57 .header.type = htons (TALER_DBEVENT_EXCHANGE_KYC_COMPLETED), 58 .h_payto = *h_payto 59 }; 60 61 return GNUNET_PQ_get_event_notify_channel (&rep.header); 62 } 63 64 65 /** 66 * Closure for #helper_cb(). 67 */ 68 struct Context 69 { 70 /** 71 * Array with entries set to 'true' for transfers we had already imported. 72 */ 73 bool *transaction_duplicates; 74 75 /** 76 * Length of the @e transaction_duplicates array. 77 */ 78 unsigned int length; 79 80 /** 81 * Set to #GNUNET_SYSERR on failures. 82 */ 83 enum GNUNET_GenericReturnValue status; 84 }; 85 86 87 /** 88 * Function called with the rows the stored procedure returned, one per 89 * reserve transfer, in input order. 90 * 91 * @param cls closure of type `struct Context *` 92 * @param result the postgres result 93 * @param num_results the number of results in @a result 94 */ 95 static void 96 helper_cb (void *cls, 97 PGresult *result, 98 unsigned int num_results) 99 { 100 struct Context *ctx = cls; 101 102 if (num_results != ctx->length) 103 { 104 /* The procedure returns one row per reserve transfer we passed in; a 105 different count means the two have drifted apart. More rows would 106 write past the caller's array, fewer would leave entries of it 107 unset. */ 108 GNUNET_break (0); 109 ctx->status = GNUNET_SYSERR; 110 return; 111 } 112 for (unsigned int i = 0; i<num_results; i++) 113 { 114 struct GNUNET_PQ_ResultSpec rs[] = { 115 GNUNET_PQ_result_spec_bool ("out_duplicate", 116 &ctx->transaction_duplicates[i]), 117 GNUNET_PQ_result_spec_end 118 }; 119 120 if (GNUNET_OK != 121 GNUNET_PQ_extract_result (result, 122 rs, 123 i)) 124 { 125 GNUNET_break (0); 126 ctx->status = GNUNET_SYSERR; 127 return; 128 } 129 } 130 } 131 132 133 enum GNUNET_DB_QueryStatus 134 TALER_EXCHANGEDB_do_import_credits ( 135 struct TALER_EXCHANGEDB_PostgresContext *pg, 136 const struct TALER_EXCHANGEDB_CreditBatch *batch, 137 enum GNUNET_DB_QueryStatus *reserve_results) 138 { 139 unsigned int rlen = batch->reserves_length; 140 unsigned int klen = batch->kycauths_length; 141 unsigned int wlen = batch->wads_length; 142 unsigned int dups = 0; 143 144 /* reserve transfers */ 145 struct TALER_ReservePublicKeyP reserve_pubs[GNUNET_NZL (rlen)]; 146 uint64_t wire_references[GNUNET_NZL (rlen)]; 147 struct TALER_Amount balances[GNUNET_NZL (rlen)]; 148 struct GNUNET_TIME_Timestamp execution_times[GNUNET_NZL (rlen)]; 149 struct TALER_FullPaytoHashP h_full_paytos[GNUNET_NZL (rlen)]; 150 struct TALER_NormalizedPaytoHashP h_normalized_paytos[GNUNET_NZL (rlen)]; 151 const char *payto_uris[GNUNET_NZL (rlen)]; 152 char *notify_s[GNUNET_NZL (rlen)]; 153 bool transaction_duplicates[GNUNET_NZL (rlen)]; 154 155 /* KYC authentication transfers */ 156 union TALER_AccountPublicKeyP ka_account_pubs[GNUNET_NZL (klen)]; 157 uint64_t ka_wire_references[GNUNET_NZL (klen)]; 158 struct TALER_Amount ka_balances[GNUNET_NZL (klen)]; 159 struct GNUNET_TIME_Timestamp ka_execution_times[GNUNET_NZL (klen)]; 160 struct TALER_FullPaytoHashP ka_h_full_paytos[GNUNET_NZL (klen)]; 161 struct TALER_NormalizedPaytoHashP ka_h_normalized_paytos[GNUNET_NZL (klen)]; 162 const char *ka_payto_uris[GNUNET_NZL (klen)]; 163 char *ka_notify_s[GNUNET_NZL (klen)]; 164 165 /* WAD transfers */ 166 struct TALER_WadIdentifierP wad_ids[GNUNET_NZL (wlen)]; 167 const char *wad_origin_exchange_urls[GNUNET_NZL (wlen)]; 168 struct TALER_Amount wad_balances[GNUNET_NZL (wlen)]; 169 struct GNUNET_TIME_Timestamp wad_execution_times[GNUNET_NZL (wlen)]; 170 171 struct GNUNET_TIME_Timestamp reserve_expiration 172 = GNUNET_TIME_relative_to_timestamp (pg->idle_reserve_expiration_time); 173 struct GNUNET_TIME_Timestamp gc 174 = GNUNET_TIME_relative_to_timestamp (pg->legal_reserve_expiration_time); 175 struct GNUNET_TIME_Absolute lease_until 176 = GNUNET_TIME_relative_to_absolute (batch->lease); 177 enum GNUNET_DB_QueryStatus qs; 178 179 for (unsigned int i = 0; i<rlen; i++) 180 { 181 const struct TALER_EXCHANGEDB_ReserveInInfo *reserve = &batch->reserves[i]; 182 183 TALER_full_payto_hash (reserve->sender_account_details, 184 &h_full_paytos[i]); 185 TALER_full_payto_normalize_and_hash (reserve->sender_account_details, 186 &h_normalized_paytos[i]); 187 notify_s[i] = compute_notify_on_reserve (reserve->reserve_pub); 188 reserve_pubs[i] = *reserve->reserve_pub; 189 balances[i] = *reserve->balance; 190 execution_times[i] = reserve->execution_time; 191 payto_uris[i] = reserve->sender_account_details.full_payto; 192 wire_references[i] = reserve->wire_reference; 193 } 194 for (unsigned int i = 0; i<klen; i++) 195 { 196 const struct TALER_EXCHANGEDB_KycauthInInfo *ka = &batch->kycauths[i]; 197 198 TALER_full_payto_hash (ka->sender_account_details, 199 &ka_h_full_paytos[i]); 200 TALER_full_payto_normalize_and_hash (ka->sender_account_details, 201 &ka_h_normalized_paytos[i]); 202 ka_notify_s[i] = compute_notify_on_kycauth (&ka_h_normalized_paytos[i]); 203 ka_account_pubs[i] = *ka->account_pub; 204 ka_balances[i] = *ka->balance; 205 ka_execution_times[i] = ka->execution_time; 206 ka_payto_uris[i] = ka->sender_account_details.full_payto; 207 ka_wire_references[i] = ka->wire_reference; 208 } 209 for (unsigned int i = 0; i<wlen; i++) 210 { 211 const struct TALER_EXCHANGEDB_WadInInfo *wad = &batch->wads[i]; 212 213 wad_ids[i] = *wad->wad_id; 214 wad_origin_exchange_urls[i] = wad->origin_exchange_url; 215 wad_balances[i] = *wad->balance; 216 wad_execution_times[i] = wad->execution_time; 217 } 218 219 PREPARE (pg, 220 "do_import_credits", 221 "SELECT" 222 " out_duplicate" 223 " FROM exchange_do_import_credits" 224 " ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17" 225 " ,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28);"); 226 { 227 struct GNUNET_PQ_QueryParam params[] = { 228 GNUNET_PQ_query_param_string (batch->exchange_account_name), 229 GNUNET_PQ_query_param_timestamp (&gc), 230 GNUNET_PQ_query_param_timestamp (&reserve_expiration), 231 /* reserve transfers */ 232 GNUNET_PQ_query_param_array_auto_from_type (rlen, 233 reserve_pubs, 234 pg->conn), 235 GNUNET_PQ_query_param_array_uint64 (rlen, 236 wire_references, 237 pg->conn), 238 TALER_PQ_query_param_array_amount (rlen, 239 balances, 240 pg->conn), 241 GNUNET_PQ_query_param_array_timestamp (rlen, 242 execution_times, 243 pg->conn), 244 GNUNET_PQ_query_param_array_auto_from_type (rlen, 245 h_full_paytos, 246 pg->conn), 247 GNUNET_PQ_query_param_array_auto_from_type (rlen, 248 h_normalized_paytos, 249 pg->conn), 250 GNUNET_PQ_query_param_array_ptrs_string (rlen, 251 payto_uris, 252 pg->conn), 253 GNUNET_PQ_query_param_array_ptrs_string (rlen, 254 (const char **) notify_s, 255 pg->conn), 256 /* KYC authentication transfers */ 257 GNUNET_PQ_query_param_array_auto_from_type (klen, 258 ka_account_pubs, 259 pg->conn), 260 GNUNET_PQ_query_param_array_uint64 (klen, 261 ka_wire_references, 262 pg->conn), 263 TALER_PQ_query_param_array_amount (klen, 264 ka_balances, 265 pg->conn), 266 GNUNET_PQ_query_param_array_timestamp (klen, 267 ka_execution_times, 268 pg->conn), 269 GNUNET_PQ_query_param_array_auto_from_type (klen, 270 ka_h_full_paytos, 271 pg->conn), 272 GNUNET_PQ_query_param_array_auto_from_type (klen, 273 ka_h_normalized_paytos, 274 pg->conn), 275 GNUNET_PQ_query_param_array_ptrs_string (klen, 276 ka_payto_uris, 277 pg->conn), 278 GNUNET_PQ_query_param_array_ptrs_string (klen, 279 (const char **) ka_notify_s, 280 pg->conn), 281 /* WAD transfers */ 282 GNUNET_PQ_query_param_array_auto_from_type (wlen, 283 wad_ids, 284 pg->conn), 285 GNUNET_PQ_query_param_array_ptrs_string (wlen, 286 wad_origin_exchange_urls, 287 pg->conn), 288 TALER_PQ_query_param_array_amount (wlen, 289 wad_balances, 290 pg->conn), 291 GNUNET_PQ_query_param_array_timestamp (wlen, 292 wad_execution_times, 293 pg->conn), 294 /* shard bookkeeping */ 295 GNUNET_PQ_query_param_string (batch->job_name), 296 GNUNET_PQ_query_param_uint64 (&batch->shard_start), 297 GNUNET_PQ_query_param_uint64 (&batch->shard_end), 298 GNUNET_PQ_query_param_uint64 (&batch->progress_row), 299 GNUNET_PQ_query_param_absolute_time (&lease_until), 300 GNUNET_PQ_query_param_end 301 }; 302 struct Context ctx = { 303 .transaction_duplicates = transaction_duplicates, 304 .length = rlen, 305 .status = GNUNET_OK 306 }; 307 308 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 309 "do_import_credits", 310 params, 311 &helper_cb, 312 &ctx); 313 GNUNET_PQ_cleanup_query_params_closures (params); 314 if ( (qs >= 0) && 315 (GNUNET_OK != ctx.status) ) 316 qs = GNUNET_DB_STATUS_HARD_ERROR; 317 if (qs < 0) 318 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 319 "Failed to import credit batch (%d)\n", 320 qs); 321 } 322 323 for (unsigned int i = 0; i<rlen; i++) 324 GNUNET_free (notify_s[i]); 325 for (unsigned int i = 0; i<klen; i++) 326 GNUNET_free (ka_notify_s[i]); 327 if (qs < 0) 328 return qs; 329 for (unsigned int i = 0; i<rlen; i++) 330 { 331 if (transaction_duplicates[i]) 332 dups++; 333 reserve_results[i] = transaction_duplicates[i] 334 ? GNUNET_DB_STATUS_SUCCESS_NO_RESULTS 335 : GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; 336 } 337 GNUNET_PQ_event_do_poll (pg->conn); 338 if (0 != dups) 339 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 340 "%u/%u duplicates among incoming transactions. Try increasing WIREWATCH_IDLE_SLEEP_INTERVAL in the [exchange] configuration section (if this happens a lot).\n", 341 dups, 342 rlen); 343 return qs; 344 }