Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 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/iterate_wallet_merges.c
18 : * @brief Implementation of the select_wallet_merges function
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "exchange-database/iterate_wallet_merges.h"
23 : #include "helper.h"
24 :
25 :
26 : /**
27 : * Hard upper bound on the number of records returned by a single
28 : * call, regardless of the limit requested by the client.
29 : */
30 : #define MAX_RECORDS 50000
31 :
32 :
33 : /**
34 : * Closure for #handle_aml_result.
35 : */
36 : struct SelectTransferContext
37 : {
38 : /**
39 : * Function to call on each result.
40 : */
41 : TALER_EXCHANGEDB_AmlTransferCallback cb;
42 :
43 : /**
44 : * Closure for @e cb.
45 : */
46 : void *cb_cls;
47 :
48 : /**
49 : * Plugin context.
50 : */
51 : struct TALER_EXCHANGEDB_PostgresContext *pg;
52 :
53 : /**
54 : * Set to #GNUNET_SYSERR on serious errors.
55 : */
56 : enum GNUNET_GenericReturnValue status;
57 : };
58 :
59 :
60 : /**
61 : * Function to be called with the results of a SELECT statement
62 : * that has returned @a num_results results. Helper function
63 : * for #TALER_EXCHANGEDB_iterate_wallet_merges().
64 : *
65 : * @param cls closure of type `struct SelectTransferContext *`
66 : * @param result the postgres result
67 : * @param num_results the number of results in @a result
68 : */
69 : static void
70 6 : handle_transfer_result (void *cls,
71 : PGresult *result,
72 : unsigned int num_results)
73 : {
74 6 : struct SelectTransferContext *stc = cls;
75 6 : struct TALER_EXCHANGEDB_PostgresContext *pg = stc->pg;
76 :
77 9 : for (unsigned int i = 0; i<num_results; i++)
78 : {
79 : char *payto_uri;
80 : uint64_t rowid;
81 : struct GNUNET_TIME_Absolute execution_time;
82 : struct TALER_Amount amount;
83 3 : struct GNUNET_PQ_ResultSpec rs[] = {
84 3 : GNUNET_PQ_result_spec_uint64 ("serial_id",
85 : &rowid),
86 3 : GNUNET_PQ_result_spec_string ("payto_uri",
87 : &payto_uri),
88 3 : GNUNET_PQ_result_spec_absolute_time ("execution_time",
89 : &execution_time),
90 3 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
91 : &amount),
92 : GNUNET_PQ_result_spec_end
93 : };
94 :
95 3 : if (GNUNET_OK !=
96 3 : GNUNET_PQ_extract_result (result,
97 : rs,
98 : i))
99 : {
100 0 : GNUNET_break (0);
101 0 : stc->status = GNUNET_SYSERR;
102 0 : return;
103 : }
104 3 : stc->cb (stc->cb_cls,
105 : rowid,
106 : payto_uri,
107 : execution_time,
108 : &amount);
109 3 : GNUNET_PQ_cleanup_result (rs);
110 : }
111 : }
112 :
113 :
114 : enum GNUNET_DB_QueryStatus
115 6 : TALER_EXCHANGEDB_iterate_wallet_merges (
116 : struct TALER_EXCHANGEDB_PostgresContext *pg,
117 : const struct TALER_Amount *threshold,
118 : uint64_t offset,
119 : int64_t limit,
120 : const struct TALER_NormalizedPaytoHashP *h_payto,
121 : TALER_EXCHANGEDB_AmlTransferCallback cb,
122 : void *cb_cls)
123 : {
124 6 : struct SelectTransferContext stc = {
125 : .pg = pg,
126 : .cb = cb,
127 : .cb_cls = cb_cls,
128 : .status = GNUNET_OK
129 : };
130 6 : uint64_t ulimit = GNUNET_MIN ((uint64_t) MAX_RECORDS,
131 : TALER_EXCHANGEDB_abs_limit (limit));
132 6 : struct GNUNET_PQ_QueryParam params[] = {
133 6 : GNUNET_PQ_query_param_uint64 (&offset),
134 6 : GNUNET_PQ_query_param_uint64 (&ulimit),
135 6 : TALER_PQ_query_param_amount (pg->conn,
136 : threshold),
137 : NULL != h_payto
138 1 : ? GNUNET_PQ_query_param_auto_from_type (h_payto)
139 6 : : GNUNET_PQ_query_param_null (),
140 : GNUNET_PQ_query_param_end
141 : };
142 : enum GNUNET_DB_QueryStatus qs;
143 :
144 6 : PREPARE (pg,
145 : "iterate_wallet_merges_inc",
146 : "SELECT"
147 : " pd.purse_decision_serial_id AS serial_id"
148 : ",wt.payto_uri"
149 : ",pd.action_timestamp AS execution_time"
150 : ",pr.amount_with_fee AS amount"
151 : " FROM purse_decision pd"
152 : " JOIN purse_requests pr"
153 : " ON (pr.purse_pub = pd.purse_pub)"
154 : " JOIN purse_merges pm"
155 : " ON (pm.purse_pub = pd.purse_pub)"
156 : " JOIN kyc_targets kt"
157 : " ON (kt.target_pub = pm.reserve_pub)"
158 : " JOIN wire_targets wt"
159 : " ON (wt.h_normalized_payto = kt.h_normalized_payto)"
160 : " WHERE kt.is_wallet"
161 : " AND NOT pd.refunded"
162 : " AND (pd.purse_decision_serial_id > $1)"
163 : " AND ( ($4::BYTEA IS NULL) OR (wt.h_normalized_payto=$4) )"
164 : " AND ( ( (pr.amount_with_fee).val > ($3::taler_amount).val)"
165 : " OR ( ( (pr.amount_with_fee).val >= ($3::taler_amount).val)"
166 : " AND ( (pr.amount_with_fee).frac >= ($3::taler_amount).frac) ) )"
167 : " ORDER BY pd.purse_decision_serial_id ASC"
168 : " LIMIT $2");
169 6 : PREPARE (pg,
170 : "iterate_wallet_merges_dec",
171 : "SELECT"
172 : " pd.purse_decision_serial_id AS serial_id"
173 : ",wt.payto_uri"
174 : ",pd.action_timestamp AS execution_time"
175 : ",pr.amount_with_fee AS amount"
176 : " FROM purse_decision pd"
177 : " JOIN purse_requests pr"
178 : " ON (pr.purse_pub = pd.purse_pub)"
179 : " JOIN purse_merges pm"
180 : " ON (pm.purse_pub = pd.purse_pub)"
181 : " JOIN kyc_targets kt"
182 : " ON (kt.target_pub = pm.reserve_pub)"
183 : " JOIN wire_targets wt"
184 : " ON (wt.h_normalized_payto = kt.h_normalized_payto)"
185 : " WHERE kt.is_wallet"
186 : " AND NOT pd.refunded"
187 : " AND (pd.purse_decision_serial_id < $1)"
188 : " AND ( ($4::BYTEA IS NULL) OR (wt.h_normalized_payto=$4) )"
189 : " AND ( ( (pr.amount_with_fee).val > ($3::taler_amount).val)"
190 : " OR ( ( (pr.amount_with_fee).val >= ($3::taler_amount).val)"
191 : " AND ( (pr.amount_with_fee).frac >= ($3::taler_amount).frac) ) )"
192 : " ORDER BY pd.purse_decision_serial_id DESC"
193 : " LIMIT $2");
194 6 : qs = GNUNET_PQ_eval_prepared_multi_select (
195 : pg->conn,
196 : (limit > 0)
197 : ? "iterate_wallet_merges_inc"
198 : : "iterate_wallet_merges_dec",
199 : params,
200 : &handle_transfer_result,
201 : &stc);
202 6 : if (GNUNET_OK != stc.status)
203 0 : return GNUNET_DB_STATUS_HARD_ERROR;
204 6 : return qs;
205 : }
|