do_reserve_purse.c (4979B)
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/do_reserve_purse.c 18 * @brief Implementation of the do_reserve_purse function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "exchange-database/do_reserve_purse.h" 23 #include "helper.h" 24 25 26 /** 27 * Function called insert request to merge a purse into a reserve by the 28 * respective purse merge key. The purse must not have been merged into a 29 * different reserve. 30 * 31 * @param pg the database context 32 * @param purse_pub purse to merge 33 * @param merge_sig signature affirming the merge 34 * @param merge_timestamp time of the merge 35 * @param reserve_sig signature of the reserve affirming the merge 36 * @param purse_fee amount to charge the reserve for the purse creation, NULL to use the quota 37 * @param reserve_pub public key of the reserve to credit 38 * @param[out] in_conflict set to true if @a purse_pub was merged into a different reserve already 39 * @param[out] no_reserve set to true if @a reserve_pub is not a known reserve 40 * @param[out] insufficient_funds set to true if @a reserve_pub has insufficient capacity to create another purse 41 * @return transaction status code 42 */ 43 enum GNUNET_DB_QueryStatus 44 TALER_EXCHANGEDB_do_reserve_purse ( 45 struct TALER_EXCHANGEDB_PostgresContext *pg, 46 const struct TALER_PurseContractPublicKeyP *purse_pub, 47 const struct TALER_PurseMergeSignatureP *merge_sig, 48 const struct GNUNET_TIME_Timestamp merge_timestamp, 49 const struct TALER_ReserveSignatureP *reserve_sig, 50 const struct TALER_Amount *purse_fee, 51 const struct TALER_ReservePublicKeyP *reserve_pub, 52 bool *in_conflict, 53 bool *no_reserve, 54 bool *insufficient_funds) 55 { 56 struct TALER_Amount zero_fee; 57 struct TALER_NormalizedPaytoHashP h_payto; 58 struct GNUNET_TIME_Timestamp reserve_expiration 59 = GNUNET_TIME_absolute_to_timestamp ( 60 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), 61 pg->idle_reserve_expiration_time)); 62 struct GNUNET_TIME_Timestamp reserve_gc 63 = GNUNET_TIME_absolute_to_timestamp ( 64 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), 65 pg->legal_reserve_expiration_time)); 66 67 struct GNUNET_PQ_QueryParam params[] = { 68 GNUNET_PQ_query_param_auto_from_type (purse_pub), 69 GNUNET_PQ_query_param_auto_from_type (merge_sig), 70 GNUNET_PQ_query_param_timestamp (&merge_timestamp), 71 GNUNET_PQ_query_param_timestamp (&reserve_expiration), 72 GNUNET_PQ_query_param_timestamp (&reserve_gc), 73 GNUNET_PQ_query_param_auto_from_type (reserve_sig), 74 GNUNET_PQ_query_param_bool (NULL == purse_fee), 75 TALER_PQ_query_param_amount (pg->conn, 76 NULL == purse_fee 77 ? &zero_fee 78 : purse_fee), 79 GNUNET_PQ_query_param_auto_from_type (reserve_pub), 80 GNUNET_PQ_query_param_auto_from_type (&h_payto), 81 GNUNET_PQ_query_param_end 82 }; 83 struct GNUNET_PQ_ResultSpec rs[] = { 84 GNUNET_PQ_result_spec_bool ("insufficient_funds", 85 insufficient_funds), 86 GNUNET_PQ_result_spec_bool ("conflict", 87 in_conflict), 88 GNUNET_PQ_result_spec_bool ("no_reserve", 89 no_reserve), 90 GNUNET_PQ_result_spec_end 91 }; 92 93 { 94 struct TALER_NormalizedPayto payto_uri; 95 96 payto_uri = TALER_reserve_make_payto (pg->exchange_url, 97 reserve_pub); 98 TALER_normalized_payto_hash (payto_uri, 99 &h_payto); 100 GNUNET_free (payto_uri.normalized_payto); 101 } 102 GNUNET_assert (GNUNET_OK == 103 TALER_amount_set_zero (pg->currency, 104 &zero_fee)); 105 PREPARE (pg, 106 "call_reserve_purse", 107 "SELECT" 108 " out_no_funds AS insufficient_funds" 109 ",out_no_reserve AS no_reserve" 110 ",out_conflict AS conflict" 111 " FROM exchange_do_reserve_purse" 112 " ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);"); 113 114 return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 115 "call_reserve_purse", 116 params, 117 rs); 118 }