Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022, 2024 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/insert_reserve_closed.c
18 : * @brief Implementation of the insert_reserve_closed function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "exchange-database/insert_reserve_closed.h"
23 : #include "helper.h"
24 : #include "exchange-database/reserves_get.h"
25 : #include "exchange-database/reserves_update.h"
26 :
27 : enum GNUNET_DB_QueryStatus
28 9 : TALER_EXCHANGEDB_insert_reserve_closed (
29 : struct TALER_EXCHANGEDB_PostgresContext *pg,
30 : const struct TALER_ReservePublicKeyP *reserve_pub,
31 : struct GNUNET_TIME_Timestamp execution_date,
32 : const struct TALER_FullPayto receiver_account,
33 : const struct TALER_WireTransferIdentifierRawP *wtid,
34 : const struct TALER_Amount *amount_with_fee,
35 : const struct TALER_Amount *closing_fee,
36 : uint64_t close_request_row)
37 : {
38 : struct TALER_EXCHANGEDB_Reserve reserve;
39 : enum GNUNET_DB_QueryStatus qs;
40 : struct TALER_FullPaytoHashP h_payto;
41 :
42 9 : TALER_full_payto_hash (receiver_account,
43 : &h_payto);
44 : {
45 9 : struct GNUNET_PQ_QueryParam params[] = {
46 9 : GNUNET_PQ_query_param_auto_from_type (reserve_pub),
47 9 : GNUNET_PQ_query_param_timestamp (&execution_date),
48 9 : GNUNET_PQ_query_param_auto_from_type (wtid),
49 9 : GNUNET_PQ_query_param_auto_from_type (&h_payto),
50 9 : TALER_PQ_query_param_amount (pg->conn,
51 : amount_with_fee),
52 9 : TALER_PQ_query_param_amount (pg->conn,
53 : closing_fee),
54 9 : GNUNET_PQ_query_param_uint64 (&close_request_row),
55 : GNUNET_PQ_query_param_end
56 : };
57 :
58 9 : PREPARE (pg,
59 : "reserves_close_insert",
60 : "INSERT INTO reserves_close "
61 : "(reserve_pub"
62 : ",execution_date"
63 : ",wtid"
64 : ",wire_target_h_payto"
65 : ",amount"
66 : ",closing_fee"
67 : ",close_request_row"
68 : ") VALUES ($1, $2, $3, $4, $5, $6, $7);");
69 :
70 9 : qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
71 : "reserves_close_insert",
72 : params);
73 : }
74 9 : if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
75 0 : return qs;
76 :
77 : /* update reserve balance */
78 9 : reserve.pub = *reserve_pub;
79 9 : if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
80 9 : (qs = TALER_EXCHANGEDB_reserves_get (pg,
81 : &reserve)))
82 : {
83 : /* Existence should have been checked before we got here... */
84 0 : GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR == qs);
85 0 : if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
86 0 : qs = GNUNET_DB_STATUS_HARD_ERROR;
87 0 : return qs;
88 : }
89 : {
90 : enum TALER_AmountArithmeticResult ret;
91 :
92 9 : ret = TALER_amount_subtract (&reserve.balance,
93 : &reserve.balance,
94 : amount_with_fee);
95 9 : if (ret < 0)
96 : {
97 : /* The reserve history was checked to make sure there is enough of a balance
98 : left before we tried this; however, concurrent operations may have changed
99 : the situation by now. We should re-try the transaction. */
100 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
101 : "Closing of reserve `%s' refused due to balance mismatch. Retrying.\n",
102 : TALER_B2S (reserve_pub));
103 0 : return GNUNET_DB_STATUS_HARD_ERROR;
104 : }
105 9 : GNUNET_break (TALER_AAR_RESULT_ZERO == ret);
106 : }
107 9 : return TALER_EXCHANGEDB_reserves_update (pg,
108 : &reserve);
109 : }
|