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_refunds_above_serial_id.c
18 : * @brief Implementation of the iterate_refunds_above_serial_id function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_error_codes.h"
22 : #include "taler/taler_pq_lib.h"
23 : #include "exchange-database/iterate_refunds_above_serial_id.h"
24 : #include "helper.h"
25 :
26 : /**
27 : * Closure for #refunds_serial_helper_cb().
28 : */
29 : struct RefundsSerialContext
30 : {
31 :
32 : /**
33 : * Callback to call.
34 : */
35 : TALER_EXCHANGEDB_RefundCallback 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 RefundsSerialContext`
59 : * @param result the postgres result
60 : * @param num_results the number of results in @a result
61 : */
62 : static void
63 9 : refunds_serial_helper_cb (void *cls,
64 : PGresult *result,
65 : unsigned int num_results)
66 : {
67 9 : struct RefundsSerialContext *rsc = cls;
68 9 : struct TALER_EXCHANGEDB_PostgresContext *pg = rsc->pg;
69 :
70 13 : for (unsigned int i = 0; i<num_results; i++)
71 : {
72 : struct TALER_EXCHANGEDB_Refund refund;
73 : struct TALER_DenominationPublicKey denom_pub;
74 : uint64_t rowid;
75 : bool full_refund;
76 5 : struct GNUNET_PQ_ResultSpec rs[] = {
77 5 : GNUNET_PQ_result_spec_auto_from_type ("merchant_pub",
78 : &refund.details.merchant_pub),
79 5 : GNUNET_PQ_result_spec_auto_from_type ("merchant_sig",
80 : &refund.details.merchant_sig),
81 5 : GNUNET_PQ_result_spec_auto_from_type ("h_contract_terms",
82 : &refund.details.h_contract_terms),
83 5 : GNUNET_PQ_result_spec_uint64 ("rtransaction_id",
84 : &refund.details.rtransaction_id),
85 5 : TALER_PQ_result_spec_denom_pub ("denom_pub",
86 : &denom_pub),
87 5 : GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
88 : &refund.coin.coin_pub),
89 5 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
90 : &refund.details.refund_amount),
91 5 : GNUNET_PQ_result_spec_uint64 ("refund_serial_id",
92 : &rowid),
93 : GNUNET_PQ_result_spec_end
94 : };
95 : enum GNUNET_GenericReturnValue ret;
96 :
97 5 : if (GNUNET_OK !=
98 5 : GNUNET_PQ_extract_result (result,
99 : rs,
100 : i))
101 : {
102 0 : GNUNET_break (0);
103 0 : rsc->status = GNUNET_SYSERR;
104 0 : return;
105 : }
106 : {
107 5 : struct GNUNET_PQ_QueryParam params[] = {
108 5 : GNUNET_PQ_query_param_uint64 (&rowid),
109 : GNUNET_PQ_query_param_end
110 : };
111 : struct TALER_Amount amount_with_fee;
112 : uint64_t s_f;
113 : uint64_t s_v;
114 5 : struct GNUNET_PQ_ResultSpec rs2[] = {
115 5 : GNUNET_PQ_result_spec_uint64 ("s_v",
116 : &s_v),
117 5 : GNUNET_PQ_result_spec_uint64 ("s_f",
118 : &s_f),
119 5 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
120 : &amount_with_fee),
121 : GNUNET_PQ_result_spec_end
122 : };
123 : enum GNUNET_DB_QueryStatus qs;
124 :
125 5 : qs = GNUNET_PQ_eval_prepared_singleton_select (
126 : pg->conn,
127 : "iterate_refunds_above_serial_id_refund_full",
128 : params,
129 : rs2);
130 5 : if (qs <= 0)
131 : {
132 0 : GNUNET_break (0);
133 0 : rsc->status = GNUNET_SYSERR;
134 0 : return;
135 : }
136 : /* normalize */
137 5 : s_v += s_f / TALER_AMOUNT_FRAC_BASE;
138 5 : s_f %= TALER_AMOUNT_FRAC_BASE;
139 10 : full_refund = (s_v > amount_with_fee.value) ||
140 5 : ( (s_v == amount_with_fee.value) &&
141 0 : (s_f >= amount_with_fee.fraction) );
142 : }
143 5 : ret = rsc->cb (rsc->cb_cls,
144 : rowid,
145 : &denom_pub,
146 : &refund.coin.coin_pub,
147 : &refund.details.merchant_pub,
148 : &refund.details.merchant_sig,
149 : &refund.details.h_contract_terms,
150 : refund.details.rtransaction_id,
151 : full_refund,
152 : &refund.details.refund_amount);
153 5 : GNUNET_PQ_cleanup_result (rs);
154 5 : if (GNUNET_OK != ret)
155 1 : break;
156 : }
157 : }
158 :
159 :
160 : enum GNUNET_DB_QueryStatus
161 9 : TALER_EXCHANGEDB_iterate_refunds_above_serial_id (
162 : struct TALER_EXCHANGEDB_PostgresContext *pg,
163 : uint64_t serial_id,
164 : TALER_EXCHANGEDB_RefundCallback cb,
165 : void *cb_cls)
166 : {
167 9 : struct GNUNET_PQ_QueryParam params[] = {
168 9 : GNUNET_PQ_query_param_uint64 (&serial_id),
169 : GNUNET_PQ_query_param_end
170 : };
171 9 : struct RefundsSerialContext rsc = {
172 : .cb = cb,
173 : .cb_cls = cb_cls,
174 : .pg = pg,
175 : .status = GNUNET_OK
176 : };
177 : enum GNUNET_DB_QueryStatus qs;
178 :
179 : /* Fetch refunds with rowid '\geq' the given parameter */
180 9 : PREPARE (pg,
181 : "iterate_refunds_above_serial_id_incr",
182 : "SELECT"
183 : " bdep.merchant_pub"
184 : ",ref.merchant_sig"
185 : ",bdep.h_contract_terms"
186 : ",ref.rtransaction_id"
187 : ",denom.denom_pub"
188 : ",kc.coin_pub"
189 : ",ref.amount_with_fee"
190 : ",ref.refund_serial_id"
191 : " FROM refunds ref"
192 : " JOIN batch_deposits bdep"
193 : " ON (ref.batch_deposit_serial_id=bdep.batch_deposit_serial_id)"
194 : " JOIN coin_deposits cdep"
195 : " ON (ref.coin_pub=cdep.coin_pub AND ref.batch_deposit_serial_id=cdep.batch_deposit_serial_id)"
196 : " JOIN known_coins kc"
197 : " ON (cdep.coin_pub=kc.coin_pub)"
198 : " JOIN denominations denom"
199 : " ON (kc.denominations_serial=denom.denominations_serial)"
200 : " WHERE ref.refund_serial_id>=$1"
201 : " ORDER BY ref.refund_serial_id ASC;");
202 9 : PREPARE (pg,
203 : "iterate_refunds_above_serial_id_refund_full",
204 : "SELECT"
205 : " CAST(SUM(CAST((ref.amount_with_fee).frac AS INT8)) AS INT8) AS s_f"
206 : ",CAST(SUM((ref.amount_with_fee).val) AS INT8) AS s_v"
207 : ",cdep.amount_with_fee"
208 : " FROM refunds ref"
209 : " JOIN coin_deposits cdep"
210 : " ON (ref.coin_pub=cdep.coin_pub AND ref.batch_deposit_serial_id=cdep.batch_deposit_serial_id)"
211 : " WHERE ref.refund_serial_id=$1"
212 : " GROUP BY (cdep.amount_with_fee);");
213 9 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
214 : "iterate_refunds_above_serial_id_incr",
215 : params,
216 : &refunds_serial_helper_cb,
217 : &rsc);
218 9 : if (GNUNET_OK != rsc.status)
219 0 : return GNUNET_DB_STATUS_HARD_ERROR;
220 9 : return qs;
221 : }
|