Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 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 :
18 : #include "taler/taler_error_codes.h"
19 : #include "taler/taler_pq_lib.h"
20 : #include "pg_helper.h"
21 : #include "auditor-database/iterate_coin_inconsistencies.h"
22 :
23 :
24 : /**
25 : * Hard upper bound on the number of records returned by a single
26 : * call, regardless of the limit requested by the client.
27 : */
28 : #define MAX_RECORDS 50000
29 :
30 :
31 : /**
32 : * Closure for #deposit_confirmation_cb().
33 : */
34 : struct CoinInconsistencyContext
35 : {
36 :
37 : /**
38 : * Function to call for each deposit confirmation.
39 : */
40 : TALER_AUDITORDB_CoinInconsistencyCallback cb;
41 :
42 : /**
43 : * Closure for @e cb
44 : */
45 : void *cb_cls;
46 :
47 : /**
48 : * Plugin context.
49 : */
50 : struct TALER_AUDITORDB_PostgresContext *pg;
51 :
52 : /**
53 : * Query status to return.
54 : */
55 : enum GNUNET_DB_QueryStatus qs;
56 : };
57 :
58 :
59 : /**
60 : * Helper function for #TALER_AUDITORDB_iterate_coin_inconsistencies().
61 : * To be called with the results of a SELECT statement
62 : * that has returned @a num_results results.
63 : *
64 : * @param cls closure of type `struct DepositConfirmationContext *`
65 : * @param result the postgres result
66 : * @param num_results the number of results in @a result
67 : */
68 : static void
69 7 : coin_inconsistency_cb (void *cls,
70 : PGresult *result,
71 : unsigned int num_results)
72 : {
73 7 : struct CoinInconsistencyContext *dcc = cls;
74 7 : struct TALER_AUDITORDB_PostgresContext *pg = dcc->pg;
75 :
76 10 : for (unsigned int i = 0; i < num_results; i++)
77 : {
78 : struct TALER_AUDITORDB_CoinInconsistency dc;
79 : /* libgnunetpq only recently added support for
80 : GNUNET_PQ_result_spec_int16() change this after
81 : the next GNUnet release! */
82 : int64_t profitable;
83 :
84 3 : struct GNUNET_PQ_ResultSpec rs[] = {
85 3 : GNUNET_PQ_result_spec_uint64 ("row_id",
86 : &dc.row_id),
87 3 : GNUNET_PQ_result_spec_string ("operation",
88 : &dc.operation),
89 3 : TALER_PQ_RESULT_SPEC_AMOUNT ("exchange_amount",
90 : &dc.exchange_amount),
91 3 : TALER_PQ_RESULT_SPEC_AMOUNT ("auditor_amount",
92 : &dc.auditor_amount),
93 3 : GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
94 : &dc.coin_pub),
95 3 : GNUNET_PQ_result_spec_int64 ("profitable",
96 : &profitable),
97 3 : GNUNET_PQ_result_spec_bool ("suppressed",
98 : &dc.suppressed),
99 :
100 : GNUNET_PQ_result_spec_end
101 : };
102 : enum GNUNET_GenericReturnValue rval;
103 :
104 3 : if (GNUNET_OK !=
105 3 : GNUNET_PQ_extract_result (result,
106 : rs,
107 : i))
108 : {
109 0 : GNUNET_break (0);
110 0 : dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
111 0 : return;
112 : }
113 :
114 3 : dc.profitable = (int16_t) profitable;
115 3 : dcc->qs = i + 1;
116 3 : rval = dcc->cb (dcc->cb_cls,
117 : dc.row_id,
118 : &dc);
119 3 : GNUNET_PQ_cleanup_result (rs);
120 3 : if (GNUNET_OK != rval)
121 0 : break;
122 : }
123 : }
124 :
125 :
126 : enum GNUNET_DB_QueryStatus
127 7 : TALER_AUDITORDB_iterate_coin_inconsistencies (
128 : struct TALER_AUDITORDB_PostgresContext *pg,
129 : int64_t limit,
130 : uint64_t offset,
131 : bool return_suppressed, // maybe not needed
132 : TALER_AUDITORDB_CoinInconsistencyCallback cb,
133 : void *cb_cls)
134 : {
135 7 : uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS,
136 : TALER_AUDITORDB_abs_limit (limit));
137 7 : struct GNUNET_PQ_QueryParam params[] = {
138 7 : GNUNET_PQ_query_param_uint64 (&offset),
139 7 : GNUNET_PQ_query_param_bool (return_suppressed),
140 7 : GNUNET_PQ_query_param_uint64 (&plimit),
141 : GNUNET_PQ_query_param_end
142 : };
143 7 : struct CoinInconsistencyContext dcc = {
144 : .cb = cb,
145 : .cb_cls = cb_cls,
146 : .pg = pg
147 : };
148 : enum GNUNET_DB_QueryStatus qs;
149 :
150 7 : PREPARE (pg,
151 : "iterate_coin_inconsistencies_inconsistency_select_desc",
152 : "SELECT"
153 : " row_id"
154 : ",operation"
155 : ",exchange_amount"
156 : ",auditor_amount"
157 : ",coin_pub"
158 : ",profitable::INT8 AS profitable"
159 : ",suppressed"
160 : " FROM auditor_coin_inconsistency"
161 : " WHERE (row_id < $1)"
162 : " AND ($2 OR suppressed is false)"
163 : " ORDER BY row_id DESC"
164 : " LIMIT $3"
165 : );
166 7 : PREPARE (pg,
167 : "iterate_coin_inconsistencies_inconsistency_select_asc",
168 : "SELECT"
169 : " row_id"
170 : ",operation"
171 : ",exchange_amount"
172 : ",auditor_amount"
173 : ",coin_pub"
174 : ",profitable::INT8 AS profitable"
175 : ",suppressed"
176 : " FROM auditor_coin_inconsistency"
177 : " WHERE (row_id > $1)"
178 : " AND ($2 OR suppressed is false)"
179 : " ORDER BY row_id ASC"
180 : " LIMIT $3"
181 : );
182 7 : qs = GNUNET_PQ_eval_prepared_multi_select (
183 : pg->conn,
184 : (limit > 0)
185 : ? "iterate_coin_inconsistencies_inconsistency_select_asc"
186 : : "iterate_coin_inconsistencies_inconsistency_select_desc",
187 : params,
188 : &coin_inconsistency_cb,
189 : &dcc);
190 7 : if (qs > 0)
191 1 : return dcc.qs;
192 6 : GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
193 6 : return qs;
194 : }
|