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/lookup_wire_transfer.c
18 : * @brief Implementation of the lookup_wire_transfer function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "exchange-database/lookup_wire_transfer.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_lookup_wire_transfer().
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 6 : handle_wt_result (void *cls,
63 : PGresult *result,
64 : unsigned int num_results)
65 : {
66 6 : struct WireTransferResultContext *ctx = cls;
67 6 : struct TALER_EXCHANGEDB_PostgresContext *pg = ctx->pg;
68 :
69 23 : 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 17 : struct GNUNET_PQ_ResultSpec rs[] = {
82 17 : GNUNET_PQ_result_spec_uint64 ("aggregation_serial_id",
83 : &rowid),
84 17 : GNUNET_PQ_result_spec_auto_from_type ("h_contract_terms",
85 : &h_contract_terms),
86 17 : GNUNET_PQ_result_spec_string ("payto_uri",
87 : &payto_uri.full_payto),
88 17 : GNUNET_PQ_result_spec_auto_from_type ("wire_target_h_payto",
89 : &h_payto),
90 17 : TALER_PQ_result_spec_denom_pub ("denom_pub",
91 : &denom_pub),
92 17 : GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
93 : &coin_pub),
94 17 : GNUNET_PQ_result_spec_auto_from_type ("merchant_pub",
95 : &merchant_pub),
96 17 : GNUNET_PQ_result_spec_timestamp ("execution_date",
97 : &exec_time),
98 17 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
99 : &amount_with_fee),
100 17 : TALER_PQ_RESULT_SPEC_AMOUNT ("fee_deposit",
101 : &deposit_fee),
102 : GNUNET_PQ_result_spec_end
103 : };
104 :
105 17 : if (GNUNET_OK !=
106 17 : GNUNET_PQ_extract_result (result,
107 : rs,
108 : i))
109 : {
110 0 : GNUNET_break (0);
111 0 : ctx->status = GNUNET_SYSERR;
112 0 : return;
113 : }
114 17 : ctx->cb (ctx->cb_cls,
115 : rowid,
116 : &merchant_pub,
117 : payto_uri,
118 : &h_payto,
119 : exec_time,
120 : &h_contract_terms,
121 : &denom_pub,
122 : &coin_pub,
123 : &amount_with_fee,
124 : &deposit_fee);
125 17 : GNUNET_PQ_cleanup_result (rs);
126 : }
127 : }
128 :
129 :
130 : enum GNUNET_DB_QueryStatus
131 6 : TALER_EXCHANGEDB_lookup_wire_transfer (
132 : struct TALER_EXCHANGEDB_PostgresContext *pg,
133 : const struct TALER_WireTransferIdentifierRawP *wtid,
134 : TALER_EXCHANGEDB_AggregationDataCallback cb,
135 : void *cb_cls)
136 : {
137 6 : struct GNUNET_PQ_QueryParam params[] = {
138 6 : GNUNET_PQ_query_param_auto_from_type (wtid),
139 : GNUNET_PQ_query_param_end
140 : };
141 6 : struct WireTransferResultContext ctx = {
142 : .cb = cb,
143 : .cb_cls = cb_cls,
144 : .pg = pg,
145 : .status = GNUNET_OK
146 : };
147 : enum GNUNET_DB_QueryStatus qs;
148 :
149 6 : PREPARE (pg,
150 : "lookup_transactions",
151 : "SELECT"
152 : " aggregation_serial_id"
153 : ",bdep.h_contract_terms"
154 : ",payto_uri"
155 : ",wt.wire_target_h_payto"
156 : ",kc.coin_pub"
157 : ",bdep.merchant_pub"
158 : ",wire_out.execution_date"
159 : ",cdep.amount_with_fee"
160 : ",denom.fee_deposit"
161 : ",denom.denom_pub"
162 : " FROM aggregation_tracking"
163 : " JOIN batch_deposits bdep"
164 : " USING (batch_deposit_serial_id)"
165 : " JOIN coin_deposits cdep"
166 : " USING (batch_deposit_serial_id)"
167 : " JOIN wire_targets wt"
168 : " USING (wire_target_h_payto)"
169 : " JOIN known_coins kc"
170 : " USING (coin_pub)"
171 : " JOIN denominations denom"
172 : " USING (denominations_serial)"
173 : " JOIN wire_out"
174 : " USING (wtid_raw)"
175 : " WHERE wtid_raw=$1;");
176 6 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
177 : "lookup_transactions",
178 : params,
179 : &handle_wt_result,
180 : &ctx);
181 6 : if (GNUNET_OK != ctx.status)
182 0 : return GNUNET_DB_STATUS_HARD_ERROR;
183 6 : return qs;
184 : }
|