pg_do_deposit.c (7312B)
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/pg_do_deposit.c 18 * @brief Implementation of the do_deposit 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_do_deposit.h" 26 #include "pg_helper.h" 27 #include "pg_compute_shard.h" 28 29 30 enum GNUNET_DB_QueryStatus 31 TEH_PG_do_deposit ( 32 void *cls, 33 const struct TALER_EXCHANGEDB_BatchDeposit *bd, 34 const struct TALER_Amount deposit_fees[], 35 struct GNUNET_TIME_Timestamp *exchange_timestamp, 36 struct TALER_Amount *accumulated_total_without_fee, 37 bool *balance_ok, 38 uint32_t *bad_balance_index, 39 bool *ctr_conflict) 40 { 41 struct PostgresClosure *pg = cls; 42 uint64_t deposit_shard = TEH_PG_compute_shard (&bd->merchant_pub); 43 const struct TALER_CoinSpendPublicKeyP *coin_pubs[GNUNET_NZL (bd->num_cdis)]; 44 const struct TALER_CoinSpendSignatureP *coin_sigs[GNUNET_NZL (bd->num_cdis)]; 45 struct TALER_Amount amounts_with_fee[GNUNET_NZL (bd->num_cdis)]; 46 struct TALER_Amount total_amount; 47 struct TALER_Amount total_without_fee; 48 struct TALER_NormalizedPaytoHashP h_normalized_payto; 49 struct GNUNET_PQ_QueryParam params[] = { 50 /* data for batch_deposits */ 51 GNUNET_PQ_query_param_uint64 (&deposit_shard), 52 GNUNET_PQ_query_param_auto_from_type (&bd->merchant_pub), 53 GNUNET_PQ_query_param_auto_from_type (&bd->merchant_sig), 54 GNUNET_PQ_query_param_timestamp (&bd->wallet_timestamp), 55 GNUNET_PQ_query_param_timestamp (exchange_timestamp), 56 GNUNET_PQ_query_param_timestamp (&bd->refund_deadline), 57 GNUNET_PQ_query_param_timestamp (&bd->wire_deadline), 58 GNUNET_PQ_query_param_auto_from_type (&bd->h_contract_terms), 59 (bd->no_wallet_data_hash) 60 ? GNUNET_PQ_query_param_null () 61 : GNUNET_PQ_query_param_auto_from_type (&bd->wallet_data_hash), 62 GNUNET_PQ_query_param_auto_from_type (&bd->wire_salt), 63 GNUNET_PQ_query_param_auto_from_type (&bd->wire_target_h_payto), 64 GNUNET_PQ_query_param_auto_from_type (&h_normalized_payto), 65 (0 == bd->policy_details_serial_id) 66 ? GNUNET_PQ_query_param_null () 67 : GNUNET_PQ_query_param_uint64 (&bd->policy_details_serial_id), 68 GNUNET_PQ_query_param_bool (bd->policy_blocked), 69 /* to create entry in wire_targets */ 70 GNUNET_PQ_query_param_string (bd->receiver_wire_account.full_payto), 71 /* arrays for coin_deposits */ 72 GNUNET_PQ_query_param_array_ptrs_auto_from_type (bd->num_cdis, 73 coin_pubs, 74 pg->conn), 75 GNUNET_PQ_query_param_array_ptrs_auto_from_type (bd->num_cdis, 76 coin_sigs, 77 pg->conn), 78 TALER_PQ_query_param_array_amount (bd->num_cdis, 79 amounts_with_fee, 80 pg->conn), 81 TALER_PQ_query_param_array_amount (bd->num_cdis, 82 deposit_fees, 83 pg->conn), 84 TALER_PQ_query_param_amount (pg->conn, 85 &total_amount), 86 TALER_PQ_query_param_amount (pg->conn, 87 &total_without_fee), 88 GNUNET_PQ_query_param_bool (TALER_payto_is_wallet ( 89 bd->receiver_wire_account.full_payto)), 90 NULL == bd->extra_wire_subject_metadata 91 ? GNUNET_PQ_query_param_null () 92 : GNUNET_PQ_query_param_string (bd->extra_wire_subject_metadata), 93 GNUNET_PQ_query_param_end 94 }; 95 bool no_time; 96 bool no_amount; 97 struct GNUNET_PQ_ResultSpec rs[] = { 98 GNUNET_PQ_result_spec_allow_null ( 99 GNUNET_PQ_result_spec_timestamp ("exchange_timestamp", 100 exchange_timestamp), 101 &no_time), 102 GNUNET_PQ_result_spec_allow_null ( 103 TALER_PQ_RESULT_SPEC_AMOUNT ("accumulated_total_without_fee", 104 accumulated_total_without_fee), 105 &no_amount), 106 GNUNET_PQ_result_spec_allow_null ( 107 GNUNET_PQ_result_spec_uint32 ("insufficient_balance_coin_index", 108 bad_balance_index), 109 balance_ok), 110 GNUNET_PQ_result_spec_bool ("conflicted", 111 ctr_conflict), 112 GNUNET_PQ_result_spec_end 113 }; 114 enum GNUNET_DB_QueryStatus qs; 115 116 GNUNET_assert (0 < bd->num_cdis); 117 TALER_full_payto_normalize_and_hash (bd->receiver_wire_account, 118 &h_normalized_payto); 119 for (unsigned int i = 0; i < bd->num_cdis; i++) 120 { 121 const struct TALER_EXCHANGEDB_CoinDepositInformation *cdi 122 = &bd->cdis[i]; 123 124 if (0 == i) 125 { 126 total_amount = cdi->amount_with_fee; 127 total_without_fee = cdi->amount_with_fee; 128 } 129 else 130 { 131 GNUNET_assert (0 <= 132 TALER_amount_add (&total_amount, 133 &total_amount, 134 &cdi->amount_with_fee)); 135 GNUNET_assert (0 <= 136 TALER_amount_add (&total_without_fee, 137 &total_without_fee, 138 &cdi->amount_with_fee)); 139 } 140 GNUNET_assert (0 <= 141 TALER_amount_subtract ( 142 &total_without_fee, 143 &total_without_fee, 144 &deposit_fees[i])); 145 146 amounts_with_fee[i] = cdi->amount_with_fee; 147 coin_pubs[i] = &cdi->coin.coin_pub; 148 coin_sigs[i] = &cdi->csig; 149 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 150 "Do deposit %u = %s\n", 151 i, 152 TALER_B2S (&cdi->coin.coin_pub)); 153 } 154 PREPARE (pg, 155 "call_deposit", 156 "SELECT " 157 " out_exchange_timestamp AS exchange_timestamp" 158 ",out_accumulated_total_without_fee AS accumulated_total_without_fee" 159 ",out_insufficient_balance_coin_index AS insufficient_balance_coin_index" 160 ",out_conflict AS conflicted" 161 " FROM exchange_do_deposit" 162 " ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10" 163 ",$11,$12,$13,$14,$15,$16,$17,$18,$19,$20" 164 ",$21,$22,$23);"); 165 qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 166 "call_deposit", 167 params, 168 rs); 169 GNUNET_PQ_cleanup_query_params_closures (params); 170 if (no_amount) 171 memset (accumulated_total_without_fee, 172 0, 173 sizeof (struct TALER_Amount)); 174 return qs; 175 }