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