Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2023 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 backenddb/pg_lookup_pending_deposits.c
18 : * @brief Implementation of the lookup_pending_deposits function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "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_lookup_pending_deposits.h"
26 : #include "pg_helper.h"
27 :
28 :
29 : /**
30 : * Context for lookup_pending_deposits().
31 : */
32 : struct LookupDepositsContext
33 : {
34 : /**
35 : * Function to call with the results.
36 : */
37 : TALER_MERCHANTDB_PendingDepositsCallback cb;
38 :
39 : /**
40 : * Closure for @e cb.
41 : */
42 : void *cb_cls;
43 :
44 : /**
45 : * Database context.
46 : */
47 : struct PostgresClosure *pg;
48 :
49 : /**
50 : * Set to the return value on errors.
51 : */
52 : enum GNUNET_DB_QueryStatus qs;
53 :
54 : };
55 :
56 :
57 : /**
58 : * Function to be called with the results of a SELECT statement
59 : * that has returned @a num_results results about instances.
60 : *
61 : * @param cls of type `struct LookupDepositsContext *`
62 : * @param result the postgres result
63 : * @param num_results the number of results in @a result
64 : */
65 : static void
66 8 : lookup_deposits_cb (void *cls,
67 : PGresult *result,
68 : unsigned int num_results)
69 : {
70 8 : struct LookupDepositsContext *ldc = cls;
71 :
72 8 : for (unsigned int i = 0; i < num_results; i++)
73 : {
74 : uint64_t deposit_serial;
75 : struct GNUNET_TIME_Absolute wire_deadline;
76 : struct GNUNET_TIME_Relative retry_backoff;
77 : struct TALER_PrivateContractHashP h_contract_terms;
78 : struct TALER_MerchantPrivateKeyP merchant_priv;
79 : char *instance_id;
80 : struct TALER_MerchantWireHashP h_wire;
81 : struct TALER_Amount amount_with_fee;
82 : struct TALER_Amount deposit_fee;
83 : struct TALER_CoinSpendPublicKeyP coin_pub;
84 0 : struct GNUNET_PQ_ResultSpec rs[] = {
85 0 : GNUNET_PQ_result_spec_uint64 ("deposit_serial",
86 : &deposit_serial),
87 0 : GNUNET_PQ_result_spec_auto_from_type ("h_contract_terms",
88 : &h_contract_terms),
89 0 : GNUNET_PQ_result_spec_auto_from_type ("merchant_priv",
90 : &merchant_priv),
91 0 : GNUNET_PQ_result_spec_string ("merchant_id",
92 : &instance_id),
93 0 : GNUNET_PQ_result_spec_absolute_time ("wire_transfer_deadline",
94 : &wire_deadline),
95 0 : GNUNET_PQ_result_spec_relative_time ("retry_backoff",
96 : &retry_backoff),
97 0 : GNUNET_PQ_result_spec_auto_from_type ("h_wire",
98 : &h_wire),
99 0 : TALER_PQ_result_spec_amount_with_currency ("amount_with_fee",
100 : &amount_with_fee),
101 0 : TALER_PQ_result_spec_amount_with_currency ("deposit_fee",
102 : &deposit_fee),
103 0 : GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
104 : &coin_pub),
105 : GNUNET_PQ_result_spec_end
106 : };
107 :
108 0 : if (GNUNET_OK !=
109 0 : GNUNET_PQ_extract_result (result,
110 : rs,
111 : i))
112 : {
113 0 : GNUNET_break (0);
114 0 : ldc->qs = GNUNET_DB_STATUS_HARD_ERROR;
115 0 : return;
116 : }
117 0 : ldc->cb (ldc->cb_cls,
118 : deposit_serial,
119 : wire_deadline,
120 : retry_backoff,
121 : &h_contract_terms,
122 : &merchant_priv,
123 : instance_id,
124 : &h_wire,
125 : &amount_with_fee,
126 : &deposit_fee,
127 : &coin_pub);
128 0 : GNUNET_PQ_cleanup_result (rs);
129 : }
130 : }
131 :
132 :
133 : enum GNUNET_DB_QueryStatus
134 8 : TMH_PG_lookup_pending_deposits (
135 : void *cls,
136 : const char *exchange_url,
137 : uint64_t limit,
138 : bool allow_future,
139 : TALER_MERCHANTDB_PendingDepositsCallback cb,
140 : void *cb_cls)
141 : {
142 8 : struct PostgresClosure *pg = cls;
143 8 : struct LookupDepositsContext ldc = {
144 : .cb = cb,
145 : .cb_cls = cb_cls,
146 : .pg = pg
147 : };
148 : struct GNUNET_TIME_Absolute now
149 8 : = GNUNET_TIME_absolute_get ();
150 8 : struct GNUNET_PQ_QueryParam params[] = {
151 8 : GNUNET_PQ_query_param_string (exchange_url),
152 8 : GNUNET_PQ_query_param_absolute_time (&now),
153 8 : GNUNET_PQ_query_param_uint64 (&limit),
154 8 : GNUNET_PQ_query_param_bool (allow_future),
155 : GNUNET_PQ_query_param_end
156 : };
157 : enum GNUNET_DB_QueryStatus qs;
158 :
159 8 : check_connection (pg);
160 8 : PREPARE (pg,
161 : "lookup_pending_deposits",
162 : "SELECT"
163 : " md.deposit_serial"
164 : ",mct.h_contract_terms"
165 : ",mk.merchant_priv"
166 : ",mi.merchant_id"
167 : ",mdc.wire_transfer_deadline"
168 : ",ma.h_wire"
169 : ",md.amount_with_fee"
170 : ",md.deposit_fee"
171 : ",md.coin_pub"
172 : ",mdc.retry_backoff"
173 : " FROM merchant_deposit_confirmations mdc"
174 : " JOIN merchant_contract_terms mct"
175 : " USING (order_serial)"
176 : " JOIN merchant_accounts ma"
177 : " USING (account_serial)"
178 : " LEFT JOIN merchant_kyc kyc"
179 : " ON (ma.account_serial=kyc.account_serial)"
180 : " JOIN merchant_instances mi"
181 : " ON (mct.merchant_serial=mi.merchant_serial)"
182 : " JOIN merchant_keys mk"
183 : " ON (mi.merchant_serial=mk.merchant_serial)"
184 : " JOIN merchant_deposits md"
185 : " USING (deposit_confirmation_serial)"
186 : " WHERE mdc.wire_pending"
187 : " AND (mdc.exchange_url=$1)"
188 : " AND ($4 OR (mdc.wire_transfer_deadline < $2))"
189 : " AND ( (kyc.kyc_ok IS NULL) OR kyc.kyc_ok)"
190 : " ORDER BY mdc.wire_transfer_deadline ASC"
191 : " LIMIT $3");
192 8 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
193 : "lookup_pending_deposits",
194 : params,
195 : &lookup_deposits_cb,
196 : &ldc);
197 8 : if (0 > ldc.qs)
198 0 : return ldc.qs;
199 8 : return qs;
200 : }
|