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_reserves.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 ReservesContext
29 : {
30 :
31 : /**
32 : * Function to call for each bad sig loss.
33 : */
34 : TALER_AUDITORDB_ReservesCallback 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_reserves().
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 ReservesContext *`
59 : * @param result the postgres result
60 : * @param num_results the number of results in @a result
61 : */
62 : static void
63 0 : reserves_cb (void *cls,
64 : PGresult *result,
65 : unsigned int num_results)
66 : {
67 0 : struct ReservesContext *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_Reserves dc;
73 0 : struct GNUNET_PQ_ResultSpec rs[] = {
74 0 : GNUNET_PQ_result_spec_uint64 ("auditor_reserves_rowid",
75 : &dc.auditor_reserves_rowid),
76 0 : GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
77 : &dc.reserve_pub),
78 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("reserve_balance",
79 : &dc.reserve_balance),
80 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("reserve_loss",
81 : &dc.reserve_loss),
82 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("withdraw_fee_balance",
83 : &dc.withdraw_fee_balance),
84 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("close_fee_balance",
85 : &dc.close_fee_balance),
86 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("purse_fee_balance",
87 : &dc.purse_fee_balance),
88 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("open_fee_balance",
89 : &dc.open_fee_balance),
90 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("history_fee_balance",
91 : &dc.history_fee_balance),
92 0 : GNUNET_PQ_result_spec_absolute_time ("expiration_date",
93 : &dc.expiration_date),
94 0 : GNUNET_PQ_result_spec_allow_null (
95 : GNUNET_PQ_result_spec_string ("origin_account",
96 : &dc.origin_account.full_payto),
97 : NULL),
98 : GNUNET_PQ_result_spec_end
99 : };
100 : enum GNUNET_GenericReturnValue rval;
101 :
102 0 : dc.origin_account.full_payto = NULL;
103 0 : if (GNUNET_OK !=
104 0 : GNUNET_PQ_extract_result (result,
105 : rs,
106 : i))
107 : {
108 0 : GNUNET_break (0);
109 0 : dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
110 0 : return;
111 : }
112 0 : dcc->qs = i + 1;
113 0 : rval = dcc->cb (dcc->cb_cls,
114 : dc.auditor_reserves_rowid,
115 : &dc);
116 0 : GNUNET_PQ_cleanup_result (rs);
117 0 : if (GNUNET_OK != rval)
118 0 : break;
119 : }
120 : }
121 :
122 :
123 : enum GNUNET_DB_QueryStatus
124 0 : TALER_AUDITORDB_iterate_reserves (struct TALER_AUDITORDB_PostgresContext *pg,
125 : int64_t limit,
126 : uint64_t offset,
127 : TALER_AUDITORDB_ReservesCallback cb,
128 : void *cb_cls)
129 : {
130 0 : uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS,
131 : TALER_AUDITORDB_abs_limit (limit));
132 0 : struct GNUNET_PQ_QueryParam params[] = {
133 0 : GNUNET_PQ_query_param_uint64 (&offset),
134 0 : GNUNET_PQ_query_param_uint64 (&plimit),
135 : GNUNET_PQ_query_param_end
136 : };
137 0 : struct ReservesContext dcc = {
138 : .cb = cb,
139 : .cb_cls = cb_cls,
140 : .pg = pg
141 : };
142 : enum GNUNET_DB_QueryStatus qs;
143 :
144 0 : PREPARE (pg,
145 : "iterate_reserves_desc",
146 : "SELECT"
147 : " auditor_reserves_rowid,"
148 : " reserve_pub,"
149 : " reserve_balance,"
150 : " reserve_loss,"
151 : " withdraw_fee_balance,"
152 : " close_fee_balance,"
153 : " purse_fee_balance,"
154 : " open_fee_balance,"
155 : " history_fee_balance,"
156 : " expiration_date,"
157 : " origin_account"
158 : " FROM auditor_reserves"
159 : " WHERE (auditor_reserves_rowid < $1)"
160 : " ORDER BY auditor_reserves_rowid DESC"
161 : " LIMIT $2"
162 : );
163 0 : PREPARE (pg,
164 : "iterate_reserves_asc",
165 : "SELECT"
166 : " auditor_reserves_rowid,"
167 : " reserve_pub,"
168 : " reserve_balance,"
169 : " reserve_loss,"
170 : " withdraw_fee_balance,"
171 : " close_fee_balance,"
172 : " purse_fee_balance,"
173 : " open_fee_balance,"
174 : " history_fee_balance,"
175 : " expiration_date,"
176 : " origin_account"
177 : " FROM auditor_reserves"
178 : " WHERE (auditor_reserves_rowid > $1)"
179 : " ORDER BY auditor_reserves_rowid ASC"
180 : " LIMIT $2"
181 : );
182 0 : qs = GNUNET_PQ_eval_prepared_multi_select (
183 : pg->conn,
184 : (limit > 0)
185 : ? "iterate_reserves_asc"
186 : : "iterate_reserves_desc",
187 : params,
188 : &reserves_cb,
189 : &dcc);
190 0 : if (qs > 0)
191 0 : return dcc.qs;
192 0 : GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
193 0 : return qs;
194 : }
|