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/have_deposit2.c
18 : * @brief Implementation of the have_deposit2 function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "exchange-database/have_deposit2.h"
23 : #include "helper.h"
24 :
25 :
26 : enum GNUNET_DB_QueryStatus
27 0 : TALER_EXCHANGEDB_have_deposit2 (
28 : struct TALER_EXCHANGEDB_PostgresContext *pg,
29 : const struct TALER_PrivateContractHashP *h_contract_terms,
30 : const struct TALER_MerchantWireHashP *h_wire,
31 : const struct TALER_CoinSpendPublicKeyP *coin_pub,
32 : const struct TALER_MerchantPublicKeyP *merchant,
33 : struct GNUNET_TIME_Timestamp refund_deadline,
34 : struct TALER_Amount *deposit_fee,
35 : struct GNUNET_TIME_Timestamp *exchange_timestamp)
36 : {
37 0 : struct GNUNET_PQ_QueryParam params[] = {
38 0 : GNUNET_PQ_query_param_auto_from_type (coin_pub),
39 0 : GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
40 0 : GNUNET_PQ_query_param_auto_from_type (merchant),
41 : GNUNET_PQ_query_param_end
42 : };
43 : struct TALER_EXCHANGEDB_Deposit deposit2;
44 0 : struct GNUNET_PQ_ResultSpec rs[] = {
45 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
46 : &deposit2.amount_with_fee),
47 0 : GNUNET_PQ_result_spec_timestamp ("wallet_timestamp",
48 : &deposit2.timestamp),
49 0 : GNUNET_PQ_result_spec_timestamp ("exchange_timestamp",
50 : exchange_timestamp),
51 0 : GNUNET_PQ_result_spec_timestamp ("refund_deadline",
52 : &deposit2.refund_deadline),
53 0 : GNUNET_PQ_result_spec_timestamp ("wire_deadline",
54 : &deposit2.wire_deadline),
55 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("fee_deposit",
56 : deposit_fee),
57 0 : GNUNET_PQ_result_spec_auto_from_type ("wire_salt",
58 : &deposit2.wire_salt),
59 0 : GNUNET_PQ_result_spec_string ("receiver_wire_account",
60 : &deposit2.receiver_wire_account.full_payto),
61 : GNUNET_PQ_result_spec_end
62 : };
63 : enum GNUNET_DB_QueryStatus qs;
64 : struct TALER_MerchantWireHashP h_wire2;
65 :
66 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
67 : "Getting deposits for coin %s\n",
68 : TALER_B2S (coin_pub));
69 0 : PREPARE (pg,
70 : "get_deposit",
71 : "SELECT"
72 : " cdep.amount_with_fee"
73 : ",denominations.fee_deposit"
74 : ",bdep.wallet_timestamp"
75 : ",bdep.exchange_timestamp"
76 : ",bdep.refund_deadline"
77 : ",bdep.wire_deadline"
78 : ",bdep.h_contract_terms"
79 : ",bdep.wire_salt"
80 : ",wt.payto_uri AS receiver_wire_account"
81 : " FROM coin_deposits cdep"
82 : " JOIN batch_deposits bdep USING (batch_deposit_serial_id)"
83 : " JOIN known_coins kc ON (kc.coin_pub = cdep.coin_pub)"
84 : " JOIN denominations USING (denominations_serial)"
85 : " JOIN wire_targets wt USING (wire_target_h_payto)"
86 : " WHERE cdep.coin_pub=$1"
87 : " AND bdep.merchant_pub=$3"
88 : " AND bdep.h_contract_terms=$2;");
89 : /* Note: query might be made more efficient if we computed the 'shard'
90 : from merchant_pub and included that as a constraint on bdep! */
91 0 : qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
92 : "get_deposit",
93 : params,
94 : rs);
95 0 : if (0 >= qs)
96 0 : return qs;
97 0 : TALER_merchant_wire_signature_hash (deposit2.receiver_wire_account,
98 : &deposit2.wire_salt,
99 : &h_wire2);
100 0 : GNUNET_free (deposit2.receiver_wire_account.full_payto);
101 : /* Now we check that the other information in @a deposit
102 : also matches, and if not report inconsistencies. */
103 0 : if ( (GNUNET_TIME_timestamp_cmp (refund_deadline,
104 : !=,
105 0 : deposit2.refund_deadline)) ||
106 0 : (0 != GNUNET_memcmp (h_wire,
107 : &h_wire2) ) )
108 : {
109 : /* Inconsistencies detected! Does not match! */
110 0 : return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
111 : }
112 0 : return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
113 : }
|