Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022 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 : /**
17 : * @file exchangedb/iterate_withdrawals_above_serial_id.c
18 : * @brief Implementation of the iterate_withdrawals_above_serial_id function for Postgres
19 : * @author Christian Grothoff
20 : * @author Özgür Kesim
21 : */
22 : #include "taler/taler_pq_lib.h"
23 : #include "exchange-database/iterate_withdrawals_above_serial_id.h"
24 : #include "helper.h"
25 :
26 : /**
27 : * Closure for #withdraw_serial_helper_cb().
28 : */
29 : struct WithdrawSerialContext
30 : {
31 :
32 : /**
33 : * Callback to call.
34 : */
35 : TALER_EXCHANGEDB_WithdrawCallback cb;
36 :
37 : /**
38 : * Closure for @e cb.
39 : */
40 : void *cb_cls;
41 :
42 : /**
43 : * Plugin context.
44 : */
45 : struct TALER_EXCHANGEDB_PostgresContext *pg;
46 :
47 : /**
48 : * Status code, set to #GNUNET_SYSERR on hard errors.
49 : */
50 : enum GNUNET_GenericReturnValue status;
51 : };
52 :
53 :
54 : /**
55 : * Helper function 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 WithdrawSerialContext`
59 : * @param result the postgres result
60 : * @param num_results the number of results in @a result
61 : */
62 : static void
63 14 : withdraw_serial_helper_cb (void *cls,
64 : PGresult *result,
65 : unsigned int num_results)
66 : {
67 14 : struct WithdrawSerialContext *rosc = cls;
68 14 : struct TALER_EXCHANGEDB_PostgresContext *pg = rosc->pg;
69 :
70 15 : for (unsigned int i = 0; i<num_results; i++)
71 : {
72 : uint64_t rowid;
73 : struct TALER_HashBlindedPlanchetsP h_planchets;
74 : struct GNUNET_TIME_Timestamp execution_date;
75 : struct TALER_Amount amount_with_fee;
76 : struct TALER_ReservePublicKeyP reserve_pub;
77 : struct TALER_ReserveSignatureP reserve_sig;
78 : uint16_t max_age;
79 : bool no_max_age;
80 : uint16_t noreveal_index;
81 : bool no_noreveal_index;
82 : struct TALER_HashBlindedPlanchetsP selected_h;
83 : bool no_selected_h;
84 : struct TALER_BlindingMasterSeedP blinding_seed;
85 : bool no_blinding_seed;
86 : size_t num_denom_serials;
87 2 : uint64_t *denom_serials = NULL;
88 2 : struct GNUNET_PQ_ResultSpec rs[] = {
89 2 : GNUNET_PQ_result_spec_uint64 ("withdraw_id",
90 : &rowid),
91 2 : GNUNET_PQ_result_spec_auto_from_type ("planchets_h",
92 : &h_planchets),
93 2 : GNUNET_PQ_result_spec_timestamp ("execution_date",
94 : &execution_date),
95 2 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
96 : &amount_with_fee),
97 2 : GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
98 : &reserve_pub),
99 2 : GNUNET_PQ_result_spec_auto_from_type ("reserve_sig",
100 : &reserve_sig),
101 2 : GNUNET_PQ_result_spec_allow_null (
102 : GNUNET_PQ_result_spec_uint16 ("max_age",
103 : &max_age),
104 : &no_max_age),
105 2 : GNUNET_PQ_result_spec_allow_null (
106 : GNUNET_PQ_result_spec_uint16 ("noreveal_index",
107 : &noreveal_index),
108 : &no_noreveal_index),
109 2 : GNUNET_PQ_result_spec_allow_null (
110 : GNUNET_PQ_result_spec_auto_from_type (
111 : "selected_h",
112 : &selected_h),
113 : &no_selected_h),
114 2 : GNUNET_PQ_result_spec_allow_null (
115 : GNUNET_PQ_result_spec_auto_from_type (
116 : "blinding_seed",
117 : &blinding_seed),
118 : &no_blinding_seed),
119 2 : GNUNET_PQ_result_spec_array_uint64 (
120 : pg->conn,
121 : "denom_serials",
122 : &num_denom_serials,
123 : &denom_serials),
124 : GNUNET_PQ_result_spec_end
125 : };
126 : enum GNUNET_GenericReturnValue ret;
127 :
128 2 : if (GNUNET_OK !=
129 2 : GNUNET_PQ_extract_result (result,
130 : rs,
131 : i))
132 : {
133 0 : GNUNET_break (0);
134 0 : rosc->status = GNUNET_SYSERR;
135 0 : GNUNET_PQ_cleanup_result (rs);
136 0 : return;
137 : }
138 : /* Note: here we just check that the uint8_t
139 : limitations are met, other constraints are checked later */
140 2 : if (( (! no_noreveal_index) &&
141 0 : (UINT8_MAX <= noreveal_index)) ||
142 2 : ( (! no_max_age) &&
143 0 : (UINT8_MAX <= max_age)))
144 : {
145 0 : GNUNET_break (0);
146 0 : rosc->status = GNUNET_SYSERR;
147 0 : GNUNET_PQ_cleanup_result (rs);
148 0 : return;
149 : }
150 2 : ret = rosc->cb (rosc->cb_cls,
151 : rowid,
152 : num_denom_serials,
153 : denom_serials,
154 : no_selected_h ? NULL : &selected_h,
155 : &h_planchets,
156 : no_blinding_seed ? NULL : &blinding_seed,
157 : ! no_max_age,
158 2 : (uint8_t) max_age,
159 2 : (uint8_t) noreveal_index,
160 : &reserve_pub,
161 : &reserve_sig,
162 : execution_date,
163 2 : &amount_with_fee);
164 2 : GNUNET_PQ_cleanup_result (rs);
165 2 : if (GNUNET_OK != ret)
166 1 : break;
167 : }
168 : }
169 :
170 :
171 : enum GNUNET_DB_QueryStatus
172 14 : TALER_EXCHANGEDB_iterate_withdrawals_above_serial_id (
173 : struct TALER_EXCHANGEDB_PostgresContext *pg,
174 : uint64_t serial_id,
175 : TALER_EXCHANGEDB_WithdrawCallback cb,
176 : void *cb_cls)
177 : {
178 14 : struct GNUNET_PQ_QueryParam params[] = {
179 14 : GNUNET_PQ_query_param_uint64 (&serial_id),
180 : GNUNET_PQ_query_param_end
181 : };
182 14 : struct WithdrawSerialContext rosc = {
183 : .cb = cb,
184 : .cb_cls = cb_cls,
185 : .pg = pg,
186 : .status = GNUNET_OK
187 : };
188 : enum GNUNET_DB_QueryStatus qs;
189 :
190 : /* Fetch deposits with rowid '\geq' the given parameter */
191 14 : PREPARE (pg,
192 : "iterate_withdrawals_above_serial_id",
193 : "SELECT"
194 : " withdraw_id"
195 : ",planchets_h"
196 : ",execution_date"
197 : ",amount_with_fee"
198 : ",reserve_pub"
199 : ",reserve_sig"
200 : ",max_age"
201 : ",noreveal_index"
202 : ",selected_h"
203 : ",blinding_seed"
204 : ",denom_serials"
205 : " FROM withdraw"
206 : " WHERE withdraw_id>=$1"
207 : " ORDER BY withdraw_id ASC;");
208 14 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
209 : "iterate_withdrawals_above_serial_id",
210 : params,
211 : &withdraw_serial_helper_cb,
212 : &rosc);
213 14 : if (GNUNET_OK != rosc.status)
214 0 : return GNUNET_DB_STATUS_HARD_ERROR;
215 14 : return qs;
216 : }
|