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