Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022-2024 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_wire_transfers.c
18 : * @brief Implementation of the iterate_wire_transfers function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "exchange-database/iterate_wire_transfers.h"
23 : #include "helper.h"
24 :
25 : /**
26 : * Closure for #handle_wt_result.
27 : */
28 : struct WireTransferResultContext
29 : {
30 : /**
31 : * Function to call on each result.
32 : */
33 : TALER_EXCHANGEDB_AggregationDataCallback cb;
34 :
35 : /**
36 : * Closure for @e cb.
37 : */
38 : void *cb_cls;
39 :
40 : /**
41 : * Plugin context.
42 : */
43 : struct TALER_EXCHANGEDB_PostgresContext *pg;
44 :
45 : /**
46 : * Set to #GNUNET_SYSERR on serious errors.
47 : */
48 : enum GNUNET_GenericReturnValue status;
49 : };
50 :
51 :
52 : /**
53 : * Function to be called with the results of a SELECT statement
54 : * that has returned @a num_results results. Helper function
55 : * for #TALER_EXCHANGEDB_iterate_wire_transfers().
56 : *
57 : * @param cls closure of type `struct WireTransferResultContext *`
58 : * @param result the postgres result
59 : * @param num_results the number of results in @a result
60 : */
61 : static void
62 9 : handle_wt_result (void *cls,
63 : PGresult *result,
64 : unsigned int num_results)
65 : {
66 9 : struct WireTransferResultContext *ctx = cls;
67 9 : struct TALER_EXCHANGEDB_PostgresContext *pg = ctx->pg;
68 :
69 27 : for (unsigned int i = 0; i<num_results; i++)
70 : {
71 : uint64_t rowid;
72 : struct TALER_PrivateContractHashP h_contract_terms;
73 : struct TALER_CoinSpendPublicKeyP coin_pub;
74 : struct TALER_FullPaytoHashP h_payto;
75 : struct TALER_MerchantPublicKeyP merchant_pub;
76 : struct GNUNET_TIME_Timestamp exec_time;
77 : struct TALER_Amount amount_with_fee;
78 : struct TALER_Amount deposit_fee;
79 : struct TALER_DenominationPublicKey denom_pub;
80 : struct TALER_FullPayto payto_uri;
81 18 : struct TALER_FullPayto exchange_payto_uri = {
82 : .full_payto = NULL
83 : };
84 18 : struct GNUNET_PQ_ResultSpec rs[] = {
85 18 : GNUNET_PQ_result_spec_uint64 ("aggregation_serial_id",
86 : &rowid),
87 18 : GNUNET_PQ_result_spec_auto_from_type ("h_contract_terms",
88 : &h_contract_terms),
89 18 : GNUNET_PQ_result_spec_string ("payto_uri",
90 : &payto_uri.full_payto),
91 18 : GNUNET_PQ_result_spec_auto_from_type ("wire_target_h_payto",
92 : &h_payto),
93 18 : GNUNET_PQ_result_spec_allow_null (
94 : GNUNET_PQ_result_spec_string ("exchange_payto_uri",
95 : &exchange_payto_uri.full_payto),
96 : NULL),
97 18 : TALER_PQ_result_spec_denom_pub ("denom_pub",
98 : &denom_pub),
99 18 : GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
100 : &coin_pub),
101 18 : GNUNET_PQ_result_spec_auto_from_type ("merchant_pub",
102 : &merchant_pub),
103 18 : GNUNET_PQ_result_spec_timestamp ("execution_date",
104 : &exec_time),
105 18 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
106 : &amount_with_fee),
107 18 : TALER_PQ_RESULT_SPEC_AMOUNT ("fee_deposit",
108 : &deposit_fee),
109 : GNUNET_PQ_result_spec_end
110 : };
111 :
112 18 : if (GNUNET_OK !=
113 18 : GNUNET_PQ_extract_result (result,
114 : rs,
115 : i))
116 : {
117 0 : GNUNET_break (0);
118 0 : ctx->status = GNUNET_SYSERR;
119 0 : return;
120 : }
121 18 : ctx->cb (ctx->cb_cls,
122 : rowid,
123 : &merchant_pub,
124 : payto_uri,
125 : &h_payto,
126 : exchange_payto_uri,
127 : exec_time,
128 : &h_contract_terms,
129 : &denom_pub,
130 : &coin_pub,
131 : &amount_with_fee,
132 : &deposit_fee);
133 18 : GNUNET_PQ_cleanup_result (rs);
134 : }
135 : }
136 :
137 :
138 : enum GNUNET_DB_QueryStatus
139 9 : TALER_EXCHANGEDB_iterate_wire_transfers (
140 : struct TALER_EXCHANGEDB_PostgresContext *pg,
141 : const struct TALER_WireTransferIdentifierRawP *wtid,
142 : TALER_EXCHANGEDB_AggregationDataCallback cb,
143 : void *cb_cls)
144 : {
145 9 : struct GNUNET_PQ_QueryParam params[] = {
146 9 : GNUNET_PQ_query_param_auto_from_type (wtid),
147 : GNUNET_PQ_query_param_end
148 : };
149 9 : struct WireTransferResultContext ctx = {
150 : .cb = cb,
151 : .cb_cls = cb_cls,
152 : .pg = pg,
153 : .status = GNUNET_OK
154 : };
155 : enum GNUNET_DB_QueryStatus qs;
156 :
157 9 : PREPARE (pg,
158 : "iterate_wire_transfers",
159 : "SELECT"
160 : " aggregation_serial_id"
161 : ",bdep.h_contract_terms"
162 : ",payto_uri"
163 : ",wt.wire_target_h_payto"
164 : ",kc.coin_pub"
165 : ",bdep.merchant_pub"
166 : ",wo.execution_date"
167 : ",cdep.amount_with_fee"
168 : ",denom.fee_deposit"
169 : ",denom.denom_pub"
170 : ",wo.exchange_payto_uri"
171 : " FROM aggregation_tracking"
172 : " JOIN batch_deposits bdep"
173 : " USING (batch_deposit_serial_id)"
174 : " JOIN coin_deposits cdep"
175 : " USING (batch_deposit_serial_id)"
176 : " JOIN wire_targets wt"
177 : " USING (wire_target_h_payto)"
178 : " JOIN known_coins kc"
179 : " USING (coin_pub)"
180 : " JOIN denominations denom"
181 : " USING (denominations_serial)"
182 : " JOIN wire_out wo"
183 : " USING (wtid_raw)"
184 : " WHERE wtid_raw=$1;");
185 9 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
186 : "iterate_wire_transfers",
187 : params,
188 : &handle_wt_result,
189 : &ctx);
190 9 : if (GNUNET_OK != ctx.status)
191 0 : return GNUNET_DB_STATUS_HARD_ERROR;
192 9 : return qs;
193 : }
|