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