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_denomination_pending.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 DenominationPendingContext
29 : {
30 :
31 : /**
32 : * Function to call for each bad sig loss.
33 : */
34 : TALER_AUDITORDB_DenominationPendingCallback 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_denomination_pending().
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 DenominationPendingContext *`
59 : * @param result the postgres result
60 : * @param num_results the number of results in @a result
61 : */
62 : static void
63 0 : denomination_pending_cb (void *cls,
64 : PGresult *result,
65 : unsigned int num_results)
66 : {
67 0 : struct DenominationPendingContext *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 : uint64_t serial_id;
73 : struct TALER_AUDITORDB_DenominationPending dc;
74 0 : struct GNUNET_PQ_ResultSpec rs[] = {
75 0 : GNUNET_PQ_result_spec_uint64 ("row_id",
76 : &serial_id),
77 0 : GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash",
78 : &dc.denom_pub_hash),
79 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("denom_balance",
80 : &dc.denom_balance),
81 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("denom_loss",
82 : &dc.denom_loss),
83 0 : GNUNET_PQ_result_spec_uint64 ("num_issued",
84 : &dc.num_issued),
85 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("denom_risk",
86 : &dc.denom_risk),
87 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("recoup_loss",
88 : &dc.recoup_loss),
89 : GNUNET_PQ_result_spec_end
90 : };
91 : enum GNUNET_GenericReturnValue rval;
92 :
93 0 : if (GNUNET_OK !=
94 0 : GNUNET_PQ_extract_result (result,
95 : rs,
96 : i))
97 : {
98 0 : GNUNET_break (0);
99 0 : dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
100 0 : return;
101 : }
102 0 : dcc->qs = i + 1;
103 0 : rval = dcc->cb (dcc->cb_cls,
104 : serial_id,
105 : &dc);
106 0 : GNUNET_PQ_cleanup_result (rs);
107 0 : if (GNUNET_OK != rval)
108 0 : break;
109 : }
110 : }
111 :
112 :
113 : enum GNUNET_DB_QueryStatus
114 0 : TALER_AUDITORDB_iterate_denomination_pending (
115 : struct TALER_AUDITORDB_PostgresContext *pg,
116 : int64_t limit,
117 : uint64_t offset,
118 : TALER_AUDITORDB_DenominationPendingCallback cb,
119 : void *cb_cls)
120 : {
121 0 : uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS,
122 : TALER_AUDITORDB_abs_limit (limit));
123 0 : struct GNUNET_PQ_QueryParam params[] = {
124 0 : GNUNET_PQ_query_param_uint64 (&offset),
125 0 : GNUNET_PQ_query_param_uint64 (&plimit),
126 : GNUNET_PQ_query_param_end
127 : };
128 0 : struct DenominationPendingContext 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_denomination_pending_desc",
137 : "SELECT"
138 : " row_id,"
139 : " denom_pub_hash,"
140 : " denom_balance,"
141 : " denom_loss,"
142 : " num_issued,"
143 : " denom_risk,"
144 : " recoup_loss"
145 : " FROM auditor_denomination_pending"
146 : " WHERE (row_id < $1)"
147 : " ORDER BY row_id DESC"
148 : " LIMIT $2"
149 : );
150 0 : PREPARE (pg,
151 : "iterate_denomination_pending_asc",
152 : "SELECT"
153 : " row_id,"
154 : " denom_pub_hash,"
155 : " denom_balance,"
156 : " denom_loss,"
157 : " num_issued,"
158 : " denom_risk,"
159 : " recoup_loss"
160 : " FROM auditor_denomination_pending"
161 : " WHERE (row_id > $1)"
162 : " ORDER BY row_id ASC"
163 : " LIMIT $2"
164 : );
165 0 : qs = GNUNET_PQ_eval_prepared_multi_select (
166 : pg->conn,
167 : (limit > 0)
168 : ? "iterate_denomination_pending_asc"
169 : : "iterate_denomination_pending_desc",
170 : params,
171 : &denomination_pending_cb,
172 : &dcc);
173 0 : if (qs > 0)
174 0 : return dcc.qs;
175 0 : GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
176 0 : return qs;
177 : }
|