Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022, 2025 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_refreshes_above_serial_id.c
18 : * @brief Implementation of the select_refreshes_above_serial_id function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "exchange-database/select_refreshes_above_serial_id.h"
23 : #include "helper.h"
24 :
25 :
26 : /**
27 : * Closure for #refreshs_serial_helper_cb().
28 : */
29 : struct RefreshsSerialContext
30 : {
31 :
32 : /**
33 : * Callback to call.
34 : */
35 : TALER_EXCHANGEDB_RefreshesCallback 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 RefreshsSerialContext`
59 : * @param result the postgres result
60 : * @param num_results the number of results in @a result
61 : */
62 : static void
63 4 : refreshs_serial_helper_cb (void *cls,
64 : PGresult *result,
65 : unsigned int num_results)
66 : {
67 4 : struct RefreshsSerialContext *rsc = cls;
68 4 : struct TALER_EXCHANGEDB_PostgresContext *pg = rsc->pg;
69 :
70 4 : for (unsigned int i = 0; i<num_results; i++)
71 : {
72 : struct TALER_DenominationPublicKey old_denom_pub;
73 : struct TALER_CoinSpendPublicKeyP coin_pub;
74 : struct TALER_CoinSpendSignatureP coin_sig;
75 : struct TALER_AgeCommitmentHashP h_age_commitment;
76 : bool ac_isnull;
77 : struct TALER_Amount amount_with_fee;
78 : uint64_t rowid;
79 : struct TALER_RefreshCommitmentP rc;
80 : size_t num_nds;
81 : uint64_t *nds;
82 0 : struct GNUNET_PQ_ResultSpec rs[] = {
83 0 : TALER_PQ_result_spec_denom_pub ("old_denom_pub",
84 : &old_denom_pub),
85 0 : GNUNET_PQ_result_spec_auto_from_type ("old_coin_pub",
86 : &coin_pub),
87 0 : GNUNET_PQ_result_spec_allow_null (
88 : GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash",
89 : &h_age_commitment),
90 : &ac_isnull),
91 0 : GNUNET_PQ_result_spec_auto_from_type ("old_coin_sig",
92 : &coin_sig),
93 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
94 : &amount_with_fee),
95 0 : GNUNET_PQ_result_spec_uint64 ("refresh_id",
96 : &rowid),
97 0 : GNUNET_PQ_result_spec_auto_from_type ("rc",
98 : &rc),
99 0 : GNUNET_PQ_result_spec_array_uint64 (pg->conn,
100 : "new_denominations_serials",
101 : &num_nds,
102 : &nds),
103 : GNUNET_PQ_result_spec_end
104 : };
105 : enum GNUNET_GenericReturnValue ret;
106 :
107 0 : if (GNUNET_OK !=
108 0 : GNUNET_PQ_extract_result (result,
109 : rs,
110 : i))
111 : {
112 0 : GNUNET_break (0);
113 0 : rsc->status = GNUNET_SYSERR;
114 0 : return;
115 : }
116 :
117 0 : ret = rsc->cb (rsc->cb_cls,
118 : rowid,
119 : &old_denom_pub,
120 : &coin_pub,
121 : &coin_sig,
122 : ac_isnull ? NULL : &h_age_commitment,
123 : &amount_with_fee,
124 : num_nds,
125 : nds,
126 : &rc);
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 4 : TALER_EXCHANGEDB_select_refreshes_above_serial_id (
136 : struct TALER_EXCHANGEDB_PostgresContext *pg,
137 : uint64_t serial_id,
138 : TALER_EXCHANGEDB_RefreshesCallback cb,
139 : void *cb_cls)
140 : {
141 4 : struct GNUNET_PQ_QueryParam params[] = {
142 4 : GNUNET_PQ_query_param_uint64 (&serial_id),
143 : GNUNET_PQ_query_param_end
144 : };
145 4 : struct RefreshsSerialContext rsc = {
146 : .cb = cb,
147 : .cb_cls = cb_cls,
148 : .pg = pg,
149 : .status = GNUNET_OK
150 : };
151 : enum GNUNET_DB_QueryStatus qs;
152 :
153 4 : PREPARE (pg,
154 : "select_refreshes_above_serial_id",
155 : "SELECT"
156 : " denom.denom_pub AS old_denom_pub"
157 : ",r.old_coin_pub"
158 : ",kc.age_commitment_hash"
159 : ",r.old_coin_sig"
160 : ",r.amount_with_fee"
161 : ",r.refresh_id"
162 : ",r.rc"
163 : ",r.denom_serials AS new_denominations_serials"
164 : " FROM refresh r"
165 : " JOIN known_coins kc"
166 : " ON (r.old_coin_pub = kc.coin_pub)"
167 : " JOIN denominations denom"
168 : " ON (kc.denominations_serial = denom.denominations_serial)"
169 : " WHERE refresh_id>=$1"
170 : " ORDER BY refresh_id ASC;");
171 4 : qs = GNUNET_PQ_eval_prepared_multi_select (
172 : pg->conn,
173 : "select_refreshes_above_serial_id",
174 : params,
175 : &refreshs_serial_helper_cb,
176 : &rsc);
177 4 : if (GNUNET_OK != rsc.status)
178 0 : return GNUNET_DB_STATUS_HARD_ERROR;
179 4 : return qs;
180 : }
|