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/select_recoup_above_serial_id.c
18 : * @brief Implementation of the select_recoup_above_serial_id function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "exchange-database/select_recoup_above_serial_id.h"
23 : #include "helper.h"
24 :
25 :
26 : /**
27 : * Closure for #recoup_serial_helper_cb().
28 : */
29 : struct RecoupSerialContext
30 : {
31 :
32 : /**
33 : * Callback to call.
34 : */
35 : TALER_EXCHANGEDB_RecoupCallback 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 RecoupSerialContext`
59 : * @param result the postgres result
60 : * @param num_results the number of results in @a result
61 : */
62 : static void
63 8 : recoup_serial_helper_cb (void *cls,
64 : PGresult *result,
65 : unsigned int num_results)
66 : {
67 8 : struct RecoupSerialContext *psc = cls;
68 8 : struct TALER_EXCHANGEDB_PostgresContext *pg = psc->pg;
69 :
70 8 : for (unsigned int i = 0; i<num_results; i++)
71 : {
72 : uint64_t rowid;
73 : struct TALER_ReservePublicKeyP reserve_pub;
74 : struct TALER_CoinPublicInfo coin;
75 : struct TALER_CoinSpendSignatureP coin_sig;
76 : union GNUNET_CRYPTO_BlindingSecretP coin_blind;
77 : struct TALER_Amount amount;
78 : struct TALER_DenominationPublicKey denom_pub;
79 : struct GNUNET_TIME_Timestamp timestamp;
80 0 : struct GNUNET_PQ_ResultSpec rs[] = {
81 0 : GNUNET_PQ_result_spec_uint64 ("recoup_uuid",
82 : &rowid),
83 0 : GNUNET_PQ_result_spec_timestamp ("recoup_timestamp",
84 : ×tamp),
85 0 : GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
86 : &reserve_pub),
87 0 : GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
88 : &coin.coin_pub),
89 0 : TALER_PQ_result_spec_denom_pub ("denom_pub",
90 : &denom_pub),
91 0 : GNUNET_PQ_result_spec_auto_from_type ("coin_sig",
92 : &coin_sig),
93 0 : GNUNET_PQ_result_spec_auto_from_type ("coin_blind",
94 : &coin_blind),
95 0 : GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash",
96 : &coin.denom_pub_hash),
97 0 : GNUNET_PQ_result_spec_allow_null (
98 : GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash",
99 : &coin.h_age_commitment),
100 : &coin.no_age_commitment),
101 0 : TALER_PQ_result_spec_denom_sig ("denom_sig",
102 : &coin.denom_sig),
103 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
104 : &amount),
105 : GNUNET_PQ_result_spec_end
106 : };
107 : int ret;
108 :
109 0 : if (GNUNET_OK !=
110 0 : GNUNET_PQ_extract_result (result,
111 : rs,
112 : i))
113 : {
114 0 : GNUNET_break (0);
115 0 : psc->status = GNUNET_SYSERR;
116 0 : return;
117 : }
118 0 : ret = psc->cb (psc->cb_cls,
119 : rowid,
120 : timestamp,
121 : &amount,
122 : &reserve_pub,
123 : &coin,
124 : &denom_pub,
125 : &coin_sig,
126 : &coin_blind);
127 0 : GNUNET_PQ_cleanup_result (rs);
128 0 : if (GNUNET_OK != ret)
129 0 : break;
130 : }
131 : }
132 :
133 :
134 : enum GNUNET_DB_QueryStatus
135 8 : TALER_EXCHANGEDB_select_recoup_above_serial_id (
136 : struct TALER_EXCHANGEDB_PostgresContext *pg,
137 : uint64_t serial_id,
138 : TALER_EXCHANGEDB_RecoupCallback cb,
139 : void *cb_cls)
140 : {
141 8 : struct GNUNET_PQ_QueryParam params[] = {
142 8 : GNUNET_PQ_query_param_uint64 (&serial_id),
143 : GNUNET_PQ_query_param_end
144 : };
145 8 : struct RecoupSerialContext psc = {
146 : .cb = cb,
147 : .cb_cls = cb_cls,
148 : .pg = pg,
149 : .status = GNUNET_OK
150 : };
151 : enum GNUNET_DB_QueryStatus qs;
152 :
153 8 : PREPARE (pg,
154 : "recoup_get_incr",
155 : "SELECT"
156 : " recoup_uuid"
157 : ",recoup_timestamp"
158 : ",withdraw.reserve_pub"
159 : ",coins.coin_pub"
160 : ",coin_sig"
161 : ",coin_blind"
162 : ",denoms.denom_pub_hash"
163 : ",coins.denom_sig"
164 : ",coins.age_commitment_hash"
165 : ",denoms.denom_pub"
166 : ",amount"
167 : " FROM recoup"
168 : " JOIN known_coins coins"
169 : " USING (coin_pub)"
170 : " JOIN withdraw"
171 : " USING (withdraw_id)"
172 : " JOIN denominations denoms"
173 : " ON (coins.denominations_serial = denoms.denominations_serial)"
174 : " WHERE recoup_uuid>=$1"
175 : " ORDER BY recoup_uuid ASC;");
176 8 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
177 : "recoup_get_incr",
178 : params,
179 : &recoup_serial_helper_cb,
180 : &psc);
181 8 : if (GNUNET_OK != psc.status)
182 0 : return GNUNET_DB_STATUS_HARD_ERROR;
183 8 : return qs;
184 : }
|