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