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/pg_iterate_denominations.c
18 : * @brief Implementation of the iterate_denominations function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include "taler_error_codes.h"
23 : #include "taler_dbevents.h"
24 : #include "taler_pq_lib.h"
25 : #include "pg_iterate_denominations.h"
26 : #include "pg_helper.h"
27 :
28 :
29 : /**
30 : * Closure for #dominations_cb_helper()
31 : */
32 : struct DenomsIteratorContext
33 : {
34 : /**
35 : * Function to call with the results.
36 : */
37 : TALER_EXCHANGEDB_DenominationsCallback cb;
38 :
39 : /**
40 : * Closure to pass to @e cb
41 : */
42 : void *cb_cls;
43 :
44 : /**
45 : * Plugin context.
46 : */
47 : struct PostgresClosure *pg;
48 : };
49 :
50 :
51 : /**
52 : * Helper function for #TEH_PG_iterate_denominations().
53 : * Calls the callback with each denomination key.
54 : *
55 : * @param cls a `struct DenomsIteratorContext`
56 : * @param result db results
57 : * @param num_results number of results in @a result
58 : */
59 : static void
60 89 : dominations_cb_helper (void *cls,
61 : PGresult *result,
62 : unsigned int num_results)
63 : {
64 89 : struct DenomsIteratorContext *dic = cls;
65 89 : struct PostgresClosure *pg = dic->pg;
66 :
67 10985 : for (unsigned int i = 0; i<num_results; i++)
68 : {
69 10896 : struct TALER_EXCHANGEDB_DenominationKeyMetaData meta = {0};
70 10896 : struct TALER_DenominationPublicKey denom_pub = {0};
71 10896 : struct TALER_MasterSignatureP master_sig = {0};
72 10896 : struct TALER_DenominationHashP h_denom_pub = {0};
73 : bool revoked;
74 10896 : struct GNUNET_PQ_ResultSpec rs[] = {
75 10896 : GNUNET_PQ_result_spec_uint64 ("denominations_serial",
76 : &meta.serial),
77 10896 : GNUNET_PQ_result_spec_auto_from_type ("master_sig",
78 : &master_sig),
79 10896 : GNUNET_PQ_result_spec_bool ("revoked",
80 : &revoked),
81 10896 : GNUNET_PQ_result_spec_timestamp ("valid_from",
82 : &meta.start),
83 10896 : GNUNET_PQ_result_spec_timestamp ("expire_withdraw",
84 : &meta.expire_withdraw),
85 10896 : GNUNET_PQ_result_spec_timestamp ("expire_deposit",
86 : &meta.expire_deposit),
87 10896 : GNUNET_PQ_result_spec_timestamp ("expire_legal",
88 : &meta.expire_legal),
89 10896 : TALER_PQ_RESULT_SPEC_AMOUNT ("coin",
90 : &meta.value),
91 10896 : TALER_PQ_RESULT_SPEC_AMOUNT ("fee_withdraw",
92 : &meta.fees.withdraw),
93 10896 : TALER_PQ_RESULT_SPEC_AMOUNT ("fee_deposit",
94 : &meta.fees.deposit),
95 10896 : TALER_PQ_RESULT_SPEC_AMOUNT ("fee_refresh",
96 : &meta.fees.refresh),
97 10896 : TALER_PQ_RESULT_SPEC_AMOUNT ("fee_refund",
98 : &meta.fees.refund),
99 10896 : TALER_PQ_result_spec_denom_pub ("denom_pub",
100 : &denom_pub),
101 10896 : GNUNET_PQ_result_spec_uint32 ("age_mask",
102 : &meta.age_mask.bits),
103 : GNUNET_PQ_result_spec_end
104 : };
105 :
106 10896 : if (GNUNET_OK !=
107 10896 : GNUNET_PQ_extract_result (result,
108 : rs,
109 : i))
110 : {
111 0 : GNUNET_break (0);
112 0 : return;
113 : }
114 :
115 : /* make sure the mask information is the same */
116 10896 : denom_pub.age_mask = meta.age_mask;
117 :
118 10896 : TALER_denom_pub_hash (&denom_pub,
119 : &h_denom_pub);
120 10896 : dic->cb (dic->cb_cls,
121 : &denom_pub,
122 : &h_denom_pub,
123 : &meta,
124 : &master_sig,
125 : revoked);
126 10896 : GNUNET_PQ_cleanup_result (rs);
127 : }
128 : }
129 :
130 :
131 : enum GNUNET_DB_QueryStatus
132 89 : TEH_PG_iterate_denominations (void *cls,
133 : TALER_EXCHANGEDB_DenominationsCallback cb,
134 : void *cb_cls)
135 : {
136 89 : struct PostgresClosure *pg = cls;
137 89 : struct GNUNET_PQ_QueryParam params[] = {
138 : GNUNET_PQ_query_param_end
139 : };
140 89 : struct DenomsIteratorContext dic = {
141 : .cb = cb,
142 : .cb_cls = cb_cls,
143 : .pg = pg
144 : };
145 :
146 89 : PREPARE (pg,
147 : "select_denominations",
148 : "SELECT"
149 : " denominations_serial"
150 : ",denominations.master_sig"
151 : ",denom_revocations_serial_id IS NOT NULL AS revoked"
152 : ",valid_from"
153 : ",expire_withdraw"
154 : ",expire_deposit"
155 : ",expire_legal"
156 : ",coin" /* value of this denom */
157 : ",fee_withdraw"
158 : ",fee_deposit"
159 : ",fee_refresh"
160 : ",fee_refund"
161 : ",denom_type"
162 : ",age_mask"
163 : ",denom_pub"
164 : " FROM denominations"
165 : " LEFT JOIN "
166 : " denomination_revocations USING (denominations_serial);");
167 89 : return GNUNET_PQ_eval_prepared_multi_select (pg->conn,
168 : "select_denominations",
169 : params,
170 : &dominations_cb_helper,
171 : &dic);
172 : }
|